s243a

remount_save.sh (draft 6)

Dec 2nd, 2020 (edited)
778
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.               [ -z savebranch ] && choose_save
  424.              
  425.               if grep -q $savebranch /proc/mounts; then
  426.                 SANDBOX_IMG=$FAKEROOT_dir/${SANDBOX_IMG##*/}${SANDBOX_ID}
  427.               fi
  428.             fi
  429.             rmdir $FAKEROOT
  430.         else
  431.             log stop
  432.              echo "Warning chose to remount over existing mount! $FAKEROOT"
  433.              echo "ctrl-c to quote"
  434.             read -p "Press enter to continue"    
  435.             log start      
  436.         fi
  437.       fi
  438.     else
  439.       echo "Warning sandbox root not defined"
  440.       #[ -z "$FAKEROOT" ] && FAKEROOT=/
  441.     fi
  442.     if [ $CLEANUP_SANDBOX_ROOT = yes ]; then
  443.       if [ ${#del_rules_filter} -eq 0 ]; then
  444.          del_rules_filter+=( $SANDBOX_ROOT"/*" )
  445.          del_rules_filter+=( POLICY_DELETE )
  446.       fi
  447.       if [ ${#umount_rules_filter} -eq 0 ]; then
  448.          umount_rules_filter+=( $SANDBOX_ROOT"/*" )
  449.          umount_rules_filter+=( POLICY_UMOUNT )
  450.       fi    
  451.     fi
  452. }
  453. function set_sandbox_img(){
  454.      [ -z ${FAKEROOT_dir+x} ] &&  set_fakeroot
  455.      if [ ! -z ${SANDBOX_IMG+x} ]  && [ -z "${SANDBOX_IMG}" ]; then
  456.       SANDBOX_IMG=sandbox_img
  457.       SANDBOX_IMG=${SANDBOX_IMG}${SANDBOX_ID}
  458.       [ -z "$FAKEROOT_dir" ] && SANDBOX_IMG=$FAKEROOT_dir/$SANDBOX_IMG
  459.      elif [ -z "${FAKEROOT_dir}" ] ; then
  460.       SANDBOX_IMG=$FAKEROOT_dir/$SANDBOX_IMG
  461.      fi
  462. }
  463. function set_sandbox_tmpfs(){
  464.      [ -z ${FAKEROOT_dir+x} ] &&  set_fakeroot
  465.      if [ ! -z ${SANDBOX_TMPFS+x} ]  && [ -z "${SANDBOX_TMPFS}" ]; then
  466.       SANDBOX_TMPFS=sandbox_tmpfs
  467.       SANDBOX_TMPFS=${SANDBOX_TMPFS}${SANDBOX_ID}
  468.       [ -z "$FAKEROOT_dir" ] && SANDBOX_TMPFS=$FAKEROOT_dir/$SANDBOX_TMPFS
  469.     elif [ -z "${FAKEROOT_dir}" ] ; then
  470.       SANDBOX_TMPFS=$FAKEROOT_dir/$SANDBOX_TMPFS
  471.     fi
  472. }
  473. declare -a options="$(getopt -o t::,r::,s:,d:,l: --long input-file:tmpfs::,root::,save:,pupmode::,pmedia:,pdrv:,psubdir:,distro-specs:,logfile:,trace: -- "$@")"
  474. eval set --"$options"
  475. while [ $# -gt 0 ]; do
  476.   case "$1" in
  477.   -t|--tmpfs)
  478.     if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  479.       SANDBOX_TMPFS="$2"
  480.       set_sandbox_tmpfs
  481.       shift 2
  482.     else
  483.       SANDBOX_TMPFS="" #Later we use [ -z ${SANDBOX_TMPFS+x} ] to check that this is set
  484.       set_sandbox_tmpfs
  485.       shift 1
  486.     fi; ;;
  487.    -r|--root)
  488.     if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  489.       SANDBOX_ROOT="$2"
  490.       shift 2
  491.     else
  492.       SANDBOX_ROOT="" #Later we use [ -z ${SANDBOX_ROOT+x} ] to check that this is set
  493.       shift 1
  494.     fi; ;;  
  495.    -f|-n|--fake-root|--new-root)
  496.       if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  497.         FAKEROOT="$2"
  498.         set_fakeroot
  499.         shift 2
  500.       else
  501.         FAKEROOT="" #Later we use [ -z ${FAKEROOT+x} ] to check that this is set
  502.         set_fakeroot
  503.         shift 1
  504.       fi; ;;  
  505.    -s|--save) #$SANDBOX_IMG
  506.     #if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  507.       if [ -d "$2" ]; then
  508.         SANDBOX_IMG=$2
  509.         savebranch=$2
  510.       else [ -f "$2" ]
  511.         #mnt_sb_immage
  512.         #mount -o loop "$rwbranch" $SANDBOX_IMG;
  513.         savebranch=$1
  514.         loop=$(losetup-FULL -a | grep  "$savebranch"  | sed "s/:.*$//")
  515.         if [ ! -z "$loop" ]; then
  516.           SANDBOX_IMG=$(/proc/mounts | grep $loop | cut -d " " -f2)
  517.         else
  518.           SANDBOX_IMG=""
  519.           set_sandbox_img
  520.         fi
  521.         shift 2;
  522.       fi; ;;
  523.     --pupmode)
  524.       if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  525.         PUPMODE=$2
  526.         shift 2
  527.       else
  528.         PUPMODE=2
  529.         shift
  530.       fi
  531.     ;;
  532.   --pmedia) PMEDIA=$2; shift 2; ;;
  533.   -d| --pdrv) PDRV=$2; shift 2; ;;
  534.   --psubdir) PSUBDIR=$2;
  535.     shift 2; ;;
  536.   --distro-specs)
  537.      DISTRO_SPECS=$2;
  538.      . "$DISTRO_SPECS"
  539.      shift 2
  540.      ;;
  541.   -l|--logfile)
  542.     LOGFILE=$2
  543.     [ -z "$TRACE" ] && TRACE=true
  544.     shift 2
  545.     log init
  546.     ;;  
  547.   -t|--trace)
  548.     TRACE=$2
  549.     if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  550.       TRACE="$2"
  551.       shift 2
  552.     else
  553.       TRACE=true
  554.       shift 1
  555.     fi
  556.     log init
  557.     ;;
  558.    --)
  559.     shift 1
  560.     options2+=( "$@" )
  561.     break; ;;
  562.   *)
  563.      options2+=( "$1" )
  564.      shift 1; ;;
  565.    
  566.   esac
  567. done
  568. if [ -z ${SANDBOX_ROOT+x} ] && [ -z ${FAKEROOT+x} ]; then
  569.   SANDBOX_ROOT="" #Later this will change to SANDBOX_ROOT=/mnt/sb
  570.   set_fakeroot
  571.   [ -z $SANDBOX_IMG ] && set_sandbox_img
  572. fi
  573.  
  574.  
  575.  
  576. #SANDBOX_TMPFS=$SANDBOX_ROOT/sandbox # mounted rw location of
  577. #SANDBOX_TMPFS=$SANDBOX_ROOT/initrd/mnt/tmpfs/pup_rw
  578. #tmpfs used for sandbox
  579. #SANDBOX_ID=
  580. TMPFILE=$(mktemp -p /tmp)
  581. # use namespaces if available
  582. #[ -e /proc/1/ns/pid ] && [ -e /proc/1/ns/mnt ] && type unshare >/dev/null && USE_NS=1
  583.  
  584. # umount all if we are accidentally killed
  585. #trap 'umountall' 1
  586. #s243a don't unmount on error
  587.  
  588. # 0.1 must be root
  589. if [ $(id -u) -ne 0 ]; then
  590.     echo "You must be root to use sandbox."
  591.     exit
  592. fi
  593.  
  594. ## 0.2 cannot launch sandbox within sandbox
  595. #if [ "$AUFS_ROOT_ID" != "" ] ; then
  596. #   grep -q $SANDBOX_ROOT /sys/fs/aufs/$AUFS_ROOT_ID/br0 &&
  597. #       echo "Cannot launch sandbox within sandbox." && exit
  598. #fi
  599. # s243a we are remounting everything rather then creating a sandbox.
  600.  
  601. # 0.3 help
  602. case "$1" in
  603.     --help|-h)
  604.     echo "Usage: ${0##*/}"
  605.     echo "Starts an in-memory (throwaway) sandbox. Type 'exit' to leave."
  606.     exit
  607. esac
  608.  
  609. ## 0.4 if not running from terminal but in Xorg, then launch via terminal
  610. #! [ -t 0 ] && [ -n "$DISPLAY" ] && exec $XTERM -e "$0" "$@"
  611. #! [ -t 0 ] && exit
  612.  
  613. ## 0.5 is this the first sandbox? If not, then create another name for mountpoints
  614. #if grep -q $FAKEROOT /proc/mounts; then
  615. #   FAKEROOT=$(mktemp -d -p $SANDBOX_ROOT ${FAKEROOT##*/}.XXXXXXX)
  616. #   SANDBOX_ID=".${FAKEROOT##*.}"
  617. #   SANDBOX_TMPFS=$SANDBOX_ROOT/${SANDBOX_TMPFS##*/}${SANDBOX_ID}
  618. #   rmdir $FAKEROOT
  619. #fi
  620.  
  621. # 1. get aufs system-id for the root filesystem
  622. if [ -z "$AUFS_ROOT_ID" ] ; then
  623.     AUFS_ROOT_ID=$(
  624.         awk '{ if ($2 == "/" && $3 == "aufs") { match($4,/si=[0-9a-f]*/); print "si_" substr($4,RSTART+3,RLENGTH-3) } }' /proc/mounts
  625.     )
  626. fi
  627.  
  628. # 2. get branches, then map branches to mount types or loop devices
  629. items=$(
  630. { echo ==mount==; cat /proc/mounts;
  631.   echo ==losetup==; losetup-FULL -a;
  632.   echo ==branches==; ls -v /sys/fs/aufs/$AUFS_ROOT_ID/br[0-9]* | xargs sed 's/=.*//'; } | \
  633.   awk '
  634.  /==mount==/ { mode=1 }
  635.  /==losetup==/ { mode=2 }
  636.  /==branches==/ { mode=3 }
  637.  {
  638.     if (mode == 1) {
  639.         # get list of mount points, types, and devices - index is $3 (mount points)
  640.         mountdev[$2]=$1
  641.         mounttypes[$2]=$3
  642.     } else if (mode == 2) {
  643.         # get list of loop devices and files - index is $1 (loop devs)
  644.         sub(/:/,"",$1)
  645.         sub(/.*\//,"",$3); sub(/)/,"",$3)
  646.         loopdev[$1]=$3
  647.     } else if (mode == 3) {
  648.         # map mount types to loop files if mount devices is a loop
  649.         for (m in mountdev) {
  650.             if ( loopdev[mountdev[m]] != "" ) mounttypes[m]=loopdev[mountdev[m]]
  651.         }
  652.         # for (m in mountdev) print m " on " mountdev[m] " type " mounttypes[m]
  653.         mode=4
  654.     } else if (mode==4) {
  655.         # print the branches and its mappings
  656.         if ($0 in mounttypes){
  657.           print $0, mounttypes[$0], "on"
  658.         }
  659.         else {
  660.             MNT_PATH=$0
  661.             sub(/^.*[\/]/,"")
  662.             print MNT_PATH, $0, "on"
  663.         }
  664.     }
  665.  }  
  666. '
  667. )
  668. # '
  669.  
  670. # 3. Ask user to choose the SFS
  671. log stop
  672. dialog --separate-output --backtitle "tmpfs sandbox" --title "sandbox config" \
  673.     --checklist "Choose which SFS you want to use" 0 0 0 $items 2> $TMPFILE
  674. chosen="$(cat $TMPFILE)"
  675. log start
  676. clear
  677. if [ -z "$chosen" ]; then
  678.     echo "Cancelled or no SFS is chosen - exiting."
  679.     exit 1
  680. fi
  681.  
  682. # 4. convert chosen SFS to robranches
  683. robranches=""
  684. for a in $(cat $TMPFILE) ; do
  685.     robranches=$robranches:$a=ro
  686. done
  687. rm $TMPFILE
  688.  
  689. # 5. get location of rw image
  690. mkdir -p $SANDBOX_IMG
  691. [ ${#SANDBOX_IMG} -gt 1 ] || echo "Pathlenth too short SANDBOX_IMG='$SANDBOX_IMG'"
  692. if [ ! -z "$savebranch"  ]; then
  693. #The first branch should already be done so comment out.
  694. #  choose_save
  695. #  loop=$(losetup-FULL -a | grep  "$savebranch"  | sed "s/:.*$//" )
  696. #   if [ ! -z "$loop" ]; then
  697.  #         SANDBOX_IMG=$(/proc/mounts | grep $loop | cut -d " " -f2)
  698. #          mount -o loop "$savebranch" $SANDBOX_IMG
  699. #    fi
  700. #else
  701.         loop=$(losetup-FULL -a | grep  "$savebranch"  | sed "s/:.*$//" )
  702.         if [ ! -z "$loop" ]; then
  703.           SANDBOX_IMG=$(/proc/mounts | grep $loop | cut -d " " -f2)
  704.         else
  705.           mount -o loop "$savebranch" $SANDBOX_IMG
  706.         fi
  707. fi
  708. #[ ${#SANDBOX_IMG} -gt 1 ] || echo "Pathlenth too short SANDBOX_IMG='$SANDBOX_IMG'"
  709. if  [ -z "$rwdir" ] ; then
  710.   if [ -z "$PUPMODE" ]; then
  711.     rwdir=$SANDBOX_IMG
  712.   else
  713.     if [ $PUPMODE  -ne 5 ] && [ $PUPMODE  -ne 13 ] && [ $PUPMODE  -ne 77 ]; then
  714.       if  [ -z ${SANDBOX_TMPFS+x} ]; then
  715.         SANDBOX_TMPFS="" #Later we use [ -z ${SANDBOX_TMPFS+x} ] to check that this is set
  716.       fi
  717.     fi
  718.   fi
  719. fi
  720. # # 4. make the mountpoints if not exist  yet
  721. # if [ ! -z "$SANDBOX_IMG" ]; then
  722. # mkdir -p $SANDBOX_IMG
  723. # mount -o loop "$savebranch" $SANDBOX_IMG || {
  724. #     echo "Failed to mount '$savebranch' at '$SANDBOX_IMG'"
  725. #     exit
  726. # }
  727. #fi
  728. if [ ! -z "$SANDBOX_ROOT" ]; then
  729.   DEV_SAVE=$SANDBOX_ROOT/dev_save
  730.   mkdir -p "$DEV_SAVE"
  731.   if [ -z "$PDRV" ]; then
  732.     if [[ "$SANDBOX_IMG" = *"/mnt/"* ]]; then
  733.       PSUBDIR=${SANDBOX_IMG#/mnt/*/}
  734.       PDRV=${SANDBOX_IMG%"/$PSUBDIR"}
  735.       PDRV_real=$(cat /proc/mounts | grep $(realpath $PDRV) | cut -d " " -f1)
  736.           PSUBDIR_i=${PSUBDIR}
  737.           PDRV_i=${PDRV}
  738.           PDRV_real_i=${PDRV_real}      
  739.       while (true); do
  740.  
  741.           if [[ PDRV_real_i = /dev/loop* ]]; then
  742.             PDRV_real_i=$(losetup-FULL -a | grep "$(basename PDRV_real)")
  743.             PDRV_real_i=${PDRV_real#*(}
  744.             PDRV_real_i=${PDRV_real%)}
  745.             PSUBDIR_i=${PDRV_real_i#/mnt/*/}
  746.             PDRV_real=$PSUBDIR_i/$PDRV_real
  747.             PDRV_real_i=${SANDBOX_IMG%"/$PSUBDIR_i"}
  748.             PDRV_real_i=$(cat /proc/mounts | grep $(realpath $PDRV_real_i) | cut -d " " -f1)
  749.           elif [[ PDRV_real_i = /dev/* ]]; then
  750.             PDRV_real=$(blkid | grep $PDRV_real)
  751.             break
  752.           else
  753.             echo "could not identify PDRV_real"
  754.             break
  755.           fi
  756.          
  757.       done
  758.     fi
  759.   fi
  760. fi
  761. #if [ -z $PDRV ]; then
  762. #  case "$(uname -a)"
  763. #fi
  764. [ -z $PDRV ] && PDRV=/mnt/home
  765. if [ -z  "$rwbranch" ] || [ -z ${SANDBOX_TMPFS+x} ]; then
  766.   if [ -z "$SANDBOX_TMPFS" ] ; then
  767.     SANDBOX_TMPFS=$SANDBOX_ROOT/sandbox
  768.     if grep -q $SANDBOX_TMPFS /proc/mounts; then
  769.       #FAKEROOT=$(mktemp -d -p $SANDBOX_ROOT ${FAKEROOT##*/}.XXXXXXX)
  770.       #SANDBOX_ID=".${FAKEROOT##*.}"
  771.       SANDBOX_TMPFS=$SANDBOX_ROOT/${SANDBOX_ID/}${SANDBOX_ID}
  772.       mkdir -p -f SANDBOX_TMPFS
  773.       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.
  774.       rmdir $FAKEROOT
  775.     fi
  776.   fi
  777. fi
  778. if [ -z "$SANDBOX_TMPFS" ]; then
  779.   SANDBOX_TMPFS=$SANDBOX_IMG
  780. else
  781.   if [ $robranches ~= ** ]; then
  782.     robranches=$SANDBOX_IMG=rr:$robranches
  783.   fi
  784. fi
  785. mkdir -p $FAKEROOT
  786.  
  787. #mk_initrd_dir
  788.  
  789. # 5. do the magic - mount the rw image first, and then the rest with aufs
  790. #if mount -o loop "$rwbranch" $SANDBOX_IMG; then
  791. if [ ${#FAKEROOT} -gt 1 ] && ! grep -q $FAKEROOT /proc/mounts; then
  792.  
  793.         # 5. record our new aufs-root-id so tools don't hack real filesystem
  794.         SANDBOX_AUFS_ID=$(grep $FAKEROOT /proc/mounts | sed 's/.*si=/si_/; s/ .*//') #'
  795.         sed -i -e '/AUFS_ROOT_ID/ d' $FAKEROOT/etc/BOOTSTATE 2> /dev/null
  796.         echo AUFS_ROOT_ID=$SANDBOX_AUFS_ID >> $FAKEROOT/etc/BOOTSTATE  
  797.   echo "mount -t aufs -o \"br:$SANDBOX_TMPFS=rw$robranches\" aufs ${FAKEROOT}"
  798.   echo "About to mount FAKEROOT at $FAKEROOT"
  799.   read -p "Press enter to continue"
  800.  
  801.     mount -t aufs -o "br:$SANDBOX_TMPFS=rw$robranches" aufs ${FAKEROOT}
  802.    
  803.    
  804.     # 7. sandbox is ready, now just need to mount other supports - pts, proc, sysfs, usb and tmp   
  805.    
  806.     mkdir -p $FAKEROOT/dev $FAKEROOT/sys $FAKEROOT/proc $FAKEROOT/tmp
  807.     mkdir -p  "$DEV_SAVE/${PSUBDIR}"
  808.     mount -o bind  "$PDRV/${PSUBDIR}" "$DEV_SAVE/${PSUBDIR}"
  809.    
  810.     #mount -o bind  "$DEV_SAVE/${PSUBDIR}" "$FAKEROOT/initrd/mnt/dev_save"
  811.     mount -o bind  "$DEV_SAVE" "$FAKEROOT/initrd/mnt/dev_save"
  812.  
  813.     #Maybe optionally do this based on some input paramater:
  814.     #Also pull these layers from an array
  815.     for layer_name in "pup_ro2" "pup_ro3" "pup_ro4" "pup_ro5" "pup_z"; do
  816.         layer="$(eval 'echo $'$layer_name)"
  817.       if [ ! -z "$layer" ] ; then
  818.         mount -o bind  "$layer" "$FAKEROOT/initrd/$layer_name"
  819.       fi
  820.     done
  821.  
  822.     case "$(uname -a)" in
  823.     *fatdog*)
  824.       mkdir -p "$FAKEROOT/aufs"
  825.       #mount -o bind  "$DEV_SAVE/${PSUBDIR}" "$FAKEROOT/aufs/dev_save"
  826.       mkdir -p "$FAKEROOT/aufs/dev_save"
  827.       mount -o bind  /mnt/home "$FAKEROOT/aufs/dev_save"
  828.       cd $FAKEROOT
  829.       #mkdir -p mnt/home
  830.       cd mnt
  831.       ln -s home ../aufs/dev_save
  832.       pup_save="$SANDBOX_IMG"
  833.       mount -o bind  "$pup_save" "$FAKEROOT/aufs/pup_save"
  834.       base_sfs=/aufs/pup_ro
  835.       mount -o bind  "$base_sfs" "$FAKEROOT/aufs/pup_ro"
  836.       if [ "$SANDBOX_TMPFS" != "$SANDBOX_IMG" ]; then
  837.          pup_rw="$SANDBOX_TMPFS"
  838.          mount -o bind  "$pup_rw" "$FAKEROOT/aufs/pup_rw"
  839.       fi
  840.       ;;   
  841.     *) #assume this is puppy
  842.       mk_initrd_dir
  843.       #mkdir -p "$FAKEROOT/initrd/mnt/dev_save"
  844.       #mount -o bind  /mnt/home "$FAKEROOT/initrd/mnt/dev_save"
  845.       #mkdir $FAKEROOT/mnt
  846.       #cd $FAKEROOT/mnt
  847.       #ln -s ../initrd/mnt/dev_save
  848.       ;;
  849.     esac
  850.     mkdir -p $FAKEROOT/mnt/home
  851.     mount -o rbind /dev $FAKEROOT/dev
  852.     mount -t sysfs none $FAKEROOT/sys
  853.     mount -t proc none $FAKEROOT/proc
  854.     if [ PUPMODE = 2 ] || [[ "$(uname -a)" = *fatdog* ]]; then #Full Install #Maybe don't base this on PUPMODE
  855.       tmp_des=$FAKEROOT/tmp
  856.       tmp_source=/tmp
  857.     else
  858.       #case "$(uname -a)" in
  859.       #*fadog*)
  860.       #
  861.       #*) #assume this is puppy
  862.         mkdir -p $FAKEROOT/initrd/mnt/tmpfs
  863.         tmp_des=$FAKEROOT/initrd/mnt/tmpfs
  864.         tmp_source=/initrd/mnt/tmpfs
  865.         cd $FAKEROOT
  866.       rm tmp
  867.       ln -s initrd/mnt/tmpfs tmp
  868.     fi
  869.     mount -o bind $tmp_source $tmp_des
  870.  
  871.     cd $FAKEROOT
  872.     case "$(uname -a)" in
  873.     *fatdog*)
  874.       mkdir -p $FAKEROOT/aufs
  875.       mount -o bind $SANDBOX_TMPFS $FAKEROOT/$SANDBOX_TMPFS
  876.       ;;
  877.     *) #assume this is puppy
  878.       mk_initrd_dir
  879.       #ln -s initrd/mnt/tmpfs tmp  
  880.       #mkdir -p $FAKEROOT/$SANDBOX_TMPFS
  881.       #mount -o bind $SANDBOX_TMPFS $FAKEROOT/$SANDBOX_TMPFS # so we can access it within sandbox
  882.       ;;
  883.     esac
  884.  
  885. #    #mkdir -p $FAKEROOT/$SANDBOX_TMPFS
  886. #    mkdir -p $FAKEROOT/$SANDBOX_IMG
  887. #    mount -o bind $SANDBOX_IMG $FAKEROOT/$SANDBOX_IMG  # so we can access it within sandbox   
  888.      cp /usr/share/sandbox/* $FAKEROOT/usr/bin 2> /dev/null
  889.    
  890.  
  891.  
  892.         # 8. optional copy, to enable running sandbox-ed xwin
  893.         cp /usr/share/sandbox/* $FAKEROOT/usr/bin 2> /dev/null
  894.        
  895.         # 9. make sure we identify ourself as in sandbox - and we're good to go!
  896.         echo -e '\nexport PS1="sandbox'${SANDBOX_ID}'# "' >> $FAKEROOT/etc/shinit #fatdog 600
  897.         sed -i -e '/^PS1/ s/^.*$/PS1="sandbox'${SANDBOX_ID}'# "/' $FAKEROOT/etc/profile # earlier fatdog
  898.        
  899.     if [ -d "$FULL_SAVE_PATH" ]; then #TODO verify that this works with a save file
  900.       if [ $PUPMODE -eq 13 ] && [ $PUPMODE -eq 77 ]; then
  901.         #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
  902.         #and copy_folders()  https://github.com/puppylinux-woof-CE/woof-CE/blob/c483d010a8402c5a1711517c2dce782b3551a0b8/initrd-progs/0initrd/init#L482
  903.           #https://github.com/puppylinux-woof-CE/woof-CE/blob/c483d010a8402c5a1711517c2dce782b3551a0b8/initrd-progs/0initrd/init#L1091
  904.           mount -o remount,prepend:"$FULL_SAVE_PATH"=rw,mod:"$SANDBOX_TMPFS"=ro,del:"$SANDBOX_TMPFS" "$FAKEROOT"
  905.           #mount -o remount,add:1:"$FULL_SAVE_PATH"=ro+wh "$FAKEROOT"
  906.       fi
  907.     fi
  908.        
  909.        
  910.         echo "Starting sandbox now."
  911.         log stop
  912.         if [ $USE_NS ]; then
  913.             unshare -f -p --mount-proc=$FAKEROOT/proc chroot $FAKEROOT
  914.         else
  915.             chroot $FAKEROOT
  916.         fi
  917.         log start
  918.         # 8. done - clean up everything
  919.         umountall
  920.         echo "Leaving sandbox."  
  921.    
  922. elif [ "${FAKEROOT:-/}" = "/" ]; then
  923.   #[ -z "$FAKEROOT" ] &&  $FAKEROOT="/"
  924.   echo "mount -t aufs -o \"remount,br:$SANDBOX_TMPFS=rw$robranches\" aufs  ${FAKEROOT:-/}"
  925.   echo "Warning!  about to remount rootfs at $FAKEROOT"
  926.   read -p "Press enter to continue"
  927.   trap - 1 #Clear traps
  928.   trap
  929.   mount -t aufs -o "br:$SANDBOX_TMPFS=rw$robranches" aufs ${FAKEROOT:-/};
  930.   mkdir -p /dev /sys /proc /tmp #These probably already exist
  931.   [[ "`mountpoint /dev`"  != *"is a mountpoint"* ]] && mount -t devtmpfs devtmpfs /dev
  932.   mkdir -p /initrd/mnt/tmpfs
  933.   [[ "`mountpoint /initrd/mnt/tmpfs`"  != *"is a mountpoint"* ]] && mount -t tmpfs none /initrd/mnt/tmpfs
  934.   mkdir -p /initrd/mnt/tmpfs/tmp
  935.   if [[ "`mountpoint /tmp`"  != *"is a mountpoint"* ]]; then
  936.     if [[ "`mountpoint /initrd/mnt/tmpfs`"  != *"is a mountpoint"* ]]; then
  937.       mount -o bind /initrd/mnt/tmpfs/tmp /tmp
  938.     else
  939.       mount -t tmpfs none /tmp
  940.     fi
  941.   fi
  942. else
  943.   echo "not implemented for FAKEROOT=${FAKEROOT:-/}}"
  944.   exit
  945. fi
  946.        
  947.  
  948.            
  949.  
  950.        
  951.        
  952.  
  953.     #else
  954.     #   echo "Unable to mount aufs br:$SANDBOX_IMG=rw$robranches"
  955.     #   umount -l $SANDBOX_IMG 
  956.     #fi
  957. #else
  958. #   echo "unable to mount rw image: $rwbranch"
  959. #fi
  960.  
  961.  
RAW Paste Data