s243a

ironchroot (with options)

Jan 17th, 2021 (edited)
828
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. export LC_ALL=C
  3. cd "$(realpath "${0%/*}")"
  4. unset CHROOT_CMD
  5. unset EXECUTE
  6. unset NO_EXIT
  7. unset Mount_PT_to_Del
  8. declare -a options="$(getopt -o c:,e:,n: --long chroot-cmd::,chroot-command,cmd::,::command::,no-exit:: -- "$@")"
  9. eval set --"$options_str"
  10. eval set --"$options"
  11. while [ $# -gt 0 ]; do
  12.   case "$1" in
  13.   -c|--chroot-cmd|--chroot-command)
  14.     if [ $# -lt 2 ] || [[ $2 = 1* ]]; then
  15.       CHROOT_CMD=( iron )
  16.       shift 1
  17.     else
  18.       CHROOT_CMD=( $2 )
  19.       shift 2
  20.     fi
  21.     [ -z ${EXECUTE+x} ] && EXECUTE=""
  22.     ;;
  23.   -e|--cmd|--command)
  24.     if [ $# -lt 2 ] || [[ $2 = 1* ]]; then
  25.       EXECUTE=( iron )
  26.       shift 1
  27.     else
  28.       EXECUTE=( $2 )
  29.       shift 2
  30.     fi
  31.     [ -z ${CHROOT_CMD+x} ] && CHROOT_CMD=""
  32.     ;;  
  33.   --no-exit)
  34.     if [ $# -lt 2 ] || [[ $2 = 1* ]]; then
  35.       NO_EXIT=true
  36.       shift 1
  37.     else
  38.       NO_EXIT=$2
  39.       shift 2
  40.     fi
  41.     ;;
  42.   --)
  43.     shift
  44.     break
  45.     ;;
  46.   *)
  47.     shift
  48.     ;;
  49.   esac    
  50. done
  51. if [ -z ${NO_EXIT+x} ]; then
  52.   NO_EXIT=false
  53. fi
  54. if [ -z ${CHROOT_CMD+x} ] && [ -z ${EXECUTE+x} ]; then
  55.   CHROOT_CMD=( iron )
  56.   EXECUTE=""
  57. fi
  58. SFS_NAME="$(ls -1 ../*iron*.sfs | head -n 1 | sed -r 's#^[.][.]/##g' | sed -r 's#[.]sfs##g')"
  59. Parent_WD="$(realpath "$PWD/..")"
  60. SFS_PATH="$Parent_WD/$SFS_NAME".sfs
  61. Cont_Root=/mnt/cont
  62. #TODO: fix spelling of CONT_NAME_SOUCE
  63. CONT_NAME_SOUCE=cont #These manes must have a length greater than 1 so as to exclude "/". I'll make this more general later
  64. CONT_NAME_DEST=cont
  65.  
  66. Mount_PT_ID="" #Don't edit this
  67. function umountall(){
  68.   set -x
  69.  
  70.   umount -l /${CONT_NAME_DEST}/tmp/.X11-unix   
  71.   umount -l /${CONT_NAME_DEST}/dev/pts
  72.   umount -l /${CONT_NAME_DEST}/dev
  73.   umount -l /${CONT_NAME_DEST}/proc
  74.   umount -l /${CONT_NAME_DEST}/sys
  75.   umount -l "/$CONT_NAME_DEST"
  76.   umount -l "$Cont_TMPFS"
  77.   umount -l "${BIND_Target}"
  78.   if [ -z ${Mount_PT_to_Del+x} ]; then
  79.     files_to_remove=("/${CONT_NAME_DEST}" "$Cont_TMPFS" "$BIND_Target_Root")
  80.   else
  81.     files_to_remove=("/${CONT_NAME_DEST}" "$Cont_TMPFS" "$Mount_PT_to_Del" "$BIND_Target_Root")
  82.   fi
  83.   for to_remove in "${files_to_remove[@]}"; do
  84.     to_remove="$(realpath "/${to_remove}")"
  85.     if [ -z "$(cat /proc/mounts | grep "${to_remove}")" ] &&
  86.        [ ${#to_remove} -gt 1 ]; then
  87.       rm -rf ${to_remove}
  88.     fi
  89.   done
  90.  
  91.   xhost -
  92. }
  93.  
  94. trap 'umountall' 1
  95.  
  96. mkdir -p /${CONT_NAME_DEST}
  97. if [ ! -z "$(ls -A "/${CONT_NAME_DEST}")" ]; then
  98.   cd /
  99.   CONT_NAME_DEST=$(mktemp -u ${CONT_NAME_DEST}.XXXXXXX)
  100. fi
  101. mkdir -p "/$CONT_NAME_DEST"
  102.  
  103. append_mnt_id_awk='
  104. function get_mnt_id(mnt_pt,loop){
  105.  if (length(mnt_pt) > 0 && length(loop)>0){
  106.    cmd="cat /proc/self/mountinfo | sort | grep '" loop "' | grep " mnt_pt " | head -n 1"
  107.  } else if (length(mnt_pt) > 0){
  108.    cmd="cat /proc/self/mountinfo | sort | grep '" mnt_pt "' | head -n 1"
  109.  } else if (length(loop)>0){
  110.    cmd="cat /proc/self/mountinfo | sort | grep '" loop "' | head -n 1"
  111.  }
  112.  while ((cmd | getline )){
  113.    mnt_id=$1
  114.    break  
  115.  }
  116.  close(cmd)
  117.  return mnt_id
  118. }
  119. {
  120.  mnt_pt=$1
  121.  loop=$2
  122.  mnt_id=get_mnt_id(mnt_pt,loop)
  123.  print mnt_id "|" mnt_pt "|" loop
  124. }'
  125.  
  126. loop=$(losetup -a | grep  "$SFS_PATH"  | sed "s/:.*$//" )
  127. if [ ! -z "$loop" ]; then
  128.   #Mount_PT=$(findmnt -o TARGET,SOURCE -D -n | grep "${loop}\$" | cut -f1 -d' ')
  129.   Mount_PT="$(findmnt -o TARGET,SOURCE -D -n | grep 'loop5$' | awk "$append_mnt_id_awk" | sort -t '|' -k1 | cut -d'|' -f2 | head -n 1)"
  130. else
  131.   Mount_PT=${CHROOT_DIR:-/mnt/cont/sfs_img}
  132.   if [ ! -z "$(ls -A "$Mount_PT")" ]; then
  133.     Mount_PT=$(mktemp -d -p "$Cont_Root" sfs_img.XXXXXXX)
  134.     Mount_PT_ID=".${Mount_PT##*.}"
  135.   fi
  136.   mkdir -p "${Mount_PT}"
  137.   mount -o loop "$SFS_PATH" "$Mount_PT"
  138.   Mount_PT_to_Del=$Mount_PT
  139. fi
  140. #MNTPNT=${CHROOT_DIR:-/mnt/chroot-$SFS_NAME}
  141.  
  142. if [ ${#CONT_NAME_SOUCE} -le 1 ]; then
  143.   BIND_Source="${Mount_PT}"
  144. else
  145.   BIND_Source="${Mount_PT}/${CONT_NAME_SOUCE}"
  146. fi
  147. mkdir -p "$BIND_Source"
  148.  
  149. BIND_Target_Root="$Cont_Root"/sfs__target${Mount_PT_ID}
  150. if [ ${#Mount_PT_ID} -gt 0 ]; then
  151.   BIND_Target_Root="$Cont_Root"/sfs__target${Mount_PT_ID}
  152. elif [ -z "$(ls -A "$BIND_Target_Root")" ]; then
  153.   BIND_Target_Root="$Cont_Root"/sfs__target
  154. else
  155.  BIND_Target_Root=$(mktemp -d -p "$Cont_Root" sfs__target.XXXXXXX)
  156. fi
  157. mkdir -p "$BIND_Target_Root"
  158.  
  159. if [ ${#CONT_NAME_DEST} -gt 0 ]; then
  160.   BIND_Target=$BIND_Target_Root/${CONT_NAME_DEST}
  161. else
  162.   BIND_Target=$BIND_Target_Root
  163. fi
  164. mkdir -p "$BIND_Target"
  165.  
  166. if [ ${#Mount_PT_ID} -gt 0 ]; then
  167.   Cont_TMPFS="$Cont_Root"/tmpfs${Mount_PT_ID}
  168. elif [ -z "$(ls -A "${Cont_Root}/tmpfs")" ]; then
  169.   Cont_TMPFS="$Cont_Root"/tmpfs
  170. else
  171.  Cont_TMPFS=$(mktemp -d -p "$Cont_Root" tmpfs.XXXXXXX)
  172. fi
  173. mkdir -p "$Cont_TMPFS"
  174. mount -t tmpfs none "$Cont_TMPFS"
  175. #mount -t tmpfs none $BIND_Target_Root;
  176.  
  177.  
  178. #NEW=''
  179. #MOUNTED_PUP_RO=$(busybox df | grep -o '/initrd/pup_ro.*')
  180. ## pup_ro1 and pup_ro2 are reserved
  181. #for i in $(seq 3 99) # find free pup_roX
  182. #do
  183. #   if ! [ "$(echo "$MOUNTED_PUP_RO" | grep "pup_ro${i}$")" ] ; then
  184. #       NEW=${i}
  185. #       break
  186. #   fi
  187. #done
  188. #Cont_Layer=/initrd/pup_ro$NEW
  189.  
  190.  
  191.  
  192.  
  193. mount --bind "${BIND_Source}" "${BIND_Target}"
  194. #ln -s "$BIND_Target_Root" "$Cont_Layer"
  195. set +x
  196. read -p "Press enter to continue"
  197. set -x
  198.  
  199. busybox mount -t aufs -o "udba=reval,diropq=w,br:${Cont_TMPFS}=rw:${BIND_Target}=rr" aufs "/$CONT_NAME_DEST" || { umountall && exit 1; }
  200. #busybox mount -t aufs -o remount,append:$BIND_Target_Root=rr / || { umountall && exit 1; }
  201.  
  202.  
  203. mount --bind /dev /${CONT_NAME_DEST}/dev
  204. mount --bind /proc /${CONT_NAME_DEST}/proc
  205. mount --bind /sys /${CONT_NAME_DEST}/sys
  206. mount -t devpts devpts /${CONT_NAME_DEST}/dev/pts
  207. cp /etc/resolv.conf /${CONT_NAME_DEST}/etc/resolv.conf
  208. cp /var/lib/dbus/machine-id /${CONT_NAME_DEST}/var/lib/dbus/machine-id
  209. xhost +
  210. mkdir -p /${CONT_NAME_DEST}/tmp/.X11-unix
  211. mount --rbind /tmp/.X11-unix /${CONT_NAME_DEST}/tmp/.X11-unix
  212. cd /${CONT_NAME_DEST}
  213. set +x
  214.  
  215. if [ ! -z "${EXECUTE}" ]; then
  216.   echo "next cmd: \"${EXECUTE[@]}\" \"$@\""
  217.   read -p "Ready to EXECUTE Press enter to continue"
  218.   "${EXECUTE[@]}" "$@"
  219. fi
  220. if [ -z ${CHROOT_CMD+x} ]]; then
  221.   echo "next cmd: \"${CHROOT_CMD[@]}\" \"$@\""
  222.   read -p "Ready to chroot Press enter to continue"
  223.   "${CHROOT_CMD[@]}" "$@"
  224. fi
  225.  
  226. chroot /${CONT_NAME_DEST} iron "$@"
  227. set -x
  228. if [ ! NO_EXIT = true ]; then
  229.   umountall
  230. fi
  231.  
RAW Paste Data