Skip to content

Optimize containerd/nerdctl upgrader script #699

Open
@AkihiroSuda

Description

@AkihiroSuda

# Extract bin/nerdctl and compare whether it is newer than the current /usr/local/bin/nerdctl (if already exists).
# Takes 4-5 seconds. (FIXME: optimize)
tmp_extract_nerdctl="$(mktemp -d)"
tar Cxzf "${tmp_extract_nerdctl}" "${LIMA_CIDATA_MNT}"/nerdctl-full.tgz bin/nerdctl
if [ ! -f /usr/local/bin/nerdctl ] || [[ "${tmp_extract_nerdctl}"/bin/nerdctl -nt /usr/local/bin/nerdctl ]]; then
if [ -f /usr/local/bin/nerdctl ]; then
(
set +e
echo "Upgrading existing nerdctl"
echo "- Old: $(/usr/local/bin/nerdctl --version)"
echo "- New: $("${tmp_extract_nerdctl}"/bin/nerdctl --version)"
systemctl disable --now containerd buildkit stargz-snapshotter
sudo -iu "${LIMA_CIDATA_USER}" "XDG_RUNTIME_DIR=/run/user/${LIMA_CIDATA_UID}" "PATH=${PATH}" containerd-rootless-setuptool.sh uninstall
)
fi
tar Cxzf /usr/local "${LIMA_CIDATA_MNT}"/nerdctl-full.tgz
mkdir -p /etc/bash_completion.d
nerdctl completion bash >/etc/bash_completion.d/nerdctl
# TODO: enable zsh completion too
fi
rm -rf "${tmp_extract_nerdctl}"


Idea for optimization:

root@lima-default:/mnt/lima-cidata# gunzip -c nerdctl-full.tgz | head -c 512 | tar tv | awk '/ bin\/$/ {print $4}'
2022-03-03
root@lima-default:/mnt/lima-cidata# date -r /usr/local/bin/nerdctl "+%F"
2022-03-03

Then do simple string comparison on the dates.

This relies on a tar header block being 512 bytes (so tar will not throw an error), and bin/ being the first entry in the tarball. I think these are reasonable assumptions, but am not totally sure. If the first line doesn't return a non-empty string, fall back on the expensive check by extracting the tarball.

Originally posted by @jandubois in #694 (comment)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions