#!/bin/bash

echo -n "Re-mounting root ... "

if mount / -noremount,ro,noatime; then
	echo "done."
	echo Root is now read-only.
	exit 0
else
	if [[ "$1" == "-f" ]]; then
		echo Filesystem busy, forcing remount.

		echo -n Locating data partitions ...
		NODEV=`echo \`grep < /proc/filesystems nodev | awk '{print "^" $2 "|"}'\` | sed -e "s/ //g" -e "s/|$//g"`
		RWFS=`awk < /proc/mounts "\\$4 ~ \"^rw\" { if (\\$1!=\"none\" && \\$2!=\"/\" && \\$3 !~ /$NODEV/) print \\$2}"`
		echo " done."

		echo -n Syncing filesystems ...
		sync
		echo " done."

		echo -n Re-mounting everything read-only ...
		echo u > /proc/sysrq-trigger
		echo " done."

		sleep 3
		if [[ $RWFS ]]; then
			echo -n Re-mounting data partitions read-write:
			for FS in $RWFS; do
				echo -n " [$FS]"
				mount $FS -noremount,rw
			done
			echo " done."
		fi
		echo Root is now read-only.
		exit 0
	fi
	echo Failed. If you\'re sure you know what are you doing, try \"mro -f\".
	exit 1
fi


