Advertisement
s243a

sandbox_s243a-debug7.sh

Jan 24th, 2020
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 20.28 KB | None | 0 0
  1. #!/bin/bash
  2. # James Budiono 2011, 2013, 2015
  3. # puppy test/compilation sandbox
  4. # this version uses tmpfs instead of an rw image,
  5. # and you can also choose which SFS to use
  6. # run this from terminal.
  7. # version 4 - replace sed with awk - more powerful and more correct, will handle all oddball cases
  8. #             where loop-N and pup_ro-N numbers don't match
  9. # version 5 - add compatibility when running with pup_rw=tmpfs (step 2.a)
  10. # version 6 - (2012) adapted to be more flexible - for Fatdog64 600
  11. # version 7 - (2012) cleanup mounts if if we are killed
  12. # version 8 - (2013) re-launch in terminal if we aren't in terminal
  13. # version 9 - (2013) enable running multiple sandboxes
  14. # version 10 - (2015) use pid/mount namespaces if available
  15.  
  16. # 0. directory locations
  17. #. $BOOTSTATE_PATH # AUFS_ROOT_ID
  18. #XTERM="defaultterm"
  19. # -o, --output-file
  20. #    Just write layer paths to an output file but don't mount the sandbox.
  21. # --no-exit
  22. #   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.
  23. # f, --input-file
  24. #   read layer paths from a file rather than reading existing layers
  25. # m,--pmedia
  26. #   determines pupmodes. Refer to puppy boot parmaters
  27. # d, --pdrv
  28. #   this is the particiaion where the puppy files are located. The default is /mnt/home
  29. # s, psubdir
  30. #   this is the sub directory where the puppy files are located
  31. # c, --clear-env
  32. #   deletes enviornental variabls
  33. # --env-prefix
  34. #   enviornental variable prefix
  35. # b --boot-config
  36. #   path to boot config (e.g. /etc/rc.d/BOOTCONFIG
  37. # --disto-specs
  38. #   path to distro specs (e.g. /etc/DISTRO_SPECS; e.g. /initrd/distro-specs)
  39. # L, --layer
  40. #   a subgke kater
  41. #  e, --extra-sfs
  42. #   a list of extra sfs files (space seperated)
  43. #  u, --union-record
  44. # --xterm
  45. # --sandbox
  46. # -initrd
  47. # --save
  48. # --noexit
  49. # --psave
  50. # --pupmode
  51. declare -A KEYs_by_MNT_PT
  52. declare -A KEYs_by_FILE_PATH
  53. declare -A KEYs_by_trimmed_MNT_PT
  54. declare -A KEYs_by_trimmed_FILE_PATH
  55. declare -A MNT_PTs
  56. declare -A FILE_PATHs
  57. declare -A ON_status
  58. MAX_STR_LEN=50
  59.  
  60.  
  61. XTERM=${XTERM:-urxvt}
  62. SANDBOX_ROOT=${SANDBOX_ROOT:-/mnt/sb}
  63. declare -a options="$(getopt -o f:,o:,m:,d:,s:,b:,e: --long input-file:output-file:,pmedia:,pdrv:,psubdir:,boot-config:,distro-specs:,extra-sfs:,maybe-aufs,maybe-psubdir:,no-exit::,--psave:,--pupmode -- "$@")"
  64. declare -a options2
  65. declare -a LAYER_SOURCES
  66. LAYER_SOURCE=none
  67.  
  68. function mk_initrd_dir(){
  69.     mkdir -p "$FAKEROOT"/initrd
  70.     if [ -z "$PUPMODE" ] ; then
  71.         if [ -z "$PMEDIA" ]; then
  72.         #if [ "$PUPMODE" = 5 ] ; then
  73.         #  #aufs layers:            RW (top)      RO1             RO2              PUPMODE
  74.         #  #First boot (or pfix=ram):  tmpfs                         pup_xxx.sfs      5
  75.           PUPMODE=5 #MAYBE PUPMODE=2 would be better
  76.         elif [ PMEDIA = 'atahd' ] || [ "$PMEDIA" = 'usbhd' ]; then
  77.           find_save
  78.           #aufs layers:            RW (top)      RO1             RO2              PUPMODE
  79.           #Normal running puppy:      pup_save.3fs                  pup_xxx.sfs      12
  80.           if [ -f "$SAVE_PATH" ] || [ -d "$SAVE_PATH" ]; then
  81.             PUPMODE=12
  82.           else
  83.             echo "Invalid SAVE_PATH=$SAVE_PATH does not exist"
  84.             #exit 1
  85.             #Maybe use PUMPMODE=2 Full install as a fallback
  86.             PUMPMODE=2
  87.             #TODO, prompt to either search for save file/folder or alternatively create it.
  88.           fi
  89.         elif [ PMEDIA = 'usbflash' ] || [ pmedia = 'ideflash' ]; then
  90.            find_save
  91.            #aufs layers:            RW (top)      RO1             RO2              PUPMODE
  92.            #ditto, but flash drive:    tmpfs         pup_save.3fs    pup_xxx.sfs      13
  93.           if [ -f "$SAVE_PATH" ] || [ -d "$SAVE_PATH" ]; then
  94.             PUPMODE=13
  95.           else
  96.             echo "Invalid SAVE_PATH=$SAVE_PATH does not exist"
  97.             exit 1
  98.             #TODO, prompt to either search for save file/folder or alternatively create it.
  99.           fi
  100.         elif [ "$PMEDIA" =  usbcd ] || [ "$PMEDIA" =  idecd ] || [ "$PMEDIA" =  satacd ]  
  101.            SAVE_MP="${PDRV}/${PSUBDIR}"  #PROBABLY should call this something else
  102.            [ -z "$FULL_SAVE_MP" ] && FULL_SAVE_MP="${PDRV}/$SAVE_MP"
  103.            BKFOLDERS="$(find $FULL_SAVE_MP -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%^${FULL_SAVE_MP}/%% | sort -r)"
  104.            if [ ! -z "$BKFOLDERS" ]; then
  105.              [ "$PUPMODE" = 77 ]; then #MULTI-Session CD
  106.            else #First Boot
  107.                "$PUPMODE"=5
  108.            fi
  109.           #aufs layers:            RW (top)      RO1             RO2              PUPMODE
  110.           #Multisession cd/dvd:       tmpfs         folders         pup_xxx.sfs      77
  111.         else #[PUPMODE=2 -> full install
  112.           PUPMODE=2
  113.         fi    
  114.     fi
  115.     if [ "$PUPMODE" = 5 ] ; then
  116.        #aufs layers:            RW (top)      RO1             RO2              PUPMODE
  117.       #First boot (or pfix=ram):  tmpfs                         pup_xxx.sfs      5
  118.     elif [ "$PUPMODE" = 12 ] || [ PMEDIA = 'atahd' ] || [ "$PMEDIA" = usbhd ]; then
  119.       #aufs layers:            RW (top)      RO1             RO2              PUPMODE
  120.       #Normal running puppy:      pup_save.3fs                  pup_xxx.sfs      12
  121.        cd $FAKEROOT/initrd
  122.        
  123.        ln -s mnt/tmpfs/dev_save/${PSUBDIR} pup_rw      
  124.     elif [ "$PUPMODE" = 13 ] || [ PMEDIA = 'usbflash' ] || [ pmedia = 'ideflash' ]; then
  125.        #aufs layers:            RW (top)      RO1             RO2              PUPMODE
  126.        #ditto, but flash drive:    tmpfs         pup_save.3fs    pup_xxx.sfs      13
  127.        cd $FAKEROOT/initrd
  128.        ln -s mnt/tmpfs pup_rw
  129.     elif [ "$PUPMODE" = 77 ]; then
  130.       #aufs layers:            RW (top)      RO1             RO2              PUPMODE
  131.       #Multisession cd/dvd:       tmpfs         folders         pup_xxx.sfs      77
  132.     else #[PUPMODE=2 -> full install
  133.    
  134.     fi
  135. }
  136. function find_save(){
  137.     for prefix in '${DISTRO_FILE_PREFIX}save' '.*save'; do
  138.       for dir in "$PDRV/${PSUBDIR}" "PDRV";  do
  139.          
  140.         ONE_SAVE="$(ls $dir -1 | grep -m "${prefix}save")"
  141.         if [ -z "$ONE_SAVE" ]; then
  142.            continue
  143.         else
  144.            SAVE_FILE="$ONE_SAVE"
  145.            FULL_SAVE_PATH="$dir"/ONE_SAVE
  146.            break
  147.         if
  148.       done
  149.    done
  150.    echo "PSAVE"
  151. }
  152. eval set --"$options"
  153. while [ $# -gt 0 ]; do
  154.   case "$1" in
  155.   -f|--input-file) INPUT_FILE=$2;
  156.   -f|--input-file) INPUT_FILE=$2;
  157.     LAYER_SOURCE=INPUT_FILE
  158.     LAYER_SOURCES+=( input-file )
  159.     shift 2; ;;      
  160.   -o|--output-file) OUTPUT_FILE=$2; shift 2; ;;
  161.   --no-exit)
  162.     if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  163.       NO_EXIT="$2"
  164.       shift 2
  165.     else
  166.       NO_EXIT=true
  167.       shift 1
  168.     fi; ;;
  169.   -p|--env-prefix) ENV_PREFIX=$2; shift 2; ;;
  170.   -m|--pmedia) PMEDIA=$2; shift 2; ;;
  171.   -d| --pdrv) PDRV=$2; shift 2; ;;
  172.   -s|--psubdir) PSUBDIR=$2;
  173.     LAYER_SOURCE=psubdir  
  174.     LAYER_SOURCES+=( psubdir )
  175.     shift 2; ;;
  176.     --maybe-psubdir) PSUBDIR=$2;
  177.     LAYER_SOURCE=maybe-psubdir  
  178.     LAYER_SOURCES+=( maybe-psubdir )
  179.     shift 2; ;;    
  180.   --distro-specs)
  181.      DISTRO_SPECS=$2;
  182.      . "$DISTRO_SPECS"
  183.      shift 2
  184.      ;;
  185.    --boot-config)
  186.        DISTRO_SPECS=$2;
  187.      . "$BOOTCONFIG"
  188.      shift 2
  189.      ;;
  190.    --union-record)  
  191.      LASTUNIONRECORD="$2";
  192.      LAYER_SOURCES+=( union-record )
  193.      shift 2; ;;
  194.    -e|--extra-sfs)
  195.      EXTRASFSLIST="$2";
  196.      LAYER_SOURCES+=( extrasfs )
  197.      shift 2; ;;
  198.   --maybe-aufs)
  199.     LAYER_SOURCE=maybe-aufs  
  200.     LAYER_SOURCES+=( maybe-aufs )
  201.     shift 1; ;;
  202.   --psave)
  203.     PSAVE=$2
  204.     shift 2
  205.     ;;
  206.   --pupmode)
  207.     PUPMODE=$2
  208.     shift 2
  209.     ;;
  210.   --)
  211.     shift 1
  212.     options2+=( "$@" )
  213.     break; ;;
  214.   *)
  215.      options2+=( "$1" )
  216.      shift 1; ;;
  217.   esac
  218. done
  219.  
  220. #set -- "${options2[@]}"
  221. if [ "$LAYER_SOURCE" = none ] && [ ! -z "$PDRV" ]; then
  222.   PDRV=${PDRV:-/mnt/home}
  223.   for rec in $LASTUNIONRECORD; do
  224.     if [ -f "$PDRV/$rec" ]; then
  225.       items+="\"$PDRV/$rec\" \"$rec\""$'\n'
  226.     fi
  227.   done
  228.   if [ -z "$items" ]; then
  229.     [ -z "$DISTRO_PUPPYSFS" ] && DISTRO_PUPPYSFS=$(ls -1 $PDRV | grep -m1 puppy_.*\.sfs$)
  230.     [ -z "$DISTRO_ZDRVSFS" ] && DISTRO_ZDRVSFS=$(ls -1 $PDRV | grep -m1 zdrv.*\.sfs$)
  231.     [ -z "$DISTRO_FDRVSFS" ] && DISTRO_FDRVSFS=$(ls -1 $PDRV | grep -m1 fdrv.*\.sfs$)
  232.     [ -z "$DISTRO_ADRVSFS" ] && DISTRO_ADRVSFS=$(ls -1 $PDRV | grep -m1 fdrv.*\.sfs$)    
  233.     [ -z "$DISTRO_YDRVSFS" ] && DISTRO_YDRVSFS=$(ls -1 $PDRV | grep -m1 fdrv.*\.sfs$)    
  234.     for rec in "$DISTRO_PUPPYSFS" "$DISTRO_ZDRVSFS" "$DISTRO_FDRVSFS" "$DISTRO_ADRVSFS" "$DISTRO_YDRVSFS"; do
  235.       items+="$PDRV/$rec" "$rec"$'\n'  
  236.     done
  237.   fi
  238.   if [ ! -z "$items" ]; then  
  239.     for rec in $EXTRASFSLIST; do
  240.       if [ -f "$PDRV/$rec" ]; then
  241.         items+="\"$PDRV/$rec\" \"$rec\" "on"\""$'\n'
  242.       fi
  243.     done
  244.   fi
  245. fi
  246. if [ -z "$items" ] && [ "$LAYER_SOURCE" = none ] ; then
  247.     LAYER_SOURCE=aufs  
  248.     LAYER_SOURCES+=( aufs )
  249. fi
  250. [ -z "$PDRV" ] && PDRV="$(realpath /mnt/home)"
  251. if [ "$(cat /proc/mounts | grep -c "$PDRV")" = 0 ]; then
  252.   PDRV_DEV="$(blkid | grep -m1 "$PDRV" | cut -d ':' -f1)"
  253.   PDRV="$(echo "$PDRV_DEV" | sed 's#^/dev/#/mnt/#')"
  254.   mount "$PDRV_DEV" "$PDRVV"
  255. fi  
  256.  
  257.  
  258. FAKEROOT=$SANDBOX_ROOT/fakeroot   # mounted chroot location of sandbox - ie, the fake root
  259. SANDBOX_TMPFS=$SANDBOX_ROOT/sandbox # mounted rw location of tmpfs used for sandbox
  260. DEV_SAVE=$SANDBOX_ROOT/dev_save
  261. mkdir -p "$DEV_SAVE"
  262.  
  263. SANDBOX_ID=
  264. TMPFILE=$(mktemp -p /tmp)
  265. # use namespaces if available
  266. #[ -e /proc/1/ns/pid ] && [ -e /proc/1/ns/mnt ] && type unshare >/dev/null && USE_NS=1
  267.  
  268.  
  269.  
  270.  
  271. # umount all if we are accidentally killed
  272. trap 'umountall' 1
  273. umountall() {
  274.     {
  275.     umount -l $FAKEROOT/$SANDBOX_TMPFS
  276.     if [ PUPMODE = 2 ]; then #Full Install
  277.       umount -l $FAKEROOT/tmp
  278.     else
  279.       umount -l $FAKEROOT/initrd/mnt/tmpfs
  280.     fi
  281.     for layer_name in "pup_ro2" "pup_ro3" "pup_ro4" "pup_ro5" "pup_z"; do
  282.         layer="$(eval 'echo $'$layer_name)"
  283.       if [ ! -z "$layer" ] ; then
  284.         umount -l "$FAKEROOT/initrd/$layer_name"
  285.       fi
  286.     done   
  287.     umount -l $FAKEROOT/proc
  288.     umount -l $FAKEROOT/sys
  289.     umount -l $FAKEROOT/dev
  290.    
  291.     umount -l $FAKEROOT
  292.     umount -l $SANDBOX_TMPFS
  293.     rmdir $FAKEROOT
  294.     #if  [ PUPMODE = 2 ] || PUPMODE = 5 ]; then
  295.       rmdir $SANDBOX_TMPFS
  296.     #fi
  297.     } 2> /dev/null
  298. }
  299.  
  300. # 0.1 must be root
  301. if [ $(id -u) -ne 0 ]; then
  302.     echo "You must be root to use sandbox."
  303.     exit
  304. fi
  305.  
  306. # 0.2 cannot launch sandbox within sandbox
  307. if [ "$AUFS_ROOT_ID" != "" ] ; then
  308.     grep -q $SANDBOX_ROOT /sys/fs/aufs/$AUFS_ROOT_ID/br0 &&
  309.         echo "Cannot launch sandbox within sandbox." && exit
  310. fi
  311.  
  312. # 0.3 help
  313. case "$1" in
  314.     --help|-h)
  315.     echo "Usage: ${0##*/}"
  316.     echo "Starts an in-memory (throwaway) sandbox. Type 'exit' to leave."
  317.     exit
  318. esac
  319.  
  320. # 0.4 if not running from terminal but in Xorg, then launch via terminal
  321. ! [ -t 0 ] && [ -n "$DISPLAY" ] && exec $XTERM -e "$0" "$@"
  322. ! [ -t 0 ] && exit
  323. # 1. get aufs system-id for the root filesystem
  324. if [ -z "$AUFS_ROOT_ID" ] ; then
  325.     AUFS_ROOT_ID=$(
  326.         awk '{ if ($2 == "/" && $3 == "aufs") { match($4,/si=[0-9a-f]*/); print "si_" substr($4,RSTART+3,RLENGTH-3) } }' /proc/mounts
  327.     )
  328. fi
  329. function get_items(){
  330.     local out
  331.     OUTFILE=/tmp/get_items_out
  332.     rm "$OUTFILE"
  333.    
  334.     out+="$(
  335.  { echo ==mount==; cat /proc/mounts;
  336.    echo ==losetup==; losetup-FULL -a;
  337.    echo ==branches==;
  338.      if [ $# -eq 0 ]; then
  339.        ls -v /sys/fs/aufs/$AUFS_ROOT_ID/br[0-9]* | xargs sed 's/=.*//';
  340.      else
  341.        if [ "$1" = "-f" ]; then
  342.          cat "$2";
  343.        elif [ "$1" = "-s" ]; then
  344.          cat <<<"$2";
  345.        fi;
  346.      fi; } | \
  347.    awk -v PDRV="$PDRV" -v MAX_STR_LEN="$MAX_STR_LEN" -v OUTFILE="$OUTFILE" \
  348. -f /usr/bin/sandbox.awk
  349. )"
  350.   echo "$out"
  351. }
  352.  
  353. function mount_fn(){
  354.     local MNT_PT=''
  355.     FULL_PATH=$(realpath "$1")
  356.     key=$(md5sum < <( echo "$FULL_PATH" ) | cut -f1 -d' ')
  357.     key=${key:0:5}
  358.     FNAME="$(echo "${FULL_PATH}__${key}" | sed 's#/#+#g' | sed 's#\.#+#g')"
  359.    
  360.     if [ ! -z "$2" ]; then
  361.       MNT_PT="$(cat /proc/mounts | grep "$2" | grep -m1 $FNAME | cut -d ' ' -f2 )"              
  362.       [ -z "$MNT_PT" ] && MNT_PT="$(cat /proc/mounts | grep -m1 "$2" | cut -d ' ' -f2 )"
  363.       [ -z "$MNT_PT" ] && MNT_PT="$(cat /proc/mounts | grep -m1 $FNAME | cut -d ' ' -f2 )"            
  364.    else
  365.        MNT_PT="$(cat /proc/mounts | grep -m1 $FNAME | cut -d ' ' -f2)"    
  366.    fi
  367.     if [ -z "${MNT_PT}" ]; then  
  368.       #case "FNAME" in
  369.       #*.iso)
  370.       #   DIR_PATH=/media; ;;
  371.       #*)
  372.         DIR_PATH=/mnt #; ;;
  373.       #esac
  374.       #if [ -z "$2" ]; then
  375.        # MNT_PT="$2"
  376.       #fi
  377.       #[ -z "$MNT_PT" ] && MNT_PT="${DIR_PATH}/$FNAME"
  378.       LN=${#FNAME}
  379.       start="$((LN-MAX_STR_LEN-4))"
  380.       if [ "$start" -lt 0 ]; then
  381.         start=0
  382.       fi
  383.       FNAME=${FNAME:$start}
  384.       MNT_PT="${DIR_PATH}/$FNAME"
  385.      
  386.       mkdir -p "${MNT_PT}"
  387.       mount $FULL_PATH "${MNT_PT}"    
  388.       MNT_PT="$(cat /proc/mounts | grep -m1 $FNAME | cut -d ' ' -f2 )"          
  389.    fi
  390.   echo  "${MNT_PT}"
  391. }
  392. if [ -z "$items" ]; then
  393.  
  394.   for item_source in "${LAYER_SOURCES[@]}"; do
  395.   # 2. get branches, then map branches to mount types or loop devices
  396.     case "$item_source" in
  397.     input-file)
  398.   items+="$(get_items -f "$INPUT_FILE")"; ;;
  399.     union-record)
  400.        new_items=''
  401.        for rec in $LASTUNIONRECORD; do
  402.         if [ -f "$rec" ]; then
  403.           MNT_PT="$(mount_fm "$rec" )"
  404.           new_items+="\"$MNT_PT\" \"$rec\" \"on\""$'\n'
  405.         elif [ -f "$PDRV/$rec" ]; then
  406.           MNT_PT="$(mount_fm "$PDRV/$rec" )"
  407.           new_items+="\"$MNT_PT\", \"$PDRV/$rec\", \"on\""$'\n'
  408.         fi
  409.       done
  410.       items+="$(get_items -f <<<"$new_items")"
  411.       ;;
  412.     extra-sfs)
  413.        new_items=''
  414.        for rec in $EXTRASFSLIST; do
  415.        if [ -f "$rec" ]; then
  416.           MNT_PT="$(mount_fm "$rec" )"
  417.           new_items+="\"$MNT_PT\" \"$rec\" \"on\""$'\n'
  418.         elif [ -f "$PDRV/$rec" ]; then
  419.           MNT_PT="$(mount_fm "$PDRV/$rec" )"
  420.           new_items+="\"$MNT_PT\" \"$PDRV/$rec\" \"on\""$'\n'
  421.         fi
  422.       done
  423.       ;;
  424.     layer=*)
  425.       item_path="$(echo ${litem_source#*=})"
  426.       if [ -f "$item_path" ]; then
  427.         MNT_PT="$(mount_fm "$item_path" )"
  428.       elif [ -d "$item_path" ]; then  
  429.         MNT_PT="$item_path" #This isn't really a mount poing
  430.       elif [ ! -d  "$item_path" ]; then
  431.         echo "Warning  cannot mount $item_path"
  432.         continue
  433.       fi
  434.       items+="\"$MNT_PT\" \"$item_path\" \"on\""$'\n'
  435.       ;;
  436.     psubdir|maybe-psubdir)
  437.       if [ "$item_source" = "maybe-psubdir" ]; then
  438.          [ ! -z "$items" ] && continue
  439.       fi
  440.       [ -z "$DISTRO_PUPPYSFS" ] && DISTRO_PUPPYSFS=$(ls -1 "${PDRV}/${PSUBDIR}" | grep -m1 puppy_.*\.sfs$)
  441.       [ -z "$DISTRO_ZDRVSFS" ] && DISTRO_ZDRVSFS=$(ls -1 "${PDRV}/${PSUBDIR}" | grep -m1 zdrv.*\.sfs$)
  442.       [ -z "$DISTRO_FDRVSFS" ] && DISTRO_FDRVSFS=$(ls -1 "${PDRV}/${PSUBDIR}" | grep -m1 fdrv.*\.sfs$)
  443.       [ -z "$DISTRO_ADRVSFS" ] && DISTRO_ADRVSFS=$(ls -1 "${PDRV}/${PSUBDIR}" | grep -m1 fdrv.*\.sfs$)    
  444.       [ -z "$DISTRO_YDRVSFS" ] && DISTRO_YDRVSFS=$(ls -1 "${PDRV}/${PSUBDIR}" | grep -m1 fdrv.*\.sfs$)    
  445.       new_items=""
  446.       for rec in "$DISTRO_PUPPYSFS" "$DISTRO_ZDRVSFS" "$DISTRO_FDRVSFS" "$DISTRO_ADRVSFS" "$DISTRO_YDRVSFS"; do
  447.         #MNT_PATH="${rec}"
  448.         #[ ! -z "${PSUBDIR}" ] && MNT_PATH=${PSUBDIR}/${MNT_PATH}
  449.         MNT_PATH="${PDRV}/${PSUBDIR}/$rec"
  450.         MNT_PT="$(mount_fn "$MNT_PATH")"
  451.         new_items+="\"${MNT_PT}\" \"$rec\" "'"on"'$'\n'  
  452.       done
  453.       export new_items="$new_items"
  454.       echo "$new_items"
  455.       items+="$(get_items -s "$new_items")"
  456.       ;;      
  457.     aufs)
  458.       items+="$(get_items)" ; ;;  
  459.     maybe-aufs)
  460.        [  -z "$items" ] && items+="$(get_items)"; ;;  
  461.   esac
  462.   items="$(echo "$items" | sed -n '/^\s*\(on\)\?\s*$/! p' | sed -n '/^Error: Expected on/! p' | sed -n '/^Use --help on/! p')"
  463.   done
  464. fi
  465. # 3. Ask user to choose the SFS
  466.  
  467. dialog --separate-output --backtitle "tmpfs sandbox" --title "sandbox config" \
  468.     --checklist "Choose which SFS you want to use" 0 0 0 $items 2> $TMPFILE
  469. chosen="$(cat $TMPFILE)"
  470.  
  471. clear
  472. if [ -z "$chosen" ]; then
  473.     echo "Cancelled or no SFS is chosen - exiting."
  474.     exit 1
  475. fi
  476.  
  477. if [ ! -z "$OUTPUT_FILE" ]; then
  478.   cp "$TMPFILE" "$OUTPUT_FILE"
  479.   if [ ! "$NO_EXIT" = true ]; then
  480.     exit 0
  481.   fi
  482. fi
  483. # 4. convert chosen SFS to robranches
  484. robranches=""
  485. for a in $(cat $TMPFILE) ; do
  486.     #a="$(echo "$a" | sed 's/,$//')" # | sed 's/^'//' | sed 's/'$//' )"
  487.     a="$(echo "$a" | sed 's/"//g')" # | sed 's/^'//' | sed 's/'$//' )"
  488.     robranches=$robranches:$a=ro
  489. done
  490. rm $TMPFILE
  491.  
  492. #if [ PUPMODE = 2 ] || PUPMODE = 5 ]; then
  493.   # 0.5 is this the first sandbox? If not, then create another name for mountpoints
  494.   if grep -q $FAKEROOT /proc/mounts; then
  495.     FAKEROOT=$(mktemp -d -p $SANDBOX_ROOT ${FAKEROOT##*/}.XXXXXXX)
  496.     SANDBOX_ID=".${FAKEROOT##*.}"
  497.     SANDBOX_TMPFS=$SANDBOX_ROOT/${SANDBOX_TMPFS##*/}${SANDBOX_ID}
  498.     rmdir $FAKEROOT
  499.   fi
  500.   # 5. make the mountpoints if not exist  yet
  501.   mkdir -p $FAKEROOT $SANDBOX_TMPFS
  502. #else
  503. #  SANDBOX_TMPFS="$SAVE_MP_FULL_PATH"
  504. #fi
  505.  
  506.  
  507.  
  508. mk_initrd_dir
  509.  
  510.  
  511. # 6. do the magic - mount the tmpfs first, and then the rest with aufs
  512. if mount -t tmpfs none $SANDBOX_TMPFS; then
  513.     if mount -t aufs -o "udba=reval,diropq=w,br:$SANDBOX_TMPFS=rw$robranches" aufs $FAKEROOT; then
  514.         # 5. record our new aufs-root-id so tools don't hack real filesystem   
  515.         SANDBOX_AUFS_ID=$(grep $FAKEROOT /proc/mounts | sed 's/.*si=/si_/; s/ .*//') #'
  516.         sed -i -e '/AUFS_ROOT_ID/ d' $FAKEROOT/etc/BOOTSTATE 2> /dev/null
  517.         echo AUFS_ROOT_ID=$SANDBOX_AUFS_ID >> $FAKEROOT/etc/BOOTSTATE
  518.        
  519.         # 7. sandbox is ready, now just need to mount other supports - pts, proc, sysfs, usb and tmp
  520.         mkdir -p $FAKEROOT/dev $FAKEROOT/sys $FAKEROOT/proc $FAKEROOT/tmp
  521.         mkdir -p  "$DEV_SAVE/${PSUBDIR}"
  522.         mount -o bind  "PDRV/${PSUBDIR}" "$DEV_SAVE/${PSUBDIR}" #TODO: ONLY do this if we aren't going to mount all of mnt/dev_save
  523.         mount -o bind  "$DEV_SAVE/${PSUBDIR}" "$FAKEROOT/initrd/mnt/dev_save"
  524.         #Maybe optionally do this based on some input paramater:
  525.         #Also pull these layers from an array
  526.         for layer_name in "pup_ro2" "pup_ro3" "pup_ro4" "pup_ro5" "pup_z"; do
  527.           layer="$(eval 'echo $'$layer_name)"
  528.           if [ ! -z "$layer" ] ; then
  529.             mount -o bind  "$layer" "$FAKEROOT/initrd/$layer_name"
  530.           fi
  531.         done
  532.         mount -o rbind /dev $FAKEROOT/dev
  533.         mount -t sysfs none $FAKEROOT/sys
  534.         mount -t proc none $FAKEROOT/proc
  535.         if [ PUPMODE = 2 ]; then #Full Install
  536.           tmp_des=$FAKEROOT/tmp
  537.           tmp_source=/tmp
  538.         else
  539.           mkdir -p $FAKEROOT/initrd/mnt/tmpfs
  540.           tmp_des=$FAKEROOT/initrd/mnt/tmpfs
  541.           tmp_source=/initrd/mnt/tmpfs
  542.           cd $FAKEROOT
  543.           rm tmp
  544.           ln -s initrd/mnt/tmpfs tmp
  545.         fi
  546.         mount -o bind $tmp_source $tmp_des
  547.         mkdir -p $FAKEROOT/$SANDBOX_TMPFS
  548.         mount -o bind $SANDBOX_TMPFS $FAKEROOT/$SANDBOX_TMPFS   # so we can access it within sandbox
  549.        
  550.         # 8. optional copy, to enable running sandbox-ed xwin
  551.         cp /usr/share/sandbox/* $FAKEROOT/usr/bin 2> /dev/null
  552.        
  553.         # 9. make sure we identify ourself as in sandbox - and we're good to go!
  554.         echo -e '\nexport PS1="sandbox'${SANDBOX_ID}'# "' >> $FAKEROOT/etc/shinit #fatdog 600
  555.         sed -i -e '/^PS1/ s/^.*$/PS1="sandbox'${SANDBOX_ID}'# "/' $FAKEROOT/etc/profile # earlier fatdog
  556.        
  557.         if [ -d "$FULL_SAVE_PATH" ]; then #TODO verify that this works with a save file
  558.           if [ $PUPMODE -eq 13 ] && [ $PUPMODE -eq 77 ]; then
  559.             #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
  560.             #and copy_folders()  https://github.com/puppylinux-woof-CE/woof-CE/blob/c483d010a8402c5a1711517c2dce782b3551a0b8/initrd-progs/0initrd/init#L482
  561.             #https://github.com/puppylinux-woof-CE/woof-CE/blob/c483d010a8402c5a1711517c2dce782b3551a0b8/initrd-progs/0initrd/init#L1091
  562.             mount -o remount,prepend:"$FULL_SAVE_PATH"=rw,mod:"$SANDBOX_TMPFS"=ro,del:"$SANDBOX_TMPFS" "$FAKEROOT"
  563.             #mount -o remount,add:1:"$FULL_SAVE_PATH"=ro+wh "$FAKEROOT"
  564.         fi
  565.         echo "Starting sandbox now."
  566.         if [ $USE_NS ]; then
  567.             unshare -f -p --mount-proc=$FAKEROOT/proc chroot $FAKEROOT
  568.         else
  569.             chroot $FAKEROOT
  570.         fi
  571.  
  572.         # 10. done - clean up everything
  573.         umountall
  574.         echo "Leaving sandbox."
  575.     else
  576.         echo "Unable to mount aufs br:$SANDBOX_TMPFS=rw$robranches"
  577.         umount -l $SANDBOX_TMPFS       
  578.     fi
  579. else
  580.     echo "unable to mount tmpfs."
  581. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement