s243a

ruf-iron-flask.sh (ver #2)

Jan 24th, 2021 (edited)
1,020
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2. #ROUTER_IP=192.168.100.1
  3. # Rufwoof Jan 2021 (updated May 2019 version). Comments at end
  4. WM=jwm
  5. cd "$(realpath "${0%/*}")" #added by s243a (4 lines)
  6. CWD="$(realpath .)" #"$PWD"
  7. SFS_NAME="$(ls -1 ../*iron*.sfs | head -n 1 | sed -r 's#^[.][.]/##g' | sed -r 's#[.]sfs##g')"
  8. Parent_WD="$(realpath "$CWD/..")"
  9. SFS_PATH="$Parent_WD/$SFS_NAME".sfs #End Adds by s243a
  10.  
  11. xterm & # added so have at least one window I can alt-tab to out of the container
  12. #[ ! -d /mnt/sda4/shared ] && mkdir /mnt/sda4/shared # as a shared folder
  13. SHARED_LOC="$CWD"/shared #For a more portable container script add shared folder relative to script path.
  14.  
  15.  
  16. #Uncomment the following to use a shared folder:
  17. #SHARED="$SHARED_LOC"
  18.  
  19. #CHANGES_LOC=/mnt/sda4/changes        # Non pre-existing ext fs work folder
  20. CHANGES_LOC="$CWD"/container #For a more portable container script add changes folder relative to script path.
  21.  
  22. #MAIN_SFS=/mnt/sda1/FATDOG811-FINAL/fd64.sfs # Where the main sfs is located
  23. MAIN_SFS="$SFS_PATH" #s243a: Replaces above line
  24.  
  25. # Xephyr parameters
  26.    XP="-fullscreen -title container -name Xephyr2 -dpi 144 -nolisten tcp"
  27. #XEPHYR="-fullscreen -name Xephyr2 -dpi 144 -nolisten tcp"  
  28. XEPHYR="$XP"
  29.  
  30. MOUNTS="--mount=bind:/dev/snd:/dev/snd \
  31.        --mount=bind:/dev/mixer:/dev/mixer"
  32. [ ! -z "${SHARED}" ] && MOUNTS="$MOUNTS -mount=bind:${SHARED}:/home/shared"        
  33.  
  34. CAPS="--caps=all,-sys_admin,-sys_boot,-sys_chroot,-sys_ptrace,-sys_time,\
  35. -sys_tty_config,-chown,-kill,-dac_override,-dac_read_search,\
  36. -fowner,-setfcap,-setpcap,-net_admin,-mknod,-sys_module,\
  37. -sys_nice,-sys_resource"
  38. #PF="${PF} --chroot=${CHANGES_LOC}/top"  
  39. PFLASK="--keepenv --no-ipcns --no-netns ${MOUNTS} ${CAPS} \
  40.        --chroot=${CHANGES_LOC}/top"
  41.  
  42. function umountall(){
  43.   if [ ! -z "$PID" ]; then
  44.     kill PID
  45.   else
  46.     killall Xephyr
  47.   fi
  48.   cd ${CHANGES_LOC}
  49.   #umount top sfs
  50.   umount -l top
  51.   umount -l sfs
  52.   rm -rf changes
  53.   rmdir top sfs
  54.   [ -d "$SHARED/flags" ] && [ ! -z "$HOSTGRAB" ] && kill $HOSTGRAB
  55.   rm /tmp/container.run
  56. }
  57.  
  58. # Avoid double click 2 instances
  59. N=`date +%s` # Seconds since January 1970
  60. if [ -f /tmp/container.run ];then
  61.         L=`cat /tmp/container.run`
  62.         D=`expr $N - $L`
  63.         if [ $D -lt 2 ];then
  64.             exit # quick 2 launches (doubled clicked ignore second click)
  65.         fi
  66. fi
  67.  
  68. trap 'umountall' 1
  69.  
  70.  
  71.  
  72. echo $N >/tmp/container.run
  73.  
  74. # Create a separate X instance so isolated from the main real root X
  75. T=`ps -ef | grep Xephyr2 | wc -l`
  76. if [ $T -ne 2 ];then
  77.         Xephyr :2 ${XP} &
  78. else
  79.         exit # Xephyr2 already running
  80. fi
  81.  
  82. if [ ! -z "${ROUTER_IP}" ]; then
  83.   iptables -A INPUT -s ${ROUTER_IP} -j DROP     # Drop access to router admin
  84. fi
  85.  
  86. # Prepare and launch 'container' and shared folder
  87.  
  88. if [ ! -z "$SHARED" ]; then
  89.   [ ! -d $SHARED ] && mkdir $SHARED
  90.   [ ! -d ${SHARED}/flags ] && mkdir ${SHARED}/flags
  91. fi
  92.  
  93. # Create a changes folder, sfs mount point for main.sfs and top layer
  94. # folders and aufs mount to combine changes and sfs folders -> top
  95. mkdir -p "${CHANGES_LOC}"
  96. cd "${CHANGES_LOC}"
  97. # Check for possible hangover - such as if restarted X and clean out if so
  98. if [ -d top ] || [ -d sfs ] || [ -d changes ]; then
  99.         umount top sfs
  100.         rm -rf changes
  101.         rmdir top sfs
  102. fi
  103. mkdir top sfs changes
  104.  
  105. #s243a: added append_mnt_id_awk (bellow). See: https://forum.puppylinux.com/viewtopic.php?f=136&t=1932
  106. append_mnt_id_awk='
  107. function get_mnt_id(mnt_pt,loop){
  108.  if (length(mnt_pt) > 0 && length(loop)>0){
  109.    cmd="cat /proc/self/mountinfo | sort | grep '" loop "' | grep " mnt_pt " | head -n 1"
  110.  } else if (length(mnt_pt) > 0){
  111.    cmd="cat /proc/self/mountinfo | sort | grep '" mnt_pt "' | head -n 1"
  112.  } else if (length(loop)>0){
  113.    cmd="cat /proc/self/mountinfo | sort | grep '" loop "' | head -n 1"
  114.  }
  115.  while ((cmd | getline )){
  116.    mnt_id=$1
  117.    break  
  118.  }
  119.  close(cmd)
  120.  return mnt_id
  121. }
  122. {
  123.  mnt_pt=$1
  124.  loop=$2
  125.  mnt_id=get_mnt_id(mnt_pt,loop)
  126.  print mnt_id "|" mnt_pt "|" loop
  127. }'
  128. loop=$(losetup -a | grep  "${MAIN_SFS}"  | sed "s/:.*$//" )
  129. if [ ! -z "$loop" ]; then #
  130.   sfs_MP="$(findmnt -o TARGET,SOURCE -D -n | grep $loop\$ | awk "$append_mnt_id_awk" | sort -t '|' -k1 | cut -d'|' -f2 | head -n 1)"
  131. else
  132.   sfs_MP="${CHANGES_LOC}"/sfs
  133.   mount -r -t squashfs ${MAIN_SFS} "$sfs_MP"
  134. fi
  135. cd ${CHANGES_LOC}
  136. mount -t aufs -o br=changes:"$sfs_MP" none top
  137. cp /var/lib/dbus/machine-id top/var/lib/dbus/machine-id
  138. cp /etc/resolv.conf top/etc/resolv.conf
  139. ln -s top/var/lib/dbus/machine-id top/etc/machine-id
  140. #echo >$SHARED/flags/host-grab
  141.  
  142. [ -d "$SHARED/flags" ] && echo >$SHARED/flags/host-grab
  143. if [ -d "$SHARED/flags" ]; then #TODO add more conditions to execute this code.  
  144.   cat <<EOF >top/tmp/hostgrab
  145.   #!/bin/bash
  146.   xsetroot -bg \#ff0000 -mod 5 5
  147.   while inotifywait -e modify /home/shared/flags; do
  148.     C=\$(tail -1 /home/shared/flags/host-grab | grep release)
  149.     if [ ! -z "\${C}" ]; then
  150.         xsetroot -bg \#0000ff -mod 5 5
  151.     else
  152.         xsetroot -bg \#ff0000 -mod 5 5
  153.     fi
  154.   done
  155. EOF
  156. chmod +x top/tmp/hostgrab
  157. fi
  158.  
  159. # # create a script to run inside the chroot (i.e. must be a script, not a bin)
  160.  
  161. # s243a: puppy doesn't have lxqt-panel
  162. # echo "lxqt-panel &" >>top/init
  163. # echo "openbox" >>top/init
  164. case "$WM" in
  165. jwm)
  166. echo '
  167. #!/bin/sh
  168. export DISPLAY=:2
  169. . /etc/DISTRO_SPECS
  170. if [ "$DISTRO_ARCHDIR" ] ; then
  171.     ARCHDIR="/$DISTRO_ARCHDIR"
  172. fi
  173. ldconfig
  174. iconvconfig
  175. #update-pango-querymodules
  176. #Failed to create file /usr/lib/i86/-linux-gnu/pango/1.8.0/modules.cache.8P0KX0 No such file or directory
  177. gdk-pixbuf-query-loaders --update-cache
  178. update-mime-database -V /usr/share/mime/
  179. status_func $?
  180. UPDATE_MIME_DATABASE_DONE=1
  181.  
  182. source /etc/profile
  183. fixmenus #probably not necessary
  184. keymap-set --update
  185. userresources=$HOME/.Xresources
  186. usermodmap=$HOME/.Xmodmap
  187. sysresources=/usr/lib/X11/xinit/Xresources
  188. sysmodmap=/usr/lib/X11/xinit/.Xmodmap
  189.  
  190. # merge in defaults and keymaps
  191.  
  192. if [ -f $sysresources ]; then
  193.    xrdb -merge -nocpp $sysresources
  194. fi
  195.  
  196. if [ -f $sysmodmap ]; then
  197.    xmodmap $sysmodmap
  198. fi
  199.  
  200. if [ -f $userresources ]; then
  201.    xrdb -merge -nocpp $userresources
  202. fi
  203.  
  204. if [ -f $usermodmap ]; then
  205.    xmodmap $usermodmap
  206. fi
  207. setxkbmap -option keypad:pointerkeys
  208. DISPLAY=:2 jwm &
  209. DISPLAY=:2 roxfiler &
  210. DISPLAY=:2 xterm -e iron
  211. ' >top/init
  212. ;;
  213. cwm)
  214. cat <<EOF >top/init
  215. #!/bin/sh
  216. $(if [ -d "$SHARED/flags" ]; then
  217. echo '/tmp/hostgrab &
  218. /usr/bin/help &'
  219. fi )
  220. cwm -c /root/.cwmrc
  221. EOF
  222. cat <<EOF >top/usr/bin/help
  223. #!/bin/sh
  224. message() {
  225.     Xdialog --title Information --msgbox "\$1" 0 0
  226. }
  227. M="This is a Xephyr X session window where cwm is the window manager.\n"
  228. M="\${M}cwm is great for laptops (nearly all OpenBSD developer use cwm) and is relatively easy to learn.\n"
  229. M="\${M}Whilst you run as 'root' within Xephyr, root is actually a highly restricted userid that is running\n"
  230. M="\${M}in a contained environment, as such some programs may not run as expected.\n"
  231. M="\${M}\nLeft mouse press on desktop shows windows menu, right mouse press for applications menu\n"
  232. M="\${M}(a gap is left around the screen edges so the desktop is still accessible even if a window is maximised)\n\n"
  233. M="\${M}Ctrl Alt Enter : terminal. Alt m : toggles maximise. Ctrl Alt x : close window. Alt ? : Open exec launcher\n"
  234. M="\${M}\nCtrl Shift : toggles mouse/keyboard focus (desktop changes between red and blue)\n"
  235. M="\${M}Alt Tab : if desktop is red - steps between main session windows\n"
  236. M="\${M}OR if desktop is blue - steps between windows within the Xephyr container\n"
  237. M="\${M}\nWHEN THE DESKTOP IS BLUE (mouse/keyboard locked into Xephyr) ...\n"
  238. M="\${M}Alt middle mouse drag: resizes window. Alt left mouse drag : moves window\n"
  239. M="\${M}\nWHEN THE DESKTOP IS RED (mouse/keyboard unlocked from Xephyr) ...\n"
  240. M="\${M}you can use usual main system controls such as Alt-F1 for menu, Alt-F4 to close the Xephyr ...etc.\n"
  241. M="\${M}\nRevisit this list again at any time by running 'help', or search online for the cwm manual"
  242. message "\${M}"
  243. EOF
  244. chmod +x top/usr/bin/help
  245. ;;
  246. esac
  247.  
  248. chmod +x top/init
  249. # The big Xephyr capabilities dropped chroot switch
  250. DISPLAY=:2 empty -f unshare -m pflask ${PFLASK} -- /init
  251. PID=$!
  252.  
  253. if [ -d "$SHARED/flags" ]; then
  254.   function _hostgrabstate() {
  255.  
  256.     local WID=""
  257.  
  258.     while [ -z "${WID}" ]; do
  259.         WID=$(wmctrl -lp | grep Xephyr | cut -d ' ' -f 1)
  260.         sleep 1
  261.     done
  262.     sleep 2
  263.     xprop -id ${WID} -spy WM_NAME >>$SHARED/flags/host-grab
  264.   }
  265.   _hostgrabstate &
  266.   HOSTGRAB=$!
  267. fi
  268.  
  269. #DISPLAY=:2 pflask ${PF} -- /init
  270. wait $PID # above backgrounds, so we wait until that ends
  271.  
  272. # Clean up
  273.  
  274.  
  275. umountall
  276. #rmdir top sfs
  277.  
  278.  
  279. ############################################################################
  280. # FOR FATDOG 811 ... (Draft Modifications by s243a for other platforms)
  281. #
  282. # Aufs mounts changes (initial empty rw folder), main sfs, that combined
  283. # is visible/accessed via folder 'top', that we chroot into
  284. #
  285. # chroot with chroot capability dropped (to prevent chroot out of the chroot)
  286. # using another X session (Xephyr) to isolate it from the main X session.
  287. # We chroot using pflask as that makes things easier into the top folder
  288. # applying further restrictions. We use the main sfs as our base for the
  289. # chroot, so very low overheads.
  290. #
  291. # alt-F4    closes the Xephyr container (if not then ctrl-shift to unfocus
  292. #           mouse/keyboard.
  293. # alt Tab   to step to another window in main system, but if use
  294. #           xdotool keydown alt key Tab;xdotool keyup alt .... it doesn't
  295. #           work (as intended i.e. it's locked into the "container").
  296. # seamonkey from menu doesn't work, run seamonkey from within urxvt
  297. # We use DISPLAY :2 for the Xephyr server
  298. #
  299. # Requires empty, pflask and a ext filesystem to create/work within
  300. #
  301. # I use fatdog multi-session save type frugal boot, and periodically I've
  302. # re-merged the save files into fd64.sfs so my fd64.sfs isn't the standard
  303. # version (merging changes and not copying fd64.sfs to ram helps keep ram
  304. # usage low).
  305. ############################################################################
  306.  
RAW Paste Data