Recently I had a need to add a swap file to my Debian installation.
However, I am now using btrfs, and – as with any other COW filesystem – it is not possible to simply create a swap file and use it.
There are workarounds (creating a file with a COW attribute removed, and then loop-mounting it), but I just did not like them.
So I have decided to add a swap partition.
It worked amazingly (and very easily), there was even no need to reboot – at all.
I still did restart, just to make sure the system is bootable – and all was perfectly fine.
My initial setup is very simple: a single /dev/sda1 partition on the /dev/sda disk, fully used by btrfs.
Different important paths/mountpoints are btrfs subvolumes, using flat hierarchy.
For this example, let us assume that /dev/sda (and /dev/sda1) is 25GB large, and that I want to add a 2GB swap /dev/sda2 after /dev/sda1.
Brief explanation before we start:
- shrink btrfs filesystem by more than 2GB;
- shrink btrfs partition by 2GB;
- create new 2GB partition for the swap;
- resize btrfs filesystem to full size of its new-size partition;
- initialize swap and turn it on.
Here are the very easy steps! Just make sure you do not make mistakes anywhere Image may be NSFW.
Clik here to view.
- If your btrfs volume with ID 5 (top level) is a separate mountpoint: mount it now, e.g.
sudo mount /toplevel
. - Take note of your current partition label and UUID:
sudo blkid
. - Resize btrfs filesystem down (shrink) with a good margin; for example, if I want to add a 2 GB swap, then I can
sudo btrfs fi resize -3g /toplevel
– here, I’m shrinking btrfs filesystem by about a gigabyte more than necessary. The process is very quick if you have free space, so you can even use a larger margin – say,sudo btrfs fi resize -5g /toplevel
. sudo parted
, thenprint
to make sure what is the number of your btrfs partition, thenresizepart 1
(where 1 is the partition number), and answer a few questions:yes
,new_size_here
(in our example: 23.0GB),yes
. You can also create a swap partition from parted, then quit parted withq
and Enter.sudo partprobe
to let the OS know that partitions have changed.- I have used cfdisk to create a 2GB swap partition: it has a very simple ncurses UI, and is very intuitive. After creating swap partition, do run
sudo partprobe
again. - Resize btrfs filesystem back up to take all of the partition:
sudo btrfs fi resize max /toplevel
. - Simply to be sure, run a scrub:
sudo btrfs scrub start -B -r /toplevel
. - Initialize swap; you can specify uuid and/or label which you may already have in your fstab:
mkswap --label=swap --uuid=your1234-your-uuid-1234-youruuid1234 /dev/sda2
. sudo blkid
to make sure your /dev/sda1 UUID stayed the same (or to get swap uuid/label if you haven’t specified any).- Optionally, add the swap line to your /etc/fstab. Then turn on swap with
swapon -a
.
That’s it! Amazing, isn’t it? On-the-fly filesystem and partition resizing!
Image may be NSFW.
Clik here to view.Image may be NSFW.
Clik here to view.Image may be NSFW.
Clik here to view.Image may be NSFW.
Clik here to view.Image may be NSFW.
Clik here to view.Image may be NSFW.
Clik here to view.Image may be NSFW.
Clik here to view.Image may be NSFW.
Clik here to view.