Advertisement
s243a

sandbox_s243a_debug11.sh

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