Sunday, 14 April 2013

ZFS on Ubuntu

Some time back I wrote an article describing how to set up ZFS on FreeBSD.

Well, now that ZFS on Linux has been declared "production ready" I'm going to go ahead and install it on my 12.10 "Quantal" based NAS. I will keep some notes and post them here.

Note: This tutorial will install ZFS as a Linux kernel module, which is not to be confused with the zfs-fuse userland implementation which can be found in the standard Ubuntu repositories.

OK, so before we start we should ensure that your system is up to date by doing an apt-get update and dist-upgrade.

Now, we add the ZFS ppa to our system:

$ sudo add-apt-repository ppa:zfs-native/stable

Update again and install ZFS;

$ sudo apt-get update 
sudo apt-get install ubuntu-zfs nfs-kernel-server

We will need some hard disks on which we can create a zfs pool. In my example I will use /dev/sdc1 and /dev/sdd1. You will also want to come up with a name fot the pool which will appear as a directory in your file system root. I will use "store" for my pool.

Create a mirrored zfs pool "store":

$ sudo zpool create store /dev/sdc1 /dev/sdd1


Create a raidz zfs pool "store":

$ sudo zpool create store raidz /dev/sdc1 /dev/sdd1

You can check that this worked:

$ sudo zpool status
  pool: store state: ONLINE
  scan: none requested

    store       ONLINE       0     0     0
      raidz1-0  ONLINE       0     0     0
        sdc1    ONLINE       0     0     0
        sdd1    ONLINE       0     0     0

errors: No known data errors


$ sudo zpool list
store   3.62T 0G      3.62T   0%   1.00x  ONLINE  -

We can also turn on de-duplication and compression for our pool:

sudo zfs set compression=on store
sudo zfs set dedup=on store

So, presently I have a ZFS pool, which already has a default filesystem. There is no need to do a mkfs. You can see that it is mounted using the df command;

# df -h
Filesystem       Size    Used   Avail Capacity  Mounted on
/dev/ada0p2       55G     21G     29G    42%    /
devfs            1.0k    1.0k      0B   100%    /dev
store            3.4T    1.0k    3.4T     0%    /store

Normally, you would not just start dumping files straight onto the pool (which you can do if you really want to but you lose some of the benefits of ZFS), but instead you create another filesystem to store your files in. You do this with the "zfs" command.

# zfs create store/library

Check your mounted filesystems again;

# df -h
Filesystem       Size    Used   Avail Capacity  Mounted on
/dev/sda1         55G     21G     29G    42%    /
store            3.4T    1.0k    3.4T     0%    /store
store/archive    3.4T    1.0k    3.4T     0%    /store/library

Another neat thing about ZFS is how easy it is to share a filesystem using NFS. Let's share store/library;

$ sudo zfs set sharenfs=rw store/library

Unlike with "normal" NFS there is no need to restart any services after issuing this command, although you should note that is not recommended that you mix "normal" NFS (ie: /etc/exports) with ZFS controlled NFS.

In other words, keep your /etc/exports file empty.

My archive filesystem is now shared, but it is open to everybody. Usually I don't care about that at home but in other scenarios you may wish to restrict access to my network and allow the root user full control;

$ sudo zfs set sharenfs='rw=@',no_subtree_check,async,no_root_squash store/library

At this point we are pretty much done. You can start placing files in your store/library filesystem now.

Some usefull commands:

zfs get all store/library 
zfs list
zpool list