Skip to main content
  1. Posts/

Resize Btrfs Drive

·584 words·3 mins

This is just a reminder for me on how to grow a btrfs filesystem (it’s quite easy once you have freed up the space on the underlying device, in this case a qcow2 image).

Resize the qcow virtual disk #

❯ sudo qemu-img info archlinux-1.qcow2  
image: archlinux-1.qcow2
file format: qcow2
virtual size: 55 GiB (59055800320 bytes)
disk size: 53.4 GiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: true
    refcount bits: 16
    corrupt: false
    extended l2: false

The above grabs the information from your qcow image, then below does the actual resize.

❯ sudo qemu-img resize archlinux-1.qcow2 +20G
Image resized.

That’s it, disk is now 20GiB larger.

Resize the partitions in the VM #

The rest of the operation takes place within the VM. Warning, I am rather brave when it comes to making changes to tech, you might not want to follow along as I am live writing to a mounted disk, this is dangerous.

We want to use fdisk to rewrite the partition tables, as I only have boot and root, I can use primary as I’ve not using LVM. THe basic order of flow is to get teh info on the root partition (2), delete it then recreate but set the end to take up all the new space.

❯ fdisk /dev/vda

Welcome to fdisk (util-linux 2.37.1).
Changes will remain in memory only, until you deicde to write them.
Be careful before using the write command.

This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap partitions in this disk.

Command (m for help): p

Disk /dev/vda: 75 GiB, 80530636800 bytes, 157286400 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc4119c23

Device     Boot   Start       End   Sectors Size Id Type
/dev/vda1  *       2048   2099199   2097152   1G 83 Linux
/dev/vda2       2099200 115343359 113244160  54G 83 Linux

Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2099200-157286399, default 2099200): 2099200
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2099200-157286399, default 157286399): 157286399

Created a new partition 2 of type 'Linux' and of size 74 GiB.
Partition #2 contains a btrfs signature.

Do you want to remove the signature? [Y]es/[N]o: N

Command (m for help): p

Disk /dev/vda: 75 GiB, 80530636800 bytes, 157286400 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc4119c23

Device     Boot   Start       End   Sectors Size Id Type
/dev/vda1  *       2048   2099199   2097152   1G 83 Linux
/dev/vda2       2099200 157286399 155187200  74G 83 Linux

Command (m for help): w
The partition table has been altered.

Once that’s written, you can get the OS to perform a fsck by touching the file /forcefsck when we reboot. So, reboot.

Now we need to tell btrfs that it has more space to play in, this is super simple…

❯ btrfs filesystem resize +20G /
Resize device id 1 (/dev/vda2) fro 54.00GiB to 74.00GiB

And we are done.

NOTE: the syntax highlighting is a bit wonky on terminal output… It won’t look like this in your terminal.