Add a swap file or partition

How do I add swap to a TrueNAS scale system such that the swap is used across reboots? Is configuring it in fstab possible?

The best answer is, “you don’t.” If your system uses swap, it means you need RAM.

But with that said, truncate -s 16G /path/to/swapfile will create the file, mkswap /path/to/swapfile will set it up as a swap file, and swapon /path/to/swapfile will activate it. You’ll need to repeat the latter on reboot, which you can automate by setting that last command as a post-init task through the GUI.

But install more RAM.

2 Likes

Thanks but I know how to create swap files. My question is how do I add one so that I don’t have to run swapon after every reboot? Is there no provision for that in TrueNAS?

No, there isn’t. Again, you shouldn’t need swap.

That’s a terrible answer IMO. Anyway, here is my situation. I have two TrueNAS scale instances running as VM’s on two different Proxmox servers, the Proxmox servers have HBA’s dedicated to the VM’s with PCI pass through. The VM’s are configured identically as far as memory, disks, etc… The only difference is one VM is running ElectricEel-24.10.2 and the other is now running 25.04-RC.1. They are small NAS instances with 7 TB volumes with datasets that are shared with SMB and NFS. I allocated 16GB to both and that seemed to work fine until I ran into a problem.

On the 24.10 NAS, I was bench marking my disk bandwidth using dd with a large 16G file. While running that I got an Input/Output IO error and I suspected a disk or HBA problem. I ended up fixing a heat problem with the HBA but in the meantime, zfs reported that the 16G file was corrupt so, I copied it over from the backup NAS and ran a checksum on both NAS to compare. On the 24.10 NAS I got the Input/Output I/O error while running the checksum. I spent a great deal of time re-checking cables, disks. Finally, I ran dmesg to see if it would point to a disk issue. What I found was that my sum command was being killed due to out of memory. It’s interesting that this did not happen when running sum on the 25.04 machine. Anyway, I halted the 24.10 VM and allocated more memory to it and retested. No more out of memory and the file checksum matched the 25.04 machine.

You can say I solved the problem by allocating more memory to the VM but, I would still like to allocate swap to both VM’s just to avoid a recoccurence as the Proxmox servers have other VM’s and docker instances that need memory as well.

1 Like

Unfortunately there is no good solution other than more RAM. If using SWAP, then that is a problem and sometimes when a person has an unstable system, it gets traced to use of SWAP. No one should ever rely on it. Now I will admit that some folks use what feels like no RAM at all and rely on SWAP to save them. I don’t think you are that person since you did find a suitable solution.

This is very reasonable and requires no manual work upon reboot, it is automatic.

Thanks, I’ll try that. I thought I had to add it to fstab.

I don’t recall asking. That’s simply the way it is, your opinion notwithstanding. And though you seem to have overlooked it, I told you how to automate adding the swap on boot, so you don’t need to do anything manually.

FreeNAS, TrueNAS CORE, and TrueNAS SCALE through 23.10, all used swap,[1] but even there it was never intended that it would be used as swap–it was just a 2 GB partition on each of the pool drives that was intended to allow some “slop” space in case a replacement disk was slightly smaller than the others in the pool. Even then–going back nearly fifteen years–if you were actually using swap, something was badly wrong.

SCALE 24.04 kind of broke memory/swap management, and among the fixes were that the swap partitions were done away with. Swappiness was also changed such that the system wouldn’t use swap even if it had it. I don’t know if those changes persist in 24.10 or will be in 25.04.

tl;dr: even if you add swap, there’s a good chance the system won’t use it.


  1. They “used swap” in that they created swap partitions on the pool disks, and added them to the system as swap space, but were not intended to actually consume any of that swap space. ↩︎

3 Likes

I didn’t mean that personally, I just didn’t like the answer that I “No there isn’t”. Anyway, you’re right, I added swap, went back to my original 16G of memory and I run out of memory running sum on the large file. No choice but to add more memory.

1 Like

The main reason Truenas doesn’t make it easy to add swap is it is HORRIBLE for performance and with modern system memory being so cheap, why would you want to run swap? Most newer Linux versions do not set up swap by default as it did in the old days. I am running mini pcs with 96GB on each to host my VMs using Truenas on bare metal as the SAN to host the images. It works great and ZFS loves 96GB of memory. I paid $200 for 2x48GB sodimms just last week. Cheap and high performance compared to swap.

Yes, I’ve seen systems swapping under heavy load and they are slow but, they continue to run without crashing and usually this was a short term situation and they returned to normal. Also, some paging was hardly noticeable.

If you only knew how many “unstable system” bug reports just overnight disappeared the moment we got rid of swap…

3 Likes

I don’t know anything about the bugs that disappeared but typically, applications with memory leaks don’t behave well when a system is swapping. Just saying ….

Don’t get me wrong, I understand your arguments. Memory is cheap and everybody has gobs of it but I still feel like you need a swap partition for those rare times when there might be a memory short fall. A swap partition on an SSD these days can make the performance hit more bearable. Swapping and paging in my view is preferred over crashing. I’m old and have worked with UNIX since 1980 and no swap just goes against the grain with me but, I guess I can live with it :slight_smile:

@kris can correct me if I am wrong but there was a feature request to allow the user to create a ZFS partition slightly smaller than the entire drive capacity in a coming version of TrueNAS (don’t know which one) the purpose is to allow for capacity slop. This was proposed shortly after SWAP was removed from SCALE. The group as a whole is trying to take a proactive stance before problems occur.

Example:
You have an old failing 12TB drive has 11.5TB of capacity while the new replacement 12TB drive has 11.3TB of capacity. This means that the new drive cannot replace the old drive, it is too small. Drive must be of equal or greater capacity during a replacement.

The new empty partition will be there to provide a buffer as @dan mentioned before which is what the old SWAP partitions provided.

@jjrushford I know you have the answer you were looking for, well an answer and way to move forward and I understand you meant this without malice. I hope this works for you but you have heard the group, more RAM is the best solution. Or maybe on your system, a few less applications running :slight_smile:

1 Like

I believe it was accepted for 25.04’s release. But it would be only 2 GB max by default, so it would only account for pretty small discrepancies. See:

1 Like

These pools (sda/b: mirror, sdc/d/e/f: raidz2) were created under Fangtooth. There’s no partition but it seems that a small padding is there.
And there’s swap on the boot drive :wink:

admin@draco[~]$ lsblk -bo NAME,MODEL,PTTYPE,TYPE,START,SIZE,PARTTYPENAME         
NAME        MODEL                  PTTYPE TYPE    START           SIZE PARTTYPENAME
sda         WDC WDS100T1R0A-68A4W0 gpt    disk           1000204886016 
└─sda1                             gpt    part     2048   998056656896 Solaris /usr & Apple ZFS
sdb         WDC WDS100T1R0A-68A4W0 gpt    disk           1000204886016 
└─sdb1                             gpt    part     2048   998056656896 Solaris /usr & Apple ZFS
sdc         WDC WD120EFAX-68UNTN0  gpt    disk          12000138625024 
└─sdc1                             gpt    part     2048 11997991141376 Solaris /usr & Apple ZFS
sdd         WDC WD120EFAX-68UNTN0  gpt    disk          12000138625024 
└─sdd1                             gpt    part     2048 11997991141376 Solaris /usr & Apple ZFS
sde         WDC WD120EFAX-68UNTN0  gpt    disk          12000138625024 
└─sde1                             gpt    part     2048 11997991141376 Solaris /usr & Apple ZFS
sdf         WDC WD120EFAX-68UNTN0  gpt    disk          12000138625024 
└─sdf1                             gpt    part     2048 11997991141376 Solaris /usr & Apple ZFS
nvme0n1     INTEL SSDPEKKW128G7    gpt    disk            128035676160 
├─nvme0n1p1                        gpt    part     4096        1048576 BIOS boot
├─nvme0n1p2                        gpt    part     6144      536870912 EFI System
├─nvme0n1p3                        gpt    part 34609152   110315773440 Solaris /usr & Apple ZFS
└─nvme0n1p4                        gpt    part  1054720    17179869184 Linux swap

I 'm sure it did for a lot of reasons. However, there are still valid reasons to use swap, as most distributions still recommend. I’d prefer it but I do realize also lesser trained people on cheap hardware probably necessitated removing it. Properly configured, swap is never ever used on any system I ever managed, unless, it’s truly out of memory. I realize this was not the case with Scale so I get it.

SCALE 24.04 kind of broke memory/swap management, and among the fixes were that the swap partitions were done away with. Swappiness was also changed such that the system wouldn’t use swap even if it had it. I don’t know if those changes persist in 24.10 or will be in 25.04.

I noticed that vm.swappiness is set to 1 on my 24.10 machine so as to limit or eliminate any kind of swapping. I might play around with this.

I just want to mention that I think we had a good and interesting discussion here. I’ve allocated more memory to my two TrueNAS VM’s and they’re quite happy. I’ll leave swappiness alone. Thanks for all the discussion and everyone’s help!