run (3510B)
1 #!/command/execlineb -P 2 fdmove -c 2 1 3 4 s6-envdir env 5 multisubstitute { 6 importas -i -u CONTAINER_NAME CONTAINER_NAME 7 importas -i -u CONTAINER_USER CONTAINER_USER 8 importas -i -u CONTAINER_CAPS CONTAINER_CAPS 9 } 10 backtick -in CONTAINER_USER_HOME { homeof $CONTAINER_USER } 11 backtick -D "true" PREPARE_CHROOT { if { test -x data/prepare_chroot } realpath data/prepare_chroot } 12 backtick -D "exec" PID1_EXEC { if { test -x data/pid1_exec } realpath data/pid1_exec } 13 multisubstitute { 14 importas -i -u CONTAINER_USER_HOME CONTAINER_USER_HOME 15 define CONTAINER_TMPFS /run/containers/${CONTAINER_NAME}.${CONTAINER_USER} 16 define -s tmpfs_dirs "home run tmp run/inbox run/shm tmp/.X11-unix" 17 importas -D ns -s -C -u CONTAINER_MNT_DIRS CONTAINER_MNT_DIRS 18 importas -i -u PREPARE_CHROOT PREPARE_CHROOT 19 importas -i -u PID1_EXEC PID1_EXEC 20 importas -D default -u CONTAINER_SECCOMP_PROFILE CONTAINER_SECCOMP_PROFILE 21 importas -D ${CONTAINER_NAME} -u CONTAINER_HOSTNAME CONTAINER_HOSTNAME 22 } 23 24 getpid NS_PID 25 foreground { 26 importas -i NS_PID NS_PID 27 if { test -d /sys/fs/cgroup } 28 if { mkdir -p /sys/fs/cgroup/containers/${CONTAINER_USER}/${CONTAINER_NAME} } 29 redirfd -w 1 /sys/fs/cgroup/containers/${CONTAINER_USER}/${CONTAINER_NAME}/cgroup.procs 30 printf "%s" ${NS_PID} 31 } 32 unexport NS_PID 33 export HOST ${CONTAINER_HOSTNAME} 34 35 if { rm -rf ${CONTAINER_TMPFS} } 36 foreground { printf " '%s'" mkdir -p ${CONTAINER_TMPFS}/${tmpfs_dirs} ${CONTAINER_TMPFS}/mnt/${CONTAINER_MNT_DIRS} } 37 foreground { echo } 38 if { mkdir -p ${CONTAINER_TMPFS}/${tmpfs_dirs} ${CONTAINER_TMPFS}/mnt/${CONTAINER_MNT_DIRS} } 39 if { chmod 1770 ${CONTAINER_TMPFS}/${tmpfs_dirs} } 40 if { chown root:${CONTAINER_USER} ${CONTAINER_TMPFS}/${tmpfs_dirs} } 41 42 # Create default resolv.conf 43 if { redirfd -w 1 ${CONTAINER_TMPFS}/run/resolv.conf printf "nameserver 127.0.0.1\n" } 44 if { chown ${CONTAINER_USER}:${CONTAINER_USER} ${CONTAINER_TMPFS}/run/resolv.conf } 45 46 # Put UID/GID/GIDLIST into environment for use by applyuidgid-caps below 47 s6-envuidgid ${CONTAINER_USER} 48 49 unshare -m -u -i # new mount, UTS and IPC namespaces 50 51 $PID1_EXEC 52 53 emptyenv -c 54 ns_run_unshared data/root { 55 # pre pivot-root commands 56 if { s6-mount -o bind,ro,nodev,nosuid /etc/passwd ./etc/passwd } 57 if { s6-mount -o remount,bind,ro,nodev,nosuid _ ./etc/passwd } 58 59 if { s6-mount -o bind,ro,nodev,nosuid /etc/group ./etc/group } 60 if { s6-mount -o remount,bind,ro,nodev,nosuid _ ./etc/group } 61 62 if { $PREPARE_CHROOT } 63 # Run user's setup script (optional) 64 if { 65 ifelse { test -x ${CONTAINER_USER_HOME}/container-setup } { 66 env HOME=${CONTAINER_USER_HOME} USER=${CONTAINER_USER} 67 applyuidgid-caps -U "" 68 ${CONTAINER_USER_HOME}/container-setup ${CONTAINER_TMPFS} ${CONTAINER_NAME} 69 } 70 } 71 72 foreground { printf "Seccomp profile to be used:" } 73 foreground { 74 ls -lhA ./mnt/ns/bin/seccomp-${CONTAINER_SECCOMP_PROFILE}.bpf 75 } 76 } 77 78 79 # This runs with changed / so use absolute paths before dropping privs 80 /mnt/ns/bin/applyuidgid-caps -U $CONTAINER_CAPS 81 /mnt/ns/bin/seccomp-run /mnt/ns/bin/seccomp-${CONTAINER_SECCOMP_PROFILE}.bpf 82 /mnt/ns/bin/busybox env HOME=${CONTAINER_USER_HOME} USER=${CONTAINER_USER} 83 /mnt/ns/bin/foreground { 84 /mnt/ns/bin/busybox cat /proc/1/status 85 } 86 /mnt/ns/bin/foreground { 87 /mnt/ns/bin/busybox cat /proc/self/mountinfo 88 } 89 /mnt/ns/bin/ifelse { /mnt/ns/bin/busybox test -x ${CONTAINER_USER_HOME}/run/init } { 90 ${CONTAINER_USER_HOME}/run/init 91 } 92 /mnt/ns/bin/foreground { 93 /mnt/ns/bin/if -n { 94 /mnt/ns/bin/busybox stat /mnt/init/init 95 } 96 /mnt/ns/bin/busybox ls -lhA /mnt/init /mnt 97 } 98 /mnt/init/init