Articles » Resizing an Encrypted Btrfs Disk Image
04/04/2019
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.
...if needed. Also backup your original image file:
$ sudo umount /dev/mapper/LUKSVOL
$ sudo cryptsetup close LUKSVOL
$ cp LUKSVOLUME.img LUKSVOLUME.img.bak
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
$ sudo cryptsetup open LUKSVOLUME.img LUKSVOL
$ sudo cryptsetup resize -v LUKSVOL
I.e. with a 'whole disk' fs.
$ sudo mount /dev/mapper/LUKSVOL /mnt
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
$ sudo btrfs device add /dev/loopX /mnt/
$ sudo btrfs fi show /mnt/ # verify that the new device is added
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)
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.
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.
•Copyright © 2025 Caiyos LLC