Skip to content

Commit df88857

Browse files
committed
ilab-wrapper: Run podman with sudo
# Background The ilab command is wrapped by an `ilab` script which launches ilab inside a podman container. # Issue Since the ilab container image is pulled during the bootc image build process using the root user, the image is not accessible to non-root users. # Solution We run the container as sudo in order to be able to access the root container storage. But for security reasons we map root UID 0 inside the container to the current user's UID (and all the other subuids to the user's /etc/subuid range) so that we're effectively running the container as the current user. # Additional changes Changed `"--env" "HOME"` to `"--env" "HOME=$HOME"` to pass the HOME environment variable from the current shell and not from the sudo environment. # Future work In the future, we will run podman as the current user, once we figure a reasonable way for the current user to access the root's user container storage Signed-off-by: Omer Tuchfeld <[email protected]>
1 parent 151cc3b commit df88857

File tree

2 files changed

+76
-6
lines changed
  • training
    • ilab-wrapper
    • nvidia-bootc/duplicated/ilab-wrapper

2 files changed

+76
-6
lines changed

training/ilab-wrapper/ilab

+38-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
22

3+
function echo-err { echo "$@" >&2; }
4+
35
# Template values replaced by container build
46
CONTAINER_DEVICE="__REPLACE_CONTAINER_DEVICE__"
57
IMAGE_NAME="__REPLACE_IMAGE_NAME__"
@@ -8,7 +10,7 @@ export ENTRYPOINT="/opt/python3.11/venv/bin/ilab"
810
export PARAMS=("$@")
911

1012
if [[ -n "$ILAB_HOME" ]]; then
11-
HOME="$ILAB_HOME"
13+
HOME="$ILAB_HOME"
1214
fi
1315

1416
for dir in "$HOME/.cache" "$HOME/.config" "$HOME/.local"; do
@@ -40,12 +42,45 @@ for PODMAN_MOUNT in "${ADDITIONAL_MOUNTS[@]}"; do
4042
ADDITIONAL_MOUNT_OPTIONS+=("-v" "$PODMAN_MOUNT")
4143
done
4244

43-
PODMAN_COMMAND=("podman" "run" "--rm" "-it"
45+
# We run the container as sudo in order to be able to access the root container
46+
# storage, which has the ilab image pre-pulled. But for security reasons we map
47+
# root UID 0 inside the container to the current user's UID (and all the other
48+
# subuids to the user's /etc/subuid range) so that we're effectively running
49+
# the container as the current user.
50+
#
51+
# In the future, we will run podman as the current user, once we figure a
52+
# reasonable way for the current user to access the root's user container
53+
# storage.
54+
CURRENT_USER_NAME=$(id --user --name)
55+
CURRENT_USER_SUBUID_RANGE=$(awk \
56+
--field-separator ':' \
57+
--assign current_user="$CURRENT_USER_NAME" \
58+
--assign current_uid="$UID" \
59+
'$1 == current_user || $1 == current_uid {print $2 ":" $3}' \
60+
/etc/subuid)
61+
62+
# TODO: Handle multiple subuid ranges, for now, hard fail
63+
if [[ $(wc -l <<<"$CURRENT_USER_SUBUID_RANGE") != 1 ]]; then
64+
if [[ -z "$CURRENT_USER_SUBUID_RANGE" ]]; then
65+
echo-err "No subuid range found for user $CURRENT_USER_NAME ($UID)"
66+
else
67+
echo-err "Multiple subuid ranges found for user $CURRENT_USER_NAME ($UID), this is currently unsupported"
68+
echo-err "$CURRENT_USER_SUBUID_RANGE"
69+
fi
70+
exit 1
71+
fi
72+
73+
IMPERSONATE_CURRENT_USER_PODMAN_FLAGS=("--uidmap" "0:$UID" "--uidmap" "1:$CURRENT_USER_SUBUID_RANGE")
74+
75+
PODMAN_COMMAND=("sudo" "podman" "run" "--rm" "-it"
76+
"${IMPERSONATE_CURRENT_USER_PODMAN_FLAGS[@]}"
4477
"--device" "${CONTAINER_DEVICE}"
4578
"--security-opt" "label=disable" "--net" "host"
4679
"-v" "$HOME:$HOME"
4780
"${ADDITIONAL_MOUNT_OPTIONS[@]}"
48-
"--env" "HOME"
81+
# This is intentionally NOT using "--env" "HOME" because we want the HOME
82+
# of the current shell and not the HOME set by sudo
83+
"--env" "HOME=$HOME"
4984
"--entrypoint" "$ENTRYPOINT"
5085
"--env" "HF_TOKEN"
5186
"${IMAGE_NAME}")

training/nvidia-bootc/duplicated/ilab-wrapper/ilab

+38-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
22

3+
function echo-err { echo "$@" >&2; }
4+
35
# Template values replaced by container build
46
CONTAINER_DEVICE="__REPLACE_CONTAINER_DEVICE__"
57
IMAGE_NAME="__REPLACE_IMAGE_NAME__"
@@ -8,7 +10,7 @@ export ENTRYPOINT="/opt/python3.11/venv/bin/ilab"
810
export PARAMS=("$@")
911

1012
if [[ -n "$ILAB_HOME" ]]; then
11-
HOME="$ILAB_HOME"
13+
HOME="$ILAB_HOME"
1214
fi
1315

1416
for dir in "$HOME/.cache" "$HOME/.config" "$HOME/.local"; do
@@ -40,12 +42,45 @@ for PODMAN_MOUNT in "${ADDITIONAL_MOUNTS[@]}"; do
4042
ADDITIONAL_MOUNT_OPTIONS+=("-v" "$PODMAN_MOUNT")
4143
done
4244

43-
PODMAN_COMMAND=("podman" "run" "--rm" "-it"
45+
# We run the container as sudo in order to be able to access the root container
46+
# storage, which has the ilab image pre-pulled. But for security reasons we map
47+
# root UID 0 inside the container to the current user's UID (and all the other
48+
# subuids to the user's /etc/subuid range) so that we're effectively running
49+
# the container as the current user.
50+
#
51+
# In the future, we will run podman as the current user, once we figure a
52+
# reasonable way for the current user to access the root's user container
53+
# storage.
54+
CURRENT_USER_NAME=$(id --user --name)
55+
CURRENT_USER_SUBUID_RANGE=$(awk \
56+
--field-separator ':' \
57+
--assign current_user="$CURRENT_USER_NAME" \
58+
--assign current_uid="$UID" \
59+
'$1 == current_user || $1 == current_uid {print $2 ":" $3}' \
60+
/etc/subuid)
61+
62+
# TODO: Handle multiple subuid ranges, for now, hard fail
63+
if [[ $(wc -l <<<"$CURRENT_USER_SUBUID_RANGE") != 1 ]]; then
64+
if [[ -z "$CURRENT_USER_SUBUID_RANGE" ]]; then
65+
echo-err "No subuid range found for user $CURRENT_USER_NAME ($UID)"
66+
else
67+
echo-err "Multiple subuid ranges found for user $CURRENT_USER_NAME ($UID), this is currently unsupported"
68+
echo-err "$CURRENT_USER_SUBUID_RANGE"
69+
fi
70+
exit 1
71+
fi
72+
73+
IMPERSONATE_CURRENT_USER_PODMAN_FLAGS=("--uidmap" "0:$UID" "--uidmap" "1:$CURRENT_USER_SUBUID_RANGE")
74+
75+
PODMAN_COMMAND=("sudo" "podman" "run" "--rm" "-it"
76+
"${IMPERSONATE_CURRENT_USER_PODMAN_FLAGS[@]}"
4477
"--device" "${CONTAINER_DEVICE}"
4578
"--security-opt" "label=disable" "--net" "host"
4679
"-v" "$HOME:$HOME"
4780
"${ADDITIONAL_MOUNT_OPTIONS[@]}"
48-
"--env" "HOME"
81+
# This is intentionally NOT using "--env" "HOME" because we want the HOME
82+
# of the current shell and not the HOME set by sudo
83+
"--env" "HOME=$HOME"
4984
"--entrypoint" "$ENTRYPOINT"
5085
"--env" "HF_TOKEN"
5186
"${IMAGE_NAME}")

0 commit comments

Comments
 (0)