Advertisement
s243a

psandbox2.sh (draft #3)

Dec 16th, 2020 (edited)
1,249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 52.39 KB | None | 0 0
  1. #!/bin/bash
  2. #Based on James Budiono 2015 sandbox.sh (version 10) but with many options added
  3. # version 10 - (2015) use pid/mount namespaces if available
  4. #
  5. # 0. directory locations
  6. #. $BOOTSTATE_PATH # AUFS_ROOT_ID
  7. #XTERM="defaultterm"
  8. #
  9. # All options below were added by s243a:
  10. #
  11. # -o, --output-file
  12. #    Just write layer paths to an output file but don't mount the sandbox.
  13. # --no-exit
  14. #   if an output file is specified (i.e. -o or --output-file) layer paths are just written to a file and the program exits unless the no-exit flag is specified.
  15. # f, --input-file
  16. #   read layer paths from a file rather than reading existing layers
  17. # m,--pmedia
  18. #   determines pupmodes. Refer to puppy boot parmaters
  19. # d, --pdrv
  20. #   this is the particiaion where the puppy files are located. The default is /mnt/home
  21. # s, psubdir
  22. #   this is the sub directory where the puppy files are located
  23. # c, --clear-env
  24. #   deletes enviornental variabls
  25. # --env-prefix
  26. #   enviornental variable prefix
  27. # b --boot-config
  28. #   path to boot config (e.g. /etc/rc.d/BOOTCONFIG
  29. # --disto-specs
  30. #   path to distro specs (e.g. /etc/DISTRO_SPECS; e.g. /initrd/distro-specs)
  31. # L, --layer
  32. #   a subgke kater
  33. #  e, --extra-sfs
  34. #   a list of extra sfs files (space seperated)
  35. #  u, --union-record
  36. # --xterm
  37. # --sandbox
  38. # -initrd
  39. # --save
  40. # --noexit
  41. # --psave
  42. # --pupmode
  43.  
  44. #I thought some assoitive arrays might be useful but I'm not using them yet.
  45. #declare -A KEYs_by_MNT_PT
  46. #declare -A KEYs_by_FILE_PATH
  47. #declare -A KEYs_by_trimmed_MNT_PT
  48. #declare -A KEYs_by_trimmed_FILE_PATH
  49. #declare -A MNT_PTs
  50. #declare -A FILE_PATHs
  51. #declare -A ON_status
  52. CWD=$(realpath .)
  53. [ -z "$CWD" ] && CWD="$PWD"
  54. declare -a bind_sources
  55. declare -a bind_targets
  56. declare -a rev_bind_sources
  57. declare -a rev_bind_targets
  58. cd "$(dirname "$0")"
  59. MAX_STR_LEN=50
  60. SANDBOX_ID=
  61. TMPFILE=$(mktemp -p /tmp)
  62. # use namespaces if available
  63. #[ -e /proc/1/ns/pid ] && [ -e /proc/1/ns/mnt ] && type unshare >/dev/null && USE_NS=1
  64.  
  65. XTERM=${XTERM:-urxvt}
  66. SANDBOX_ROOT=${SANDBOX_ROOT:-/mnt/sb}
  67.  
  68. declare -a options2
  69. declare -a binds
  70. LOG_INITIALIZED=false
  71.  
  72. RP_FN="`which realpath`"
  73. RP_TARGET=$($RP_FN "$RP_FN")
  74. [ -z "$RP_TARGET" ] && RP_TARGET="`readlink $RP_FN`"
  75. declare -a del_rules_filter
  76. declare -a del_rules_action
  77. declare -a umount_rules_filter
  78. declare -a umount_rules_action
  79.  
  80. unset SANDBOX_ID
  81. unset rwbranch
  82. unset FAKEROOT
  83. unset SANDBOX_TMPFS
  84. unset FAKEROOT_dir
  85. unset SANDBOX_IMG
  86. FAKEROOT_SET=false
  87.  
  88. function realpath(){
  89.     case "$RP_TARGET" in
  90.     *busybox*)
  91.       if [ "$1" = -m ]; then
  92.         shift
  93.         A_PATH=$1
  94.         A_PATH=$(echo "$A_path" | sed 's#^./#'"$CWD"'#g' )
  95.         A_PATH=$(echo "$A_path" | sed 's#^../#'"$CWD"'#g' ) && A_PATH=$(dirname $A_PATH)
  96.         echo "Warning simulating '-m' option since it isn't supported by busybox" >2
  97.         echo "A_PATH=$A_PATH" >2
  98.         if [ -f  "$A_PATH" ] || [ -d  "$A_PATH" ]; then
  99.           $RP_FN "$@"
  100.         else
  101.           echo "$A_PATH"
  102.         fi
  103.       else
  104.         $RP_FN "$@"
  105.       fi
  106.       ;;
  107.     *)
  108.       $RP_FN "$@"
  109.       ;;
  110.     esac
  111. }
  112. export -f realpath
  113.  
  114. if [ -f ../local/psandbox/sandbox.awk ]; then
  115.   SANDBOX_AWK="$(realpath ../local/psandbox/sandbox.awk)"
  116. elif [ -f /usr/local/psandbox/sandbox.awk ]; then
  117.  SANDBOX_AWK=/usr/local/psandbox/sandbox.awk
  118. fi
  119. SANDBOX_AWK_DIR="$(dirname $SANDBOX_AWK)"
  120. if [ -f ../local/psandbox/sb_db_rec_field.awk ]; then
  121.   SB_DB_REC_FIELD_AWK="$(realpath ../local/psandbox/sb_db_rec_field.awk)"
  122. elif [ -f /usr/local/psandbox/sb_db_rec_field.awk ]; then
  123.   SB_DB_REC_FIELD_AWK=/usr/local/psandbox/sb_db_rec_field.awk
  124. fi
  125.  
  126. if [ -f ../local/psandbox/sandbox_mnt_fn.sh ]; then
  127.   SANDBOX_MNT_FN="$(realpath ../local/psandbox/sandbox_mnt_fn.sh)"
  128. elif [ -f /usr/local/psandbox/sandbox_mnt_fn.sh ]; then
  129.   SANDBOX_MNT_FN=/usr/local/psandbox/sandbox_mnt_fn.sh
  130. fi
  131.  
  132. . "$SANDBOX_MNT_FN"
  133.  
  134. function get_items(){
  135.     local out
  136.     OUTFILE=/tmp/get_items_out
  137.     rm "$OUTFILE"
  138.     cd "$SANDBOX_AWK_DIR"
  139.     out+="$(
  140.  { echo ==mount==; cat /proc/mounts;
  141.    echo ==losetup==; losetup-FULL -a;
  142.    echo ==branches==;
  143.      if [ $# -eq 0 ]; then
  144.        ls -v /sys/fs/aufs/$AUFS_ROOT_ID/br[0-9]* | xargs sed 's/=.*//';
  145.      else
  146.        if [ "$1" = "-f" ]; then
  147.          cat "$2";
  148.        elif [ "$1" = "-s" ]; then
  149.          cat <<<"$2";
  150.        fi;
  151.      fi; } | \
  152.    awk -v PDRV="$PDRV" -v MAX_STR_LEN="$MAX_STR_LEN" -v OUTFILE="$OUTFILE" \
  153. -f "$SANDBOX_AWK"
  154. )"
  155.   echo "$out"
  156. }
  157. function process_psubdir(){
  158.       item_source="$1"
  159.       if [ "$item_source" = "maybe-psubdir" ]; then
  160.          [ ! -z "$items" ] && continue
  161.       fi
  162.       [ -z "$DISTRO_ADRVSFS" ] && DISTRO_ADRVSFS="$(ls -1 "${PDRV}/${PSUBDIR}" | grep -i -m1 'adrv.*\.sfs$')"
  163.       [ -z "$DISTRO_YDRVSFS" ] && DISTRO_YDRVSFS="$(ls -1 "${PDRV}/${PSUBDIR}" | grep -i -m1 'ydrv.*\.sfs$')"  
  164.       [ -z "$DISTRO_ZDRVSFS" ] && DISTRO_ZDRVSFS="$(ls -1 "${PDRV}/${PSUBDIR}" | grep -i -m1 'zdrv.*\.sfs$')"
  165.       [ -z "$DISTRO_FDRVSFS" ] && DISTRO_FDRVSFS="$(ls -1 "${PDRV}/${PSUBDIR}" | grep -i -m1 'fdrv.*\.sfs$')"                        
  166.       [ -z "$DISTRO_PUPPYSFS" ] && DISTRO_PUPPYSFS="$(ls -1 "${PDRV}/${PSUBDIR}" | grep -i -m1 'puppy_.*\.sfs$')"
  167.  
  168.       new_items=""
  169.       for rec in "$DISTRO_ADRVSFS" "$DISTRO_YDRVSFS" "$DISTRO_ZDRVSFS" "$DISTRO_FDRVSFS" "$DISTRO_PUPPYSFS";  do
  170.         #MNT_PATH="${rec}"
  171.         [ -z "$rec" ] && continue
  172.         #[ ! -z "${PSUBDIR}" ] && MNT_PATH=${PSUBDIR}/${MNT_PATH}
  173.         MNT_PATH="${PDRV}/${PSUBDIR}/$rec"
  174.         MNT_PT="$(mount_fn "$MNT_PATH")"
  175.         new_items+="\"${MNT_PT}\" \"$rec\" \"on\""$'\n'
  176.        
  177.       done
  178.       #export new_items="$new_items"
  179.       #echo "$new_items"
  180.       log stop
  181.       new_items_result="$(get_items -s "$new_items")"$'\n'
  182.       items+="$new_items_result"
  183.       echo "items+=\"$new_items_result\""
  184.       log start
  185.      
  186. }
  187. process_union_record(){
  188.        new_items=''
  189.        for rec in $LASTUNIONRECORD; do
  190.         if [ -f "$rec" ]; then
  191.           MNT_PT="$(mount_fm "$rec" )"
  192.           new_items+="\"$MNT_PT\" \"$rec\" \"on\""$'\n'
  193.         elif [ -f "$PDRV/$rec" ]; then
  194.           MNT_PT="$(mount_fm "$PDRV/$rec" )"
  195.           new_items+="\"$MNT_PT\", \"$PDRV/$rec\", \"on\""$'\n'
  196.         fi
  197.       done
  198.       items+="$(get_items -f <<<"$new_items")"$'\n'    
  199. }
  200. process_extra_sfs(){
  201.      EXTRASFSLIST="$2";
  202.      unset new_items
  203.      if [ ! -f "$EXTRASFSLIST" ]; then
  204.        EXTRASFSLIST_tmp=$(realpath "$PDRV/$PSUBDIR/$EXTRASFSLIST")
  205.        if [ -f "$EXTRASFSLIST_tmp" ]; then
  206.          EXTRASFSLIST="$EXTRASFSLIST_tmp"
  207.        fi
  208.      fi
  209.      if [ ! -f "$EXTRASFSLIST" ]; then
  210.        EXTRASFSLIST_tmp=$(realpath "$PDRV/$EXTRASFSLIST")
  211.        if [ -f "$EXTRASFSLIST_tmp" ]; then
  212.          EXTRASFSLIST="$EXTRASFSLIST_tmp"
  213.        fi
  214.      fi
  215.      if [[ "$EXTRASFSLIST" = *.sfs ]]; then
  216.          a_sfs="$EXTRASFSLIST"
  217.          MNT_PT="$(mount_fn "$a_sfs" )"
  218.          new_items+="\"$MNT_PT\" \"$a_sfs\" \"on\""$'\n'
  219.      else
  220.        while read a_sfs; do
  221.          a_sfs=$(echo"$a_sfs") #Trims leading and trailing whitespace
  222.          if [ -f "$a_sfs" ]; then
  223.            a_sfs=$(realpath "$a_sfs")
  224.          else
  225.            a_sfs1="$PDRV/${PSUBDIR}/$a_sfs"
  226.            a_sfs=$(realpath "$a_sfs")
  227.            if [ -f "$a_sfs"]; then
  228.              a_sfs=$(realpath "$a_sfs")
  229.            else        
  230.              a_sfs1="$PDRV/$a_sfs1"
  231.              if [ -f "$a_sfs1" ]; then
  232.                a_sfs=$(realpath "$a_sfs")
  233.              fi            
  234.            fi
  235.          fi
  236.          if [ -f  "$a_sfs" ]; then
  237.            MNT_PT="$(mount_fn "$a_sfs" )"
  238.            new_items+="\"$MNT_PT\" \"$a_sfs\" \"on\""$'\n'        
  239.          fi
  240.        done <"$EXTRASFSLIST"
  241.      fi
  242.      items+="$(get_items -s "$new_items")"$'\n'
  243.      #items+="$(get_items -f <<<"$new_items")"
  244. }
  245. process_layer(){
  246.       item_path="$2"
  247.       if [ -f "$item_path" ]; then
  248.         MNT_PT="$(mount_fm "$item_path" )"
  249.       elif [ -d "$item_path" ]; then  
  250.         MNT_PT="$item_path" #This isn't really a mount poing
  251.       elif [ ! -d  "$item_path" ]; then
  252.         echo "Warning  cannot mount $item_path"
  253.         continue
  254.       fi
  255.       items+="\"$MNT_PT\" \"$item_path\" \"on\""$'\n'  
  256. }
  257.  
  258. function mount_items(){
  259.   local Moun_Point
  260.   local File_PATH #Might be a directory
  261.   cd "$SANDBOX_AWK_DIR"
  262.   while IFS="" read -r p || [ -n "$p" ]; do #https://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash
  263.      File_PATH="$(echo "$1" | awk -v FIELD_NUM=6 -f "$SB_DB_REC_FIELD_AWK")"
  264.      Mount_Point="$(echo "$1" | awk -v FIELD_NUM=1 -f "$SB_DB_REC_FIELD_AWK")"
  265.      PDRV_MNT="$(echo "$1" | awk -v FIELD_NUM=7 -f "$SB_DB_REC_FIELD_AWK")"
  266.      PDRV_UUID="$(echo "$1" | awk -v FIELD_NUM=8 -f "$SB_DB_REC_FIELD_AWK")"
  267.      
  268.      [ -z "$PDRV_MNT" ] &&
  269.      mount_fn2 "PDRV" "$File_PATH" "$Moun_Point"
  270.   done <"$1"
  271. }
  272. function log(){
  273.   local SET_X=false
  274.   set +x #TODO add more verbose log option that doesn't do this.
  275.   local logfile="${2}"
  276.   local trace="$3"
  277.   #[ -z "$logfile" ] && LOGFILE
  278.   #[ -z "$trace" ] && trace=TRACE
  279.   if [ ! -z "$LOGFILE" ]; then
  280.     case "$1" in
  281.     init)
  282.       [ "$TRACE" = true ] && SET_X=true
  283.       [ ! -f "$LOGFILE" ] && rm "$LOGFILE"
  284.       exec 6>&1           # Link file descriptor #6 with stdout.
  285.       #exec &1> >(tee -a "$LOGFILE")
  286.       #exec &2> >(tee -a "$LOGFILE")
  287.       [ ! -f "$LOGFILE" ] && touch "$LOGFILE"
  288.       exec &> >(tee -a "$LOGFILE")
  289.       ;;
  290.     start)
  291.       [ "$TRACE" = true ] && SET_X=true
  292.       #exec &1> >(tee -a "$LOGFILE")
  293.       #exec &2> >(tee -a "$LOGFILE")
  294.      
  295.       exec &> >(tee -a "$LOGFILE")
  296.       ;;
  297.     stop)
  298.       #https://stackoverflow.com/questions/21106465/restoring-stdout-and-stderr-to-default-value
  299.       #[ "$TRACE" = true ] && set +x
  300.       exec 1>&6  
  301.       #exec 6>&-      # Restore stdout and close file descriptor #6.
  302.       exec &2> /dev/stderr    
  303.       ;;
  304.     esac
  305.   fi    
  306.   [ "$SET_X" = true ] && set -x
  307. }
  308.  
  309.  
  310.  
  311. safe_delete(){
  312.     unset safe_delete_result
  313.     POLICY="" #Default action if no rule is found
  314.   if [ ! -z "$(cat /proc/mounts | grep -F "$(ls -1d $1 | sed 's:/$::')" - )" ] ||
  315.     [ ! -z "$(cat /proc/mounts | grep -F "$(ls -1d $1 | xargs realpath )" - )" ] ; then
  316.          #It is not safe to delete a mounted directory
  317.          safe_delete_result=1 #This is in case one doesn't want to use the return status right away
  318.          return 1    
  319.  else
  320.    PATH_TO_DEL="$(realpath -m $1)"
  321.    [ -d "$PATH_TO_DEL" ] && "PATH_TO_DEL=$PATH_TO_DEL/"
  322.    for a_rule_key in "${!del_rules_filter[@]}"; do
  323.      rule_i="${a_rull_key[$a_rule_key]}"
  324.      if [ ! -z "$(echo $PATH_TO_DEL | grep "$rule_i")" ] ||
  325.         [[ "$PATH_TO_DEL" = $rule_i ]]; then
  326.        action="${del_rules_action[$a_rule_key]}"
  327.        case $action in
  328.        DELETE)
  329.          safe_delete_result=0 #This is in case one doesn't want to use the return status right away
  330.          return 0
  331.          break #This is probably redundant
  332.          ;;
  333.        KEEP)
  334.          safe_delete_result=1 #This is in case one doesn't want to use the return status right away
  335.          return 1
  336.          break #This is probably redundant
  337.          ;;          
  338.        POLICY_KEEP)
  339.          POLICY=KEEP
  340.          ;;    
  341.        POLICY_DELETE)
  342.          POLICY=DELETE
  343.          ;;    
  344.        esac
  345.      fi
  346.    done
  347.    if [ -z ${safe_delete_result+x} ]; then #If we don't have a result yet set result based on policy
  348.      case "$POLICY" in
  349.      KEEP)
  350.        safe_delete_result=1
  351.        return 1; ;;
  352.      DELETE)
  353.        safe_delete_result=0
  354.        return 0; ;;
  355.      *)
  356.        echo "No Delete policy set, so keeping file/dir $1"
  357.        safe_delete_result=1
  358.        return 1    
  359.      esac
  360.    fi
  361.  fi
  362. }
  363. safe_umount(){
  364.    unset safe_umount_result
  365.    POLICY="" #Default action if no rule is found
  366.    PATH_TO_UMOUNT="$(realpath -m $1)"
  367.    [ -d "$PATH_TO_UMOUNT" ] && PATH_TO_UMOUNT="$PATH_TO_UMOUNT/" #TODO, this should always be true so add error if it is not.
  368.  
  369.    for a_rule_key in "${!umount_rules_filter[@]}"; do
  370.      rule_i="${a_rull_key[$a_rule_key]}"
  371.      if [ ! -z "$(echo $1 | grep "$rule_i")" ] ||
  372.        [[ "$PATH_TO_UMOUNT" = $rule_i ]]; then
  373.        action="${umount_rules_action[$a_rule_key]}"
  374.        case $action in
  375.        DELETE)
  376.          safe_umount_result=0 #This is in case one doesn't want to use the return status right away
  377.          return 0
  378.          break #This is probably redundant
  379.          ;;
  380.        UMOUNT)
  381.          safe_umount_result=1 #This is in case one doesn't want to use the return status right away
  382.          return 1
  383.          break #This is probably redundant
  384.          ;;          
  385.        POLICY_KEEP)
  386.          POLICY=KEEP
  387.          ;;    
  388.        POLICY_UMOUNT)
  389.          POLICY=UMOUNT
  390.          ;;    
  391.        esac
  392.      fi
  393.    done
  394.    if [ -z ${safe_umount_result+x} ]; then #If we don't have a result yet set result based on policy
  395.      case "$POLICY" in
  396.      KEEP)
  397.        safe_umount_result=1
  398.        return 1; ;;
  399.      UMOUNT)
  400.        safe_umount_result=0
  401.        return 0; ;;
  402.      *)
  403.        echo "No Delete policy set, so keeping file/dir $1"
  404.        safe_umount_result=1
  405.        return 1    
  406.      esac
  407.    fi
  408.  
  409. }
  410. umountall() {
  411.  {
  412.  log start
  413.  set -x
  414.  #R_FR=$(realpath -m "$FAKEROOT")
  415.  #[ ${#R_FR} -lt 2 ] && exit
  416.  safe_umount $SANDBOX_TMPFS
  417.  [ $safe_umount_result -eq 0 ] && umount -l $SANDBOX_TMPFS
  418.  if [ PUPMODE = 2 ]; then #Full Install
  419.      safe_umount $FAKEROOT/tmp
  420.      umount -l $FAKEROOT/tmp
  421.    else
  422.      safe_umount $FAKEROOT/initrd/mnt/tmpfs
  423.      [ $safe_umount_result -eq 0 ] && umount -l $FAKEROOT/initrd/mnt/tmpfs
  424.    fi
  425.  for layer_name in "pup_ro2" "pup_ro3" "pup_ro4" "pup_ro5" "pup_z"; do
  426.    layer="$(eval 'echo $'$layer_name)"
  427.    if [ ! -z "$layer" ] ; then
  428.      safe_umount "$FAKEROOT/initrd/$layer_name"
  429.      [ $safe_umount_result -eq 0 ] && umount -l "$FAKEROOT/initrd/$layer_name"
  430.    fi
  431.  done
  432.  for aFolder in /proc /sys /dev ""; do
  433.     safe_umount $FAKEROOT"/$aFolder"
  434.     [ $safe_umount_result -eq 0 ] && umount -l $FAKEROOT$aFolder
  435.  done
  436.  
  437.  
  438.  if [ -z "$RW_LAYER" ]; then
  439.    safe_umount "$SANDBOX_TMPFS"
  440.    [ $safe_umount_result -eq 0 ] && umount -l $SANDBOX_TMPFS
  441.    safe_delete "$SANDBOX_TMPFS"
  442.    [ $safe_delete_result -eq 0 ] && rmdir $SANDBOX_TMPFS
  443.  fi
  444.  
  445.  safe_delete $FAKEROOT
  446.  [ $safe_delete_result -eq 0 ] && rmdir $FAKEROOT
  447.  
  448.  } # 2> /dev/null
  449. }
  450. function choose_save(){
  451.    # location not specified - then ask
  452.    log stop
  453.    dialog --backtitle "rw image sandbox" --title "choose rw image" \
  454.    --extra-button --extra-label "Create" --ok-label "Locate" \
  455.    --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
  456.    chosen=$?
  457.    log start
  458.    case $chosen in
  459.        0) # ok - locate
  460.            log stop
  461.            dialog --backtitle "rw image sandbox" --title "Specify location of existing rw image" --fselect `pwd` 8 60 2> $TMPFILE
  462.            savebranch=`cat $TMPFILE`
  463.            log start
  464.            rm $TMPFILE
  465.            if [ -n "$savebranch" ]; then
  466.                if [ -d "$savebranch" ]; then
  467.                  case "$savebranch" in
  468.                  "/mnt"|"/mnt/"|"/mnt/home"|"/mnt/home/"|"/")
  469.                    log stop
  470.                    echo "warning chose the following savebranch $savebranch"
  471.                    read -p "Press enter to continue"
  472.                    log start
  473.                    set_sandbox_img ""
  474.                    ;;
  475.                  esac
  476.                  SANDBOX_IMG=$savebranch
  477.                elif [ ! -f "$savebranch" ]; then
  478.                    echo "$savebranch doesn't exist - exiting."
  479.                    echo "will use tmpfs instead"
  480.                    log stop
  481.                    read -p "Press enter to continue"
  482.                    log start
  483.                    savebranch=""
  484.                else
  485.                  set_sandbox_img ""                    
  486.                fi
  487.            else
  488.                echo "You didn't specify any file or you pressed cancel. Exiting."
  489.                exit
  490.            fi
  491.            ;;
  492.        3) # create
  493.            echo "create"
  494.            log stop
  495.            dialog --backtitle "save image sandbox" --title "Specify name and path of new the file" --fselect `pwd` 8 60 2> $TMPFILE
  496.            savebranch=`cat $TMPFILE`
  497.            log start
  498.            rm $TMPFILE
  499.            if [ -n "$savebranch" ]; then
  500.                if [ -f "$savebranch" ]; then
  501.                    echo "$savebranch already exist - exiting."
  502.                    exit
  503.                else
  504.                    # get the size
  505.                    log stop
  506.                    dialog --title "Create new save image" --inputbox "Specify size (in megabytes)" 0 40 100 2> $TMPFILE
  507.                    size=`cat $TMPFILE`
  508.                    log start
  509.                    rm $TMPFILE
  510.                    
  511.                    if [ -n "$size" ]; then
  512.                        if dd if=/dev/zero of="$savebranch" bs=1 count=0 seek="$size"M; then
  513.                            if ! mke2fs -F "$savebranch"; then
  514.                                echo "I fail to make an ext2 filesystem at $savebranch, exiting."
  515.                                exit
  516.                            fi
  517.                        else
  518.                            echo "I fail to create a ${size}M file at $savebranch,, exiting."
  519.                            exit
  520.                        fi
  521.                    else
  522.                        echo "You didn't specify the size or your press cancel. Exiting."
  523.                        exit
  524.                    fi                  
  525.                fi
  526.            else
  527.                echo "You didn't specify any file or you pressed cancel. Exiting."
  528.                exit
  529.            fi
  530.            ;;
  531.        1 | 255) # 1 is cancel, 255 is Escape
  532.            ;&
  533.        *) # invalid input - treat as cancel
  534.            echo "Cancelled - exiting."
  535.            exit
  536.            ;;
  537.    esac
  538. }
  539. function find_save(){
  540.  for prefix in '${DISTRO_FILE_PREFIX}save' '.*save'; do
  541.    for dir in "$PDRV/${PSUBDIR}" "PDRV";  do
  542.      
  543.      ONE_SAVE="$(ls $dir -1 | grep -m "${prefix}save")"
  544.      if [ -z "$ONE_SAVE" ]; then
  545.         continue
  546.      else
  547.         SAVE_FILE="$ONE_SAVE"
  548.         FULL_SAVE_PATH="$dir"/ONE_SAVE
  549.         break
  550.      fi
  551.    done
  552.   done
  553.   echo "PSAVE"mount_items
  554. }
  555. function find_bk_folders(){
  556.  for a_PDRV in "$PDRV" sr0 sr1; do #Consider adding /mnt/home here
  557.    for a_psubdir in "${PSUBDIR}" "";  do
  558.      MT_PT_of_Folder="$(mount_fn2 "$PDRV" "${PSUBDIR}")"
  559.      #https://github.com/puppylinux-woof-CE/woof-CE/blob/c483d010a8402c5a1711517c2dce782b3551a0b8/initrd-progs/0initrd/init#L981
  560.      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)"
  561.      [ ! -z "#BKFOLDERS" ] && break  
  562.    done
  563.  done
  564. }
  565. function mk_initrd_dir(){
  566.  mkdir -p "$FAKEROOT"/initrd
  567.  if [ -z "$PUPMODE" ] ; then
  568.    if [ -z "$PMEDIA" ]; then
  569.      #if [ "$PUPMODE" = 5 ] ; then
  570.      #  #aufs layers:              RW (top)      RO1             RO2              PUPMODE
  571.      #  #First boot (or pfix=ram): tmpfs                         pup_xxx.sfs      5
  572.      PUPMODE=5 #MAYBE PUPMODE=2 would be better
  573.    elif [ PMEDIA = 'atahd' ] || [ "$PMEDIA" = 'usbhd' ]; then
  574.      find_save
  575.      if [ -f "$FULL_SAVE_PATH" ] || [ -d "$FULL_SAVE_PATH" ]; then
  576.        #aufs layers:               RW (top)      RO1             RO2              PUPMODE
  577.        #Normal running puppy:      pup_save.3fs                  pup_xxx.sfs      12      
  578.        PUPMODE=12
  579.      else
  580.        echo "Invalid SAVE_PATH=$SAVE_PATH does not exist"
  581.        PUMPMODE=2
  582.        #TODO, prompt to either search for save file/folder or alternatively create it.
  583.      fi
  584.    elif [ PMEDIA = 'usbflash' ] || [ pmedia = 'ideflash' ]; then
  585.      find_save
  586.      #aufs layers:                 RW (top)      RO1             RO2              PUPMODE
  587.      #ditto, but flash drive:      tmpfs         pup_save.3fs    pup_xxx.sfs      13
  588.      if [ -f "$SAVE_PATH" ] || [ -d "$SAVE_PATH" ]; then
  589.        #aufs layers:               RW (top)      RO1             RO2              PUPMODE
  590.        #ditto, but flash drive:    tmpfs         pup_save.3fs    pup_xxx.sfs      13
  591.        PUPMODE=13
  592.      else
  593.        echo "Invalid SAVE_PATH=$SAVE_PATH does not exist"
  594.        PUPMODE=5
  595.      fi
  596.    elif [ "$PMEDIA" =  usbcd ] || [ "$PMEDIA" =  idecd ] || [ "$PMEDIA" =  satacd ] ; then
  597.      find_bk_folders
  598.      if [ ! -z "$BKFOLDERS" ]; then
  599.        PUPMODE=77  #MULTI-Session CD
  600.      else #First Boot
  601.        find_save
  602.        if [ -f "$FULL_SAVE_PATH" ] || [ -d "$FULL_SAVE_PATH" ]; then
  603.          PUPMODE=13      
  604.        else
  605.          PUPMODE=5
  606.        fi
  607.      fi
  608.      #aufs layers:            RW (top)      RO1             RO2              PUPMODE
  609.      #Multisession cd/dvd:       tmpfs         folders         pup_xxx.sfs      77
  610.    else #[PUPMODE=2 -> full install
  611.      PUPMODE=2
  612.    fi
  613.    #TODO: add option to make initrd anyway even if full install. For now make it regardless:
  614.    if [ "$PUPMODE" = 2 ] && [ 1 -ne 1 ]; then #Full install; then #Full install
  615.      echo "Full install has no initrd"
  616.    else
  617.      mkdir -p "$FAKEROOT/initrd"
  618.      cd $FAKEROOT/initrd
  619.      if [ "$PUPMODE" = 12 ]; then # Usually [ PMEDIA = 'atahd' ] || [ "$PMEDIA" = usbhd ]
  620.        ln -s mnt/dev_save/"${SAVE_PATH}" pup_rw
  621.      elif [ "$PUPMODE" = 13 ] || [ "$PUPMODE" = 5 ] || [ "$PUPMODE" = 77 ]; then
  622.        ln -s mnt/tmpfs/pup_rw pup_rw
  623.        if [ "$PUPMODE" = 13 ]; then  # Usually [ PMEDIA = 'usbflash' ] || [ pmedia = 'ideflash' ]
  624.          ln -s "mnt/tmpfs/dev_save/${SAVE_PATH}" pup_ro1
  625.        elif [ "$PUPMODE" = 77 ]; then
  626.          ln -s mnt/tmpfs/pup_ro1/"${SAVE_PATH}" pup_ro1  #Usually [ "$PMEDIA" =  usbcd ] || [ "$PMEDIA" =  idecd ] || [ "$PMEDIA" =  satacd ]
  627.        fi
  628.      fi
  629.      #!!!! This code was moved from elsehwere inside this section.
  630.       mkdir -p "$FAKEROOT/initrd/mnt/dev_save"
  631.      mount -o bind  /mnt/home "$FAKEROOT/initrd/mnt/dev_save"
  632.      mkdir $FAKEROOT/mnt
  633.      cd $FAKEROOT/mnt
  634.      ln -s ../initrd/mnt/dev_save
  635.      cd $FAKEROOT
  636.      mount -o bind $SANDBOX_IMG initrd/mnt/tmpfs/pup_rw #not sure if this applies to all pupmodes.    
  637.    fi
  638.  fi
  639. }
  640. function set_fakeroot(){
  641.    unset CLEANUP_SANDBOX_ROOT
  642.    # if SANDBOX_ROOT is set to null then set it in this function
  643.    # if FAKEROOT is null then this implies "/"
  644.    if [ -z ${FAKEROOT+x} ] && [ $FAKEROOT_SET = false ]; then
  645.      FAKEROOT=fakeroot
  646.      CLEANUP_SANDBOX_ROOT=yes
  647.    fi
  648.    if [ ! -z ${SANDBOX_ROOT+x} ] || [ ${#FAKEROOT} -gt 1 ]; then
  649.      
  650.      if [ "${CLEANUP_SANDBOX_ROOT}" = yes ]; then
  651.        SANDBOX_ROOT=/mnt/sb
  652.      else
  653.        FAKEROOT_SET
  654.      fi    
  655.      
  656.  
  657.      if [ ${#SANDBOX_ROOT} -gt 1 ] && [ $FAKEROOT_SET = false ]; then
  658.        FAKEROOT=$SANDBOX_ROOT/$FAKEROOT
  659.        FAKEROOT_SET=true
  660.      elif [ ! -z ${FAKEROOT+x} ]; then
  661.        FAKEROOT_SET=true
  662.      fi
  663.       FAKEROOT_dir="${FAKEROOT%/*}"
  664.      [ -z "${SANDBOX_ROOT}" ] && SANDBOX_ROOT=${FAKEROOT_dir}
  665.      if grep -q $FAKEROOT /proc/mounts; then
  666.        if [ ! "$SANDBOX_ROOT" = /mnt/sb ]; then
  667.            log stop
  668.            dialog --backtitle "rename root" --title "already mounted" \
  669.            --extra-button --extra-label "Rename" --ok-label "Keep" \
  670.            --yesno "FAKEROOTI is already a mount point. Rename Root?" 0 0
  671.            chosen=$?
  672.            log start      
  673.           case "#chosen" in
  674.           1)
  675.             RENAME_ROOT=yes; ;;
  676.           0)
  677.             RENAME_ROOT=no; ;;
  678.           esac
  679.          
  680.        else
  681.          RENAME_ROOT=yes
  682.        fi  
  683.        
  684.        if [ "${RENAME_ROOT}" = yes ]; then    
  685.          FAKEROOT=$(mktemp -d -p ${FAKEROOT%/*}/ ${FAKEROOT##*/}.XXXXXXX)
  686.          SANDBOX_ID=".${FAKEROOT##*.}"
  687.          if [ -z "$SANDBOX_IMG" ]; then
  688.            [ -z "$savebranch" ] && choose_save
  689.            savebranch="$(realpath -m $savebranch)"
  690.            if [ -d  "$savebranch" ]; then
  691.              #TODO: verify this works if savebranch is a mounted directory.
  692.              SANDBOX_IMG="$savebranch"
  693.            else
  694.              [ ! -z "$savebranch"  ] && loop=$(losetup-FULL -a | grep  "$savebranch"  | sed "s/:.*$//" )
  695.              if [ ! -z "$loop" ]; then
  696.                SANDBOX_IMG=$(cat /proc/mounts | grep $loop | cut -d " " -f2)
  697.              fi
  698.              if [ -z "$SANDBOX_IMG" ] ; then
  699.                SANDBOX_IMG=$FAKEROOT_dir/sandbox_img${SANDBOX_ID}
  700.                mkdir -p $SANDBOX_IMG
  701.              fi
  702.            fi
  703.            rmdir $FAKEROOT
  704.          else
  705.            log stop
  706.            echo "Warning chose to remount over existing mount! $FAKEROOT"
  707.            echo "ctrl-c to quote"
  708.            read -p "Press enter to continue"  
  709.            log start    
  710.          fi
  711.        fi
  712.      else
  713.            echo "This is our first sandbox"
  714.      fi
  715.    else
  716.      echo "Warning sandbox root not defined"
  717.      #[ -z "$FAKEROOT" ] && FAKEROOT=/
  718.    fi
  719.    if [ $CLEANUP_SANDBOX_ROOT = yes ]; then
  720.      if [ ${#del_rules_filter} -eq 0 ]; then
  721.         del_rules_filter+=( $SANDBOX_ROOT"/*" )
  722.         del_rules_filter+=( POLICY_DELETE )
  723.      fi
  724.      if [ ${#umount_rules_filter} -eq 0 ]; then
  725.         umount_rules_filter+=( $SANDBOX_ROOT"/*" )
  726.         umount_rules_filter+=( POLICY_UMOUNT )
  727.      fi    
  728.    fi
  729.    mkdir -p "$SANDBOX_ROOT"
  730.    mkdir -p "$FAKEROOT"
  731. }
  732. function set_sandbox_img(){
  733.     [ -z ${FAKEROOT_dir+x} ] &&  set_fakeroot
  734.     if [ ! -z ${SANDBOX_IMG+x} ]  && [ -z "${SANDBOX_IMG}" ] || [ ! -z ${1+x} ]; then
  735.      SANDBOX_IMG=sandbox_img
  736.      SANDBOX_IMG=${SANDBOX_IMG}${SANDBOX_ID}
  737.      [ ! -z "$FAKEROOT_dir" ] && SANDBOX_IMG=$FAKEROOT_dir/$SANDBOX_IMG
  738.     elif [ -z "${FAKEROOT_dir}" ] ; then
  739.      SANDBOX_IMG=$FAKEROOT_dir/$SANDBOX_IMG
  740.     fi
  741. }
  742. function set_sandbox_tmpfs(){
  743.     [ -z ${FAKEROOT_dir+x} ] &&  set_fakeroot
  744.     if [ ! -z ${SANDBOX_TMPFS+x} ]  && [ -z "${SANDBOX_TMPFS}" ]; then
  745.      SANDBOX_TMPFS=sandbox_tmpfs
  746.      SANDBOX_TMPFS=${SANDBOX_TMPFS}${SANDBOX_ID}
  747.      [ -z "$FAKEROOT_dir" ] && SANDBOX_TMPFS=$FAKEROOT_dir/$SANDBOX_TMPFS
  748.    elif [ -z "${FAKEROOT_dir}" ] ; then
  749.      SANDBOX_TMPFS=$FAKEROOT_dir/$SANDBOX_TMPFS
  750.    fi
  751. }
  752. function make_set(){
  753.  log stop
  754.  local item
  755.  local key
  756.  unset make_set_rtn
  757.  declare -gA make_set_rtn
  758.  for item in "$@"; do
  759.    key=md5_$(md5sum < <( echo "$item" ) | cut -f1 -d' ')
  760.    make_set_rtn[$key]="$item"
  761.  done
  762.  log start
  763. }
  764.  
  765.  
  766. declare -a options="$(getopt -o f:,o:,m:,d:,s:,b:,e:,l:,t::,a::,u::,r::,j: --long input-file:,output-file:,:tmpfs::,root::,pmedia:,pdrv:,psubdir:,boot-config:,distro-specs:,extra-sfs:,aufs,maybe-aufs,maybe-psubdir:,no-exit::,psave:,pupmode:,logfile:,trace::,rw-layer:,copy-Xauth::,bind-X11-sockets::,copy-resolv_conf::,layer:,rev-bind:,bind:,before-chroot: -- "$@")"
  767. eval set --"$options_str"
  768. #set -- $options
  769. #declare -a options=$( $options_str )
  770. eval set --"$options"
  771. while [ $# -gt 0 ]; do
  772.  log stop
  773.  echo "processing args: $@"
  774.  log start
  775.  case $1 in
  776.  -f|--input-file)
  777.     INPUT_FILE=$2
  778.    mount_items "$INPUT_FILE"
  779.    items+="$(get_items -f "$INPUT_FILE")"
  780.    shift 2; ;;      
  781.  -o|--output-file) OUTPUT_FILE=$2; shift 2; ;;
  782.  --no-exit)
  783.    if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  784.      NO_EXIT="$2"
  785.      [ -z "$NO_EXI" ] && NO_EXIT=true
  786.      shift 2
  787.    else
  788.      NO_EXIT=true
  789.      shift 1
  790.    fi; ;;
  791.  -p|--env-prefix) ENV_PREFIX=$2; shift 2; ;;
  792.  -m|--pmedia) PMEDIA=$2; shift 2; ;;
  793.  -d| --pdrv) PDRV=$2; shift 2; ;;
  794.  -s|--psubdir) PSUBDIR=$2;
  795.    process_psubdir psubdir
  796.    shift 2; ;;
  797.    --maybe-psubdir) PSUBDIR=$2;
  798.    process_psubdir maybe-psubdir    
  799.    shift 2; ;;    
  800.  --distro-specs)
  801.     DISTRO_SPECS=$2;
  802.     [ -f "$DISTRO_SPECS" ] && . "$DISTRO_SPECS"
  803.     shift 2
  804.     ;;
  805.   --boot-config)
  806.       BOOTCONFIG=$2;
  807.     [ -f "$BOOTCONFIG" ] && . "$BOOTCONFIG"
  808.     shift 2
  809.     ;;
  810.   --union-record)  
  811.     LASTUNIONRECORD=$2;
  812.     process_union_record union-record "$LASTUNIONRECORD"
  813.     shift 2; ;;
  814.   -e|--extra-sfs)
  815.     EXTRASFSLIST=$2;
  816.     process_extra_sfs extra-sfs "$EXTRASFSLIST"
  817.     shift 2; ;;
  818.  --aufs)
  819.    items+="$(get_items)"
  820.    shift 1; ;;
  821.  --maybe-aufs)
  822.    [  -z "$items" ] && items+="$(get_items)"
  823.    shift 1; ;;
  824.   -t|--tmpfs)
  825.    if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  826.      SANDBOX_TMPFS="$2"
  827.      set_sandbox_tmpfs
  828.      shift 2
  829.    else
  830.      SANDBOX_TMPFS="" #Later we use [ -z ${SANDBOX_TMPFS+x} ] to check that this is set
  831.      set_sandbox_tmpfs
  832.      shift 1
  833.    fi; ;;  
  834.    -r|--root)
  835.    if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  836.      SANDBOX_ROOT="$2"
  837.      shift 2
  838.    else
  839.      SANDBOX_ROOT="" #Later we use [ -z ${SANDBOX_ROOT+x} ] to check that this is set
  840.      shift 1
  841.    fi; ;;  
  842.   -f|-n|--fake-root|--new-root)
  843.      if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  844.        FAKEROOT="$2"
  845.        set_fakeroot
  846.        shift 2
  847.      else
  848.        FAKEROOT="" #Later we use [ -z ${FAKEROOT+x} ] to check that this is set
  849.        set_fakeroot
  850.        shift 1
  851.      fi; ;;
  852.  --rw-layer) #TOTO unlike
  853.     if [ $PUPMODE -eq 5 ] || [ $PUPMODE -eq 13 ] || [ $PUPMODE -eq 77 ]; then
  854.        tmpfsbranch="$2"
  855.        if [ -d "$2" ]; then
  856.          SANDBOX_TMPFS="$2"          
  857.          [ -z savebranch ] && savebranch="$SANDBOX_TMPFS"
  858.        else
  859.           tmpfsbranch="$2"
  860.           set_sandbox_tmpfs #TODO: verify this does what we want.
  861.        fi
  862.     else
  863.        if [ -d "$2" ]; then
  864.        SANDBOX_IMG=$2
  865.        savebranch=$2
  866.      else [ -f "$2" ]
  867.        #mnt_sb_immage
  868.        #mount -o loop "$rwbranch" $SANDBOX_IMG;
  869.        savebranch=$1
  870.        loop=$(losetup-FULL -a | grep  "$savebranch"  | sed "s/:.*$//")
  871.        if [ ! -z "$loop" ]; then
  872.          SANDBOX_IMG=$(/proc/mounts | grep $loop | cut -d " " -f2)
  873.        else
  874.          SANDBOX_IMG=""
  875.          set_sandbox_img
  876.        fi    
  877.     fi
  878.    shift 2
  879.    ;;        
  880.  --save|--psave|--rw-layer|-t|--tmpfs)
  881.    #!!!!New code. See the -s|--save option of remount_save.sh https://pastebin.com/0ZeU2Prj
  882.    #PSAVE=$2
  883.    #shift 2
  884.    #;;
  885.      if [ "$1" = "--rw-layer" ]
  886.        if [ $PUPMODE -eq 5 ] || [ $PUPMODE -eq 13 ] || [ $PUPMODE -eq 77 ] || [ "$1" = "-t" ] || [ "$1" = "--tmpfs" ] ; then
  887.          make_set "$PDRV/$PSUBDIR" "$PRRV" "$PSUBDIR"
  888.          for a_path in "${make_set_rtn[@]}"
  889.             if [ -d "$2" ] || [ ! -e "$2" ]; then
  890.               SANDBOX_TMPFS="$2"          
  891.               [ -z savebranch ] && savebranch="$SANDBOX_TMPFS"
  892.               break
  893.             elif [ -f "$2" ]; then
  894.                tmpfsbranch="$2"
  895.                set_sandbox_tmpfs #TODO: verify this does what we want.
  896.                break          
  897.             fi
  898.           done      
  899.        fi
  900.      fi
  901.      
  902.      if [ -d "$2" ]; then
  903.        SANDBOX_IMG=$2
  904.        savebranch=$2
  905.      else [ -f "$2" ]
  906.        #mnt_sb_immage
  907.        #mount -o loop "$rwbranch" $SANDBOX_IMG;
  908.        savebranch=$2
  909.        loop=$(losetup-FULL -a | grep  "$savebranch"  | sed "s/:.*$//")
  910.        if [ ! -z "$loop" ]; then
  911.          SANDBOX_IMG=$(/proc/mounts | grep $loop | cut -d " " -f2)
  912.        else
  913.          SANDBOX_IMG=""
  914.          set_sandbox_img
  915.        fi
  916.        shift 2;
  917.      fi; ;;  
  918.  --pupmode)
  919.    PUPMODE=$2
  920.    shift 2
  921.    ;;
  922.  
  923.  --layer)
  924.    RW_LAYER=$2
  925.    process_layer layer $2
  926.    shift 2
  927.    ;;
  928.  -l|--logfile)
  929.    LOGFILE=$2
  930.    [ -z "$TRACE" ] && TRACE=true
  931.    shift 2
  932.    log init
  933.    ;;  
  934.  -t|--trace)
  935.    TRACE=$2
  936.    if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  937.      TRACE="$2"
  938.      [ -z "$TRACE" ] && TRACE=true
  939.      shift 2
  940.    else
  941.      TRACE=true
  942.      shift 1
  943.    fi
  944.    log init
  945.    ;;
  946.  -a|--copy-Xauth)
  947.    if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  948.      XAUTH=$(realpath "$2")
  949.      log stop
  950.      [ -z "$XAUTH" ] && XAUTH=$(realpath "~/.Xauthority")
  951.      shift 2
  952.    else
  953.      log stop
  954.      XAUTH=$(realpath "~/.Xauthority")
  955.      shift 1
  956.    fi
  957.    echo "XAUTH=$XAUTH"
  958.    log start
  959.    ;;
  960.  -u|--bind-X11-sockets)
  961.    if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  962.      uSocketDir=$(realpath "$2");
  963.      log stop
  964.      [ -z "$uSocketDir" ] && uSocketDir=/tmp/.X11-unix
  965.      shift 2
  966.    else
  967.      log stop
  968.      uSocketDir=/tmp/.X11-unix
  969.      shift 1
  970.    fi
  971.    echo "uSocketDir=$uSocketDir"
  972.    log start
  973.    ;;
  974.  -r|--copy-resolv_conf)
  975.    if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  976.      RESOLV_CONF_PATH=$(realpath "$2")
  977.      log stop
  978.      [ -z "$RESOLV_CONF_PATH" ] && RESOLV_CONF_PATH=/etc/resolv.conf
  979.      shift 2
  980.    else
  981.      log stop
  982.      RESOLV_CONF_PATH=/etc/resolv.conf
  983.      shift 1
  984.    fi
  985.    echo "RESOLV_CONF_PATH=$RESOLV_CONF_PATH"
  986.    log start
  987.    ;;
  988.  --rev-bind) #Bind the fakeroot into a folder (e.g. a samba share)
  989.       unset rev_b_source
  990.      unset rev_b_target
  991.      while true
  992.      do
  993.        if [ $# -lt 1 ]; then
  994.          break
  995.        fi
  996.        case "$1" in
  997.        --)    
  998.  
  999.          if [ ! -z "$rev_b_source" ]; then
  1000.            break
  1001.          else
  1002.          #TODO, add some further checking here. What we want to do is eat the -- at the end of the positional parameters if we are missing a target.
  1003.            shift
  1004.            continue
  1005.          fi
  1006.          ;;
  1007.        --rev-bind)
  1008.            shift
  1009.            continue
  1010.            ;;
  1011.        -*)
  1012.            break
  1013.            ;;
  1014.        esac
  1015.        if [ -z "$rev_b_source" ]; then
  1016.          if [ ! -z "$1" ]; then
  1017.            rev_b_source="$1"; shift
  1018.            continue
  1019.          fi        
  1020.        elif [ -z "$b_target" ]; then
  1021.          if [ ! -z "$1" ]; then
  1022.            rev_b_target="$1"; shift
  1023.            
  1024.            log stop
  1025.            
  1026.            echo "rev_bind_sources+=( \"$rev_b_source\" )"
  1027.            echo "rev_bind_targets+=( \"$rev_b_target\" )"
  1028.            
  1029.            
  1030.            rev_bind_sources+=( "$rev_b_source" )
  1031.            rev_bind_targets+=( "$rev_b_target" )
  1032.            
  1033.            log start
  1034.            
  1035.            unset rev_b_source
  1036.            unset rev_b_target
  1037.            shift
  1038.            continue
  1039.          else
  1040.            shift
  1041.            continue
  1042.          fi      
  1043.        else
  1044.          shift
  1045.          break
  1046.        fi
  1047.      done
  1048. #   fi
  1049.    ;;
  1050.  --before-chroot)
  1051.     BEFORE_CHROOT_CMD="$2";
  1052.     shift 2
  1053.     ;;
  1054.  --bind)
  1055. #    if [ $# -ge 4 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ] && \
  1056. #[[ "$3" = -j ]] && [[ ! "$4" = --* ]] && [ ! -z "$4" ]; then
  1057. #       bind_source=$(realpath "$2")
  1058. #       bind_target=$(realpath "$2")
  1059. #       bind_sources+=( "$bind_source" )
  1060. #       bind_targets+=( "$bind_target" )
  1061. #       shift 4
  1062. #    else
  1063.      unset b_source
  1064.      unset b_target
  1065.      while true
  1066.      do
  1067.        if [ $# -lt 1 ]; then
  1068.          break
  1069.        fi
  1070.        case "$1" in
  1071.        --)    
  1072.  
  1073.          if [ ! -z "$b_source" ] && [ $# -lt 3 ]; then
  1074.            break
  1075.          else
  1076.          #TODO, add some further checking here. What we want to do is eat the -- at the end of the positional parameters if we are missing a target.
  1077.            shift
  1078.            continue
  1079.          fi
  1080.          ;;
  1081.        --bind)
  1082.            shift
  1083.            continue
  1084.            ;;      
  1085.        -*)
  1086.        
  1087.            break
  1088.            ;;
  1089.        esac        
  1090.        if [[ $1 = -* ]]; then
  1091.          if [ ! -z "$b_target" ]; then
  1092.            break
  1093.          else
  1094.          #TODO, add some further checking here. What we want to do is eat the -- at the end of the positional parameters if we are missing a target.
  1095.            shift
  1096.            continue
  1097.          fi
  1098.        fi
  1099.        if [ -z "$b_source" ]; then
  1100.          if [ ! -z "$1" ]; then
  1101.            b_source="$1"; shift
  1102.            continue
  1103.          fi
  1104.        elif [ -z "$b_target" ]; then
  1105.          if [ ! -z "$1" ]; then
  1106.            b_target="$1"; shift
  1107.            log stop
  1108.            
  1109.            echo "bind_sources+=( \"$b_source\" )"
  1110.            echo "bind_targets+=( \"$b_target\" )"            
  1111.            
  1112.            bind_sources+=( "$b_source" )
  1113.            bind_targets+=( "$b_target" )
  1114.            log start
  1115.            
  1116.            unset b_source
  1117.            unset b_target
  1118.            shift
  1119.            continue
  1120.          fi        
  1121.        fi
  1122.        shift
  1123.      done
  1124. #   fi
  1125.    ;;          
  1126.  --)
  1127.    shift 1
  1128.    options2+=( "$@" )
  1129.    break; ;;
  1130.  *)
  1131.     options2+=( "$1" )
  1132.     shift 1; ;;
  1133.  esac
  1134. done
  1135. items="$(echo "$items" | sed -n '/^\s*\(on\)\?\s*$/! p' | sed -n '/^Error: Expected on/! p' | sed -n '/^Use --help on/! p')"
  1136.  
  1137.      log stop
  1138.      read -p "Press enter to continue"
  1139.      log start
  1140.  
  1141. #!!!! Use new code further down to find and mount PDRV
  1142.  
  1143. #!!!!Fake root now set with set_fakeroot() (above) for old code see psandbox_removed.sh
  1144.  
  1145. if [ -z ${SANDBOX_ROOT+x} ] && [ -z ${FAKEROOT+x} ]; then
  1146.  SANDBOX_ROOT="" #Later this will change to SANDBOX_ROOT=/mnt/sb
  1147.  set_fakeroot
  1148.  [ -z $SANDBOX_IMG ] && set_sandbox_img
  1149. fi
  1150.  
  1151. # umount all if we are accidentally killed
  1152. trap 'umountall' 1
  1153.  
  1154. #!!!! Old umountall() function removed. See new function above
  1155.  
  1156.  
  1157. # 0.1 must be root
  1158. if [ $(id -u) -ne 0 ]; then
  1159.  echo "You must be root to use sandbox."
  1160.  exit
  1161. fi
  1162.  
  1163. # 0.2 cannot launch sandbox within sandbox
  1164. if [ "$AUFS_ROOT_ID" != "" ] ; then
  1165.  grep -q $SANDBOX_ROOT /sys/fs/aufs/$AUFS_ROOT_ID/br0 &&
  1166.    echo "Cannot launch sandbox within sandbox." && exit
  1167. fi
  1168.  
  1169. # 0.3 help
  1170. case "$1" in
  1171.  --help|-h)
  1172.  echo "Usage: ${0##*/}"
  1173.  echo "Starts an in-memory (throwaway) sandbox. Type 'exit' to leave."
  1174.  exit
  1175. esac
  1176.  
  1177. # 0.4 if not running from terminal but in Xorg, then launch via terminal
  1178. ! [ -t 0 ] && [ -n "$DISPLAY" ] && exec $XTERM -e "$0" "$@"
  1179. ! [ -t 0 ] && exit
  1180. # 1. get aufs system-id for the root filesystem
  1181. if [ -z "$AUFS_ROOT_ID" ] ; then
  1182.  AUFS_ROOT_ID=$(
  1183.    awk '{ if ($2 == "/" && $3 == "aufs") { match($4,/si=[0-9a-f]*/); print "si_" substr($4,RSTART+3,RLENGTH-3) } }' /proc/mounts
  1184.  )
  1185. fi
  1186.  
  1187.  
  1188. # 3. Ask user to choose the SFS
  1189. #TODO: add quite mode.
  1190. echo "items=$items"
  1191. cat <<EOF
  1192. dialog --separate-output --backtitle "tmpfs sandbox" --title "sandbox config" \
  1193.  --checklist "Choose which SFS you want to use" 0 0 0 $items 2> $TMPFILE
  1194. EOF
  1195.  
  1196. log stop
  1197. dialog --separate-output --backtitle "tmpfs sandbox" --title "sandbox config" \
  1198.  --checklist "Choose which SFS you want to use" 0 0 0 $items 2> $TMPFILE
  1199. chosen="$(cat $TMPFILE)"
  1200. log start
  1201. clear
  1202. if [ -z "$chosen" ]; then
  1203.  echo "Cancelled or no SFS is chosen - exiting."
  1204.  exit 1 #TODO: Maybe instead of exiting we ask the user if they want to continue. For instance, perhaps the user wants an empty chroot directory to work with.
  1205. fi
  1206.  
  1207.  
  1208. # 4. convert chosen SFS to robranches
  1209. robranches=""
  1210. for a in $(cat $TMPFILE) ; do
  1211.    #a="$(echo "$a" | sed 's/,$//')" # | sed 's/^'//' | sed 's/'$//' )"
  1212.    echo "a=$a"
  1213.      log stop
  1214.      read -p "Press enter to continue"
  1215.      log start
  1216.    a="$(echo "$a" | sed 's/"//g')" # | sed 's/^'//' | sed 's/'$//' )"
  1217.   robranches=$robranches:$a=ro
  1218.   #TODO, remove line if layer is off in config file.
  1219.   sed -i "\#^$a # {s/ off / on /}" /tmp/get_items_out #If we have a config file then we may need to strip out some stuff like if the layer is off or on.
  1220.  
  1221. done
  1222. if [ ! -z "$OUTPUT_FILE" ]; then
  1223.   cp "/tmp/get_items_out" "$OUTPUT_FILE"
  1224.   if [ ! "$NO_EXIT" = true ]; then
  1225.     exit 0
  1226.   fi
  1227. fi
  1228. rm $TMPFILE
  1229.  
  1230. #!!!! New Section Get rw image if not specified (unless pfx=ram?), and mount SANDBOX_IMG if not already mounted and nt a directory.
  1231. # 5. get location of rw image
  1232. [ -z ${savebranch+x} ] && choose_save
  1233. mkdir -p $SANDBOX_IMG
  1234. [ ${#SANDBOX_IMG} -gt 1 ] || echo "Pathlenth too short SANDBOX_IMG='$SANDBOX_IMG'"
  1235. if [ ! -z "$savebranch"  ]; then
  1236.   if [ -f "$savebranch" ]; then
  1237.         loop=$(losetup-FULL -a | grep  "$savebranch"  | sed "s/:.*$//" )
  1238.         if [ ! -z "$loop" ]; then
  1239.           SANDBOX_IMG=$(cat /proc/mounts | grep $loop | cut -d " " -f2)
  1240.         else
  1241.           mount -o loop "$savebranch" $SANDBOX_IMG
  1242.         fi
  1243.   fi
  1244. fi
  1245.  
  1246.  
  1247. #!!!! New code. Flag TMPFS for creation, if in suitable pumode.
  1248. #!!!! TMPFS may already be flagged for creation if suitable option is provided.
  1249.   if [ ! -z "$PUPMODE" ]; then
  1250.     if [ $PUPMODE  -ne 5 ] && [ $PUPMODE  -ne 5 ] && [ $PUPMODE  -ne 13 ] && [ $PUPMODE  -ne 77 ]; then
  1251.       if  [ -z ${SANDBOX_TMPFS+x} ]; then
  1252.         SANDBOX_TMPFS="" #Later we use [ -z ${SANDBOX_TMPFS+x} ] to check that this is set
  1253.       fi
  1254.     fi
  1255.   fi
  1256.  
  1257. function real_dirname(){
  1258.     local file_dir_or_mnt_path=$(realpath -m "$1")
  1259.     local r_dir="$(file_dir_or_mnt_path#/mnt/*/)"
  1260.     local r_drive=$(file_dir_or_mnt_path%"/$PSUBDIR_rw")
  1261.       while (true); do
  1262.         r_dir_i="$r_dir"   
  1263.         r_drive_i="$r_drive"
  1264.         if [[ r_drive_ = /dev/loop* ]]; then
  1265.             PDRV_real_i=$(losetup-FULL -a | grep "$(basename PDRV_real)")          
  1266. #!!!! New find PDRV code
  1267. if [ ! -z "$SANDBOX_ROOT" ]; then
  1268.   DEV_SAVE=$SANDBOX_ROOT/dev_save
  1269.   mkdir -p "$DEV_SAVE"
  1270.   if [ -z "$PDRV" ]; then
  1271.     if [[ "$SANDBOX_IMG" = *"/mnt/"* ]]; then
  1272.       PSUBDIR_rw=${SANDBOX_IMG#/mnt/*/}
  1273.       PDRV_rw=${SANDBOX_IMG%"/$PSUBDIR_rw"}
  1274.       PDRV_real=$(cat /proc/mounts | grep $(realpath $PDRV_rw) | cut -d " " -f1)
  1275.           PSUBDIR_i=${PSUBDIR}
  1276.           PDRV_i=${PDRV}
  1277.           PDRV_real_i=${PDRV_real}      
  1278.       while (true); do
  1279.  
  1280.           if [[ PDRV_real_i = /dev/loop* ]]; then
  1281.             PDRV_real_i=$(losetup-FULL -a | grep "$(basename PDRV_real)")
  1282.             PDRV_real_i=${PDRV_real#*(}
  1283.             PDRV_real_i=${PDRV_real%)}
  1284.             PSUBDIR_i=${PDRV_real_i#/mnt/*/}
  1285.             PSUBDIR_rw=$PSUBDIR_i/$PSUBDIR_rw
  1286.             PDRV_real_i=${SANDBOX_IMG%"/$PSUBDIR_i"}
  1287.             PDRV_real_i=$(cat /proc/mounts | grep $(realpath $PDRV_real_i) | cut -d " " -f1)
  1288.           elif [[ PDRV_real_i = /dev/* ]]; then
  1289.             PDRV_real=$(blkid | grep $PDRV_real)
  1290.             break
  1291.           else
  1292.             echo "could not identify PDRV_real"
  1293.             break
  1294.           fi
  1295.          
  1296.       done
  1297.     fi
  1298.   fi
  1299. fi
  1300. function real_dirname_helper_grep_proc_mnts(){
  1301.     local a_path="$(realpath -m "$1")"
  1302.     local a_line
  1303.     local mnt_pt
  1304.     local subdir
  1305.     #local mounts_last="$(cat /proc/mounts)"
  1306.     local R_path="/${a_path#/*/}"
  1307.     local L_path=${a_path#a_p%"$a_path2"}
  1308.     local R_path_last="$R_path"
  1309.     local L_path_last="$L_path"
  1310.     local mounts=${echo "$mounts" | grep "$L_path"}
  1311.     local mounts_last=mounts
  1312.     mounts_new_file=$(mktemp -p /tmp)
  1313.     rm -f "mounts_new_file"
  1314.     touch "mounts_new_file"
  1315.     while ture; do
  1316.       while read a_line; do
  1317.         if [ ${#mounts} -eq 0 ]; then
  1318.           if [ ${#mounts} -ne 0 ]; then #paranoid check
  1319.             while read mount_line; do
  1320.                 dev__mnt_pt=$(echo "$mount_line" | sed -r 's#([[:blank:]]+[^[:blank:]]+){4,4}$##g')
  1321.                 a_dev=${dev__mnt_pt%%" "*}
  1322.                 a_mnt_pt=${dev__mnt_pt#"$a_dev "}
  1323.                 a_mnt_source=$(cat /proc/self/mountinfo | grep "$a_dev" | grep "a_mnt_pt")
  1324.                 [ ${#a_mnt_source} -gt 1 ] && a_mnt_pt=$a_mnt_source$a_mnt_pt
  1325.                 case "$a_dev" in
  1326.                 /dev/loop*)  
  1327.                    file_path=$(losetup-FULL -a | grep "$a_dev")
  1328.                    file_path=${file_path#*(}
  1329.                    file_path=${file_path%)*}
  1330.                    #TODO return result of :
  1331.                    #Call this function again with file_path
  1332.                    #append R_path to the result.
  1333.                    ;;
  1334.                 /dev/*)
  1335.                   #PDRV_UUID="$(echo "$1" | awk -v FIELD_NUM=8 -f "$SB_DB_REC_FIELD_AWK")"                                 
  1336.                   #PDRV_real=$(blkid | grep $a_dev|)
  1337.                    PDRV_real=$(blkid | grep " $a_dev" | tr ' ' '\n' | grep -E '^(UUID=)')
  1338.                    subdir=${a_mnt_pt#/}${R_path} #Return ths fult once we get no more items from mounts.
  1339.                    
  1340.                    ;;
  1341.                esac
  1342.                    a_path=$a_mnt_pt$subdir
  1343.                    R_path="/${a_path#/*/}"
  1344.                    L_path=${a_path#a_p%"$a_path2"}
  1345.     local R_path_last="$R_path"
  1346.     local L_path_last="$L_path"
  1347.     local mounts=${echo "$mounts" | grep "$L_path"}              
  1348.             done < <(echo "$mounts")
  1349.           fi
  1350.         fi
  1351.         a_path2="/${a_path2#/*/}"
  1352.         a_path1=${a_path#a_p%"$a_path2"}
  1353.         mounts=${echo "$mounts" | grep          
  1354.         if [ "$a_line" = EOF ]; then
  1355.           mnt_pt=cut -d " " -f1
  1356.           break
  1357.            
  1358.       done < <(cat /proc/mounts | grep $(realpath $PDRV_real_i); echo "EOF" | )
  1359.     done
  1360. }
  1361. function real_dirname_helper(){
  1362.     local a_path="$(realpath -m "$1")"
  1363.    
  1364.     while true; do
  1365.      
  1366.     done
  1367. }
  1368. #(!!!!Removed Section -- old set set_sandbox_tmpfs code -- Se psandbox_removed.sh)
  1369.  
  1370.  
  1371. [ -z $PDRV ] && PDRV=/mnt/home
  1372. if [ -z  "$SANDBOX_IMG" ] || [ ! -z ${SANDBOX_TMPFS+x} ]; then
  1373.   if [ -z "$SANDBOX_TMPFS" ] ; then
  1374.     #SANDBOX_TMPFS=$SANDBOX_ROOT/sandbox
  1375.     set_sandbox_tmpfs
  1376.    
  1377.     #if grep -q $SANDBOX_TMPFS /proc/mounts; then
  1378.     #  #FAKEROOT=$(mktemp -d -p $SANDBOX_ROOT ${FAKEROOT##*/}.XXXXXXX)
  1379.     #  #SANDBOX_ID=".${FAKEROOT##*.}"
  1380.     #  SANDBOX_TMPFS=$SANDBOX_ROOT/${SANDBOX_ID/}${SANDBOX_ID}
  1381.     #  mkdir -p -f SANDBOX_TMPFS
  1382.     #  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.
  1383.     #  rmdir $FAKEROOT
  1384.     #fi
  1385.   fi
  1386.  
  1387.   if [ ! -z "${SANDBOX_TMPFS}" ] && [ ! -z "$(grep -q ${SANDBOX_TMPFS} /proc/mounts)" ]; then
  1388.     mount -t tmpfs none $SANDBOX_TMPFS;
  1389.   fi
  1390. elif [ ! -z "${SANDBOX_TMPFS}" ]; then
  1391.   if [ ! -z "${SANDBOX_TMPFS}" ] && [ ! -z "$(grep -q ${SANDBOX_TMPFS} /proc/mounts)" ]; then
  1392.     mount -t tmpfs none $SANDBOX_TMPFS;
  1393.   fi
  1394. fi
  1395. if [ -z "$SANDBOX_TMPFS" ]; then
  1396.   SANDBOX_TMPFS=$SANDBOX_IMG
  1397. else
  1398.   if [ $robranches ~= ** ]; then
  1399.     robranches=$SANDBOX_IMG=rr:$robranches
  1400.   fi
  1401. fi
  1402. mkdir -p $FAKEROOT
  1403.  
  1404. #mk_initrd_dir
  1405.  
  1406.  
  1407. ## (!!!Removed Section. See psandbox_removed.sh!!) 6. do the magic - mount the tmpfs first, and then the rest with aufs
  1408.  
  1409.  
  1410.  
  1411.  
  1412.  
  1413. # 5. do the magic - mount the rw image first, and then the rest with aufs
  1414. #if mount -o loop "$rwbranch" $SANDBOX_IMG; then
  1415. if [ ${#FAKEROOT} -gt 1 ] && ! grep -q $FAKEROOT /proc/mounts; then
  1416.  
  1417.  
  1418.   echo "mount -t aufs -o \"br:$SANDBOX_TMPFS=rw$robranches\" aufs ${FAKEROOT}"
  1419.   echo "About to mount FAKEROOT at $FAKEROOT"
  1420.   read -p "Press enter to continue"
  1421.  
  1422.     mount -t aufs -o "br:$SANDBOX_TMPFS=rw$robranches" aufs ${FAKEROOT}
  1423.  
  1424.         # 5. record our new aufs-root-id so tools don't hack real filesystem
  1425.         SANDBOX_AUFS_ID=$(grep $FAKEROOT /proc/mounts | sed 's/.*si=/si_/; s/ .*//') #'
  1426.         sed -i -e '/AUFS_ROOT_ID/ d' $FAKEROOT/etc/BOOTSTATE 2> /dev/null
  1427.         echo AUFS_ROOT_ID=$SANDBOX_AUFS_ID >> $FAKEROOT/etc/BOOTSTATE  
  1428.    
  1429.    
  1430.     # 7. sandbox is ready, now just need to mount other supports - pts, proc, sysfs, usb and tmp    
  1431.    
  1432.     mkdir -p $FAKEROOT/dev $FAKEROOT/sys $FAKEROOT/proc $FAKEROOT/tmp
  1433.     mkdir -p  "$DEV_SAVE/${PSUBDIR}"
  1434.     mount -o bind  "$PDRV/${PSUBDIR}" "$DEV_SAVE/${PSUBDIR}"
  1435.    
  1436.     #mount -o bind  "$DEV_SAVE/${PSUBDIR}" "$FAKEROOT/initrd/mnt/dev_save"
  1437.     mount -o bind  "$DEV_SAVE" "$FAKEROOT/initrd/mnt/dev_save"
  1438.  
  1439.     #Maybe optionally do this based on some input paramater:
  1440.     #Also pull these layers from an array
  1441.     for layer_name in "pup_ro2" "pup_ro3" "pup_ro4" "pup_ro5" "pup_z"; do
  1442.         layer="$(eval 'echo $'$layer_name)"
  1443.       if [ ! -z "$layer" ] ; then
  1444.         mount -o bind  "$layer" "$FAKEROOT/initrd/$layer_name"
  1445.       fi
  1446.     done
  1447.  
  1448.     case "$(uname -a)" in
  1449.     *fatdog*)
  1450.       mkdir -p "$FAKEROOT/aufs"
  1451.       #mount -o bind  "$DEV_SAVE/${PSUBDIR}" "$FAKEROOT/aufs/dev_save"
  1452.       mkdir -p "$FAKEROOT/aufs/dev_save"
  1453.       mount -o bind  /mnt/home "$FAKEROOT/aufs/dev_save"
  1454.       cd $FAKEROOT
  1455.       #mkdir -p mnt/home
  1456.       cd mnt
  1457.       ln -s home ../aufs/dev_save
  1458.       pup_save="$SANDBOX_IMG"
  1459.       mount -o bind  "$pup_save" "$FAKEROOT/aufs/pup_save"
  1460.       base_sfs=/aufs/pup_ro
  1461.       mount -o bind  "$base_sfs" "$FAKEROOT/aufs/pup_ro"
  1462.       if [ "$SANDBOX_TMPFS" != "$SANDBOX_IMG" ]; then
  1463.          pup_rw="$SANDBOX_TMPFS"
  1464.          mount -o bind  "$pup_rw" "$FAKEROOT/aufs/pup_rw"
  1465.       fi
  1466.       ;;    
  1467.     *) #assume this is puppy
  1468.       mk_initrd_dir
  1469.       #mkdir -p "$FAKEROOT/initrd/mnt/dev_save"
  1470.       #mount -o bind  /mnt/home "$FAKEROOT/initrd/mnt/dev_save"
  1471.       #mkdir $FAKEROOT/mnt
  1472.       #cd $FAKEROOT/mnt
  1473.       #ln -s ../initrd/mnt/dev_save
  1474.       ;;
  1475.     esac
  1476.     mkdir -p $FAKEROOT/mnt/home
  1477.     mount -o rbind /dev $FAKEROOT/dev
  1478.     mount -t sysfs none $FAKEROOT/sys
  1479.     mount -t proc none $FAKEROOT/proc
  1480.     if [ PUPMODE = 2 ] || [[ "$(uname -a)" = *fatdog* ]]; then #Full Install #Maybe don't base this on PUPMODE
  1481.       tmp_des=$FAKEROOT/tmp
  1482.       tmp_source=/tmp
  1483.     else
  1484.       #case "$(uname -a)" in
  1485.       #*fadog*)
  1486.       #
  1487.       #*) #assume this is puppy
  1488.         mkdir -p $FAKEROOT/initrd/mnt/tmpfs
  1489.         tmp_des=$FAKEROOT/initrd/mnt/tmpfs
  1490.         tmp_source=/initrd/mnt/tmpfs
  1491.         cd $FAKEROOT
  1492.       rm tmp
  1493.       ln -s initrd/mnt/tmpfs tmp
  1494.     fi
  1495.     mount -o bind $tmp_source $tmp_des
  1496.  
  1497.     cd $FAKEROOT
  1498.     case "$(uname -a)" in
  1499.     *fatdog*)
  1500.       mkdir -p $FAKEROOT/aufs
  1501.       mount -o bind $SANDBOX_TMPFS $FAKEROOT/$SANDBOX_TMPFS
  1502.       ;;
  1503.     *) #assume this is puppy
  1504.       mk_initrd_dir
  1505.       #ln -s initrd/mnt/tmpfs tmp  
  1506.       #mkdir -p $FAKEROOT/$SANDBOX_TMPFS
  1507.       #mount -o bind $SANDBOX_TMPFS $FAKEROOT/$SANDBOX_TMPFS # so we can access it within sandbox
  1508.       ;;
  1509.     esac
  1510.  
  1511. #    #mkdir -p $FAKEROOT/$SANDBOX_TMPFS
  1512. #    mkdir -p $FAKEROOT/$SANDBOX_IMG
  1513. #    mount -o bind $SANDBOX_IMG $FAKEROOT/$SANDBOX_IMG  # so we can access it within sandbox    
  1514.      cp /usr/share/sandbox/* $FAKEROOT/usr/bin 2> /dev/null
  1515.    
  1516.  
  1517.  
  1518.         # 8. optional copy, to enable running sandbox-ed xwin
  1519.         cp /usr/share/sandbox/* $FAKEROOT/usr/bin 2> /dev/null
  1520.        
  1521.         # 9. make sure we identify ourself as in sandbox - and we're good to go!
  1522.         echo -e '\nexport PS1="sandbox'${SANDBOX_ID}'# "' >> $FAKEROOT/etc/shinit #fatdog 600
  1523.         sed -i -e '/^PS1/ s/^.*$/PS1="sandbox'${SANDBOX_ID}'# "/' $FAKEROOT/etc/profile # earlier fatdog
  1524.        
  1525.     if [ -d "$FULL_SAVE_PATH" ]; then #TODO verify that this works with a save file
  1526.       if [ $PUPMODE -eq 13 ] && [ $PUPMODE -eq 77 ]; then
  1527.         #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
  1528.         #and copy_folders()  https://github.com/puppylinux-woof-CE/woof-CE/blob/c483d010a8402c5a1711517c2dce782b3551a0b8/initrd-progs/0initrd/init#L482
  1529.           #https://github.com/puppylinux-woof-CE/woof-CE/blob/c483d010a8402c5a1711517c2dce782b3551a0b8/initrd-progs/0initrd/init#L1091
  1530.           mount -o remount,prepend:"$FULL_SAVE_PATH"=rw,mod:"$SANDBOX_TMPFS"=ro,del:"$SANDBOX_TMPFS" "$FAKEROOT"
  1531.           #mount -o remount,add:1:"$FULL_SAVE_PATH"=ro+wh "$FAKEROOT"
  1532.       fi
  1533.     fi
  1534.        
  1535.        
  1536.         echo "Starting sandbox now."
  1537.         log stop
  1538.         if [ $USE_NS ]; then
  1539.             unshare -f -p --mount-proc=$FAKEROOT/proc chroot $FAKEROOT
  1540.         else
  1541.             chroot $FAKEROOT
  1542.         fi
  1543.         log start
  1544.         # 8. done - clean up everything
  1545.         umountall
  1546.         echo "Leaving sandbox."  
  1547.    
  1548. elif [ "${FAKEROOT:-/}" = "/" ]; then
  1549.   #[ -z "$FAKEROOT" ] &&  $FAKEROOT="/"
  1550.   echo "mount -t aufs -o \"remount,br:$SANDBOX_TMPFS=rw$robranches\" aufs  ${FAKEROOT:-/}"
  1551.   echo "Warning!  about to remount rootfs at $FAKEROOT"
  1552.   read -p "Press enter to continue"
  1553.   trap - 1 #Clear traps
  1554.   trap
  1555.   mount -t aufs -o "br:$SANDBOX_TMPFS=rw$robranches" aufs ${FAKEROOT:-/};
  1556.   mkdir -p /dev /sys /proc /tmp #These probably already exist
  1557.   [[ "`mountpoint /dev`"  != *"is a mountpoint"* ]] && mount -t devtmpfs devtmpfs /dev
  1558.   mkdir -p /initrd/mnt/tmpfs
  1559.   [[ "`mountpoint /initrd/mnt/tmpfs`"  != *"is a mountpoint"* ]] && mount -t tmpfs none /initrd/mnt/tmpfs
  1560.   mkdir -p /initrd/mnt/tmpfs/tmp
  1561.   if [[ "`mountpoint /tmp`"  != *"is a mountpoint"* ]]; then
  1562.     if [[ "`mountpoint /initrd/mnt/tmpfs`"  != *"is a mountpoint"* ]]; then
  1563.       mount -o bind /initrd/mnt/tmpfs/tmp /tmp
  1564.     else
  1565.       mount -t tmpfs none /tmp
  1566.     fi
  1567.   fi
  1568. else
  1569.   echo "not implemented for FAKEROOT=${FAKEROOT:-/}}"
  1570.   exit
  1571. fi
  1572.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement