User Tools

Site Tools


quickreference:zfs

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

quickreference:zfs [2019/06/15 16:35] (current)
Line 1: Line 1:
 +====== ZFS Quick Guide ======
 +
 +The ZFS file system is used widely on BSD, and is coming into more use on Linux. Following are some of my notes on it.
 +
 +===== Initial Setup (FreeBSD) =====
 +==== Start ZFS service ====
 +
 +FreeBSD comes with the ZFS service installed, but not active. We need to start the service, and also tell the system to start it when the system reboots.
 +
 +<code bash>
 +echo '​zfs_enable="​YES"'​ >> /​etc/​rc.conf
 +service zfs start
 +</​code>​
 +
 +==== Create a zpool ====
 +Now that we have ZFS running, we'll create a zpool, the basic container for all of our stuff. In this case, I want to use the raidz2 for redundancy (two drives are used for checksumming). Since I don't know the correct names for everything, I'll egrep /​var/​run/​dmesg.boot to find them
 +
 +<code bash>
 +# find the drives on the system
 +egrep '​da[0-9]|cd[0-9]'​ /​var/​run/​dmesg.boot | sort
 +# we want RAID-6, name it storage, and us /dev/da0 through 7
 +zpool create -f storage raidz2 /​dev/​da[01234567]
 +</​code>​
 +
 +===== Use a ZFS Volume for swap space =====
 +
 +It is perfectly fine to set up swap on a ZFS volume, but we do want to turn of checksumming. Here, we'll create a 2G ZFS volume named swap (in storage, so storage/​swap),​ add an entry to fstab, and turn it on.
 +
 +<code bash>
 +zfs create -V 2G -o checksum=off storage/​swap
 +echo '/​dev/​zvol/​storage/​swap none swap sw 0 0' >> /etc/fstab
 +swapon /​dev/​zvol/​storage/​swap
 +</​code>​
 +
 +NOTE: you can add space to this swap area in real time, if you do it at a time when the swap is not necessary to the continued operating of the machine. Simply turn off swap, increase the volume size, then turn swap back on again.
 +
 +<code bash>
 +# turn off swap
 +swapoff /​dev/​zvol/​storage/​swap
 +# increase volume size to 4G
 +zfs set volsize=4096M storage/​swap
 +# check that it worked
 +zfs get volsize,​reservation storage/​swap
 +# turn swap back on (could also use swapon /​dev/​zvol/​storage/​swap,​ but I'm lazy)
 +swapon -aL
 +</​code>​
 +
 +===== Getting and setting properties =====
 +
 +ZFS has properties that allow you to modify the way the file system works. The following example sets a quota of 100 Megabytes for storage/​varlog so our logs do not fill up the system. ​
 +
 +Note the last line showing you can set multiple properties at one time by separating the properties by spaces
 +
 +<code bash>
 +zfs get all storage/​varlog
 +zfs get quota storage/​varlog
 +zfs set quota=100M storage/​varlog
 +zfs set exec=off checksum=off storage/​varlog
 +</​code>​
 +
 +If you want to return to the default settings, where a property is inherited from the container, use the following code
 +<code bash>
 +zfs inherit -r quota storage/​varlog
 +</​code>​
 +
 +===== Find differences between two snapshots =====
 +So, you have zfs running, and you have some automated process running every morning at 4am. Now, you want to see what changed in the 24 hour period.
 +<code bash>
 +# get a list of snapshots so we know exactly what to ask for
 +zfs list -r -t snapshot storage/​someplace
 +# find the differences. First one should be the most recent
 +zfs diff storage/​someplace/​subdir@20181209_041339 storage/​someplace/​subdir@20181209_041339
 +# same thing, but looking only for one subdir named joe
 +zfs diff storage/​someplace/​subdir@20181209_041339 storage/​someplace/​subdir@20181208_041339 | grep '/​joe/'​
 +</​code>​
 +
 +output is similar to Subversion, ie a '​M'​ indicates it was modified, a - indicates it was removed, and a + indicates it was added. Note that your snapshots do not have to be consecutive;​ you can look at the diff between your oldest and newest snapshot, or even your current copy, as this shows:
 +<code bash>
 +# compare snapshot taken 20181209_041339 with the current copy
 +zfs diff storage/​someplace/​subdir@20181209_041339 storage/​someplace/​subdir
 +</​code>​
 +===== Useful commands =====
 +List all snapshots in a particular tree. gives USED (space used by snapshot) and REFER (data referred to in original set)
 +<code bash>
 +zfs list -r -t snapshot /​storage/​varlog
 +</​code>​
 +
 +Remove an existing snapshot (use above command to find the correct name)
 +<code bash>
 +zfs destroy -r tank/​storage/​varlog/​@20181026_054020
 +</​code>​
 +
 +
 +Get a nice list of stats on every dataset in a tree (does the whole tree). Gives AVAIL, ie amount of space available, USED, USEDSNAP (space used by snapshots), USEDDS (space used by the dataset exclusive of snapshots, ie actual data), USEDREFRESERV (whatever that is) and USEDCHILD (used by children of the dataset).
 +<code bash>
 +zfs list -o space -r storage/​varlog
 +</​code>​
 +
 +
 +===== References =====
 +   * https://​www.freebsd.org/​doc/​handbook/​zfs.html
 +   * https://​www.freebsd.org/​doc/​handbook/​zfs-quickstart.html
 +   * https://​forums.freebsd.org/​threads/​is-swap-on-zfs-safe.27855/​
 +   * https://​www.freebsd.org/​doc/​handbook/​adding-swap-space.html
 +   * https://​www.cyberciti.biz/​faq/​freebsd-hard-disk-information/​
 +   * https://​www.thegeekdiary.com/​how-to-find-the-space-consumed-by-zfs-snapshots/​
 +
  
quickreference/zfs.txt ยท Last modified: 2019/06/15 16:35 (external edit)