s243a

simplechroot (draft - 4.31 Phoenix)

Mar 7th, 2021 (edited)
1,035
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. set -x
  3. export LC_ALL=C
  4. CWD="$(realpath "${0%/*}")"
  5. cd "$CWD"
  6.  
  7. EXTRA_SFS=(../PaleMoon32_29.01-portable.sfs)
  8.  
  9. Cont_Root=/mnt/cont
  10. #TODO: fix spelling of CONT_NAME_SOUCE
  11. CONT_NAME_SOUCE="" #These manes must have a length greater than 1 so as to exclude "/". I'll make this more general later
  12. CONT_NAME_DEST="puppy4_palemoon"
  13.  
  14. unset CHROOT_CMD; [ -z "$CHROOT_CMD" ] && CHROOT_CMD=/opt/PaleMoon-portable-v29.01/LAUNCH #defaultbrowser
  15. unset EXECUTE
  16. unset NO_EXIT
  17. unset Mount_PT_to_Del
  18. declare -a options="$(busybox getopt -o c:,e:,n: -l "chroot-command::,chroot-cmd::,cmd::,command::,no-exit::,source-subdir:,dest-subdir:,xephyr-disp::,use-unshare::" -- "$@")"
  19. #eval set --"$options_str"
  20. eval set --"$options"
  21. while [ $# -gt 0 ]; do
  22.   case "$1" in
  23.   --use-unshare)
  24.     if [ $# -lt 2 ] || [[ $2 = -* ]]; then
  25.       USE_UNSHAR=":2"
  26.       shift 1
  27.     else  
  28.       USE_UNSHAR="yes"
  29.       shift 2
  30.     fi
  31.     ;;  
  32.   --xephyr-disp)
  33.     if [ $# -lt 2 ] || [[ $2 = -* ]]; then
  34.       XephyrDISPLAY=":2"
  35.       shift 1
  36.     else  
  37.       XephyrDISPLAY="$2"
  38.       shift 2
  39.     fi
  40.     ;;
  41.   -c|--chroot-cmd|--chroot-command)
  42.     if [ $# -lt 2 ] || [[ $2 = -* ]]; then
  43.       CHROOT_CMD=( /bin/sh )
  44.       shift 1
  45.     else
  46.       CHROOT_CMD=( $2 )
  47.       shift 2
  48.     fi
  49.     [ -z ${EXECUTE+x} ] && EXECUTE=""
  50.     ;;
  51.   -e|--cmd|--command)
  52.     if [ $# -lt 2 ] || [[ $2 = -* ]]; then
  53.       EXECUTE=( /bin/sh )
  54.       shift 1
  55.     else
  56.       EXECUTE=( $2 )
  57.       shift 2
  58.     fi
  59.     [ -z ${CHROOT_CMD+x} ] && CHROOT_CMD=""
  60.     ;;
  61.   --source-subdir)
  62.     CONT_NAME_SOUCE="$2"
  63.     shift 2    
  64.     ;;
  65.   --dest-subdir)
  66.     CONT_NAME_DEST="$2"
  67.     shift 2
  68.     ;;    
  69.   --no-exit)
  70.     if [ $# -lt 2 ] || [[ $2 = 1* ]]; then
  71.       NO_EXIT=true
  72.       shift 1
  73.     else
  74.       NO_EXIT=$2
  75.       shift 2
  76.     fi
  77.     ;;
  78.   --)
  79.     shift
  80.     break
  81.     ;;
  82.   *)
  83.     shift
  84.     ;;
  85.   esac    
  86. done
  87. export CONT_NAME_DEST="$CONT_NAME_DEST"
  88. if [ -z ${NO_EXIT+x} ]; then
  89.   NO_EXIT=false
  90. fi
  91. if [ -z ${CHROOT_CMD+x} ] && [ -z ${EXECUTE+x} ]; then
  92.   CHROOT_CMD=( /bin/sh )
  93.   EXECUTE=""
  94. fi
  95. for a_pattern in 'puppy_.*[.]sfs' '.*iron.*[.]sfs' '.*chromium.*[.]sfs' '.*firefox.*[.]sfs'; do
  96.   SFS_NAME="$(ls -1 .. | grep -E "$a_pattern" | head -n 1 | sed -r 's#^[.][.]/##g' | sed -r 's#[.]sfs##g')"
  97.   [ ! -z "$SFS_NAME" ] && break
  98. done
  99. Parent_WD="$(realpath "$PWD/..")"
  100. SFS_PATH="$Parent_WD/$SFS_NAME".sfs
  101. SFS_PATH="$(realpath "$SFS_PATH")"
  102.  
  103. Mount_PT_ID="" #Don't edit this
  104. function umountall(){
  105.   set +x
  106.  read -p "umountall(): Press enter to continue"
  107.   set -x
  108.  
  109.   umount -l /${CONT_NAME_DEST}/tmp/.X11-unix   
  110.   umount -l /${CONT_NAME_DEST}/dev/pts
  111.   umount -l /${CONT_NAME_DEST}/dev
  112.   umount -l /${CONT_NAME_DEST}/proc
  113.   umount -l /${CONT_NAME_DEST}/sys
  114.   umount -l "/$CONT_NAME_DEST"
  115.   umount -l "$Cont_TMPFS"
  116.   umount -l "${BIND_Target}"
  117.   for a_mp in "${EXTRA_SFS_MPS[@]}"; do
  118.     umount -l "$a_mp"
  119.   done
  120.   if [ -z ${Mount_PT_to_Del+x} ]; then
  121.     files_to_remove=("/${CONT_NAME_DEST}" "$Cont_TMPFS" "$BIND_Target_Root")
  122.   else
  123.     files_to_remove=("/${CONT_NAME_DEST}" "$Cont_TMPFS" "$Mount_PT_to_Del" "$BIND_Target_Root")
  124.   fi
  125.   for to_remove in "${files_to_remove[@]}"; do
  126.     to_remove="$(realpath "/${to_remove}")"
  127.     if [ -z "$(cat /proc/mounts | grep "${to_remove}")" ] &&
  128.        [ ${#to_remove} -gt 1 ]; then
  129.       rm -rf ${to_remove}
  130.     fi
  131.   done
  132.  
  133.   xhost -
  134. }
  135.  
  136. trap 'umountall' 1
  137.  
  138. if [ ! -e "/${CONT_NAME_DEST}" ] && [ ! -h "/${CONT_NAME_DEST}" ] || [ -z "$(ls -A "/${CONT_NAME_DEST}")" ]; then
  139.   mkdir -p /${CONT_NAME_DEST}
  140. else
  141.   cd /
  142.   CONT_NAME_DEST=$(mktemp -d ${CONT_NAME_DEST}.XXXXXXX)
  143. fi
  144.  
  145. append_mnt_id_awk='
  146. function get_mnt_id(mnt_pt,loop){
  147.  if (length(mnt_pt) > 0 && length(loop)>0){
  148.    cmd="cat /proc/self/mountinfo | sort | grep '" loop "' | grep " mnt_pt " | head -n 1"
  149.  } else if (length(mnt_pt) > 0){
  150.    cmd="cat /proc/self/mountinfo | sort | grep '" mnt_pt "' | head -n 1"
  151.  } else if (length(loop)>0){
  152.    cmd="cat /proc/self/mountinfo | sort | grep '" loop "' | head -n 1"
  153.  }
  154.  while ((cmd | getline )){
  155.    mnt_id=$1
  156.    break  
  157.  }
  158.  close(cmd)
  159.  return mnt_id
  160. }
  161. {
  162.  mnt_pt=$1
  163.  loop=$2
  164.  mnt_id=get_mnt_id(mnt_pt,loop)
  165.  print mnt_id "|" mnt_pt "|" loop
  166. }'
  167.  
  168. loop=$(losetup -a | grep  "$SFS_PATH"  | sed "s/:.*$//" )
  169. if [ ! -z "$loop" ]; then
  170.   #Mount_PT=$(findmnt -o TARGET,SOURCE -D -n | grep "${loop}\$" | cut -f1 -d' ')
  171.   Mount_PT="$(findmnt -o TARGET,SOURCE -D -n | grep "${loop}\$" | awk "$append_mnt_id_awk" | sort -t '|' -k1 | cut -d'|' -f2 | head -n 1)"
  172. else
  173.   Mount_PT=${CHROOT_DIR:-/mnt/cont/sfs_img}
  174.   if [ -d "$Mount_PT" ]; then
  175.     echo "Directory is empty but exists: $Mount_PT" >&2
  176.   elif [ ! -z "$(ls -A "$Mount_PT")" ]; then
  177.     Mount_PT=$(mktemp -d -p "$Cont_Root" sfs_img.XXXXXXX)
  178.     Mount_PT_ID=".${Mount_PT##*.}"
  179.     mkdir -p "${Mount_PT}"
  180.   fi
  181.   mkdir -p "${Mount_PT}"
  182.   mount -o loop "$SFS_PATH" "$Mount_PT"
  183.   Mount_PT_to_Del=$Mount_PT
  184. fi
  185. #MNTPNT=${CHROOT_DIR:-/mnt/chroot-$SFS_NAME}
  186.  
  187. if [ ${#CONT_NAME_SOUCE} -le 1 ]; then
  188.   BIND_Source="${Mount_PT}"
  189. else
  190.   BIND_Source="${Mount_PT}/${CONT_NAME_SOUCE}"
  191. fi
  192. #mkdir -p "$BIND_Source"
  193.  
  194. BIND_Target_Root="$Cont_Root"/sfs__target${Mount_PT_ID}
  195. if [ ${#Mount_PT_ID} -gt 0 ]; then
  196.   BIND_Target_Root="$Cont_Root"/sfs__target${Mount_PT_ID}
  197. elif [ -z "$(ls -A "$BIND_Target_Root")" ]; then
  198.   BIND_Target_Root="$Cont_Root"/sfs__target
  199. else
  200.  BIND_Target_Root=$(mktemp -d -p "$Cont_Root" sfs__target.XXXXXXX)
  201. fi
  202. mkdir -p "$BIND_Target_Root"
  203.  
  204. if [ ${#CONT_NAME_DEST} -gt 0 ]; then
  205.   BIND_Target=$BIND_Target_Root/${CONT_NAME_DEST}
  206. else
  207.   BIND_Target=$BIND_Target_Root
  208. fi
  209. mkdir -p "$BIND_Target"
  210.  
  211. if [ ${#Mount_PT_ID} -gt 0 ]; then
  212.   Cont_TMPFS="$Cont_Root"/tmpfs${Mount_PT_ID}
  213. elif [ -z "$(ls -A "${Cont_Root}/tmpfs")" ]; then
  214.   Cont_TMPFS="$Cont_Root"/tmpfs
  215. else
  216.  Cont_TMPFS=$(mktemp -d -p "$Cont_Root" tmpfs.XXXXXXX)
  217. fi
  218. mkdir -p "$Cont_TMPFS"
  219. mount -t tmpfs none "$Cont_TMPFS"
  220. #mount -t tmpfs none $BIND_Target_Root;
  221. append_mnt_id_awk='
  222. function get_mnt_id(mnt_pt,loop){
  223.  if (length(mnt_pt) > 0 && length(loop)>0){
  224.    cmd="cat /proc/self/mountinfo | sort | grep '" loop "' | grep " mnt_pt " | head -n 1"
  225.  } else if (length(mnt_pt) > 0){
  226.    cmd="cat /proc/self/mountinfo | sort | grep '" mnt_pt "' | head -n 1"
  227.  } else if (length(loop)>0){
  228.    cmd="cat /proc/self/mountinfo | sort | grep '" loop "' | head -n 1"
  229.  }
  230.  while ((cmd | getline )){
  231.    mnt_id=$1
  232.    break  
  233.  }
  234.  close(cmd)
  235.  return mnt_id
  236. }
  237. {
  238.  mnt_pt=$1
  239.  loop=$2
  240.  mnt_id=get_mnt_id(mnt_pt,loop)
  241.  print mnt_id "|" mnt_pt "|" loop
  242. }'
  243. EXTRA_SFS_MPS=()
  244. for a_sfs_o in "${EXTRA_SFS[@]}"; do
  245.    a_sfs="$(realpath "$a_sfs_o")"
  246.    [ -z "$a_sfs" ] && a_sfs="$(realpath "../$a_sfs_o")"
  247.    [ -z "$a_sfs" ] && a_sfs=$(cd .. ; echo `pwd`/$a_sfs_o)
  248.    [ ! -f "$a_sfs" ] && continue
  249.    a_sfs_bname="$(basename "$a_sfs")"
  250.   if [ ${#Mount_PT_ID} -gt 0 ]; then
  251.     a_sfs_MP="$Cont_Root"/"$a_sfs_bname"${Mount_PT_ID}
  252.   elif [ -z "$(ls -A "${Cont_Root}/a_sfs_bname")" ]; then
  253.     a_sfs_MP="$Cont_Root"/"$a_sfs_bname"
  254.   else
  255.     a_sfs_MP=$(mktemp -d -p "$Cont_Root" a_sfs_bname.XXXXXXX)
  256.   fi
  257.  
  258.   mkdir -p "$a_sfs_MP"
  259.   loop=$(losetup -a | grep  "${a_sfs}"  | sed "s/:.*$//" )
  260.   if [ ! -z "$loop" ]; then #
  261.     sfs_MP="$(findmnt -o TARGET,SOURCE -D -n | grep $loop\$ | awk "$append_mnt_id_awk" | sort -t '|' -k1 | cut -d'|' -f2 | head -n 1)"
  262.     mount --bind "${sfs_MP}" "${a_sfs_MP}"
  263.   else
  264.     mount -r -t squashfs "${a_sfs}" "$a_sfs_MP"
  265.  
  266.   fi
  267.   [ -d "$a_sfs_MP" ] && [ -f "$a_sfs" ] && EXTRA_SFS_MPS+=("$a_sfs_MP")
  268. done
  269.  
  270. #NEW=''
  271. #MOUNTED_PUP_RO=$(busybox df | grep -o '/initrd/pup_ro.*')
  272. ## pup_ro1 and pup_ro2 are reserved
  273. #for i in $(seq 3 99) # find free pup_roX
  274. #do
  275. #   if ! [ "$(echo "$MOUNTED_PUP_RO" | grep "pup_ro${i}$")" ] ; then
  276. #       NEW=${i}
  277. #       break
  278. #   fi
  279. #done
  280. #Cont_Layer=/initrd/pup_ro$NEW
  281.  
  282.  
  283.  
  284.  
  285. mount --bind "${BIND_Source}" "${BIND_Target}"
  286. #ln -s "$BIND_Target_Root" "$Cont_Layer"
  287. #set +x
  288. #read -p "Press enter to continue"
  289. #set -x
  290.  
  291.  
  292. BRANCHES="udba=reval,diropq=w,br:${Cont_TMPFS}=rw:${BIND_Target}=rr"
  293. for a_branch in "${EXTRA_SFS_MPS}"; do
  294.   BRANCHES+=":$a_branch=ro"
  295. done
  296.  
  297. busybox mount -t aufs -o "$BRANCHES" aufs "/$CONT_NAME_DEST" || { umountall && exit 1; }
  298. #busybox mount -t aufs -o remount,append:$BIND_Target_Root=rr / || { umountall && exit 1; }
  299.  
  300. CMD_FP="/$CONT_NAME_DEST/${CHROOT_CMD}"
  301. if [ ! -e "$CMD_FP" ]; then
  302.   CMD_FP_trim="${CMD_FP%-nosb}"
  303.   if [ "$CMD_FP_trim" != "$CMD_FP" ] && [ -e "$CMD_FP_trim" ]; then
  304.     cat "$CMD_FP_trim" | sed -rn \
  305. '/run-as-spot "\$HERE\/chrome"/ {s#^(.*)$#"$HERE/chrome" --no-sandbox --user-data-dir=$HERE/PROFILE --disable-infobars "$@"#g;}
  306. /run-as-spot "\$HERE\/chrome"/! {p}' > "$CMD_FP"
  307.   chmod +x "$CMD_FP"    
  308.   fi
  309. fi
  310.  
  311. mkdir -p /${CONT_NAME_DEST}/dev
  312. mkdir -p /${CONT_NAME_DEST}/proc
  313. mkdir -p /${CONT_NAME_DEST}/sys
  314. mkdir -p /${CONT_NAME_DEST}/dev/pts
  315.  
  316. mount --bind /dev /${CONT_NAME_DEST}/dev
  317. mount --bind /proc /${CONT_NAME_DEST}/proc
  318. mount --bind /sys /${CONT_NAME_DEST}/sys
  319. mount -t devpts devpts /${CONT_NAME_DEST}/dev/pts
  320. cp /etc/resolv.conf /${CONT_NAME_DEST}/etc/resolv.conf
  321. cp /var/lib/dbus/machine-id /${CONT_NAME_DEST}/var/lib/dbus/machine-id
  322. [ -f .Xauthority ] && cp /root/.Xauthority /${CONT_NAME_DEST}/root/.Xauthority
  323. xhost +
  324. mkdir -p /${CONT_NAME_DEST}/tmp/.X11-unix
  325. mount --rbind /tmp/.X11-unix /${CONT_NAME_DEST}/tmp/.X11-unix
  326. cd /${CONT_NAME_DEST}
  327. #set +x
  328.  
  329. if [ -z ${USE_UNSHARE+x} ]; then
  330.         if [ -e /proc/1/ns/pid ] && [ -e /proc/1/ns/mnt ] && type unshare >/dev/null #&& [ -z $NO_NS ];
  331.         then
  332.             USE_UNSHARE=yes
  333.         else
  334.             USE_UNSHARE=no
  335.         fi
  336. fi
  337. if [ "$USE_UNSHARE" = yes ]; then
  338.   CHROOT_UTIL=( unshare -f -C -u -p --mount-proc="/${CONT_NAME_DEST}/proc" chroot )
  339. else
  340.   CHROOT_UTIL=(chroot)
  341. fi
  342.  
  343. if [ ! -z "${EXECUTE}" ]; then
  344.   cd /"$CONT_NAME_DEST"
  345.   set +x
  346.   echo "next cmd: \"${EXECUTE[@]}\" \"$@\""
  347.   read -p "Ready to EXECUTE Press enter to continue"
  348.   set -x
  349.   #set +x
  350.   "${EXECUTE[@]}" "$@"
  351.  
  352. elif [ ! -z "${CHROOT_CMD}" ]; then
  353.   set +x
  354.   echo "next cmd: chroot /${CONT_NAME_DEST} \"${CHROOT_CMD[@]}\" \"$@\""
  355.   read -p "Ready to chroot Press enter to continue"
  356.   set -x  
  357.   if [ -z "$XephyrDISPLAY" ]; then
  358.      #Can't XFORWARD over ssh from puppy4 to newer puppies, but can use Xephyr as a workaround.
  359.      LD_LIBRARY_PATH="/lib:/usr/lib:/usr/X11R7/lib:/root/my-applications/lib:/opt/qt4/lib" ${CHROOT_UTIL[@]} /${CONT_NAME_DEST} "${CHROOT_CMD[@]}" "$@"  
  360.   else
  361.      cd "$CWD"
  362.      source init_scripts.sh
  363.      if [ "$(ls -1 /tmp/.X11-unix | tr 'X' ':')" != "$XephyrDISPLAY" ]; then
  364.        XP="-fullscreen -title container -name Xephyr2 -dpi 144 -nolisten tcp"
  365.        Xephyr $XephyrDISPLAY ${XP} &
  366.      fi  
  367.      
  368.      DISPLAY=$XephyrDISPLAY ${CHROOT_UTIL[@]} /${CONT_NAME_DEST} "${CHROOT_CMD[@]}" "$@"  
  369.   fi
  370. else
  371.   chroot /${CONT_NAME_DEST} /bin/sh "$@"
  372. fi
  373. set -x
  374. if [ ! NO_EXIT = true ]; then
  375.   umountall
  376. fi
  377.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×