Last active
February 18, 2025 01:02
-
-
Save unixabg/04c214b5d72ec955a131ac962820e097 to your computer and use it in GitHub Desktop.
Script to aid in chroot operations
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 | |
# chroot-tools v1.0 | |
# Author: Richard Nelson [email protected] | |
# This script sets up a chroot environment for testing on a Debian system. | |
# Run with sudo privileges. | |
VERSION="1.0" | |
# Default chroot directory, action, and packages | |
CHROOT_DIR="" | |
ACTION="" | |
PACKAGES="" | |
RELEASE="stable" # Default release | |
# Packages required for debootstrap | |
REQUIRED_PACKAGES=(debootstrap) | |
# Function to display version | |
display_version() { | |
echo "chroot-tools version $VERSION" | |
exit 0 | |
} | |
# Function to display help | |
display_help() { | |
echo "chroot-tools v$VERSION" | |
echo "Usage: sudo ./chroot-tools.sh [build|enter|remove|version] --dir <path_to_chroot> [--release <stable|testing|sid>] [--packages <packages>]" | |
echo | |
echo "Commands:" | |
echo " build Set up a new chroot environment" | |
echo " enter Enter an existing chroot environment" | |
echo " remove Remove an existing chroot environment" | |
echo " version Display the script version" | |
echo | |
echo "Options:" | |
echo " --dir, -d Specify the target directory for the chroot" | |
echo " --release, -r Specify the Debian release (default: stable)" | |
echo " --packages Specify packages to install in the chroot" | |
echo | |
echo "Examples:" | |
echo " sudo ./chroot-tools.sh build --dir /srv/chroot/test-env --release testing --packages \"python3 curl\"" | |
echo " sudo ./chroot-tools.sh enter --dir /srv/chroot/test-env" | |
echo " sudo ./chroot-tools.sh remove --dir /srv/chroot/test-env" | |
echo " sudo ./chroot-tools.sh version" | |
exit 1 | |
} | |
# Function to parse command-line arguments | |
parse_arguments() { | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
build|enter|remove|version) | |
ACTION=$1 | |
shift | |
;; | |
-d|--dir) | |
CHROOT_DIR=$2 | |
shift 2 | |
;; | |
--release|-r) | |
RELEASE=$2 | |
shift 2 | |
;; | |
--packages) | |
PACKAGES=$2 | |
shift 2 | |
;; | |
*) | |
echo "Unknown option: $1" | |
display_help | |
;; | |
esac | |
done | |
if [[ "$ACTION" == "version" ]]; then | |
display_version | |
fi | |
if [[ -z "$CHROOT_DIR" ]]; then | |
echo "Error: Target directory is required. Use -d to specify it." | |
display_help | |
fi | |
if [[ -z "$ACTION" ]]; then | |
echo "Error: No action specified. Use 'build', 'enter', 'remove', or 'version'." | |
display_help | |
fi | |
} | |
install_required_packages() { | |
echo "Installing required packages..." | |
apt-get update | |
apt-get install -y "${REQUIRED_PACKAGES[@]}" | |
} | |
create_chroot_directory() { | |
echo "Creating chroot directory at $CHROOT_DIR..." | |
mkdir -p "$CHROOT_DIR" | |
} | |
bootstrap_chroot() { | |
echo "Bootstrapping Debian $RELEASE environment into $CHROOT_DIR..." | |
debootstrap --variant=minbase $RELEASE "$CHROOT_DIR" http://deb.debian.org/debian/ | |
} | |
set_chroot_hostname() { | |
local hostname | |
hostname=$(basename "$CHROOT_DIR") | |
echo "$hostname" > "$CHROOT_DIR/etc/hostname" | |
echo "127.0.1.1 $hostname" >> "$CHROOT_DIR/etc/hosts" | |
} | |
mount_filesystems() { | |
echo "Mounting necessary filesystems..." | |
mount --bind /dev "$CHROOT_DIR/dev" | |
mount --bind /proc "$CHROOT_DIR/proc" | |
mount --bind /sys "$CHROOT_DIR/sys" | |
} | |
set_chroot_runtime_hostname() { | |
local hostname | |
hostname=$(cat "$CHROOT_DIR/etc/hostname") | |
echo "Setting runtime hostname to $hostname" | |
chroot "$CHROOT_DIR" /bin/bash -c "hostname $hostname" | |
} | |
install_packages_in_chroot() { | |
if [[ -n "$PACKAGES" ]]; then | |
echo "Installing packages in chroot: $PACKAGES" | |
chroot "$CHROOT_DIR" /bin/bash -c "apt-get update && apt-get install -y $PACKAGES" | |
else | |
echo "No additional packages specified. Setting up basic chroot only." | |
fi | |
} | |
unmount_filesystems() { | |
echo "Unmounting filesystems..." | |
umount "$CHROOT_DIR/dev" | |
umount "$CHROOT_DIR/proc" | |
umount "$CHROOT_DIR/sys" | |
} | |
enter_chroot() { | |
echo "Entering chroot environment at $CHROOT_DIR..." | |
mount_filesystems | |
set_chroot_runtime_hostname | |
chroot "$CHROOT_DIR" /bin/bash | |
unmount_filesystems | |
} | |
build_chroot() { | |
if [[ -d "$CHROOT_DIR" && -f "$CHROOT_DIR/bin/bash" ]]; then | |
echo "Chroot environment already exists at $CHROOT_DIR." | |
exit 1 | |
fi | |
install_required_packages | |
create_chroot_directory | |
bootstrap_chroot | |
set_chroot_hostname | |
mount_filesystems | |
install_packages_in_chroot | |
unmount_filesystems | |
echo "Chroot environment built successfully at $CHROOT_DIR." | |
echo "To enter the chroot environment, run: sudo ./chroot-tools.sh enter --dir $CHROOT_DIR" | |
} | |
remove_chroot() { | |
if [[ ! -d "$CHROOT_DIR" ]]; then | |
echo "Error: Chroot directory $CHROOT_DIR does not exist." | |
exit 1 | |
fi | |
read -p "Are you sure you want to remove the chroot environment at $CHROOT_DIR? [y/N] " confirm | |
if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then | |
echo "Aborted." | |
exit 1 | |
fi | |
echo "Unmounting any mounted filesystems..." | |
umount "$CHROOT_DIR/dev" 2>/dev/null || true | |
umount "$CHROOT_DIR/proc" 2>/dev/null || true | |
umount "$CHROOT_DIR/sys" 2>/dev/null || true | |
echo "Removing chroot directory at $CHROOT_DIR..." | |
rm -rf "$CHROOT_DIR" | |
echo "Chroot environment at $CHROOT_DIR has been removed." | |
} | |
main() { | |
if [[ "$ACTION" == "build" ]]; then | |
build_chroot | |
elif [[ "$ACTION" == "enter" ]]; then | |
enter_chroot | |
elif [[ "$ACTION" == "remove" ]]; then | |
remove_chroot | |
else | |
display_help | |
fi | |
} | |
parse_arguments "$@" | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment