s243a

Remount_save.sh (draft)

Nov 27th, 2020 (edited)
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.59 KB | None | 0 0
  1. #!/bin/sh
  2. #-t --tmpfs  path to temporary save file
  3. #-r --root   Parent directory to the new rootfs. Only applied to options which are to the right of this option.  
  4. #-f --fake-root This is the location where the fake (or new) rootfs will be mounted.
  5. #-n --new-root See: -f --fake-root
  6. #   This is a subdirectory of the -r|--root option when the -r --root option is applied to the left of this argument
  7. unset SANDBOX_ID
  8. uset rwbranch
  9. #SANDBOX_ROOT=/mnt/sb
  10. SANDBOX_ROOT=""
  11.  
  12. function choose_save(){
  13.     # location not specified - then ask
  14.     dialog --backtitle "rw image sandbox" --title "choose rw image" \
  15.     --extra-button --extra-label "Create" --ok-label "Locate" \
  16.     --yesno "You didn't specify the location of the rw image file. Do you want to locate existing file, or do you want to create a new one?" 0 0
  17.     case $? in
  18.         0) # ok - locate
  19.             dialog --backtitle "rw image sandbox" --title "Specify location of existing rw image" --fselect `pwd` 8 60 2> $TMPFILE
  20.             savebranch=`cat $TMPFILE`
  21.             rm $TMPFILE
  22.             if [ -n "$savebranch" ]; then
  23.                 if [ -d "$savebranch" ]; then
  24.                   SANDBOX_IMG=$savebranch
  25.                 elif [ ! -f "$savebranch" ]; then
  26.                     echo "$savebranch doesn't exist - exiting."
  27.                     exit                   
  28.                 fi
  29.             else
  30.                 echo "You didn't specify any file or you pressed cancel. Exiting."
  31.                 exit
  32.             fi
  33.             ;;
  34.         3) # create
  35.             echo "create"
  36.             dialog --backtitle "save image sandbox" --title "Specify name and path of new the file" --fselect `pwd` 8 60 2> $TMPFILE
  37.             savebranch=`cat $TMPFILE`
  38.             rm $TMPFILE
  39.             if [ -n "$savebranch" ]; then
  40.                 if [ -f "$savebranch" ]; then
  41.                     echo "$savebranch already exist - exiting."
  42.                     exit
  43.                 else
  44.                     # get the size
  45.                     dialog --title "Create new save image" --inputbox "Specify size (in megabytes)" 0 40 100 2> $TMPFILE
  46.                     size=`cat $TMPFILE`
  47.                     rm $TMPFILE
  48.                    
  49.                     if [ -n "$size" ]; then
  50.                         if dd if=/dev/zero of="$savebranch" bs=1 count=0 seek="$size"M; then
  51.                             if ! mke2fs -F "$savebranch"; then
  52.                                 echo "I fail to make an ext2 filesystem at $savebranch, exiting."
  53.                                 exit
  54.                             fi
  55.                         else
  56.                             echo "I fail to create a ${size}M file at $savebranch,, exiting."
  57.                             exit
  58.                         fi
  59.                     else
  60.                         echo "You didn't specify the size or your press cancel. Exiting."
  61.                         exit
  62.                     fi                 
  63.                 fi
  64.             else
  65.                 echo "You didn't specify any file or you pressed cancel. Exiting."
  66.                 exit
  67.             fi
  68.             ;;
  69.         1 | 255) # 1 is cancel, 255 is Escape
  70.             ;&
  71.         *) # invalid input - treat as cancel
  72.             echo "Cancelled - exiting."
  73.             exit
  74.             ;;
  75.     esac
  76. }
  77.  
  78.  
  79. declare -a options="$(getopt -o t::,r::,s: --long input-file:tmpfs::,root::,save: -- "$@")"
  80. eval set --"$options"
  81. while [ $# -gt 0 ]; do
  82.   case "$1" in
  83.   -t|--tmpfs)
  84.     if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  85.       SANDBOX_TMPFS="$2"
  86.       shift 2; ;;
  87.     else
  88.       [ -z "$TMPFILE" ] && TMPFILE=$(mktemp -p /tmp)
  89.       SANDBOX_TMPFS=$SANDBOX_ROOT/sandbox
  90.       shift 1
  91.     fi; ;;
  92.    -r|--root)
  93.     if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  94.       SANDBOX_ROOT="$2"
  95.       shift 2; ;;
  96.     else
  97.       SANDBOX_ROOT=/mnt/sb
  98.       shift 1
  99.     fi; ;;  
  100.    -f|-n|--fake-root|--new-root)
  101.       if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  102.         FAKEROOT="$2"
  103.         shift 2
  104.       else
  105.         FAKEROOT=fakeroot
  106.         shift 1
  107.       fi; ;;  
  108.       if [ ! -z "$SANDBOX_ROOT" ]; then
  109.         FAKEROOT=$SANDBOX_ROOT/$FAKEROOT
  110.       fi
  111.  
  112.    -s|--save) #$SANDBOX_IMG
  113.     #if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
  114.       if [ -d "$2" ]; then
  115.         SANDBOX_IMG=$2
  116.         savebranch=$2
  117.       else [ -f "$2" ]; then
  118.         #mnt_sb_immage
  119.         #mount -o loop "$rwbranch" $SANDBOX_IMG;
  120.         savebranch=$1
  121.         loop=$(losetup-FULL -a) | grep  "$savebranch"  | sed "s/:.*$//")
  122.         if [ ! -z "$loop" ]; then
  123.           SANDBOX_IMG=$(/proc/mounts | grep $loop | cut -d " " -f2)
  124.         fi
  125.         shift 2; ;;
  126.       fi
  127.     #else
  128.     #  SANDBOX_ROOT=/mnt/sb
  129.     #  #${var+x} https://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash
  130.     #  shift 1
  131.     #fi; ;;        
  132.   esac
  133. done
  134.  
  135. if [ ! -z SANDBOX_ROOT ]; then
  136.   [ -z "$FAKEROOT" ] && FAKEROOT=$SANDBOX_ROOT/fakeroot
  137.   if grep -q $FAKEROOT /proc/mounts; then
  138.     FAKEROOT=$(mktemp -d -p $(SANDBOX_ROOT:-/) ${FAKEROOT##*/}.XXXXXXX)
  139.     SANDBOX_ID=".${FAKEROOT##*.}"
  140.     if [ -z "$SANDBOX_IMG" ]; then
  141.       if grep -q $SANDBOX_IMG /proc/mounts; then
  142.         SANDBOX_IMG=$SANDBOX_ROOT/${SANDBOX_IMG##*/}${SANDBOX_ID}
  143.       fi
  144.     fi
  145.     rmdir $FAKEROOT
  146.   fi
  147. else
  148.   echo ("Warning sandbox root not defined")
  149.   #[ -z "$FAKEROOT" ] && FAKEROOT=/
  150. fi
  151.  
  152.  
  153. #SANDBOX_TMPFS=$SANDBOX_ROOT/sandbox # mounted rw location of
  154. #SANDBOX_TMPFS=$SANDBOX_ROOT/initrd/mnt/tmpfs/pup_rw
  155. #tmpfs used for sandbox
  156. #SANDBOX_ID=
  157. TMPFILE=$(mktemp -p /tmp)
  158. # use namespaces if available
  159. #[ -e /proc/1/ns/pid ] && [ -e /proc/1/ns/mnt ] && type unshare >/dev/null && USE_NS=1
  160.  
  161. # umount all if we are accidentally killed
  162. #trap 'umountall' 1
  163. #s243a don't unmount on error
  164.  
  165. # 0.1 must be root
  166. if [ $(id -u) -ne 0 ]; then
  167.     echo "You must be root to use sandbox."
  168.     exit
  169. fi
  170.  
  171. ## 0.2 cannot launch sandbox within sandbox
  172. #if [ "$AUFS_ROOT_ID" != "" ] ; then
  173. #   grep -q $SANDBOX_ROOT /sys/fs/aufs/$AUFS_ROOT_ID/br0 &&
  174. #       echo "Cannot launch sandbox within sandbox." && exit
  175. #fi
  176. # s243a we are remounting everything rather then creating a sandbox.
  177.  
  178. # 0.3 help
  179. case "$1" in
  180.     --help|-h)
  181.     echo "Usage: ${0##*/}"
  182.     echo "Starts an in-memory (throwaway) sandbox. Type 'exit' to leave."
  183.     exit
  184. esac
  185.  
  186. ## 0.4 if not running from terminal but in Xorg, then launch via terminal
  187. #! [ -t 0 ] && [ -n "$DISPLAY" ] && exec $XTERM -e "$0" "$@"
  188. #! [ -t 0 ] && exit
  189.  
  190. ## 0.5 is this the first sandbox? If not, then create another name for mountpoints
  191. #if grep -q $FAKEROOT /proc/mounts; then
  192. #   FAKEROOT=$(mktemp -d -p $SANDBOX_ROOT ${FAKEROOT##*/}.XXXXXXX)
  193. #   SANDBOX_ID=".${FAKEROOT##*.}"
  194. #   SANDBOX_TMPFS=$SANDBOX_ROOT/${SANDBOX_TMPFS##*/}${SANDBOX_ID}
  195. #   rmdir $FAKEROOT
  196. #fi
  197.  
  198. # 1. get aufs system-id for the root filesystem
  199. if [ -z "$AUFS_ROOT_ID" ] ; then
  200.     AUFS_ROOT_ID=$(
  201.         awk '{ if ($2 == "/" && $3 == "aufs") { match($4,/si=[0-9a-f]*/); print "si_" substr($4,RSTART+3,RLENGTH-3) } }' /proc/mounts
  202.     )
  203. fi
  204.  
  205. # 2. get branches, then map branches to mount types or loop devices
  206. items=$(
  207. { echo ==mount==; cat /proc/mounts;
  208.   echo ==losetup==; losetup-FULL -a;
  209.   echo ==branches==; ls -v /sys/fs/aufs/$AUFS_ROOT_ID/br[0-9]* | xargs sed 's/=.*//'; } | \
  210.   awk '
  211.  /==mount==/ { mode=1 }
  212.  /==losetup==/ { mode=2 }
  213.  /==branches==/ { mode=3 }
  214.  {
  215.     if (mode == 1) {
  216.         # get list of mount points, types, and devices - index is $3 (mount points)
  217.         mountdev[$2]=$1
  218.         mounttypes[$2]=$3
  219.     } else if (mode == 2) {
  220.         # get list of loop devices and files - index is $1 (loop devs)
  221.         sub(/:/,"",$1)
  222.         sub(/.*\//,"",$3); sub(/)/,"",$3)
  223.         loopdev[$1]=$3
  224.     } else if (mode == 3) {
  225.         # map mount types to loop files if mount devices is a loop
  226.         for (m in mountdev) {
  227.             if ( loopdev[mountdev[m]] != "" ) mounttypes[m]=loopdev[mountdev[m]]
  228.         }
  229.         # for (m in mountdev) print m " on " mountdev[m] " type " mounttypes[m]
  230.         mode=4
  231.     } else if (mode==4) {
  232.         # print the branches and its mappings
  233.         if ($0 in mounttypes){
  234.           print $0, mounttypes[$0], "on"
  235.         }
  236.         else {
  237.             MNT_PATH=$0
  238.             sub(/^.*[\/]/,"")
  239.             print MNT_PATH, $0, "on"
  240.         }
  241.     }
  242.  }  
  243. '
  244. )
  245. # '
  246.  
  247. # 3. Ask user to choose the SFS
  248. dialog --separate-output --backtitle "tmpfs sandbox" --title "sandbox config" \
  249.     --checklist "Choose which SFS you want to use" 0 0 0 $items 2> $TMPFILE
  250. chosen="$(cat $TMPFILE)"
  251.  
  252. clear
  253. if [ -z "$chosen" ]; then
  254.     echo "Cancelled or no SFS is chosen - exiting."
  255.     exit 1
  256. fi
  257.  
  258. # 4. convert chosen SFS to robranches
  259. robranches=""
  260. for a in $(cat $TMPFILE) ; do
  261.     robranches=$robranches:$a=ro
  262. done
  263. rm $TMPFILE
  264.  
  265. # 5. get location of rw image
  266. if [ -z "$savebranch"  ]; then
  267.   choose_save
  268.    loop=$(losetup-FULL -a) | grep  "$savebranch"  | sed "s/:.*$//")
  269.    if [ ! -z "$loop" ]; then
  270.           SANDBOX_IMG=$(/proc/mounts | grep $loop | cut -d " " -f2)
  271.           mount -o loop "$savebranch" $SANDBOX_IMG
  272.     fi
  273. fi
  274. if  [ -z "$rwdir" ] ; then
  275.   if [ -z "$PUPMODE" ];
  276.     rwdir=$SANDBOX_IMG
  277.   else
  278.     if [ $PUPMODE  -ne 5 ] && [ $PUPMODE  -ne 13 ] && [ $PUPMODE  -ne 77 ]; then
  279.      
  280.     fi
  281.   fi
  282. fi
  283. # 4. make the mountpoints if not exist  yet
  284. if [ -z "$SANDBOX_IMG" ]; then
  285.  mkdir -p $SANDBOX_IMG
  286.  mount -o loop "$savebranch" $SANDBOX_IMG || {
  287.      echo "Failed to mount '$savebranch' at '$SANDBOX_IMG'"
  288.      exit
  289.  }
  290. fi
  291. if [ -z  "$rwbranch" ]; then
  292.   [ -z "$SANDBOX_TMPFS" ] && SANDBOX_TMPFS=$SANDBOX_ROOT/sandbox
  293.   if grep -q $FAKEROOT /proc/mounts; then
  294.     FAKEROOT=$(mktemp -d -p $SANDBOX_ROOT ${FAKEROOT##*/}.XXXXXXX)
  295.     SANDBOX_ID=".${FAKEROOT##*.}"
  296.     SANDBOX_TMPFS=$SANDBOX_ROOT/${SANDBOX_TMPFS##*/}${SANDBOX_ID}
  297.     rmdir $FAKEROOT
  298. fi
  299.  
  300. fi
  301.  
  302. mkdir -p $FAKEROOT $SANDBOX_IMG
  303.  
  304. # 5. do the magic - mount the rw image first, and then the rest with aufs
  305. #if mount -o loop "$rwbranch" $SANDBOX_IMG; then
  306.     if mount -t aufs -o "br:$SANDBOX_IMG=rw$robranches" aufs $FAKEROOT; then
  307.         # 5. record our new aufs-root-id so tools don't hack real filesystem
  308.         SANDBOX_AUFS_ID=$(grep $FAKEROOT /proc/mounts | sed 's/.*si=/si_/; s/ .*//') #'
  309.         sed -i -e '/AUFS_ROOT_ID/ d' $FAKEROOT/etc/BOOTSTATE 2> /dev/null
  310.         echo AUFS_ROOT_ID=$SANDBOX_AUFS_ID >> $FAKEROOT/etc/BOOTSTATE
  311.        
  312.         # 5. sandbox is ready, now just need to mount other supports - pts, proc, sysfs, usb and tmp
  313.         mkdir -p $FAKEROOT/dev $FAKEROOT/sys $FAKEROOT/proc $FAKEROOT/tmp
  314.         mount -o rbind /dev $FAKEROOT/dev
  315.         mount -t sysfs none $FAKEROOT/sys
  316.         mount -t proc none $FAKEROOT/proc
  317.         mount -o bind /tmp $FAKEROOT/tmp
  318.         mkdir -p $FAKEROOT/$SANDBOX_IMG
  319.         mount -o bind $SANDBOX_IMG $FAKEROOT/$SANDBOX_IMG   # so we can access it within sandbox
  320.        
  321.         # 6. optional copy, to enable running sandbox-ed xwin
  322.         cp /usr/share/sandbox/* $FAKEROOT/usr/bin 2> /dev/null
  323.        
  324.         # 7. make sure we identify ourself as in sandbox - and we're good to go!
  325.         echo -e '\nexport PS1="sandbox'${SANDBOX_ID}'# "' >> $FAKEROOT/etc/shinit #fatdog 600
  326.         sed -i -e '/^PS1/ s/^.*$/PS1="sandbox'${SANDBOX_ID}'# "/' $FAKEROOT/etc/profile # earlier fatdog
  327.         echo "Starting sandbox now."
  328.         if [ $USE_NS ]; then
  329.             unshare -f -p --mount-proc=$FAKEROOT/proc chroot $FAKEROOT
  330.         else
  331.             chroot $FAKEROOT
  332.         fi
  333.  
  334.         # 8. done - clean up everything
  335.         umountall
  336.         echo "Leaving sandbox."
  337.     else
  338.         echo "Unable to mount aufs br:$SANDBOX_IMG=rw$robranches"
  339.         umount -l $SANDBOX_IMG 
  340.     fi
  341. #else
  342. #   echo "unable to mount rw image: $rwbranch"
  343. #fi
  344.  
  345.  
Advertisement
Add Comment
Please, Sign In to add comment