s243a

psandbox.sh (new)

Nov 16th, 2020 (edited)
2,177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 24.67 KB | None | 0 0
  1. #!/bin/bash
  2. #Based on James Budiono 2015 sandbox.sh (version 10) but with many options added
  3. # version 10 - (2015) use pid/mount namespaces if available
  4. #
  5. # 0. directory locations
  6. #. $BOOTSTATE_PATH # AUFS_ROOT_ID
  7. #XTERM="defaultterm"
  8. #
  9. # All options below were added by s243a:
  10. #
  11. # -o, --output-file
  12. #    Just write layer paths to an output file but don't mount the sandbox.
  13. # --no-exit
  14. #   if an output file is specified (i.e. -o or --output-file) layer paths are just written to a file and the program exits unless the no-exit flag is specified.
  15. # f, --input-file
  16. #   read layer paths from a file rather than reading existing layers
  17. # m,--pmedia
  18. #   determines pupmodes. Refer to puppy boot parmaters
  19. # d, --pdrv
  20. #   this is the particiaion where the puppy files are located. The default is /mnt/home
  21. # s, psubdir
  22. #   this is the sub directory where the puppy files are located
  23. # c, --clear-env
  24. #   deletes enviornental variabls
  25. # --env-prefix
  26. #   enviornental variable prefix
  27. # b --boot-config
  28. #   path to boot config (e.g. /etc/rc.d/BOOTCONFIG
  29. # --disto-specs
  30. #   path to distro specs (e.g. /etc/DISTRO_SPECS; e.g. /initrd/distro-specs)
  31. # L, --layer
  32. #   a subgke kater
  33. #  e, --extra-sfs
  34. #   a list of extra sfs files (space seperated)
  35. #  u, --union-record
  36. # --xterm
  37. # --sandbox
  38. # -initrd
  39. # --save
  40. # --noexit
  41. # --psave
  42. # --pupmode
  43.  
  44. #I thought some assoitive arrays might be useful but I'm not using them yet.
  45. #declare -A KEYs_by_MNT_PT
  46. #declare -A KEYs_by_FILE_PATH
  47. #declare -A KEYs_by_trimmed_MNT_PT
  48. #declare -A KEYs_by_trimmed_FILE_PATH
  49. #declare -A MNT_PTs
  50. #declare -A FILE_PATHs
  51. #declare -A ON_status
  52. cd "$(dirname "$0")"
  53. MAX_STR_LEN=50
  54. if [ -f ../local/psandbox/sandbox.awk ]; then
  55.   SANDBOX_AWK="$(realpath ../local/psandbox/sandbox.awk)"
  56. elif [ -f /usr/local/psandbox/sandbox.awk ]; then
  57.  SANDBOX_AWK=/usr/local/psandbox/sandbox.awk
  58. fi
  59. SANDBOX_AWK_DIR="$(dirname $SANDBOX_AWK)"
  60. if [ -f ../local/psandbox/sb_db_rec_field.awk ]; then
  61.   SB_DB_REC_FIELD_AWK="$(realpath ../local/psandbox/sb_db_rec_field.awk)"
  62. elif [ -f /usr/local/psandbox/sb_db_rec_field.awk ]; then
  63.   SB_DB_REC_FIELD_AWK=/usr/local/psandbox/sb_db_rec_field.awk
  64. fi
  65.  
  66. if [ -f ../local/psandbox/sandbox_mnt_fn.sh ]; then
  67.   SANDBOX_MNT_FN="$(realpath ../local/psandbox/sandbox_mnt_fn.sh)"
  68. elif [ -f /usr/local/psandbox/sandbox_mnt_fn.sh ]; then
  69.   SANDBOX_MNT_FN=/usr/local/psandbox/sandbox_mnt_fn.sh
  70. fi
  71.  
  72. . "$SANDBOX_MNT_FN"
  73.  
  74. XTERM=${XTERM:-urxvt}
  75. SANDBOX_ROOT=${SANDBOX_ROOT:-/mnt/sb}
  76.  
  77. declare -a options2
  78. function log(){
  79.   local logfile="${2}"
  80.   local trace="$3"
  81.   #[ -z "$logfile" ] && LOGFILE
  82.   #[ -z "$trace" ] && trace=TRACE
  83.   if [ ! -z "$LOGFILE" ]; then
  84.     case "$1" in
  85.     init)
  86.       [ "$TRACE" = true ] && set -x
  87.       [ ! -z "$LOGFILE" ] && rm "$LOGFILE"
  88.       exec 6>&1           # Link file descriptor #6 with stdout.
  89.       #exec &1> >(tee -a "$LOGFILE")
  90.       #exec &2> >(tee -a "$LOGFILE")
  91.       exec &> >(tee -a "$LOGFILE")
  92.       ;;
  93.     start)
  94.       [ "$TRACE" = true ] && set -x
  95.       #exec &1> >(tee -a "$LOGFILE")
  96.       #exec &2> >(tee -a "$LOGFILE")
  97.       exec &> >(tee -a "$LOGFILE")
  98.       ;;
  99.     stop)
  100.       #https://stackoverflow.com/questions/21106465/restoring-stdout-and-stderr-to-default-value
  101.       [ "$TRACE" = true ] && set +x
  102.       exec 1>&6  
  103.       exec 6>&-      # Restore stdout and close file descriptor #6.
  104.       exec &2> /dev/stderr    
  105.       ;;
  106.     esac
  107.   fi    
  108. }
  109. function find_save(){
  110.   for prefix in '${DISTRO_FILE_PREFIX}save' '.*save'; do
  111.     for dir in "$PDRV/${PSUBDIR}" "PDRV";  do
  112.        
  113.       ONE_SAVE="$(ls $dir -1 | grep -m "${prefix}save")"
  114.       if [ -z "$ONE_SAVE" ]; then
  115.          continue
  116.       else
  117.          SAVE_FILE="$ONE_SAVE"
  118.          FULL_SAVE_PATH="$dir"/ONE_SAVE
  119.          break
  120.       fi
  121.     done
  122.    done
  123.    echo "PSAVE"mount_items
  124. }
  125. function find_bk_folders(){
  126.   for a_PDRV in "$PDRV" sr0 sr1; do #Consider adding /mnt/home here
  127.     for a_psubdir in "${PSUBDIR}" "";  do
  128.       MT_PT_of_Folder="$(mount_fn2 "$PDRV" "${PSUBDIR}")"
  129.       #https://github.com/puppylinux-woof-CE/woof-CE/blob/c483d010a8402c5a1711517c2dce782b3551a0b8/initrd-progs/0initrd/init#L981
  130.       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)"
  131.       [ ! -z "#BKFOLDERS" ] && break  
  132.     done
  133.   done
  134. }
  135. function mount_items(){
  136.   local Moun_Point
  137.   local File_PATH #Might be a directory
  138.   cd "$SANDBOX_AWK_DIR"
  139.   while IFS="" read -r p || [ -n "$p" ]; do #https://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash
  140.      File_PATH="$(echo "$1" | awk -v FIELD_NUM=6 -f "$SB_DB_REC_FIELD_AWK")"
  141.      Mount_Point="$(echo "$1" | awk -v FIELD_NUM=1 -f "$SB_DB_REC_FIELD_AWK")"
  142.      PDRV_MNT="$(echo "$1" | awk -v FIELD_NUM=7 -f "$SB_DB_REC_FIELD_AWK")"
  143.      PDRV_UUID="$(echo "$1" | awk -v FIELD_NUM=8 -f "$SB_DB_REC_FIELD_AWK")"
  144.      
  145.      [ -z "$PDRV_MNT" ] &&
  146.      mount_fn2 "PDRV" "$File_PATH" "$Moun_Point"
  147.   done <"$1"
  148. }
  149.  
  150. function mk_initrd_dir(){
  151.   mkdir -p "$FAKEROOT"/initrd
  152.   if [ -z "$PUPMODE" ] ; then
  153.     if [ -z "$PMEDIA" ]; then
  154.       #if [ "$PUPMODE" = 5 ] ; then
  155.       #  #aufs layers:              RW (top)      RO1             RO2              PUPMODE
  156.       #  #First boot (or pfix=ram): tmpfs                         pup_xxx.sfs      5
  157.       PUPMODE=5 #MAYBE PUPMODE=2 would be better
  158.     elif [ PMEDIA = 'atahd' ] || [ "$PMEDIA" = 'usbhd' ]; then
  159.       find_save
  160.       if [ -f "$FULL_SAVE_PATH" ] || [ -d "$FULL_SAVE_PATH" ]; then
  161.         #aufs layers:               RW (top)      RO1             RO2              PUPMODE
  162.         #Normal running puppy:      pup_save.3fs                  pup_xxx.sfs      12      
  163.         PUPMODE=12
  164.       else
  165.         echo "Invalid SAVE_PATH=$SAVE_PATH does not exist"
  166.         PUMPMODE=2
  167.         #TODO, prompt to either search for save file/folder or alternatively create it.
  168.       fi
  169.     elif [ PMEDIA = 'usbflash' ] || [ pmedia = 'ideflash' ]; then
  170.       find_save
  171.       #aufs layers:                 RW (top)      RO1             RO2              PUPMODE
  172.       #ditto, but flash drive:      tmpfs         pup_save.3fs    pup_xxx.sfs      13
  173.       if [ -f "$SAVE_PATH" ] || [ -d "$SAVE_PATH" ]; then
  174.         #aufs layers:               RW (top)      RO1             RO2              PUPMODE
  175.         #ditto, but flash drive:    tmpfs         pup_save.3fs    pup_xxx.sfs      13
  176.         PUPMODE=13
  177.       else
  178.         echo "Invalid SAVE_PATH=$SAVE_PATH does not exist"
  179.         PUPMODE=5
  180.       fi
  181.     elif [ "$PMEDIA" =  usbcd ] || [ "$PMEDIA" =  idecd ] || [ "$PMEDIA" =  satacd ] ; then
  182.       find_bk_folders
  183.       if [ ! -z "$BKFOLDERS" ]; then
  184.         PUPMODE=77  #MULTI-Session CD
  185.       else #First Boot
  186.         find_save
  187.         if [ -f "$FULL_SAVE_PATH" ] || [ -d "$FULL_SAVE_PATH" ]; then
  188.           PUPMODE=13      
  189.         else
  190.           PUPMODE=5
  191.         fi
  192.       fi
  193.       #aufs layers:            RW (top)      RO1             RO2              PUPMODE
  194.       #Multisession cd/dvd:       tmpfs         folders         pup_xxx.sfs      77
  195.     else #[PUPMODE=2 -> full install
  196.       PUPMODE=2
  197.     fi
  198.     if [ "$PUPMODE" = 2 ]; then #Full install
  199.       echo "Full install has no initrd"
  200.     else
  201.       mkdir -p "$FAKEROOT/initrd"
  202.       cd $FAKEROOT/initrd
  203.       if [ "$PUPMODE" = 12 ]; then # Usually [ PMEDIA = 'atahd' ] || [ "$PMEDIA" = usbhd ]
  204.         ln -s mnt/dev_save/"${SAVE_PATH}" pup_rw
  205.       elif [ "$PUPMODE" = 13 ] || [ "$PUPMODE" = 5 ] || [ "$PUPMODE" = 77 ]; then
  206.         ln -s mnt/tmpfs/pup_rw pup_rw
  207.         if [ "$PUPMODE" = 13 ]; then  # Usually [ PMEDIA = 'usbflash' ] || [ pmedia = 'ideflash' ]
  208.           ln -s "mnt/tmpfs/dev_save/${SAVE_PATH}" pup_ro1
  209.         elif [ "$PUPMODE" = 77 ]; then
  210.           ln -s mnt/tmpfs/pup_ro1/"${SAVE_PATH}" pup_ro1  #Usually [ "$PMEDIA" =  usbcd ] || [ "$PMEDIA" =  idecd ] || [ "$PMEDIA" =  satacd ]
  211.         fi
  212.       fi
  213.     fi
  214.   fi
  215. }
  216. function get_items(){
  217.     local out
  218.     OUTFILE=/tmp/get_items_out
  219.     rm "$OUTFILE"
  220.     cd "$SANDBOX_AWK_DIR"
  221.     out+="$(
  222.  { echo ==mount==; cat /proc/mounts;
  223.    echo ==losetup==; losetup-FULL -a;
  224.    echo ==branches==;
  225.      if [ $# -eq 0 ]; then
  226.        ls -v /sys/fs/aufs/$AUFS_ROOT_ID/br[0-9]* | xargs sed 's/=.*//';
  227.      else
  228.        if [ "$1" = "-f" ]; then
  229.          cat "$2";
  230.        elif [ "$1" = "-s" ]; then
  231.          cat <<<"$2";
  232.        fi;
  233.      fi; } | \
  234.    awk -v PDRV="$PDRV" -v MAX_STR_LEN="$MAX_STR_LEN" -v OUTFILE="$OUTFILE" \
  235. -f "$SANDBOX_AWK"
  236. )"
  237.   echo "$out"
  238. }
  239. function process_psubdir(){
  240.       item_source="$1"
  241.       if [ "$item_source" = "maybe-psubdir" ]; then
  242.          [ ! -z "$items" ] && continue
  243.       fi
  244.       [ -z "$DISTRO_ADRVSFS" ] && DISTRO_ADRVSFS="$(ls -1 "${PDRV}/${PSUBDIR}" | grep -i -m1 'adrv.*\.sfs$')"
  245.       [ -z "$DISTRO_YDRVSFS" ] && DISTRO_YDRVSFS="$(ls -1 "${PDRV}/${PSUBDIR}" | grep -i -m1 'ydrv.*\.sfs$')"  
  246.       [ -z "$DISTRO_ZDRVSFS" ] && DISTRO_ZDRVSFS="$(ls -1 "${PDRV}/${PSUBDIR}" | grep -i -m1 'zdrv.*\.sfs$')"
  247.       [ -z "$DISTRO_FDRVSFS" ] && DISTRO_FDRVSFS="$(ls -1 "${PDRV}/${PSUBDIR}" | grep -i -m1 'fdrv.*\.sfs$')"                        
  248.       [ -z "$DISTRO_PUPPYSFS" ] && DISTRO_PUPPYSFS="$(ls -1 "${PDRV}/${PSUBDIR}" | grep -i -m1 'puppy_.*\.sfs$')"
  249.  
  250.       new_items=""
  251.       for rec in "$DISTRO_ADRVSFS" "$DISTRO_YDRVSFS" "$DISTRO_ZDRVSFS" "$DISTRO_FDRVSFS" "$DISTRO_PUPPYSFS";  do
  252.         #MNT_PATH="${rec}"
  253.         [ -z "$rec" ] && continue
  254.         #[ ! -z "${PSUBDIR}" ] && MNT_PATH=${PSUBDIR}/${MNT_PATH}
  255.         MNT_PATH="${PDRV}/${PSUBDIR}/$rec"
  256.         MNT_PT="$(mount_fn "$MNT_PATH")"
  257.         new_items+="\"${MNT_PT}\" \"$rec\" \"on\""$'\n'
  258.        
  259.       done
  260.       #export new_items="$new_items"
  261.       #echo "$new_items"
  262.       items+="$(get_items -s "$new_items")"$'\n'
  263.      
  264. }
  265. process_union_record(){
  266.        new_items=''
  267.        for rec in $LASTUNIONRECORD; do
  268.         if [ -f "$rec" ]; then
  269.           MNT_PT="$(mount_fm "$rec" )"
  270.           new_items+="\"$MNT_PT\" \"$rec\" \"on\""$'\n'
  271.         elif [ -f "$PDRV/$rec" ]; then
  272.           MNT_PT="$(mount_fm "$PDRV/$rec" )"
  273.           new_items+="\"$MNT_PT\", \"$PDRV/$rec\", \"on\""$'\n'
  274.         fi
  275.       done
  276.       items+="$(get_items -f <<<"$new_items")"$'\n'    
  277. }
  278. process_extra_sfs(){
  279.      EXTRASFSLIST="$2";
  280.      unset new_items
  281.      if [ ! -f "$EXTRASFSLIST" ]; then
  282.        EXTRASFSLIST_tmp=$(realpath "$PDRV/$PSUBDIR/$EXTRASFSLIST")
  283.        if [ -f "$EXTRASFSLIST_tmp" ]; then
  284.          EXTRASFSLIST="$EXTRASFSLIST_tmp"
  285.        fi
  286.      fi
  287.      if [ ! -f "$EXTRASFSLIST" ]; then
  288.        EXTRASFSLIST_tmp=$(realpath "$PDRV/$EXTRASFSLIST")
  289.        if [ -f "$EXTRASFSLIST_tmp" ]; then
  290.          EXTRASFSLIST="$EXTRASFSLIST_tmp"
  291.        fi
  292.      fi
  293.      if [[ "$EXTRASFSLIST" = *.sfs ]]; then
  294.          a_sfs="$EXTRASFSLIST"
  295.          MNT_PT="$(mount_fn "$a_sfs" )"
  296.          new_items+="\"$MNT_PT\" \"$a_sfs\" \"on\""$'\n'
  297.      else
  298.        while read a_sfs; do
  299.          a_sfs=$(echo"$a_sfs") #Trims leading and trailing whitespace
  300.          if [ -f "$a_sfs" ]; then
  301.            a_sfs=$(realpath "$a_sfs")
  302.          else
  303.            a_sfs1="$PDRV/${PSUBDIR}/$a_sfs"
  304.            a_sfs=$(realpath "$a_sfs")
  305.            if [ -f "$a_sfs"]; then
  306.              a_sfs=$(realpath "$a_sfs")
  307.            else        
  308.              a_sfs1="$PDRV/$a_sfs1"
  309.              if [ -f "$a_sfs1" ]; then
  310.                a_sfs=$(realpath "$a_sfs")
  311.              fi            
  312.            fi
  313.          fi
  314.          if [ -f  "$a_sfs" ]; then
  315.            MNT_PT="$(mount_fn "$a_sfs" )"
  316.            new_items+="\"$MNT_PT\" \"$a_sfs\" \"on\""$'\n'        
  317.          fi
  318.        done <"$EXTRASFSLIST"
  319.      fi
  320.      items+="$(get_items -s "$new_items")"$'\n'
  321.      #items+="$(get_items -f <<<"$new_items")"
  322. }
  323. process_layer(){
  324.       item_path="$2"
  325.       if [ -f "$item_path" ]; then
  326.         MNT_PT="$(mount_fm "$item_path" )"
  327.       elif [ -d "$item_path" ]; then  
  328.         MNT_PT="$item_path" #This isn't really a mount poing
  329.       elif [ ! -d  "$item_path" ]; then
  330.         echo "Warning  cannot mount $item_path"
  331.         continue
  332.       fi
  333.       items+="\"$MNT_PT\" \"$item_path\" \"on\""$'\n'  
  334. }
  335. declare -a options="$(getopt -o f:,o:,m:,d:,s:,b:,e:,l:,t::,a::,u::,r:: --long input-file:output-file:,pmedia:,pdrv:,psubdir:,boot-config:,distro-specs:,extra-sfs:,aufs,maybe-aufs,maybe-psubdir:,no-exit::,psave:,pupmode:,logfile:,trace:,rw-layer:,copy-Xauth::,bind-X11-sockets::,copy-resolv_conf::,layer: -- "$@")"
  336. eval set --"$options"
  337. while [ $# -gt 0 ]; do
  338.   echo "processing args: $@"
  339.   case "$1" in
  340.   -f|--input-file)
  341.      INPUT_FILE=$2
  342.     mount_items "$INPUT_FILE"
  343.     items+="$(get_items -f "$INPUT_FILE")"
  344.     shift 2; ;;      
  345.   -o|--output-file) OUTPUT_FILE=$2; shift 2; ;;
  346.   --no-exit)
  347.     if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  348.       NO_EXIT="$2"
  349.       shift 2
  350.     else
  351.       NO_EXIT=true
  352.       shift 1
  353.     fi; ;;
  354.   -p|--env-prefix) ENV_PREFIX=$2; shift 2; ;;
  355.   -m|--pmedia) PMEDIA=$2; shift 2; ;;
  356.   -d| --pdrv) PDRV=$2; shift 2; ;;
  357.   -s|--psubdir) PSUBDIR=$2;
  358.     process_psubdir psubdir
  359.     shift 2; ;;
  360.     --maybe-psubdir) PSUBDIR=$2;
  361.     process_psubdir maybe-psubdir    
  362.     shift 2; ;;    
  363.   --distro-specs)
  364.      DISTRO_SPECS=$2;
  365.      . "$DISTRO_SPECS"
  366.      shift 2
  367.      ;;
  368.    --boot-config)
  369.        DISTRO_SPECS=$2;
  370.      . "$BOOTCONFIG"
  371.      shift 2
  372.      ;;
  373.    --union-record)  
  374.      LASTUNIONRECORD="$2";
  375.      process_union_record union-record "$LASTUNIONRECORD"
  376.      shift 2; ;;
  377.    -e|--extra-sfs)
  378.      EXTRASFSLIST="$2";
  379.      process_extra_sfs extra-sfs "$EXTRASFSLIST"
  380.      shift 2; ;;
  381.   --aufs)
  382.     items+="$(get_items)"
  383.     shift 1; ;;
  384.   --maybe-aufs)
  385.     [  -z "$items" ] && items+="$(get_items)"
  386.     shift 1; ;;
  387.   --psave)
  388.     PSAVE=$2
  389.     shift 2
  390.     ;;
  391.   --pupmode)
  392.     PUPMODE=$2
  393.     shift 2
  394.     ;;
  395.   --rw-layer)
  396.     RW_LAYER=$2
  397.     shift 2
  398.     ;;
  399.   --layer)
  400.     RW_LAYER=$2
  401.     process_layer layer $2
  402.     shift 2
  403.     ;;
  404.   -l|--logfile)
  405.     LOGFILE=$2
  406.     [ -z "$TRACE" ] && TRACE=true
  407.     shift 2
  408.     log init
  409.     ;;  
  410.   -t|--trace)
  411.     TRACE=$2
  412.     if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  413.       TRACE="$2"
  414.       shift 2
  415.     else
  416.       TRACE=true
  417.       shift 1
  418.     fi
  419.     log init
  420.     ;;
  421.   -a|--copy-Xauth)
  422.     if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  423.       XAUTH=$(realpath "$2")
  424.       shift 2
  425.     else
  426.       XAUTH=$(realpath "~/.Xauthority")
  427.       shift 1
  428.     fi
  429.     ;;
  430.   -u|--bind-X11-sockets)
  431.     if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  432.       XAUTH=$(realpath "$2")
  433.       shift 2
  434.     else
  435.       uSocketDir=/tmp/.X11-unix
  436.       shift 1
  437.     fi
  438.     ;;
  439.   -r|--copy-resolv_conf)
  440.     if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  441.       RESOLV_CONF_PATH=$(realpath "$2")
  442.       shift 2
  443.     else
  444.       RESOLV_CONF_PATH=/etc/resolv.conf
  445.       shift 1
  446.     fi
  447.     ;;                    
  448.   --)
  449.     shift 1
  450.     options2+=( "$@" )
  451.     break; ;;
  452.   *)
  453.      options2+=( "$1" )
  454.      shift 1; ;;
  455.   esac
  456. done
  457. items="$(echo "$items" | sed -n '/^\s*\(on\)\?\s*$/! p' | sed -n '/^Error: Expected on/! p' | sed -n '/^Use --help on/! p')"
  458. #set -- "${options2[@]}"
  459. #if [ "$LAYER_SOURCE" = none ] && [ ! -z "$PDRV" ]; then
  460. #  PDRV=${PDRV:-/mnt/home}
  461. #  for rec in $LASTUNIONRECORD; do
  462. #    if [ -f "$PDRV/$rec" ]; then
  463. #      items+="\"$PDRV/$rec\" \"$rec\""$'\n'
  464. #    fi
  465. #  done
  466. #  if [ ! -z "$$PDRV" ]; then #if [ -z "$items" ]; then
  467. #    [ -z "$DISTRO_ADRVSFS" ] && DISTRO_ADRVSFS=$(ls -1 $PDRV | grep -i -m1 adrv.*\.sfs$)  
  468. #    [ -z "$DISTRO_YDRVSFS" ] && DISTRO_YDRVSFS=$(ls -1 $PDRV | grep -i -m1 ydrv.*\.sfs$)      
  469. #    [ -z "$DISTRO_ZDRVSFS" ] && DISTRO_ZDRVSFS=$(ls -1 $PDRV | grep -i -m1 zdrv.*\.sfs$)
  470. #    [ -z "$DISTRO_FDRVSFS" ] && DISTRO_FDRVSFS=$(ls -1 $PDRV | grep -i -m1 fdrv.*\.sfs$)        
  471. #    [ -z "$DISTRO_PUPPYSFS" ] && DISTRO_PUPPYSFS=$(ls -1 $PDRV | grep -i -m1 puppy_.*\.sfs$)
  472. #
  473. #    for rec in "$DISTRO_ADRVSFS" "$DISTRO_YDRVSFS" "$DISTRO_ZDRVSFS" "$DISTRO_FDRVSFS" "$DISTRO_PUPPYSFS"; do
  474. #      [ -z "$rec" ] && continue
  475. #      items+="$PDRV/$rec" "$rec"$'\n'  
  476. #    done
  477. #  fi
  478. #  #if [ ! -z "$items" ]; then  
  479. #  #  for rec in $EXTRASFSLIST; do
  480. #  #    if [ -f "$rec" ]; then
  481. #  #      items+="\"$PDRV/$rec\" \"$rec\" "on"\""$'\n'
  482. #  #    elif [ -f "$PDRV/$rec" ]; then
  483. #  #      items+="\"$PDRV/$rec\" \"$rec\" "on"\""$'\n'
  484. #  #    fi
  485. #  #  done
  486. #  #fi
  487. #fi
  488. #if [ -z "$items" ] && [ "$LAYER_SOURCE" = none ] ; then
  489. #    LAYER_SOURCE=aufs  
  490. #    LAYER_SOURCES+=( aufs )
  491. #fi
  492. #    for rec in "${LAYER_SOURCES[@]}"; do
  493. #      [ -z "$rec" ] && continue
  494. #      if [ -f "$rec" ]; then
  495. #        rec=$(realpath "$rec")
  496. #        rec_name=$(basename $rec);
  497. #        items+="'$rec' '$rec'"$'\n'
  498. #      elif [ -f "$PDRV/$rec" ]; then
  499. #        items+="'$PDRV/$rec' '$rec'"$'\n'
  500. #      fi
  501. #    done
  502. #[ -z "$PDRV" ] && PDRV="/mnt/home"
  503.  
  504. if [ "$(cat /proc/mounts | grep -c "$(realpath "$PDRV")")" = 0 ]; then
  505.  PDRV_DEV="$(blkid | grep -m1 "$PDRV" | cut -d ':' -f1)"
  506.  PDRV="$(echo "$PDRV_DEV" | sed 's#^/dev/#/mnt/#')"
  507.  mount "$PDRV_DEV" "$PDRVV"
  508. fi  
  509.  
  510.  
  511. FAKEROOT=$SANDBOX_ROOT/fakeroot   # mounted chroot location of sandbox - ie, the fake root
  512. [ -z "$RW_LAYER" ] && SANDBOX_TMPFS=$SANDBOX_ROOT/sandbox # mounted rw location of tmpfs used for sandbox
  513. DEV_SAVE=$SANDBOX_ROOT/dev_save
  514. mkdir -p "$DEV_SAVE"
  515.  
  516. SANDBOX_ID=
  517. TMPFILE=$(mktemp -p /tmp)
  518. # use namespaces if available
  519. #[ -e /proc/1/ns/pid ] && [ -e /proc/1/ns/mnt ] && type unshare >/dev/null && USE_NS=1
  520.  
  521.  
  522.  
  523.  
  524. # umount all if we are accidentally killed
  525. trap 'umountall' 1
  526. umountall() {
  527.  {
  528.  umount -l $FAKEROOT/$SANDBOX_TMPFS
  529.  if [ PUPMODE = 2 ]; then #Full Install
  530.      umount -l $FAKEROOT/tmp
  531.    else
  532.      umount -l $FAKEROOT/initrd/mnt/tmpfs
  533.    fi
  534.  for layer_name in "pup_ro2" "pup_ro3" "pup_ro4" "pup_ro5" "pup_z"; do
  535.    layer="$(eval 'echo $'$layer_name)"
  536.    if [ ! -z "$layer" ] ; then
  537.      umount -l "$FAKEROOT/initrd/$layer_name"
  538.    fi
  539.  done    
  540.  umount -l $FAKEROOT/proc
  541.  umount -l $FAKEROOT/sys
  542.  umount -l $FAKEROOT/dev
  543.  
  544.  umount -l $FAKEROOT
  545.  [ -z "$RW_LAYER" ] && umount -l $SANDBOX_TMPFS
  546.  rmdir $FAKEROOT
  547.  #if  [ PUPMODE = 2 ] || PUPMODE = 5 ]; then
  548.    [ -z "$RW_LAYER" ] && rmdir $SANDBOX_TMPFS
  549.  #fi
  550.  } 2> /dev/null
  551. }
  552.  
  553. # 0.1 must be root
  554. if [ $(id -u) -ne 0 ]; then
  555.  echo "You must be root to use sandbox."
  556.  exit
  557. fi
  558.  
  559. # 0.2 cannot launch sandbox within sandbox
  560. if [ "$AUFS_ROOT_ID" != "" ] ; then
  561.  grep -q $SANDBOX_ROOT /sys/fs/aufs/$AUFS_ROOT_ID/br0 &&
  562.    echo "Cannot launch sandbox within sandbox." && exit
  563. fi
  564.  
  565. # 0.3 help
  566. case "$1" in
  567.  --help|-h)
  568.  echo "Usage: ${0##*/}"
  569.  echo "Starts an in-memory (throwaway) sandbox. Type 'exit' to leave."
  570.  exit
  571. esac
  572.  
  573. # 0.4 if not running from terminal but in Xorg, then launch via terminal
  574. ! [ -t 0 ] && [ -n "$DISPLAY" ] && exec $XTERM -e "$0" "$@"
  575. ! [ -t 0 ] && exit
  576. # 1. get aufs system-id for the root filesystem
  577. if [ -z "$AUFS_ROOT_ID" ] ; then
  578.  AUFS_ROOT_ID=$(
  579.    awk '{ if ($2 == "/" && $3 == "aufs") { match($4,/si=[0-9a-f]*/); print "si_" substr($4,RSTART+3,RLENGTH-3) } }' /proc/mounts
  580.  )
  581. fi
  582.  
  583.  
  584. # 3. Ask user to choose the SFS
  585. echo "items=$items"
  586. cat <<EOF
  587. dialog --separate-output --backtitle "tmpfs sandbox" --title "sandbox config" \
  588.  --checklist "Choose which SFS you want to use" 0 0 0 $items 2> $TMPFILE
  589. EOF
  590.  
  591. log stop
  592. dialog --separate-output --backtitle "tmpfs sandbox" --title "sandbox config" \
  593.  --checklist "Choose which SFS you want to use" 0 0 0 $items 2> $TMPFILE
  594. chosen="$(cat $TMPFILE)"
  595. log start
  596. clear
  597. if [ -z "$chosen" ]; then
  598.  echo "Cancelled or no SFS is chosen - exiting."
  599.  exit 1
  600. fi
  601.  
  602.  
  603. # 4. convert chosen SFS to robranches
  604. robranches=""
  605. for a in $(cat $TMPFILE) ; do
  606.    #a="$(echo "$a" | sed 's/,$//')" # | sed 's/^'//' | sed 's/'$//' )"
  607.     a="$(echo "$a" | sed 's/"//g')" # | sed 's/^'//' | sed 's/'$//' )"
  608.   robranches=$robranches:$a=ro
  609.   sed -i "\#^$a # {s/ off / on /}" /tmp/get_items_out
  610. done
  611. if [ ! -z "$OUTPUT_FILE" ]; then
  612.   cp "/tmp/get_items_out" "$OUTPUT_FILE"
  613.   if [ ! "$NO_EXIT" = true ]; then
  614.     exit 0
  615.   fi
  616. fi
  617. rm $TMPFILE
  618.  
  619. #if [ PUPMODE = 2 ] || PUPMODE = 5 ]; then
  620.   # 0.5 is this the first sandbox? If not, then create another name for mountpoints
  621.   if grep -q $FAKEROOT /proc/mounts && [ -z "$RW_LAYER" ]; then
  622.   FAKEROOT=$(mktemp -d -p $SANDBOX_ROOT ${FAKEROOT##*/}.XXXXXXX)
  623.   SANDBOX_ID=".${FAKEROOT##*.}"
  624.   SANDBOX_TMPFS=$SANDBOX_ROOT/${SANDBOX_TMPFS##*/}${SANDBOX_ID}
  625.   rmdir $FAKEROOT
  626.   fi
  627.   # 5. make the mountpoints if not exist  yet
  628.   [ -z "$RW_LAYER" ] && mkdir -p $FAKEROOT $SANDBOX_TMPFS
  629. #else
  630. #  SANDBOX_TMPFS="$SAVE_MP_FULL_PATH"
  631. #fi
  632.  
  633.  
  634.  
  635. mk_initrd_dir
  636.  
  637.  
  638. # 6. do the magic - mount the tmpfs first, and then the rest with aufs
  639. if mount -t tmpfs none $SANDBOX_TMPFS || [ ! -z "$RW_LAYER" ]; then
  640.   if [ -z "$RW_LAYER" ]; then
  641.     TOP_LAYER="$SANDBOX_TMPFS"
  642.   else
  643.     mkdir -p "$RW_LAYER"
  644.     #TODO maybe check if the RW layer is a file and if so mount it first.
  645.     TOP_LAYER="$RW_LAYER"
  646.   fi
  647.   if mount -t aufs -o "udba=reval,diropq=w,br:$TOP_LAYER=rw$robranches" aufs $FAKEROOT; then
  648.     # 5. record our new aufs-root-id so tools don't hack real filesystem  
  649.     SANDBOX_AUFS_ID=$(grep $FAKEROOT /proc/mounts | sed 's/.*si=/si_/; s/ .*//') #'
  650.     sed -i -e '/AUFS_ROOT_ID/ d' $FAKEROOT/etc/BOOTSTATE 2> /dev/null
  651.     echo AUFS_ROOT_ID=$SANDBOX_AUFS_ID >> $FAKEROOT/etc/BOOTSTATE
  652.    
  653.     # 7. sandbox is ready, now just need to mount other supports - pts, proc, sysfs, usb and tmp
  654.     mkdir -p $FAKEROOT/dev $FAKEROOT/sys $FAKEROOT/proc $FAKEROOT/tmp
  655.     mkdir -p  "$DEV_SAVE/${PSUBDIR}"
  656.     mount -o bind  "PDRV/${PSUBDIR}" "$DEV_SAVE/${PSUBDIR}" #TODO: ONLY do this if we aren't going to mount all of mnt/dev_save
  657.     mount -o bind  "$DEV_SAVE/${PSUBDIR}" "$FAKEROOT/initrd/mnt/dev_save"
  658.     #Maybe optionally do this based on some input paramater:
  659.     #Also pull these layers from an array
  660.     for layer_name in "pup_ro2" "pup_ro3" "pup_ro4" "pup_ro5" "pup_z"; do
  661.         layer="$(eval 'echo $'$layer_name)"
  662.       if [ ! -z "$layer" ] ; then
  663.         mount -o bind  "$layer" "$FAKEROOT/initrd/$layer_name"
  664.       fi
  665.     done
  666.     mount -o rbind /dev $FAKEROOT/dev
  667.     mount -t sysfs none $FAKEROOT/sys
  668.     mount -t proc none $FAKEROOT/proc
  669.     if [ PUPMODE = 2 ]; then #Full Install
  670.       tmp_des=$FAKEROOT/tmp
  671.       tmp_source=/tmp
  672.     else
  673.         mkdir -p $FAKEROOT/initrd/mnt/tmpfs
  674.       tmp_des=$FAKEROOT/initrd/mnt/tmpfs
  675.       tmp_source=/initrd/mnt/tmpfs
  676.       cd $FAKEROOT
  677.       rm tmp
  678.       ln -s initrd/mnt/tmpfs tmp
  679.     fi
  680.     mount -o bind $tmp_source $tmp_des
  681.     mkdir -p $FAKEROOT/$SANDBOX_TMPFS
  682.     mount -o bind $SANDBOX_TMPFS $FAKEROOT/$SANDBOX_TMPFS # so we can access it within sandbox
  683.    
  684.     # 8. optional copy, to enable running sandbox-ed xwin
  685.     cp /usr/share/sandbox/* $FAKEROOT/usr/bin 2> /dev/null
  686.    
  687.     # 9. make sure we identify ourself as in sandbox - and we're good to go!
  688.     echo -e '\nexport PS1="sandbox'${SANDBOX_ID}'# "' >> $FAKEROOT/etc/shinit #fatdog 600
  689.     sed -i -e '/^PS1/ s/^.*$/PS1="sandbox'${SANDBOX_ID}'# "/' $FAKEROOT/etc/profile # earlier fatdog
  690.    
  691.     if [ -d "$FULL_SAVE_PATH" ]; then #TODO verify that this works with a save file
  692.       if [ $PUPMODE -eq 13 ] && [ $PUPMODE -eq 77 ]; then
  693.         #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
  694.         #and copy_folders()  https://github.com/puppylinux-woof-CE/woof-CE/blob/c483d010a8402c5a1711517c2dce782b3551a0b8/initrd-progs/0initrd/init#L482
  695.           #https://github.com/puppylinux-woof-CE/woof-CE/blob/c483d010a8402c5a1711517c2dce782b3551a0b8/initrd-progs/0initrd/init#L1091
  696.           mount -o remount,prepend:"$FULL_SAVE_PATH"=rw,mod:"$SANDBOX_TMPFS"=ro,del:"$SANDBOX_TMPFS" "$FAKEROOT"
  697.           #mount -o remount,add:1:"$FULL_SAVE_PATH"=ro+wh "$FAKEROOT"
  698.       fi
  699.     fi
  700.     if [ ! -z "$XAUTH" ]; then
  701.       cp "$XAUTH" "$FAKEROOT/$XAUTH"
  702.     fi
  703.     if [ ! -z "$uSocketDir" ]; then
  704.       mkdir -p "$FAKEROOT$uSocketDir"
  705.       mount --bind "$uSocketDir" "$FAKEROOT$uSocketDir"    
  706.     fi  
  707.     if [ ! -z "$RESOLV_CONF_PATH" ]; then
  708.       cp "$RESOLV_CONF_PATH" "$FAKEROOT/etc/resolv.conf"
  709.     fi  
  710.        
  711.     echo "Starting sandbox now."
  712.     log stop    
  713.     if [ $USE_NS ]; then
  714.       unshare -f -p --mount-proc=$FAKEROOT/proc chroot $FAKEROOT
  715.     else
  716.       chroot $FAKEROOT
  717.     fi
  718. log start
  719.     # 10. done - clean up everything
  720.     umountall
  721.     echo "Leaving sandbox."
  722.   else
  723.     echo "Unable to mount aufs br:$SANDBOX_TMPFS=rw$robranches"
  724.     umount -l $SANDBOX_TMPFS    
  725.   fi
  726. else
  727.   echo "unable to mount tmpfs."
  728. fi
  729.  
Add Comment
Please, Sign In to add comment