What is the correct way to setup a swap partition/file?

Hi

I have a Linksys WRT1900AC which comes with 256MB RAM which is far more than a lot of routers, but I have installed the Speedtest client from https://github.com/sivel/speedtest-cli and when doing the tests It does utilise RAM so SWAP will be useful here.

Using https://oldwiki.archive.openwrt.org/doc/uci/fstab as the guide I have succesfully enabled SWAP by using the following commands:-

mkswap -L SWAP /dev/sdb2
swapon /dev/sdb2

From my understanding you have to create a Linux Swap partition for which the SWAP file goes onto correct?? Just with the above command is that always necessary or can I just soley create a SWAP partition with the SWAP file.

I created the SWAP partiion by installing 'fdisk' and setting the '/dev/sdb2' partition to Linux Swap (Hex 82) and then through the LuCI GUI > System > Mount Points and clicking on the add button under SWAP. However, I couldn't ever get this to work. The overview never showed SWAP and when uisng the 'free' command the SWAP showed as 0. On the other hand creating the SWAP file seems to fix this.

Slightly off-topic but does the SWAP file survive factory resets? To cut a long story short I compiled my own firmware which I have pre-setup all of the configurations settings so it is literally plug and play. Instead of issuing the 'mkswap' command everytime, is there a directory or file that holds the SWAP file settings?

Could someone shed some light on this please?

Many thanks

Will

Your first step would be to install media that isn't flash for swap. Well, actually your first step should be considering if there is a better way to accomplish what you want. If your router's CPU and memory can't handle a benchmarking application running on it, that suggests to me that any tests made would be very suspect in their results.

Back to swap -- running swap (or anything with repeated writes) on flash is a sure way to grind it into the ground quickly. You should consider an SSD in a USB enclosure, or perhaps a very fast writing USB stick. (Regrettably, the ones SanDisk used to make that were moderately fast aren't made any more.) You're probably not going to get much more than 5-15 MB/s out of a USB stick for write, so you can do the math about how long a couple dozen MB gets written to swap. Yep, go get a coffee. Small capacity, good quality SSDs are now available for ~US$50-75 for 250/500 MB, a decent USB 3 case (might as well, they're effectively the same price as USB 2) is another US$10 or so.

Swap files surviving anything is a bit of a strange concept. By definition, their contents are transient and should be considered opaque to anything but the running kernel. If set up as a partition of "normal" drive, then that partition is part of the disk's data and is unaffected by reset of the OS to ROM defaults (assuming you don't have something really wonky in a custom ROM). As far as the configuration of swap goes, unless you've baked that into a custom image, it is like any other post-install configuration and get wiped on reset to ROM defaults.

2 Likes