Advertisement
s243a

psandbox.sh

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