1#!/bin/sh
2# Do unmount/remount-ro. Wait.
3# KILL everybody. Wait.
4# Repeat.
5
6umountcnt=2
7writeout=0	# increase if your kernel does not guarantee writes to complete
8
9# No /usr - we are expecting all binaries to be accessible
10# from root fs alone
11PATH=/sbin:/bin
12
13say() {
14	printf "\r%s\n\r" "$*"
15}
16
17showps() {
18	# sleep 1 ensures that xargs will have time to start up
19	# this makes pslist less prone to random jitter
20	pslist=`{ sleep 1; ps -A -o comm=; } | sort | xargs`
21	pscnt=$(( `say "$pslist" | wc -w` + 0 ))
22	if test x"$VERBOSE" = x; then
23		say "* `date '+%H:%M:%S'` $pscnt processes"
24	else
25		say "* `date '+%H:%M:%S'` Processes ($pscnt): $pslist"
26	fi
27}
28
29say "<*> `date '+%Y-%m-%d %H:%M:%S'` Executing '$0 $*'"
30
31showps
32
33i="$umountcnt"
34while test "$i" -gt 0; do
35	say "* `date '+%H:%M:%S'` Unmounting filesystems"
36	umount -a -n -r -f
37	# In case we unmounted proc...
38	test -e /proc/version || mount -t proc none /proc
39	# Remounting / RO isn't necessary when /etc/mtab is linked to /proc/mounts:
40	# already done. But let's be more paranoid here...
41	say "* `date '+%H:%M:%S'` Remounting root filesystem read-only"
42	mount -n -o remount,ro /
43	say "* `date '+%H:%M:%S'` Freeing loop devices"
44	for a in /dev/loop*; do
45		test -b "$a" && losetup -d "$a"
46	done
47	say "* `date '+%H:%M:%S'` Syncing"
48	sync
49	say "* `date '+%H:%M:%S'` Executing: killall5 -KILL"
50	killall5 -9
51	showps
52	i=$((i-1))
53done
54
55say "* `date '+%H:%M:%S'` Filesystem status (/proc/mounts)"
56cat /proc/mounts \
57| {
58	bad=false
59	while read dev mntpoint fstype opt n1 n2; do
60		case "$fstype" in
61		( proc | sysfs | usbfs | devpts | rpc_pipefs | binfmt_misc | autofs | rootfs | tmpfs | ramfs )
62			say "$dev $mntpoint $fstype $opt $n1 $n2"
63			continue
64			;;
65		esac
66		if test "${opt:0:2}" = "rw"; then
67			say "$dev $mntpoint $fstype $opt $n1 $n2 - RW!"
68			bad=true
69		else
70			say "$dev $mntpoint $fstype $opt $n1 $n2"
71		fi
72	done
73	if $bad; then
74		say "ERROR: we have filesystems mounted RW! Press <Enter> (^J)..."
75		read junk </dev/console
76		#sh </dev/console >&0 2>&0 # debug
77	fi
78}
79
80# Disk cache writeout
81sleep "$writeout"
82