Advertisement
s243a

sandbox.sh (fixed?)

Jan 15th, 2020
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. #!/bin/sh
  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. XTERM=urxvt
  20. SANDBOX_ROOT=/mnt/sb
  21. FAKEROOT=$SANDBOX_ROOT/fakeroot # mounted chroot location of sandbox - ie, the fake root
  22. SANDBOX_TMPFS=$SANDBOX_ROOT/sandbox # mounted rw location of tmpfs used for sandbox
  23. SANDBOX_ID=
  24. TMPFILE=$(mktemp -p /tmp)
  25. # use namespaces if available
  26. #[ -e /proc/1/ns/pid ] && [ -e /proc/1/ns/mnt ] && type unshare >/dev/null && USE_NS=1
  27.  
  28. # umount all if we are accidentally killed
  29. trap 'umountall' 1
  30. umountall() {
  31. {
  32. umount -l $FAKEROOT/$SANDBOX_TMPFS
  33. umount -l $FAKEROOT/tmp
  34. umount -l $FAKEROOT/proc
  35. umount -l $FAKEROOT/sys
  36. umount -l $FAKEROOT/dev
  37.  
  38. umount -l $FAKEROOT
  39. umount -l $SANDBOX_TMPFS
  40. rmdir $FAKEROOT
  41. rmdir $SANDBOX_TMPFS
  42. } 2> /dev/null
  43. }
  44.  
  45. # 0.1 must be root
  46. if [ $(id -u) -ne 0 ]; then
  47. echo "You must be root to use sandbox."
  48. exit
  49. fi
  50.  
  51. # 0.2 cannot launch sandbox within sandbox
  52. if [ "$AUFS_ROOT_ID" != "" ] ; then
  53. grep -q $SANDBOX_ROOT /sys/fs/aufs/$AUFS_ROOT_ID/br0 &&
  54. echo "Cannot launch sandbox within sandbox." && exit
  55. fi
  56.  
  57. # 0.3 help
  58. case "$1" in
  59. --help|-h)
  60. echo "Usage: ${0##*/}"
  61. echo "Starts an in-memory (throwaway) sandbox. Type 'exit' to leave."
  62. exit
  63. esac
  64.  
  65. # 0.4 if not running from terminal but in Xorg, then launch via terminal
  66. ! [ -t 0 ] && [ -n "$DISPLAY" ] && exec $XTERM -e "$0" "$@"
  67. ! [ -t 0 ] && exit
  68.  
  69. # 0.5 is this the first sandbox? If not, then create another name for mountpoints
  70. if grep -q $FAKEROOT /proc/mounts; then
  71. FAKEROOT=$(mktemp -d -p $SANDBOX_ROOT ${FAKEROOT##*/}.XXXXXXX)
  72. SANDBOX_ID=".${FAKEROOT##*.}"
  73. SANDBOX_TMPFS=$SANDBOX_ROOT/${SANDBOX_TMPFS##*/}${SANDBOX_ID}
  74. rmdir $FAKEROOT
  75. fi
  76.  
  77. # 1. get aufs system-id for the root filesystem
  78. if [ -z "$AUFS_ROOT_ID" ] ; then
  79. AUFS_ROOT_ID=$(
  80. awk '{ if ($2 == "/" && $3 == "aufs") { match($4,/si=[0-9a-f]*/); print "si_" substr($4,RSTART+3,RLENGTH-3) } }' /proc/mounts
  81. )
  82. fi
  83.  
  84. # 2. get branches, then map branches to mount types or loop devices
  85. items=$(
  86. { echo ==mount==; cat /proc/mounts;
  87. echo ==losetup==; losetup-FULL -a;
  88. echo ==branches==; ls -v /sys/fs/aufs/$AUFS_ROOT_ID/br[0-9]* | xargs sed 's/=.*//'; echo ''; } | \
  89. awk '
  90. /==mount==/ { mode=1 }
  91. /==losetup==/ { mode=2 }
  92. /==branches==/ { mode=3 }
  93. {
  94. if (mode == 1) {
  95. # get list of mount points, types, and devices - index is $3 (mount points)
  96. mountdev[$2]=$1
  97. mounttypes[$2]=$3
  98. } else if (mode == 2) {
  99. # get list of loop devices and files - index is $1 (loop devs)
  100. sub(/:/,"",$1)
  101. sub(/.*\//,"",$3); sub(/)/,"",$3)
  102. loopdev[$1]=$3
  103. } else if (mode == 3) {
  104. # map mount types to loop files if mount devices is a loop
  105. for (m in mountdev) {
  106. if ( loopdev[mountdev[m]] != "" ) mounttypes[m]=loopdev[mountdev[m]]
  107. }
  108. # for (m in mountdev) print m " on " mountdev[m] " type " mounttypes[m]
  109. mode=4
  110. } else if (mode=4) {
  111. # print the branches and its mappings
  112. print $0, mounttypes[$0], "on"
  113. }
  114. }
  115. '
  116. )
  117. # '
  118.  
  119. # got a savedir.. breaks the dialog.. that should not happen
  120. items="$(echo "$items" | grep "\(squashfs\|\.sfs\)")" #only need SFS's
  121.  
  122. # 3. Ask user to choose the SFS
  123. dialog --separate-output --backtitle "tmpfs sandbox" --title "sandbox config" \
  124. --checklist "Choose which SFS you want to use" 0 0 0 $items 2> $TMPFILE
  125. chosen="$(cat $TMPFILE)"
  126.  
  127. clear
  128. if [ -z "$chosen" ]; then
  129. echo "Cancelled or no SFS is chosen - exiting."
  130. exit 1
  131. fi
  132.  
  133. # 4. convert chosen SFS to robranches
  134. robranches=""
  135. for a in $(cat $TMPFILE) ; do
  136. robranches=$robranches:$a=ro
  137. done
  138. rm $TMPFILE
  139.  
  140. # 5. make the mountpoints if not exist yet
  141. mkdir -p $FAKEROOT $SANDBOX_TMPFS
  142.  
  143. # 6. do the magic - mount the tmpfs first, and then the rest with aufs
  144. if mount -t tmpfs none $SANDBOX_TMPFS; then
  145. if mount -t aufs -o "br:$SANDBOX_TMPFS=rw$robranches" aufs $FAKEROOT; then
  146. # 5. record our new aufs-root-id so tools don't hack real filesystem
  147. SANDBOX_AUFS_ID=$(grep $FAKEROOT /proc/mounts | sed 's/.*si=/si_/; s/ .*//') #'
  148. sed -i -e '/AUFS_ROOT_ID/ d' $FAKEROOT/etc/BOOTSTATE 2> /dev/null
  149. echo AUFS_ROOT_ID=$SANDBOX_AUFS_ID >> $FAKEROOT/etc/BOOTSTATE
  150.  
  151. # 7. sandbox is ready, now just need to mount other supports - pts, proc, sysfs, usb and tmp
  152. mkdir -p $FAKEROOT/dev $FAKEROOT/sys $FAKEROOT/proc $FAKEROOT/tmp
  153. mount -o rbind /dev $FAKEROOT/dev
  154. mount -t sysfs none $FAKEROOT/sys
  155. mount -t proc none $FAKEROOT/proc
  156. mount -o bind /tmp $FAKEROOT/tmp
  157. mkdir -p $FAKEROOT/$SANDBOX_TMPFS
  158. mount -o bind $SANDBOX_TMPFS $FAKEROOT/$SANDBOX_TMPFS # so we can access it within sandbox
  159.  
  160. # 8. optional copy, to enable running sandbox-ed xwin
  161. cp /usr/share/sandbox/* $FAKEROOT/usr/bin 2> /dev/null
  162.  
  163. # 9. make sure we identify ourself as in sandbox - and we're good to go!
  164. echo -e '\nexport PS1="sandbox'${SANDBOX_ID}'# "' >> $FAKEROOT/etc/shinit #fatdog 600
  165. sed -i -e '/^PS1/ s/^.*$/PS1="sandbox'${SANDBOX_ID}'# "/' $FAKEROOT/etc/profile # earlier fatdog
  166. echo "Starting sandbox now."
  167. if [ $USE_NS ]; then
  168. unshare -f -p --mount-proc=$FAKEROOT/proc chroot $FAKEROOT
  169. else
  170. chroot $FAKEROOT
  171. fi
  172.  
  173. # 10. done - clean up everything
  174. umountall
  175. echo "Leaving sandbox."
  176. else
  177. echo "Unable to mount aufs br:$SANDBOX_TMPFS=rw$robranches"
  178. umount -l $SANDBOX_TMPFS
  179. fi
  180. else
  181. echo "unable to mount tmpfs."
  182. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement