Add a Swap partition

So I got to checking out NoHang, the low-memory handler for Linux. I ran nohang -m to test it, and it works.

That got me to thinking about how much swap space I had, so I ran swapon --show... I've only got 2 GB of swap space in the sda2 partition.

Well, changing the size of that partition isn't going to be easy, so I decided to set up a second swap partition, with a bit more space.

I have a 4 TB USB hard drive attached... 1 TB is used as a ZFS mirror of the 1 TB internal drive, and the rest is currently unused. So in the Disks application, at the end of the drive, I created a partition equal to the size of my memory plus 2 GB (14 GB total).

I then formatted the partition. I gave it the same name as the original swap partition... "Swap", and I clicked "Other", selected "Linux Swap Partition", then clicked the arrow button to activate the swap partition.

swapon --show
NAME TYPE SIZE USED PRIO
/dev/sda2 partition 2G 699.3M -2
/dev/sdb3 partition 12.7G 0B -3

You'll note a 14 GB partition only translates to a 12.7 GB swap space.

I then tested again:

nohang -m

Enter the numbers 405776 to confirm that you are not a robot: 405776

Warning! The process will consume memory until 40 MiB of memory
(MemAvailable + SwapFree) remain free, and it will be terminated via SIGUSR1
at the end. This may cause the system to freeze and processes to terminate.
Do you want to continue? [No/Yes] Yes
Memory consumption has started!
MemAvailable: 109 MiB, SwapFree: 3617 MiB
Terminated

And:

swapon --show
NAME TYPE SIZE USED PRIO
/dev/sda2 partition 2G 729M -2
/dev/sdb3 partition 12.7G 411.7M -3

The CPU fan speed increased a bit while nohang -m was gobbling memory, the interface got a little slower right at the end, but overall, nothing major.

If you want to clear your swap file:

sudo swapoff -a
... wait until the hard drives stop thrashing as it chucks swap data back to memory...
sudo swapon -a

swapon --show
NAME TYPE SIZE USED PRIO
/dev/sda2 partition 2G 0B -2
/dev/sdb3 partition 12.7G 0B -3

One thing I noticed is that the drive containing the sdb3 partition spun down when I cleared the swap file, necessitating that I go into the Disks application and click the arrow button for that partition to spin up the drive.

Now you can reboot, just to be sure that the machine is still operating correctly. You'll note that the new Swap partition isn't automatically mounted. Let's fix that.

First, go into the Disks application and click the arrow button to mount that partition.

Next, you have to get the UUID of the partition you're now using as a Swap partition... sudo blkid

In my case, I'm looking for /dev/sdb3, the Swap partition.

/dev/sdb3: LABEL="Swap" UUID="192e39a6-0fd0-450c-a5f3-ba801f028195" TYPE="swap" PARTUUID="91e96b6a-d5b6-493a-b832-9f267c3a8495"

sudo gedit /etc/fstab

Then I'd enter (following the template of the existing Swap partition):

UUID=192e39a6-0fd0-450c-a5f3-ba801f028195 none swap discard 0 0

I just copied the line from the existing Swap partition, then changed the UUID.

Then paste that line (it won't be the same as above... you have to follow the steps and find your own UUID) into fstab, save the file, then reboot.

After you've booted:

swapon --show
NAME TYPE SIZE USED PRIO
/dev/sdb3 partition 12.7G 0B -2
/dev/sda2 partition 2G 0B -3