January 30, 2017

ZFS saves your space

Below are the simple steps to save your space using ZFS’s data deduplication and compression.

Tried in Ubuntu 16.04.1 LTS (Linux 4.4.0-59-generic, x86_64)

The original FS I had was ext4.

Create a block device

I used a file instead of a RAW device just to bring a quick example of how to save space with minimum action. You should use a RAW device, but for that, repartitioning is required.

# apt -y install zfsutils-linux
# fallocate -l100G zfspool.block

Create a ZFS pool

After that, /etc/zfs/zpool.cache will be created with the information about where the last used ZFS pool backing device was located. That way the zfs-import-cache.service will be able to find & import your ZFS pool device after system reboot. And zfs-mount.service is going to mount your FS that we will create in the next step.

# zpool create zfspool /root/zfspool.block
# zpool status

Enable compression and deduplication

lz4 has great performance, though a little worse compression ratio. You can chose different algo.

# zfs set compression=lz4 zfspool
# zfs set dedup=on zfspool

Create your filesystems

I would like to move my /home and the docker directories from ext4 to ZFS. Since I am using docker, it stores its images in /var/lib/docker by default.

# zfs create zfspool/home
# zfs create zfspool/docker
# zfs list

Sync your data

# rsync -avHx --progress --stats /home/ /zfspool/home/

# systemctl stop docker
# rsync -avHx --progress --stats /var/lib/docker/ /zfspool/docker/

See how much we have saved

# du -sm /home/ /zfspool/home/
67723	/home/
40307	/zfspool/home/

# du -sm /var/lib/docker/ /zfspool/docker/
4273	/var/lib/docker/
1748	/zfspool/docker/

We have just saved nearly 30 GiB !

Set new mountpoints

# mv /var/lib/docker /var/lib/docker2DEL
# mkdir OLDHOME
# mv -- /home/* OLDHOME/
# zfs set mountpoint=/home zfspool/home
# zfs set mountpoint=/var/lib/docker zfspool/docker
# zfs list

Extra

There are plenty of other great features that ZFS can offer, e.g. snapshots

# zfs snapshot zfspool/home@30-Jan-2017
# zfs snapshot zfspool/docker@30-Jan-2017

# zfs list -t snapshot 
NAME                         USED  AVAIL  REFER  MOUNTPOINT
zfspool/docker@30-Jan-2017      0      -  2.25G  -
zfspool/home@30-Jan-2017        0      -  39.3G  -