As mentioned on previous posts, I have spent the past few weeks dealing with a ZFS crash on my FreeNAS install. Because of that, not only was I forced to learn how to troubleshoot ZFS, but I also had to learn how to setup new volumes and come up with new backup strategies (between a few other things).
What is ZFS and Why Should I Use it? The Z file system is a free and open source logical volume manager built by Sun Microsystems for use in their Solaris operating system. Some of its most appealing features include. Hello, I just built a box with Core i3, ECC RAM 20Gb, 500Gbx2 hard drives (just use them for practice). I installed the newest version of FreeNAS in a 16GB USB driver. When I booted FreeNAS entered 'storage', I couldn't find the tab of 'ZFS Volume Manager' like I watched the video or somewhere. ZFS Volume Manager. Next, add a new name for your volume, Here I’ve defined as tecmintpool. To add the available disks, click on + sign and add the disks. There are totally 8 drives available now, add them all. Introduction to ZFS. The following are some of the features of ZFS filesystem: Protection against. ZFS is a combined filesystem and logical volume manager. The features of ZFS include protection against data corruption, support for high storage capacities, efficient data compression, integration of the filesystem and volume management concept, snapshots and copy-on-write clones, continuous integrity checking and automatic repair, RAID-Z and native NFSv4 ACLs.
This was a great opportunity for me to learn more about ZFS (because I new ‘nada’ to start with). And I’m happy to share some of the knowledge that I gathered with you on this post.
Please keep in mind that I don’t consider myself an expert on ZFS (not even close), but I will try to make things simple and easy to understand for someone, who like me, is just getting started with ZFS.
What is ZFS and It’s History
ZFS is a local filesystem (i.e.: ext4, NTFS, exfat) and logical volume manager (i.e.: LVM on Linux) created by Sun Microsystems. ZFS was published under an open source license until Oracle bought Sun Microsystems and closed source the license. Because the source code was already in the open and ported to different OSs, eventually a project called ‘OpenZFS’ was created, and that is the core code that is used on most Unix like systems today (Linux, FreeBSD, etc.).
ZFS Components
vdev
A vdev is composed of one or more physical drives (can also be of things other than hard drive, like files). They can be combined together in mirrors or RAIDZs.
💡 TIP: There are 7 different types of vdevs, and some of them (like host spare, L2ARC and ZIL) are very important.
Pool
A pool is composed of one or more vdevs and they usually contain a volume or a dataset (which you create after creating the pool). You create/define your vdevs when you create a pool (with the zpool
command which we’ll see later). This allows you to mix vdev types together to achieve other RAIDZ levels (see example below):
Datasets
Dataset is the filesystem part of ZFS (so far we’ve seen the LVM components). Here you can define user access, quotas, compression, snapshots, etc…
Volume
Volume is the brother of datasets but in a block device representation. It provides some of the features that datasets have, but not all. Volumes can be useful to run other filesystems on top of ZFS, or to export iSCSI extents.
RAIDZ Types
- Dynamic/Simple Stripe (RAID0) - Distributes data without parity. Loosing a device means loosing all data
- MIRROR (RAID1) - Mirrored drives. Used with 2 to 4 disks (or more)
- RAIDZ-1 (RAID5) - Distributes parity along with the data and can lose one physical drive before a raid failure. RAIDZ requires at least 3 disks
- RAIDZ-2 (RAID6) - Distributes parity along with the data and can lose up to 2 physical drives. RAIDZ-2 requires at least 4 disks
- RAIDZ-3 - Distributes parity along with the data and can lose up to 3 physical drives. RAIDZ-3 requires at least 4, but should be used with no less than 5 disks
Let’s take a look at the most common commands for handling ZFS pools and filesystem. We’ll use /dev/sdx
to refer to device names, but keep in mind that using the device UUID is preferred in order to avoid boot issues due to device name changes.
1.ZFS Pool Commands
These are the commands related to creating vdevs and pools. We will be looking at:
zpool create
- Create a pool (and vdevs)zpool status
- Displays pool statuszpool list
- List pool and it’s detailszpool history
- Shows history of commands for zpoolzpool import
- Imports and mounts poolzpool export
- Exports and unmounts poolzpool destroy
- Destroy pool and all filesystemszpool scrub
- Starts scrub of pool
1.1.Creating a Pool (and vdevs)
To create a new pool we use the zpool create
command. We specify the pool name and the device we want to use.
It’s basic usage is:
Now let’s look at different examples for this command.
Create a pool on a single disk
The command below creates a pool on a single disk.
Create a dynamic stripe pool on 3 disks
Remember that dynamic stripe is the same as RAID0 and that it has no parity.
We can view the new pool with zpool status
Note that the pool name is ‘tank’ and the vdevs are ‘sdb’, ‘sdc’ and ‘sdd’
Create a mirrorred pool on 2 disks
Note that I can omit /dev
and give the device name. Let’s view the result.
Our vdev is ‘mirror-0’ and our pool is tank.
Create a RAID-Z pool
And the result indicating that my vdev is RAIDZ1.
You can use the same command to create RAIDZ2,3 pools.
Specifying a default mount point for the pool
You can also specify the default mount point for the pool by using the -m
flag as you create it.
We can see that our new pool was created and mounted at /mnt/tank
💡 TIP: Also read up on the zpool add
command.
1.2.Getting Pool Status
After we create a new pool it’s automatically imported into our system. As we have seen before, we can view details of the pool with the zpool status
command.
Some of the fields we did not look before are:
state:
Indicates if pool is online or notstatus:
Additional information about the poolaction:
Indicates if there are any pending actions for the poolscan:
If a scrub is in progress or the last scrub run statuserrors:
Indicates if there are any problems with the pool
For example:
Another example:
1.3.Listing Pools
As we have seen before, we can view some details of the pool with the zpool status
command. But there are other commands, like zpool list
that can give us information about the pool.
On the example below, we look at the details for our mirrored tank pool:
1.4.Show Pool History
This is another useful command that displays the history of commands that were executed against a pool from it’s creation (of course only commands that make changes to the pool’s configuration).
1.5.Importing Pools
Usually after creating a pool it’s set to import and mount automatically, but you may encounter scenarios where you need to manually import a pool (like when troubleshooting or after re-imaging a system).
Note that the import command will also mount the pool.
Lists pools available to import
Running the zpool import
command without a pool name will show you a list of pools that can be imported.
Example of when no pools are available to be imported.
Here we have a pool that can be imported.
Importing the pool
Give the command a pool name and it will be imported.
You can also import all available pools by using the -a
option.
Importing a Pool with an Alternate Root Location
Use the -R
flag to mount the pool to an alternate root location. Note that this is not the mount path for the pool, but an alternate root folder.
tank
is by default configured to be mounted at /mnt/tank
1.6.Exporting the Pool
As expected, this is the opposite of the import command. The export command attempts to unmount any mounted file systems within the pool before continuing.
For example:
If any of the file systems fail to unmount you can forcefully unmount them by using the -f
option. However, if ZFS volumes exist and are in use, even with -f
it will fail to export.
1.7.Destroying/Deleting Pools
We can use the zfs destroy
command to delete pools and all it’s child datasets and/or volumes.
⚠️ WARNING:This will delete all your data, including any snapshots your may have.
1.8.Scrubbing Pools
ZFS scrub checks every block in a pool against its known checksum to make sure that the data is valid. If you have vdevs with parity, ZFS scrub will also repair the data using healthy data from other disks. Scrubs should run on a schedule to make sure your systems stays healthy.
Initiating a scrub
Initiating a scrub of a pool is as simple as running:
Checking the status of a scrub
You can check the status of a scrub with zpool status
and looking for messages in the ‘scan’ section.
Stopping a scrub
Use the -s
flag.
2.ZFS Filesystem Commands
Now we will look at the commands that will help us work with filesystems (datasets) and volumes. We will concentrate more on the filesystem side of things and will not cover volumes.
The commands we will review are:
zfs create
- Creates a new volume or filesystemzfs mount/umount
- Mounts the filesystemzfs list
- Lists datasets and snapshotszfs get/set
- Gets configuration and sets configuration for the datasetzfs snapshot
- Handles snapshotszfs diff
- Used to compare data between snapshotzfs rollback
- Rolls back a snapshotzfs send/recv
- Sends a snapshot as a stream of datazfs destroy
- Deletes datasets and snapshots
2.1. Creating Datasets
We can create datasets with the zfs create
command. Here we create ‘dataset1’ as child of the ‘tank’ dataset (that was created automatically with the zpool create
command).
Creating missing parent datasets
We can also create missing parent datasets with the -p
flag (similar to mkdir -p
).
2.2. Mounting Filesystems (Datasets)
Zfs Volume Manager App
We can use the zfs mount/unmount
commands to view the current mount points as well as mounting/unmounting filesystems.
Viewing current mounted filesystems
Without any arguments, zfs mount
will display all mounted zfs filesystems and their respective mount points (without the child datasets).
Mounting filesystems
Use zfs mount [pool|dataset]
to mount filesystems. On the example below we use zfs mount
to establish that no datasets are mounted, and then we mount the ‘tank’ dataset and confirm that is mounted with zfs mount
.
Use the -a
option to mount all filesystems.
Mount a child dataset
You can also mount a child dataset without the parent datasets. For example, here we confirm that ‘tank’ is not mounted, then we look at the available datasets, and we execute the command to mount the tank/dataset2/childset2
dataset only.
Note that this will create the required path in the OS filesystem to mount the child dataset. If you decide to mount the parent dataset later you may run into a directory is not empty
error because of the created directories.
Unmounting filesystems
Runzfs unmount
and specify the dataset name.
2.3. Listing Filesystems (Datasets)
You can list the dataset by running zfs list [dataset name]
.
And you can also specify the mount point as an argument.
If run without a dataset name, zfs list
will show all datasets in the system recursively.
💡 TIP: when specifying a dataset name you can also use the -r
flag to display the dataset recursively.
2.4. Getting and Setting Dataset Properties
Properties control the behavior of filesystems, volumes, snapshots, and clones. ZFS properties can look similar to mount options.
Getting a list of all the properties for a dataset
Getting the current value for a specific property
Setting a property value
Use the zfs set
command.
2.5. Creating Snapshots
Snapshots allow you to save the state of a filesystem to a current point in time, without duplicating storage (files are not copied). It flags existing data as «read-only» while allowing new data to be added to the filesystem without affecting the existing data blocks that are protected by the snapshot (the whole process is a bit more complicated than this).
Let’s take a look at the image below as an example. You have a filesystem with existing data (Data A) and you take a snapshot (snapshot 1). Then you make some changes, add new files (Data B) and take another snapshot (snapthot 2). After that you make even more changes (Data C).
Snapshot 1 protects the original data (Data A), while snapshot 2 protects Data Changes (B) as well as the original data (Data A). So you can delete snapshot 1 and data (A) will still be protected.
Zfs Volume Manager Login
Note: The amount of data used for the snapshots is very small because we are not copying the files, but instead the filesystem top-level metadata block indicating the they belong to a snapshot.
And here are a few scenarios of what happens when you delete files and snapshots:
Snapshots are great for testing software development, or creating a failsafe before an upgrade. But by no means they should be considered (by itself) as a backup or DR solution.
Creating a snapshot
Zfs Volume Manager Freenas
For example:
Creating recursive snapshots
If you have multiple child datasets, you can either create one snapshot of the top-level dataset (usually the pool name), or use the -r
flag to create snapshots recursively.
Snapshot of the main dataset:
Recursive snapshot:
Listing snapshots
Use zfs list -t snapshot
.
2.6. Comparing Snapshots
You can use zfs diff
to compare snapshots.
For example:
2.7. Restoring a Snapshot
Restore a snapshots with zfs rollback
. Note that restoring a snapshot will delete all files that were created after the snapshot (as we saw in our example). It will also delete any newer snapshots (you will be asked to use the -r
option to rollback and delete newer snapshots).
2.9. Sending and Receiving Snapshots
One of the best features of ZFS is ‘ZFS send’. It allows you send snapshots as a stream of data. This is a great way replicate a snapshot and it’s dataset to a file, another pool or even to another system via SSH. Amazing no!
Let’s look at the example below. We have 2 pools in our system named ‘tank’ and ‘backup’.
In our tank pool we have a dataset for our Movies.
Before we can send this data we need create a snapshot:
And now we can send our snapshot to our backup pool with zfs send/recv
.
Zfs Volume Manager Windows 10
And let’s confirm that it worked.
💡 TIP: It’s worth to look into all the options and use cases for ZFS send. Combined with RAIDZs and snapshots, it can help you make your filesystem almost indestructible.
2.10. Destroying Filesystems (Datasets) and Snapshots
Destroying datasets
To destroy a dataset, use zfs destroy
(the -r
flag also works here).
Destroying snapshots
To destroy a snapshot, also use the zfs destroy
command (and the -r
flag also works here).
Conclusion
While we covered a lot of different topics and commands on ZFS, in reality, we really only scratched the surface on what ZFS can do. If you want to learn more about ZFS I’ve added a few links below with some great reading.
References and additional reading:
Zfs Volume Manager Mac
- FreeBSD Mastery: ZFS - https://www.goodreads.com/book/show/25595471-freebsd-mastery
- Ubuntu Wiki - https://wiki.ubuntu.com/ZFS
- Zpool Administration - https://pthree.org/2012/04/17/install-zfs-on-debian-gnulinux/
- ZFS Build - http://www.zfsbuild.com/
- ZFS Features and Terminology - https://www.freebsd.org/doc/handbook/zfs-term.html
- Klennet Storage Software - https://www.klennet.com/zfs-recovery/zfs-basics.aspx