diff --git a/configuration/examples/basic.nix b/configuration/examples/basic.nix
index 0676f6e39fc..718cfb7f83c 100644
--- a/configuration/examples/basic.nix
+++ b/configuration/examples/basic.nix
@@ -9,7 +9,9 @@
}
];
- swapDevices = ["/dev/sdb1"];
+ swapDevices = [
+ { device = "/dev/sdb1"; }
+ ];
services = {
sshd = {
diff --git a/system/options.nix b/system/options.nix
index 21c2c7b2493..5828beb097d 100644
--- a/system/options.nix
+++ b/system/options.nix
@@ -212,10 +212,19 @@
{
name = ["swapDevices"];
default = [];
- example = ["/dev/hda7" "/dev/hdb3" "/var/swapfile"];
+ example = [
+ {device="/dev/hda7";}
+ {device="/var/swapfile";}
+ {label="bigswap";}
+ ];
description = "
The swap devices and swap files. These must have been
- initialised using mkswap.
+ initialised using mkswap. Each element
+ should be an attribute set specifying either the path of the
+ swap device or file (device) or the label
+ of the swap device (label, see
+ mkswap -L). Using a label is
+ recommended.
";
}
diff --git a/system/upstart.nix b/system/upstart.nix
index f2227a729cd..d46c51f9dc2 100644
--- a/system/upstart.nix
+++ b/system/upstart.nix
@@ -46,7 +46,7 @@ import ../upstart-jobs/gather.nix {
# Swapping.
(import ../upstart-jobs/swap.nix {
- inherit (pkgs) utillinux;
+ inherit (pkgs) utillinux library;
swapDevices = config.get ["swapDevices"];
})
diff --git a/upstart-jobs/swap.nix b/upstart-jobs/swap.nix
index 54756072bc2..e5938cb16da 100644
--- a/upstart-jobs/swap.nix
+++ b/upstart-jobs/swap.nix
@@ -1,4 +1,14 @@
-{utillinux, swapDevices}:
+{library, utillinux, swapDevices}:
+
+let
+
+ devicesByPath =
+ map (x: x.device) (library.filter (x: x ? device) swapDevices);
+
+ devicesByLabel =
+ map (x: x.label) (library.filter (x: x ? label) swapDevices);
+
+in
{
name = "swap";
@@ -8,21 +18,25 @@ start on startup
start on new-devices
script
- for device in ${toString swapDevices}; do
- # !!! Check whether we are already swapping to $device.
+ for device in ${toString devicesByPath}; do
${utillinux}/sbin/swapon \"$device\" || true
done
- # Remove swap devices not listed in swapDevices.
- for used in $(cat /proc/swaps | grep '^/' | sed 's/ .*//'); do
- found=
- for device in ${toString swapDevices}; do
- if test \"$used\" = \"$device\"; then found=1; fi
- done
- if test -z \"$found\"; then
- ${utillinux}/sbin/swapoff \"$used\" || true
- fi
+ for label in ${toString devicesByLabel}; do
+ ${utillinux}/sbin/swapon -L \"$label\" || true
done
+
+ # Remove swap devices not listed in swapDevices.
+ # !!! disabled because it doesn't work with labels
+ #for used in $(cat /proc/swaps | grep '^/' | sed 's/ .*//'); do
+ # found=
+ # for device in $ {toString swapDevices}; do
+ # if test \"$used\" = \"$device\"; then found=1; fi
+ # done
+ # if test -z \"$found\"; then
+ # ${utillinux}/sbin/swapoff \"$used\" || true
+ # fi
+ #done
end script
";