Advertisement
s243a

sandbox_s243a_debug8.sh

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