s243a

ironchroot

Jan 17th, 2021
665
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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".sfs
  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_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. mkdir -p /${CONT_NAME_DEST}
  34. if [ ! -z "$(ls -A "/${CONT_NAME_DEST}")" ]; then
  35.   cd /
  36.   CONT_NAME_DEST=$(mktemp -u ${CONT_NAME_DEST}.XXXXXXX)
  37. fi
  38. mkdir -p "/$CONT_NAME_DEST"
  39.  
  40. loop=$(losetup -a | grep  "$SFS_PATH"  | sed "s/:.*$//" )
  41. if [ ! -z "$loop" ]; then
  42.   Mount_PT=$(findmnt -o TARGET,SOURCE -D -n | grep "${loop}\$" | cut -f1 -d' ')
  43. else
  44.   Mount_PT=${CHROOT_DIR:-/mnt/cont/sfs_img}
  45.   if [ ! -z "$(ls -A "$Mount_PT")" ]; then
  46.     Mount_PT=$(mktemp -d -p "$Cont_Root" sfs_img.XXXXXXX)
  47.     Mount_PT_ID=".${Mount_PT##*.}"
  48.   fi
  49.   mkdir -p "${Mount_PT}"
  50.   mount -o loop "$SFS_PATH" "$Mount_PT"
  51. fi
  52. #MNTPNT=${CHROOT_DIR:-/mnt/chroot-$SFS_NAME}
  53.  
  54. if [ ${#CONT_NAME_SOUCE} -le 1 ]; then
  55.   BIND_Source="${Mount_PT}"
  56. else
  57.   BIND_Source="${Mount_PT}/${CONT_NAME_SOUCE}"
  58. fi
  59. mkdir -p "$BIND_Source"
  60.  
  61. BIND_Target_Root="$Cont_Root"/sfs__target${Mount_PT_ID}
  62. if [ ${#Mount_PT_ID} -gt 0 ]; then
  63.   BIND_Target_Root="$Cont_Root"/sfs__target${Mount_PT_ID}
  64. elif [ -z "$(ls -A "$BIND_Target_Root")" ]; then
  65.   BIND_Target_Root="$Cont_Root"/sfs__target
  66. else
  67.  BIND_Target_Root=$(mktemp -d -p "$Cont_Root" sfs__target.XXXXXXX)
  68. fi
  69. mkdir -p "$BIND_Target_Root"
  70.  
  71. if [ ${#CONT_NAME_DEST} -gt 0 ]; then
  72.   BIND_Target=$BIND_Target_Root/${CONT_NAME_DEST}
  73. else
  74.   BIND_Target=$BIND_Target_Root
  75. fi
  76. mkdir -p "$BIND_Target"
  77.  
  78. if [ ${#Mount_PT_ID} -gt 0 ]; then
  79.   Cont_TMPFS="$Cont_Root"/tmpfs${Mount_PT_ID}
  80. elif [ -z "$(ls -A "${Cont_Root}/tmpfs")" ]; then
  81.   Cont_TMPFS="$Cont_Root"/tmpfs
  82. else
  83.  Cont_TMPFS=$(mktemp -d -p "$Cont_Root" tmpfs.XXXXXXX)
  84. fi
  85. mkdir -p "$Cont_TMPFS"
  86. mount -t tmpfs none "$Cont_TMPFS"
  87. #mount -t tmpfs none $BIND_Target_Root;
  88.  
  89.  
  90. #NEW=''
  91. #MOUNTED_PUP_RO=$(busybox df | grep -o '/initrd/pup_ro.*')
  92. ## pup_ro1 and pup_ro2 are reserved
  93. #for i in $(seq 3 99) # find free pup_roX
  94. #do
  95. #   if ! [ "$(echo "$MOUNTED_PUP_RO" | grep "pup_ro${i}$")" ] ; then
  96. #       NEW=${i}
  97. #       break
  98. #   fi
  99. #done
  100. #Cont_Layer=/initrd/pup_ro$NEW
  101.  
  102.  
  103.  
  104.  
  105. mount --bind "${BIND_Source}" "${BIND_Target}"
  106. #ln -s "$BIND_Target_Root" "$Cont_Layer"
  107. set +x
  108. read -p "Press enter to continue"
  109. set -x
  110.  
  111. busybox mount -t aufs -o "udba=reval,diropq=w,br:${Cont_TMPFS}=rw:${BIND_Target}=rr" aufs "/$CONT_NAME_DEST" || { umountall && exit 1; }
  112. #busybox mount -t aufs -o remount,append:$BIND_Target_Root=rr / || { umountall && exit 1; }
  113.  
  114.  
  115. mount --bind /dev /${CONT_NAME_DEST}/dev
  116. mount --bind /proc /${CONT_NAME_DEST}/proc
  117. mount --bind /sys /${CONT_NAME_DEST}/sys
  118. mount -t devpts devpts /${CONT_NAME_DEST}/dev/pts
  119. cp /etc/resolv.conf /${CONT_NAME_DEST}/etc/resolv.conf
  120. cp /var/lib/dbus/machine-id /${CONT_NAME_DEST}/var/lib/dbus/machine-id
  121. xhost +
  122. mkdir -p /${CONT_NAME_DEST}/tmp/.X11-unix
  123. mount --rbind /tmp/.X11-unix /${CONT_NAME_DEST}/tmp/.X11-unix
  124. set +x
  125. read -p "Ready to chroot Press enter to continue"
  126. chroot /${CONT_NAME_DEST} iron "$@"
  127. set -x
  128. umountall
  129.  
RAW Paste Data