Swap file on LUKS partition

Hello,

for the past months I’ve been debugging build issues. My system froze whenever nix tried to rebuild one of the larger packages - firefox, thunderbird, seamonkey, etc.

I’ve finally traced it down to a swap issue: I have

swapDevices = [{
  device = "/swapfile";
  size = 32*1024;
  randomEncryption.enable = true;
}];

in my configuration. The root file system resides on a LUKS encrypted partition.

This appears to break swap, so that the system completely freezes as soon as swap is used - no crash log, no terminal, no kernel log message, just completely frozen.

Oddly this does not appear to improve when “randomEncryption.enable = true;” is removed.

If “/swapfile” is replaced by “/dev/disk/by-partuuid/…” of a dedicated (unencrypted) swap partition, everything works.

Is this indended/expected behavior?

This is definitely not expected behavior. Can you keep dmesg -w running in a terminal (potentially via ssh) to watch for kernel messages before the freeze happens?

Before you do that, though, to prevent potentially destructive mistakes, are you using btrfs? If so, check for data corruption before continuing to use the system, you can’t simply use a raw swap file on btrfs, you need to specially prepare it: Swapfile — BTRFS documentation

I really disrecommend using swapfiles in general, if you need more swap space just resize your partitions:

1 Like

So, firstly, I wonder why you’re using randomEncryption when the swapfile is stored on a LUKS encrypted file system already.

Anyway, yea swapfiles can be pretty tricky to get right. It matters a lot what file system you’re using under it, so we probably need to know that. And if you created the swapfile manually, we’d need to know how you did that.

2 Likes

I’ve tried that multiple times, there are no related kernel messages at all. I’ve also had the issue on two separate machines with the same NixOS configuration.

I’m using ext4 on all partitions.

I set up the swap before learning about randomEncryption, and resizing LUKS partitions can be a bit of a pain, so I had hoped I could get away with a swap file, as I basically never use it. But the randomEncryption option doesn’t make a difference, even with it set to false it freezes.

As far as I can remember, I didn’t manually set up the swapfile except for the line in the nixos configuration.

Might be related? Swapfile does not seem to work if randomEncryption.enable = true