Last active
June 12, 2025 05:34
-
-
Save luavixen/61c2a5e2e47009c1cbfbfec78f3f09b0 to your computer and use it in GitHub Desktop.
Create a new Debian `chroot` environment in Alpine Linux
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/sh | |
if [[ $(id -u) -eq 0 ]]; then | |
echo "This script cannot be run as root, root access will be requested through \`sudo\`" | |
exit 1 | |
fi | |
user="$USER" | |
group="$user" | |
path_working="$PWD" | |
path_debian="$path_working/debian" | |
path_slash="$path_debian/slash" | |
slash="$path_slash" | |
debian="$path_debian" | |
read -p "Creating \"$slash\" with user $user:$group, continue (y/n)? " choice | |
case "$choice" in | |
y|Y ) echo "Confirmed" ;; | |
* ) exit 1 ;; | |
esac | |
mkdir -p "$path_debian" | |
mkdir -p "$path_slash" | |
sudo apk add debootstrap perl | |
sudo debootstrap --arch i386 buster "$slash" "http://deb.debian.org/debian" | |
sudo apk del debootstrap perl | |
sudo rm "$slash/etc/resolv.conf" "$slash/etc/hosts" | |
sudo ln "/etc/resolv.conf" "$slash/etc/resolv.conf" | |
sudo ln "/etc/hosts" "$slash/etc/hosts" | |
file_mount="$debian/mount.sh" | |
file_shell="$debian/shell.sh" | |
cat > "$file_mount" << EOF | |
#!/bin/sh | |
if [[ \$(id -u) -ne 0 ]]; then | |
echo "Permission denied, please run as root" | |
exit 1 | |
fi | |
runfile="/run/debian-mounted" | |
if [[ -f \$runfile ]]; then | |
echo "Already mounted" | |
exit 1 | |
fi | |
touch \$runfile | |
slash='$slash' | |
mount --bind /dev "\$slash/dev" | |
mount --bind /dev/pts "\$slash/dev/pts" | |
mount --bind /proc "\$slash/proc" | |
mount --bind /sys "\$slash/sys" | |
mount --bind /run "\$slash/run" | |
EOF | |
sudo chmod +x "$file_mount" | |
cat > "$file_shell" << EOF | |
#!/bin/sh | |
if [[ \$(id -u) -ne 0 ]]; then | |
echo "Permission denied, please run as root" | |
exit 1 | |
fi | |
chroot '$slash' /usr/bin/su -s /usr/bin/bash -g '$group' -l '$user' | |
EOF | |
sudo chmod +x "$file_shell" | |
sudo sh "$file_mount" | |
sudo chroot "$slash" /usr/bin/bash << EOF | |
cat > /usr/sbin/policy-rc.d << EOM | |
#!/bin/sh | |
exit 101 | |
EOM | |
chmod +x /usr/sbin/policy-rc.d | |
printf "deb http://deb.debian.org/debian buster main contrib non-free" > /etc/apt/sources.list | |
apt update | |
apt upgrade | |
apt install sudo nano wget | |
exit | |
EOF | |
echo "===[ Setting up chroot user ]===================================================" | |
sudo chroot "$slash" /usr/sbin/adduser "$user" | |
echo "================================================================================" | |
sudo chroot "$slash" /usr/bin/bash << EOF | |
usermod -aG sudo lua | |
printf '\n$user ALL=(ALL) ALL\n' >> /etc/sudoers | |
exit | |
EOF |
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/sh | |
path_working="$PWD" | |
path_debian="$path_working/debian" | |
path_slash="$path_debian/slash" | |
slash="$path_slash" | |
debian="$path_debian" | |
read -p "Destroying \"$slash\" and parent \"$debian\", continue (y/n)? " choice | |
case "$choice" in | |
y|Y ) echo "Confirmed" ;; | |
* ) exit 1 ;; | |
esac | |
sudo umount -l "$slash/dev/pts" | |
sudo umount -l "$slash/dev" && \ | |
sudo umount -l "$slash/proc" && \ | |
sudo umount -l "$slash/sys" && \ | |
sudo umount -l "$slash/run" || exit 1 | |
sudo rm -rf "$slash" | |
sudo rm -rf "$debian" | |
sudo rm -rf "/etc/init.d/debian-mount" | |
sudo rm -rf "/etc/init.d/debian-chroot-mount" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment