Getting Started with ZFS on Debian 8

1329

harddrivesI am quite careful about protecting my files, which include images, videos, and various documents – mostly drafts of my science fiction novels – that I have been working on for years. Images and videos of family, including the birth of my son, his first smile, and other moments are way too valuable for me to lose. But, all sorts of things can lead to data corruption, and I would not be happy if that happened to any of my valuable files. That’s where filesystems like ZFS and Btrfs enter the picture.

ZFS has been around for a very long time (more than 10 years now), and although Btrfs is the future filesystem for Linux machines, ZFS has its own significance. It was the first filesystem to implement many features to help protect data from getting corrupted.

But, although some distros have started shipping Btrfs, it’s still relatively young. ZFS, on the other hand, is quite mature, and if you are looking at setting up your own drives with a filesystem that is capable of healing corrupt data, then you should be looking at ZFS.

There are many reasons why you should use ZFS for your file servers to create a pool of hard drives to increase redundancy. For one thing, it’s scalable, so there is virtually no limit on how big your storage is. And, it keeps checking the integrity of your data to protect it from corruption.

In this tutorial, I will show how to install and configure ZFS on a test machine running fully updated Debian 8. To get started, open the terminal, become root, and start installing the packages:

$ su –
# apt-get install lsb-release
# wget http://archive.zfsonlinux.org/debian/pool/main/z/zfsonlinux/zfsonlinux_6_all.deb
# dpkg -i zfsonlinux_6_all.deb

Next, install debian-zfs package:

# apt-get update
# apt-get install debian-zfs

This will take a lot of time, so go grab some coffee. Once the package is installed, it will be time to create a pool of drives. Now, to check whether the package is installed, you can run:

# zfs list

If you get an error, you will have to load it manually:

# /sbin/modprobe zfs

Now, run the command again:

# zfs list
no datasets available

“no datasets available” is an expected outcome, as we have not yet created any pool or dataset. We first have to create a pool of hard drives. I’ll need to find the block device name using the lsblk command. Next, I’ll create a redundant filesystem with “raidz” using this pattern:

# zpool create -f [pool_name] raidz drives

Here is an example of my system: 

# zpool create -f swapnil0 raidz /dev/sdb /dev/sdc /dev/sdd

You can run the zfs list command to see if it was created successfully and how much space you have, for example:

# zfs list
NAME       USED  AVAIL  REFER  MOUNTPOINT
swapnil0  67.9K  57.4G  24.0K  /swapnil0 

Now you can see that the pool of three storage devices have created 57.4GB of redundant storage. However, instead of creating regular directories inside swapnil0, I will create a dataset. There are many advantages of using datasets over directories, and the biggest one is to create snapshots.

Next, I’ll create a dataset called tux inside swapni0

zfs create swapnil0/tux
# zfs create swapnil0/images
# zfs create swapnil0/videos

Now, let’s check out the zfs structure:

root@dell-mini:~# zfs list
NAME              USED  AVAIL  REFER  MOUNTPOINT
swapnil0         5.11M  57.4G  4.98M  /swapnil0
swapnil0/images  24.0K  57.4G  24.0K  /swapnil0/images
swapnil0/tux     24.0K  57.4G  24.0K  /swapnil0/tux
swapnil0/videos  24.0K  57.4G  24.0K  /swapnil0/videos

Because in Debian, you perform everything as root, you will need to change the the owner to your the user so you can write files to these datasets. 

# chown -R user:user /pool 

For example:

#chown -R swapnil:swapnil /swapnil0

Fire up the file manager and open the swapnil0 directory and start using it. You can check the status of your pool by running this command:

# zpool status swapnil0
  pool: swapnil0
state: ONLINE
  scan: none requested
config:
NAME        STATE     READ WRITE CKSUM
swapnil0    ONLINE       0     0     0
 raidz1-0  ONLINE       0     0     0
   sdb     ONLINE       0     0     0
   sdc     ONLINE       0     0     0
   sdd     ONLINE       0     0     0
errors: No known data errors

If you want to, you can add more disks to your storage pool without worrying about their filesystems. Now you won’t have to worry about your precious files getting corrupted.

Conclusion

This article was aimed at giving you some basics of ZFS, and I have barely scratched the surface. In the next article, I will talk about optimizing it, taking snapshots, and using it to its fullest potential.