chroot-rescue 852 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env bash
  2. # This script will mount and chroot a linux system
  3. # Copyright 2024 Bryan C. Roessler
  4. lsblk
  5. # Set defaults
  6. RP=${ROOT_PART:-/dev/nvme0n0p3}
  7. BP=${BOOT_PART:-/dev/nvme0n0p1}
  8. echo "Press enter to accept default value"
  9. read -r -p "Root partition [$RP]: " RP
  10. read -r -p "Boot partition [$BP]: " BP
  11. MD="${MOUNT_DIR:-/mnt/${RP##*/}}"
  12. [[ -d $MD ]] && MD=$MD-$RANDOM
  13. read -r -p "Mount directory [$MD]: " MD
  14. [[ -e $RP && -e $BP ]] || exit 1
  15. # Mount and entire the chroot
  16. echo "Mounting and entering chroot"
  17. sudo mkdir -p "$MD" &&
  18. sudo mount "$RP" "$MD" &&
  19. for i in proc sys dev; do
  20. sudo mount --bind /$i "$MD/$i"
  21. done &&
  22. sudo mount "$BP" "$MD/boot/efi" &&
  23. sudo chroot "$MD";
  24. # After chroot
  25. echo "Exiting and unmounting chroot"
  26. sudo umount "$MD/boot/efi"
  27. for i in proc sys dev; do
  28. sudo umount "$MD/$i"
  29. done
  30. sudo umount "$MD"
  31. exit $?