Stage 3 · Build
Storage & Filesystems
Btrfs & ZFS
Copy-on-write, snapshots, compression, and RAID-Z — advanced storage features on Linux.
Copy-on-Write
Copy-on-write (CoW) filesystems never overwrite existing data in place. Instead, they write new data to a new location and then update the metadata pointer. This provides atomic updates, efficient snapshots, and prevents corruption from partial writes.
# Traditional filesystem (ext4)
# 1. Write data to existing blocks
# 2. Crash during write = corrupted data
# CoW filesystem (Btrfs/ZFS)
# 1. Write new data to free space
# 2. Atomically update metadata pointer
# 3. Crash during write = old data intact
# Check CoW status on Btrfs
lsattr /mnt/btrfs/file
# -----C-- /mnt/btrfs/file
# Disable CoW for specific files
chattr -C /mnt/btrfs/database.img
lsattr /mnt/btrfs/database.img
# ------C- /mnt/btrfs/database.imgCoW provides crash consistency without complex journaling. The trade-off is fragmentation over time, which Btrfs addresses with periodic defragmentation.
Btrfs Overview
Btrfs (B-tree filesystem) is a CoW filesystem with built-in RAID, compression, snapshots, and volume management. It is the default for openSUSE and Fedora workstation. Btrfs integrates many features that traditionally require separate tools.
# Create a Btrfs filesystem
sudo mkfs.btrfs -L mypool /dev/sdb1 /dev/sdc1
# Mount and check
sudo mount /dev/sdb1 /mnt
sudo btrfs filesystem usage /mnt
# Overall:
# Device size: 1.00TiB
# Device allocated: 1.00TiB
# Device unallocated: 0.00B
# Add a device
sudo btrfs device add /dev/sdd1 /mnt
# Remove a device
sudo btrfs device remove /dev/sdd1 /mntBtrfs can span multiple devices and provide RAID-like redundancy. It manages volumes, RAID, and filesystems in a single layer.
ZFS on Linux
ZFS combines a filesystem with a volume manager, providing enterprise-grade features like copy-on-write, snapshots, compression, deduplication, and RAID-Z. ZFS on Linux (OpenZFS) is available as a kernel module.
# Create a ZFS pool
sudo zpool create mypool mirror /dev/sdb /dev/sdc
# Check pool status
sudo zpool status mypool
# pool: mypool
# state: ONLINE
# NAME STATE READ WRITE CKSUM
# mypool ONLINE 0 0 0
# mirror-0 ONLINE 0 0 0
# sdb ONLINE 0 0 0
# sdc ONLINE 0 0 0
# Create a dataset
sudo zfs create mypool/data
sudo zfs set compression=lz4 mypool/data
sudo zfs set mountpoint=/data mypool/dataZFS pools (zpools) are the storage foundation. Datasets are filesystems within a pool. ZFS manages space allocation across all devices in the pool automatically.
Snapshots
Snapshots are instant, space-efficient copies of the filesystem at a point in time. Because CoW never overwrites data, snapshots are created by simply freezing the current state. They consume no additional space until the original data changes.
# Btrfs snapshot
sudo btrfs subvolume snapshot /mnt/data /mnt/snapshots/data-$(date +%F)
# List snapshots
sudo btrfs subvolume list /mnt
# ID 256 gen 1000 top level 5 path data
# ID 257 gen 1001 top level 5 path snapshots/data-2024-01-15
# Restore a snapshot
sudo btrfs subvolume delete /mnt/data
sudo btrfs subvolume snapshot /mnt/snapshots/data-2024-01-15 /mnt/data
# ZFS snapshot
sudo zfs snapshot mypool/data@before-upgrade
sudo zfs list -t snapshot
# NAME USED REFER MOUNTPOINT
# mypool/data@before-upgrade 0B 100G -
# Rollback
sudo zfs rollback mypool/data@before-upgradeSnapshots are nearly instant and consume no space initially. They are ideal for backups, rollbacks, and point-in-time recovery.
Compression
# Btrfs compression
sudo btrfs property set /mnt compression lz4
sudo btrfs filesystem usage /mnt
# ZFS compression
sudo zfs set compression=lz4 mypool/data
sudo zfs get compression mypool/data
# NAME PROPERTY VALUE SOURCE
# mypool/data compression lz4 local
# Check compression ratio
sudo zfs get compressratio mypool/data
# NAME PROPERTY VALUE SOURCE
# mypool/data compressratio 1.45x -LZ4 is the best balance of speed and compression ratio. ZSTD offers higher compression but more CPU overhead. LZ4 typically achieves 1.5-2x compression with negligible performance impact.
RAID Features
| Feature | Btrfs | ZFS |
|---|---|---|
| RAID 0 | Yes | Yes (striped) |
| RAID 1 | Yes | Yes (mirrored) |
| RAID 5/6 | Experimental | RAID-Z1/Z2/Z3 (production) |
| Self-healing | Metadata only | Data + metadata |
| Deduplication | No | Yes (memory-intensive) |
| Scrub | Yes | Yes |
# Btrfs RAID 1
sudo mkfs.btrfs -d raid1 -m raid1 /dev/sdb /dev/sdc
# ZFS RAID-Z2 (like RAID 6)
sudo zpool create mypool raidz2 /dev/sd{b,c,d,e}
# Scrub (verify data integrity)
sudo btrfs scrub start /mnt
sudo btrfs scrub status /mnt
# ZFS scrub
sudo zpool scrub mypool
sudo zpool status mypoolRegular scrubs verify data integrity and repair silent corruption using parity data. Schedule scrubs weekly for production systems.
ZFS is the best choice for NAS, backup, and storage servers. Its data integrity features, snapshots, and RAID-Z are battle-tested. Btrfs is better for workstation use with snapshot-based rollbacks.
Btrfs RAID 5/6 has known issues including the write hole and incomplete recovery. Use Btrfs RAID 0, 1, or 10 only. For RAID 5/6, use ZFS RAID-Z or mdadm.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.