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 12, 2022
1 parent c12ca71 commit 4083f48
Showing 1 changed file with 38 additions and 22 deletions.
60 changes: 38 additions & 22 deletions toolbox
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@ set -o pipefail
machine=$(uname -m)

case ${machine} in
aarch64 )
TOOLBOX_DOCKER_IMAGE=arm64v8/fedora
TOOLBOX_DOCKER_TAG=latest
;;
x86_64 )
TOOLBOX_DOCKER_IMAGE=fedora
TOOLBOX_DOCKER_TAG=latest
;;
* )
echo "Warning: Unknown machine type ${machine}" >&2
;;
arm)
PLATFORM=linux/arm/v7
;;
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
;;
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,20 +57,27 @@ 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}"
sudo mkdir -p "${machinepath}"
sudo chown "${USER}:" "${machinepath}"

if 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"

# 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"
elif ctr version &> /dev/null; then
sudo --preserve-env ctr images pull --platform ${PLATFORM} ${TOOLBOX_DOCKER_IMAGE}:${TOOLBOX_DOCKER_TAG}
sudo ctr images mount --rw --platform ${PLATFORM} ${TOOLBOX_DOCKER_IMAGE}:${TOOLBOX_DOCKER_TAG} ${machinepath}
fi

sudo touch "${osrelease}"
sudo touch "${osrelease}"
fi

# Special case for when SSH tries to pass a shell command with -c
Expand Down

0 comments on commit 4083f48

Please sign in to comment.