ZFS commonly known as Z file system is mainly based on storage and redundancy. The concept of ZFS was developed with file servers in mind where high availability and data integrity are the most crucial factors. ZFS uses virtual storage pools commonly known as zpools to deal with the storage and management of large amounts of data. In this article, we will get to know about how to install zfs and its common uses in the technical environment.
Fresh Ubuntu 20.04 server
Sudo privileged non-root user account
Installation of ZFS is a straightforward process. You can use the command line terminal application to install the ZFS file system. Lunch the command line terminal tool and execute the following command.
$ sudo apt update
$ sudo apt install zfsutils-linux
During the installation, you may be prompted for the password, provide sudo password and continue.
Once running the above command, you will be asked for confirmation if you want to continue with installation or not. Press ‘y’ and enter. Now the software package will be installed on your system.
To verify the installation of ZFS on your system, use the following command.
$ zfs --version
You will get the output similar as:
ZFS Basic usage and commands
Once the ZFS is installed, now you can use hard drives with ZFS. When the new hard drives are inserted into your system, ZFS addresses them by their device name. For instance something similar as the lines of /dev/sda or similar. You can use fdisk command line utility to check available hard drives.
$ sudo fdisk -l
You will find output similar as:
How to create and destroy zpools
ZFS works with the concept of pooling disks together. Various Raid levels can be used while creating pools. The first pool we will be creating is RAID 0 . RAID level 0 works by striping your data across a number of disks. When a file is written to or read from the storage pool all the disks will come in action to present the portion of data. This level of RAID provides you a speed boost for your read and write operation speed however it lacks data redundancy. Any critical issue on the disk or disk malfunction in the pool will cost complete loss of data.
In this example, I have used two hard drives /dev/sdb and /dev/sdd for creating the pool named test-pool. You can have your own assumption on disk and pool name selection. Run the following command to create zpool.
$ sudo zpool create test-pool /dev/sdb /dev/sdd
If any error appears, you can run the command using -f option after the zpool create command which forces the command to be executed.
$ sudo zpool create -f test-pool /dev/sdb /dev/sdd
Once created, you can find out the mount point using the following command:
$ df -h
From the output, we can see that the pool is mounted at /test-pool . You can change the mount point for the pool, use the following syntax.
$ sudo zfs set mountpoint =<path> <pool_name>
In this example, I have used /var/www as the new mount point. You can have your own consideration for the mount point.
$ sudo zfs set mountpoint=/var/www test-pool
Verify the new point using the following command.
$ df -h
You can create the directories under the storage pool. In this example, I have created the directory named MySQL under the storage pool test-tool.
$ sudo zfs create test-pool/mysql
Run the following command to view all the ZFS storage pools on the system.
$ zpool list
To find the configuration and status of each device in the ZFS pool, use the following command.
$ zpool status
To troubleshoot ZFS storage pool in case of any issue, execute the following command which shows the zpool events. Replace pool_name with your ZFS storage pool.
$ sudo zpool events pool_name -v
If you want to add another hard disk in the ZFS storage pool, you can use the following command. In this example I have used a new hard disk /dev/sdc to add in the previously created ZFS storage pool test-pool. You can select your hard disk and zpool name accordingly.
$ sudo zpool add test-pool /dev/sdc
Once the disk is added verify the pool using the command:
$ zpool status
To destroy the ZFS storage pool, run the following command with your valid pool name.
$ sudo zpool destroy pool_name
$ sudo zpool destroy test-pool
How to use encryption with ZFS
After creating a ZFS storage pool, you can enable encryption on it with the following command. For this example, I have used a previously created ZFS pool named “test-pool” for encryption. You can select your ZFS pool accordingly.
$ sudo zfs create -o encryption=on -o keylocation=prompt -o keyformat=passphrase test-pool/encrypted
You will be asked to enter the passphrase twice. Enter your passphrase and continue.
A new directory will be created under the storage mount point and anything under this directory will be encrypted. Whenever the system is a reboot, you may need to manually mount the dataset. Remember to use the -l flag when mounting the encrypted dataset. You will be prompted for the passphrase, supply the passphrase you have used to encrypt the pool, and continue.
$ sudo zfs mount -l test-pool/encrypted
$ df -hT | grep zfs
How to take, roll back and destroy ZFS snapshots
Taking the snapshot
Creating the snapshot in ZFS is quite an easy and straightforward process. You can use the zfs snapshot command followed by the name of the snapshot as an argument to create a snapshot. In this example, I have used test-pool/mysql to create a snapshot. You can choose your pool name and data set accordingly.
$ sudo zfs snapshot test-pool/[email protected]
Verify the snapshot taken using the following command:
$ zfs list -t snapshot
Renaming the snapshot
Snapshot can be rolled back using command zfs rollback with snapshot name to revert all the changes made since a specific snapshot. In this example, the test-pool/mysql file system is rolled back to the sunday snapshot.
$ sudo zfs rollback test-pool/[email protected]
Once the rollback is completed, you can verify the files present in the directory.
In this article, I have covered the basic explanation of zfs. We have learned how to create ZFS storage pools, how to destroy the pool, and specify the mount point. Also, we got some basic ideas about zfs pool encryption, the process of taking a snapshot and rolling back the snapshot to the earliest version. Thank you for reading this article. Feedbacks and suggestions will be highly appreciated.