mrrl-containers

MRRL version of container scripts
git clone https://ccx.te2000.cz/git/mrrl-containers
Log | Files | Refs

commit ff1cc6c4f1577e443903c3b6eaddeaf1005d3d79
parent 1c2cd8431e44fa2657e948297caaf56274b3b0f4
Author: Jan Pobrislo <ccx@te2000.cz>
Date:   Wed,  5 Aug 2026 18:56:11 +0000

Move mounting to prepare_chroot except for toplevel bind-mount

Diffstat:
Mservice_scripts/generic/run | 18+++++++++---------
Mzsh-functions/confz_containers_init | 63+++++++++++++++++++++++++++++++++++----------------------------
Mzsh-functions/confz_site_containers_init | 38+++++++++++++++-----------------------
3 files changed, 59 insertions(+), 60 deletions(-)

diff --git a/service_scripts/generic/run b/service_scripts/generic/run @@ -50,15 +50,6 @@ unshare -m -u -i # new mount, UTS and IPC namespaces $PID1_EXEC -# Run user's setup script (optional) -if { - ifelse { test -x ${CONTAINER_USER_HOME}/container-setup } { - env HOME=${CONTAINER_USER_HOME} USER=${CONTAINER_USER} - applyuidgid-caps -U "" - ${CONTAINER_USER_HOME}/container-setup ${CONTAINER_TMPFS} ${CONTAINER_NAME} - } -} - foreground { printf "Seccomp profile to be used:" } foreground { ls -lhA ./mnt/ns/bin/seccomp-${CONTAINER_SECCOMP_PROFILE}.bpf @@ -72,6 +63,15 @@ ns_run_unshared data/root { $PREPARE_CHROOT } +# Run user's setup script (optional) +if { + ifelse { test -x ${CONTAINER_USER_HOME}/container-setup } { + env HOME=${CONTAINER_USER_HOME} USER=${CONTAINER_USER} + applyuidgid-caps -U "" + ${CONTAINER_USER_HOME}/container-setup ${CONTAINER_TMPFS} ${CONTAINER_NAME} + } +} + # This runs with changed / so use absolute paths before dropping privs /mnt/ns/bin/applyuidgid-caps -U $CONTAINER_CAPS /mnt/ns/bin/seccomp-run /mnt/ns/bin/seccomp-${CONTAINER_SECCOMP_PROFILE}.bpf diff --git a/zsh-functions/confz_containers_init b/zsh-functions/confz_containers_init @@ -404,11 +404,6 @@ confz_container_mount_layout_check() { run=/run/containers/$vars[container_name].$vars[user] container=$vars[container_name] container_user_dir=$vars[containers_dir]/user/$vars[user]/$container - bind_mounts=( - $vars[containers_dir]/systems/$vars[image_name] - $container_user_dir/root - ro - ) require fs_d filename=$vars[containers_dir]/user/$vars[user] require fs_m filename=$vars[containers_dir]/user/$vars[user] mode=751 @@ -418,7 +413,7 @@ confz_container_mount_layout_check() { require fs_m filename=$container_user_dir mode=751 require fs_o filename=$container_user_dir owner=0:$vars[gid] - require fs_d filename=$container_user_dir/root + require fs_r filename=$container_user_dir/root case $vars[persistent_home] in (true) @@ -451,20 +446,18 @@ confz_container_mount_layout_check() { esac bind_mounts+=( - $run/run $container_user_dir/root/run rw - $run/tmp $container_user_dir/root/tmp rw - $run/mnt $container_user_dir/root/mnt rw + $run/run ./run rw + $run/tmp ./tmp rw + $run/mnt ./mnt rw ) - mount_el_lines=( "#!$(which execlineb) -S0" ) local src dst rw for src dst rw in "$bind_mounts[@]"; do - fstab+=( $src$'\t'$dst$'\tnone\tbind,'$rw$',nosuid,nodev,slave\t0 0' ) src_el=\"${${src//\\/\\\\}//\"/\\\"}\" dst_el=\"${${dst//\\/\\\\}//\"/\\\"}\" mount_el_lines+=( "if { s6-mount -o bind,$rw,nodev,nosuid,slave $src_el $dst_el }" - "if { s6-mount -o remount,bind,$rw,nodev,nosuid . $dst_el }" + "if { s6-mount -o remount,bind,$rw,nodev,nosuid _ $dst_el }" ) done unify mount_el $mount_el_lines @@ -472,11 +465,10 @@ confz_container_mount_layout_check() { confz_container_service_ephemeral_check() { local uid gid container_user_dir svc_dir log_dir container - local -a fstab mnt_dirs + local -a mnt_dirs prepare_el_lines optional_args checkvars containers_dir svscan_dir image_name user defvar seccomp_profile 'default' defvar linux_caps '' - defvar fstab_extra '' defvar mnt_dirs_extra '' defvar container_name "$vars[image_name]" container=$vars[container_name] @@ -491,17 +483,25 @@ confz_container_service_ephemeral_check() { :containers_dir :image_name :container_name :user \ uid=$uid gid=$gid persistent_home=false %mount_el - pid1_el_lines=( "#!$(which execlineb) -S0" $vars[mount_el] ) - (($+vars[pid1_el])) && pid1_el_lines+=( $vars[pid1_el] ) - pid1_el_lines+=( '$@' ) + if (($+vars[pid1_el])); then + local -a pid1_el_lines=( + "#!${commands[execlineb]} -S0" + $vars[pid1_el] + '$@' + ) + optional_args+=( pid1_exec=${(F)pid1_el_lines} ) + fi + prepare_el_lines=( "#!${commands[execlineb]} -P" $vars[mount_el] ) + (($+vars[prepare_el])) && prepare_el_lines+=( $vars[prepare_el] ) + prepare_el_lines+=( '$@' ) require container_service_preset preset=generic \ svc_dir=$svc_dir control_user=$uid control_group=$gid \ log_dir=/run/container-logs/$container.$vars[user] \ log_uid=0 log_gid=$gid \ - root_link=$container_user_dir/root \ - fstab=${(F)fstab} \?down \?fstab_post \ - \?prepare_chroot pid1_exec=${(F)pid1_el_lines} + root_link=$vars[containers_dir]/systems/$vars[image_name] \ + \?down \ + prepare_chroot=${(F)prepare_el_lines} "$optional_args[@]" require fs_d filename=$svc_dir/env require fs_contentnl filename=$svc_dir/env/CONTAINER_USER \ content=$vars[user] @@ -517,11 +517,10 @@ confz_container_service_ephemeral_check() { confz_container_service_generic_check() { local uid gid container_user_dir svc_dir log_dir container - local -a fstab mnt_dirs + local -a mnt_dirs prepare_el_lines checkvars containers_dir svscan_dir image_name user defvar seccomp_profile 'default' defvar linux_caps '' - defvar fstab_extra '' defvar mnt_dirs_extra '' defvar container_name "$vars[image_name]" container=$vars[container_name] @@ -536,17 +535,25 @@ confz_container_service_generic_check() { :containers_dir :image_name :container_name :user \ uid=$uid gid=$gid persistent_home=true %mount_el - pid1_el_lines=( "#!$(which execlineb) -S0" $vars[mount_el] ) - (($+vars[pid1_el])) && pid1_el_lines+=( $vars[pid1_el] ) - pid1_el_lines+=( '$@' ) + if (($+vars[pid1_el])); then + local -a pid1_el_lines=( + "#!${commands[execlineb]} -S0" + $vars[pid1_el] + '$@' + ) + optional_args+=( pid1_exec=${(F)pid1_el_lines} ) + fi + prepare_el_lines=( "#!${commands[execlineb]} -P" $vars[mount_el] ) + (($+vars[prepare_el])) && prepare_el_lines+=( $vars[prepare_el] ) + prepare_el_lines+=( '$@' ) require container_service_preset preset=generic \ svc_dir=$svc_dir control_user=$uid control_group=$gid \ log_dir=/run/container-logs/$container.$vars[user] \ log_uid=0 log_gid=$gid \ - root_link=$container_user_dir/root \ - fstab=${(F)fstab} \?down \?fstab_post \ - \?prepare_chroot pid1_exec=${(F)pid1_el_lines} + root_link=$vars[containers_dir]/systems/$vars[image_name] \ + \?down \ + prepare_chroot=${(F)prepare_el_lines} "$optional_args[@]" require fs_d filename=$svc_dir/env require fs_contentnl filename=$svc_dir/env/CONTAINER_USER \ content=$vars[user] diff --git a/zsh-functions/confz_site_containers_init b/zsh-functions/confz_site_containers_init @@ -39,7 +39,7 @@ confz_site_containers_usersvc_simple_check() { defvar container_type generic local userdir bindroot flags mtp src dst src_el dst_el - local -a fstab mnt_dirs_extra pid1_el_lines + local -a mnt_dirs_extra prepare_el_lines userdir=$vars[containers_dir]/user/$vars[user] bindroot=$userdir/$vars[container_name]/root @@ -47,15 +47,14 @@ confz_site_containers_usersvc_simple_check() { flags=$'\tnone\tbind,ro,nosuid,nodev\t0 0' for mtp in ${=vars[mount_ro]}; do src=$site_containers_mountpoints[$mtp] - dst=$bindroot/mnt/$mtp + dst=./mnt/$mtp mnt_dirs_extra+=( $mtp ) - fstab+=( $src$'\t'$dst$flags ) src_el=\"${${src//\\/\\\\}//\"/\\\"}\" dst_el=\"${${dst//\\/\\\\}//\"/\\\"}\" - pid1_el_lines+=( + prepare_el_lines+=( "if { mkdir -p $dst_el }" "if { s6-mount -r -o bind,nodev,nosuid $src_el $dst_el }" - "if { s6-mount -o remount,bind,ro,nodev,nosuid . $dst_el }" + "if { s6-mount -o remount,bind,ro,nodev,nosuid _ $dst_el }" ) done fi @@ -63,15 +62,14 @@ confz_site_containers_usersvc_simple_check() { flags=$'\tnone\tbind,rw,nosuid,nodev\t0 0' for mtp in ${=vars[mount_rw]}; do src=$site_containers_mountpoints[$mtp] - dst=$bindroot/mnt/$mtp + dst=./mnt/$mtp mnt_dirs_extra+=( $mtp ) - fstab+=( $src$'\t'$dst$flags ) src_el=\"${${src//\\/\\\\}//\"/\\\"}\" dst_el=\"${${dst//\\/\\\\}//\"/\\\"}\" - pid1_el_lines+=( + prepare_el_lines+=( "if { mkdir -p $dst_el }" "if { s6-mount -r -o bind,nodev,nosuid $src_el $dst_el }" - "if { s6-mount -o remount,bind,rw,nodev,nosuid . $dst_el }" + "if { s6-mount -o remount,bind,rw,nodev,nosuid _ $dst_el }" ) done fi @@ -80,12 +78,11 @@ confz_site_containers_usersvc_simple_check() { if (($#mnt_dirs_extra)); then unify mnt_dirs_extra "$mnt_dirs_extra" - unify fstab_extra ${(F)fstab} fi require container_service_$vars[container_type] \ :image_name :container_name :containers_dir :svscan_dir :user \ - \?mnt_dirs_extra \?fstab_extra \?prepare_chroot \?linux_caps \ + \?mnt_dirs_extra \?prepare_el \?linux_caps \ \?seccomp_profile \?display_number \ pid1_el=${(F)pid1_el_lines} @@ -121,7 +118,7 @@ confz_site_containers_user_single_check() { :containers_dir :svscan_dir :user :uid :gid \ :container_name :image_name :mount_ro \?mount_rw \?linux_caps \ \?seccomp_profile \ - \?prepare_chroot + \?prepare_el } # User Container @@ -182,24 +179,21 @@ confz_site_containers_user_check() { local with_more_fds=pid1_el='zsh -c "ulimit -Hn 16384 && exec \"$@\"" --' local -a el_mount_usb_devices=( - "#!$(which execlineb) -P" 'if { mount -o bind,ro /dev/bus/usb dev/bus/usb }' 'mount -t sysfs sysfs sys' ) - local with_usb=prepare_chroot=${(F)el_mount_usb_devices} + local with_usb=prepare_el=${(F)el_mount_usb_devices} local -a el_mount_audio_devices=( - "#!$(which execlineb) -P" 'if { mount -o bind,ro /dev/snd dev/snd }' 'mount -t sysfs sysfs sys' # maybe not necessary? ) - local with_audio=prepare_chroot=${(F)el_mount_audio_devices} + local with_audio=prepare_el=${(F)el_mount_audio_devices} local -a el_mount_ccx_opt=( - "#!$(which execlineb) -P" 'if { mount -o bind,ro /home/ccx/opt ./opt }' ) - local with_opt=prepare_chroot=${(F)el_mount_ccx_opt} + local with_opt=prepare_el=${(F)el_mount_ccx_opt} local display for display in 5 6 7 8; do @@ -351,26 +345,24 @@ confz_site_container_services_check() { #require container_service_sysroot :containers_dir :svscan_dir \ # image_name=alpine-dev local -a el_rfkill_and_sys=( - "#!$(which execlineb) -P" 'importas -i GID GID' 'if { mknod -m 660 ./dev/rfkill c 10 242 }' 'if { chown 0:${GID} ./dev/rfkill }' 'mount -t sysfs sysfs sys' ) local -a el_net_tun=( - "#!$(which execlineb) -P" 'importas -i GID GID' 'if { mknod -m 660 ./dev/net/tun c 10 200 }' 'if { chown 0:${GID} ./dev/net/tun }' ) SUCa wpa_supplicant linux_caps='^CAP_NET_ADMIN,^CAP_NET_RAW' \ - prepare_chroot=${(F)el_rfkill_and_sys} + prepare_el=${(F)el_rfkill_and_sys} SUCa dhcpcd linux_caps='^CAP_NET_ADMIN,^CAP_NET_RAW,^CAP_NET_BIND_SERVICE' SUCa unbound linux_caps='^CAP_NET_BIND_SERVICE' - SUCa tinc linux_caps='^CAP_NET_ADMIN' prepare_chroot=${(F)el_net_tun} + SUCa tinc linux_caps='^CAP_NET_ADMIN' prepare_el=${(F)el_net_tun} SUCa networking linux_caps='^CAP_NET_ADMIN,^CAP_NET_RAW' \ - prepare_chroot=${(F)el_rfkill_and_sys} + prepare_el=${(F)el_rfkill_and_sys} } confz_site_container_alpine_check() {