2006-12-11 01:52:36 +01:00
|
|
|
{bash, event, utillinux}:
|
2006-11-23 23:53:16 +01:00
|
|
|
|
|
|
|
assert event == "reboot"
|
|
|
|
|| event == "halt"
|
|
|
|
|| event == "system-halt"
|
|
|
|
|| event == "power-off";
|
2006-11-20 21:50:52 +01:00
|
|
|
|
|
|
|
{
|
2006-11-23 23:53:16 +01:00
|
|
|
name = "sys-" + event;
|
2006-11-20 21:50:52 +01:00
|
|
|
|
|
|
|
job = "
|
2006-11-23 23:53:16 +01:00
|
|
|
start on ${event}
|
2006-11-20 21:50:52 +01:00
|
|
|
|
|
|
|
script
|
|
|
|
exec < /dev/tty1 > /dev/tty1 2>&1
|
|
|
|
echo \"\"
|
|
|
|
echo \"<<< SYSTEM SHUTDOWN >>>\"
|
|
|
|
echo \"\"
|
|
|
|
|
2006-12-21 19:12:19 +01:00
|
|
|
export PATH=${utillinux}/bin:${utillinux}/sbin:$PATH
|
2006-12-11 01:52:36 +01:00
|
|
|
|
2007-02-20 16:51:13 +01:00
|
|
|
|
2006-11-20 22:19:34 +01:00
|
|
|
# Do an initial sync just in case.
|
|
|
|
sync || true
|
|
|
|
|
2007-02-20 16:51:13 +01:00
|
|
|
|
|
|
|
# Kill all remaining processes except init and this one.
|
|
|
|
echo \"Sending the TERM signal to all processes...\"
|
|
|
|
kill -TERM -1
|
|
|
|
|
|
|
|
sleep 1 # wait briefly
|
|
|
|
|
|
|
|
echo \"Sending the KILL signal to all processes...\"
|
|
|
|
kill -KILL -1
|
|
|
|
|
|
|
|
|
|
|
|
# Unmount helper functions.
|
2006-12-21 19:12:19 +01:00
|
|
|
getMountPoints() {
|
|
|
|
cat /proc/mounts \\
|
|
|
|
| grep -v '^rootfs' \\
|
|
|
|
| sed 's|^[^ ]\\+ \\+\\([^ ]\\+\\).*|\\1|' \\
|
|
|
|
| grep -v '/proc\\|/sys\\|/dev'
|
|
|
|
}
|
2006-11-20 22:19:34 +01:00
|
|
|
|
2006-12-21 19:12:19 +01: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 18:08:34 +01:00
|
|
|
if umount -f -n \"$mp\"; then
|
2006-12-21 19:12:19 +01:00
|
|
|
if test \"$mp\" != /; then tryAgain=1; fi
|
2007-01-09 10:52:16 +01:00
|
|
|
else
|
2007-01-11 01:40:08 +01:00
|
|
|
mount -n -o remount,ro \"$mp\" || true
|
2006-12-21 19:12:19 +01: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 22:19:34 +01:00
|
|
|
|
|
|
|
# Final sync.
|
|
|
|
sync || true
|
|
|
|
|
2006-11-20 21:50:52 +01:00
|
|
|
# Right now all events above power off the system.
|
2006-11-23 23:53:16 +01:00
|
|
|
if test ${event} = reboot; then
|
|
|
|
exec reboot -f
|
|
|
|
else
|
|
|
|
exec halt -f -p
|
|
|
|
fi
|
2006-11-20 21:50:52 +01:00
|
|
|
end script
|
|
|
|
";
|
|
|
|
|
|
|
|
}
|