Skip to content

Instantly share code, notes, and snippets.

@lpenz
Created January 26, 2020 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lpenz/a80eca8420bbe689e42e45767fe0fa63 to your computer and use it in GitHub Desktop.
Save lpenz/a80eca8420bbe689e42e45767fe0fa63 to your computer and use it in GitHub Desktop.
chroot script that mounts /dev, /proc, etc. in a private mount namespace
#!/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