How to create a swap area on a partition and make it available at boot?

Fedora 41

I’m trying to create a swap area on a dedicated partition. I’m reading the mkswap man page, and I have a question on that.

Here is my partition configuration on the external hard drive that I will use to hold the swap area.

$ lsblk -b -i -o name,fstype,fsver,label,partlabel,partuuid,uuid,size
NAME        FSTYPE FSVER LABEL   PARTLABEL            PARTUUID                             UUID                                          SIZE
sda                                                                                                                             2000365289472
|-sda1      ext4   1.0   disk2s1 disk2s1              baaed8f5-307f-439a-9df6-1c6943b7a78e c4c592ac-e97a-42a3-8671-a6e8a54f27fc  965998542848
|-sda2      ext4   1.0   disk2s2 disk2s2              e63e835d-4bd9-4080-ad7d-0ade6e6881bf ddf57568-9704-4426-9a07-5c4c6d932946  434000363520
|-sda3      ext4   1.0   disk2s3 disk2s3              188ca273-2588-414a-89dc-cb4141f84f02 9db4ba26-573c-431e-af61-664330d4ed99  434000363520
`-sda4      ext4   1.0           disk2s4              b09cc65d-8e56-42ec-be32-b1f266a4bf27 6337d4af-12ea-4235-8311-60a92993a49b  137999941632

I will use partition /dev/sda4 for the swap area.

And here is my /etc/fstab file (excerpted to show only the entries on the external hard disk to contain the swap partition):

# fs_spec            fs_file               fs_vfstype     fs_mntops                              fs_freq  fs_passno
LABEL=disk1s1        /mnt/disk1s1          ext4           auto,noexec,nosuid,nodev,nofail        0 0
LABEL=disk1s2        /mnt/disk1s2          ext4           defaults,nofail                        0 0
LABEL=disk1s3        /mnt/disk1s3          ext4           defaults,nofail                        0 0
LABEL=disk1s4        none                  swap           sw,nofail                              0 0

I don’t understand the following quote excerpted from the man page:

After creating the swap area, you need the swapon(8) command to start using it. Usually swap areas are listed in /etc/fstab so that they can be taken into use at boot time by a swapon -a command in some boot script.

According to the mkswap man page, after I run mkswap, I need to run swapon. I believe that the swap partition will be mounted and available to the system when I boot because it is defined in /etc/fstab. Is this accurate? In other words, I don’t have to run swapon in a startup script (or manually after boot) do I?

The mkswap command completed with the following output:

$ sudo mkswap --check /dev/sda4
[sudo] password for rhimbo: 
0 bad pages
mkswap: /dev/sda4: warning: wiping old ext4 signature.
Setting up swapspace version 1, size = 128.5 GiB (137999937536 bytes)
no label, UUID=460e5fb4-bcdc-416d-99be-c35c8ddbbdb7
$ 
$

I don’t understand the last line, considering that the partition I defined for swap does have a label, namely disk1s4. I defined that label using parted when I was partitioning the external hard drive.

So now I guess I could run swapon. But I’m wondering if I could just reboot to see if the swap partition gets ‘mounted’ and is available to the system.

Partition labels and filesystem labels are two different things. You can always use sgdisk to set partition labels. For filesystems, however, you have to use different commands depending on the filesystem. Also, the rules about what characters are allowed and how long the label can be will vary from filesystem to filesystem. I prefer to use partition labels. In /etc/fstab, LABEL=... refers to the filesystem label. You have to use PARTLABEL=... to refer to the partition label. There are also corresponding /dev/disk/by-label and /dev/disk/by-partlabel directories where you can find listings of your labels and what devices they are referring to.

You’ll need to make an entry in /etc/fstab if you want your swap partition to automount. One possible exception is if you are booting a Live image. I’m not sure if it is still the case, but Live images used to automount swap partitions that were labeled SWAP. (I think the label had to be made using the -L parameter to mkswap.)

1 Like

The default of using zram swap is not suitable for your use case?

This topic brings back memories:

At work we were offered use of a system in a cluster running a numerical model if we put it our small machine room. When we turned it on the fans ramped up and our UPS reported an overload — the numerical model was supposed to sit idle when the system was being used for other things, but the startup needed all CPU’s and lots of swap to store the state of the numerical model. We had to find space for it a machine room with a bigger UPS.

1 Like

Right, got it. But I thought that I do have appropriate labels for both file systems and partitions; I used parted. Did I do something wrong (see the output of lsblk in my first post, link to it below).

I used the same text for both the partition and file system labels. I don’t know if that is an issue.

OK, thanks, @glb . Yes, I created the entries in /etc/fstab (hopefully correctly!). I’ll run swapon as my system is up and running. Then, next time I boot, I’ll test and verify that the swap partition was ‘mounted’ (is that the correct usage of the term?) at boot.

I hesitate to boot as each time I have to reposition and resize all my windows… 'cause the KDE Plasma ‘Save Session’ just don’t work, at least not while running Wayland.
:wink:

Well, my understanding is that zram is a swap partition (not an area for paging) that uses RAM. I wonder why I would want to render some of my RAM inaccessible and unusable to running processes. Even if it were intended for paging this does not make sense to me.

But perhaps I’m missing something about what zram is or how it functions…? I don’t know…

Linux misnames the page files as swap files. Linux only pages, never swaps.

The ram is not reserved. Zram paging allocates ram when you need to page out. It can compress the pages which is how it free up memory.

Because disk is very slow (even SSD) compared to ram the zram prevents problems with system stalls.

Fedora has been shipping with zram as the default for the desktop for a long time and it is almost always better than disk swap.

1 Like

It is best if you stick with the default configuration when you are not sure a change is necessary. Getting help with non-standard configurations requires a lot more work to explain your setup, and few others will have a similar configuration, so the potential to get help is greatly reduced. There are now good tools to resize partitions and make use of free space, so there is little downside to having an unused partition while you try running with zram. Note that zram is now waidely uased acros linux distros – it is not specific to Fedora. As is often the case, Arch Linux has a good explanation of zram.

Is this the case even for those of us who are memory bound?

I have a system with only 8GB of memory. I use a pdf editor (PDF Studio) that will bring my system to its knees, meaning it’ll stall until the OOM killer acts, when viewing large, image-laden pdf files.

This topic is making me think I need to revisit the possibility of using a swap partition to alleviate this. Is it possible, or even make sense, to use a swap partition in parallel to zram swap?

Thanks!

You can use a swap partition to workaround insufficient RAM. Just be warned that unless you have some really fast SSDs, it will degrade your system’s performance as compared to having adequate RAM for the software you are trying to run.

Edit: Also be warned that using swap memory heavily will reduce the lifetime of your SSD. And, when SSDs fail, they tend to go all at once. If possible, you might want to use a separate SSD for your swap memory so it doesn’t take everything else out with it when it goes.

Yes, it is perfectly fine to have both ZRAM and normal swap partitions.

1 Like

Understood. But for my under-RAM’d system, I’m just going to have to do the best I can manage with the army I have, not the army I’d like to have. :slight_smile:

Thanks for your help!

1 Like

Thanks…! I created the entry in /etc/fstab. I ran mkswap on the partition. I ran swapon successfully. Now swapon --summary reports as expected.

1 Like

Done…! Thanks, @glb

I did make one change to my /etc/fstab. The entry in question was like this:
LABEL=disk1s4 none swap sw,nofail 0 0

I changed it to this:
LABEL=disk1s4 swap swap sw,nofail 0 0

1 Like

Linux doesn’t have the concept of swap space…?! Well, I definitely learned something today…!

That being said, I don’t see how using RAM for either paging or swapping makes sense with respect to performance or resource management. Even in regard to paging, a system pages when there is not enough RAM to accommodate the main memory needs of a process or thread. Thus using RAM for paging just exacerbates the problem by reducing the amount of RAM available for program execution. How does this make sense?

Perhaps the motivation was to make it more convenient for users by eliminating the need to do system configuration or sysadmin stuff. But why would the system not just reserve some of the hard disk space for paging (or swap), for example, during installation of the OS?

Did OOM Killer act on PDF Studio in a recent version of Fedora with zram? Many programs allocate large blocks of memory that are highly compressible (such as images). You may find that zram swapping provides much better performance than swapping to mass storage.

Because of the typical 4:1 compression your 8GiB ram is more like 16GiB ram. It’s easy to compress typical pages in memory. When under memory pressure the time to free ram is critical to keeping the system responsive.

1 Like

Yep.

The documents in question were several hundred page pdf files with numerous imported large-format architectural and engineering plans containing hand-drawn figures. It was very repeatable and unmistakable.

I’m confident that zram swap is the best method. Unfortunately, I’m using a low-end laptop with soldered RAM on the motherboard that cannot be expanded.