Advertisement
s243a

sandbox_s243a.sh

Jan 19th, 2020
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 14.64 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.  
  48. XTERM=${XTERM:-urxvt}
  49. SANDBOX_ROOT=${SANDBOX_ROOT:-/mnt/sb}
  50. 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 -- "$@")"
  51. declare -a options2
  52. declare -a LAYER_SOURCES
  53. LAYER_SOURCE=none
  54. eval set --"$options"
  55. while [ $# -gt 0 ]; do
  56.   case "$1" in
  57.   -f|--input-file) INPUT_FILE=$2;
  58.     LAYER_SOURCE=INPUT_FILE
  59.     LAYER_SOURCES+=( input-file )
  60.     shift 2; ;;      
  61.   -o|--output-file) OUTPUT_FILE=$2; shift 2; ;;
  62.   --no-exit)
  63.     if [ $# -gt 1 ] && [[ ! "$2" = --* ]]; then
  64.       NO_EXIT="$2"
  65.     else
  66.       NO_EXIT=true
  67.     fi; ;;
  68.   -p|--env-prefix) ENV_PREFIX=$2; shift 2; ;;
  69.   -m|--pmedia) PMEDIA=$2; shift 2; ;;
  70.   -d| --pdrv) PDRV=$2; shift 2; ;;
  71.   -s|--psubdir) PSUBDIR=$2;
  72.     LAYER_SOURCE=psubdir  
  73.     LAYER_SOURCES+=( psubdir )
  74.     shift 2; ;;
  75.   --distro-specs)
  76.      DISTRO_SPECS=$2;
  77.      . "$DISTRO_SPECS"
  78.      shift 2
  79.      ;;
  80.    --boot-config)
  81.        DISTRO_SPECS=$2;
  82.      . "$BOOTCONFIG"
  83.      shift 2
  84.      ;;
  85.    --union-record)  
  86.      LASTUNIONRECORD="$2";
  87.      LAYER_SOURCES+=( union-record )
  88.      shift 2; ;;
  89.    -e|--extra-sfs)
  90.      EXTRASFSLIST="$2";
  91.      LAYER_SOURCES+=( extrasfs )
  92.      shift 2; ;;
  93.   --maybe-aufs)
  94.     LAYER_SOURCE=maybe-aufs  
  95.     LAYER_SOURCES+=( maybe-aufs )
  96.     shift 1; ;;
  97.   --)
  98.     shift 1
  99.     options2+=( "$@" )
  100.     break; ;;
  101.   *)
  102.      options2+=( "$1" )
  103.      shift 1; ;;
  104.   esac
  105. done
  106.  
  107. #set -- "${options2[@]}"
  108. if [ "$LAYER_SOURCE" = none ] && [ ! -z "$PDRV" ]; then
  109.   PDRV=${PDRV:-/mnt/home}
  110.   for rec in $LASTUNIONRECORD; do
  111.     if [ -f "$pdrv/$rec" ]; then
  112.       items+="$pdrv/$rec" "$rec"$'\n'
  113.     fi
  114.   done
  115.   if [ -z "$items" ]; then
  116.     [ -z "$DISTRO_PUPPYSFS" ] && DISTRO_PUPPYSFS=$(ls -1 /mnt/home | grep -m1 puppy_.*\.sfs$)
  117.     [ -z "$DISTRO_ZDRVSFS" ] && DISTRO_ZDRVSFS=$(ls -1 /mnt/home | grep -m1 zdrv.*\.sfs$)
  118.     [ -z "$DISTRO_FDRVSFS" ] && DISTRO_FDRVSFS=$(ls -1 /mnt/home | grep -m1 fdrv.*\.sfs$)
  119.     [ -z "$DISTRO_ADRVSFS" ] && DISTRO_ADRVSFS=$(ls -1 /mnt/home | grep -m1 fdrv.*\.sfs$)    
  120.     [ -z "$DISTRO_YDRVSFS" ] && DISTRO_YDRVSFS=$(ls -1 /mnt/home | grep -m1 fdrv.*\.sfs$)    
  121.     for rec in "$DISTRO_PUPPYSFS" "$DISTRO_ZDRVSFS" "$DISTRO_FDRVSFS" "$DISTRO_ADRVSFS" "$DISTRO_YDRVSFS"; do
  122.       items+="$pdrv/$rec" "$rec"$'\n'  
  123.     done
  124.   fi
  125.   if [ ! -z "$items" ]; then  
  126.     for rec in $EXTRASFSLIST; do
  127.       if [ -f "$pdrv/$rec" ]; then
  128.         items+="$pdrv/$rec" "$rec"$'\n'
  129.       fi
  130.     done
  131.   fi
  132. fi
  133. if [ -z "$items" ] && [ "$LAYER_SOURCE" = none ] ; then
  134.     LAYER_SOURCE=aufs  
  135.     LAYER_SOURCES+=( aufs )
  136. fi
  137. FAKEROOT=$SANDBOX_ROOT/fakeroot   # mounted chroot location of sandbox - ie, the fake root
  138. SANDBOX_TMPFS=$SANDBOX_ROOT/sandbox # mounted rw location of tmpfs used for sandbox
  139. SANDBOX_ID=
  140. TMPFILE=$(mktemp -p /tmp)
  141. # use namespaces if available
  142. #[ -e /proc/1/ns/pid ] && [ -e /proc/1/ns/mnt ] && type unshare >/dev/null && USE_NS=1
  143.  
  144.  
  145.  
  146.  
  147. # umount all if we are accidentally killed
  148. trap 'umountall' 1
  149. umountall() {
  150.     {
  151.     umount -l $FAKEROOT/$SANDBOX_TMPFS
  152.     umount -l $FAKEROOT/tmp
  153.     umount -l $FAKEROOT/proc
  154.     umount -l $FAKEROOT/sys
  155.     umount -l $FAKEROOT/dev
  156.    
  157.     umount -l $FAKEROOT
  158.     umount -l $SANDBOX_TMPFS
  159.     rmdir $FAKEROOT
  160.     rmdir $SANDBOX_TMPFS
  161.     } 2> /dev/null
  162. }
  163.  
  164. # 0.1 must be root
  165. if [ $(id -u) -ne 0 ]; then
  166.     echo "You must be root to use sandbox."
  167.     exit
  168. fi
  169.  
  170. # 0.2 cannot launch sandbox within sandbox
  171. if [ "$AUFS_ROOT_ID" != "" ] ; then
  172.     grep -q $SANDBOX_ROOT /sys/fs/aufs/$AUFS_ROOT_ID/br0 &&
  173.         echo "Cannot launch sandbox within sandbox." && exit
  174. fi
  175.  
  176. # 0.3 help
  177. case "$1" in
  178.     --help|-h)
  179.     echo "Usage: ${0##*/}"
  180.     echo "Starts an in-memory (throwaway) sandbox. Type 'exit' to leave."
  181.     exit
  182. esac
  183.  
  184. # 0.4 if not running from terminal but in Xorg, then launch via terminal
  185. ! [ -t 0 ] && [ -n "$DISPLAY" ] && exec $XTERM -e "$0" "$@"
  186. ! [ -t 0 ] && exit
  187. # 1. get aufs system-id for the root filesystem
  188. if [ -z "$AUFS_ROOT_ID" ] ; then
  189.     AUFS_ROOT_ID=$(
  190.         awk '{ if ($2 == "/" && $3 == "aufs") { match($4,/si=[0-9a-f]*/); print "si_" substr($4,RSTART+3,RLENGTH-3) } }' /proc/mounts
  191.     )
  192. fi
  193. function get_items(){
  194.  
  195.  
  196.  
  197.     items+=$(
  198.   { echo ==mount==; cat /proc/mounts;
  199.     echo ==losetup==; losetup-FULL -a;
  200.     echo ==branches==;
  201.       if [ $# -eq 0 ]; then
  202.         ls -v /sys/fs/aufs/$AUFS_ROOT_ID/br[0-9]* | xargs sed 's/=.*//';
  203.       else
  204.         case $1 in
  205.         -f) cat "$2"
  206.         esac
  207.       fi; } | \
  208.     awk '
  209.    function mount(MNT_PT,MNT_PATH){
  210.      system("mkdir -p " MNT_PT)
  211.      system("mount " MNT_PATH " " MNT_PT " 2>/dev/null")
  212.    }
  213.    function mount_if_valid(MNT_PT,MNT_PATH,PROOT,PDRV                  ,F_MNT_PATH,D_MNT_PATH){
  214.          if (system("[ -f " MNT_PATH " ] && exit 0 || exit 1") == 0){
  215.            F_MNT_PATH=MNT_PATH
  216.          } else if (system("[ -f " PROOT "/" MNT_PATH " ] && exit 0 || exit 1") == 0){
  217.           F_MNT_PATH=PROOT "/" MNT_PATH
  218.         } else if (system("[ -f " PDRV "/" PROOT "/" MNT_PATH " ]  && exit 0 || exit 1") == 0){
  219.           F_MNT_PATH=PROOT "/" MNT_PATH
  220.          } else if (system("[ -d " MNT_PATH " ] && exit 0 || exit 1") == 0){
  221.            D_MNT_PATH=MNT_PATH
  222.         } else if (system("[ -d " PROOT "/" MNT_PATH " ]  && exit 0 || exit 1") == 0){
  223.           D_MNT_PATH=PROOT "/" MNT_PATH
  224.         } else if (system("[ -d " PDRV "/" PROOT "/" MNT_PATH " ]  && exit 0 || exit 1") == 0 ){
  225.           D_MNT_PATH=PROOT "/" MNT_PATH
  226.         } else {
  227.           F_MNT_PATH=""
  228.           D_MNT_PATH=""
  229.        }
  230.        if (length(F_MNT_PATH)>0){
  231.            mount(MNT_PT,F_MNT_PATH)
  232.            if (system("cat /proc/mounts | grep -c -m1 " MNT_PT) > 0){
  233.              result=sucess }
  234.            else {
  235.              result=fail }
  236.        }
  237.        else if (length(D_MNT_PATH)>0) {
  238.           result=directory
  239.        } else {
  240.          result=fail
  241.        }
  242.        return result    
  243.    }
  244.  
  245.    /==mount==/ { mode=1 }
  246.    /==losetup==/ { mode=2 }
  247.    /==branches==/ { mode=3 }
  248.    {
  249.    if (mode == 1) {
  250.      # get list of mount points, types, and devices - index is $3 (mount points)
  251.      mountdev[$2]=$1
  252.      mounttypes[$2]=$3
  253.    } else if (mode == 2) {
  254.      # get list of loop devices and files - index is $1 (loop devs)
  255.      sub(/:/,"",$1)
  256.      sub(/.*\//,"",$3); sub(/)/,"",$3)
  257.      loopdev[$1]=$3
  258.    } else if (mode == 3) {
  259.      # map mount types to loop files if mount devices is a loop
  260.      for (m in mountdev) {
  261.        if ( loopdev[mountdev[m]] != "" ) mounttypes[m]=loopdev[mountdev[m]]
  262.      }
  263.      # for (m in mountdev) print m " on " mountdev[m] " type " mounttypes[m]
  264.      mode=4
  265.    } else if (mode==4) {
  266.      # print the branches and its mappings
  267.      if ($1 in mounttypes){
  268.        print $1, mounttypes[$1], "on"
  269.      }
  270.      else {
  271.        MNT_PT=$1
  272.        MNT_PATH=$2
  273.        if (length(MNT_PATH)==0){
  274.          MNT_PATH=$1
  275.        }
  276.        if (length(MNT_PATH) ==0){
  277.           next
  278.        }
  279.        mnt_status=mount_if_valid(MNT_PT,MNT_PATH,PDRV)
  280.        if(mnt_status == "fail"){
  281.            next
  282.        }
  283.        BNAME=MNT_PATH
  284.        sub(/.*\//,"",BNAME)    
  285.        if (NF>2){
  286.          STATE=$3
  287.        } else {
  288.          STATE="on"
  289.       }
  290.       if ("$STATE" !~ /(on|off)/){
  291.         STATE="on"
  292.       }
  293.       print MNT_PT, BNAME, STATE
  294.    }
  295.    }
  296.  }
  297.  '
  298.   )
  299.   echo "$items"
  300. }
  301. function mount_fn(){
  302.     FULL_PATH=$(realpath "$1")
  303.     FNAME="$(echo "$FULL_PATH" | sed 's#/#+')"
  304.     if [ ! -z "$2" ]; then
  305.       DIR_PATH=$2
  306.       case "FNAME" in
  307.       *.iso)
  308.          DIR_PATH=/media; ;;
  309.       *)
  310.         DIR_PATH=/iso; ;;
  311.       esac
  312.       MNT_PT="$(cat grep -m1 $FNAME)"
  313.       if [ -z "$MNT_PT" ]; then
  314.         MNT_PT="${DIR_PATH}/$FNAME"
  315.         mount $FULL_PATH "${MNT_PT}"
  316.      fi
  317.    fi
  318.   echo  "${MNT_PT}"
  319. }
  320. if [ -z "$items" ]; then
  321.  
  322.   for item_source in "${LAYER_SOURCES[@]}"; do
  323.   # 2. get branches, then map branches to mount types or loop devices
  324.     case "$item_source" in
  325.     input-file)
  326.   items+="$(get_items -f "$INPUT_FILE")"; ;;
  327.     union-record)
  328.        new_items=''
  329.        for rec in $LASTUNIONRECORD; do
  330.         if [ -f "$rec" ]; then
  331.           MNT_PT="$(mount_fm "$rec" )"
  332.           new_items+="'$MNT_PT', 'cu$rec', "'"on"'$'\n'
  333.         elif [ -f "$pdrv/$rec" ]; then
  334.           MNT_PT="$(mount_fm "$pdrv/$rec" )"
  335.           new_items+="'$MNT_PT', '$pdrv/$rec', "'"on"'$'\n'
  336.         fi
  337.       done
  338.       items+="$(get_items -f <<<"$new_items")"
  339.       ;;
  340.     extra-sfs)
  341.        new_items=''
  342.        for rec in $EXTRASFSLIST; do
  343.        if [ -f "$rec" ]; then
  344.           MNT_PT="$(mount_fm "$rec" )"
  345.           new_items+="'$MNT_PT', 'cu$rec', "'"on"'$'\n'
  346.         elif [ -f "$pdrv/$rec" ]; then
  347.           MNT_PT="$(mount_fm "$pdrv/$rec" )"
  348.           new_items+="'$MNT_PT', '$pdrv/$rec', "'"on"'$'\n'
  349.         fi
  350.       done
  351.       ;;
  352.     layer=*)
  353.       item_path="$(echo ${litem_source#*=})"
  354.       if [ -f "$item_path" ]; then
  355.         MNT_PT="$(mount_fm "$item_path" )"
  356.       elif [ -d "$item_path" ]; then  
  357.         MNT_PT="$item_path" #This isn't really a mount poing
  358.       elif [ ! -d  "$item_path" ]; then
  359.         echo "Warning  cannot mount $item_path"
  360.         continue
  361.       fi
  362.       items+="'$MNT_PT', '$item_path', "'"on"'$'\n'
  363.       ;;
  364.     psubdir)
  365.       [ -z "$DISTRO_PUPPYSFS" ] && DISTRO_PUPPYSFS=$(ls -1 /mnt/home | grep -m1 puppy_.*\.sfs$)
  366.       [ -z "$DISTRO_ZDRVSFS" ] && DISTRO_ZDRVSFS=$(ls -1 /mnt/home | grep -m1 zdrv.*\.sfs$)
  367.       [ -z "$DISTRO_FDRVSFS" ] && DISTRO_FDRVSFS=$(ls -1 /mnt/home | grep -m1 fdrv.*\.sfs$)
  368.       [ -z "$DISTRO_ADRVSFS" ] && DISTRO_ADRVSFS=$(ls -1 /mnt/home | grep -m1 fdrv.*\.sfs$)    
  369.       [ -z "$DISTRO_YDRVSFS" ] && DISTRO_YDRVSFS=$(ls -1 /mnt/home | grep -m1 fdrv.*\.sfs$)    
  370.       for rec in "$DISTRO_PUPPYSFS" "$DISTRO_ZDRVSFS" "$DISTRO_FDRVSFS" "$DISTRO_ADRVSFS" "$DISTRO_YDRVSFS"; do
  371.         items+="'$pdrv/$rec', '$rec', "'"on"'$'\n'  
  372.       done
  373.       ;;      
  374.     aufs)
  375.       items+="$(get_items)" ; ;;  
  376.     maybe-aufs)
  377.        [  -z "$items" ] && items+="$(get_items)"; ;;  
  378.   esac
  379.   items="$(echo "$items" | sed -n '/^\s*\(on\)\?\s*$/! p' | sed -n '/^Error: Expected on/! p' | sed -n '/^Use --help on/! p')"
  380.   done
  381. fi
  382. # 3. Ask user to choose the SFS
  383. dialog --separate-output --backtitle "tmpfs sandbox" --title "sandbox config" \
  384.     --checklist "Choose which SFS you want to use" 0 0 0 $items 2> $TMPFILE
  385. chosen="$(cat $TMPFILE)"
  386.  
  387. clear
  388. if [ -z "$chosen" ]; then
  389.     echo "Cancelled or no SFS is chosen - exiting."
  390.     exit 1
  391. fi
  392.  
  393. if [ ! -z "$OUTPUT_FILE" ]; then
  394.   cp "$TMPFILE" "$OUTPUT_FILE"
  395.   if [ ! "$NO_EXIT" = true ]; then
  396.     exit 0
  397.   fi
  398. fi
  399. # 0.5 is this the first sandbox? If not, then create another name for mountpoints
  400. if grep -q $FAKEROOT /proc/mounts; then
  401.     FAKEROOT=$(mktemp -d -p $SANDBOX_ROOT ${FAKEROOT##*/}.XXXXXXX)
  402.     SANDBOX_ID=".${FAKEROOT##*.}"
  403.     SANDBOX_TMPFS=$SANDBOX_ROOT/${SANDBOX_TMPFS##*/}${SANDBOX_ID}
  404.     rmdir $FAKEROOT
  405. fi
  406.  
  407.  
  408.  
  409. # 4. convert chosen SFS to robranches
  410. robranches=""
  411. for a in $(cat $TMPFILE) ; do
  412.     robranches=$robranches:$a=ro
  413. done
  414. rm $TMPFILE
  415.  
  416. # 5. make the mountpoints if not exist  yet
  417. mkdir -p $FAKEROOT $SANDBOX_TMPFS
  418.  
  419. # 6. do the magic - mount the tmpfs first, and then the rest with aufs
  420. if mount -t tmpfs none $SANDBOX_TMPFS; then
  421.     if mount -t aufs -o "br:$SANDBOX_TMPFS=rw$robranches" aufs $FAKEROOT; then
  422.         # 5. record our new aufs-root-id so tools don't hack real filesystem   
  423.         SANDBOX_AUFS_ID=$(grep $FAKEROOT /proc/mounts | sed 's/.*si=/si_/; s/ .*//') #'
  424.         sed -i -e '/AUFS_ROOT_ID/ d' $FAKEROOT/etc/BOOTSTATE 2> /dev/null
  425.         echo AUFS_ROOT_ID=$SANDBOX_AUFS_ID >> $FAKEROOT/etc/BOOTSTATE
  426.        
  427.         # 7. sandbox is ready, now just need to mount other supports - pts, proc, sysfs, usb and tmp
  428.         mkdir -p $FAKEROOT/dev $FAKEROOT/sys $FAKEROOT/proc $FAKEROOT/tmp
  429.         mount -o rbind /dev $FAKEROOT/dev
  430.         mount -t sysfs none $FAKEROOT/sys
  431.         mount -t proc none $FAKEROOT/proc
  432.         mount -o bind /tmp $FAKEROOT/tmp
  433.         mkdir -p $FAKEROOT/$SANDBOX_TMPFS
  434.         mount -o bind $SANDBOX_TMPFS $FAKEROOT/$SANDBOX_TMPFS   # so we can access it within sandbox
  435.        
  436.         # 8. optional copy, to enable running sandbox-ed xwin
  437.         cp /usr/share/sandbox/* $FAKEROOT/usr/bin 2> /dev/null
  438.        
  439.         # 9. make sure we identify ourself as in sandbox - and we're good to go!
  440.         echo -e '\nexport PS1="sandbox'${SANDBOX_ID}'# "' >> $FAKEROOT/etc/shinit #fatdog 600
  441.         sed -i -e '/^PS1/ s/^.*$/PS1="sandbox'${SANDBOX_ID}'# "/' $FAKEROOT/etc/profile # earlier fatdog
  442.         echo "Starting sandbox now."
  443.         if [ $USE_NS ]; then
  444.             unshare -f -p --mount-proc=$FAKEROOT/proc chroot $FAKEROOT
  445.         else
  446.             chroot $FAKEROOT
  447.         fi
  448.  
  449.         # 10. done - clean up everything
  450.         umountall
  451.         echo "Leaving sandbox."
  452.     else
  453.         echo "Unable to mount aufs br:$SANDBOX_TMPFS=rw$robranches"
  454.         umount -l $SANDBOX_TMPFS       
  455.     fi
  456. else
  457.     echo "unable to mount tmpfs."
  458. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement