Friday, January 8, 2016

How to Create ZFS Filesystem with File Compression on Linux


ZFS filesystem is getting a wider recognition on Linux.
In ZFS, you can enable compression at the filesystem level. This will store the data in compressed format, which will save lot of disk space.
In this article, we’ll explain how to create the filesystem from the ZFS storage pool and enable compression on ZFS.

This is the 2nd article in the ZFS filesystem series.
In the first part of this series, we explained the fundamentals of ZFS, and how to install ZFS on linux. We also created a ZFS pool.

Create ZFS Filesystem

First, view all the current ZFS filesystems using zfs list command as shown below. In this example, we currently have one ZFS filesystem.
# zfs list
NAME     USED  AVAIL  REFER  MOUNTPOINT
mypool   296K  5.84G    30K  /mypool
Now, create a new ZFS filesystem using zfs create command.
# zfs create mypool/fs1
As we see below, the new ZFS filesystem is now created successfully.
 
# zfs list
NAME         USED  AVAIL  REFER  MOUNTPOINT
mypool       170K  5.84G    30K  /mypool
mypool/fs1    30K  5.84G    30K  /mypool/fs1

Set ZFS quote and Reservation

When you create a ZFS filesystem, by default it consumes all the space in the pool. So, you must specify a quota and reservation for the filesystem.
To set a quote, use zfs set command as shown below. Here we are specifying the quota as 1GB for this filesystem.
# zfs set quota=1G mypool/fs1
Next, set the reservation for the filesystem. In this example, fs1 is reserved 256M out of 5.59G so that no one can use this space and also it can extend up to 1G based on the quota we set if there is free space available.
# zfs set reservation=256M mypool/fs1

# zfs list
NAME         USED  AVAIL  REFER  MOUNTPOINT
mypool       256M  5.59G  32.5K  /mypool
mypool/fs1    30K  1024M    30K  /mypool/fs1

Create ZFS alternate Mount Point

Instead of mounting it using the “mypool/fs1″ name, you can also set an alternative mount point with any name that you wish for a filesystem.
For example, the following command will set the mount point as “/testmnt”, instead of “mypool/fs1″.
# zfs set mountpoint=/testmnt mypool/fs1
As we see from the following output, the first column NAME indicates the real name of the ZFS filesystem. The last column MOUNTPOINT indicates the alternative mount point that we created above.
# zfs list
NAME         USED  AVAIL  REFER  MOUNTPOINT
mypool       256M  5.59G  32.5K  /mypool
mypool/fs1    30K  1024M    30K  /testmnt
When you execute df command, you’ll see the alternative mount point as shown below.
# df -h
Filesystem                  Size  Used Avail Use% Mounted on
..
mypool                      5.6G  128K  5.6G   1% /mypool
mypool/fs1                  1.0G  128K  1.0G   1% /testmnt

Enable Compression on ZFS Filesystem

To set compression on a ZFS dataset, you can set the compression property as shown below. Once this property is set, any large files stored on this ZFS filesystem will be compressed.
# zfs set compression=lzjb mypool/fs1
The following are the valid compression properties:
  • on
  • off
  • lzjb
  • gzip
  • gzip[1-9]
  • zle
You can enable compression on an existing filesystem as well. In that case, the compression will be applied only to the new and modified data; and any existing data will remain uncompressed.

Verify ZFS Compression

In following example, we have copied the 61M tar file to the ZFS filesystem mypool/fs1 mounted under /testmnt.
# ls -lh /testmnt/test.tar
-rw-r--r--. 1 root root 61M Nov 11 09:44 /testmnt/test.tar
If you look at the total size of USED space from the zfs list command, you will see only 20.9M space is consumed which indicates that compression is turned on and working.
# zfs list
NAME         USED  AVAIL  REFER  MOUNTPOINT
mypool       256M  5.59G  32.5K  /mypool
mypool/fs1  20.9M  1003M  20.9M  /testmnt
You can also get the compression ratio using the following command.
# zfs get compressratio mypool/fs1
NAME        PROPERTY       VALUE  SOURCE
mypool/fs1  compressratio  2.90x  -
Apart from compression, ZFS filesystem has several advanced features. In the next article of the ZFS series, we’ll discuss about how to take ZFS clones and snapshots.

No comments:

Post a Comment