Loopback file size doesn't change on data deletion

I was just toying around with one of my LXD containers that has a loopback zfs volume. One strange thing I noticed is this:
I had 1.5GB of data stored in my container “test”. And then I deleted 300mb data. I was left with 1.2GB of data inside but then when I came back to host to check the size of my loopback volume file, it was still 1.5GB.

For the second test I duplicated 300mb twice inside the container. Thus I had now 1.8GB of data inside. Then I came back on host to check the loopback file size and it was 1.8GB.

The strange part is that size of loopback file is increasing on data addition but not decreasing on data deletion.

Is this normal behaviour or is something wrong with my setup?

This is expected behavior, and is related to how sparse files work.
When you first create the loopback file, it does not allocate all the space that you have specified.
Here is my just-created loopback file,

$ lxc storage create myloopback zfs size=3GB
Storage pool myloopback created
$ sudo du -h /var/snap/lxd/common/lxd/disks/myloopback.img
2,4M	/var/snap/lxd/common/lxd/disks/myloopback.img
$ 

As you are using LXD and creating containers, the reported size of the myloopback.img will only increase, until it reaches the allocated size.

$ lxc launch ubuntu: mycontainer --storage myloopback
Creating mycontainer
Starting mycontainer                             
$ sudo du -h /var/snap/lxd/common/lxd/disks/myloopback.img
549M	/var/snap/lxd/common/lxd/disks/myloopback.img
$ lxc shell mycontainer
root@mycontainer:~# df -h .
Filesystem                                                                        Size  Used Avail Use% Mounted on
/var/snap/lxd/common/lxd/storage-pools/myloopback/containers/mycontainer/rootfs  2.7G  545M  2.1G  21% /
root@mycontainer:~# 
1 Like

On recent enough systems you may be able to enable autotrim on the ZFS zpool which may then release some space back to the host.

1 Like

Thanks @simos and @stgraber for the info. I got it now.

Will explore autotrim on ZFS zpools.