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