Create, mount and expand BTRFS loop device.

In this tutorial, we will learn how to create a BTRFS loop device (virtual block device) and how to mount this device permanently so that it is available when the system reboots. We will further see how to expand a BTRFS loop device without losing the data already stored.

# mkdir /btrfs-dir

Create a new 2 GB image.

# truncate -s 2GB /btrfs-dir/test.img

And create a BTRFS file system.

# mkfs.btrfs /btrfs-dir/test.img
btrfs-progs v5.4.1
See http://btrfs.wiki.kernel.org for more information.

Label:              (null)
UUID:               68565cf3-9459-4658-9c28-ec2a28f8b87f
Node size:          16384
Sector size:        4096
Filesystem size:    1.86GiB
Block group profiles:
  Data:             single            8.00MiB
  Metadata:         DUP              95.31MiB
  System:           DUP               8.00MiB
SSD detected:       no
Incompat features:  extref, skinny-metadata
Checksum:           crc32c
Number of devices:  1
Devices:
   ID        SIZE  PATH
    1     1.86GiB  /btrfs-dir/test.img

Create a new mountpoint “/mnt/btrfs” (This is arbitrary so can be changed if desired).

# mkdir /mnt/btrfs

Mount our image as a loop device on new mountpoint.

# mount -o loop /btrfs-dir/test.img /mnt/btrfs/

Check the size.

# df -h /mnt/btrfs/
/dev/loop12     1.9G  3.5M  1.7G   1% /mnt/btrfs

You can see our system created a loop device “/dev/loop12” with 1.9 GB space.

Now we want to expand this to 5 GB by adding 3GB to our existing image “test.img”.

# truncate -s +3GB /btrfs-dir/test.img

Resize the loop device.

# losetup -c /dev/loop12

And resize the BTRFS file system.

# btrfs filesystem resize max /mnt/btrfs

Check the available size.

# df -h /mnt/btrfs/
/dev/loop12     4.7G  3.5M  4.5G   1% /mnt/btrfs

We now have 4.7GB available.

To permanently mount this device and have it automatically mounted at boot time, we need to modify the “fstab” file.

# vi /etc/fstab

And add a new line.

/btrfs-dir/test.img  /mnt/btrfs    auto loop 0 0

Similar Posts