From: Ralph Ronnquist Date: Sat, 12 Nov 2022 08:20:20 +0000 (+1100) Subject: unmount most before unshare X-Git-Tag: v0.1.8~1^2~2 X-Git-Url: https://git.rrq.au/?a=commitdiff_plain;h=3b288e812dc2d5d72c7dd91d5e8cacbd72508a6a;p=rrq%2Foverlay-boot.git unmount most before unshare --- diff --git a/functions b/functions index ddcd97d..e9111fe 100644 --- a/functions +++ b/functions @@ -11,6 +11,10 @@ die() { exit 1 } +beginswith() { + [ "$1" != "${1#$2}" ] +} + # Function to setup subhost name and log file subhost_name() { CONFIG="$1" diff --git a/overlay-boot b/overlay-boot index fc4b8b4..6577cad 100755 --- a/overlay-boot +++ b/overlay-boot @@ -31,7 +31,19 @@ exithandler() { } trap "exithandler" 0 -CMD="unshare -fp --mount-proc -i -u ip netns exec $NAME chroot $LIVE /bin/sh" -echo "$CMD" -env CONFIG="$CONFIG" $INIT | $CMD -echo "EXITED $CMD" +# This process has an unshared mount namespace, so we unmount almost +# everything before chroot. Exceptions are: $LIVE and anything mounted +# below that, "/run/netns/$NAME" and its parent paths (incidentally +# including "/" as well) and "/proc". +sort -rk2,2 < /proc/mounts | while read D P A2 ; do + beginswith "$P" "$LIVE" && continue + beginswith "/run/netns/$NAME" "$P" && continue + [ "$P" = /proc ] && continue + umount "$P" +done + +echo "Starting $NAME" +env CONFIG="$CONFIG" $INIT | \ + unshare -fp --mount-proc -i -u \ + ip netns exec $NAME chroot $LIVE /bin/sh +echo "Exited $NAME"