Nixpkgs: Swap file created by the module not compatible with Linux 5.7

Created on 2 Jul 2020  Â·  12Comments  Â·  Source: NixOS/nixpkgs

Describe the bug

I have the following in my configuration.nix:

{pkgs}: {
  boot.kernelPackages = pkgs.linuxPackages_latest;
  swapDevices = [
    {
      device = "/var/swap";
      size = 1024 * 8 * 2; # twice the RAM should leave enough space for hibernation
    }
  ];
}

but the service fails with:

Jul 02 06:32:27 kaiser swapon[3741]: swapon: /var/swap: swapon failed: Invalid argument
Jul 02 06:32:27 kaiser kernel: swapon: swapfile has holes
Jul 02 06:32:27 kaiser systemd[1]: var-swap.swap: Swap process exited, code=exited, status=255/EXCEPTION

Apparently this is because the swapfile is created by fallocate:

https://github.com/NixOS/nixpkgs/blob/48704fbd4fc4dc9660ab04f938048b4894ecb140/nixos/modules/config/swap.nix#L197

which kernel does not like.

See the Arch bug and the kernel bug.

Running nixos-unstable @ 22a81aa5fc15b2d41b12f7160a71cd4a9f3c3fa1.

bug good-first-bug nixos

Most helpful comment

Find out how to detect files with holes in them.

To clarify: these files _does not_ have holes. It's a bug in the kernel causing the kernel to think it has holes when using it as swap.

All 12 comments

As a walkaround, I ran the dd code path manually:

sudo dd if=/dev/zero of=/var/swap bs=1M count=$((1024 * 8 * 2))
sudo mkswap /var/swap
sudo systemctl restart var-swap.swap

The size you use for the dd command needs to exactly match the size set in the module, otherwise the module will just call fallocate again, ruining your work.

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/how-do-i-set-up-a-swap-file/8323/3

Any update here?

The fix should be rather straightforward:

  1. Remove the fallocate branch.
  2. Find out how to detect files with holes in them.
  3. Add that as another condition that will trigger dd when not fulfilled.
  4. Test it thoroughly.

Find out how to detect files with holes in them.

To clarify: these files _does not_ have holes. It's a bug in the kernel causing the kernel to think it has holes when using it as swap.

Tagging this for 20.09 since it will likely break swap for people upgrading. cc @NixOS/nixos-release-managers

There is now a patch that's likely to be merged: https://lore.kernel.org/linux-fsdevel/[email protected]/

Just as a reminder, the 20.09 release is scheduled to happen this monday, the 28th.

If this is still relevant to blocking the release, then there should be some forward movement.

A blocker meeting has still yet to be scheduled. But, if you consider this item to still warrant blocking the entirety of the nixos-20.09 release, then please post on the Feature freeze discussion issue. A template for proposing an item can be found https://github.com/NixOS/nixpkgs/issues/95475#issuecomment-699218336

Looks like linuxPackages_latest → linuxPackages_5_7 bump also happened in 20.03 so people using swap created by the module and running linuxPackages_latest already encountered the bug. People using regular linuxPackages will not be affected in 20.09 since it still points to linuxPackages_5_4. Moving this to 21.03 Blockers.

Since this is a good first bug, I'd like to help with this. I'm a bit confused with the approach to this though. This says that a patch was applied for the problem, but I don't see it in the linux kernel source tree.

Can we use an overlay to add the patch to the linux-5.7 package?

I would expect https://github.com/NixOS/nixpkgs/blob/c2bb4af48d26ed091e5674394bacbf8d488c7939/pkgs/os-specific/linux/kernel/patches.nix would be the right place to add a patch to, plus listing it in pkgs/top-level/all-packages.nix (search for linux_5_7 =).

For testing, you can use boot.kernelPatches NixOS option.

I made a PR here. I'll tag you in there, you can take a look whenever you can find the time :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ob7 picture ob7  Â·  3Comments

lverns picture lverns  Â·  3Comments

matthiasbeyer picture matthiasbeyer  Â·  3Comments

yawnt picture yawnt  Â·  3Comments

sid-kap picture sid-kap  Â·  3Comments