ZFS adding swap space

I setup this HDD with zfs but no swap, now even with 32GB of ram I am running into issues related to swap.

I created a swap file but I've been told that's a bad idea and can lead to deadlocks. My goal is to eventually debug the programs that I am using to see how to lower their memory usage but for now I just want to add a swap partition.

My hdd layout looks like this:
gpart show
=>       40  500118112  nvd0  GPT  (238G)
         40     409600     1  efi  (200M)
     409640       1024     2  freebsd-boot  (512K)
     410664        984        - free -  (492K)
     411648    4194304     3  freebsd-swap  (2.0G)
    4605952  495511552     4  freebsd-zfs  (236G)
  500117504        648        - free -  (324K)
I tried reading this but I do not want to add a disk: https://www.freebsd.org/doc/handbook/disks-adding.html

This guide describes doing the partitioning on FreeBSD install, I don't want to reinstall my OS: https://www.freebsd.org/doc/handbook/bsdinstall-partitioning.html#configtuning-initial

This document leads to the previous two I listed above: https://www.freebsd.org/doc/handbook/adding-swap-space.html

So how can I actually repartition my hdd to get 4 or 8GB of additional swap space.

My hdd currently has 160GB free and that's nvd0p4 from the gpart show.
How can I shrink that space by 4-8GB and create a swap parition there w/o destroying my setup?
  • Thanks
Reactions: Oko
This is one of the things which are better to remember for your next FreeBSD installation.
Because ZFS has one limitation over other end-user filesystems: you cannot shrink.

Don't worry, I had to set up a swap file too in my first ZFS installation, too.
It's easy, start reading with man swapon.
It worked fine a long time, didn't notice any problem.

However when I installed new release (onto other HDDs) I took the default of 2 GB swap. Why?
1. It does not hurt.
2. You'll be happy you did reserve some space because some HDD in the mirror failed and you find that the replacement is a very little bit smaller, so you cannot attach the new disk.

I notice you do not like the idea of losing your setup.
I guess you will notice that you'd like to pay much more than the $20 you "saved" if you could restore your setup, just because you didn't buy a mirror drive, as soon as you happen to notice your drive has failed.

Buy another, make sure it is maybe 5-10 GB bigger, take just a 300gb one, install a fresh system onto that, adding a swap partition big enough (50 GB or so) that you can later make a mirror.
Then copy your setup to the new HDD.
Finally when you made sure all is set up well on the new HDD, make a new filesystem on the old HDD (throwing away the old ZFS partition) and simply attach that to your new HDD.

Then you do not need to grief when a HDD dies.
You can take that easy then, as a mere low-level annoyance of having to change a drive and resilvering.


Staff member
How can I shrink that space by 4-8GB and create a swap parition there w/o destroying my setup?
You can't. You will have to destroy the freebsd-zfs and freebsd-swap partitions and create new ones (destroying the data in the process).
Dear bcomputerguy,
if you really have a spare disk it is a perfect oppertunity to test backup and restore with zfs send and zfs receive. You can back-up to your spare disk and try to boot from that. If that works you could rearrange the actual disk and restore the system again without having any headaches.
I do get some hardware lock-ups since adding this swap but I figure out that instead of adding additional swap space to a machine with 32GB of ram, the best solution might be to figure out what exactly is chewing up so much RAM.