Last active
April 22, 2025 23:19
-
-
Save linux4life798/9ddee6292ace64896ec5f6b3aed1f8cc to your computer and use it in GitHub Desktop.
Script to setup and enter a chroot
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 | |
# 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