Caiyos Computer Services


Resizing an Encrypted Btrfs Disk Image


Recently I needed to resize a btrfs-formatted cryptsetup-created disk image, and found no comprehensive instruction on how to do it online. So, I'm sharing what I learned for the benefit of others.

Step 1. Unmount and Close

...if needed. Also backup your original image file:

$ sudo umount /dev/mapper/LUKSVOL
$ sudo cryptsetup close LUKSVOL

Step 2. Expand the Container

Note that to calculate the additional space you want to add, multiply the 'byte size' (bs) times the count, e.g.: 1M * 100 = 100M.

$ dd if=/dev/zero of=LUKSVOLUME.img bs=1M count=100 oflag=append conv=notrunc

Step 3. Open and Resize the Encrypted Volume

$ sudo cryptsetup open LUKSVOLUME.img LUKSVOL
$ sudo cryptsetup resize -v LUKSVOL

Step 4. Expand the Partitionless Btrfs FS

I.e. with a 'whole disk' fs.

a. Mount the Btrfs FS

$ sudo mount /dev/mapper/LUKSVOL /mnt

b. Create a Temporary Loop Device

Be sure it's as large or larger than the old btrfs device (i.e. before it was appended in step 1):

$ sudo dd if=/dev/zero of=/tmp/btrfs-vol1.img bs=1M count=50
$ sudo losetup -f /tmp/btrfs-vol1.img
$ losetup # check the new device id

c. Add the New Loop Device to the Btrfs FS

$ sudo btrfs device add /dev/loopX /mnt/
$ sudo btrfs fi show /mnt/ # verify that the new device is added

d. Remove and Re-Add Devices

Removing and re-adding the original device allows the parent btrfs fs to update the storage capacity (I wasn't able to achieve this with scrub or balance or any other method I tried). After, the loop device can be removed from the btrfs fs:

$ sudo btrfs device delete /dev/mapper/LUKSVOL /mnt/
$ sudo btrfs device add /dev/mapper/LUKSVOL /mnt/
$ sudo btrfs device delete /dev/loopX /mnt/
$ sudo btrfs fi show /mnt/ # verify proper device(s)

d. Balance and Scrub

To be safe:

$ sudo btrfs fi balance start /mnt/
$ sudo btrfs scrub start /mnt/

Then you can unmount and cryptsetup close, and your image file should be properly resized.

Tags: IT, Linux,


Latest Articles

View by Tags

Other Views

About Us

We are a small business in Central Oregon who's mission is to help other small businesses simplify and optimize their technology resources with linux and open-source software.

Brooks, the owner of Caiyos, is self-educated and has over 20 years of experience working in the fields of IT and computer science with dozens of companies. Along with delivering professional service, he provides clear, understandable, and timely communication.