Chroot-tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
4.7KB

  1. #!/bin/bash
  2. # WTFPL
  3. set -e
  4. ## functions
  5. # ==========
  6. set_chroot (){
  7. case "$option" in
  8. "exit") chroot_umount $folder ;;
  9. "proot") chroot_proot $folder ;;
  10. "fakechroot") chroot_fakechroot $folder ;;
  11. "systemd") chroot_systemd $folder ;;
  12. "chroot") chroot_chroot $folder ;;
  13. "arch") chroot_arch $folder ;;
  14. "mac") chroot_darwin $folder ;;
  15. "darwin") chroot_darwin $folder ;;
  16. "bsd") chroot_bsd $folder ;;
  17. *) printf "unknown option\n" ;;
  18. esac
  19. }
  20. super_chroot(){
  21. OS=$(uname)
  22. if [ "$UID" -ne 0 ]; then
  23. if [ $OS == "Darwin" ] || [ $OS == "FreeBSD" ]; then
  24. printf "You need root access.\n"
  25. elif [ $OS == "Linux" ]; then
  26. if which fakechroot >/dev/null; then
  27. printf "fakechroot found...\n"
  28. chroot_fakechroot $folder
  29. elif which proot >/dev/null; then
  30. printf "proot found ...\n"
  31. chroot_proot $folder
  32. else
  33. printf "I can not find fakechroot nor proot and you aren't root. Sorry, but I can\'t chroot.\n"
  34. fi
  35. else
  36. printf "Unknown plattform\n"
  37. fi
  38. else
  39. if [ $OS == "Darwin" ]; then
  40. chroot_darwin $folder
  41. elif [ $OS == "FreeBSD" ]; then
  42. chroot_bsd $folder
  43. elif [ $OS == "Linux" ]; then
  44. if which systemd-nspawn >/dev/null; then
  45. chroot_systemd $folder
  46. elif which arch-chroot >/dev/null; then
  47. chroot_arch $folder
  48. else
  49. printf "\nRunning classic chroot with mount -t... \n\n"
  50. chroot_chroot $folder
  51. fi
  52. else
  53. printf "Unknown plattform\n"
  54. fi
  55. fi
  56. }
  57. chroot_mount() {
  58. OS=$(uname)
  59. if [ $OS == "FreeBSD" ]; then
  60. if ! df -P -T $folder/dev/|grep nullfs>/dev/null; then
  61. mount_nullfs /proc $folder/proc
  62. mount_nullfs /dev $folder/dev
  63. mount_nullfs /run $folder/run
  64. mount_nullfs /tmp $folder/tmp
  65. fi
  66. elif [ $OS == "Linux" ]; then
  67. mountpoint -q "$folder/proc" || mount proc "$folder/proc" -t proc
  68. mountpoint -q "$folder/sys" || mount sys "$folder/sys" -t sysfs
  69. mountpoint -q "$folder/dev" || mount udev "$folder/dev" -t devtmpfs
  70. mountpoint -q "$folder/dev/pts" || mount devpts "$folder/dev/pts" -t devpts
  71. mountpoint -q "$folder/dev/shm" || mount shm "$folder/dev/shm" -t tmpfs
  72. mountpoint -q "$folder/run" || mount run "$folder/run" -t tmpfs
  73. mountpoint -q "$folder/tmp" || mount tmp "$folder/tmp" -t tmpfs
  74. fi
  75. }
  76. mount_bindfs() {
  77. bindfs /dev $folder/dev
  78. bindfs /tmp $folder/tmp
  79. }
  80. umount_bindfs() {
  81. fusermount -u $folder/dev
  82. fusermount -u $folder/tmp
  83. }
  84. chroot_umount() {
  85. OS=$(uname)
  86. if [ $OS == "Darwin" ]; then
  87. umount_bindfs $folder
  88. elif [ $OS == "FreeBSD" ]; then
  89. umount $folder/proc
  90. umount $folder/dev
  91. umount $folder/run
  92. umount $folder/tmp
  93. else
  94. printf "Doing lazy umount chroot folder ...\n"
  95. umount -l "$folder/proc"
  96. umount -l "$folder/sys"
  97. umount -l "$folder/dev/pts"
  98. umount -l "$folder/dev/shm"
  99. umount -l "$folder/dev"
  100. umount -l "$folder/run"
  101. umount -l "$folder/tmp"
  102. fi
  103. }
  104. chroot_proot(){
  105. proot -r $folder
  106. }
  107. chroot_fakechroot(){
  108. export FAKECHROOT_EXCLUDE_PATH=/proc:/dev:/sys
  109. fakechroot -s fakeroot chroot $folder
  110. }
  111. chroot_systemd(){
  112. systemd-nspawn -D $folder
  113. }
  114. chroot_arch(){
  115. arch-chroot $folder
  116. }
  117. chroot_chroot(){
  118. chroot_mount "$folder"
  119. chroot $folder $terminal
  120. }
  121. chroot_darwin(){
  122. mount_bindfs "$folder"
  123. chroot $folder
  124. }
  125. chroot_bsd(){
  126. chroot_mount "$folder"
  127. chroot $folder
  128. }
  129. print_help(){
  130. echo '
  131. superchroot <folder>
  132. superchroot [chroot-option] <folder>
  133. [chroot options]: systemd (root), chroot (root), arch (root), mac (root), bsd (root), fakechroot, proot, exit (umount directories from chroot methode)
  134. EXAMPLES - force method
  135. ~$: su -c "superchroot systemd arch/"
  136. ~$: sudo superchroot chroot arch/
  137. ~$: superchroot proot arch/
  138. ~$: superchroot fakechroot arch/
  139. EXAMPLES - automaticaly best choice
  140. ~#: superchroot arch/
  141. ~$: superchroot arch/
  142. ATTENTION
  143. On a Linux system: you can only chroot Linux systems!
  144. On a FreeBSD system: you can only chroot FreeBSD systems!
  145. On a MacOS system: you can only chroot MacOS systems!
  146. '
  147. }
  148. ## Variable
  149. ## ========
  150. terminal="/bin/bash"
  151. ## MAIN
  152. ## ====
  153. if (("$#" == 1)); then
  154. if [[ "$1" = "--help" ]]; then
  155. print_help
  156. exit
  157. fi
  158. folder=$1
  159. super_chroot "$@"
  160. elif (("$#" == 2)); then
  161. option=$1
  162. folder=$2
  163. set_chroot "$@"
  164. else
  165. printf "Wrong usage!\n\n"
  166. print_help
  167. exit
  168. fi