Skip to content

Instantly share code, notes, and snippets.

@linux4life798
Last active April 22, 2025 23:19
Show Gist options
  • Save linux4life798/9ddee6292ace64896ec5f6b3aed1f8cc to your computer and use it in GitHub Desktop.
Save linux4life798/9ddee6292ace64896ec5f6b3aed1f8cc to your computer and use it in GitHub Desktop.
Script to setup and enter a chroot
#!/bin/bash
# Craig Hesling <[email protected]>
set -e
root="$1"
if [[ -z "$root" || ! -d "$root" ]]; then
echo "Error: Directory '$root' does not exist"
echo
echo "Usage: $0 /path/to/existing/chroot/directory"
exit 1
fi
if ! mountpoint -q "$root/proc"; then
mount -t proc /proc "$root/proc"
fi
if ! mountpoint -q "$root/sys"; then
mount -t sysfs /sys "$root/sys"
fi
if ! mountpoint -q "$root/dev"; then
mount -o bind /dev "$root/dev"
fi
cp /etc/resolv.conf "$root/etc/resolv.conf"
export debian_chroot="$(basename "$root")"
# We can enable PID namespacing by uncommenting the following, but /proc
# would need to be unmounted and remounted inside the namespace. Otherwise,
# the processes would still be able to see host proc info, through the fs.
#unshare --pid --mount-proc --fork \
chroot "$root" /bin/bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment