Created
June 10, 2024 02:00
-
-
Save devinci-it/92daaba030664f2f15b86cf831dcd606 to your computer and use it in GitHub Desktop.
This script automates the process of entering a chroot environment.
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 | |
| : ' | |
| Interactive Chroot Script | |
| This script automates the process of entering a chroot environment. It lists available disks and partitions, | |
| prompts the user to select a partition, mounts the necessary filesystems, and enters the chroot environment. | |
| The script also provides a command (`exit-chroot`) to trigger the unmounting of filesystems and exit the script. | |
| Functions: | |
| - list_disks: Lists available disks and partitions using lsblk. | |
| - select_partition: Prompts the user to select a partition to chroot into and validates the input. | |
| - mount_filesystems: Mounts the target partition and binds /dev, /dev/pts, /proc, /sys, and /run to the chroot environment. | |
| - unmount_filesystems: Unmounts the filesystems and exits the script. | |
| - enter_chroot: Enters the chroot environment. | |
| - key_monitor: Monitors for the specific command `exit-chroot` to trigger unmounting. | |
| Usage: | |
| - Run the script as root to ensure it has the necessary privileges. | |
| - Follow the prompts to select a partition. | |
| - Type `exit-chroot` at any time to unmount the filesystems and exit the script. | |
| ' | |
| # Function to list available disks and partitions | |
| list_disks() { | |
| echo "Available disks and partitions:" | |
| lsblk | |
| } | |
| # Function to prompt the user to select a partition | |
| select_partition() { | |
| echo "Enter the partition to chroot into (e.g., /dev/sda1):" | |
| read TARGET_PARTITION | |
| if [ ! -b "$TARGET_PARTITION" ]; then | |
| echo "Error: Invalid partition. Please try again." | |
| select_partition | |
| fi | |
| } | |
| # Function to mount necessary filesystems | |
| mount_filesystems() { | |
| echo "Mounting filesystems..." | |
| sudo mount $TARGET_PARTITION $MOUNT_POINT && echo "Mounted $TARGET_PARTITION on $MOUNT_POINT" | |
| sudo mount --bind /dev $MOUNT_POINT/dev && echo "Mounted /dev on $MOUNT_POINT/dev" | |
| sudo mount --bind /dev/pts $MOUNT_POINT/dev/pts && echo "Mounted /dev/pts on $MOUNT_POINT/dev/pts" | |
| sudo mount --bind /proc $MOUNT_POINT/proc && echo "Mounted /proc on $MOUNT_POINT/proc" | |
| sudo mount --bind /sys $MOUNT_POINT/sys && echo "Mounted /sys on $MOUNT_POINT/sys" | |
| sudo mount --bind /run $MOUNT_POINT/run && echo "Mounted /run on $MOUNT_POINT/run" | |
| echo "Filesystems mounted." | |
| } | |
| # Function to unmount filesystems | |
| unmount_filesystems() { | |
| echo "Unmounting filesystems..." | |
| sudo umount $MOUNT_POINT/dev/pts && echo "Unmounted $MOUNT_POINT/dev/pts" | |
| sudo umount $MOUNT_POINT/dev && echo "Unmounted $MOUNT_POINT/dev" | |
| sudo umount $MOUNT_POINT/proc && echo "Unmounted $MOUNT_POINT/proc" | |
| sudo umount $MOUNT_POINT/sys && echo "Unmounted $MOUNT_POINT/sys" | |
| sudo umount $MOUNT_POINT/run && echo "Unmounted $MOUNT_POINT/run" | |
| sudo umount $MOUNT_POINT && echo "Unmounted $MOUNT_POINT" | |
| echo "Filesystems unmounted." | |
| exit 0 | |
| } | |
| # Function to enter chroot environment | |
| enter_chroot() { | |
| echo "Entering chroot environment..." | |
| sudo chroot $MOUNT_POINT /bin/bash | |
| } | |
| # Function to handle unmount command | |
| command_monitor() { | |
| while true; do | |
| echo "Type 'exit-chroot' to unmount and exit." | |
| read -p "> " cmd | |
| if [[ $cmd == "exit-chroot" ]]; then | |
| echo "Unmounting triggered by user command." | |
| unmount_filesystems | |
| fi | |
| done | |
| } | |
| # Check if the script is being run with root privileges | |
| if [ "$EUID" -ne 0 ]; then | |
| echo "Please run as root" | |
| exit | |
| fi | |
| # Define the mount point | |
| MOUNT_POINT="/mnt" | |
| # List disks and prompt user to select a partition | |
| list_disks | |
| select_partition | |
| # Mount filesystems | |
| mount_filesystems | |
| # Start command monitor in background | |
| command_monitor & | |
| # Enter chroot environment | |
| enter_chroot | |
| # Unmount filesystems after exiting chroot | |
| unmount_filesystems |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment