s243a

Remount_save.sh (draft 2)

Nov 28th, 2020 (edited)
531
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2. #-t --tmpfs  path to temporary save file
  3. #-r --root   Parent directory to the new rootfs. Only applied to options which are to the right of this option.  
  4. #-f --fake-root This is the location where the fake (or new) rootfs will be mounted.
  5. #-n --new-root See: -f --fake-root
  6. #   This is a subdirectory of the -r|--root option when the -r --root option is applied to the left of this argument
  7. unset SANDBOX_ID
  8. uset rwbranch
  9. unset FAKEROOT
  10. unset SANDBOX_TMPF
  11. unset FAKEROOT_dir
  12. unset SANDBOX_IMG
  13. #SANDBOX_ROOT=/mnt/sb
  14. unset SANDBOX_ROOT
  15. # [ -z "$TMPFILE" ] && TMPFILE=$(mktemp -p /tmp)
  16.  TMPFILE=$(mktemp -p /tmp)
  17. trap 'umountall' 1
  18. function log(){
  19.   local logfile="${2}"
  20.   local trace="$3"
  21.   #[ -z "$logfile" ] && LOGFILE
  22.   #[ -z "$trace" ] && trace=TRACE
  23.   if [ ! -z "$LOGFILE" ]; then
  24.     case "$1" in
  25.     init)
  26.       [ "$TRACE" = true ] && set -x
  27.       [ ! -z "$LOGFILE" ] && rm "$LOGFILE"
  28.       exec 6>&1           # Link file descriptor #6 with stdout.
  29.       #exec &1> >(tee -a "$LOGFILE")
  30.       #exec &2> >(tee -a "$LOGFILE")
  31.       exec &> >(tee -a "$LOGFILE")
  32.       ;;
  33.     start)
  34.       [ "$TRACE" = true ] && set -x
  35.       #exec &1> >(tee -a "$LOGFILE")
  36.       #exec &2> >(tee -a "$LOGFILE")
  37.       exec &> >(tee -a "$LOGFILE")
  38.       ;;
  39.     stop)
  40.       #https://stackoverflow.com/questions/21106465/restoring-stdout-and-stderr-to-default-value
  41.       [ "$TRACE" = true ] && set +x
  42.       exec 1>&6  
  43.       exec 6>&-      # Restore stdout and close file descriptor #6.
  44.       exec &2> /dev/stderr    
  45.       ;;
  46.     esac
  47.   fi    
  48. }
  49.  
  50. umountall() {
  51.   {
  52.   umount -l $FAKEROOT/$SANDBOX_TMPFS
  53.   if [ PUPMODE = 2 ]; then #Full Install
  54.       umount -l $FAKEROOT/tmp
  55.     else
  56.       umount -l $FAKEROOT/initrd/mnt/tmpfs
  57.     fi
  58.   for layer_name in "pup_ro2" "pup_ro3" "pup_ro4" "pup_ro5" "pup_z"; do
  59.     layer="$(eval 'echo $'$layer_name)"
  60.     if [ ! -z "$layer" ] ; then
  61.       umount -l "$FAKEROOT/initrd/$layer_name"
  62.     fi
  63.   done    
  64.   umount -l $FAKEROOT/proc
  65.   umount -l $FAKEROOT/sys
  66.   umount -l $FAKEROOT/dev
  67.  
  68.   umount -l $FAKEROOT
  69.   [ -z "$RW_LAYER" ] && umount -l $SANDBOX_TMPFS
  70.   rmdir $FAKEROOT
  71.   #if  [ PUPMODE = 2 ] || PUPMODE = 5 ]; then
  72.     [ -z "$RW_LAYER" ] && rmdir $SANDBOX_TMPFS
  73.   #fi
  74.   } 2> /dev/null
  75. }
  76.  
  77. function choose_save(){
  78.     # location not specified - then ask
  79.     log stop
  80.     dialog --backtitle "rw image sandbox" --title "choose rw image" \
  81.     --extra-button --extra-label "Create" --ok-label "Locate" \
  82.     --yesno "You didn't specify the location of the rw image file. Do you want to locate existing file, or do you want to create a new one?" 0 0
  83.     chosen=$?
  84.     log start
  85.     case $chosen in
  86.         0) # ok - locate
  87.             log stop
  88.             dialog --backtitle "rw image sandbox" --title "Specify location of existing rw image" --fselect `pwd` 8 60 2> $TMPFILE
  89.             savebranch=`cat $TMPFILE`
  90.             log start
  91.             rm $TMPFILE
  92.             if [ -n "$savebranch" ]; then
  93.                 if [ -d "$savebranch" ]; then
  94.                   SANDBOX_IMG=$savebranch
  95.                 elif [ ! -f "$savebranch" ]; then
  96.                     echo "$savebranch doesn't exist - exiting."
  97.                     exit                   
  98.                 fi
  99.             else
  100.                 echo "You didn't specify any file or you pressed cancel. Exiting."
  101.                 exit
  102.             fi
  103.             ;;
  104.         3) # create
  105.             echo "create"
  106.             log stop
  107.             dialog --backtitle "save image sandbox" --title "Specify name and path of new the file" --fselect `pwd` 8 60 2> $TMPFILE
  108.             savebranch=`cat $TMPFILE`
  109.             log start
  110.             rm $TMPFILE
  111.             if [ -n "$savebranch" ]; then
  112.                 if [ -f "$savebranch" ]; then
  113.                     echo "$savebranch already exist - exiting."
  114.                     exit
  115.                 else
  116.                     # get the size
  117.                     log stop
  118.                     dialog --title "Create new save image" --inputbox "Specify size (in megabytes)" 0 40 100 2> $TMPFILE
  119.                     size=`cat $TMPFILE`
  120.                     log start
  121.                     rm $TMPFILE
  122.                    
  123.                     if [ -n "$size" ]; then
  124.                         if dd if=/dev/zero of="$savebranch" bs=1 count=0 seek="$size"M; then
  125.                             if ! mke2fs -F "$savebranch"; then
  126.                                 echo "I fail to make an ext2 filesystem at $savebranch, exiting."
  127.                                 exit
  128.                             fi
  129.                         else
  130.                             echo "I fail to create a ${size}M file at $savebranch,, exiting."
  131.                             exit
  132.                         fi
  133.                     else
  134.                         echo "You didn't specify the size or your press cancel. Exiting."
  135.                         exit
  136.                     fi                 
  137.                 fi
  138.             else
  139.                 echo "You didn't specify any file or you pressed cancel. Exiting."
  140.                 exit
  141.             fi
  142.             ;;
  143.         1 | 255) # 1 is cancel, 255 is Escape
  144.             ;&
  145.         *) # invalid input - treat as cancel
  146.             echo "Cancelled - exiting."
  147.             exit
  148.             ;;
  149.     esac
  150. }
  151. function find_save(){
  152.   if [ ! -z "SANDBOX_IMG" ]; then
  153.     FULL_SAVE_PATH=$SANDBOX_IMG
  154.   else
  155.    
  156.     for prefix in '${DISTRO_FILE_PREFIX}save' '.*save'; do
  157.       for dir in "$PDRV/${PSUBDIR}" "PDRV";  do
  158.        
  159.         ONE_SAVE="$(ls $dir -1 | grep -m "${prefix}save")"
  160.         if [ -z "$ONE_SAVE" ]; then
  161.            continue
  162.         else
  163.            SAVE_FILE="$ONE_SAVE"
  164.            FULL_SAVE_PATH="$dir"/ONE_SAVE
  165.            break
  166.         fi
  167.       done
  168.      done
  169.      echo "PSAVE"mount_items
  170.    fi
  171. }
  172. function find_bk_folders(){
  173.   for a_PDRV in "$PDRV" sr0 sr1; do #Consider adding /mnt/home here
  174.     for a_psubdir in "${PSUBDIR}" "";  do
  175.       MT_PT_of_Folder="$(mount_fn2 "$PDRV" "${PSUBDIR}")"
  176.       #https://github.com/puppylinux-woof-CE/woof-CE/blob/c483d010a8402c5a1711517c2dce782b3551a0b8/initrd-progs/0initrd/init#L981
  177.       BKFOLDERS="$(find $MT_PT_of_Folder -maxdepth 1 -xdev -type d -name '20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]' | sed -e s%^${SAVE_MP}/%% | sort -r)"
  178.       [ ! -z "#BKFOLDERS" ] && break  
  179.     done
  180.   done
  181. }
  182.  
  183. function mk_initrd_dir(){
  184.   mkdir -p "$FAKEROOT"/initrd
  185.   if [ -z "$PUPMODE" ] ; then
  186.     if [ -z "$PMEDIA" ]; then
  187.       #if [ "$PUPMODE" = 5 ] ; then
  188.       #  #aufs layers:              RW (top)      RO1             RO2              PUPMODE
  189.       #  #First boot (or pfix=ram): tmpfs                         pup_xxx.sfs      5
  190.       PUPMODE=5 #MAYBE PUPMODE=2 would be better
  191.     elif [ PMEDIA = 'atahd' ] || [ "$PMEDIA" = 'usbhd' ]; then
  192.       find_save
  193.       if [ -f "$FULL_SAVE_PATH" ] || [ -d "$FULL_SAVE_PATH" ]; then
  194.         #aufs layers:               RW (top)      RO1             RO2              PUPMODE
  195.         #Normal running puppy:      pup_save.3fs                  pup_xxx.sfs      12      
  196.         PUPMODE=12
  197.       else
  198.         echo "Invalid SAVE_PATH=$SAVE_PATH does not exist"
  199.         PUMPMODE=2
  200.         #TODO, prompt to either search for save file/folder or alternatively create it.
  201.       fi
  202.     elif [ PMEDIA = 'usbflash' ] || [ pmedia = 'ideflash' ]; then
  203.       find_save
  204.       #aufs layers:                 RW (top)      RO1             RO2              PUPMODE
  205.       #ditto, but flash drive:      tmpfs         pup_save.3fs    pup_xxx.sfs      13
  206.       if [ -f "$SAVE_PATH" ] || [ -d "$SAVE_PATH" ]; then
  207.         #aufs layers:               RW (top)      RO1             RO2              PUPMODE
  208.         #ditto, but flash drive:    tmpfs         pup_save.3fs    pup_xxx.sfs      13
  209.         PUPMODE=13
  210.       else
  211.         echo "Invalid SAVE_PATH=$SAVE_PATH does not exist"
  212.         PUPMODE=5
  213.       fi
  214.     elif [ "$PMEDIA" =  usbcd ] || [ "$PMEDIA" =  idecd ] || [ "$PMEDIA" =  satacd ] ; then
  215.       find_bk_folders
  216.       if [ ! -z "$BKFOLDERS" ]; then
  217.         PUPMODE=77  #MULTI-Session CD
  218.       else #First Boot
  219.         find_save
  220.         if [ -f "$FULL_SAVE_PATH" ] || [ -d "$FULL_SAVE_PATH" ]; then
  221.           PUPMODE=13      
  222.         else
  223.           PUPMODE=5
  224.         fi
  225.       fi
  226.       #aufs layers:            RW (top)      RO1             RO2              PUPMODE
  227.       #Multisession cd/dvd:       tmpfs         folders         pup_xxx.sfs      77
  228.     else #[PUPMODE=2 -> full install
  229.       PUPMODE=2
  230.     fi
  231.     if [ "$PUPMODE" = 2 ]; then #Full install
  232.       echo "Full install has no initrd"
  233.     else
  234.       mkdir -p "$FAKEROOT/initrd"
  235.       cd $FAKEROOT/initrd
  236.       if [ "$PUPMODE" = 12 ]; then # Usually [ PMEDIA = 'atahd' ] || [ "$PMEDIA" = usbhd ]
  237.         ln -s mnt/dev_save/"${SAVE_PATH}" pup_rw
  238.       elif [ "$PUPMODE" = 13 ] || [ "$PUPMODE" = 5 ] || [ "$PUPMODE" = 77 ]; then
  239.         ln -s mnt/tmpfs/pup_rw pup_rw
  240.         if [ "$PUPMODE" = 13 ]; then  # Usually [ PMEDIA = 'usbflash' ] || [ pmedia = 'ideflash' ]
  241.           ln -s "mnt/tmpfs/dev_save/${SAVE_PATH}" pup_ro1
  242.         elif [ "$PUPMODE" = 77 ]; then
  243.           ln -s mnt/tmpfs/pup_ro1/"${SAVE_PATH}" pup_ro1  #Usually [ "$PMEDIA" =  usbcd ] || [ "$PMEDIA" =  idecd ] || [ "$PMEDIA" =  satacd ]
  244.         fi
  245.       fi
  246.     fi
  247.   fi
  248. }
  249.  
  250. function set_fakeroot(){
  251.     if [ ! -z ${FAKEROOT+x} ] || [ ! -z ${SANDBOX_ROOT+x} ]; then
  252.       [ ! -z ${FAKEROOT+x} ]  && [ -z "${fakeroot}" ] && FAKEROOT=fakeroot
  253.       [ ${#SANDBOX_ROOT} -gt 1 ] &&   FAKEROOT=$SANDBOX_ROOT/$FAKEROOT
  254.        FAKEROOT_dir="${FAKEROOT%/*}"
  255.       [ -z "${SANDBOX_ROOT}" ] && SANDBOX_ROOT=${FAKEROOT_dir}
  256.       if grep -q $FAKEROOT /proc/mounts; then
  257.         [ -z "${fakeroot}" ]
  258.         FAKEROOT=$(mktemp -d -p ${FAKEROOT%/*}/ ${FAKEROOT##*/}.XXXXXXX)
  259.         SANDBOX_ID=".${FAKEROOT##*.}"
  260.         if [ -z "$SANDBOX_IMG" ]; then
  261.           if grep -q $SANDBOX_IMG /proc/mounts; then
  262.             SANDBOX_IMG=$FAKEROOT_dir/${SANDBOX_IMG##*/}${SANDBOX_ID}
  263.           fi
  264.         fi
  265.         rmdir $FAKEROOT
  266.       fi
  267.     else
  268.       echo "Warning sandbox root not defined"
  269.       #[ -z "$FAKEROOT" ] && FAKEROOT=/
  270.     fi
  271. }
  272. function set_sandbox_img(){
  273.      [ ! -z ${FAKEROOT+x} ]   &&  FAKEROOT_dir=""
  274.      [ -z ${FAKEROOT_dir+x} ] &&  set_fakeroot
  275.      if [ ! -z ${SANDBOX_IMG+x} ]  && [ -z "${SANDBOX_IMG}" ]; then
  276.       SANDBOX_IMG=sandbox_img
  277.       SANDBOX_IMG=${SANDBOX_IMG}${SANDBOX_ID}
  278.       [ -z "$FAKEROOT_dir" ] && SANDBOX_IMG=$FAKEROOT_dir/$SANDBOX_IMG
  279.      elif [ -z "${FAKEROOT_dir}" ] ; then
  280.       SANDBOX_IMG=$FAKEROOT_dir/$SANDBOX_IMG
  281.      fi
  282. }
  283. function set_sandbox_tmpfs(){
  284.      [ ! -z ${FAKEROOT+x} ]   &&  FAKEROOT_dir=""
  285.      [ -z ${FAKEROOT_dir+x} ] &&  set_fakeroot
  286.      if [ ! -z ${SANDBOX_TMPFS+x} ]  && [ -z "${SANDBOX_TMPFS}" ]; then
  287.       SANDBOX_TMPF=sandbox_tmpfs
  288.       SANDBOX_TMPF=${SANDBOX_TMPF}${SANDBOX_ID}
  289.       [ -z "$FAKEROOT_dir" ] && SANDBOX_TMPF=$FAKEROOT_dir/$SANDBOX_TMPF
  290.     elif [ -z "${FAKEROOT_dir}" ] ; then
  291.       SANDBOX_TMPF=$FAKEROOT_dir/$SANDBOX_TMPF
  292.     fi
  293. }
  294. declare -a options="$(getopt -o t::,r::,s:,d:,l: --long input-file:tmpfs::,root::,save:,pupmode::,pmedia:,pdrv:,psubdir:,distro-specs:,logfile:,trace: -- "$@")"
  295. eval set --"$options"
  296. while [ $# -gt 0 ]; do
  297.   case "$1" in
  298.   -t|--tmpfs)
  299.     if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  300.       SANDBOX_TMPFS="$2"
  301.       set_sandbox_tmpfs
  302.       shift 2
  303.     else
  304.       SANDBOX_TMPF="" #Later we use [ -z ${SANDBOX_TMPF+x} ] to check that this is set
  305.       set_sandbox_tmpfs
  306.       shift 1
  307.     fi; ;;
  308.    -r|--root)
  309.     if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  310.       SANDBOX_ROOT="$2"
  311.       shift 2
  312.     else
  313.       SANDBOX_ROOT="" #Later we use [ -z ${SANDBOX_ROOT+x} ] to check that this is set
  314.       shift 1
  315.     fi; ;;  
  316.    -f|-n|--fake-root|--new-root)
  317.       if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  318.         FAKEROOT="$2"
  319.         set_fakeroot
  320.         shift 2
  321.       else
  322.         FAKEROOT="" #Later we use [ -z ${FAKEROOT+x} ] to check that this is set
  323.         set_fakeroot
  324.         shift 1
  325.       fi; ;;  
  326.    -s|--save) #$SANDBOX_IMG
  327.     #if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  328.       if [ -d "$2" ]; then
  329.         SANDBOX_IMG=$2
  330.         savebranch=$2
  331.       else [ -f "$2" ]
  332.         #mnt_sb_immage
  333.         #mount -o loop "$rwbranch" $SANDBOX_IMG;
  334.         savebranch=$1
  335.         loop=$(losetup-FULL -a | grep  "$savebranch"  | sed "s/:.*$//")
  336.         if [ ! -z "$loop" ]; then
  337.           SANDBOX_IMG=$(/proc/mounts | grep $loop | cut -d " " -f2)
  338.         else
  339.           SANDBOX_IMG=""
  340.           set_sandbox_img
  341.         fi
  342.         shift 2;
  343.       fi; ;;
  344.     --pupmode)
  345.       if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  346.         PUPMODE=$2
  347.         shift 2
  348.       else
  349.         PUPMODE=2
  350.         shift
  351.       fi
  352.     ;;
  353.   --pmedia) PMEDIA=$2; shift 2; ;;
  354.   -d| --pdrv) PDRV=$2; shift 2; ;;
  355.   --psubdir) PSUBDIR=$2;
  356.     shift 2; ;;
  357.   --distro-specs)
  358.      DISTRO_SPECS=$2;
  359.      . "$DISTRO_SPECS"
  360.      shift 2
  361.      ;;
  362.   -l|--logfile)
  363.     LOGFILE=$2
  364.     [ -z "$TRACE" ] && TRACE=true
  365.     shift 2
  366.     log init
  367.     ;;  
  368.   -t|--trace)
  369.     TRACE=$2
  370.     if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  371.       TRACE="$2"
  372.       shift 2
  373.     else
  374.       TRACE=true
  375.       shift 1
  376.     fi
  377.     log init
  378.     ;;
  379.    --)
  380.     shift 1
  381.     options2+=( "$@" )
  382.     break; ;;
  383.   *)
  384.      options2+=( "$1" )
  385.      shift 1; ;;
  386.    
  387.   esac
  388. done
  389.  
  390.  
  391.  
  392.  
  393. #SANDBOX_TMPFS=$SANDBOX_ROOT/sandbox # mounted rw location of
  394. #SANDBOX_TMPFS=$SANDBOX_ROOT/initrd/mnt/tmpfs/pup_rw
  395. #tmpfs used for sandbox
  396. #SANDBOX_ID=
  397. TMPFILE=$(mktemp -p /tmp)
  398. # use namespaces if available
  399. #[ -e /proc/1/ns/pid ] && [ -e /proc/1/ns/mnt ] && type unshare >/dev/null && USE_NS=1
  400.  
  401. # umount all if we are accidentally killed
  402. #trap 'umountall' 1
  403. #s243a don't unmount on error
  404.  
  405. # 0.1 must be root
  406. if [ $(id -u) -ne 0 ]; then
  407.     echo "You must be root to use sandbox."
  408.     exit
  409. fi
  410.  
  411. ## 0.2 cannot launch sandbox within sandbox
  412. #if [ "$AUFS_ROOT_ID" != "" ] ; then
  413. #   grep -q $SANDBOX_ROOT /sys/fs/aufs/$AUFS_ROOT_ID/br0 &&
  414. #       echo "Cannot launch sandbox within sandbox." && exit
  415. #fi
  416. # s243a we are remounting everything rather then creating a sandbox.
  417.  
  418. # 0.3 help
  419. case "$1" in
  420.     --help|-h)
  421.     echo "Usage: ${0##*/}"
  422.     echo "Starts an in-memory (throwaway) sandbox. Type 'exit' to leave."
  423.     exit
  424. esac
  425.  
  426. ## 0.4 if not running from terminal but in Xorg, then launch via terminal
  427. #! [ -t 0 ] && [ -n "$DISPLAY" ] && exec $XTERM -e "$0" "$@"
  428. #! [ -t 0 ] && exit
  429.  
  430. ## 0.5 is this the first sandbox? If not, then create another name for mountpoints
  431. #if grep -q $FAKEROOT /proc/mounts; then
  432. #   FAKEROOT=$(mktemp -d -p $SANDBOX_ROOT ${FAKEROOT##*/}.XXXXXXX)
  433. #   SANDBOX_ID=".${FAKEROOT##*.}"
  434. #   SANDBOX_TMPFS=$SANDBOX_ROOT/${SANDBOX_TMPFS##*/}${SANDBOX_ID}
  435. #   rmdir $FAKEROOT
  436. #fi
  437.  
  438. # 1. get aufs system-id for the root filesystem
  439. if [ -z "$AUFS_ROOT_ID" ] ; then
  440.     AUFS_ROOT_ID=$(
  441.         awk '{ if ($2 == "/" && $3 == "aufs") { match($4,/si=[0-9a-f]*/); print "si_" substr($4,RSTART+3,RLENGTH-3) } }' /proc/mounts
  442.     )
  443. fi
  444.  
  445. # 2. get branches, then map branches to mount types or loop devices
  446. items=$(
  447. { echo ==mount==; cat /proc/mounts;
  448.   echo ==losetup==; losetup-FULL -a;
  449.   echo ==branches==; ls -v /sys/fs/aufs/$AUFS_ROOT_ID/br[0-9]* | xargs sed 's/=.*//'; } | \
  450.   awk '
  451.  /==mount==/ { mode=1 }
  452.  /==losetup==/ { mode=2 }
  453.  /==branches==/ { mode=3 }
  454.  {
  455.     if (mode == 1) {
  456.         # get list of mount points, types, and devices - index is $3 (mount points)
  457.         mountdev[$2]=$1
  458.         mounttypes[$2]=$3
  459.     } else if (mode == 2) {
  460.         # get list of loop devices and files - index is $1 (loop devs)
  461.         sub(/:/,"",$1)
  462.         sub(/.*\//,"",$3); sub(/)/,"",$3)
  463.         loopdev[$1]=$3
  464.     } else if (mode == 3) {
  465.         # map mount types to loop files if mount devices is a loop
  466.         for (m in mountdev) {
  467.             if ( loopdev[mountdev[m]] != "" ) mounttypes[m]=loopdev[mountdev[m]]
  468.         }
  469.         # for (m in mountdev) print m " on " mountdev[m] " type " mounttypes[m]
  470.         mode=4
  471.     } else if (mode==4) {
  472.         # print the branches and its mappings
  473.         if ($0 in mounttypes){
  474.           print $0, mounttypes[$0], "on"
  475.         }
  476.         else {
  477.             MNT_PATH=$0
  478.             sub(/^.*[\/]/,"")
  479.             print MNT_PATH, $0, "on"
  480.         }
  481.     }
  482.  }  
  483. '
  484. )
  485. # '
  486.  
  487. # 3. Ask user to choose the SFS
  488. log stop
  489. dialog --separate-output --backtitle "tmpfs sandbox" --title "sandbox config" \
  490.     --checklist "Choose which SFS you want to use" 0 0 0 $items 2> $TMPFILE
  491. chosen="$(cat $TMPFILE)"
  492. log start
  493. clear
  494. if [ -z "$chosen" ]; then
  495.     echo "Cancelled or no SFS is chosen - exiting."
  496.     exit 1
  497. fi
  498.  
  499. # 4. convert chosen SFS to robranches
  500. robranches=""
  501. for a in $(cat $TMPFILE) ; do
  502.     robranches=$robranches:$a=ro
  503. done
  504. rm $TMPFILE
  505.  
  506. # 5. get location of rw image
  507. if [ -z "$savebranch"  ]; then
  508.   choose_save
  509.    loop=$(losetup-FULL -a) | grep  "$savebranch"  | sed "s/:.*$//")
  510.    if [ ! -z "$loop" ]; then
  511.           SANDBOX_IMG=$(/proc/mounts | grep $loop | cut -d " " -f2)
  512.           mount -o loop "$savebranch" $SANDBOX_IMG
  513.     fi
  514. else
  515.         loop=$(losetup-FULL -a) | grep  "$savebranch"  | sed "s/:.*$//")
  516.         if [ ! -z "$loop" ]; then
  517.           SANDBOX_IMG=$(/proc/mounts | grep $loop | cut -d " " -f2)
  518.         else
  519.           mount -o loop "$savebranch" $SANDBOX_IMG
  520.         fi
  521. fi
  522. if  [ -z "$rwdir" ] ; then
  523.   if [ -z "$PUPMODE" ];
  524.     rwdir=$SANDBOX_IMG
  525.   else
  526.     if [ $PUPMODE  -ne 5 ] && [ $PUPMODE  -ne 13 ] && [ $PUPMODE  -ne 77 ]; then
  527.       if  [ -z ${SANDBOX_TMPF+x} ]; then
  528.         SANDBOX_TMPF="" #Later we use [ -z ${SANDBOX_TMPF+x} ] to check that this is set
  529.       fi
  530.     fi
  531.   fi
  532. fi
  533. # 4. make the mountpoints if not exist  yet
  534. if [ ! -z "$SANDBOX_IMG" ]; then
  535.  mkdir -p $SANDBOX_IMG
  536.  mount -o loop "$savebranch" $SANDBOX_IMG || {
  537.      echo "Failed to mount '$savebranch' at '$SANDBOX_IMG'"
  538.      exit
  539.  }
  540. fi
  541. if [ ! -z "$SANDBOX_ROOT" ]; then
  542.   DEV_SAVE=$SANDBOX_ROOT/dev_save
  543.   mkdir -p "$DEV_SAVE"
  544.   if [ -z "$PDRV" ]; then
  545.     if [[ "$SANDBOX_IMG" = *"/mnt/"* ]]; then
  546.       PSUBDIR=${SANDBOX_IMG#/mnt/*/}
  547.       PDRV=${SANDBOX_IMG%"/$PSUBDIR"}
  548.       PDRV_real=$(cat /proc/mounts | grep $(realpath $PDRV) | cut -d " " -f1)
  549.           PSUBDIR_i=${PSUBDIR}
  550.           PDRV_i=${PDRV}
  551.           PDRV_real_i=${PDRV_real}      
  552.       while (true); do
  553.  
  554.           if [[ PDRV_real_i = /dev/loop* ]]; then
  555.             PDRV_real_i=$(losetup-FULL -a | grep "$(basename PDRV_real)")
  556.             PDRV_real_i=${PDRV_real#*(}
  557.             PDRV_real_i=${PDRV_real%)}
  558.             PSUBDIR_i=${PDRV_real_i#/mnt/*/}
  559.             PDRV_real=$PSUBDIR_i/$PDRV_real
  560.             PDRV_real_i=${SANDBOX_IMG%"/$PSUBDIR_i"}
  561.             PDRV_real_i=$(cat /proc/mounts | grep $(realpath $PDRV_real_i) | cut -d " " -f1)
  562.           elif [[ PDRV_real_i = /dev/* ]]; then
  563.             PDRV_real=$(blkid | grep $PDRV_real)
  564.             break
  565.           else
  566.             echo "could not identify PDRV_real"
  567.             break
  568.           fi
  569.          
  570.       done
  571.     fi
  572.   fi
  573. fi
  574.  
  575. if [ -z  "$rwbranch" ] || [ -z ${SANDBOX_TMPF+x} ]; then
  576.   if [ -z "$SANDBOX_TMPFS" ] ; then
  577.     SANDBOX_TMPFS=$SANDBOX_ROOT/sandbox
  578.     if grep -q $SANDBOX_TMPFS /proc/mounts; then
  579.       #FAKEROOT=$(mktemp -d -p $SANDBOX_ROOT ${FAKEROOT##*/}.XXXXXXX)
  580.       #SANDBOX_ID=".${FAKEROOT##*.}"
  581.       SANDBOX_TMPFS=$SANDBOX_ROOT/${SANDBOX_ID/}${SANDBOX_ID}
  582.       mkdir -p -f SANDBOX_TMPFS
  583.       mount -t tmpfs none $SANDBOX_TMPFS; #We could exit if this fails but we don't need to becuase we can still write to SANDBOX_TMPFS even if it isn't tmpfs.
  584.       rmdir $FAKEROOT
  585.     fi
  586.   fi
  587. fi
  588. if [ -z "$SANDBOX_TMPFS" ]; then
  589.   SANDBOX_TMPFS=$SANDBOX_IMG
  590. else
  591.   if [ $robranches ~= ** ]; then
  592.     robranches=$SANDBOX_IMG=rr:$robranches
  593.   fi
  594. fi
  595. mkdir -p $FAKEROOT
  596.  
  597. mk_initrd_dir
  598.  
  599. # 5. do the magic - mount the rw image first, and then the rest with aufs
  600. #if mount -o loop "$rwbranch" $SANDBOX_IMG; then
  601. if [ ${#FAKEROOT} -gt 1 ] && ! grep -q $FAKEROOT /proc/mounts; then
  602.  
  603.         # 5. record our new aufs-root-id so tools don't hack real filesystem
  604.         SANDBOX_AUFS_ID=$(grep $FAKEROOT /proc/mounts | sed 's/.*si=/si_/; s/ .*//') #'
  605.         sed -i -e '/AUFS_ROOT_ID/ d' $FAKEROOT/etc/BOOTSTATE 2> /dev/null
  606.         echo AUFS_ROOT_ID=$SANDBOX_AUFS_ID >> $FAKEROOT/etc/BOOTSTATE  
  607.   echo "mount -t aufs -o \"br:$SANDBOX_TMPFS=rw$robranches\" aufs ${FAKEROOT}"
  608.   echo "About to mount FAKEROOT at $FAKEROOT"
  609.   read -p "Press enter to continue"
  610.  
  611.     mount -t aufs -o "br:$SANDBOX_TMPFS=rw$robranches" aufs ${FAKEROOT}
  612.    
  613.    
  614.     # 7. sandbox is ready, now just need to mount other supports - pts, proc, sysfs, usb and tmp   
  615.    
  616.     mkdir -p $FAKEROOT/dev $FAKEROOT/sys $FAKEROOT/proc $FAKEROOT/tmp
  617.     mkdir -p  "$DEV_SAVE/${PSUBDIR}"
  618.     mount -o bind  "$PDRV/${PSUBDIR}" "$DEV_SAVE/${PSUBDIR}"
  619.    
  620.     mount -o bind  "$DEV_SAVE/${PSUBDIR}" "$FAKEROOT/initrd/mnt/dev_save"
  621.  
  622.     #Maybe optionally do this based on some input paramater:
  623.     #Also pull these layers from an array
  624.     for layer_name in "pup_ro2" "pup_ro3" "pup_ro4" "pup_ro5" "pup_z"; do
  625.         layer="$(eval 'echo $'$layer_name)"
  626.       if [ ! -z "$layer" ] ; then
  627.         mount -o bind  "$layer" "$FAKEROOT/initrd/$layer_name"
  628.       fi
  629.     done
  630.  
  631.    
  632.     mkdir -p "$FAKEROOT/initrd/mnt/dev_save"
  633.     mount -o bind  "$DEV_SAVE/${PSUBDIR}" "$FAKEROOT/initrd/mnt/dev_save"
  634.    
  635.     mount -o rbind /dev $FAKEROOT/dev
  636.     mount -t sysfs none $FAKEROOT/sys
  637.     mount -t proc none $FAKEROOT/proc
  638.     if [ PUPMODE = 2 ]; then #Full Install #Maybe don't base this on PUPMODE
  639.       tmp_des=$FAKEROOT/tmp
  640.       tmp_source=/tmp
  641.     else
  642.         mkdir -p $FAKEROOT/initrd/mnt/tmpfs
  643.       tmp_des=$FAKEROOT/initrd/mnt/tmpfs
  644.       tmp_source=/initrd/mnt/tmpfs
  645.       cd $FAKEROOT
  646.       rm tmp
  647.       ln -s initrd/mnt/tmpfs tmp
  648.     fi
  649.     mount -o bind $tmp_source $tmp_des
  650.  
  651.     cd $FAKEROOT
  652.     ln -s initrd/mnt/tmpfs tmp 
  653.     mkdir -p $FAKEROOT/$SANDBOX_TMPFS
  654.     mount -o bind $SANDBOX_TMPFS $FAKEROOT/$SANDBOX_TMPFS # so we can access it within sandbox
  655.    
  656.  
  657.     #mkdir -p $FAKEROOT/$SANDBOX_TMPFS
  658.     mkdir -p $FAKEROOT/$SANDBOX_IMG
  659.     mount -o bind $SANDBOX_IMG $FAKEROOT/$SANDBOX_IMG   # so we can access it within sandbox   
  660.      cp /usr/share/sandbox/* $FAKEROOT/usr/bin 2> /dev/null
  661.    
  662.  
  663.  
  664.         # 8. optional copy, to enable running sandbox-ed xwin
  665.         cp /usr/share/sandbox/* $FAKEROOT/usr/bin 2> /dev/null
  666.        
  667.         # 9. make sure we identify ourself as in sandbox - and we're good to go!
  668.         echo -e '\nexport PS1="sandbox'${SANDBOX_ID}'# "' >> $FAKEROOT/etc/shinit #fatdog 600
  669.         sed -i -e '/^PS1/ s/^.*$/PS1="sandbox'${SANDBOX_ID}'# "/' $FAKEROOT/etc/profile # earlier fatdog
  670.        
  671.     if [ -d "$FULL_SAVE_PATH" ]; then #TODO verify that this works with a save file
  672.       if [ $PUPMODE -eq 13 ] && [ $PUPMODE -eq 77 ]; then
  673.         #TODO: when PUPMODE=77 (multisession cd) we need to copy folders. See: https://github.com/puppylinux-woof-CE/woof-CE/blob/c483d010a8402c5a1711517c2dce782b3551a0b8/initrd-progs/0initrd/init#L1084
  674.         #and copy_folders()  https://github.com/puppylinux-woof-CE/woof-CE/blob/c483d010a8402c5a1711517c2dce782b3551a0b8/initrd-progs/0initrd/init#L482
  675.           #https://github.com/puppylinux-woof-CE/woof-CE/blob/c483d010a8402c5a1711517c2dce782b3551a0b8/initrd-progs/0initrd/init#L1091
  676.           mount -o remount,prepend:"$FULL_SAVE_PATH"=rw,mod:"$SANDBOX_TMPFS"=ro,del:"$SANDBOX_TMPFS" "$FAKEROOT"
  677.           #mount -o remount,add:1:"$FULL_SAVE_PATH"=ro+wh "$FAKEROOT"
  678.       fi
  679.     fi
  680.        
  681.        
  682.         echo "Starting sandbox now."
  683.         log stop
  684.         if [ $USE_NS ]; then
  685.             unshare -f -p --mount-proc=$FAKEROOT/proc chroot $FAKEROOT
  686.         else
  687.             chroot $FAKEROOT
  688.         fi
  689.         log start
  690.         # 8. done - clean up everything
  691.         umountall
  692.         echo "Leaving sandbox."  
  693.    
  694. elif [ "${FAKEROOT:-/}" = "/" ]; then
  695.   #[ -z "$FAKEROOT" ] &&  $FAKEROOT="/"
  696.   echo "mount -t aufs -o \"remount,br:$SANDBOX_TMPFS=rw$robranches\" aufs  ${FAKEROOT:-/}"
  697.   echo "Warning!  about to remount rootfs at $FAKEROOT"
  698.   read -p "Press enter to continue"
  699.   trap - 1 #Clear traps
  700.   trap
  701.   mount -t aufs -o "br:$SANDBOX_TMPFS=rw$robranches" aufs ${FAKEROOT:-/};
  702.   mkdir -p /dev /sys /proc /tmp #These probably already exist
  703.   [[ "`mountpoint /dev`"  != *"is a mountpoint"* ]] && mount -t devtmpfs devtmpfs /dev
  704.   mkdir -p /initrd/mnt/tmpfs
  705.   [[ "`mountpoint /initrd/mnt/tmpfs`"  != *"is a mountpoint"* ]] && mount -t tmpfs none /initrd/mnt/tmpfs
  706.   mkdir -p /initrd/mnt/tmpfs/tmp
  707.   if [[ "`mountpoint /tmp`"  != *"is a mountpoint"* ]]; then
  708.     if [[ "`mountpoint /initrd/mnt/tmpfs`"  != *"is a mountpoint"* ]];
  709.       mount -o bind /initrd/mnt/tmpfs/tmp /tmp
  710.     else
  711.       mount -t tmpfs none /tmp
  712.     fi
  713.   fi
  714. else
  715.   echo "not implemented for FAKEROOT=${FAKEROOT:-/}}"
  716.   exit
  717. fi
  718.        
  719.  
  720.            
  721.  
  722.        
  723.        
  724.  
  725.     #else
  726.     #   echo "Unable to mount aufs br:$SANDBOX_IMG=rw$robranches"
  727.     #   umount -l $SANDBOX_IMG 
  728.     #fi
  729. #else
  730. #   echo "unable to mount rw image: $rwbranch"
  731. #fi
  732.  
  733.  
RAW Paste Data