s243a

inronchroot (Wrong Approach - binds to propagate up))

Jan 16th, 2021 (edited)
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.08 KB | None | 0 0
  1. #!/bin/bash
  2. export LC_ALL=C
  3. SFS_NAME="$(ls -1 ../*iron*.sfs | head -n 1 | sed -r 's#^[.][.]/##g' | sed -r 's#[.]sfs##g')"
  4. Parent_WD="$(realpath "$PWD/..")"
  5. SFS_PATH="$Parent_WD/$SFS_NAME"
  6. Cont_Root=/mnt/cont
  7. CONT_NAME_SOUCE=cont #These manes must have a length greater than 1 so as to exclude "/". I'll make this more general later
  8. CONT_NAME_DEST=cont
  9.  
  10. Mount_PT_ID="" #Don't edit this
  11. function umountall(){
  12.   set -x
  13.   umount -l /${CONT_NAME_DEST}/tmp/.X11-unix   
  14.   umount -l /${CONT_NAME_DEST}/dev/pts
  15.   umount -l /${CONT_NAME_DEST}/dev
  16.   umount -l /${CONT_NAME_DEST}/proc
  17.   umount -l /${CONT_NAME_DEST}/sys
  18.   umount -l "${BIND_Target}"
  19.  
  20.   for to_remove in "${Cont_Layer}" "/${CONT_NAME_DEST}"; do
  21.     to_remove="$(realpath "/${to_remove}")"
  22.     if [ -z "$(cat /proc/mounts | grep "${to_remove}")" ] &&
  23.        [ ${#to_remove} -gt 1 ]; then
  24.       rm -rf ${to_remove}
  25.     fi
  26.   done
  27.  
  28.   xhost -
  29. }
  30.  
  31. trap 'umountall' 1
  32.  
  33. loop=$(losetup -a | grep  "$SFS_PATH"  | sed "s/:.*$//" )
  34. if [ ! -z "$loop" ]; then
  35.   Mount_PT=$(findmnt -o TARGET,SOURCE -D -n | grep "${loop}\$" | cut -f1 -d' ')
  36. else
  37.   Mount_PT=${CHROOT_DIR:-/mnt/cont/sfs_img}
  38.   if [ ! -z "$(ls -A "$Mount_PT")" ]; then
  39.     Mount_PT=$(mktemp -d -p "$Cont_Root" sfs_img.XXXXXXX)
  40.     Mount_PT_ID=".${Mount_PT##*.}"
  41.   fi
  42.   mkdir -p "${Mount_PT}"
  43.   mount -o loop "$SFS_PATH" "$Mount_PT"
  44. fi
  45. #MNTPNT=${CHROOT_DIR:-/mnt/chroot-$SFS_NAME}
  46.  
  47. if [ ${#CONT_NAME_SOUCE} -le 1 ]; then
  48.   BIND_Source="${Mount_PT}"
  49. else
  50.   BIND_Source="${Mount_PT}/${CONT_NAME_SOUCE}"
  51. fi
  52.  
  53. BIND_Target_Root="$Cont_Root"/sfs__target${Mount_PT_ID}
  54. if [ ${#Mount_PT_ID} -gt 0 ]; then
  55.   BIND_Target_Root="$Cont_Root"/sfs__target${Mount_PT_ID}
  56. elif [ -z "$(ls -A "$BIND_Target_Root")" ]; then
  57.   BIND_Target_Root="$Cont_Root"/sfs__target
  58. else
  59.  BIND_Target_Root=$(mktemp -d -p "$Cont_Root" sfs__target.XXXXXXX)
  60. fi
  61. mount -t tmpfs none $BIND_Target_Root;
  62. if [ ${#CONT_NAME_DEST} -gt 0 ]; then
  63.   BIND_Target=$BIND_Target_Root/${CONT_NAME_DEST}
  64. else
  65.   BIND_Target=$BIND_Target_Root
  66. fi
  67.  
  68.  
  69.  
  70. mkdir -p "$BIND_Target"
  71.  
  72. NEW=''
  73. MOUNTED_PUP_RO=$(busybox df | grep -o '/initrd/pup_ro.*')
  74. # pup_ro1 and pup_ro2 are reserved
  75. for i in $(seq 3 99) # find free pup_roX
  76. do
  77.     if ! [ "$(echo "$MOUNTED_PUP_RO" | grep "pup_ro${i}$")" ] ; then
  78.         NEW=${i}
  79.         break
  80.     fi
  81. done
  82. Cont_Layer=/initrd/pup_ro$NEW
  83.  
  84.  
  85.  
  86.  
  87. mount --bind "${BIND_Source}" "${BIND_Target}"
  88. ln -s "$BIND_Target_Root" "$Cont_Layer"
  89. set +x
  90. read -p "Press enter to continue"
  91. set -x
  92.  
  93.  
  94. busybox mount -t aufs -o remount,append:$BIND_Target_Root=rr / || { umountall && exit 1; }
  95.  
  96.  
  97. mount --bind /dev /${CONT_NAME_DEST}/dev
  98. mount --bind /proc /${CONT_NAME_DEST}/proc
  99. mount --bind /sys /${CONT_NAME_DEST}/sys
  100. mount -t devpts devpts /${CONT_NAME_DEST}/dev/pts
  101. cp /etc/resolv.conf /${CONT_NAME_DEST}/etc/resolv.conf
  102. cp /var/lib/dbus/machine-id /${CONT_NAME_DEST}/var/lib/dbus/machine-id
  103. xhost +
  104. mkdir -p /${CONT_NAME_DEST}/tmp/.X11-unix
  105. mount --rbind /tmp/.X11-unix /${CONT_NAME_DEST}/tmp/.X11-unix
  106. set +x
  107. read -p "Ready to chroot Press enter to continue"
  108. chroot /${CONT_NAME_DEST} iron "$@"
  109. set -x
  110. umountall
  111.  
Add Comment
Please, Sign In to add comment