Activating swap works sometimes, fails eventually

I have a PC with 16 GB RAM which isn’t quite enough for my obelisk-project that, among others, compiles pandoc.

I decided to add a swap partition to my system. In order to avoid trouble, I am using a conventional (i.e. slooow) hard-drive for that. My SSD is where NixOS resides and by using the conventional hard-drive, I avoid the process of resizing my partition on the SSD which would require booting into NixOS from a usb device and so on.

I used the graphical interface of gparted and create a space on the unmounted conventional hard-drive: 29.80 GiB, formatted as “linux-swap” and put this into my configuration:

# /etc/nixos/hardware-configuration.nix
 swapDevices = [                                                                      
       { device = "/dev/sdb2";
         size = 31990;
       }
    ];

The size I put originally is 29.80 GiB in Megabytes = 31997.

That worked immediately, without reboot. I could compile w/o running out of memory and everything seemed fine.

However …

Whenever I reboot now, I get errors which first delay the boot process and then leave me without my swap partition.

I checked the error with

$ systemctl status mkswap-dev-sdb2.service

× mkswap-dev-sdb2.service - Initialisation of swap device /dev/sdb2
     Loaded: loaded (/etc/systemd/system/mkswap-dev-sdb2.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Fri 2023-02-24 06:52:19 -05; 36min ago
   Main PID: 637 (code=exited, status=1/FAILURE)
        CPU: 13.137s

Feb 24 06:51:51 ruben mkswap-dev-sdb2-start[653]: dd: error writing '/dev/sdb2': No space left on device
Feb 24 06:52:19 ruben mkswap-dev-sdb2-start[653]: 30518+0 records in
Feb 24 06:52:19 ruben mkswap-dev-sdb2-start[653]: 30517+0 records out
Feb 24 06:52:19 ruben mkswap-dev-sdb2-start[653]: 31999393792 bytes (32 GB, 30 GiB) copied, 295.342 s, 108 MB/s
Feb 24 06:52:19 ruben systemd[1]: mkswap-dev-sdb2.service: Main process exited, code=exited, status=1/FAILURE
Feb 24 06:52:19 ruben systemd[1]: mkswap-dev-sdb2.service: Failed with result 'exit-code'.
Feb 24 06:52:19 ruben systemd[1]: Failed to start Initialisation of swap device /dev/sdb2.
Feb 24 06:52:19 ruben systemd[1]: mkswap-dev-sdb2.service: Consumed 13.137s CPU time, no IP traffic.
Notice: journal has been rotated since unit was started, output may be incomplete.

… where it says “No space left on device”. Fair enough: lowering the size of my swap partition made it work again, but not for long. I lowered from 31997 to 31990 and it worked only until I rebooted again – so maybe it isn’t really about size/disk space.

If it is about the correct size, how do I determine the correct value (and why do my values work until reboot?)

Any idea? Google is awfully quiet on the topic.

According to the option description in man configuration.nix:

       swapDevices.*.size
           If this option is set, ‘device’ is interpreted as the path of a swapfile that will be created automatically with the indicated size (in megabytes).

so setting size and using a block device is not an intended use mode. It’s attempting to make that a swapfile, not a swap device. Just leave size unset, and it should use the partition table like normal things do.

1 Like

that was it. So the solution is even simpler. Gotta love that about NixOS. Also I didn’t know about man configuration.nix, thanks!