ZFS Administration
Dataset and Filesystem Management
In ZFS, datasets and filesystems are the core units for managing data. A dataset can be a file system, a volume (ZVOL), or a snapshot. This section covers how to create, delete, and rename datasets, configure dataset properties, and manage filesystem mounting and unmounting in ZFS.
Creating, Deleting, and Renaming Datasets
Creating Datasets
In ZFS, creating a dataset within a ZPOOL is a straightforward process. Each dataset can be customized with specific properties, such as compression or quotas. To create a dataset, the zfs create
command is used, followed by the name of the pool and the desired name of the dataset:
sudo zfs create mypool/mydataset
This command creates a dataset named mydataset
inside the mypool
ZPOOL. The dataset can be used like any traditional filesystem but comes with ZFS-specific features.
Deleting Datasets
To delete a dataset, the zfs destroy
command is used. This permanently removes the dataset and all its data. Be cautious when deleting datasets, as this operation cannot be undone.
sudo zfs destroy mypool/mydataset
In this example, the mydataset
dataset in the mypool
ZPOOL is deleted. This removes the dataset and frees the associated storage.
Renaming Datasets
If you need to rename a dataset, the zfs rename
command allows this without affecting the data or structure of the dataset. The syntax is as follows:
sudo zfs rename mypool/mydataset mypool/newdatasetname
This command renames mydataset
to newdatasetname
while keeping the dataset in the same ZPOOL (mypool
).
Setting Dataset Properties
ZFS datasets come with various properties that can be adjusted to meet the requirements of specific workloads. Common properties include compression, deduplication, quotas, and reservation limits.
Enabling Compression
Compression is a valuable feature in ZFS that can save disk space without a significant performance hit, depending on the workload. To enable compression on a dataset, use the zfs set
command:
sudo zfs set compression=on mypool/mydataset
ZFS supports multiple compression algorithms, such as lz4
, gzip
, and zle
. You can specify the compression type as follows:
sudo zfs set compression=lz4 mypool/mydataset
Enabling Deduplication
Deduplication eliminates redundant copies of data blocks within a ZFS pool, reducing storage usage. However, deduplication can have a significant impact on performance and memory usage, so it should be used with caution. To enable deduplication on a dataset:
sudo zfs set dedup=on mypool/mydataset
To disable deduplication later:
sudo zfs set dedup=off mypool/mydataset
Mounting and Unmounting Filesystems
ZFS automatically mounts datasets as filesystems at boot time, but filesystems can also be mounted and unmounted manually as needed.
Mounting Filesystems
By default, ZFS filesystems are automatically mounted in the /mypool
directory. However, if manual mounting is required, the zfs mount
command can be used:
sudo zfs mount mypool/mydataset
This command mounts the mydataset
filesystem, making it accessible.
Unmounting Filesystems
To unmount a ZFS filesystem, use the zfs unmount
command:
sudo zfs unmount mypool/mydataset
Unmounting removes the dataset from the filesystem hierarchy without deleting the data. It can be mounted again later.
Dataset and Filesystem Management
Creating, Deleting, and Renaming Datasets
In ZFS, datasets are logical units of storage that can be either file systems or volumes (ZVOLs). Managing datasets is straightforward and involves creating, deleting, and renaming them as needed.
To create a dataset, the zfs create
command is used. For example, to create a dataset named mypool/mydataset
, the following command is issued:
zfs create mypool/mydataset
This creates a dataset within the storage pool mypool
.
Deleting a dataset is performed using the zfs destroy
command. For example, to delete the dataset mypool/mydataset
, use:
zfs destroy mypool/mydataset
If there are nested datasets, the -r
flag can be used to recursively delete the dataset and all its children.
To rename a dataset, use the zfs rename
command. For example, renaming mypool/mydataset
to mypool/mynewdataset
would be done as follows:
zfs rename mypool/mydataset mypool/mynewdataset
Renaming a dataset does not affect the data within, but it changes the dataset’s name, which can also alter its mount point.
Setting Dataset Properties (compression, deduplication)
ZFS allows various properties to be set on datasets to optimize performance, reduce storage usage, and enforce quotas. Two of the most commonly used properties are compression and deduplication.
To enable compression on a dataset, use the zfs set
command:
zfs set compression=on mypool/mydataset
This applies ZFS's transparent compression algorithm, which automatically compresses data as it is written, reducing the physical storage required.
Deduplication eliminates duplicate data blocks across the dataset, saving storage space. Deduplication is enabled with:
zfs set dedup=on mypool/mydataset
However, deduplication can have a performance impact, especially on memory usage, so it should be enabled with caution.
Dataset properties can also include features like readonly
, atime
, and recordsize
, allowing for fine-tuned control over how data is managed.
Mounting and Unmounting Filesystems
ZFS automatically mounts file systems as they are created, but file systems can be manually mounted or unmounted as needed.
To unmount a file system, use the zfs unmount
command:
zfs unmount mypool/mydataset
This unmounts the file system, making it temporarily unavailable until remounted.
To mount a file system manually, use:
zfs mount mypool/mydataset
ZFS can also handle temporary and permanent mounting. By default, datasets are automatically mounted at /mypool/mydataset
, but custom mount points can be set using:
zfs set mountpoint=/mnt/mycustommount mypool/mydataset
This allows the flexibility to structure file systems according to user or application needs.
Managing Snapshots and Clones
Automating Snapshot Creation
Snapshots in ZFS are point-in-time representations of datasets and volumes. These are efficient and instantaneous, consuming little space initially, as only the changes made after the snapshot are stored.
Automating the creation of snapshots can be handled through cron jobs or tools like zfs-auto-snapshot. Here’s an example cron job that takes a snapshot of mypool/mydataset
every day at midnight:
0 0 * * * zfs snapshot mypool/mydataset@$(date +\%Y-\%m-\%d)
This job creates daily snapshots with the date appended as the snapshot name. More advanced tools like zfs-auto-snapshot allow users to configure automatic snapshot retention policies for hourly, daily, weekly, and monthly snapshots.
Rolling Back to Snapshots
One of the most powerful features of ZFS is the ability to roll back to a previous snapshot, effectively returning the dataset to its previous state. This is useful in cases of accidental deletion or corruption.
To roll back to a snapshot, the following command is used:
zfs rollback mypool/mydataset@snapshotname
Rolling back destroys all changes made after the snapshot, so it is a destructive operation. ZFS allows rollback to intermediate snapshots if necessary, but it requires destroying more recent snapshots to do so.
Deleting Snapshots and Clones
Deleting snapshots is straightforward and helps free up storage space consumed by historical changes. Use the zfs destroy
command:
zfs destroy mypool/mydataset@snapshotname
Deleting a snapshot frees space consumed by the changes made after the snapshot was taken, but it leaves the underlying dataset intact.
Clones, unlike snapshots, are writable copies of snapshots. They are created using the zfs clone
command and must be based on an existing snapshot. Clones are independent of the original dataset and can be promoted to become the primary dataset.
To delete a clone, first ensure that it is not dependent on any other datasets. Then use:
zfs destroy mypool/myclone
Clones are a powerful feature in ZFS, allowing developers to test changes on a dataset without affecting the original data.