From 82c3e2aa50713cf8669173360edeffb21228d701 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 6 Nov 2009 21:08:06 +0000 Subject: [PATCH] * Updated poweroff/reboot/maintenance mode for Upstart 0.6. Upstart no longer emits specific events for those. Instead it emits a "runlevel" event. The "runlevel" task starts the "shutdown" task to perform the desired action. * Upstart 0.6 no longer has a "shutdown" event, so "stop on shutdown" no longer works. Therefore the shutdown task explicitly stops all running Upstart jobs, before sending a TERM/KILL signal to all remaining processes. * Do a "chvt 1" at the start of the shutdown task to switch to the console. * Use /dev/console instead of /dev/tty1, since if somebody is logged in on tty1, bad things will happen. svn path=/nixos/branches/upstart-0.6/; revision=18224 --- modules/module-list.nix | 4 +- modules/system/boot/stage-2-init.sh | 7 +-- .../upstart-events/maintenance-shell.nix | 22 -------- modules/system/upstart-events/runlevel.nix | 25 +++++++++ .../upstart-events/{halt.nix => shutdown.nix} | 53 ++++++++++++------- 5 files changed, 64 insertions(+), 47 deletions(-) delete mode 100644 modules/system/upstart-events/maintenance-shell.nix create mode 100644 modules/system/upstart-events/runlevel.nix rename modules/system/upstart-events/{halt.nix => shutdown.nix} (75%) diff --git a/modules/module-list.nix b/modules/module-list.nix index eab8503f1eeb..8c5cdcfd9dd4 100644 --- a/modules/module-list.nix +++ b/modules/module-list.nix @@ -126,8 +126,8 @@ ./system/boot/stage-2.nix ./system/etc/etc.nix ./system/upstart-events/control-alt-delete.nix - ./system/upstart-events/halt.nix - ./system/upstart-events/maintenance-shell.nix + ./system/upstart-events/runlevel.nix + ./system/upstart-events/shutdown.nix ./system/upstart/upstart.nix ./tasks/filesystems.nix ./tasks/kbd.nix diff --git a/modules/system/boot/stage-2-init.sh b/modules/system/boot/stage-2-init.sh index a95ff1873ef4..5e1a2104ca3b 100644 --- a/modules/system/boot/stage-2-init.sh +++ b/modules/system/boot/stage-2-init.sh @@ -141,9 +141,6 @@ export MODULE_DIR=@kernel@/lib/modules/ # For debugging Upstart. #@shell@ --login < /dev/console > /dev/console 2>&1 & -# Start Upstart's init. We start it through the -# /var/run/current-system symlink indirection so that we can upgrade -# init in a running system by changing the symlink and sending init a -# HUP signal. +# Start Upstart's init. echo "starting Upstart..." -exec /var/run/current-system/upstart/sbin/init -v +PATH=/var/run/current-system/upstart/sbin exec init diff --git a/modules/system/upstart-events/maintenance-shell.nix b/modules/system/upstart-events/maintenance-shell.nix deleted file mode 100644 index a8eceb93158a..000000000000 --- a/modules/system/upstart-events/maintenance-shell.nix +++ /dev/null @@ -1,22 +0,0 @@ -{ config, pkgs, ... }: - -###### implementation - -{ - jobs.maintenance_shell = - { name = "maintenance-shell"; - - startOn = [ "maintenance" "stalled" ]; - - task = true; - - script = - '' - exec < /dev/tty1 > /dev/tty1 2>&1 - echo \ - echo "<<< MAINTENANCE SHELL >>>" - echo "" - exec ${pkgs.bash}/bin/sh - ''; - }; -} diff --git a/modules/system/upstart-events/runlevel.nix b/modules/system/upstart-events/runlevel.nix new file mode 100644 index 000000000000..750d9a07f50c --- /dev/null +++ b/modules/system/upstart-events/runlevel.nix @@ -0,0 +1,25 @@ +{ config, pkgs, ... }: + +with pkgs.lib; + +{ + + jobs.runlevel = + { name = "runlevel"; + + startOn = "runlevel [0123456S]"; + + task = true; + + script = + '' + case "$RUNLEVEL" in + 0) initctl start shutdown MODE=poweroff;; + 1) initctl start shutdown MODE=maintenance;; + 6) initctl start shutdown MODE=reboot;; + *) echo "Unsupported runlevel: $RUNLEVEL";; + esac + ''; + }; + +} diff --git a/modules/system/upstart-events/halt.nix b/modules/system/upstart-events/shutdown.nix similarity index 75% rename from modules/system/upstart-events/halt.nix rename to modules/system/upstart-events/shutdown.nix index 36efc3fe2cf5..a8519edbd84f 100644 --- a/modules/system/upstart-events/halt.nix +++ b/modules/system/upstart-events/shutdown.nix @@ -2,29 +2,29 @@ with pkgs.lib; -###### implementation +{ -let - - inherit (pkgs) bash utillinux; - - jobFun = event: - { startOn = event; + jobs.shutdown = + { name = "shutdown"; task = true; + environment = { MODE = "poweroff"; }; + script = '' set +e # continue in case of errors + + ${pkgs.kbd}/bin/chvt 1 - exec < /dev/tty1 > /dev/tty1 2>&1 + exec < /dev/console > /dev/console 2>&1 echo "" echo "<<< SYSTEM SHUTDOWN >>>" echo "" - export PATH=${utillinux}/bin:${utillinux}/sbin:$PATH - + export PATH=${pkgs.utillinux}/bin:${pkgs.utillinux}/sbin:$PATH + # Set the hardware clock to the system time. echo "setting the hardware clock..." hwclock --systohc --utc @@ -32,6 +32,15 @@ let # Do an initial sync just in case. sync + + + # Stop all Upstart jobs. + initctl list | while read jobName rest; do + if test "$jobName" != shutdown; then + echo "stopping $jobName..." + stop "$jobName" + fi + done # Kill all remaining processes except init and this one. @@ -42,7 +51,20 @@ let echo "sending the KILL signal to all processes..." kill -KILL -1 - + + + # 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 "" + echo "<<< MAINTENANCE SHELL >>>" + echo "" + while ! ${pkgs.bash}/bin/bash --login; do true; done + initctl emit startup + exit 0 + fi + # Unmount helper functions. getMountPoints() { @@ -101,7 +123,7 @@ let # Either reboot or power-off the system. Note that the "halt" # event also does a power-off. - if test ${event} = reboot; then + if test "$MODE" = reboot; then echo "rebooting..." sleep 1 exec reboot -f @@ -113,9 +135,4 @@ let ''; }; -in - -{ - jobs = listToAttrs (map (n: nameValuePair "sys-${n}" (jobFun n)) - [ "reboot" "halt" "system-halt" "power-off" ] ); -} +} \ No newline at end of file