2006-12-11 00:52:36 +00:00
|
|
|
{bash, event, utillinux}:
|
2006-11-23 22:53:16 +00:00
|
|
|
|
|
|
|
assert event == "reboot"
|
|
|
|
|| event == "halt"
|
|
|
|
|| event == "system-halt"
|
|
|
|
|| event == "power-off";
|
2006-11-20 20:50:52 +00:00
|
|
|
|
|
|
|
{
|
2006-11-23 22:53:16 +00:00
|
|
|
name = "sys-" + event;
|
2006-11-20 20:50:52 +00:00
|
|
|
|
|
|
|
job = "
|
2006-11-23 22:53:16 +00:00
|
|
|
start on ${event}
|
2006-11-20 20:50:52 +00:00
|
|
|
|
|
|
|
script
|
|
|
|
exec < /dev/tty1 > /dev/tty1 2>&1
|
|
|
|
echo \"\"
|
|
|
|
echo \"<<< SYSTEM SHUTDOWN >>>\"
|
|
|
|
echo \"\"
|
|
|
|
|
2006-12-21 18:12:19 +00:00
|
|
|
export PATH=${utillinux}/bin:${utillinux}/sbin:$PATH
|
2006-12-11 00:52:36 +00:00
|
|
|
|
2006-11-20 21:19:34 +00:00
|
|
|
# Do an initial sync just in case.
|
|
|
|
sync || true
|
|
|
|
|
2006-12-21 18:12:19 +00:00
|
|
|
getMountPoints() {
|
|
|
|
cat /proc/mounts \\
|
|
|
|
| grep -v '^rootfs' \\
|
|
|
|
| sed 's|^[^ ]\\+ \\+\\([^ ]\\+\\).*|\\1|' \\
|
|
|
|
| grep -v '/proc\\|/sys\\|/dev'
|
|
|
|
}
|
2006-11-20 21:19:34 +00:00
|
|
|
|
2006-12-21 18:12:19 +00:00
|
|
|
getDevice() {
|
|
|
|
local mountPoint=$1
|
|
|
|
cat /proc/mounts \\
|
|
|
|
| grep -v '^rootfs' \\
|
|
|
|
| grep \"^[^ ]\\+ \\+$mountPoint \\+\" \\
|
|
|
|
| sed 's|^\\([^ ]\\+\\).*|\\1|'
|
|
|
|
}
|
|
|
|
|
|
|
|
# Unmount file systems. We repeat this until no more file systems
|
|
|
|
# can be unmounted. This is to handle loopback devices, file
|
|
|
|
# systems mounted on other file systems and so on.
|
|
|
|
tryAgain=1
|
|
|
|
while test -n \"$tryAgain\"; do
|
|
|
|
tryAgain=
|
|
|
|
|
|
|
|
for mp in $(getMountPoints); do
|
|
|
|
device=$(getDevice $mp)
|
|
|
|
echo \"unmounting $mp...\"
|
2007-01-10 17:08:34 +00:00
|
|
|
if umount -f -n \"$mp\"; then
|
2006-12-21 18:12:19 +00:00
|
|
|
if test \"$mp\" != /; then tryAgain=1; fi
|
2007-01-09 09:52:16 +00:00
|
|
|
else
|
2007-01-11 00:40:08 +00:00
|
|
|
mount -n -o remount,ro \"$mp\" || true
|
2006-12-21 18:12:19 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Hack: work around a bug in mount (mount -o remount on a
|
|
|
|
# loop device forgets the loop=/dev/loopN entry in
|
|
|
|
# /etc/mtab).
|
|
|
|
if echo \"$device\" | grep -q '/dev/loop'; then
|
|
|
|
echo \"removing loop device $device...\"
|
|
|
|
losetup -d \"$device\" || true
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
|
|
|
cat /proc/mounts
|
2006-11-20 21:19:34 +00:00
|
|
|
|
|
|
|
# Final sync.
|
|
|
|
sync || true
|
|
|
|
|
2006-11-20 20:50:52 +00:00
|
|
|
# Right now all events above power off the system.
|
2006-11-23 22:53:16 +00:00
|
|
|
if test ${event} = reboot; then
|
|
|
|
exec reboot -f
|
|
|
|
else
|
|
|
|
exec halt -f -p
|
|
|
|
fi
|
2006-11-20 20:50:52 +00:00
|
|
|
end script
|
|
|
|
";
|
|
|
|
|
|
|
|
}
|