ZFS, or Zettabyte File System, is a powerful and advanced file system and logical volume manager known for its data integrity, snapshot capabilities, and scalability. It primarily uses two command-line utilities: zpool
for managing storage pools (the underlying storage devices) and zfs
for managing datasets (file systems or volumes created within pools).
I. ZFS Pool Management (zpool
)
ZFS pools are the fundamental storage units, built from one or more virtual devices (vdevs) like disks, mirrors, or RAID-Z configurations.
1.1 Pool Creation ➕
- Basic Pool: Creates a pool from one or more disks.
sudo zpool create <poolname> <device1> [<device2> ...]
Example:sudo zpool create mypool /dev/sda /dev/sdb
(creates a striped pool) - Mirrored Pool (RAID 1): Creates a pool with redundant copies of data.
sudo zpool create <poolname> mirror <device1> <device2> [...]
Example:sudo zpool create datapool mirror /dev/sda /dev/sdb
- RAID-Z Pool (RAID 5/6/7 equivalent): Creates a pool with parity for data protection.
raidz
(single parity, similar to RAID 5):sudo zpool create <poolname> raidz <device1> <device2> <device3> [...]
raidz2
(double parity, similar to RAID 6):sudo zpool create <poolname> raidz2 <device1> <device2> <device3> <device4> [...]
raidz3
(triple parity):sudo zpool create <poolname> raidz3 <device1> <device2> <device3> <device4> <device5> [...]
Example:sudo zpool create archivepool raidz2 /dev/sde /dev/sdf /dev/sdg /dev/sdh
- Adding Devices (to expand pool):
sudo zpool add <poolname> <vdev_type> <device1> [<device2> ...]
Example:sudo zpool add mypool mirror /dev/sdc /dev/sdd
(adds a new mirrored vdev tomypool
) - Adding Hot Spare: Adds a disk that ZFS can automatically use if another disk fails.
sudo zpool add <poolname> spare <device>
Example:sudo zpool add datapool spare /dev/sdz
1.2 Pool Status & Information 📊
- List all pools:
zpool list
- Check pool status (health, errors, vdevs):
zpool status [<poolname>]
Example:zpool status -x
(shows status for unhealthy pools only) - Show I/O statistics:
zpool iostat [<poolname>] [<interval>]
Example:zpool iostat -v mypool 5
(shows verbose I/O stats formypool
every 5 seconds) - Scrub a pool (data integrity check): Initiates a read of all data to detect and correct silent data corruptions.
sudo zpool scrub <poolname>
Example:sudo zpool scrub datapool
1.3 Pool Maintenance & Repair 🔧
- Offline a device: Temporarily takes a device offline for maintenance.
sudo zpool offline <poolname> <device>
- Online a device: Brings an offline device back online.
sudo zpool online <poolname> <device>
- Replace a device: Replaces a failed or soon-to-fail disk with a new one.
sudo zpool replace <poolname> <old_device> <new_device>
Example:sudo zpool replace datapool /dev/sda /dev/sdb
(replaces/dev/sda
with/dev/sdb
) - Detach a device (from a mirror): Removes a device from a mirrored vdev.
sudo zpool detach <poolname> <device>
Example:sudo zpool detach datapool /dev/sdc
- Clear errors: Clears error counts on devices.
sudo zpool clear <poolname> [<device>]
- Destroy a pool: Permanently deletes a pool and all its data. Use with extreme caution.
sudo zpool destroy <poolname>
Example:sudo zpool destroy testpool
1.4 Pool Import/Export 🌐
- Export a pool: Disconnects a pool from the current system, making it safe to move disks.
sudo zpool export <poolname>
- List pools available for import:
zpool import
- Import a pool: Attaches a previously exported pool to the current system.
sudo zpool import <poolname>
Example:sudo zpool import -d /dev/disk/by-id oldpool
(importsoldpool
searching in/dev/disk/by-id
)
II. ZFS Dataset Management (zfs
)
Datasets are file systems or block devices (zvols) created within a ZFS pool. They share the pool’s storage and can inherit properties.
2.1 Dataset Creation & Deletion ➕➖
- Create a filesystem dataset:
sudo zfs create <poolname>/<datasetname>
Example:sudo zfs create mypool/data
(mounts by default to/mypool/data
) - Create a volume (zvol): Creates a block device that can be used like a disk.
sudo zfs create -V <size> <poolname>/<volumename>
Example:sudo zfs create -V 10G mypool/swapvol
(creates a 10GB volume) - Destroy a dataset:Deletes a dataset and all its contents.
sudo zfs destroy <poolname>/<datasetname>
Example:sudo zfs destroy mypool/olddata
- To destroy recursively (including child datasets and snapshots):
sudo zfs destroy -r <poolname>/<datasetname>
- To destroy recursively (including child datasets and snapshots):
- Rename a dataset:
sudo zfs rename <old_name> <new_name>
Example:sudo zfs rename mypool/data mypool/webserver
2.2 Dataset Properties ⚙️
ZFS datasets have many properties that can be set and inherited.
- List properties:
zfs get all <poolname>/<datasetname>
Example:zfs get compression,mountpoint,quota mypool/data
- Set a property:
sudo zfs set <property>=<value> <poolname>/<datasetname>
Example:sudo zfs set compression=lz4 mypool/data
(enables LZ4 compression)sudo zfs set quota=100G mypool/users
(sets a 100GB quota)sudo zfs set recordsize=1M mypool/bigfiles
(optimizes for large files)sudo zfs set mountpoint=/var/www mypool/webserver
(changes mount point)
- Inherit a property: Resets a property to its inherited value from its parent.
sudo zfs inherit <property> <poolname>/<datasetname>
Example:sudo zfs inherit compression mypool/data/subdata
2.3 Snapshots & Clones 📸
Snapshots are read-only copies of a dataset at a specific point in time, consuming no additional space initially.
- Create a snapshot:
sudo zfs snapshot <poolname>/<datasetname>@<snapshotname>
Example:sudo zfs snapshot mypool/data@yesterday
- List snapshots:
zfs list -t snapshot
Example:zfs list -r -t snapshot mypool/data
(lists snapshots formypool/data
and its children) - Rollback to a snapshot: Reverts a dataset to the state of a snapshot. Any changes made after the snapshot will be lost.
sudo zfs rollback <poolname>/<datasetname>@<snapshotname>
- To rollback when newer snapshots exist, you must destroy the newer snapshots first or use the
-r
(recursive) option to destroy them automatically:sudo zfs rollback -r mypool/data@yesterday
- To rollback when newer snapshots exist, you must destroy the newer snapshots first or use the
- Destroy a snapshot:
sudo zfs destroy <poolname>/<datasetname>@<snapshotname>
- Create a clone: Creates a writable copy of a snapshot. The clone initially shares data with the snapshot.
sudo zfs clone <poolname>/<datasetname>@<snapshotname> <poolname>/<clonename>
Example:sudo zfs clone mypool/data@yesterday mypool/data_clone
2.4 Send & Receive (Replication) 📤📥
ZFS send
and receive
allow for efficient, incremental backups and replication of datasets and snapshots.
- Send a full snapshot to a file:
sudo zfs send <poolname>/<datasetname>@<snapshotname> > <backup_file>
Example:sudo zfs send mypool/data@snap1 > /mnt/backup/data_snap1.zfs
- Receive a full snapshot from a file:
sudo zfs receive <new_pool>/<new_datasetname> < <backup_file>
Example:sudo zfs receive backup_pool/data_restore < /mnt/backup/data_snap1.zfs
- Send an incremental snapshot (changes between two snapshots):
sudo zfs send -i <poolname>/<datasetname>@<previous_snapshot> <poolname>/<datasetname>@<current_snapshot> > <incremental_backup_file>
Example:sudo zfs send -i mypool/data@snap1 mypool/data@snap2 > /mnt/backup/data_inc_snap1_2.zfs
- Receive an incremental snapshot:
sudo zfs receive <receiving_pool>/<receiving_datasetname> < <incremental_backup_file>
Example:sudo zfs receive backup_pool/data_restore < /mnt/backup/data_inc_snap1_2.zfs
- Send/Receive over network (replication):
sudo zfs send <pool>/<dataset>@<snapshot> | ssh user@remote_host "sudo zfs receive <remote_pool>/<remote_dataset>"
Example:sudo zfs send mypool/data@daily_snap | ssh user@backup_server "sudo zfs receive backup_pool/data_replica"
III. Mounting & Sharing 📁
- Mount all ZFS filesystems:
sudo zfs mount -a
- Unmount a ZFS filesystem:
sudo zfs umount <poolname>/<datasetname>
- Share via NFS:
sudo zfs set sharenfs=on <poolname>/<datasetname>
- Share via SMB/Samba:
sudo zfs set sharesmb=on <poolname>/<datasetname>