2014-04-25 01:56:36 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.zramSwap;
|
|
|
|
|
2018-12-27 23:00:48 +01:00
|
|
|
# don't set swapDevices as mkDefault, so we can detect user had read our warning
|
|
|
|
# (see below) and made an action (or not)
|
|
|
|
devicesCount = if cfg.swapDevices != null then cfg.swapDevices else cfg.numDevices;
|
|
|
|
|
|
|
|
devices = map (nr: "zram${toString nr}") (range 0 (devicesCount - 1));
|
2014-04-25 01:56:36 +02:00
|
|
|
|
2016-08-14 12:00:52 +02:00
|
|
|
modprobe = "${pkgs.kmod}/bin/modprobe";
|
2014-04-25 01:56:36 +02:00
|
|
|
|
2018-12-27 23:00:48 +01:00
|
|
|
warnings =
|
|
|
|
assert cfg.swapDevices != null -> cfg.numDevices >= cfg.swapDevices;
|
|
|
|
flatten [
|
|
|
|
(optional (cfg.numDevices > 1 && cfg.swapDevices == null) ''
|
|
|
|
Using several small zram devices as swap is no better than using one large.
|
|
|
|
Set either zramSwap.numDevices = 1 or explicitly set zramSwap.swapDevices.
|
|
|
|
|
|
|
|
Previously multiple zram devices were used to enable multithreaded
|
|
|
|
compression. Linux supports multithreaded compression for 1 device
|
|
|
|
since 3.15. See https://lkml.org/lkml/2014/2/28/404 for details.
|
|
|
|
'')
|
|
|
|
];
|
|
|
|
|
2014-04-25 01:56:36 +02:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
zramSwap = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2018-12-27 23:00:48 +01:00
|
|
|
Enable in-memory compressed devices and swap space provided by the zram
|
|
|
|
kernel module.
|
2022-07-28 23:19:15 +02:00
|
|
|
See [
|
2018-12-27 23:00:48 +01:00
|
|
|
https://www.kernel.org/doc/Documentation/blockdev/zram.txt
|
2022-07-28 23:19:15 +02:00
|
|
|
](https://www.kernel.org/doc/Documentation/blockdev/zram.txt).
|
2014-04-25 01:56:36 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
numDevices = mkOption {
|
2018-04-19 16:44:08 +02:00
|
|
|
default = 1;
|
2014-04-25 01:56:36 +02:00
|
|
|
type = types.int;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2018-12-27 23:00:48 +01:00
|
|
|
Number of zram devices to create. See also
|
2022-07-28 23:19:15 +02:00
|
|
|
`zramSwap.swapDevices`
|
2018-12-27 23:00:48 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
swapDevices = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = 1;
|
|
|
|
type = with types; nullOr int;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2018-12-27 23:00:48 +01:00
|
|
|
Number of zram devices to be used as swap. Must be
|
2022-07-28 23:19:15 +02:00
|
|
|
`<= zramSwap.numDevices`.
|
|
|
|
Default is same as `zramSwap.numDevices`, recommended is 1.
|
2014-04-25 01:56:36 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
memoryPercent = mkOption {
|
|
|
|
default = 50;
|
|
|
|
type = types.int;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2022-08-31 15:15:02 +02:00
|
|
|
Maximum total amount of memory that can be stored in the zram swap devices
|
2014-04-25 01:56:36 +02:00
|
|
|
(as a percentage of your total memory). Defaults to 1/2 of your total
|
2022-08-31 15:15:02 +02:00
|
|
|
RAM. Run `zramctl` to check how good memory is compressed.
|
|
|
|
This doesn't define how much memory will be used by the zram swap devices.
|
2014-04-25 01:56:36 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-11-05 03:29:46 +01:00
|
|
|
memoryMax = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = with types; nullOr int;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2022-08-31 15:15:02 +02:00
|
|
|
Maximum total amount of memory (in bytes) that can be stored in the zram
|
2020-11-05 03:29:46 +01:00
|
|
|
swap devices.
|
2022-08-31 15:15:02 +02:00
|
|
|
This doesn't define how much memory will be used by the zram swap devices.
|
2020-11-05 03:29:46 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-04-25 01:56:36 +02:00
|
|
|
priority = mkOption {
|
|
|
|
default = 5;
|
|
|
|
type = types.int;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2014-04-25 01:56:36 +02:00
|
|
|
Priority of the zram swap devices. It should be a number higher than
|
|
|
|
the priority of your disk-based swap devices (so that the system will
|
|
|
|
fill the zram swap devices before falling back to disk swap).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-12-27 23:00:48 +01:00
|
|
|
algorithm = mkOption {
|
2020-05-21 20:30:03 +02:00
|
|
|
default = "zstd";
|
2019-03-07 01:07:52 +01:00
|
|
|
example = "lz4";
|
2018-12-27 23:00:48 +01:00
|
|
|
type = with types; either (enum [ "lzo" "lz4" "zstd" ]) str;
|
2022-08-15 07:16:25 +02:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Compression algorithm. `lzo` has good compression,
|
|
|
|
but is slow. `lz4` has bad compression, but is fast.
|
|
|
|
`zstd` is both good compression and fast, but requires newer kernel.
|
2018-12-27 23:00:48 +01:00
|
|
|
You can check what other algorithms are supported by your zram device with
|
2022-08-15 07:16:25 +02:00
|
|
|
{command}`cat /sys/class/block/zram*/comp_algorithm`
|
2018-12-27 23:00:48 +01:00
|
|
|
'';
|
|
|
|
};
|
2014-04-25 01:56:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
2018-12-27 23:00:48 +01:00
|
|
|
inherit warnings;
|
|
|
|
|
2014-04-25 01:56:36 +02:00
|
|
|
system.requiredKernelConfig = with config.lib.kernelConfig; [
|
|
|
|
(isModule "ZRAM")
|
|
|
|
];
|
|
|
|
|
|
|
|
# Disabling this for the moment, as it would create and mkswap devices twice,
|
|
|
|
# once in stage 2 boot, and again when the zram-reloader service starts.
|
|
|
|
# boot.kernelModules = [ "zram" ];
|
|
|
|
|
|
|
|
boot.extraModprobeConfig = ''
|
|
|
|
options zram num_devices=${toString cfg.numDevices}
|
|
|
|
'';
|
|
|
|
|
|
|
|
services.udev.extraRules = ''
|
|
|
|
KERNEL=="zram[0-9]*", ENV{SYSTEMD_WANTS}="zram-init-%k.service", TAG+="systemd"
|
|
|
|
'';
|
|
|
|
|
|
|
|
systemd.services =
|
|
|
|
let
|
|
|
|
createZramInitService = dev:
|
|
|
|
nameValuePair "zram-init-${dev}" {
|
|
|
|
description = "Init swap on zram-based device ${dev}";
|
|
|
|
after = [ "dev-${dev}.device" "zram-reloader.service" ];
|
|
|
|
requires = [ "dev-${dev}.device" "zram-reloader.service" ];
|
|
|
|
before = [ "dev-${dev}.swap" ];
|
|
|
|
requiredBy = [ "dev-${dev}.swap" ];
|
2019-01-17 20:18:45 +01:00
|
|
|
unitConfig.DefaultDependencies = false; # needed to prevent a cycle
|
2014-04-25 01:56:36 +02:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
2018-03-01 20:38:53 +01:00
|
|
|
ExecStop = "${pkgs.runtimeShell} -c 'echo 1 > /sys/class/block/${dev}/reset'";
|
2014-04-25 01:56:36 +02:00
|
|
|
};
|
|
|
|
script = ''
|
2018-12-27 23:00:48 +01:00
|
|
|
set -euo pipefail
|
|
|
|
|
2014-04-25 01:56:36 +02:00
|
|
|
# Calculate memory to use for zram
|
2018-12-27 23:00:48 +01:00
|
|
|
mem=$(${pkgs.gawk}/bin/awk '/MemTotal: / {
|
2020-11-05 03:29:46 +01:00
|
|
|
value=int($2*${toString cfg.memoryPercent}/100.0/${toString devicesCount}*1024);
|
|
|
|
${lib.optionalString (cfg.memoryMax != null) ''
|
|
|
|
memory_max=int(${toString cfg.memoryMax}/${toString devicesCount});
|
|
|
|
if (value > memory_max) { value = memory_max }
|
|
|
|
''}
|
|
|
|
print value
|
2018-12-27 23:00:48 +01:00
|
|
|
}' /proc/meminfo)
|
2014-04-25 01:56:36 +02:00
|
|
|
|
2020-11-24 16:29:28 +01:00
|
|
|
${pkgs.util-linux}/sbin/zramctl --size $mem --algorithm ${cfg.algorithm} /dev/${dev}
|
|
|
|
${pkgs.util-linux}/sbin/mkswap /dev/${dev}
|
2014-04-25 01:56:36 +02:00
|
|
|
'';
|
|
|
|
restartIfChanged = false;
|
|
|
|
};
|
|
|
|
in listToAttrs ((map createZramInitService devices) ++ [(nameValuePair "zram-reloader"
|
|
|
|
{
|
|
|
|
description = "Reload zram kernel module when number of devices changes";
|
2018-12-27 23:00:48 +01:00
|
|
|
wants = [ "systemd-udevd.service" ];
|
|
|
|
after = [ "systemd-udevd.service" ];
|
2019-01-17 20:18:45 +01:00
|
|
|
unitConfig.DefaultDependencies = false; # needed to prevent a cycle
|
2014-04-25 01:56:36 +02:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
|
|
|
ExecStartPre = "${modprobe} -r zram";
|
|
|
|
ExecStart = "${modprobe} zram";
|
|
|
|
ExecStop = "${modprobe} -r zram";
|
|
|
|
};
|
2018-12-27 23:00:48 +01:00
|
|
|
restartTriggers = [
|
|
|
|
cfg.numDevices
|
|
|
|
cfg.algorithm
|
|
|
|
cfg.memoryPercent
|
|
|
|
];
|
2014-04-25 01:56:36 +02:00
|
|
|
restartIfChanged = true;
|
|
|
|
})]);
|
|
|
|
|
|
|
|
swapDevices =
|
|
|
|
let
|
|
|
|
useZramSwap = dev:
|
|
|
|
{
|
|
|
|
device = "/dev/${dev}";
|
|
|
|
priority = cfg.priority;
|
|
|
|
};
|
|
|
|
in map useZramSwap devices;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|