ZFS

From Segfault
Jump to navigation Jump to search

Getting started

Create a pool[1]:

zpool create tank0 /dev/sdc /dev/sdd /dev/sde

Now we should have something like this:

$ zpool status tank0
  pool: tank0
 state: ONLINE
  scan: none requested
config:
       NAME        STATE     READ WRITE CKSUM
       tank0       ONLINE       0     0     0
         sdc       ONLINE       0     0     0
         sdd       ONLINE       0     0     0
         sde       ONLINE       0     0     0

Rename a pool:

zpool export tank0
zpool import tank0 tank2

Compression

$ df -h /data/backup
Filesystem             size   used  avail capacity  Mounted on
tank0/backup           7.0G   5.1G   1.9G    73%    /data/backup

$ zfs set compression=lz4 tank0/backup
$ zfs get compression tank0/backup
NAME          PROPERTY     VALUE         SOURCE
tank0/backup  compression  lz4            local

$ dd if=/dev/zero of=/data/backup/null.img bs=1024k count=1k
$ ls -hl /data/backup/null.img 
-rw-r--r--   1 root     root        1.0G Dec 15 12:54 /data/backup/null.img

$ du -sh /data/backup/null.img 
   0K   /data/backup/null.img

$ df -h /data/backup
Filesystem             size   used  avail capacity  Mounted on
tank0/backup           7.0G   5.1G   1.9G    73%    /data/backup

Re-Compression

Compressing filesystem contents after the fact:

  • Doesn't work for hardlinks.
  • Must be done manually[2]
$ df -h /data/test
Filesystem             size   used  avail capacity  Mounted on
tank0/test             2.0G   465M   1.5G    23%    /data/test

$ zfs set compression=lz4 tank0/test
$ zfs get compression tank0/test
NAME        PROPERTY     VALUE       SOURCE
tank0/test  compression  lz4          local

$ df -h /data/test
Filesystem             size   used  avail capacity  Mounted on
tank0/test             2.0G   465M   1.5G    23%    /data/test

$ cd /data/test
$ du -sh share/
481M   share/

$ find . -type f -size +1024 | while read f; do
      echo "re-creating $f ..."
      cp "$f" "$f".$$ && rm "$f"
      mv "$f".$$ "$f"
done

$ du -sh share/
 417M   share/

$ df -h /data/test/
Filesystem             size   used  avail capacity  Mounted on
tank0/test             2.0G   401M   1.6G    20%    /data/test

$ zfs get compressratio tank0/backup
NAME          PROPERTY       VALUE         SOURCE
tank0/backup  compressratio  1.00x         -

Swap

Using ZFS as a swap or dump device:[3][4][5]

zfs create -V 1gb export/swap
swap -a /dev/zvol/dsk/export/swap

Deduplication

$ zfs set dedup=sha256 tank0

$ cp -a /usr/libexec/ /tank0/one

$ df -h /tank0
Filesystem         Size       Used      Avail %Cap Mounted on
tank0              368M       103M       265M  27% /tank0

$ zfs list tank0
NAME   USED  AVAIL  REFER  MOUNTPOINT
tank0   104M   265M   103M  /tank0

And once more:

$ cp -a /usr/libexec/ /tank0/two

$ zfs list tank0
NAME   USED  AVAIL  REFER  MOUNTPOINT
tank0  202M   265M   201M  /tank0

$ df -h /tank0
Filesystem         Size       Used      Avail %Cap Mounted on
tank0              470M       205M       265M  43% /tank0

Repeating the same ever so often will make both the df and zfs list output look more and more unrealistic, as now the filesystem reports a bigger total size the more copies it holds. We can query the pool for more details:

$ zpool get -o all size,capacity,dedupditto,dedupratio,free,allocated,fragmentation tank0
NAME   PROPERTY       VALUE       SOURCE
tank0  size           496M        -
tank0  capacity       20%         -
tank0  dedupditto     0           default
tank0  dedupratio     8.05x       -
tank0  free           392M        -
tank0  allocated      104M        -
tank0  fragmentation  17%         -

Links

References