s243a

remount_save.sh (draft 5)

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