Rezising a loop-based BTRFS storage pool for LXD.
ZFS is without a doubt the best storage pool for use with LXD containers, mainly because of its advanced features, error correction and its great performance. However, in some cases it is better not to use ZFS because of conflicts or poor performance. This is the case, for example, when you want to run Docker in a LXD container with a ZFS storage pool. To solve this issue, a separate BTRFS storage pool based on an image (virtual disk) and mounted as a loop device is often used. This storage pool can then be used entirely when creating a new container or as a volume added to an existing container.
For this tutorial, we will first create a new 2GB BTRFS storage pool called “test”.
# lxc storage create test btrfs size=2GB
Check the details of our new storage pool “test”.
# lxc storage show test
config: size: 2GB source: /var/snap/lxd/common/lxd/disks/test.img description: "" name: test driver: btrfs used_by:  status: Created locations: - none
As you can see a 2GB image “test.img” was created in “/var/snap/lxd/common/lxd/disks/” directory.
To expand the available disk space to 7GB, we need to expand our image by 5GB. If your image is already being used by one or more containers, make sure to stop those containers first before continuing the process to avoid data loss. It is also advisable to take a snapshot and/or backup of the relevant containers in case something goes wrong and you need to restore.
# truncate -s +5G /var/snap/lxd/common/lxd/disks/test.img
Next find the mounted loop device that is associated with the storage pool image.
# losetup -j /var/snap/lxd/common/lxd/disks/test.img
/dev/loop11: :6160593 (/var/snap/lxd/common/lxd/disks/test.img)
In our case “/dev/loop11” which we need to resize.
# losetup -c /dev/loop11
Finally resize our BTRFS file system to the maximum.
When running LXD as snap package the storage pools are mounted in the directory “/var/snap/lxd/common/mntns/var/snap/lxd/common/lxd/storage-pools/”. For our storage pool “test” that means our storage pool is mounted on “/var/snap/lxd/common/mntns/var/snap/lxd/common/lxd/storage-pools/test/”.
# btrfs filesystem resize max /var/snap/lxd/common/mntns/var/snap/lxd/common/lxd/storage-pools/test/
Since LXD is not aware of the new size we need to adjust the meta data manually by adding the 5GB to the innitial size of 2GB in total 7GB.
# lxc storage set test size=7GB