Advertisement
s243a

psandbox.sh (3)

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