Enlarging a swap file

I am using btrfs and I have a swapfile (from installation) which I want to enlarge.

Is the easiest way just to

  • swapoff it
  • then enlarge it with dd command
  • and finally run mkswap


Just make a new swap file, and delete the old one. There is nothing special about it.

# swapoff /swapfile
# rm -f /swapfile

followed by:


1 Like

Thinking further I think that both ways are ok. Here I did it by enlarging it.

Depending on how much free space you have, you don’t even need to swapoff and delete the old swap file, before the new one is made.

I have plenty of free space so this doesn’t matter here.

Then the easiest procedure is to make a new swap file, and just switch to it.

Hm, just playing with creating a swapfile and being confused

The existing swapfile in subvol @swap has

$ lsattr swapfile

---------------C-----m swapfile

Creating a new one

truncate -s 0 /swap/swapfile1
chattr +C /swap/swapfile1

and lsattr shows

---------------C------ swapfile1

To get the missing m I tried

btrfs property set ./swapfile1 compression none


ERROR: failed to set compression for ./swapfile1: Invalid argument


If I understand correctly it means that the btrfs property set command is no longer required.

But it doesn’t answer how the swapfile created during installation shows the m-flag which means don’t compress and the newly created swapfile1 doesn’t. Or in other words: how do I get the same output from lsattr for both swapfile and swapfile1?

I found here https://btrfs.readthedocs.io/en/latest/Swapfile.html
that starting with btrfs 6.1 a swapfile in btrfs can simply be created by

btrfs filesystem mkswapfile swapfile

The default size is 2GB. -s SIZE allows to specify a size