Advertisement
s243a

psandbox.sh (wary #2)

Dec 12th, 2020
1,147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 30.67 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. CWD=$(realpath .)
  53. [ -z "$CWD" ] && CWD="$PWD"
  54. declare -a bind_sources
  55. declare -a bind_targets
  56. declare -a rev_bind_sources
  57. declare -a rev_bind_targets
  58. cd "$(dirname "$0")"
  59. MAX_STR_LEN=50
  60. if [ -f ../local/psandbox/sandbox.awk ]; then
  61.   SANDBOX_AWK="$(realpath ../local/psandbox/sandbox.awk)"
  62. elif [ -f /usr/local/psandbox/sandbox.awk ]; then
  63.  SANDBOX_AWK=/usr/local/psandbox/sandbox.awk
  64. fi
  65. SANDBOX_AWK_DIR="$(dirname $SANDBOX_AWK)"
  66. if [ -f ../local/psandbox/sb_db_rec_field.awk ]; then
  67.   SB_DB_REC_FIELD_AWK="$(realpath ../local/psandbox/sb_db_rec_field.awk)"
  68. elif [ -f /usr/local/psandbox/sb_db_rec_field.awk ]; then
  69.   SB_DB_REC_FIELD_AWK=/usr/local/psandbox/sb_db_rec_field.awk
  70. fi
  71.  
  72. if [ -f ../local/psandbox/sandbox_mnt_fn.sh ]; then
  73.   SANDBOX_MNT_FN="$(realpath ../local/psandbox/sandbox_mnt_fn.sh)"
  74. elif [ -f /usr/local/psandbox/sandbox_mnt_fn.sh ]; then
  75.   SANDBOX_MNT_FN=/usr/local/psandbox/sandbox_mnt_fn.sh
  76. fi
  77.  
  78. . "$SANDBOX_MNT_FN"
  79.  
  80. XTERM=${XTERM:-urxvt}
  81. SANDBOX_ROOT=${SANDBOX_ROOT:-/mnt/sb}
  82.  
  83. declare -a options2
  84. declare -a binds
  85. LOG_INITIALIZED=false
  86.  
  87. RP_FN="`which realpath`"
  88. RP_TARGET=$($RP_FN "$RP_FN")
  89. [ -z "$RP_TARGET" ] && RP_TARGET="`readlink $RP_FN`"
  90.  
  91. function realpath(){
  92.     case "$RP_TARGET" in
  93.     *busybox*)
  94.       if [ "$1" = -m ]; then
  95.         shift
  96.         A_PATH=$1
  97.         A_PATH=$(echo "$A_path" | sed 's#^./#'"$CWD"'#g' )
  98.         A_PATH=$(echo "$A_path" | sed 's#^../#'"$CWD"'#g' ) && A_PATH=$(dirname $A_PATH)
  99.         echo "Warning simulating '-m' option since it isn't supported by busybox" >2
  100.         echo "A_PATH=$A_PATH" >2
  101.         if [ -f  "$A_PATH" ] || [ -d  "$A_PATH" ]; then
  102.           $RP_FN "$@"
  103.         else
  104.           echo "$A_PATH"
  105.         fi
  106.       else
  107.         $RP_FN "$@"
  108.       fi
  109.       ;;
  110.     *)
  111.       $RP_FN "$@"
  112.       ;;
  113.     esac
  114. }
  115. export -f realpath
  116. function log(){
  117.   local logfile="${2}"
  118.   local trace="$3"
  119.   #[ -z "$logfile" ] && LOGFILE
  120.   #[ -z "$trace" ] && trace=TRACE
  121.   if [ ! -z "$LOGFILE" ]; then
  122.     case "$1" in
  123.     init)
  124.       [ "$TRACE" = true ] && set -x
  125.       [ ! -f "$LOGFILE" ] && rm "$LOGFILE"
  126.       exec 6>&1           # Link file descriptor #6 with stdout.
  127.       #exec &1> >(tee -a "$LOGFILE")
  128.       #exec &2> >(tee -a "$LOGFILE")
  129.       [ ! -f "$LOGFILE" ] && touch "$LOGFILE"
  130.       exec &> >(tee -a "$LOGFILE")
  131.       ;;
  132.     start)
  133.       [ "$TRACE" = true ] && set -x
  134.       #exec &1> >(tee -a "$LOGFILE")
  135.       #exec &2> >(tee -a "$LOGFILE")
  136.      
  137.       exec &> >(tee -a "$LOGFILE")
  138.       ;;
  139.     stop)
  140.       #https://stackoverflow.com/questions/21106465/restoring-stdout-and-stderr-to-default-value
  141.       [ "$TRACE" = true ] && set +x
  142.       exec 1>&6  
  143.       #exec 6>&-      # Restore stdout and close file descriptor #6.
  144.       exec &2> /dev/stderr    
  145.       ;;
  146.     esac
  147.   fi    
  148. }
  149. function find_save(){
  150.   for prefix in '${DISTRO_FILE_PREFIX}save' '.*save'; do
  151.     for dir in "$PDRV/${PSUBDIR}" "PDRV";  do
  152.        
  153.       ONE_SAVE="$(ls $dir -1 | grep -m "${prefix}save")"
  154.       if [ -z "$ONE_SAVE" ]; then
  155.          continue
  156.       else
  157.          SAVE_FILE="$ONE_SAVE"
  158.          FULL_SAVE_PATH="$dir"/ONE_SAVE
  159.          break
  160.       fi
  161.     done
  162.    done
  163.    echo "PSAVE"mount_items
  164. }
  165. function find_bk_folders(){
  166.   for a_PDRV in "$PDRV" sr0 sr1; do #Consider adding /mnt/home here
  167.     for a_psubdir in "${PSUBDIR}" "";  do
  168.       MT_PT_of_Folder="$(mount_fn2 "$PDRV" "${PSUBDIR}")"
  169.       #https://github.com/puppylinux-woof-CE/woof-CE/blob/c483d010a8402c5a1711517c2dce782b3551a0b8/initrd-progs/0initrd/init#L981
  170.       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)"
  171.       [ ! -z "#BKFOLDERS" ] && break  
  172.     done
  173.   done
  174. }
  175. function mount_items(){
  176.   local Moun_Point
  177.   local File_PATH #Might be a directory
  178.   cd "$SANDBOX_AWK_DIR"
  179.   while IFS="" read -r p || [ -n "$p" ]; do #https://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash
  180.      File_PATH="$(echo "$1" | awk -v FIELD_NUM=6 -f "$SB_DB_REC_FIELD_AWK")"
  181.      Mount_Point="$(echo "$1" | awk -v FIELD_NUM=1 -f "$SB_DB_REC_FIELD_AWK")"
  182.      PDRV_MNT="$(echo "$1" | awk -v FIELD_NUM=7 -f "$SB_DB_REC_FIELD_AWK")"
  183.      PDRV_UUID="$(echo "$1" | awk -v FIELD_NUM=8 -f "$SB_DB_REC_FIELD_AWK")"
  184.      
  185.      [ -z "$PDRV_MNT" ] &&
  186.      mount_fn2 "PDRV" "$File_PATH" "$Moun_Point"
  187.   done <"$1"
  188. }
  189.  
  190. function mk_initrd_dir(){
  191.   mkdir -p "$FAKEROOT"/initrd
  192.   if [ -z "$PUPMODE" ] ; then
  193.     if [ -z "$PMEDIA" ]; then
  194.       #if [ "$PUPMODE" = 5 ] ; then
  195.       #  #aufs layers:              RW (top)      RO1             RO2              PUPMODE
  196.       #  #First boot (or pfix=ram): tmpfs                         pup_xxx.sfs      5
  197.       PUPMODE=5 #MAYBE PUPMODE=2 would be better
  198.     elif [ PMEDIA = 'atahd' ] || [ "$PMEDIA" = 'usbhd' ]; then
  199.       find_save
  200.       if [ -f "$FULL_SAVE_PATH" ] || [ -d "$FULL_SAVE_PATH" ]; then
  201.         #aufs layers:               RW (top)      RO1             RO2              PUPMODE
  202.         #Normal running puppy:      pup_save.3fs                  pup_xxx.sfs      12      
  203.         PUPMODE=12
  204.       else
  205.         echo "Invalid SAVE_PATH=$SAVE_PATH does not exist"
  206.         PUMPMODE=2
  207.         #TODO, prompt to either search for save file/folder or alternatively create it.
  208.       fi
  209.     elif [ PMEDIA = 'usbflash' ] || [ pmedia = 'ideflash' ]; then
  210.       find_save
  211.       #aufs layers:                 RW (top)      RO1             RO2              PUPMODE
  212.       #ditto, but flash drive:      tmpfs         pup_save.3fs    pup_xxx.sfs      13
  213.       if [ -f "$SAVE_PATH" ] || [ -d "$SAVE_PATH" ]; then
  214.         #aufs layers:               RW (top)      RO1             RO2              PUPMODE
  215.         #ditto, but flash drive:    tmpfs         pup_save.3fs    pup_xxx.sfs      13
  216.         PUPMODE=13
  217.       else
  218.         echo "Invalid SAVE_PATH=$SAVE_PATH does not exist"
  219.         PUPMODE=5
  220.       fi
  221.     elif [ "$PMEDIA" =  usbcd ] || [ "$PMEDIA" =  idecd ] || [ "$PMEDIA" =  satacd ] ; then
  222.       find_bk_folders
  223.       if [ ! -z "$BKFOLDERS" ]; then
  224.         PUPMODE=77  #MULTI-Session CD
  225.       else #First Boot
  226.         find_save
  227.         if [ -f "$FULL_SAVE_PATH" ] || [ -d "$FULL_SAVE_PATH" ]; then
  228.           PUPMODE=13      
  229.         else
  230.           PUPMODE=5
  231.         fi
  232.       fi
  233.       #aufs layers:            RW (top)      RO1             RO2              PUPMODE
  234.       #Multisession cd/dvd:       tmpfs         folders         pup_xxx.sfs      77
  235.     else #[PUPMODE=2 -> full install
  236.       PUPMODE=2
  237.     fi
  238.     if [ "$PUPMODE" = 2 ]; then #Full install
  239.       echo "Full install has no initrd"
  240.     else
  241.       mkdir -p "$FAKEROOT/initrd"
  242.       cd $FAKEROOT/initrd
  243.       if [ "$PUPMODE" = 12 ]; then # Usually [ PMEDIA = 'atahd' ] || [ "$PMEDIA" = usbhd ]
  244.         ln -s mnt/dev_save/"${SAVE_PATH}" pup_rw
  245.       elif [ "$PUPMODE" = 13 ] || [ "$PUPMODE" = 5 ] || [ "$PUPMODE" = 77 ]; then
  246.         ln -s mnt/tmpfs/pup_rw pup_rw
  247.         if [ "$PUPMODE" = 13 ]; then  # Usually [ PMEDIA = 'usbflash' ] || [ pmedia = 'ideflash' ]
  248.           ln -s "mnt/tmpfs/dev_save/${SAVE_PATH}" pup_ro1
  249.         elif [ "$PUPMODE" = 77 ]; then
  250.           ln -s mnt/tmpfs/pup_ro1/"${SAVE_PATH}" pup_ro1  #Usually [ "$PMEDIA" =  usbcd ] || [ "$PMEDIA" =  idecd ] || [ "$PMEDIA" =  satacd ]
  251.         fi
  252.       fi
  253.     fi
  254.   fi
  255. }
  256. function get_items(){
  257.     local out
  258.     OUTFILE=/tmp/get_items_out
  259.     rm "$OUTFILE"
  260.     cd "$SANDBOX_AWK_DIR"
  261.     out+="$(
  262.  { echo ==mount==; cat /proc/mounts;
  263.    echo ==losetup==; losetup-FULL -a;
  264.    echo ==branches==;
  265.      if [ $# -eq 0 ]; then
  266.        ls -v /sys/fs/aufs/$AUFS_ROOT_ID/br[0-9]* | xargs sed 's/=.*//';
  267.      else
  268.        if [ "$1" = "-f" ]; then
  269.          cat "$2";
  270.        elif [ "$1" = "-s" ]; then
  271.          cat <<<"$2";
  272.        fi;
  273.      fi; } | \
  274.    awk -v PDRV="$PDRV" -v MAX_STR_LEN="$MAX_STR_LEN" -v OUTFILE="$OUTFILE" \
  275. -f "$SANDBOX_AWK"
  276. )"
  277.   echo "$out"
  278. }
  279. function process_psubdir(){
  280.       item_source="$1"
  281.       if [ "$item_source" = "maybe-psubdir" ]; then
  282.          [ ! -z "$items" ] && continue
  283.       fi
  284.       [ -z "$DISTRO_ADRVSFS" ] && DISTRO_ADRVSFS="$(ls -1 "${PDRV}/${PSUBDIR}" | grep -i -m1 'adrv.*\.sfs$')"
  285.       [ -z "$DISTRO_YDRVSFS" ] && DISTRO_YDRVSFS="$(ls -1 "${PDRV}/${PSUBDIR}" | grep -i -m1 'ydrv.*\.sfs$')"  
  286.       [ -z "$DISTRO_ZDRVSFS" ] && DISTRO_ZDRVSFS="$(ls -1 "${PDRV}/${PSUBDIR}" | grep -i -m1 'zdrv.*\.sfs$')"
  287.       [ -z "$DISTRO_FDRVSFS" ] && DISTRO_FDRVSFS="$(ls -1 "${PDRV}/${PSUBDIR}" | grep -i -m1 'fdrv.*\.sfs$')"                        
  288.       [ -z "$DISTRO_PUPPYSFS" ] && DISTRO_PUPPYSFS="$(ls -1 "${PDRV}/${PSUBDIR}" | grep -i -m1 'puppy_.*\.sfs$')"
  289.  
  290.       new_items=""
  291.       for rec in "$DISTRO_ADRVSFS" "$DISTRO_YDRVSFS" "$DISTRO_ZDRVSFS" "$DISTRO_FDRVSFS" "$DISTRO_PUPPYSFS";  do
  292.         #MNT_PATH="${rec}"
  293.         [ -z "$rec" ] && continue
  294.         #[ ! -z "${PSUBDIR}" ] && MNT_PATH=${PSUBDIR}/${MNT_PATH}
  295.         MNT_PATH="${PDRV}/${PSUBDIR}/$rec"
  296.         MNT_PT="$(mount_fn "$MNT_PATH")"
  297.         new_items+="\"${MNT_PT}\" \"$rec\" \"on\""$'\n'
  298.        
  299.       done
  300.       #export new_items="$new_items"
  301.       #echo "$new_items"
  302.       items+="$(get_items -s "$new_items")"$'\n'
  303.      
  304. }
  305. process_union_record(){
  306.        new_items=''
  307.        for rec in $LASTUNIONRECORD; do
  308.         if [ -f "$rec" ]; then
  309.           MNT_PT="$(mount_fm "$rec" )"
  310.           new_items+="\"$MNT_PT\" \"$rec\" \"on\""$'\n'
  311.         elif [ -f "$PDRV/$rec" ]; then
  312.           MNT_PT="$(mount_fm "$PDRV/$rec" )"
  313.           new_items+="\"$MNT_PT\", \"$PDRV/$rec\", \"on\""$'\n'
  314.         fi
  315.       done
  316.       items+="$(get_items -f <<<"$new_items")"$'\n'    
  317. }
  318. process_extra_sfs(){
  319.      EXTRASFSLIST="$2";
  320.      unset new_items
  321.      if [ ! -f "$EXTRASFSLIST" ]; then
  322.        EXTRASFSLIST_tmp=$(realpath "$PDRV/$PSUBDIR/$EXTRASFSLIST")
  323.        if [ -f "$EXTRASFSLIST_tmp" ]; then
  324.          EXTRASFSLIST="$EXTRASFSLIST_tmp"
  325.        fi
  326.      fi
  327.      if [ ! -f "$EXTRASFSLIST" ]; then
  328.        EXTRASFSLIST_tmp=$(realpath "$PDRV/$EXTRASFSLIST")
  329.        if [ -f "$EXTRASFSLIST_tmp" ]; then
  330.          EXTRASFSLIST="$EXTRASFSLIST_tmp"
  331.        fi
  332.      fi
  333.      if [[ "$EXTRASFSLIST" = *.sfs ]]; then
  334.          a_sfs="$EXTRASFSLIST"
  335.          MNT_PT="$(mount_fn "$a_sfs" )"
  336.          new_items+="\"$MNT_PT\" \"$a_sfs\" \"on\""$'\n'
  337.      else
  338.        while read a_sfs; do
  339.          a_sfs=$(echo"$a_sfs") #Trims leading and trailing whitespace
  340.          if [ -f "$a_sfs" ]; then
  341.            a_sfs=$(realpath "$a_sfs")
  342.          else
  343.            a_sfs1="$PDRV/${PSUBDIR}/$a_sfs"
  344.            a_sfs=$(realpath "$a_sfs")
  345.            if [ -f "$a_sfs"]; then
  346.              a_sfs=$(realpath "$a_sfs")
  347.            else        
  348.              a_sfs1="$PDRV/$a_sfs1"
  349.              if [ -f "$a_sfs1" ]; then
  350.                a_sfs=$(realpath "$a_sfs")
  351.              fi            
  352.            fi
  353.          fi
  354.          if [ -f  "$a_sfs" ]; then
  355.            MNT_PT="$(mount_fn "$a_sfs" )"
  356.            new_items+="\"$MNT_PT\" \"$a_sfs\" \"on\""$'\n'        
  357.          fi
  358.        done <"$EXTRASFSLIST"
  359.      fi
  360.      items+="$(get_items -s "$new_items")"$'\n'
  361.      #items+="$(get_items -f <<<"$new_items")"
  362. }
  363. process_layer(){
  364.       item_path="$2"
  365.       if [ -f "$item_path" ]; then
  366.         MNT_PT="$(mount_fm "$item_path" )"
  367.       elif [ -d "$item_path" ]; then  
  368.         MNT_PT="$item_path" #This isn't really a mount poing
  369.       elif [ ! -d  "$item_path" ]; then
  370.         echo "Warning  cannot mount $item_path"
  371.         continue
  372.       fi
  373.       items+="\"$MNT_PT\" \"$item_path\" \"on\""$'\n'  
  374. }
  375. declare -a options="$(getopt -o f:,o:,m:,d:,s:,b:,e:,l:,t::,a::,u::,r::,j: --long input-file:output-file:,pmedia:,pdrv:,psubdir:,boot-config:,distro-specs:,extra-sfs:,aufs,maybe-aufs,maybe-psubdir:,no-exit::,psave:,pupmode:,logfile:,trace::,rw-layer:,copy-Xauth::,bind-X11-sockets::,copy-resolv_conf::,layer:,rev_bind:,bind:,before-chroot: -- "$@")"
  376. eval set --"$options_str"
  377. #set -- $options
  378. #declare -a options=$( $options_str )
  379. eval set --"$options"
  380. while [ $# -gt 0 ]; do
  381.   echo "processing args: $@"
  382.   case $1 in
  383.   -f|--input-file)
  384.      INPUT_FILE=$2
  385.     mount_items "$INPUT_FILE"
  386.     items+="$(get_items -f "$INPUT_FILE")"
  387.     shift 2; ;;      
  388.   -o|--output-file) OUTPUT_FILE=$2; shift 2; ;;
  389.   --no-exit)
  390.     if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  391.       NO_EXIT="$2"
  392.       [ -z "$NO_EXI" ] && NO_EXIT=true
  393.       shift 2
  394.     else
  395.       NO_EXIT=true
  396.       shift 1
  397.     fi; ;;
  398.   -p|--env-prefix) ENV_PREFIX=$2; shift 2; ;;
  399.   -m|--pmedia) PMEDIA=$2; shift 2; ;;
  400.   -d| --pdrv) PDRV=$2; shift 2; ;;
  401.   -s|--psubdir) PSUBDIR=$2;
  402.     process_psubdir psubdir
  403.     shift 2; ;;
  404.     --maybe-psubdir) PSUBDIR=$2;
  405.     process_psubdir maybe-psubdir    
  406.     shift 2; ;;    
  407.   --distro-specs)
  408.      DISTRO_SPECS=$2;
  409.      [ -f "$DISTRO_SPECS" ] && . "$DISTRO_SPECS"
  410.      shift 2
  411.      ;;
  412.    --boot-config)
  413.        BOOTCONFIG=$2;
  414.      [ -f "$BOOTCONFIG" ] && . "$BOOTCONFIG"
  415.      shift 2
  416.      ;;
  417.    --union-record)  
  418.      LASTUNIONRECORD=$2;
  419.      process_union_record union-record "$LASTUNIONRECORD"
  420.      shift 2; ;;
  421.    -e|--extra-sfs)
  422.      EXTRASFSLIST=$2;
  423.      process_extra_sfs extra-sfs "$EXTRASFSLIST"
  424.      shift 2; ;;
  425.   --aufs)
  426.     items+="$(get_items)"
  427.     shift 1; ;;
  428.   --maybe-aufs)
  429.     [  -z "$items" ] && items+="$(get_items)"
  430.     shift 1; ;;
  431.   --psave)
  432.     PSAVE=$2
  433.     shift 2
  434.     ;;
  435.   --pupmode)
  436.     PUPMODE=$2
  437.     shift 2
  438.     ;;
  439.   --rw-layer)
  440.     RW_LAYER=$2
  441.     shift 2
  442.     ;;
  443.   --layer)
  444.     RW_LAYER=$2
  445.     process_layer layer $2
  446.     shift 2
  447.     ;;
  448.   -l|--logfile)
  449.     LOGFILE=$2
  450.     [ -z "$TRACE" ] && TRACE=true
  451.     shift 2
  452.     log init
  453.     ;;  
  454.   -t|--trace)
  455.     TRACE=$2
  456.     if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  457.       TRACE="$2"
  458.       [ -z "$TRACE" ] && TRACE=true
  459.       shift 2
  460.     else
  461.       TRACE=true
  462.       shift 1
  463.     fi
  464.     log init
  465.     ;;
  466.   -a|--copy-Xauth)
  467.     if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  468.       XAUTH=$(realpath "$2")
  469.       [ -z "$XAUTH" ] && XAUTH=$(realpath "~/.Xauthority")
  470.       shift 2
  471.     else
  472.       XAUTH=$(realpath "~/.Xauthority")
  473.       shift 1
  474.     fi
  475.     ;;
  476.   -u|--bind-X11-sockets)
  477.     if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  478.       uSocketDir=$(realpath "$2")
  479.       [ -z "$uSocketDir" ] && uSocketDir=/tmp/.X11-unix
  480.       shift 2
  481.     else
  482.       uSocketDir=/tmp/.X11-unix
  483.       shift 1
  484.     fi
  485.     ;;
  486.   -r|--copy-resolv_conf)
  487.     if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  488.       RESOLV_CONF_PATH=$(realpath "$2")
  489.       [ -z "$RESOLV_CONF_PATH" ] && RESOLV_CONF_PATH=/etc/resolv.conf
  490.       shift 2
  491.     else
  492.       RESOLV_CONF_PATH=/etc/resolv.conf
  493.       shift 1
  494.     fi
  495.     ;;
  496.   --rev_bind) #Bind the fakeroot into a folder (e.g. a samba share)
  497.        unset rev_b_source
  498.       unset rev_b_target
  499.       while true
  500.       do
  501.         if [ $# -lt 1 ]; then
  502.           break
  503.         fi
  504.         case "$1" in
  505.         --)    
  506.  
  507.           if [ ! -z "$rev_b_source" ]; then
  508.             break
  509.           else
  510.           #TODO, add some further checking here. What we want to do is eat the -- at the end of the positional parameters if we are missing a target.
  511.             shift
  512.             continue
  513.           fi
  514.           ;;
  515.         --rev-bind)
  516.             shift
  517.             continue
  518.             ;;
  519.         -*)
  520.             break
  521.             ;;
  522.         esac
  523.         if [ -z "$rev_b_source" ]; then
  524.           if [ ! -z "$1" ]; then
  525.             rev_b_source="$1"; shift
  526.             continue
  527.           fi        
  528.         elif [ -z "$b_target" ]; then
  529.           if [ ! -z "$1" ]; then
  530.             rev_b_target="$1"; shift
  531.             rev_bind_sources+=( "$rev_b_source" )
  532.             rev_bind_targets+=( "$rev_b_target" )
  533.             unset rev_b_source
  534.             unset rev_b_target
  535.             shift
  536.             continue
  537.           fi        
  538.         else
  539.           shift
  540.           break
  541.         fi
  542.       done
  543.  #   fi
  544.     ;;
  545.   --before-chroot)
  546.      BEFORE_CHROOT_CMD="$2";
  547.      shift 2
  548.      ;;
  549.   --bind)
  550. #    if [ $# -ge 4 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ] && \
  551. #[[ "$3" = -j ]] && [[ ! "$4" = --* ]] && [ ! -z "$4" ]; then
  552. #       bind_source=$(realpath "$2")
  553. #       bind_target=$(realpath "$2")
  554. #       bind_sources+=( "$bind_source" )
  555. #       bind_targets+=( "$bind_target" )
  556. #       shift 4
  557. #    else
  558.       unset b_source
  559.       unset b_target
  560.       while true
  561.       do
  562.         if [ $# -lt 1 ]; then
  563.           break
  564.         fi
  565.         case "$1" in
  566.         --)    
  567.  
  568.           if [ ! -z "$b_source" ] && [ $# -lt 3 ]; then
  569.             break
  570.           else
  571.           #TODO, add some further checking here. What we want to do is eat the -- at the end of the positional parameters if we are missing a target.
  572.             shift
  573.             continue
  574.           fi
  575.           ;;
  576.         --bind)
  577.             shift
  578.             continue        
  579.         -*)
  580.             break
  581.             ;;
  582.         esac        
  583.         if [[ $1 = -* ]]; then
  584.           if [ ! -z "$b_target" ]; then
  585.             break
  586.           else
  587.           #TODO, add some further checking here. What we want to do is eat the -- at the end of the positional parameters if we are missing a target.
  588.             shift
  589.             continue
  590.           fi
  591.         fi
  592.         if [ -z "$b_source" ]; then
  593.           if [ ! -z "$1" ]; then
  594.             b_source="$1"; shift
  595.             continue
  596.           fi
  597.         elif [ -z "$b_target" ]; then
  598.           if [ ! -z "$1" ]; then
  599.             b_target="$1"; shift
  600.             bind_sources+=( "$b_source" )
  601.             bind_targets+=( "$b_target" )
  602.             unset b_source
  603.             unset b_target
  604.             shift
  605.             continue
  606.           fi        
  607.         fi
  608.         shift
  609.       done
  610.  #   fi
  611.     ;;          
  612.   --)
  613.     shift 1
  614.     options2+=( "$@" )
  615.     break; ;;
  616.   *)
  617.      options2+=( "$1" )
  618.      shift 1; ;;
  619.   esac
  620. done
  621. items="$(echo "$items" | sed -n '/^\s*\(on\)\?\s*$/! p' | sed -n '/^Error: Expected on/! p' | sed -n '/^Use --help on/! p')"
  622.  
  623.       log stop
  624.       read -p "Press enter to continue"
  625.       log start
  626. #set -- "${options2[@]}"
  627. #if [ "$LAYER_SOURCE" = none ] && [ ! -z "$PDRV" ]; then
  628. #  PDRV=${PDRV:-/mnt/home}
  629. #  for rec in $LASTUNIONRECORD; do
  630. #    if [ -f "$PDRV/$rec" ]; then
  631. #      items+="\"$PDRV/$rec\" \"$rec\""$'\n'
  632. #    fi
  633. #  done
  634. #  if [ ! -z "$$PDRV" ]; then #if [ -z "$items" ]; then
  635. #    [ -z "$DISTRO_ADRVSFS" ] && DISTRO_ADRVSFS=$(ls -1 $PDRV | grep -i -m1 adrv.*\.sfs$)  
  636. #    [ -z "$DISTRO_YDRVSFS" ] && DISTRO_YDRVSFS=$(ls -1 $PDRV | grep -i -m1 ydrv.*\.sfs$)      
  637. #    [ -z "$DISTRO_ZDRVSFS" ] && DISTRO_ZDRVSFS=$(ls -1 $PDRV | grep -i -m1 zdrv.*\.sfs$)
  638. #    [ -z "$DISTRO_FDRVSFS" ] && DISTRO_FDRVSFS=$(ls -1 $PDRV | grep -i -m1 fdrv.*\.sfs$)        
  639. #    [ -z "$DISTRO_PUPPYSFS" ] && DISTRO_PUPPYSFS=$(ls -1 $PDRV | grep -i -m1 puppy_.*\.sfs$)
  640. #
  641. #    for rec in "$DISTRO_ADRVSFS" "$DISTRO_YDRVSFS" "$DISTRO_ZDRVSFS" "$DISTRO_FDRVSFS" "$DISTRO_PUPPYSFS"; do
  642. #      [ -z "$rec" ] && continue
  643. #      items+="$PDRV/$rec" "$rec"$'\n'  
  644. #    done
  645. #  fi
  646. #  #if [ ! -z "$items" ]; then  
  647. #  #  for rec in $EXTRASFSLIST; do
  648. #  #    if [ -f "$rec" ]; then
  649. #  #      items+="\"$PDRV/$rec\" \"$rec\" "on"\""$'\n'
  650. #  #    elif [ -f "$PDRV/$rec" ]; then
  651. #  #      items+="\"$PDRV/$rec\" \"$rec\" "on"\""$'\n'
  652. #  #    fi
  653. #  #  done
  654. #  #fi
  655. #fi
  656. #if [ -z "$items" ] && [ "$LAYER_SOURCE" = none ] ; then
  657. #    LAYER_SOURCE=aufs  
  658. #    LAYER_SOURCES+=( aufs )
  659. #fi
  660. #    for rec in "${LAYER_SOURCES[@]}"; do
  661. #      [ -z "$rec" ] && continue
  662. #      if [ -f "$rec" ]; then
  663. #        rec=$(realpath "$rec")
  664. #        rec_name=$(basename $rec);
  665. #        items+="'$rec' '$rec'"$'\n'
  666. #      elif [ -f "$PDRV/$rec" ]; then
  667. #        items+="'$PDRV/$rec' '$rec'"$'\n'
  668. #      fi
  669. #    done
  670. #[ -z "$PDRV" ] && PDRV="/mnt/home"
  671.  
  672. if [ "$(cat /proc/mounts | grep -c "$(realpath "$PDRV")")" = 0 ]; then
  673.  PDRV_DEV="$(blkid | grep -m1 "$PDRV" | cut -d ':' -f1)"
  674.  PDRV="$(echo "$PDRV_DEV" | sed 's#^/dev/#/mnt/#')"
  675.  mount "$PDRV_DEV" "$PDRVV"
  676. fi  
  677.  
  678.  
  679. FAKEROOT=$SANDBOX_ROOT/fakeroot   # mounted chroot location of sandbox - ie, the fake root
  680. [ -z "$RW_LAYER" ] && SANDBOX_TMPFS=$SANDBOX_ROOT/sandbox # mounted rw location of tmpfs used for sandbox
  681. DEV_SAVE=$SANDBOX_ROOT/dev_save
  682. mkdir -p "$DEV_SAVE"
  683.  
  684. SANDBOX_ID=
  685. TMPFILE=$(mktemp -p /tmp)
  686. # use namespaces if available
  687. #[ -e /proc/1/ns/pid ] && [ -e /proc/1/ns/mnt ] && type unshare >/dev/null && USE_NS=1
  688.  
  689.  
  690.  
  691.  
  692. # umount all if we are accidentally killed
  693. trap 'umountall' 1
  694. umountall() {
  695.  #{
  696.  set +x
  697.  if [ ${#FAKEROOT} -gt 3 ]; then
  698.       umount -l $FAKEROOT/$SANDBOX_TMPFS
  699.       if [ "$PUPMODE" = 2 ]; then #Full Install
  700.           umount -l $FAKEROOT/tmp
  701.         else
  702.           umount -l $FAKEROOT/initrd/mnt/tmpfs
  703.         fi
  704.       for layer_name in "pup_ro2" "pup_ro3" "pup_ro4" "pup_ro5" "pup_z"; do
  705.         layer="$(eval 'echo $'$layer_name)"
  706.         if [ ! -z "$layer" ] ; then
  707.           umount -l "$FAKEROOT/initrd/$layer_name"
  708.         fi
  709.       done    
  710.       umount -l $FAKEROOT/proc
  711.       umount -l $FAKEROOT/sys
  712.       umount -l $FAKEROOT/dev
  713.      
  714.       umount -l $FAKEROOT
  715.       [ -z "$RW_LAYER" ] && umount -l $SANDBOX_TMPFS
  716.       rmdir $FAKEROOT
  717.       #if  [ PUPMODE = 2 ] || PUPMODE = 5 ]; then
  718.         [ -z "$RW_LAYER" ] && rmdir $SANDBOX_TMPFS
  719.       #fi
  720.      # } 2> /dev/null
  721.  fi
  722. }
  723.  
  724. # 0.1 must be root
  725. if [ $(id -u) -ne 0 ]; then
  726.  echo "You must be root to use sandbox."
  727.  exit
  728. fi
  729.  
  730. # 0.2 cannot launch sandbox within sandbox
  731. if [ "$AUFS_ROOT_ID" != "" ] ; then
  732.  grep -q $SANDBOX_ROOT /sys/fs/aufs/$AUFS_ROOT_ID/br0 &&
  733.    echo "Cannot launch sandbox within sandbox." && exit
  734. fi
  735.  
  736. # 0.3 help
  737. case "$1" in
  738.  --help|-h)
  739.  echo "Usage: ${0##*/}"
  740.  echo "Starts an in-memory (throwaway) sandbox. Type 'exit' to leave."
  741.  exit
  742. esac
  743.  
  744. # 0.4 if not running from terminal but in Xorg, then launch via terminal
  745. ! [ -t 0 ] && [ -n "$DISPLAY" ] && exec $XTERM -e "$0" "$@"
  746. ! [ -t 0 ] && exit
  747. # 1. get aufs system-id for the root filesystem
  748. if [ -z "$AUFS_ROOT_ID" ] ; then
  749.  AUFS_ROOT_ID=$(
  750.    awk '{ if ($2 == "/" && $3 == "aufs") { match($4,/si=[0-9a-f]*/); print "si_" substr($4,RSTART+3,RLENGTH-3) } }' /proc/mounts
  751.  )
  752. fi
  753.  
  754.  
  755. # 3. Ask user to choose the SFS
  756. echo "items=$items"
  757. cat <<EOF
  758. dialog --separate-output --backtitle "tmpfs sandbox" --title "sandbox config" \
  759.  --checklist "Choose which SFS you want to use" 0 0 0 $items 2> $TMPFILE
  760. EOF
  761.  
  762. log stop
  763. dialog --separate-output --backtitle "tmpfs sandbox" --title "sandbox config" \
  764.  --checklist "Choose which SFS you want to use" 0 0 0 $items 2> $TMPFILE
  765. chosen="$(cat $TMPFILE)"
  766. log start
  767. clear
  768. if [ -z "$chosen" ]; then
  769.  echo "Cancelled or no SFS is chosen - exiting."
  770.  exit 1
  771. fi
  772.  
  773.  
  774. # 4. convert chosen SFS to robranches
  775. robranches=""
  776. for a in $(cat $TMPFILE) ; do
  777.    #a="$(echo "$a" | sed 's/,$//')" # | sed 's/^'//' | sed 's/'$//' )"
  778.     echo "a=$a"
  779.       log stop
  780.       read -p "Press enter to continue"
  781.       log start
  782.     a="$(echo "$a" | sed 's/"//g')" # | sed 's/^'//' | sed 's/'$//' )"
  783.   robranches=$robranches:$a=ro
  784.   sed -i "\#^$a # {s/ off / on /}" /tmp/get_items_out
  785. done
  786. if [ ! -z "$OUTPUT_FILE" ]; then
  787.   cp "/tmp/get_items_out" "$OUTPUT_FILE"
  788.   if [ ! "$NO_EXIT" = true ]; then
  789.     exit 0
  790.   fi
  791. fi
  792. rm $TMPFILE
  793.  
  794. #if [ PUPMODE = 2 ] || PUPMODE = 5 ]; then
  795.   # 0.5 is this the first sandbox? If not, then create another name for mountpoints
  796.   if grep -q $FAKEROOT /proc/mounts && [ -z "$RW_LAYER" ]; then
  797.   FAKEROOT=$(mktemp -d -p $SANDBOX_ROOT ${FAKEROOT##*/}.XXXXXXX)
  798.   SANDBOX_ID=".${FAKEROOT##*.}"
  799.   SANDBOX_TMPFS=$SANDBOX_ROOT/${SANDBOX_TMPFS##*/}${SANDBOX_ID}
  800.   rmdir $FAKEROOT
  801.   fi
  802.   # 5. make the mountpoints if not exist  yet
  803.   [ -z "$RW_LAYER" ] && mkdir -p $FAKEROOT $SANDBOX_TMPFS
  804. #else
  805. #  SANDBOX_TMPFS="$SAVE_MP_FULL_PATH"
  806. #fi
  807.  
  808.  
  809.  
  810. mk_initrd_dir
  811.  
  812.  
  813. # 6. do the magic - mount the tmpfs first, and then the rest with aufs
  814. if mount -t tmpfs none $SANDBOX_TMPFS || [ ! -z "$RW_LAYER" ]; then
  815.   if [ -z "$RW_LAYER" ]; then
  816.     TOP_LAYER="$SANDBOX_TMPFS"
  817.   else
  818.     mkdir -p "$RW_LAYER"
  819.     #TODO maybe check if the RW layer is a file and if so mount it first.
  820.     TOP_LAYER="$RW_LAYER"
  821.   fi
  822.   if mount -t aufs -o "udba=reval,diropq=w,br:$TOP_LAYER=rw$robranches" aufs $FAKEROOT; then
  823.     # 5. record our new aufs-root-id so tools don't hack real filesystem  
  824.     SANDBOX_AUFS_ID=$(grep $FAKEROOT /proc/mounts | sed 's/.*si=/si_/; s/ .*//') #'
  825.     sed -i -e '/AUFS_ROOT_ID/ d' $FAKEROOT/etc/BOOTSTATE 2> /dev/null
  826.     echo AUFS_ROOT_ID=$SANDBOX_AUFS_ID >> $FAKEROOT/etc/BOOTSTATE
  827.    
  828.     # 7. sandbox is ready, now just need to mount other supports - pts, proc, sysfs, usb and tmp
  829.     mkdir -p $FAKEROOT/dev $FAKEROOT/sys $FAKEROOT/proc $FAKEROOT/tmp
  830.     mkdir -p  "$DEV_SAVE/${PSUBDIR}"
  831.     mount -o bind  "PDRV/${PSUBDIR}" "$DEV_SAVE/${PSUBDIR}" #TODO: ONLY do this if we aren't going to mount all of mnt/dev_save
  832.     mount -o bind  "$DEV_SAVE/${PSUBDIR}" "$FAKEROOT/initrd/mnt/dev_save"
  833.     #Maybe optionally do this based on some input paramater:
  834.     #Also pull these layers from an array
  835.     for layer_name in "pup_ro2" "pup_ro3" "pup_ro4" "pup_ro5" "pup_z"; do
  836.         layer="$(eval 'echo $'$layer_name)"
  837.       if [ ! -z "$layer" ] ; then
  838.         mount -o bind  "$layer" "$FAKEROOT/initrd/$layer_name"
  839.       fi
  840.     done
  841.     mount -o rbind /dev $FAKEROOT/dev
  842.     mount -t sysfs none $FAKEROOT/sys
  843.     #mount -t proc none $FAKEROOT/proc
  844.     mount -t proc /proc $FAKEROOT/proc
  845.     if [ PUPMODE = 2 ]; then #Full Install
  846.       tmp_des=$FAKEROOT/tmp
  847.       tmp_source=/tmp
  848.     else
  849.         mkdir -p $FAKEROOT/initrd/mnt/tmpfs
  850.       tmp_des=$FAKEROOT/initrd/mnt/tmpfs
  851.       tmp_source=/initrd/mnt/tmpfs
  852.       cd $FAKEROOT
  853.       rm tmp
  854.       ln -s initrd/mnt/tmpfs tmp
  855.     fi
  856.     mount -o bind $tmp_source $tmp_des
  857.     mkdir -p $FAKEROOT/$SANDBOX_TMPFS
  858.     mount -o bind $SANDBOX_TMPFS $FAKEROOT/$SANDBOX_TMPFS # so we can access it within sandbox
  859.    
  860.     # 8. optional copy, to enable running sandbox-ed xwin
  861.     cp /usr/share/sandbox/* $FAKEROOT/usr/bin 2> /dev/null
  862.    
  863.     # 9. make sure we identify ourself as in sandbox - and we're good to go!
  864.     echo -e '\nexport PS1="sandbox'${SANDBOX_ID}'# "' >> $FAKEROOT/etc/shinit #fatdog 600
  865.     sed -i -e '/^PS1/ s/^.*$/PS1="sandbox'${SANDBOX_ID}'# "/' $FAKEROOT/etc/profile # earlier fatdog
  866.    
  867.     if [ -d "$FULL_SAVE_PATH" ]; then #TODO verify that this works with a save file
  868.       if [ $PUPMODE -eq 13 ] && [ $PUPMODE -eq 77 ]; then
  869.         #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
  870.         #and copy_folders()  https://github.com/puppylinux-woof-CE/woof-CE/blob/c483d010a8402c5a1711517c2dce782b3551a0b8/initrd-progs/0initrd/init#L482
  871.           #https://github.com/puppylinux-woof-CE/woof-CE/blob/c483d010a8402c5a1711517c2dce782b3551a0b8/initrd-progs/0initrd/init#L1091
  872.           mount -o remount,prepend:"$FULL_SAVE_PATH"=rw,mod:"$SANDBOX_TMPFS"=ro,del:"$SANDBOX_TMPFS" "$FAKEROOT"
  873.           #mount -o remount,add:1:"$FULL_SAVE_PATH"=ro+wh "$FAKEROOT"
  874.       fi
  875.     fi
  876.     if [ ! -z "$XAUTH" ]; then
  877.       cp "$XAUTH" "$FAKEROOT/$XAUTH"
  878.     fi
  879.     if [ ! -z "$uSocketDir" ]; then
  880.       mkdir -p "$FAKEROOT$uSocketDir"
  881.       mount --bind "$uSocketDir" "$FAKEROOT$uSocketDir"    
  882.     fi  
  883.     if [ ! -z "$RESOLV_CONF_PATH" ]; then
  884.       cp "$RESOLV_CONF_PATH" "$FAKEROOT/etc/resolv.conf"
  885.     fi  
  886.     for i in "${!bind_sources[@]}"; do
  887.       b_source="${bind_sources[$i]}"
  888. #      chroot "$FAKEROOT" busybox ash <<EOF
  889. #         cd "$FAKEROOT"
  890. #         if [ ! -z "\`which realpath\`" ]; then
  891. #           target=\$(realpath "${bind_targets[$i]}"
  892. #           mkdir -p $target
  893. #         else
  894. #           target=${bind_targets[$i]}
  895. #           mkdir -p $target
  896. #         fi
  897. #        
  898. #EOF
  899.       b_target=$(realpath -m "$FAKEROOT/${bind_targets[$i]}")
  900.       mkdir -p "$b_target"
  901.       mkdir -p "$b_source"
  902.       log stop
  903.       read -p "Press enter to continue"
  904.       log start
  905.       mount --bind "$b_source" "$b_target"
  906.     done
  907.     for i in "${!rev_bind_sources[@]}"; do
  908.       rev_b_source="$rev_bind_sources[$i]"
  909.       rev_b_source="${rev_b_source#/}"
  910.       rev_b_source="$FAKEROOT/$rev_bind_sources"
  911.       rev_b_target="${bind_targets[$i]}"
  912.       rev_b_target=$(eval "echo \"$rev_b_target\"")
  913.       rev_b_target=$(realpath -m "$rev_b_target")
  914.       mkdir -p "$rev_b_target"
  915.       mkdir -p "$rev_b_source"
  916.       log stop
  917.       read -p "Press enter to continue"
  918.       log start
  919.       mount --bind "$b_source" "$b_target"
  920.     done    
  921.     echo "Starting sandbox now."
  922.     echo "USE_NS=$USE_NS"
  923.     $BEFORE_CHROOT_CMD
  924.     log stop    
  925.     if [ $USE_NS ]; then
  926.        read -p "Press enter to continue"
  927.       unshare -f -p --mount-proc=$FAKEROOT/proc chroot $FAKEROOT
  928.     else
  929.       read -p "Press enter to continue"
  930.  
  931.       chroot $FAKEROOT
  932.     fi
  933. log start
  934.     # 10. done - clean up everything
  935.     umountall
  936.     echo "Leaving sandbox."
  937.   else
  938.     echo "Unable to mount aufs br:$SANDBOX_TMPFS=rw$robranches"
  939.     umount -l $SANDBOX_TMPFS    
  940.   fi
  941. else
  942.   echo "unable to mount tmpfs."
  943. fi
  944.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement