Guest User

termux-chroot-arm64.sh

a guest
Aug 30th, 2025
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.18 KB | Source Code | 0 0
  1. #!/data/data/com.termux/files/usr/bin/bash
  2. set -e
  3. if [ "$EUID" -ne 0 ]; then
  4.     echo "This script must be run as root."
  5.     exit 1
  6. fi
  7. 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.'
  8. read -p 'Enter Your Choice (1/2/3): ' choice
  9. choice="${choice,,}"
  10. if [[ "$choice" == 1 ]]; then
  11.     read -p "Enter the Location of the rootfs tar archive: " file
  12.     if [ ! -f "$file" ]; then
  13.         echo -e "ERROR: Required File does not exist at $file."
  14.         exit 1
  15.     fi
  16.     echo -e "Please Wait till we check the rootfs tar archive for Corruption."
  17.     if tar -t -f "$file"  &>/dev/null; then
  18.         read -p "Enter a non-existing Directory Location to Extract the rootfs: " dir
  19.         if [ ! -d "$dir" ]; then
  20.             mkdir -p "$dir"
  21.             echo -e "Directory Created at $dir"
  22.         else
  23.             echo -e "Directory already exists at $dir"
  24.             exit 1
  25.         fi
  26.         echo "Extracting rootfs..."
  27.         tar -xpf "$file" -C "$dir"
  28.     else
  29.         echo "Error: The file is not a valid tar archive or is corrupted. Exiting Script."
  30.         exit 1
  31.     fi
  32.     essential_dirs=("bin" "etc" "lib" "dev" "proc" "sys" "sbin")
  33.         if [ ! -d "$dir" ]; then
  34.                 echo -e "Error:Rootfs Directory $dir does NOT exists."
  35.                 exit 1
  36.         else
  37.                 for rootfs_dirs in  "{$essential_dirs[@]}"; do
  38.                         if [ ! -d "$dir/$essential_dirs" ]; then
  39.                                 echo -e "Directory $dir/$essential_dirs does NOT exists ! \nThe provided TAR File may not be a rootfs !"
  40.                                 exit 1
  41.             fi
  42.                 done
  43.     fi
  44.     mount -t proc proc "$dir/proc"
  45.     mount -t sysfs sys "$dir/sys"
  46.     mount --bind /dev "$dir/dev"
  47.     mount -t devpts devpts "$dir/dev/pts"
  48.     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"
  49.     umount "$dir/dev/pts"
  50.     umount "$dir/dev"
  51.     umount "$dir/sys"
  52.     umount "$dir/proc"
  53. elif [[ "$choice" == 2 ]]; then
  54.     essential_dirs=("bin" "etc" "lib" "dev" "proc" "sys" "sbin")
  55.     read -p "Enter the rootfs Directory Location: " dir
  56.     if [ ! -d "$dir" ]; then
  57.         echo -e "Error:Rootfs Directory $dir does NOT exists."
  58.         exit 1
  59.         else
  60.         for rootfs_dirs in  "{$essential_dirs[@]}"; do
  61.             if [ ! -d "$dir/$essential_dirs" ]; then
  62.                 echo -e "Directory $dir/$essential_dirs does NOT exists ! \nThe provided Directory may NOT be a rootfs !"
  63.                 exit 1
  64.             fi
  65.         done
  66.                 mount -t proc proc "$dir/proc"
  67.         mount -t sysfs sys "$dir/sys"
  68.         mount --bind /dev "$dir/dev"
  69.         mount -t devpts devpts "$dir/dev/pts"
  70.         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"
  71.         umount "$dir/dev/pts"
  72.         umount "$dir/dev"
  73.         umount "$dir/sys"
  74.         umount "$dir/proc"
  75.         fi
  76. elif [[ "$choice" == 3 ]]; then
  77.     essential_dirs=("bin" "etc" "lib" "dev" "proc" "sys" "sbin")
  78.         read -p "Enter the rootfs Directory Location: " dir
  79.         if [ ! -d "$dir" ]; then
  80.                 echo -e "Error:Rootfs Directory $dir does NOT exists."
  81.         else
  82.                 for rootfs_dirs in  "{$essential_dirs[@]}"; do
  83.                         if [ ! -d "$dir/$essential_dirs" ]; then
  84.                                 echo -e "Directory $dir/$essential_dirs does NOT exists ! \nThe provided Directory may NOT be a rootfs !"
  85.                                 exit 1
  86.                         fi
  87.                 done
  88.         read -p "Do you want to delete the rootfs at $dir ? (y/N): " confirm_delete
  89.             confirm_delete="${confirm_delete,,}"
  90.             if [[ "$confirm_delete" == "y" || "$confirm_delete" == "yes" ]]; then
  91.                     rm -r "$dir"
  92.                     echo -e "Rootfs at $dir has been Deleted."
  93.             else
  94.                     echo -e "Rootfs at $dir has NOT been Deleted."
  95.             fi
  96.     fi
  97. else
  98.     echo "Exiting Script..."
  99.     exit 0
  100. fi
  101.  
Advertisement
Add Comment
Please, Sign In to add comment