1
1
#! /bin/bash
2
2
3
+ function echo-err { echo " $@ " >&2 ; }
4
+
3
5
# Template values replaced by container build
4
6
CONTAINER_DEVICE=" __REPLACE_CONTAINER_DEVICE__"
5
7
IMAGE_NAME=" __REPLACE_IMAGE_NAME__"
@@ -8,7 +10,7 @@ export ENTRYPOINT="ilab"
8
10
export PARAMS=(" $@ " )
9
11
10
12
if [[ -n " $ILAB_HOME " ]]; then
11
- HOME=" $ILAB_HOME "
13
+ HOME=" $ILAB_HOME "
12
14
fi
13
15
14
16
for dir in " $HOME /.cache" " $HOME /.config" " $HOME /.local" ; do
@@ -40,12 +42,45 @@ for PODMAN_MOUNT in "${ADDITIONAL_MOUNTS[@]}"; do
40
42
ADDITIONAL_MOUNT_OPTIONS+=(" -v" " $PODMAN_MOUNT " )
41
43
done
42
44
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[@]} "
44
77
" --device" " ${CONTAINER_DEVICE} "
45
78
" --security-opt" " label=disable" " --net" " host"
46
79
" -v" " $HOME :$HOME "
47
80
" ${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 "
49
84
" --entrypoint" " $ENTRYPOINT "
50
85
" --env" " HF_TOKEN"
51
86
" ${IMAGE_NAME} " )
0 commit comments