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
|
2011-09-14 19:20:50 +01: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 ""
|
2010-12-02 20:23:45 +00:00
|
|
|
|
|
|
|
|
|
${config.powerManagement.powerDownCommands}
|
2011-09-14 19:20:50 +01:00
|
|
|
|
|
2009-11-06 21:08:06 +00:00
|
|
|
|
export PATH=${pkgs.utillinux}/bin:${pkgs.utillinux}/sbin:$PATH
|
2010-12-02 20:23:45 +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
|
|
|
|
|
|
|
|
|
|
2010-06-09 23:29:06 +01: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 19:20:50 +01:00
|
|
|
|
|
2009-08-11 22:08:05 +01:00
|
|
|
|
echo "sending the TERM signal to all processes..."
|
2010-06-09 23:29:06 +01:00
|
|
|
|
${pkgs.sysvtools}/bin/killall5 -15 $job $omittedPids
|
2011-09-14 19:20:50 +01:00
|
|
|
|
|
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..."
|
2010-06-09 23:29:06 +01: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 22:10:48 +01: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 13:48:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Write a shutdown record to wtmp while /var/log is still writable.
|
|
|
|
|
reboot --wtmp-only
|
|
|
|
|
|
|
|
|
|
|
2010-06-02 17:04:08 +01:00
|
|
|
|
# Set the hardware clock to the system time.
|
|
|
|
|
echo "setting the hardware clock..."
|
2012-07-11 20:33:34 +01:00
|
|
|
|
hwclock --systohc ${if config.time.hardwareClockInLocalTime then "--localtime" else "--utc"}
|
2010-06-08 11:06:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Stop all swap devices.
|
|
|
|
|
swapoff -a
|
2010-06-09 23:29:06 +01: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 12:39:48 +01: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 12:52:16 +01:00
|
|
|
|
failed= # list of mount points that couldn't be unmounted/remounted
|
|
|
|
|
|
2011-11-03 02:15:33 +00: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 12:52:16 +01: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-16 23:16:43 +01:00
|
|
|
|
# Skip various special filesystems. Non-existent
|
|
|
|
|
# mount points are typically tmpfs/aufs mounts from
|
|
|
|
|
# the initrd.
|
2011-07-16 20:27:45 +01: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 19:20:50 +01:00
|
|
|
|
|
2009-03-06 12:27:47 +00:00
|
|
|
|
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).
|
2010-06-08 12:52:16 +01: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 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.
|
2011-10-30 15:19:58 +00:00
|
|
|
|
if [ "$mp" != / -a "$mp" != /nix -a "$mp" != /nix/store ]; then
|
2010-06-08 12:52:16 +01:00
|
|
|
|
if umount -n "$mp"; then success=1; tryAgain=1; fi
|
2009-03-06 12:27:47 +00:00
|
|
|
|
fi
|
2010-06-08 12:52:16 +01:00
|
|
|
|
|
|
|
|
|
if [ -z "$success" ]; then failed="$failed $mp"; fi
|
2009-03-06 12:27:47 +00:00
|
|
|
|
done
|
|
|
|
|
done
|
2010-06-08 11:06:00 +01:00
|
|
|
|
|
|
|
|
|
|
2010-06-08 12:52:16 +01: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 16:40:13 +00: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 16:57:39 +00:00
|
|
|
|
${pkgs.bashInteractive}/bin/bash -i < /dev/console &> /dev/console
|
2011-12-26 16:40:13 +00:00
|
|
|
|
fi
|
2010-06-08 12:52:16 +01:00
|
|
|
|
sleep 5
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
2009-03-06 12:27:47 +00:00
|
|
|
|
# Final sync.
|
2009-08-11 22:08:05 +01:00
|
|
|
|
sync
|
2011-09-14 19:20:50 +01: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
|
|
|
|
}
|