Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- #-t --tmpfs path to temporary save file
- #-r --root Parent directory to the new rootfs. Only applied to options which are to the right of this option.
- #-f --fake-root This is the location where the fake (or new) rootfs will be mounted.
- #-n --new-root See: -f --fake-root
- # This is a subdirectory of the -r|--root option when the -r --root option is applied to the left of this argument
- unset SANDBOX_ID
- uset rwbranch
- #SANDBOX_ROOT=/mnt/sb
- SANDBOX_ROOT=""
- function choose_save(){
- # location not specified - then ask
- dialog --backtitle "rw image sandbox" --title "choose rw image" \
- --extra-button --extra-label "Create" --ok-label "Locate" \
- --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
- case $? in
- 0) # ok - locate
- dialog --backtitle "rw image sandbox" --title "Specify location of existing rw image" --fselect `pwd` 8 60 2> $TMPFILE
- savebranch=`cat $TMPFILE`
- rm $TMPFILE
- if [ -n "$savebranch" ]; then
- if [ -d "$savebranch" ]; then
- SANDBOX_IMG=$savebranch
- elif [ ! -f "$savebranch" ]; then
- echo "$savebranch doesn't exist - exiting."
- exit
- fi
- else
- echo "You didn't specify any file or you pressed cancel. Exiting."
- exit
- fi
- ;;
- 3) # create
- echo "create"
- dialog --backtitle "save image sandbox" --title "Specify name and path of new the file" --fselect `pwd` 8 60 2> $TMPFILE
- savebranch=`cat $TMPFILE`
- rm $TMPFILE
- if [ -n "$savebranch" ]; then
- if [ -f "$savebranch" ]; then
- echo "$savebranch already exist - exiting."
- exit
- else
- # get the size
- dialog --title "Create new save image" --inputbox "Specify size (in megabytes)" 0 40 100 2> $TMPFILE
- size=`cat $TMPFILE`
- rm $TMPFILE
- if [ -n "$size" ]; then
- if dd if=/dev/zero of="$savebranch" bs=1 count=0 seek="$size"M; then
- if ! mke2fs -F "$savebranch"; then
- echo "I fail to make an ext2 filesystem at $savebranch, exiting."
- exit
- fi
- else
- echo "I fail to create a ${size}M file at $savebranch,, exiting."
- exit
- fi
- else
- echo "You didn't specify the size or your press cancel. Exiting."
- exit
- fi
- fi
- else
- echo "You didn't specify any file or you pressed cancel. Exiting."
- exit
- fi
- ;;
- 1 | 255) # 1 is cancel, 255 is Escape
- ;&
- *) # invalid input - treat as cancel
- echo "Cancelled - exiting."
- exit
- ;;
- esac
- }
- declare -a options="$(getopt -o t::,r::,s: --long input-file:tmpfs::,root::,save: -- "$@")"
- eval set --"$options"
- while [ $# -gt 0 ]; do
- case "$1" in
- -t|--tmpfs)
- if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
- SANDBOX_TMPFS="$2"
- shift 2; ;;
- else
- [ -z "$TMPFILE" ] && TMPFILE=$(mktemp -p /tmp)
- SANDBOX_TMPFS=$SANDBOX_ROOT/sandbox
- shift 1
- fi; ;;
- -r|--root)
- if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
- SANDBOX_ROOT="$2"
- shift 2; ;;
- else
- SANDBOX_ROOT=/mnt/sb
- shift 1
- fi; ;;
- -f|-n|--fake-root|--new-root)
- if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
- FAKEROOT="$2"
- shift 2
- else
- FAKEROOT=fakeroot
- shift 1
- fi; ;;
- if [ ! -z "$SANDBOX_ROOT" ]; then
- FAKEROOT=$SANDBOX_ROOT/$FAKEROOT
- fi
- -s|--save) #$SANDBOX_IMG
- #if [ $# -gt 1 ] && [[ ! "$2" = --* ]] && [ ! -z "$2" ]; then
- if [ -d "$2" ]; then
- SANDBOX_IMG=$2
- savebranch=$2
- else [ -f "$2" ]; then
- #mnt_sb_immage
- #mount -o loop "$rwbranch" $SANDBOX_IMG;
- savebranch=$1
- loop=$(losetup-FULL -a) | grep "$savebranch" | sed "s/:.*$//")
- if [ ! -z "$loop" ]; then
- SANDBOX_IMG=$(/proc/mounts | grep $loop | cut -d " " -f2)
- fi
- shift 2; ;;
- fi
- #else
- # SANDBOX_ROOT=/mnt/sb
- # #${var+x} https://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash
- # shift 1
- #fi; ;;
- esac
- done
- if [ ! -z SANDBOX_ROOT ]; then
- [ -z "$FAKEROOT" ] && FAKEROOT=$SANDBOX_ROOT/fakeroot
- if grep -q $FAKEROOT /proc/mounts; then
- FAKEROOT=$(mktemp -d -p $(SANDBOX_ROOT:-/) ${FAKEROOT##*/}.XXXXXXX)
- SANDBOX_ID=".${FAKEROOT##*.}"
- if [ -z "$SANDBOX_IMG" ]; then
- if grep -q $SANDBOX_IMG /proc/mounts; then
- SANDBOX_IMG=$SANDBOX_ROOT/${SANDBOX_IMG##*/}${SANDBOX_ID}
- fi
- fi
- rmdir $FAKEROOT
- fi
- else
- echo ("Warning sandbox root not defined")
- #[ -z "$FAKEROOT" ] && FAKEROOT=/
- fi
- #SANDBOX_TMPFS=$SANDBOX_ROOT/sandbox # mounted rw location of
- #SANDBOX_TMPFS=$SANDBOX_ROOT/initrd/mnt/tmpfs/pup_rw
- #tmpfs used for sandbox
- #SANDBOX_ID=
- TMPFILE=$(mktemp -p /tmp)
- # use namespaces if available
- #[ -e /proc/1/ns/pid ] && [ -e /proc/1/ns/mnt ] && type unshare >/dev/null && USE_NS=1
- # umount all if we are accidentally killed
- #trap 'umountall' 1
- #s243a don't unmount on error
- # 0.1 must be root
- if [ $(id -u) -ne 0 ]; then
- echo "You must be root to use sandbox."
- exit
- fi
- ## 0.2 cannot launch sandbox within sandbox
- #if [ "$AUFS_ROOT_ID" != "" ] ; then
- # grep -q $SANDBOX_ROOT /sys/fs/aufs/$AUFS_ROOT_ID/br0 &&
- # echo "Cannot launch sandbox within sandbox." && exit
- #fi
- # s243a we are remounting everything rather then creating a sandbox.
- # 0.3 help
- case "$1" in
- --help|-h)
- echo "Usage: ${0##*/}"
- echo "Starts an in-memory (throwaway) sandbox. Type 'exit' to leave."
- exit
- esac
- ## 0.4 if not running from terminal but in Xorg, then launch via terminal
- #! [ -t 0 ] && [ -n "$DISPLAY" ] && exec $XTERM -e "$0" "$@"
- #! [ -t 0 ] && exit
- ## 0.5 is this the first sandbox? If not, then create another name for mountpoints
- #if grep -q $FAKEROOT /proc/mounts; then
- # FAKEROOT=$(mktemp -d -p $SANDBOX_ROOT ${FAKEROOT##*/}.XXXXXXX)
- # SANDBOX_ID=".${FAKEROOT##*.}"
- # SANDBOX_TMPFS=$SANDBOX_ROOT/${SANDBOX_TMPFS##*/}${SANDBOX_ID}
- # rmdir $FAKEROOT
- #fi
- # 1. get aufs system-id for the root filesystem
- if [ -z "$AUFS_ROOT_ID" ] ; then
- AUFS_ROOT_ID=$(
- awk '{ if ($2 == "/" && $3 == "aufs") { match($4,/si=[0-9a-f]*/); print "si_" substr($4,RSTART+3,RLENGTH-3) } }' /proc/mounts
- )
- fi
- # 2. get branches, then map branches to mount types or loop devices
- items=$(
- { echo ==mount==; cat /proc/mounts;
- echo ==losetup==; losetup-FULL -a;
- echo ==branches==; ls -v /sys/fs/aufs/$AUFS_ROOT_ID/br[0-9]* | xargs sed 's/=.*//'; } | \
- awk '
- /==mount==/ { mode=1 }
- /==losetup==/ { mode=2 }
- /==branches==/ { mode=3 }
- {
- if (mode == 1) {
- # get list of mount points, types, and devices - index is $3 (mount points)
- mountdev[$2]=$1
- mounttypes[$2]=$3
- } else if (mode == 2) {
- # get list of loop devices and files - index is $1 (loop devs)
- sub(/:/,"",$1)
- sub(/.*\//,"",$3); sub(/)/,"",$3)
- loopdev[$1]=$3
- } else if (mode == 3) {
- # map mount types to loop files if mount devices is a loop
- for (m in mountdev) {
- if ( loopdev[mountdev[m]] != "" ) mounttypes[m]=loopdev[mountdev[m]]
- }
- # for (m in mountdev) print m " on " mountdev[m] " type " mounttypes[m]
- mode=4
- } else if (mode==4) {
- # print the branches and its mappings
- if ($0 in mounttypes){
- print $0, mounttypes[$0], "on"
- }
- else {
- MNT_PATH=$0
- sub(/^.*[\/]/,"")
- print MNT_PATH, $0, "on"
- }
- }
- }
- '
- )
- # '
- # 3. Ask user to choose the SFS
- dialog --separate-output --backtitle "tmpfs sandbox" --title "sandbox config" \
- --checklist "Choose which SFS you want to use" 0 0 0 $items 2> $TMPFILE
- chosen="$(cat $TMPFILE)"
- clear
- if [ -z "$chosen" ]; then
- echo "Cancelled or no SFS is chosen - exiting."
- exit 1
- fi
- # 4. convert chosen SFS to robranches
- robranches=""
- for a in $(cat $TMPFILE) ; do
- robranches=$robranches:$a=ro
- done
- rm $TMPFILE
- # 5. get location of rw image
- if [ -z "$savebranch" ]; then
- choose_save
- loop=$(losetup-FULL -a) | grep "$savebranch" | sed "s/:.*$//")
- if [ ! -z "$loop" ]; then
- SANDBOX_IMG=$(/proc/mounts | grep $loop | cut -d " " -f2)
- mount -o loop "$savebranch" $SANDBOX_IMG
- fi
- fi
- if [ -z "$rwdir" ] ; then
- if [ -z "$PUPMODE" ];
- rwdir=$SANDBOX_IMG
- else
- if [ $PUPMODE -ne 5 ] && [ $PUPMODE -ne 13 ] && [ $PUPMODE -ne 77 ]; then
- fi
- fi
- fi
- # 4. make the mountpoints if not exist yet
- if [ -z "$SANDBOX_IMG" ]; then
- mkdir -p $SANDBOX_IMG
- mount -o loop "$savebranch" $SANDBOX_IMG || {
- echo "Failed to mount '$savebranch' at '$SANDBOX_IMG'"
- exit
- }
- fi
- if [ -z "$rwbranch" ]; then
- [ -z "$SANDBOX_TMPFS" ] && SANDBOX_TMPFS=$SANDBOX_ROOT/sandbox
- if grep -q $FAKEROOT /proc/mounts; then
- FAKEROOT=$(mktemp -d -p $SANDBOX_ROOT ${FAKEROOT##*/}.XXXXXXX)
- SANDBOX_ID=".${FAKEROOT##*.}"
- SANDBOX_TMPFS=$SANDBOX_ROOT/${SANDBOX_TMPFS##*/}${SANDBOX_ID}
- rmdir $FAKEROOT
- fi
- fi
- mkdir -p $FAKEROOT $SANDBOX_IMG
- # 5. do the magic - mount the rw image first, and then the rest with aufs
- #if mount -o loop "$rwbranch" $SANDBOX_IMG; then
- if mount -t aufs -o "br:$SANDBOX_IMG=rw$robranches" aufs $FAKEROOT; then
- # 5. record our new aufs-root-id so tools don't hack real filesystem
- SANDBOX_AUFS_ID=$(grep $FAKEROOT /proc/mounts | sed 's/.*si=/si_/; s/ .*//') #'
- sed -i -e '/AUFS_ROOT_ID/ d' $FAKEROOT/etc/BOOTSTATE 2> /dev/null
- echo AUFS_ROOT_ID=$SANDBOX_AUFS_ID >> $FAKEROOT/etc/BOOTSTATE
- # 5. sandbox is ready, now just need to mount other supports - pts, proc, sysfs, usb and tmp
- mkdir -p $FAKEROOT/dev $FAKEROOT/sys $FAKEROOT/proc $FAKEROOT/tmp
- mount -o rbind /dev $FAKEROOT/dev
- mount -t sysfs none $FAKEROOT/sys
- mount -t proc none $FAKEROOT/proc
- mount -o bind /tmp $FAKEROOT/tmp
- mkdir -p $FAKEROOT/$SANDBOX_IMG
- mount -o bind $SANDBOX_IMG $FAKEROOT/$SANDBOX_IMG # so we can access it within sandbox
- # 6. optional copy, to enable running sandbox-ed xwin
- cp /usr/share/sandbox/* $FAKEROOT/usr/bin 2> /dev/null
- # 7. make sure we identify ourself as in sandbox - and we're good to go!
- echo -e '\nexport PS1="sandbox'${SANDBOX_ID}'# "' >> $FAKEROOT/etc/shinit #fatdog 600
- sed -i -e '/^PS1/ s/^.*$/PS1="sandbox'${SANDBOX_ID}'# "/' $FAKEROOT/etc/profile # earlier fatdog
- echo "Starting sandbox now."
- if [ $USE_NS ]; then
- unshare -f -p --mount-proc=$FAKEROOT/proc chroot $FAKEROOT
- else
- chroot $FAKEROOT
- fi
- # 8. done - clean up everything
- umountall
- echo "Leaving sandbox."
- else
- echo "Unable to mount aufs br:$SANDBOX_IMG=rw$robranches"
- umount -l $SANDBOX_IMG
- fi
- #else
- # echo "unable to mount rw image: $rwbranch"
- #fi
Advertisement
Add Comment
Please, Sign In to add comment