Out of the box, Vultr FreeBSD servers are not configured to include swap space. If your intention is for a disposable cloud instance, you probably don’t need to worry about this. If you’ve created a “real” server that’s going to stick around a while, read on.
Even the most ridiculously decked-out server requires swap space. That’s just how the OS operates. During those times when your app goes a little nuts and eats more RAM than it should, having swap space will give you a fighting chance to regain control of the machine. Even under normal operation, FreeBSD is proactive about memory management. For example, long-running programs that are sitting idle in memory will be swapped to disk to make room for useful things, like filesystem caches. Running with zero swap is a recipe for unpredictable behavior, and no one running a server wants that!
Confirm swap status
Before we do anything, let’s make sure that we really don’t have any swap.
swapinfo
should return nothing. Like so:
vultr [~]# swapinfo
Device 1K-blocks Used Avail Capacity
vultr [~]#
If indeed it’s blank, let’s continue and make ourselves a swap file. But first…
How much swap is needed?
The old rules of thumb (1x RAM, 2x RAM, .5x RAM, etc) don’t really apply these days. If your server has 16GB of memory, you most certainly do not need to waste 32GB of your precious disk space on a swap file. That said, if you have crash dumps enabled, depending on how it’s configured, having swap equal to your memory may be required for it to work.
So, how big to make the swap file is really up to you. The good news is, you can always change it later, and with SSD-backed storage, you don’t have to worry too much about where it ends up on your disk. Anyway, let’s move on to…
Creating and using a swap file
We will create a 512 meg swap file located at /usr/swap0
. Feel free to change any of that if it doesn’t work for you. For example, if you want 1 gig of swap, change the 512 to 1024, and so on up. If you decide to change the filename, make sure you change both references to it.
Carefully copy the following, execute as root, and you’ll be all set:
dd if=/dev/zero of=/usr/swap0 bs=1m count=512
chmod 0600 /usr/swap0
echo "md99 none swap sw,file=/usr/swap0,late 0 0" >> /etc/fstab
swapon -aL
Confirm that it has worked:
vultr [~]# swapinfo
Device 1K-blocks Used Avail Capacity
/dev/md99 524288 0 524288 0%
At this point, you’re all set. Swap is active, and it will continue to work after a reboot.
Explanation for the curious
The dd
command creates the actual file. After that, we set appropriate permissions, add a line to /etc/fstab
for swap, and then turn it on. The command swapon -aL
tells the OS to activate all swap partitions/files in fstab, including those marked as late. Due to the way FreeBSD 10.2 boots, the swap file must be mounted late. When the OS boots normally, your swap will get turned on during the “mounting late filesystems” stage.
Want to contribute?
You could earn up to $300 by adding new articles
Suggest an update
Request an article