Skip to content

Commit

Permalink
toolbox: add support for ctr usage
Browse files Browse the repository at this point in the history
Toolbox version using docker as principal CRI but with ctr fallback for
containerd systems running without docker.

Use debian:unstable-slim image as default.
  • Loading branch information
spnngl committed Oct 20, 2022
1 parent 8541a8d commit ee2289f
Showing 1 changed file with 37 additions and 15 deletions.
52 changes: 37 additions & 15 deletions toolbox
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,29 @@ set -o pipefail
machine=$(uname -m)

case ${machine} in
aarch64 )
TOOLBOX_DOCKER_IMAGE=arm64v8/fedora
TOOLBOX_DOCKER_TAG=latest
arm)
PLATFORM=linux/arm/v7
;;
x86_64 )
TOOLBOX_DOCKER_IMAGE=fedora
TOOLBOX_DOCKER_TAG=latest
aarch64)
PLATFORM=linux/arm64/v8
;;
* )
riscv64)
PLATFORM=linux/riscv64
;;
x86)
PLATFORM=linux/386
;;
x86_64)
PLATFORM=linux/amd64
;;
*)
echo "Warning: Unknown machine type ${machine}" >&2
exit 1
;;
esac

TOOLBOX_DOCKER_IMAGE=docker.io/library/debian
TOOLBOX_DOCKER_TAG=unstable-slim
TOOLBOX_USER=root
TOOLBOX_DIRECTORY="/var/lib/toolbox"
TOOLBOX_BIND="--bind=/:/media/root --bind=/usr:/media/root/usr --bind=/run:/media/root/run --bind=/sys/fs/bpf:/sys/fs/bpf"
Expand Down Expand Up @@ -48,18 +58,30 @@ fi
machinename=$(echo "${USER}-${TOOLBOX_NAME}" | sed -r 's/[^a-zA-Z0-9_.-]/_/g')
machinepath="${TOOLBOX_DIRECTORY}/${machinename}"
osrelease="${machinepath}/etc/os-release"

if [ ! -f "${osrelease}" ] || systemctl is-failed -q "${machinename}" ; then
sudo mkdir -p "${machinepath}"
sudo chown "${USER}:" "${machinepath}"

# Download and extract the image.
sudo --preserve-env docker pull "${TOOLBOX_DOCKER_IMAGE}:${TOOLBOX_DOCKER_TAG}"
dcid=$(sudo --preserve-env docker create "${TOOLBOX_DOCKER_IMAGE}:${TOOLBOX_DOCKER_TAG}")
sudo --preserve-env docker export -o "${machinepath}/${dcid}.tar" ${dcid}
sudo --preserve-env docker rm ${dcid}
sudo --preserve-env docker rmi "${TOOLBOX_DOCKER_IMAGE}:${TOOLBOX_DOCKER_TAG}" || true
sudo tar xvf "${machinepath}/${dcid}.tar" -C "${machinepath}"
sudo rm -f "${machinepath}/${dcid}.tar"
if sudo -E ctr version &> /dev/null; then
# Check iw we already got our image, do not overwrite
if ! sudo -E ctr images check | grep "${TOOLBOX_DOCKER_IMAGE}:${TOOLBOX_DOCKER_TAG}"; then
sudo -E ctr images pull --platform ${PLATFORM} ${TOOLBOX_DOCKER_IMAGE}:${TOOLBOX_DOCKER_TAG}
fi
sudo ctr images mount --rw --platform ${PLATFORM} ${TOOLBOX_DOCKER_IMAGE}:${TOOLBOX_DOCKER_TAG} ${machinepath}
elif sudo -E docker stats --no-stream &> /dev/null; then
# Download and extract the image.
sudo --preserve-env docker pull "${TOOLBOX_DOCKER_IMAGE}:${TOOLBOX_DOCKER_TAG}"
dcid=$(sudo --preserve-env docker create "${TOOLBOX_DOCKER_IMAGE}:${TOOLBOX_DOCKER_TAG}")
sudo --preserve-env docker export -o "${machinepath}/${dcid}.tar" ${dcid}
sudo --preserve-env docker rm ${dcid}
sudo --preserve-env docker rmi "${TOOLBOX_DOCKER_IMAGE}:${TOOLBOX_DOCKER_TAG}" || true
sudo tar xvf "${machinepath}/${dcid}.tar" -C "${machinepath}"
sudo rm -f "${machinepath}/${dcid}.tar"
else
echo "No supported container runtime found." >&2
exit 2
fi

sudo touch "${osrelease}"
fi
Expand Down

0 comments on commit ee2289f

Please sign in to comment.