Add swap (optional)

Swap is worth adding even on a machine with plenty of memory. It gives the kernel somewhere to put pages that genuinely are not being used, which frees real memory for the page cache, and it turns “the machine froze solid” into “the machine got slow” when something runs away with allocations.

On btrfs a swapfile is easier to live with than a swap partition: it costs no partitioning decision and can be resized or deleted later with ordinary file commands. It does have requirements – no copy-on-write, no compression, fully allocated – and btrfs filesystem mkswapfile handles all of them:

Run this on the live system:
btrfs filesystem mkswapfile --size 8g --uuid clear /mnt/swap/swapfile
swapon /mnt/swap/swapfile
(stdout)
create swapfile /mnt/swap/swapfile size 8.00GiB (8589934592) page size 4096

Size it to taste. Matching your RAM is the traditional advice and is only strictly necessary for hibernation, which this book does not set up; 8 GiB is a reasonable default on a machine with enough disk to spare.

Turning it on now, during the install, is not ceremony – pacstrap in the next chapter unpacks a few hundred megabytes, and on a low-memory machine the swap you just created is what keeps that from failing.

Confirm it:

[bash]: Run this on your workstation:
swapon --show
(stdout)
NAME               TYPE SIZE USED PRIO
/mnt/swap/swapfile file   8G   0B   -2
Page size on the Raspberry Pi

On the Pi the same command reports page size 16384 rather than 4096, because linux-rpi-16k is built with 16 KiB pages. That is expected and nothing to fix.

It does leave one trap: a swapfile records the page size it was made with, and a kernel with a different page size refuses to enable it. So if you ever move this disk to a generic 4 KiB-page arm64 kernel, swapon will fail with Invalid argument – delete the swapfile and make a new one rather than trying to debug it.

With the disk partitioned, formatted, mounted under /mnt and swapped on, the target is ready to receive a system. The next chapter installs one.

// settings
theme:
fx: