From d5fb41795f93127e6d20dac7ee532400e2d5a64d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Llu=C3=ADs=20Batlle=20i=20Rossell?= Date: Thu, 2 Dec 2010 20:23:45 +0000 Subject: [PATCH] Adding a wake on lan module. svn path=/nixos/trunk/; revision=24958 --- modules/config/power-management.nix | 22 +++++++-- modules/module-list.nix | 1 + modules/services/networking/wakeonlan.nix | 56 ++++++++++++++++++++++ modules/system/upstart-events/shutdown.nix | 4 +- 4 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 modules/services/networking/wakeonlan.nix diff --git a/modules/config/power-management.nix b/modules/config/power-management.nix index ce67285140e4..6baefa12791f 100644 --- a/modules/config/power-management.nix +++ b/modules/config/power-management.nix @@ -10,9 +10,14 @@ let '' #! ${pkgs.stdenv.shell} action="$1" - if [ "$action" = "resume" ]; then - ${cfg.resumeCommands} - ${cfg.powerUpCommands} + case "$action" in + hibernate|suspend) + ${cfg.powerDownCommands} + ;; + thaw|resume) + ${cfg.resumeCommands} + ${cfg.powerUpCommands} + ;; fi ''; @@ -50,6 +55,17 @@ in it resumes from suspend or hibernation. ''; }; + + powerDownCommands = mkOption { + default = ""; + example = "${pkgs.hdparm}/sbin/hdparm -B 255 /dev/sda"; + description = + '' + Commands executed when the machine powers down. That is, + they're executed both when the system shuts down and when + it goes to suspend or hibernation. + ''; + }; }; diff --git a/modules/module-list.nix b/modules/module-list.nix index 8c927aa61429..0c7b9b6592f0 100644 --- a/modules/module-list.nix +++ b/modules/module-list.nix @@ -107,6 +107,7 @@ ./services/networking/ssh/sshd.nix ./services/networking/tftpd.nix ./services/networking/vsftpd.nix + ./services/networking/wakeonlan.nix ./services/networking/wicd.nix ./services/networking/wpa_supplicant.nix ./services/networking/xinetd.nix diff --git a/modules/services/networking/wakeonlan.nix b/modules/services/networking/wakeonlan.nix new file mode 100644 index 000000000000..936936d29483 --- /dev/null +++ b/modules/services/networking/wakeonlan.nix @@ -0,0 +1,56 @@ +{ config, pkgs, ... }: + +with pkgs.lib; + +let + interfaces = config.services.wakeonlan.interfaces; + + ethtool = "${pkgs.ethtool}/sbin/ethtool"; + + passwordParameter = password : if (password == "") then "" else + "sopass ${password}"; + + methodParameter = {method, password} : + if method == "magicpacket" then "wol g" + else if method == "password" then "wol s so ${passwordParameter password}" + else throw "Wake-On-Lan method not supported"; + + line = { interface, method ? "magicpacket", password ? "" }: '' + ${ethtool} -s ${interface} ${methodParameter {inherit method password;}} + ''; + + concatStrings = fold (x: y: x + y) ""; + lines = concatStrings (map (l: line l) interfaces); + +in +{ + + ###### interface + + options = { + + services.wakeonlan.interfaces = mkOption { + default = [ ]; + example = [ + { + interface = "eth0"; + method = "password"; + password = "00:11:22:33:44:55"; + } + ]; + description = '' + Interfaces where to enable Wake-On-LAN, and how. Two methods available: + "magickey" and "password". The password has the shape of six bytes + in hexadecimal separated by a colon each. For more information, + check the ethtool manual. + ''; + }; + + }; + + + ###### implementation + + config.powerManagement.powerDownCommands = lines; + +} diff --git a/modules/system/upstart-events/shutdown.nix b/modules/system/upstart-events/shutdown.nix index 51b741d54e0a..85ad601914d8 100644 --- a/modules/system/upstart-events/shutdown.nix +++ b/modules/system/upstart-events/shutdown.nix @@ -29,9 +29,11 @@ with pkgs.lib; echo "<<< System shutdown >>>" fi echo "" + + ${config.powerManagement.powerDownCommands} export PATH=${pkgs.utillinux}/bin:${pkgs.utillinux}/sbin:$PATH - + # Do an initial sync just in case. sync