I have a 16GB SD card which shows as /dev/sdb unallocated space in Disks app. I can do
mkswap /dev/sdb
swapon /dev/sdbbut when I restart the whole thing disappears. How can I make it stay?
2 Answers
You don't want to do this if you can avoid it, as it is slow and will cause the SD card to fail prematurely. That said
You need to mount the disk as swap in /etc/fstab. (i.e. edit that file with a text editor, and add the appropriate line with a filesystem type if swap. While you can specify /dev/sdb if this SD card is reliably registered as this at startup you might be better served using blkid to find the devices unique identifier and use that.
You should use zram instead. It's much faster than HDD, SD, eMMC or an older SSD (and possibly even many new SSDs based to the benchmark results here with the default lzo algorithm) and causes no wear on the flash memory. You can change to lzo-rle or lz4 (which is nearly 5 times faster than lzo) for even better performance with a little bit less compression ratio. In the future there may be zstd support since it's been included in the latest Linux kernel. If you're low on RAM you can easily feel the difference system responsiveness after enabling it
Many modern OSes like ChromeOS, Android and various Linux distros (Lubuntu, Fedora) have enabled zram by default for years, especially for systems with less RAM. In fact Windows 10 and macOS also use similar pagefile compression techniques by default
You can make a small backup swap partition or swap file and keep it at a lower priority. If you're on Ubuntu just run sudo apt-get install zram-config and you're good to go. There's also zswap and zcache. I haven't tried them but they're probably worth a try