Advertisement
rockdrilla

chr - chroot(1) helper (too bad and ugly)

Sep 3rd, 2014
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.02 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #### $ which chr
  4. #### /opt/scripts/chr
  5. #### $ cat /etc/sudoers.d/_opt_scripts_chr
  6. #### %sudo ALL=(ALL:ALL) NOPASSWD: /opt/scripts/chr
  7.  
  8. test -z "$1" && exit 1
  9.  
  10. # : ${CHR_ROOT:=$1}
  11. CHR_ROOT=$(readlink -e "$1")
  12. if test ! -d "${CHR_ROOT}"; then CHR_ROOT=$(readlink -e "/home/$1"); fi
  13. test ! -d "${CHR_ROOT}" && echo 'not found' && exit 1
  14.  
  15. : ${CHR_WRAP:=$2}
  16. : ${CHR_HOME:=$3}
  17. : ${CHR_HOME:=${HOME}}
  18. : ${CHR_PIPE:=${CHR_HOME}/gentoo}
  19.  
  20. test $(id -u) != 0 && exec sudo -n $0 "${CHR_ROOT}" "${CHR_WRAP}" "${CHR_HOME}"
  21. test $(id -u) != 0 && echo 'cant call myself with sudo' && exit 1
  22.  
  23. MNT_LIST=''
  24. MNT_TMPFS='/tmp /var/cache/apt/archives /var/lib/apt/lists /run'
  25. MNT_BIND='/sys /proc /dev /dev/pts /dev/shm /proc/sys/fs/binfmt_misc'
  26. DIR_FIXUP='/run/lock /run/user'
  27.  
  28. if test -d "${CHR_PIPE}"; then
  29.     MNT_BIND=${MNT_BIND}" ${CHR_PIPE}:/gentoo"
  30. fi
  31.  
  32. for i in ${MNT_TMPFS[@]}; do
  33.     d=${CHR_ROOT}$i
  34. #   test ! -d "$i" && continue
  35.     mount -t tmpfs -o size=70% tmpfs "$d"
  36.     MNT_LIST=$d' '${MNT_LIST}
  37. done
  38. for i in ${MNT_BIND[@]}; do
  39.     s=$i
  40.     d=${CHR_ROOT}$i
  41. #   if echo "$i" | fgrep -qe ':'; then
  42.     if [[ "$i" =~ ':' ]]; then
  43.     #   s=$(echo "$i" | cut -d':' -f1)
  44.         s=${i%:*}
  45.     #   d=${CHR_ROOT}$(echo "$i" | cut -d':' -f2)
  46.         d=${CHR_ROOT}${i#*:}
  47.     fi
  48. #   test ! -d "$d" && continue
  49.     mount --bind "$s" "$d"
  50.     MNT_LIST=$d' '${MNT_LIST}
  51. done
  52. for i in ${DIR_FIXUP[@]}; do
  53.     mkdir -p "${CHR_ROOT}$i"
  54. done
  55.  
  56. ionice -c 3 nice -n +40 ${CHR_WRAP} chroot "${CHR_ROOT}" login -f root
  57.  
  58. for i in ${MNT_LIST[@]}; do
  59. #   test ! -d "$i" && continue
  60.     umount -f -l "$i"
  61. done
  62.  
  63. if test -d "${CHR_PIPE}"; then
  64.     ug=$(stat -c %U:%G "${CHR_PIPE}")
  65. #   find "${CHR_PIPE}" \
  66. #       \( -exec chown -c ${ug} {} + -exec chmod -c a+r,u+w,go-w {} + \) , \
  67. #       \( -perm /0100 -exec chmod -c a+x {} + \)
  68.     find "${CHR_PIPE}" \
  69.         \( -exec chown -c ${ug} {} + \) , \
  70.         \(   -perm /0100 -exec chmod -c 755 {} + \) , \
  71.         \( ! -perm /0100 -exec chmod -c 644 {} + \)
  72. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement