s243a

remount_save.sh (draft 7)

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