Adding ZFS compression to existing dataset

Hello!

I’ve been using LXD for about 2 years now with ZFS as a storage backend. My Storage pools are LVM-backed block devices. Unfortunately I didn’t enable ZFS compression back then. Yesterday I figured out that it might be a good idea to enable LZ4 compression, since it is capable of saving considerable amounts of storage space.

I enabled compression on both of my ZFS storage pools globally, but as I understood, compression is only applied to new / changed data - not data already existing. To achieve some compression now, I’d have to

  1. re-write every file in each container or

  2. copy the ZFS filesystem of a container, then delete the old one, and rename the new one to the name of the old file system. That way, all the data would have gone through a copy / write process, which uses compression.

(Also see: https://github.com/openzfs/zfs/issues/3013)

So I’d do the following (assuming I have a container mastodon on storage pool default:

Create a snapshot of my LXD container:

lxc snapshot mastodon without-compression

Then copy the snapshot over to a new file system, named similarly:

zfs send default/containers/mastodon@without-compression | zfs receive -F default/containers/mastodon_compressed

Now it’s time to shut down the mastodon container

lxc stop mastodon

… also remove all snapshots, because otherwise LXD will (by its SQL database) expect some snapshots to be existent on the mastodon_compressed file system, which of course will not exist after moving the mastodon_compressed file system in place:

lxc delete mastodon/snap1
lxc delete mastodon/snap2
lcx delete [..]

Delete the original, uncompressed file system:

zfs destroy default/containers/mastodon

Then move the new ZFS file system in place by renaming it from mastodon_compressed to its original name:

zfs rename default/containers/mastodon_compressed default/containers/mastodon

Now my question: Is this a valid approach to achieve compression of old data? Will my manual process interfere with LXD / LXD’s SQL database in any way? Are there more reliable ways to do it?

Thanks for your feedback. Would really appreciate it :slight_smile:

Just noticed that compression seems to have been active on another storage pool for a while. But the storage pool that I have created later on (I guess manually by zfs commands) didn’t have it enabled. Seems like LXD is already enabling compression? Anyway, I still need to get it working on my other storage pool (with compression enabled yesterday), too :wink: