Advertisement
s243a

sandbox_s243a_debug3.sh

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