Advertisement
s243a

psandbox.sh (new(2))

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