Advertisement
s243a

sandbox_s243a_degbug6.sh

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