Creating a swapfile on a Btrfs file system requires a few extra steps due to Copy-on-Write (COW) and fragmentation concerns. This guide walks you through setting up a swapfile with hibernation (resume
) support.
1. Calculate Recommended Swap Size
SWAPSIZE=$(free | awk '/Mem/ {x=$2/1024/1024; printf "%.0fG", (x<2 ? 2*x : x<8 ? 1.5*x : x) }')
- If RAM < 2GB → swap = 2 × RAM
- If RAM < 8GB → swap = 1.5 × RAM
- Otherwise → swap = RAM
2. Create a Dedicated Btrfs Subvolume
sudo btrfs subvolume create /swap
3. Disable Copy-on-Write (COW)
sudo chattr +C /swap
Note: Run this before creating the swapfile.
4. Create and Configure the Swapfile
sudo mkswap --file -L SWAPFILE --size $SWAPSIZE /swap/swapfile
sudo bash -c 'echo /swap/swapfile none swap defaults 0 0 >>/etc/fstab'
sudo swapon -av
5. Get the UUID of the Swap Partition
sudo findmnt /swap -o UUID
Take note of the UUID for the next step.
6. Find the resume_offset
for Hibernation
sudo btrfs inspect-internal map-swapfile -r /swap/swapfile
Copy the returned resume_offset
value.
7. Configure GRUB for Hibernation Support
Edit /etc/default/grub
and add the following to GRUB_CMDLINE_LINUX_DEFAULT
:
resume=UUID=XXXXXXXXXXXXXXXXXXXXXXXXXX resume_offset=XXXX
- Replace
XXXXXXXXXXXXXXXXXXXXXXXXXX
with the UUID from step 5 - Replace
XXXX
with the offset from step 6
sudo update-grub
8. Enable Hibernation in KDE Menu (Kubuntu)
To make the hibernate option appear in the KDE system menu, create the following file:
sudo nano /etc/polkit-1/rules.d/10-enable-hibernate.rules
Add this content:
polkit.addRule(function(action, subject) {
if (action.id == "org.freedesktop.login1.hibernate" ||
action.id == "org.freedesktop.login1.hibernate-multiple-sessions" ||
action.id == "org.freedesktop.upower.hibernate" ||
action.id == "org.freedesktop.login1.handle-hibernate-key" ||
action.id == "org.freedesktop.login1.hibernate-ignore-inhibit")
{
return polkit.Result.YES;
}
});
Save and reboot. The hibernate option should now be available in the KDE menu.
Done!
You now have a properly configured Btrfs-based swapfile with hibernation support, including KDE menu integration on Kubuntu.
Comments