Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic root toolbox script for rootless nerdctl #3130

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ LIMA_CMDS = $(sort lima lima$(bat)) # $(sort ...) deduplicates the list
LIMA_DEPS = $(addprefix _output/bin/,$(LIMA_CMDS))
lima: $(LIMA_DEPS)

HELPER_CMDS = nerdctl.lima apptainer.lima docker.lima podman.lima kubectl.lima
HELPER_CMDS = nerdctl.lima apptainer.lima docker.lima podman.lima kubectl.lima toolbox.lima
HELPERS_DEPS = $(addprefix _output/bin/,$(HELPER_CMDS))
helpers: $(HELPERS_DEPS)

Expand Down Expand Up @@ -435,6 +435,7 @@ uninstall:
"$(DEST)/bin/docker.lima" \
"$(DEST)/bin/podman.lima" \
"$(DEST)/bin/kubectl.lima" \
"$(DEST)/bin/toolbox.lima" \
"$(DEST)/share/man/man1/lima.1" \
"$(DEST)/share/man/man1/limactl"*".1" \
"$(DEST)/share/lima" "$(DEST)/share/doc/lima"
Expand Down
100 changes: 100 additions & 0 deletions cmd/toolbox.lima
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/bin/bash
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite different from other *.lima wrappers, as this doesn't even execute limactl

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't like the name of the "toolbox", as it is likely to conflict with something else, e.g., https://android.googlesource.com/platform/system/core/+/froyo/toolbox/Android.mk

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@afbjorklund afbjorklund Jan 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script executes itself with lima (if needed), so it should use limactl (eventually)
But there is not so much host code in it, like checking if nerdctl is actually available etc
As mentioned in the issue, the "toolbox" is somewhat overloaded with 3+ implementations


set -e
set -o pipefail

hostname=$(hostname)

case ${hostname} in
lima-*)
# already in lima
;;
*)
exec lima "$0" "$@"
;;
esac

machine=$(uname -m)

case ${machine} in
aarch64)
PLATFORM=linux/arm64
;;
armv7l)
PLATFORM=linux/arm/v7
;;
riscv64)
PLATFORM=linux/riscv64
;;
x86_64)
PLATFORM=linux/amd64
;;
*)
echo "Warning: Unknown machine type ${machine}" >&2
exit 1
;;
esac

TOOLBOX_DOCKER_IMAGE=fedora
TOOLBOX_DOCKER_TAG=latest
TOOLBOX_USER=root
TOOLBOX_BIND="--volume=/:/media/root --volume=/usr:/media/root/usr --volume=/run:/media/root/run"
TOOLBOX_FLAGS=""
# Ex: "--env=KEY=VALUE"
TOOLBOX_ENV=""

toolboxrc="${HOME}"/.toolboxrc

# System defaults
if [ -f "/etc/default/toolbox" ]; then
# shellcheck disable=SC1091
source "/etc/default/toolbox"
fi

# User overrides
if [ -f "${toolboxrc}" ]; then
# shellcheck disable=SC1090
source "${toolboxrc}"
fi

if [[ -n "${TOOLBOX_DOCKER_IMAGE}" ]] && [[ -n "${TOOLBOX_DOCKER_TAG}" ]]; then
TOOLBOX_NAME=${TOOLBOX_DOCKER_IMAGE}-${TOOLBOX_DOCKER_TAG}
have_docker_image="y"
fi

machinename=$(echo "${USER}-${TOOLBOX_NAME}" | sed -r 's/[^a-zA-Z0-9_.-]/_/g')
machineimage="${TOOLBOX_IMAGE:-${TOOLBOX_DOCKER_IMAGE}:${TOOLBOX_DOCKER_TAG}}"
if ! nerdctl image inspect "${machineimage}" >/dev/null; then
if [[ -n "${have_docker_image}" ]]; then
nerdctl pull "${TOOLBOX_DOCKER_IMAGE}:${TOOLBOX_DOCKER_TAG}"
elif [[ -n "${TOOLBOX_DOCKER_ARCHIVE}" ]]; then
curl "${TOOLBOX_DOCKER_ARCHIVE}" | zstd -cd | nerdctl load
else
echo "Error: No toolbox image specified." >&2
exit 1
fi
fi

# Special case for when SSH tries to pass a shell command with -c
if [ "x${1-}" == x-c ]; then
set /bin/sh "$@"
fi

printf "\033[0;90m\u2591 Entering container %s on %s.\033[0m\n" "${machinename}" "${machineimage}"

# If the container already exists, we need to exec rather than run
if nerdctl container inspect "${machinename}" >/dev/null 2>&1; then
nerdctl start "${machinename}" >/dev/null 2>&1 || true
exec nerdctl exec -it "${machinename}" "${@-/bin/bash}"
fi

# shellcheck disable=SC2086
nerdctl run --platform ${PLATFORM} --interactive --tty \
--name="${machinename}" \
--hostname="${hostname}" \
--ipc host --network host --pid host \
--privileged \
${TOOLBOX_FLAGS} \
${TOOLBOX_BIND} \
${TOOLBOX_ENV} \
--user="${TOOLBOX_USER}" "${machineimage}" "$@"
Loading