Advertisement
s243a

remount_save.sh (draft 4)

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