2009-10-12 18:36:19 +02:00
|
|
|
|
{ config, pkgs, ... }:
|
2006-11-23 23:53:16 +01:00
|
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
|
with pkgs.lib;
|
2009-03-06 13:27:47 +01:00
|
|
|
|
|
2009-11-06 22:08:06 +01:00
|
|
|
|
{
|
2009-03-06 13:27:47 +01:00
|
|
|
|
|
2009-11-06 22:08:06 +01:00
|
|
|
|
jobs.shutdown =
|
|
|
|
|
{ name = "shutdown";
|
2009-10-12 18:36:19 +02:00
|
|
|
|
|
|
|
|
|
task = true;
|
|
|
|
|
|
2010-06-02 18:04:08 +02:00
|
|
|
|
stopOn = ""; # must override the default ("starting shutdown")
|
|
|
|
|
|
2009-11-06 22:08:06 +01:00
|
|
|
|
environment = { MODE = "poweroff"; };
|
|
|
|
|
|
2010-06-02 18:04:08 +02:00
|
|
|
|
extraConfig = "console owner";
|
|
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
|
script =
|
|
|
|
|
''
|
2009-03-06 13:27:47 +01:00
|
|
|
|
set +e # continue in case of errors
|
2009-11-06 22:08:06 +01:00
|
|
|
|
|
|
|
|
|
${pkgs.kbd}/bin/chvt 1
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
2009-11-06 22:08:06 +01:00
|
|
|
|
exec < /dev/console > /dev/console 2>&1
|
2009-03-06 13:27:47 +01:00
|
|
|
|
echo ""
|
2009-11-06 22:51:28 +01:00
|
|
|
|
if test "$MODE" = maintenance; then
|
|
|
|
|
echo "[1;32m<<< Entering maintenance mode >>>[0m"
|
|
|
|
|
else
|
|
|
|
|
echo "[1;32m<<< System shutdown >>>[0m"
|
|
|
|
|
fi
|
2009-03-06 13:27:47 +01:00
|
|
|
|
echo ""
|
2010-12-02 21:23:45 +01:00
|
|
|
|
|
|
|
|
|
${config.powerManagement.powerDownCommands}
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
2009-11-06 22:08:06 +01:00
|
|
|
|
export PATH=${pkgs.utillinux}/bin:${pkgs.utillinux}/sbin:$PATH
|
2010-12-02 21:23:45 +01:00
|
|
|
|
|
2009-11-06 22:08:06 +01:00
|
|
|
|
|
2009-03-06 13:27:47 +01:00
|
|
|
|
# Do an initial sync just in case.
|
2009-08-11 23:08:05 +02:00
|
|
|
|
sync
|
2009-11-06 22:08:06 +01:00
|
|
|
|
|
|
|
|
|
|
2010-06-10 00:29:06 +02: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"
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
2009-08-11 23:08:05 +02:00
|
|
|
|
echo "sending the TERM signal to all processes..."
|
2010-06-10 00:29:06 +02:00
|
|
|
|
${pkgs.sysvtools}/bin/killall5 -15 $job $omittedPids
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
2009-03-06 13:27:47 +01:00
|
|
|
|
sleep 1 # wait briefly
|
2009-08-11 23:08:05 +02:00
|
|
|
|
|
|
|
|
|
echo "sending the KILL signal to all processes..."
|
2010-06-10 00:29:06 +02:00
|
|
|
|
${pkgs.sysvtools}/bin/killall5 -9 $job $omittedPids
|
2009-11-06 22:08:06 +01: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 22:51:28 +01:00
|
|
|
|
echo "[1;32m<<< Maintenance shell >>>[0m"
|
2009-11-06 22:08:06 +01:00
|
|
|
|
echo ""
|
2010-06-02 23:10:48 +02:00
|
|
|
|
${pkgs.shadow}/bin/login root
|
2009-11-06 22:38:40 +01:00
|
|
|
|
initctl emit -n startup
|
2009-11-06 22:08:06 +01:00
|
|
|
|
exit 0
|
|
|
|
|
fi
|
2010-06-08 14:48:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Write a shutdown record to wtmp while /var/log is still writable.
|
|
|
|
|
reboot --wtmp-only
|
|
|
|
|
|
|
|
|
|
|
2010-06-02 18:04:08 +02:00
|
|
|
|
# Set the hardware clock to the system time.
|
|
|
|
|
echo "setting the hardware clock..."
|
2012-07-11 21:33:34 +02:00
|
|
|
|
hwclock --systohc ${if config.time.hardwareClockInLocalTime then "--localtime" else "--utc"}
|
2010-06-08 12:06:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Stop all swap devices.
|
|
|
|
|
swapoff -a
|
2010-06-10 00:29:06 +02:00
|
|
|
|
|
|
|
|
|
|
2009-03-06 13:27:47 +01: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 13:39:48 +02:00
|
|
|
|
# systems mounted on other file systems and so on.
|
2009-03-06 13:27:47 +01:00
|
|
|
|
tryAgain=1
|
|
|
|
|
while test -n "$tryAgain"; do
|
|
|
|
|
tryAgain=
|
2010-06-08 13:52:16 +02:00
|
|
|
|
failed= # list of mount points that couldn't be unmounted/remounted
|
|
|
|
|
|
2011-11-03 03:15:33 +01:00
|
|
|
|
# Get rid of loopback devices.
|
|
|
|
|
loDevices=$(losetup -a | sed 's#^\(/dev/loop[0-9]\+\).*#\1#')
|
|
|
|
|
if [ -n "$loDevices" ]; then
|
|
|
|
|
echo "removing loopback devices $loDevices..."
|
|
|
|
|
losetup -d $loDevices
|
|
|
|
|
fi
|
|
|
|
|
|
2010-06-08 13:52:16 +02:00
|
|
|
|
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-17 00:16:43 +02:00
|
|
|
|
# Skip various special filesystems. Non-existent
|
|
|
|
|
# mount points are typically tmpfs/aufs mounts from
|
|
|
|
|
# the initrd.
|
2011-07-16 21:27:45 +02:00
|
|
|
|
if [ "$mp" = /proc -o "$mp" = /sys -o "$mp" = /dev -o "$device" = "rootfs" -o "$mp" = /run -o "$mp" = /var/run -o "$mp" = /var/lock -o ! -e "$mp" ]; then continue; fi
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
2009-03-06 13:27:47 +01:00
|
|
|
|
echo "unmounting $mp..."
|
2009-08-11 23:08:05 +02:00
|
|
|
|
|
2009-12-17 00:59:05 +01: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 13:52:16 +02:00
|
|
|
|
success=
|
|
|
|
|
if mount -t "$fstype" -n -i -o remount,ro "device" "$mp"; then success=1; fi
|
2009-12-17 00:59:05 +01:00
|
|
|
|
|
2009-08-11 23:08:05 +02: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.
|
2011-10-30 16:19:58 +01:00
|
|
|
|
if [ "$mp" != / -a "$mp" != /nix -a "$mp" != /nix/store ]; then
|
2010-06-08 13:52:16 +02:00
|
|
|
|
if umount -n "$mp"; then success=1; tryAgain=1; fi
|
2009-03-06 13:27:47 +01:00
|
|
|
|
fi
|
2010-06-08 13:52:16 +02:00
|
|
|
|
|
|
|
|
|
if [ -z "$success" ]; then failed="$failed $mp"; fi
|
2009-03-06 13:27:47 +01:00
|
|
|
|
done
|
|
|
|
|
done
|
2010-06-08 12:06:00 +02:00
|
|
|
|
|
|
|
|
|
|
2010-06-08 13:52:16 +02: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
|
2011-12-26 17:40:13 +01:00
|
|
|
|
echo Enter 'i' to launch a shell, or wait 10 seconds to continue.
|
|
|
|
|
read -t 10 A
|
|
|
|
|
if [ "$A" == "i" ]; then
|
2011-12-30 17:57:39 +01:00
|
|
|
|
${pkgs.bashInteractive}/bin/bash -i < /dev/console &> /dev/console
|
2011-12-26 17:40:13 +01:00
|
|
|
|
fi
|
2010-06-08 13:52:16 +02:00
|
|
|
|
sleep 5
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
2009-03-06 13:27:47 +01:00
|
|
|
|
# Final sync.
|
2009-08-11 23:08:05 +02:00
|
|
|
|
sync
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
|
|
|
|
|
2009-11-06 22:51:28 +01:00
|
|
|
|
# Either reboot or power-off the system.
|
2009-11-06 22:08:06 +01:00
|
|
|
|
if test "$MODE" = reboot; then
|
2009-08-11 23:08:05 +02:00
|
|
|
|
echo "rebooting..."
|
|
|
|
|
sleep 1
|
2009-03-06 13:27:47 +01:00
|
|
|
|
exec reboot -f
|
|
|
|
|
else
|
2009-08-11 23:08:05 +02:00
|
|
|
|
echo "powering off..."
|
|
|
|
|
sleep 1
|
2009-03-06 13:27:47 +01:00
|
|
|
|
exec halt -f -p
|
|
|
|
|
fi
|
2009-10-12 18:36:19 +02:00
|
|
|
|
'';
|
|
|
|
|
};
|
2009-03-06 13:27:47 +01:00
|
|
|
|
|
2009-12-17 00:59:05 +01:00
|
|
|
|
}
|