2009-10-12 16:36:19 +00:00
|
|
|
|
{ config, pkgs, ... }:
|
2006-11-23 22:53:16 +00:00
|
|
|
|
|
2009-10-12 16:36:19 +00: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 16:36:19 +00:00
|
|
|
|
|
|
|
|
|
task = true;
|
|
|
|
|
|
2010-06-02 16:04:08 +00:00
|
|
|
|
stopOn = ""; # must override the default ("starting shutdown")
|
|
|
|
|
|
2009-11-06 21:08:06 +00:00
|
|
|
|
environment = { MODE = "poweroff"; };
|
|
|
|
|
|
2010-06-02 16:04:08 +00:00
|
|
|
|
extraConfig = "console owner";
|
|
|
|
|
|
2009-10-12 16:36:19 +00: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 21:08:05 +00:00
|
|
|
|
sync
|
2009-11-06 21:08:06 +00:00
|
|
|
|
|
|
|
|
|
|
2010-06-09 22:29:06 +00:00
|
|
|
|
# Kill all remaining processes except init, this one and any
|
|
|
|
|
# Upstart jobs that don't stop on the "starting shutdown"
|
|
|
|
|
# event, as these are necessary to complete the shutdown.
|
|
|
|
|
omittedPids=$(initctl list | sed -e 's/.*process \([0-9]\+\)/-o \1/;t;d')
|
|
|
|
|
#echo "saved PIDs: $omittedPids"
|
|
|
|
|
|
2009-08-11 21:08:05 +00:00
|
|
|
|
echo "sending the TERM signal to all processes..."
|
2010-06-09 22:29:06 +00:00
|
|
|
|
${pkgs.sysvtools}/bin/killall5 -15 $job $omittedPids
|
2009-03-06 12:27:47 +00:00
|
|
|
|
|
|
|
|
|
sleep 1 # wait briefly
|
2009-08-11 21:08:05 +00:00
|
|
|
|
|
|
|
|
|
echo "sending the KILL signal to all processes..."
|
2010-06-09 22:29:06 +00:00
|
|
|
|
${pkgs.sysvtools}/bin/killall5 -9 $job $omittedPids
|
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 21:10:48 +00:00
|
|
|
|
${pkgs.shadow}/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
|
2010-06-08 12:48:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Write a shutdown record to wtmp while /var/log is still writable.
|
|
|
|
|
reboot --wtmp-only
|
|
|
|
|
|
|
|
|
|
|
2010-06-02 16:04:08 +00:00
|
|
|
|
# Set the hardware clock to the system time.
|
|
|
|
|
echo "setting the hardware clock..."
|
|
|
|
|
hwclock --systohc --utc
|
2010-06-08 10:06:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Stop all swap devices.
|
|
|
|
|
swapoff -a
|
2010-06-09 22:29:06 +00:00
|
|
|
|
|
|
|
|
|
|
2009-03-06 12:27:47 +00:00
|
|
|
|
# Unmount file systems. We repeat this until no more file systems
|
|
|
|
|
# can be unmounted. This is to handle loopback devices, file
|
2010-09-03 11:39:48 +00:00
|
|
|
|
# systems mounted on other file systems and so on.
|
2009-03-06 12:27:47 +00:00
|
|
|
|
tryAgain=1
|
|
|
|
|
while test -n "$tryAgain"; do
|
|
|
|
|
tryAgain=
|
2010-06-08 11:52:16 +00:00
|
|
|
|
failed= # list of mount points that couldn't be unmounted/remounted
|
|
|
|
|
|
|
|
|
|
cp /proc/mounts /dev/.mounts # don't read /proc/mounts while it's changing
|
|
|
|
|
exec 4< /dev/.mounts
|
|
|
|
|
while read -u 4 device mp fstype options rest; do
|
2010-06-16 22:16:43 +00:00
|
|
|
|
# Skip various special filesystems. Non-existent
|
|
|
|
|
# mount points are typically tmpfs/aufs mounts from
|
|
|
|
|
# the initrd.
|
|
|
|
|
if [ "$mp" = /proc -o "$mp" = /sys -o "$mp" = /dev -o "$device" = "rootfs" -o "$mp" = /var/run -o ! -e "$mp" ]; then continue; fi
|
2010-06-08 11:52:16 +00:00
|
|
|
|
|
2009-03-06 12:27:47 +00:00
|
|
|
|
echo "unmounting $mp..."
|
2009-08-11 21:08:05 +00: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).
|
2010-06-08 11:52:16 +00:00
|
|
|
|
success=
|
|
|
|
|
if mount -t "$fstype" -n -i -o remount,ro "device" "$mp"; then success=1; fi
|
2009-12-16 23:59:05 +00:00
|
|
|
|
|
2009-08-11 21:08:05 +00: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.
|
2010-09-03 11:39:48 +00:00
|
|
|
|
if [ "$mp" != / -a "$mp" != /nix -a "$mp" != /nix/store ]; then
|
2010-06-08 11:52:16 +00:00
|
|
|
|
if umount -n "$mp"; then success=1; tryAgain=1; fi
|
2009-03-06 12:27:47 +00:00
|
|
|
|
fi
|
2010-06-08 11:52:16 +00:00
|
|
|
|
|
|
|
|
|
if [ -z "$success" ]; then failed="$failed $mp"; fi
|
2009-03-06 12:27:47 +00:00
|
|
|
|
done
|
|
|
|
|
done
|
2010-06-08 10:06:00 +00:00
|
|
|
|
|
|
|
|
|
|
2010-06-08 11:52:16 +00:00
|
|
|
|
# Warn about filesystems that could not be unmounted or
|
|
|
|
|
# remounted read-only.
|
|
|
|
|
if [ -n "$failed" ]; then
|
|
|
|
|
echo "[1;31mwarning:[0m the following filesystems could not be unmounted:"
|
|
|
|
|
for mp in $failed; do echo " $mp"; done
|
|
|
|
|
sleep 5
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
2009-03-06 12:27:47 +00:00
|
|
|
|
# Final sync.
|
2009-08-11 21:08:05 +00: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 21:08:05 +00:00
|
|
|
|
echo "rebooting..."
|
|
|
|
|
sleep 1
|
2009-03-06 12:27:47 +00:00
|
|
|
|
exec reboot -f
|
|
|
|
|
else
|
2009-08-11 21:08:05 +00:00
|
|
|
|
echo "powering off..."
|
|
|
|
|
sleep 1
|
2009-03-06 12:27:47 +00:00
|
|
|
|
exec halt -f -p
|
|
|
|
|
fi
|
2009-10-12 16:36:19 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
2009-03-06 12:27:47 +00:00
|
|
|
|
|
2009-12-16 23:59:05 +00:00
|
|
|
|
}
|