New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
zramSwap: allow configure compression algorithm + cleanups #52991
Conversation
I came across this also, and what I was doing for a while was that I added an
I also think there's a |
@worldofpeace yeah, according to gist https://gist.github.com/asssaf/f9bef956a755e67ed742e3265765e48d it is possible to do only with udev. However I'm not sure udev solution will apply required changes when options are changed. For example, when you change compressor and rebuild system, does it rebuild zram devices with new algo? |
It would rebuild the udev rules and I'm pretty sure those are monitored for changes so it would just reapply them once changed. |
Also, just learned that |
Alright, I've fixed things up.
cc for review @worldofpeace @infinisil @bjornfor @wmertens @wizeman |
This pull request has been mentioned on Nix community. There might be relevant details there: https://discourse.nixos.org/t/install-nixos-on-512-mb-ram/1713/13 |
nixos/modules/config/zram.nix
Outdated
description = '' | ||
Compression algorithm. <literal>lzo</literal> has good compression, | ||
but is slow. <literal>lz4</literal> has bad compression, but is fast. | ||
<literal>zstd</literal> is both good compression and fast (kernel 4.18+). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There could be an assertion generated if the kernel is too old (config.boot.kernelPackages.kernel.version)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd better remove this section. Current kernel is already capable of zstd zram.
nixos/modules/config/zram.nix
Outdated
totalmem=$(${pkgs.gnugrep}/bin/grep 'MemTotal: ' /proc/meminfo | ${pkgs.gawk}/bin/awk '{print $2}') | ||
mem=$(((totalmem * ${toString cfg.memoryPercent} / 100 / ${toString cfg.numDevices}) * 1024)) | ||
totalmem=$(${pkgs.gawk}/bin/awk '/MemTotal: /{print $2}' /proc/meminfo) | ||
mem=$(echo "$totalmem*${toString cfg.memoryPercent}/100.0/${toString devicesCount}*1024" | ${bc}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we depend on bc
in the minimal closure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise gawk could also perform floating point calculation I think.
- add `zramSwap.algorithm` option, which allows to change compressor declaratively. zstd as default - add `zramSwap.swapDevices` option, which allows to define how many zram devices will be used as swap. Rest devices can be managed freely - simpler floating calculations - fix udev race condition - some documentation changes - replaced `/sys/block/zram*` handling with `zramctl`, because I had occasional "Device is busy" error (looks like zram has to be configured in predefined order) - added `memoryPercent` and `algorithm` as restart triggers. I think, it was a bug that changing `memoryPercent` in configuration wasn't applied immediately. - removed a bind to .swap device. While it looks natural (when swap device goes off, so should zram device), it wasn't implemented properly. This caused problems with swapon/swapoff: ``` $ cat /proc/swaps Filename Type Size Used Priority /dev/zram0 partition 8166024 0 -2 /var/swapfile file 5119996 5120 1 $ sudo swapoff -a $ sudo swapon -a swapon: /dev/zram0: read swap header failed $ cat /proc/swaps Filename Type Size Used Priority /var/swapfile file 5119996 0 1 ```
@Mic92 addressed your issues. Also, added some release notes. I'll merge in a week if nobody beats me |
This creates a dependency cycle when used with boot.tmpOnTmpfs: basic.target <- tmp.mount <- swap.target <- zram-init-dev0 <- basic.target This same fix is done already for tmp.mount Fixes NixOS#47474
I can confirm, that the patch resolves the dependency cycle. |
Motivation for this change
As I mentioned in https://discourse.nixos.org/t/install-nixos-on-512-mb-ram/1713, I'm using zstd compression, but current module doesn't allow to change compressor declaratively. Here we go, and fix few problems with this module.
Things done
zramSwap.algorithm
option, which allows to change compressordeclaratively
/sys/block/zram*
handling withzramctl
, because I had occasional"Device is busy" error (looks like zram has to be configured in predefined order)
memoryPercent
andalgorithm
as restart triggers. I think, it wasa bug that changing
memoryPercent
in configuration wasn't applied immediately.off, so should zram device), it wasn't implemented properly. This caused problems
with swapon/swapoff:
sandbox
innix.conf
on non-NixOS)nix-shell -p nox --run "nox-review wip"
./result/bin/
)nix path-info -S
before and after)cc @Mic92