2009-10-12 17:36:19 +01:00
|
|
|
|
{ config, pkgs, ... }:
|
2006-11-23 22:53:16 +00:00
|
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
|
with pkgs.lib;
|
2009-03-06 12:27:47 +00:00
|
|
|
|
|
2009-11-06 21:08:06 +00:00
|
|
|
|
{
|
2009-03-06 12:27:47 +00:00
|
|
|
|
|
2009-11-06 21:08:06 +00:00
|
|
|
|
jobs.shutdown =
|
|
|
|
|
{ name = "shutdown";
|
2009-10-12 17:36:19 +01:00
|
|
|
|
|
|
|
|
|
task = true;
|
|
|
|
|
|
2010-06-02 17:04:08 +01:00
|
|
|
|
stopOn = ""; # must override the default ("starting shutdown")
|
|
|
|
|
|
2009-11-06 21:08:06 +00:00
|
|
|
|
environment = { MODE = "poweroff"; };
|
|
|
|
|
|
2010-06-02 17:04:08 +01:00
|
|
|
|
extraConfig = "console owner";
|
|
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
|
script =
|
|
|
|
|
''
|
2009-03-06 12:27:47 +00:00
|
|
|
|
set +e # continue in case of errors
|
2009-11-06 21:08:06 +00:00
|
|
|
|
|
|
|
|
|
${pkgs.kbd}/bin/chvt 1
|
2009-03-06 12:27:47 +00:00
|
|
|
|
|
2009-11-06 21:08:06 +00:00
|
|
|
|
exec < /dev/console > /dev/console 2>&1
|
2009-03-06 12:27:47 +00:00
|
|
|
|
echo ""
|
2009-11-06 21:51:28 +00:00
|
|
|
|
if test "$MODE" = maintenance; then
|
|
|
|
|
echo "[1;32m<<< Entering maintenance mode >>>[0m"
|
|
|
|
|
else
|
|
|
|
|
echo "[1;32m<<< System shutdown >>>[0m"
|
|
|
|
|
fi
|
2009-03-06 12:27:47 +00:00
|
|
|
|
echo ""
|
|
|
|
|
|
2009-11-06 21:08:06 +00:00
|
|
|
|
export PATH=${pkgs.utillinux}/bin:${pkgs.utillinux}/sbin:$PATH
|
2009-03-06 12:27:47 +00:00
|
|
|
|
|
2009-11-06 21:08:06 +00:00
|
|
|
|
|
2009-03-06 12:27:47 +00:00
|
|
|
|
# Do an initial sync just in case.
|
2009-08-11 22:08:05 +01:00
|
|
|
|
sync
|
2009-11-06 21:08:06 +00:00
|
|
|
|
|
|
|
|
|
|
2009-03-06 12:27:47 +00:00
|
|
|
|
# Kill all remaining processes except init and this one.
|
2009-08-11 22:08:05 +01:00
|
|
|
|
echo "sending the TERM signal to all processes..."
|
|
|
|
|
kill -TERM -1
|
2009-03-06 12:27:47 +00:00
|
|
|
|
|
|
|
|
|
sleep 1 # wait briefly
|
2009-08-11 22:08:05 +01:00
|
|
|
|
|
|
|
|
|
echo "sending the KILL signal to all processes..."
|
|
|
|
|
kill -KILL -1
|
2009-11-06 21:08:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# If maintenance mode is requested, start a root shell, and
|
|
|
|
|
# afterwards emit the "startup" event to bring everything
|
|
|
|
|
# back up.
|
|
|
|
|
if test "$MODE" = maintenance; then
|
|
|
|
|
echo ""
|
2009-11-06 21:51:28 +00:00
|
|
|
|
echo "[1;32m<<< Maintenance shell >>>[0m"
|
2009-11-06 21:08:06 +00:00
|
|
|
|
echo ""
|
2010-06-02 17:04:08 +01:00
|
|
|
|
${pkgs.pam_login}/bin/login root
|
2009-11-06 21:38:40 +00:00
|
|
|
|
initctl emit -n startup
|
2009-11-06 21:08:06 +00:00
|
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
|
2009-03-06 12:27:47 +00:00
|
|
|
|
|
2010-06-02 17:04:08 +01:00
|
|
|
|
# Set the hardware clock to the system time.
|
|
|
|
|
echo "setting the hardware clock..."
|
|
|
|
|
hwclock --systohc --utc
|
|
|
|
|
|
|
|
|
|
|
2009-03-06 12:27:47 +00:00
|
|
|
|
# Unmount helper functions.
|
|
|
|
|
getMountPoints() {
|
|
|
|
|
cat /proc/mounts \
|
|
|
|
|
| grep -v '^rootfs' \
|
|
|
|
|
| sed 's|^[^ ]\+ \+\([^ ]\+\).*|\1|' \
|
|
|
|
|
| grep -v '/proc\|/sys\|/dev'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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..."
|
2009-08-11 22:08:05 +01:00
|
|
|
|
|
2009-12-16 23:59:05 +00:00
|
|
|
|
# We need to remount,ro before attempting any
|
|
|
|
|
# umount, or bind mounts may get confused, with
|
|
|
|
|
# the fs not being properly flushed at the end.
|
|
|
|
|
|
|
|
|
|
# `-i' is to workaround a bug in mount.cifs (it
|
|
|
|
|
# doesn't recognise the `remount' option, and
|
|
|
|
|
# instead mounts the FS again).
|
|
|
|
|
mount -n -i -o remount,ro "$mp"
|
|
|
|
|
|
2009-08-11 22:08:05 +01:00
|
|
|
|
# Note: don't use `umount -f'; it's very buggy.
|
|
|
|
|
# (For instance, when applied to a bind-mount it
|
|
|
|
|
# unmounts the target of the bind-mount.) !!! But
|
|
|
|
|
# we should use `-f' for NFS.
|
|
|
|
|
if umount -n "$mp"; then
|
2009-03-06 12:27:47 +00:00
|
|
|
|
if test "$mp" != /; then tryAgain=1; fi
|
|
|
|
|
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..."
|
2009-08-11 22:08:05 +01:00
|
|
|
|
losetup -d "$device"
|
2009-03-06 12:27:47 +00:00
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Final sync.
|
2009-08-11 22:08:05 +01:00
|
|
|
|
sync
|
2009-03-06 12:27:47 +00:00
|
|
|
|
|
|
|
|
|
|
2009-11-06 21:51:28 +00:00
|
|
|
|
# Either reboot or power-off the system.
|
2009-11-06 21:08:06 +00:00
|
|
|
|
if test "$MODE" = reboot; then
|
2009-08-11 22:08:05 +01:00
|
|
|
|
echo "rebooting..."
|
|
|
|
|
sleep 1
|
2009-03-06 12:27:47 +00:00
|
|
|
|
exec reboot -f
|
|
|
|
|
else
|
2009-08-11 22:08:05 +01:00
|
|
|
|
echo "powering off..."
|
|
|
|
|
sleep 1
|
2009-03-06 12:27:47 +00:00
|
|
|
|
exec halt -f -p
|
|
|
|
|
fi
|
2009-10-12 17:36:19 +01:00
|
|
|
|
'';
|
|
|
|
|
};
|
2009-03-06 12:27:47 +00:00
|
|
|
|
|
2009-12-16 23:59:05 +00:00
|
|
|
|
}
|