Adding a hardware swap partition

Mar 11, 2026 Last reply: 3 months ago 41 Replies

Is there a "recipe" for adding a hardware swap partition to an existing RasPiOS installation? Ideally I'd like a traditional layout, with swap situated between / and /usr. I know how to do it with FreeBSD during the install process but this particular case involves a running, somewhat valuable RasPiOS installation and the tools offered on RasPiOS are different enough to warrant a study of prior art if it's available.



What I'd like to do is resize the existing root to roughly its present, occupied size, add a swap partition in the freed space and then create /usr in the remaining space, copying the old /usr to the new, cleaning out usr files from the original root partition and mounting the new /usr on the empty mountpoint.



This needs to be done under single-user mode and I don't know how to get at single-user in RasPiOS. It could be done via booting from a microSD, but that pitches me into the installer which isn't exactly familiar territory.



In case it matters, this is on an 8GB Pi5 running Bookworm with dual monitors and a 1 TB mechanical hard drive. df reports



Filesystem 1K-blocks Used Available Use% Mounted on udev 4081296 0 4081296 0% /dev tmpfs 1650304 6512 1643792 1% /run /dev/sda2 961067256 61907904 850331260 7% / tmpfs 4125728 163136 3962592 4% /dev/shm tmpfs 5120 48 5072 1% /run/lock /dev/sda1 522230 79520 442710 16% /boot/firmware tmpfs 825136 272 824864 1% /run/user/1000



The need for "real" swap arises when Chromium and Firefox are both running with multiple tabs open.



Thanks for reading, and any suggestions!



bob prohaska


I don't think that is necessarily true.

If you create a bootable SD card, you can do all you want to the sda disk whilst it's online

You could also create a swap file in / and add that as an alternative

Yaj well no fine

I'd add a swap file. work of a few minutes only

$ dd if=/dev/zero of=/path/to/swapfile bs=1M count=1024 # For 1GB swap file $ mkswap /path/to/swapfile $ swapon /path/to/swapfile

This is done by modifying the kernel command line -- there should be a bootloader option to do this on a one-time basis before actually loading the kernel.

The option “single” should do the trick, though I think it will prompt for the root password before allowing you in.

Another option to try is “init=/bin/bash”.

These options should be common across Linux kernels, independent of architecture or bootloader.

Newsgroups: comp.sys.raspberry-pi From: Jim Jackson snipped-for-privacy@franjam.org.uk>

Subject: Re: Add> Is there a "recipe" for adding a hardware swap partition to an

AFAIK modern PIOS versions all use merged /usr, as they are based on debian which /usr merged. This means that the /usr directory has to reside on the root partition. I usually put my swap partition between root and /home partitions.

I'm assuming your PIOS is running systemd so try looking at

man systemd.swap

I remember that on a SYSV init system I think I ran mkswap on the disk partition, then added an entry to /etc/fstab like

/dev/sda2 none swap sw 0 0

and just rebooted.

I tend to use a swap file nowadays.

cheers Jim

I can understand editing cmdline.txt and rebooting as a way to get single user, but by the time there's anything visible on the monitor the kernel is already starting services and well past any opportunity to intervene.

It looks as if swap files have their advocates. Just how much slower is a swap file? It certainly looks easier and might be a good experiment.

Thanks to everybody for replying!

bob prohaska

No, this is using one of the options in the bootloader menu (“advanced”, I think it is) to do a one-time edit to the command line for that boot. You don’t want to permanently put the system to booting into single-user mode every time, do you?

I'm not familiar with such a menu, how is it invoked?

No, but I could edit cmdline.txt, reboot to single-user, finish my business, re-edit cmdline.txt and reboot again.

I was hopeful somebody would respond to my query regarding the relative speeds of swapfiles vs swap partitions. If they're anywhere close in performance a swapfile seems worth trying.

Thanks for writing!

bob prohaska

That is an interesting thought.

Google AI sez

"Key Differences and Performance Factors:

- Performance: A swap partition is technically faster as it writes directly to the disk, bypassing the filesystem, but this advantage is rarely noticeable in daily use.

- Flexibility: Swap files are much easier to resize, create, or delete without modifying disk partitions, making them the preferred choice for most modern setups.

- Fragmentation: On traditional HDDs, a fragmented swap file can be slower, but this is not an issue on SSDs.

- Hard Drive Placement: On spinning HDDs, a partition placed at the start of the drive is slightly faster, but this does not apply to SSDs.

Recommendation:

For the majority of users (especially on SSDs), a swap file is recommended for its convenience without any meaningful loss in speed. A swap partition is only recommended if you have specific, high-performance needs or are using older, heavily fragmented hard drives. "

I think that is an interesting summary, the point being made that speed is dominated today by disk access times, not CPU cycles in addressing a file system. And with the demise of spinning rust, there is no seek delay either, so the theoretical advantages of an actual partition in the disks low sector area, no longer outweigh the operational simplicity of a swap file.

Like so many other things that grew out of limited RAM and slow spinning rust disks, the swap partition is really no longer necessary...

One advantage of a swap partition is that it's a dedicated space - if you start running low on space it may limit the size of swapfile that can be created, which has a knock-on impact on performance.

Another reason is if you're using hibernate. You need swap space at least as large as RAM to save out your memory contents to, so that can prevent hibernate working if you are running low on space. Also hibernate/restore is a fairly low-level process and swap partitions are easier to setup for that than swap files.

Theo

I dont think you understand the nature of a swapfile.

It is a fixed length file.

Again, you are talking bollocks. Swap files are pre-allocated, of fixed size, and zero filled.

I think you will rue that decision later, unless you are sure that you are never going to add more packages (which will occupy some space in /etc and /var, even with the bulk of the space being in /usr), never do updates which may use more space in /boot, /etc/ or /var, and so on.

I haven't had a separate /usr partition on any system in a long time (and I'm wondering why you want to do that), so I can't give any helpful amounts on the minimum extra space you should leave. But I strongly advise you to give yourself enough extra space in / to handle future updates to your system.

Jim

Ah, it seems only Windows does variable length files.

Depending on the filesystem the swapfile may not be backed by physical blocks, ie the file exists but the space is not preallocated. It looks like you have to explicitly force that.

For swap files, hibernation appears to be more complicated:

formatting link
indicates you need to tell the kernel the offset of the swapfile within the partition, which suggests that the kernel is reading it directly rather than via the filesystem. That implies the filesystem must allocate it contiguously and is not allowed to have any kind of fragmentation. That means it could be impossible to set up on a machine that's been running a while.

Theo

Run: vmstat -t 10 60 in a terminal while you are doing your daily tasks in other windows to see how much swap I/O there really is.

This command does 60 measurements with an interval of 10 seconds, the si and so columns show the swap I/O.

Usually it is not much, so I wouldn't worry too much about performance.

dphys_swapfile creates a fixed size file on the disc, rather than dynamically increasing it.

I have got a swap partition set on my Linux laptops, but they never want to hibernate. It may be the swap partition the same size as the memory is not sufficient, if any of the swap has already been used.

---druck

I posted the magic spell you create a file and write the required length using dd with zeros.

sudo dd if=/dev/zero of=/var/swapfile bs=1K count=4M etc.

Raspberry Pi OS (RaspiOS) does not natively support traditional ACPI hibernation (suspend-to-disk) out of the box

Because Raspberry Pi lacks proper ACPI support, traditional hibernation is difficult and often requires a custom kernel, though it is "in theory" possible.

Workaround: Users often use a swapfile combined with systemd-sleep to enable this functionality.

GPIO Shutdown: A common "shutdown" alternative is to connect a button to GPIO3 to perform a safe shutdown rather than true hibernation.

During a full update several gigabytes of space may be required to download new packages which remain until you do an apt clean. More space on top is required during the installation of each package, and /boot/firmware may double in size temporarily as it is updated.

The whole separate partition thing was way back in the days of unreliable filing systems on small discs. It just isn't needed these days when you can just have everything on one large partition and not have to try and work out the high water mark needed for each part.

---druck

Whilst that is true, there are newer reasons for partitioning..

- You cant fill a root partition with user data if that is on a separate partition.

- You cant e.g. fill /var with e.g. MySQL data if that is on a separate partition..

That is while performance issues are completely irrelevant with large SSDs, some security issues still remain.

Do my NAS server has a small but adequate root partition, and separate var, home and backup partitions

I'm helping a local IT person with Linux (he's a Windows person) and we have had a notebook from a customer which didn't start the GUI for exactly that reason: she had kept files and large files and huge files in her $HOME and at one point the partition was 100% full, no space left for the GUI temporary files. My friend sold her a new disk and I partitioned it such that there is a root partition and a /home and we haven't heard from her since.

Just thought I'd share some hands-on experience.

Josef

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required