Skip to content

Commit dc2d390

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 7409fdc commit dc2d390

File tree

2 files changed

+88
-22
lines changed
  • training
    • ilab-wrapper
    • nvidia-bootc/duplicated/ilab-wrapper

2 files changed

+88
-22
lines changed

training/ilab-wrapper/ilab

+44-11
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,54 @@ export ENTRYPOINT="/opt/python3.11/venv/bin/ilab"
88
export PARAMS=("$@")
99

1010
for dir in "$HOME/.cache" "$HOME/.config" "$HOME/.local"; do
11-
mkdir -p "$dir"
11+
mkdir -p "$dir"
1212
done
1313

1414
if [[ "$1" = "shell" ]]; then
15-
export ENTRYPOINT=bash
16-
export PARAMS=()
15+
export ENTRYPOINT=bash
16+
export PARAMS=()
1717
fi
1818

19-
PODMAN_COMMAND=("podman" "run" "--rm" "-it"
20-
"--device" "${CONTAINER_DEVICE}"
21-
"--security-opt" "label=disable" "--net" "host"
22-
"-v" "$HOME:$HOME"
23-
"--env" "HOME"
24-
"--entrypoint" "$ENTRYPOINT"
25-
"--env" "HF_TOKEN"
26-
"${IMAGE_NAME}")
19+
# We run the container as sudo in order to be able to access the root container
20+
# storage, which has the ilab image pre-pulled. But for security reasons we map
21+
# root UID 0 inside the container to the current user's UID (and all the other
22+
# subuids to the user's /etc/subuid range) so that we're effectively running
23+
# the container as the current user.
24+
#
25+
# In the future, we will run podman as the current user, once we figure a
26+
# reasonable way for the current user to access the root's user container
27+
# storage.
28+
CURRENT_USER_NAME=$(id --user --name)
29+
CURRENT_USER_SUBUID_RANGE=$(awk \
30+
--field-separator ':' \
31+
--assign current_user="$CURRENT_USER_NAME" \
32+
--assign current_uid="$UID" \
33+
'$1 == current_user || $1 == current_uid {print $2 ":" $3}' \
34+
/etc/subuid)
35+
36+
# TODO: Handle multiple subuid ranges, for now, hard fail
37+
if [[ $(echo "$CURRENT_USER_SUBUID_RANGE" | wc -l) != 1 ]]; then
38+
if [[ -z "$CURRENT_USER_SUBUID_RANGE" ]]; then
39+
echo "No subuid range found for user $CURRENT_USER_NAME ($UID)"
40+
else
41+
echo "Multiple subuid ranges found for user $CURRENT_USER_NAME ($UID):"
42+
echo "$CURRENT_USER_SUBUID_RANGE"
43+
fi
44+
exit 1
45+
fi
46+
47+
IMPERSONATE_CURRENT_USER_PODMAN_FLAGS=("--uidmap" "0:$UID" "--uidmap" "1:$CURRENT_USER_SUBUID_RANGE")
48+
49+
PODMAN_COMMAND=("sudo" "podman" "run" "--rm" "-it"
50+
"${IMPERSONATE_CURRENT_USER_PODMAN_FLAGS[@]}"
51+
"--device" "${CONTAINER_DEVICE}"
52+
"--security-opt" "label=disable" "--net" "host"
53+
"-v" "$HOME:$HOME"
54+
# This is intentionally NOT using "--env" "HOME" because we want the HOME
55+
# of the current shell and not the HOME set by sudo
56+
"--env" "HOME=$HOME"
57+
"--entrypoint" "$ENTRYPOINT"
58+
"--env" "HF_TOKEN"
59+
"${IMAGE_NAME}")
2760

2861
exec "${PODMAN_COMMAND[@]}" "${PARAMS[@]}"

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

+44-11
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,54 @@ export ENTRYPOINT="/opt/python3.11/venv/bin/ilab"
88
export PARAMS=("$@")
99

1010
for dir in "$HOME/.cache" "$HOME/.config" "$HOME/.local"; do
11-
mkdir -p "$dir"
11+
mkdir -p "$dir"
1212
done
1313

1414
if [[ "$1" = "shell" ]]; then
15-
export ENTRYPOINT=bash
16-
export PARAMS=()
15+
export ENTRYPOINT=bash
16+
export PARAMS=()
1717
fi
1818

19-
PODMAN_COMMAND=("podman" "run" "--rm" "-it"
20-
"--device" "${CONTAINER_DEVICE}"
21-
"--security-opt" "label=disable" "--net" "host"
22-
"-v" "$HOME:$HOME"
23-
"--env" "HOME"
24-
"--entrypoint" "$ENTRYPOINT"
25-
"--env" "HF_TOKEN"
26-
"${IMAGE_NAME}")
19+
# We run the container as sudo in order to be able to access the root container
20+
# storage, which has the ilab image pre-pulled. But for security reasons we map
21+
# root UID 0 inside the container to the current user's UID (and all the other
22+
# subuids to the user's /etc/subuid range) so that we're effectively running
23+
# the container as the current user.
24+
#
25+
# In the future, we will run podman as the current user, once we figure a
26+
# reasonable way for the current user to access the root's user container
27+
# storage.
28+
CURRENT_USER_NAME=$(id --user --name)
29+
CURRENT_USER_SUBUID_RANGE=$(awk \
30+
--field-separator ':' \
31+
--assign current_user="$CURRENT_USER_NAME" \
32+
--assign current_uid="$UID" \
33+
'$1 == current_user || $1 == current_uid {print $2 ":" $3}' \
34+
/etc/subuid)
35+
36+
# TODO: Handle multiple subuid ranges, for now, hard fail
37+
if [[ $(echo "$CURRENT_USER_SUBUID_RANGE" | wc -l) != 1 ]]; then
38+
if [[ -z "$CURRENT_USER_SUBUID_RANGE" ]]; then
39+
echo "No subuid range found for user $CURRENT_USER_NAME ($UID)"
40+
else
41+
echo "Multiple subuid ranges found for user $CURRENT_USER_NAME ($UID):"
42+
echo "$CURRENT_USER_SUBUID_RANGE"
43+
fi
44+
exit 1
45+
fi
46+
47+
IMPERSONATE_CURRENT_USER_PODMAN_FLAGS=("--uidmap" "0:$UID" "--uidmap" "1:$CURRENT_USER_SUBUID_RANGE")
48+
49+
PODMAN_COMMAND=("sudo" "podman" "run" "--rm" "-it"
50+
"${IMPERSONATE_CURRENT_USER_PODMAN_FLAGS[@]}"
51+
"--device" "${CONTAINER_DEVICE}"
52+
"--security-opt" "label=disable" "--net" "host"
53+
"-v" "$HOME:$HOME"
54+
# This is intentionally NOT using "--env" "HOME" because we want the HOME
55+
# of the current shell and not the HOME set by sudo
56+
"--env" "HOME=$HOME"
57+
"--entrypoint" "$ENTRYPOINT"
58+
"--env" "HF_TOKEN"
59+
"${IMAGE_NAME}")
2760

2861
exec "${PODMAN_COMMAND[@]}" "${PARAMS[@]}"

0 commit comments

Comments
 (0)