2009-10-12 19:27:57 +02:00
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
|
|
|
with pkgs.lib;
|
2009-03-06 13:27:35 +01:00
|
|
|
|
2007-01-10 14:07:57 +01:00
|
|
|
let
|
|
|
|
|
2009-12-26 17:20:00 +01:00
|
|
|
inherit (pkgs) cryptsetup utillinux;
|
2009-05-28 01:14:38 +02:00
|
|
|
|
2009-07-16 16:51:49 +02:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
2009-05-28 01:14:38 +02:00
|
|
|
options = {
|
|
|
|
|
2009-07-16 16:51:49 +02:00
|
|
|
swapDevices = mkOption {
|
2009-05-28 01:14:38 +02:00
|
|
|
default = [];
|
|
|
|
example = [
|
|
|
|
{ device = "/dev/hda7"; }
|
|
|
|
{ device = "/var/swapfile"; }
|
|
|
|
{ label = "bigswap"; }
|
|
|
|
];
|
2009-07-16 16:51:49 +02:00
|
|
|
description = ''
|
2009-05-28 01:14:38 +02:00
|
|
|
The swap devices and swap files. These must have been
|
|
|
|
initialised using <command>mkswap</command>. Each element
|
|
|
|
should be an attribute set specifying either the path of the
|
|
|
|
swap device or file (<literal>device</literal>) or the label
|
|
|
|
of the swap device (<literal>label</literal>, see
|
|
|
|
<command>mkswap -L</command>). Using a label is
|
|
|
|
recommended.
|
2009-07-16 16:51:49 +02:00
|
|
|
'';
|
|
|
|
|
|
|
|
type = types.list types.optionSet;
|
|
|
|
|
2009-12-26 17:20:00 +01:00
|
|
|
options = {config, options, ...}: {
|
|
|
|
|
|
|
|
options = {
|
|
|
|
device = mkOption {
|
|
|
|
example = "/dev/sda3";
|
|
|
|
type = types.string;
|
|
|
|
description = ''
|
|
|
|
Path of the device.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
label = mkOption {
|
|
|
|
example = "swap";
|
|
|
|
type = types.string;
|
|
|
|
description = "
|
|
|
|
Label of the device. Can be used instead of <varname>device</varname>.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
|
|
|
cipher = mkOption {
|
|
|
|
default = false;
|
|
|
|
example = true;
|
|
|
|
type = types.bool;
|
|
|
|
description = "
|
2009-12-31 14:56:47 +01:00
|
|
|
Cipher the swap device to protect swapped data. This option
|
|
|
|
does not work with labels.
|
2009-12-26 17:20:00 +01:00
|
|
|
";
|
|
|
|
};
|
|
|
|
|
|
|
|
command = mkOption {
|
|
|
|
description = "
|
|
|
|
Command used to activate the swap device.
|
|
|
|
";
|
|
|
|
};
|
2009-07-16 16:51:49 +02:00
|
|
|
};
|
|
|
|
|
2009-12-26 17:20:00 +01:00
|
|
|
config = {
|
|
|
|
device =
|
|
|
|
if options.label.isDefined then
|
|
|
|
"/dev/disk/by-label/${config.label}"
|
|
|
|
else
|
|
|
|
mkNotdef;
|
|
|
|
|
|
|
|
command = ''
|
|
|
|
if test -e "${config.device}"; then
|
|
|
|
${if config.cipher then ''
|
2009-12-31 14:56:47 +01:00
|
|
|
plainDevice="${config.device}"
|
|
|
|
name="crypt$(echo "$plainDevice" | sed -e 's,/,.,g')"
|
|
|
|
device="/dev/mapper/$name"
|
|
|
|
if ! test -e "$device"; then
|
|
|
|
${cryptsetup}/sbin/cryptsetup -c aes -s 128 -d /dev/urandom create "$name" "$plainDevice"
|
|
|
|
${utillinux}/sbin/mkswap -f "$device" || true
|
|
|
|
fi
|
|
|
|
''
|
|
|
|
else ''
|
|
|
|
device="${config.device}"
|
|
|
|
''
|
|
|
|
}
|
2010-01-06 23:41:59 +01:00
|
|
|
device=$(readlink -f "$device")
|
2009-12-31 14:56:47 +01:00
|
|
|
# Add new swap devices.
|
|
|
|
if echo $unused | grep -q "^$device\$"; then
|
2010-01-26 10:56:53 +01:00
|
|
|
unused="$(echo $unused | grep -v "^$device\$" || true)"
|
2009-12-31 14:56:47 +01:00
|
|
|
else
|
|
|
|
${utillinux}/sbin/swapon "$device" || true
|
|
|
|
fi
|
2009-12-26 17:20:00 +01:00
|
|
|
fi
|
|
|
|
'';
|
2009-07-16 16:51:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2009-05-28 01:14:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
2009-07-16 16:51:49 +02:00
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = {
|
|
|
|
|
2009-10-12 20:09:34 +02:00
|
|
|
jobs.swap =
|
2009-10-12 19:27:57 +02:00
|
|
|
{ task = true;
|
2009-12-31 14:56:47 +01:00
|
|
|
|
2009-07-16 16:51:49 +02:00
|
|
|
startOn = ["startup" "new-devices"];
|
|
|
|
|
|
|
|
script =
|
2009-12-26 17:20:00 +01:00
|
|
|
''
|
2009-12-31 14:56:47 +01:00
|
|
|
unused="$(sed '1d; s/ .*//' /proc/swaps)"
|
|
|
|
|
2009-12-26 17:20:00 +01:00
|
|
|
${toString (map (x: x.command) config.swapDevices)}
|
2009-07-16 16:51:49 +02:00
|
|
|
|
2009-12-31 14:56:47 +01:00
|
|
|
# Remove remaining swap devices.
|
|
|
|
test -n "$unused" && ${utillinux}/sbin/swapoff $unused || true
|
2009-07-16 16:51:49 +02:00
|
|
|
'';
|
|
|
|
};
|
2009-05-28 01:14:38 +02:00
|
|
|
|
2009-07-16 16:51:49 +02:00
|
|
|
};
|
2009-05-28 01:14:38 +02:00
|
|
|
|
2006-12-21 02:07:23 +01:00
|
|
|
}
|