Register for the iXsystems Community to get an ad-free experience and exclusive discounts in our eBay Store.
Resource icon

How to relocate swap to an SSD or other partition

FreeNAS by default will allocate 2GiB on each pool HD and use this as swap. Unfortunately, if a disk fails, your system may crash.

If you have an SSD, or another device/partition for use as swap it can be beneficial to relocate swap to that device. Alternatively, if you would rather locate the swap on the boot device, there is an alternate procedure documented here

At the same time it is beneficial to leave the default pool swap where it is because it allows you the ability to replace a failed HD with a similar HD which may be slightly smaller.

The tricky part is that FreeNAS rebuilds the swap config every time you reboot.

There is a simple solution :)

Firstly, in order to allocate a disk for swap you need to format it for swap usage.

WARNING: The below procedure will destroy ALL the data on the disk you intend to use for swap.

In order to format an entire disk for swap, use the following commands, where daX is the device you wish to format

gpart create -s gpt daX
gpart add -i 1 -t freebsd-swap daX

If the 'gpart create' command fails because the disk is busy, either wipe it in the GUI, or use
gpart destroy -F daX

This will create a swap partition which uses all available space on the device. If you wanted to you could create a partition on any device, of any size, the only important thing is that it is of type 'freebsd-swap'

Next, you will need the gptid for the partition you created

glabel status | grep daX

The gptid is used to safely identify the swap partition. The bit we are interested in the string of numbers/letters/dashes that follows "gptid/"

The above commands, when executed will look something like this:
Code:
root@freenas:~ # gpart create -s gpt da1
da1 created
root@freenas:~ # gpart add -i 1 -t freebsd-swap da1
da1p1 added
root@freenas:~ # glabel status | grep da1
gptid/7a273425-87da-11e7-9ec5-000c290148d6	 N/A  da1p1


And finally, in the GUI, click on Tasks -> Init/Shutdown Scripts, Add Init/Shutdown Script:

Then add a "Command" where the command is the following
Code:
swapoff -a ; grep -v -E 'none[[:blank:]]+swap[[:blank:]]' /etc/fstab > /etc/fstab.new && echo "/dev/gptid/<gptid>.eli none swap sw 0 0" >> /etc/fstab.new && mv /etc/fstab.new /etc/fstab ; swapon -a

Note: change the <gptid> in the above command to use the gptid that you retrieved from glabel in the above steps, ie the bit after "gptid/". Do not include the angle brackets (<>) in the final command

And set it to "Post Init" for "When"

Screen Shot 2017-08-23 at 11.39.54 PM.png



When finished it should look something like this, of course, your gptid will be different.

Screen Shot 2017-08-23 at 11.10.16 PM.png



Now restart.

To verify that the new swap is being used and the old swap system has been disabled you can run swapinfo

And you'll see something like this:
Code:
root@freenas:~ # swapinfo
Device		  1K-blocks	 Used	Avail Capacity
/dev/gptid/7a273425-87da-11e7-9   4192256		0  4192256	 0%


And you can check the /etc/fstab too
cat /etc/fstab

And it will look something like this:
Code:
root@freenas:~ # cat /etc/fstab
freenas-boot/grub	/boot/grub	zfs	rw,noatime	1	0
fdescfs	/dev/fd	fdescfs rw	0 0
/dev/gptid/7a273425-87da-11e7-9ec5-000c290148d6.eli none swap sw 0 0


And that's it.

I have a walk-through with screenshots here


Why do it this way, and not another?


This method of disabling/re-enabling swap does not require you to remove swap partitions from your pool drives, it makes no permanent changes to your installation, and it will not fail and accidentally use a precious data disk as a swap device, even if the device order changes.

It will also not affect the normal FreeBSD swapon/swapoff functionality, which means it will also work with my page-in-swap script.


What do I do if I want to change my swap disk?

Just change the gptid to point to your new swap partition.


What do I do if I want to go back to using the pool swap?

Just remove the Post-Init command and reboot.


But what is this doing?

Code:
swapoff -a ; grep -v -E 'none[[:blank:]]+swap[[:blank:]]' /etc/fstab > /etc/fstab.new && echo "/dev/gptid/7a273425-87da-11e7-9ec5-000c290148d6.eli none swap sw 0 0" >> /etc/fstab.new && mv /etc/fstab.new /etc/fstab ; swapon -a


first we disable all current swap devices
swapoff -a

then we create a new fstab without any swap devices
grep -v -E 'none[[:blank:]]+swap[[:blank:]]' /etc/fstab > /etc/fstab.new

then if that worked we add our new encrypted swap device to the new fstab. If you wanted to add multiple swap devices, you can repeat this clause multiple times.
echo "/dev/gptid/7a273425-87da-11e7-9ec5-000c290148d6.eli none swap sw 0 0" >> /etc/fstab.new

then if that worked, we replace the original fstab with the new fstab
mv /etc/fstab.new /etc/fstab

And finally, even if we failed to update the fstab, we re-enable swap.
swapon -a


How do you know this works?

When FreeNAS boots, the /conf/base/etc/ix.rc.d/ix-fstab script is run to regenerate the /etc/fstab file. The script replaces /etc/fstab with /conf/base/etc/fstab, which contains the default boot mount information, then it adds various mount options that are required according to the config and imported disks, and then finally it iterates over all imported pools and finds their swap partitions and adds an encrypted swap entry for each swap partition found.

This Post-Init command removes those swap entries safely, and then adds the partitions that you specify in the same way, except via gptid.


Caveats

If you detach or import a pool via the GUI, FreeNAS will regenerate the /etc/fstab. This will mean that swapoff -a will no longer work to temporarily disable your swap partition. This is not a big deal.

Additionally, when you import a pool, FreeNAS will re-enable that pool's swap partitions.

Both of these issues can be resolved by re-running the Post-Init command, and will be resolved at next reboot.
Author
Stux
Views
2,468
First release
Last update
Rating
5.00 star(s) 1 ratings

More resources from Stux

Latest reviews

Well done. It would be nice if freenas modified their base code to do something like this. It would improve the reliability of freenas.
Top