Merge pull request #623 from muggenhor/feat/swap-discard

swap: support swapDevices' discardPolicy and priority options
This commit is contained in:
lassulus 2024-05-11 18:33:55 +02:00 committed by GitHub
commit 159d87ea5b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 2 deletions

View file

@ -29,12 +29,14 @@
content = { content = {
type = "swap"; type = "swap";
randomEncryption = true; randomEncryption = true;
priority = 100; # prefer to encrypt as long as we have space for it
}; };
}; };
plainSwap = { plainSwap = {
size = "100%"; size = "100%";
content = { content = {
type = "swap"; type = "swap";
discardPolicy = "both";
resumeDevice = true; # resume from hiberation from this device resumeDevice = true; # resume from hiberation from this device
}; };
}; };

View file

@ -11,11 +11,32 @@
default = device; default = device;
description = "Device"; description = "Device";
}; };
discardPolicy = lib.mkOption {
default = null;
example = "once";
type = lib.types.nullOr (lib.types.enum [ "once" "pages" "both" ]);
description = lib.mdDoc ''
Specify the discard policy for the swap device. If "once", then the
whole swap space is discarded at swapon invocation. If "pages",
asynchronous discard on freed pages is performed, before returning to
the available pages pool. With "both", both policies are activated.
See swapon(8) for more information.
'';
};
extraArgs = lib.mkOption { extraArgs = lib.mkOption {
type = lib.types.listOf lib.types.str; type = lib.types.listOf lib.types.str;
default = [ ]; default = [ ];
description = "Extra arguments"; description = "Extra arguments";
}; };
priority = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
description = lib.mdDoc ''
Specify the priority of the swap device. Priority is a value between 0 and 32767.
Higher numbers indicate higher priority.
null lets the kernel choose a priority, which will show up as a negative value.
'';
};
randomEncryption = lib.mkOption { randomEncryption = lib.mkOption {
type = lib.types.bool; type = lib.types.bool;
default = false; default = false;
@ -53,7 +74,14 @@
default = lib.optionalAttrs (!config.randomEncryption) { default = lib.optionalAttrs (!config.randomEncryption) {
fs.${config.device} = '' fs.${config.device} = ''
if ! swapon --show | grep -q "^$(readlink -f ${config.device}) "; then if ! swapon --show | grep -q "^$(readlink -f ${config.device}) "; then
swapon ${config.device} swapon ${
lib.optionalString (config.discardPolicy != null)
"--discard${lib.optionalString (config.discardPolicy != "both")
"=${config.discardPolicy}"
}"} ${
lib.optionalString (config.priority != null)
"--priority=${toString config.priority}"
} ${config.device}
fi fi
''; '';
}; };
@ -64,7 +92,7 @@
default = [{ default = [{
swapDevices = [{ swapDevices = [{
device = config.device; device = config.device;
randomEncryption = config.randomEncryption; inherit (config) discardPolicy priority randomEncryption;
}]; }];
boot.resumeDevice = lib.mkIf config.resumeDevice config.device; boot.resumeDevice = lib.mkIf config.resumeDevice config.device;
}]; }];