Created
January 26, 2020 16:14
-
-
Save lpenz/a80eca8420bbe689e42e45767fe0fa63 to your computer and use it in GitHub Desktop.
chroot script that mounts /dev, /proc, etc. in a private mount namespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
TARGET=${1-PWD} | |
shift | |
TMP=$(mktemp) | |
trap 'rm -f $TMP' EXIT | |
chmod u+x "$TMP" | |
set -e | |
cat > "$TMP" <<END | |
#!/bin/bash | |
set -e -x | |
: Entered private mount namespace | |
mount -t devtmpfs dev "$TARGET/dev" | |
mount -t devpts devpts "$TARGET/dev/pts" | |
mount -t proc proc "$TARGET/proc" | |
mount -t sysfs sysfs "$TARGET/sys" | |
mount -t none -o bind /run "$TARGET/run" | |
END | |
if [ "$#" = 0 ]; then | |
echo chroot "$TARGET" >> "$TMP" | |
else | |
echo chroot "$TARGET" "$@" >> "$TMP" | |
fi | |
unshare -m -- "$TMP" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment