1

With this fresh install of Pi Zero 2W, after any reboot, if you check for a swap on the Raspberry Pi Zero 2 W, it has none terminal command: free

To prevent unnecessary IO (and to extend life), instead of making a swap file on the OS's SD card, I went ahead and created a swap file on a dedicated USB drive. This USB drive will always be connected and it's only purpose in life is to serve as a swap "container".

umount /dev/sda1
sudo mkfs.ext4 /dev/sda1
sudo mkswap /dev/sda1 -L Swap-USB1
sudo swapon /dev/sda1
free

USB Swap File

  1. The fist problem is that after a reboot the pi goes back to not using a swap!
  2. According to some sources, I need to edit /etc/dphys-swapfile. However the Zero 2W does not have a swap config file (Ubuntu)
  3. Then some report having issues with /etc/dphys-swapfile because it is loaded before the USB

How can I setup the swap file to be in use AFTER the USB has been loaded and to stay in use even after reboot?

1
  • Do you need swap? You can try to disable it altogether. Dec 7, 2023 at 15:17

1 Answer 1

1

There are three major things to do when creating a swap partition: set the partition type in the disk partition table, format the partition (you did this) and add an entry to /etc/fstab for the new swap partition.

First, use a partition management tool like fdisk, sfdisk or gparted to set the partition type. Assuming you have a DOS partition table (most USB drives ship this way), the partition type needs to be set to 82 "Linux swap". For example, use sfdisk like this:

sudo sfdisk --part-type /dev/sda 1 82

Next, make the swap partition just like you did. The label is not required. To anyone else following along, this will destroy all data on /dev/sda1 so be very sure sda1 is the right partition before issuing this command:

sudo mkswap /dev/sda1 -L Swap-USB1

From here, you can manually enable the swap, but that won't persist across reboots. So add the partition to /etc/fstab to make this swap partition available after reboot. It could be bad to accidentally use the wrong disk if you happen to reboot with another drive plugged in. So instead of listing /dev/sda1, get the unique identifier for that partition and stash it in an environment variable.

uuid=$(sudo blkid -s UUID -o value /dev/sda1)

Now create the fstab entry:

echo "UUID=${uuid} none swap sw 0 0" | sudo tee -a /etc/fstab

At this point, you can sudo swapon -a to enable all available swap partitions but a more cautious approach would be to sudo reboot to verify that your new swap partition becomes available after a reboot.

2
  • I imagine that a "Linux Swap" partition is different than an EXT4? I read that EXT4 was the best for swap but had no idea there's a swap-specific partition. If so, pretty cool
    – Omar
    Dec 5, 2023 at 4:12
  • @Omar, partition types can be things like "Linux", "Linux Swap", "Linux raid auto", etc. Filesystems can be things like ext4, swapfs, vfat, etc. It should be considered "wrong" to format a Linux Swap partition as ext4 just as it would be to format a Linux partition as swapfs. The standard and most direct way for the kernel to use swap is as a swap-partition. Swap-files inside ext4 are useful when adding a drive or restructuring the partition table isn't an option. There is added overhead when putting swapfs in another filesystem but I would agree that ext4 handles this better than most.
    – Kenny
    Dec 9, 2023 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.