ZFS Compression – A Win-Win

por | 3 enero, 2019

Guest Author

This may sound counterintuitive, but turning on ZFS compression not only saves space, but also improves performance. This is because the time it takes to compress and decompress the data is quicker than then time it takes to read and write the uncompressed data to disk (at least on newer laptops with multi-core chips).

To turn on compression simply run:

pfexec zfs set compression=on rpool

All the child datasets of rpool will inherit this setting. For example:

[email protected]:~$ zfs get compression rpool/export/home
NAME PROPERTY VALUE SOURCE
rpool/export/home compression on inherited from rpool/export

Note, only new data written after turning on compression is effected. Your existing data is left in its uncompressed state.

To check the compression you’re achieving on a dataset, use the compressratioproperty:

[email protected]:~$ zfs get compressratio rpool/export/home
NAME PROPERTY VALUE SOURCE
rpool/export/home compressratio 1.00x

I’m seeing 1.00x because I just enabled compression. Over time, as I write new data, this number will increase. 

zfs get all rpool

An Example 

This part is optional but will give you a better feeling for how compression works. 

Start by creating a new (therefore, empty) file system, ensuring compression is off (otherwise it will inherit the setting from rpool):

[email protected]:~$ pfexec zfs create -o compression=off rpool/test
[email protected]:~$ cd /rpool/test

Copy a file into the test file system, turn on compression, and copy it again:

[email protected]:/rpool/test$ pfexec cp /platform/i86pc/kernel/amd64/unix unix1
[email protected]:/rpool/test$ pfexec zfs set compression=on rpool/test
[email protected]:/rpool/test$ pfexec cp /platform/i86pc/kernel/amd64/unix unix2

Check the compression ratio:

[email protected]:/rpool/test$ pfexec zfs get compressratio rpool/test
NAME PROPERTY VALUE SOURCE
rpool/test compressratio 1.28x -

Check the difference in file size:

[email protected]:/rpool/test$ du -hs u\*
1.7Munix1
936Kunix2

Also note the difference between using du and ls:

[email protected]:/rpool/test$ ls -lh
total 2.6M
-rwxr-xr-x 1 root root 1.6M 2009-04-28 13:32 unix1\*
-rwxr-xr-x 1 root root 1.6M 2009-04-28 13:33 unix2\*

ls does not show the compressed file size! See ZFS Compression, whcn du and ls appear to disagree for a great explanation of this.

Finally, delete unix1, which was not compressed, and notice how the compression ratio for the file system rises accordingly:

[email protected]:/rpool/test$ pfexec rm unix1
[email protected]:/rpool/test$ pfexec zfs get compressratio rpool/test
NAME PROPERTY VALUE SOURCE
rpool/test compressratio 1.77x -


Finally, clean up:

[email protected]:/rpool/test$ cd
[email protected]:~$ pfexec zfs destroy rpool/test