Autarchy of the Private Cave

Tiny bits of bioinformatics, [web-]programming etc

    How to: easily add swap partition to a live system on btrfs

    14th April 2016

    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:

    1. shrink btrfs filesystem by more than 2GB;
    2. shrink btrfs partition by 2GB;
    3. create new 2GB partition for the swap;
    4. resize btrfs filesystem to full size of its new-size partition;
    5. initialize swap and turn it on.

    Here are the very easy steps! Just make sure you do not make mistakes anywhere ;)

    1. If your btrfs volume with ID 5 (top level) is a separate mountpoint: mount it now, e.g. sudo mount /toplevel.
    2. Take note of your current partition label and UUID: sudo blkid.
    3. 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.
    4. sudo parted, then print to make sure what is the number of your btrfs partition, then resizepart 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 with q and Enter.
    5. sudo partprobe to let the OS know that partitions have changed.
    6. 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.
    7. Resize btrfs filesystem back up to take all of the partition: sudo btrfs fi resize max /toplevel.
    8. Simply to be sure, run a scrub: sudo btrfs scrub start -B -r /toplevel.
    9. 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.
    10. sudo blkid to make sure your /dev/sda1 UUID stayed the same (or to get swap uuid/label if you haven’t specified any).
    11. 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!

    StumbleUponDeliciousCiteULikePocketKindle ItEvernotePinterestShare

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>