Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/data/data/com.termux/files/usr/bin/bash
- set -e
- if [ "$EUID" -ne 0 ]; then
- echo "This script must be run as root."
- exit 1
- fi
- echo -e 'Do you want to: \n1. Setup a New chroot environment. \n2. Login to an already existing rootfs. \n3. Delete a Rootfs. \nAny other Character to Exit.'
- read -p 'Enter Your Choice (1/2/3): ' choice
- choice="${choice,,}"
- if [[ "$choice" == 1 ]]; then
- read -p "Enter the Location of the rootfs tar archive: " file
- if [ ! -f "$file" ]; then
- echo -e "ERROR: Required File does not exist at $file."
- exit 1
- fi
- echo -e "Please Wait till we check the rootfs tar archive for Corruption."
- if tar -t -f "$file" &>/dev/null; then
- read -p "Enter a non-existing Directory Location to Extract the rootfs: " dir
- if [ ! -d "$dir" ]; then
- mkdir -p "$dir"
- echo -e "Directory Created at $dir"
- else
- echo -e "Directory already exists at $dir"
- exit 1
- fi
- echo "Extracting rootfs..."
- tar -xpf "$file" -C "$dir"
- else
- echo "Error: The file is not a valid tar archive or is corrupted. Exiting Script."
- exit 1
- fi
- essential_dirs=("bin" "etc" "lib" "dev" "proc" "sys" "sbin")
- if [ ! -d "$dir" ]; then
- echo -e "Error:Rootfs Directory $dir does NOT exists."
- exit 1
- else
- for rootfs_dirs in "{$essential_dirs[@]}"; do
- if [ ! -d "$dir/$essential_dirs" ]; then
- echo -e "Directory $dir/$essential_dirs does NOT exists ! \nThe provided TAR File may not be a rootfs !"
- exit 1
- fi
- done
- fi
- mount -t proc proc "$dir/proc"
- mount -t sysfs sys "$dir/sys"
- mount --bind /dev "$dir/dev"
- mount -t devpts devpts "$dir/dev/pts"
- env -i LD_PRELOAD='' TERM=xterm chroot "$dir" /usr/bin/bash --login -c "export PATH=/usr/bin:/bin:/usr/sbin:/sbin; echo -e '!!! EXITING WILL KILL ALL THE PROCESSES INSIDE THIS CHROOT !!! \nTo get internet access you may want to execute: \necho nameserver 8.8.8.8 >> /etc/resolv.conf'; /bin/bash"
- umount "$dir/dev/pts"
- umount "$dir/dev"
- umount "$dir/sys"
- umount "$dir/proc"
- elif [[ "$choice" == 2 ]]; then
- essential_dirs=("bin" "etc" "lib" "dev" "proc" "sys" "sbin")
- read -p "Enter the rootfs Directory Location: " dir
- if [ ! -d "$dir" ]; then
- echo -e "Error:Rootfs Directory $dir does NOT exists."
- exit 1
- else
- for rootfs_dirs in "{$essential_dirs[@]}"; do
- if [ ! -d "$dir/$essential_dirs" ]; then
- echo -e "Directory $dir/$essential_dirs does NOT exists ! \nThe provided Directory may NOT be a rootfs !"
- exit 1
- fi
- done
- mount -t proc proc "$dir/proc"
- mount -t sysfs sys "$dir/sys"
- mount --bind /dev "$dir/dev"
- mount -t devpts devpts "$dir/dev/pts"
- env -i LD_PRELOAD='' TERM=xterm chroot "$dir" /usr/bin/bash --login -c "export PATH=/usr/bin:/bin:/usr/sbin:/sbin; echo -e '!!! EXITING WILL KILL ALL THE PROCESSES INSIDE THIS CHROOT !!! \nTo get internet access you may want to execute: \necho nameserver 8.8.8.8 >> /etc/resolv.conf'; /bin/bash"
- umount "$dir/dev/pts"
- umount "$dir/dev"
- umount "$dir/sys"
- umount "$dir/proc"
- fi
- elif [[ "$choice" == 3 ]]; then
- essential_dirs=("bin" "etc" "lib" "dev" "proc" "sys" "sbin")
- read -p "Enter the rootfs Directory Location: " dir
- if [ ! -d "$dir" ]; then
- echo -e "Error:Rootfs Directory $dir does NOT exists."
- else
- for rootfs_dirs in "{$essential_dirs[@]}"; do
- if [ ! -d "$dir/$essential_dirs" ]; then
- echo -e "Directory $dir/$essential_dirs does NOT exists ! \nThe provided Directory may NOT be a rootfs !"
- exit 1
- fi
- done
- read -p "Do you want to delete the rootfs at $dir ? (y/N): " confirm_delete
- confirm_delete="${confirm_delete,,}"
- if [[ "$confirm_delete" == "y" || "$confirm_delete" == "yes" ]]; then
- rm -r "$dir"
- echo -e "Rootfs at $dir has been Deleted."
- else
- echo -e "Rootfs at $dir has NOT been Deleted."
- fi
- fi
- else
- echo "Exiting Script..."
- exit 0
- fi
Advertisement
Add Comment
Please, Sign In to add comment