s243a

remount_save.sh (draft 3)

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