Skip to content

Commit

Permalink
Make it available inside the toolbox container
Browse files Browse the repository at this point in the history
Commit 5b3d234 had made the toolbox script work inside a
toolbox container, but most people didn't get to use it because it
needed extra effort to get access to the script inside the container.
One either had to grab the Toolbox sources or had to install the RPM.
Both options were inconvenient - the former needed knowing too many
technical details, while the latter would drag in dependencies like
Buildah and Podman that don't work inside the toolbox container.

This makes it easier to use the toolbox script inside a toolbox
container by bind mounting the script from the host inside the
container and keeping track of the path using the TOOLBOX_PATH
environment variable. The environment variable ensures that running
'toolbox create' from inside a toolbox container would continue to
bind mount the same script from the host that was used to create the
current container and is available inside it.

Compatibility with existing toolbox containers is broken when using
the script within a container because it insists on the TOOLBOX_PATH
environment variable being set inside. This might not be that big of a
deal because using the toolbox script inside a toolbox container wasn't
very convenient, and hence likely not used widely. In case of
complaints, this can be relaxed by falling back to "$0" when forwarding
the command to the host, but unless that happens it's better to keep
things simple to avoid a larger test matrix.

Based on an idea from Colin Walters.

https://github.com/debarshiray/toolbox/pull/126
  • Loading branch information
debarshiray committed Apr 24, 2019
1 parent c0f476e commit 5d78707
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions toolbox
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ base_toolbox_image=""
# https://github.com/containers/libpod/blob/master/libpod/options.go
container_name_regexp="[a-zA-Z0-9][a-zA-Z0-9_.-]*"

environment=$(set)
environment_variables="COLORTERM \
DBUS_SESSION_BUS_ADDRESS \
DBUS_SYSTEM_BUS_ADDRESS \
Expand All @@ -35,6 +36,7 @@ environment_variables="COLORTERM \
SHELL \
SSH_AUTH_SOCK \
TERM \
TOOLBOX_PATH \
VTE_VERSION \
WAYLAND_DISPLAY \
XDG_CURRENT_DESKTOP \
Expand All @@ -57,6 +59,7 @@ release_default=""
spinner_animation="[>----] [=>---] [==>--] [===>-] [====>] [----<] [---<=] [--<==] [-<===] [<====]"
spinner_template="toolbox-spinner-XXXXXXXXXX"
tab="$(printf '\t')"
toolbox_command_path=""
toolbox_container=""
toolbox_container_default=""
toolbox_container_old=""
Expand Down Expand Up @@ -322,7 +325,6 @@ create_environment_options()
echo "$environment_variables" \
| sed "s/ \+/\n/g" 2>&3 \
| {
environment=$(set)
environment_options=""
echo "$base_toolbox_command: creating list of environment variables to forward" >&3
value=""
Expand Down Expand Up @@ -703,6 +705,9 @@ create()
tmpfs_size=$((total_ram * 1024 / 2)) # bytes
fi

toolbox_path_bind="--volume $TOOLBOX_PATH:/usr/bin/toolbox:ro"
toolbox_path_set="--env TOOLBOX_PATH=$TOOLBOX_PATH"

max_uid_count=65536
max_minus_uid=$((max_uid_count - user_id_real))
uid_plus_one=$((user_id_real + 1))
Expand All @@ -721,6 +726,7 @@ create()

# shellcheck disable=SC2086
$prefix_sudo podman create \
$toolbox_path_set \
--group-add wheel \
--hostname toolbox \
--name $toolbox_container \
Expand All @@ -733,6 +739,7 @@ create()
--uidmap 0:1:"$user_id_real" \
--uidmap "$uid_plus_one":"$uid_plus_one":"$max_minus_uid" \
$kcm_socket_bind \
$toolbox_path_bind \
--volume "$HOME":"$HOME":rslave \
--volume "$XDG_RUNTIME_DIR":"$XDG_RUNTIME_DIR" \
--volume "$dbus_system_bus_path":"$dbus_system_bus_path" \
Expand Down Expand Up @@ -1147,13 +1154,13 @@ forward_to_host()
set_environment=$(create_environment_options)

echo "$base_toolbox_command: forwarding to host:" >&3
echo "$base_toolbox_command: $0" >&3
echo "$base_toolbox_command: $TOOLBOX_PATH" >&3
for i in "$@"; do
echo "$base_toolbox_command: $i" >&3
done

# shellcheck disable=SC2086
flatpak-spawn $set_environment --host "$0" "$@" 2>&3
flatpak-spawn $set_environment --host "$TOOLBOX_PATH" "$@" 2>&3
)


Expand Down Expand Up @@ -1275,6 +1282,13 @@ while has_prefix "$1" -; do
shift
done

if ! toolbox_command_path=$(realpath "$0" 2>&3); then
echo "$base_toolbox_command: failed to resolve absolute path to $0" >&2
exit 1
fi

echo "$base_toolbox_command: resolved absolute path for $0 to $toolbox_command_path" >&3

if [ -f /run/.containerenv ] 2>&3; then
if ! command -v flatpak-spawn >/dev/null 2>&3; then
echo "$base_toolbox_command: flatpak-spawn not found" >&2
Expand Down Expand Up @@ -1309,8 +1323,19 @@ if [ -f /run/.containerenv ] 2>&3; then
fi

echo "$base_toolbox_command: podman PID is $podman_pid" >&3

if [ "$TOOLBOX_PATH" = "" ] 2>&3; then
echo "$base_toolbox_command: TOOLBOX_PATH not set" >&2
exit 1
fi
else
if [ "$TOOLBOX_PATH" = "" ] 2>&3; then
TOOLBOX_PATH="$toolbox_command_path"
fi
fi

echo "$base_toolbox_command: TOOLBOX_PATH is $TOOLBOX_PATH" >&3

if [ "$1" = "" ]; then
echo "$base_toolbox_command: missing command" >&2
echo >&2
Expand Down

0 comments on commit 5d78707

Please sign in to comment.