Advertisement
s243a

sandbox_s243a_debug10.sh

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