Btrfs (B-tree File System) ist ein modernes Copy-on-Write-Dateisystem für Linux. Es bietet Snapshots, Subvolumes, integriertes RAID und Selbstheilung.

Btrfs-Features

Übersicht

| Feature | Beschreibung | |---------|--------------| | Copy-on-Write | Sichere Schreiboperationen | | Snapshots | Sofortige Dateisystem-Kopien | | Subvolumes | Unabhängige Dateisystem-Bereiche | | RAID | Integrierte RAID-Modi | | Checksums | Datenintegritätsprüfung | | Kompression | Transparente Komprimierung | | Online-Resize | Größenänderung im Betrieb |

Installation und Erstellung

Tools installieren

# Debian/Ubuntu
apt install btrfs-progs

# CentOS/RHEL
dnf install btrfs-progs

Dateisystem erstellen

# Einzelne Disk
mkfs.btrfs /dev/sdb

# Mit Label
mkfs.btrfs -L data /dev/sdb

# Mehrere Disks (RAID 0)
mkfs.btrfs -d raid0 -m raid0 /dev/sdb /dev/sdc

# RAID 1
mkfs.btrfs -d raid1 -m raid1 /dev/sdb /dev/sdc

# RAID 10
mkfs.btrfs -d raid10 -m raid10 /dev/sdb /dev/sdc /dev/sdd /dev/sde

Mounten

# Standard
mount /dev/sdb /mnt/data

# Mit Optionen
mount -o compress=zstd,noatime /dev/sdb /mnt/data

# In fstab
/dev/sdb    /mnt/data    btrfs    defaults,compress=zstd,noatime    0    0

Mount-Optionen

Performance

| Option | Beschreibung | |--------|--------------| | compress | zstd, lzo, zlib | | noatime | Keine Access-Time | | ssd | SSD-Optimierung | | space_cache=v2 | Schnellere Speicherallokation | | autodefrag | Automatische Defragmentierung |

Empfohlene Optionen

# HDD
mount -o defaults,noatime,compress=zstd,space_cache=v2 /dev/sdb /mnt

# SSD
mount -o defaults,noatime,compress=zstd,ssd,discard=async,space_cache=v2 /dev/sdb /mnt

# fstab-Eintrag
UUID=xxx    /mnt/data    btrfs    defaults,noatime,compress=zstd:3,space_cache=v2    0    0

Subvolumes

Konzept

Btrfs-Dateisystem
    │
    ├── @ (Root-Subvolume)
    ├── @home (Home-Subvolume)
    ├── @snapshots
    └── @var

Subvolume erstellen

# Subvolume erstellen
btrfs subvolume create /mnt/data/subvol1

# Liste anzeigen
btrfs subvolume list /mnt/data

# Mit ID
btrfs subvolume list -p /mnt/data

Subvolume mounten

# Spezifisches Subvolume mounten
mount -o subvol=@home /dev/sdb /home

# Per ID
mount -o subvolid=257 /dev/sdb /home

# fstab
/dev/sdb    /         btrfs    subvol=@,defaults    0    0
/dev/sdb    /home     btrfs    subvol=@home,defaults    0    0

Subvolume löschen

btrfs subvolume delete /mnt/data/subvol1

Snapshots

Snapshot erstellen

# Read-Only Snapshot
btrfs subvolume snapshot -r /mnt/data /mnt/data/snapshots/snap-2026-01-26

# Read-Write Snapshot
btrfs subvolume snapshot /mnt/data /mnt/data/snapshots/snap-rw

Snapshot wiederherstellen

# Altes Subvolume umbenennen
mv /mnt/@home /mnt/@home-old

# Snapshot als neues Subvolume
btrfs subvolume snapshot /mnt/@snapshots/home-2026-01-26 /mnt/@home

# Altes löschen nach Prüfung
btrfs subvolume delete /mnt/@home-old

Automatische Snapshots mit Snapper

# Snapper installieren
apt install snapper

# Konfiguration erstellen
snapper -c home create-config /home

# Snapshot erstellen
snapper -c home create --description "Vor Update"

# Snapshots auflisten
snapper -c home list

# Snapshot löschen
snapper -c home delete 5

Snapper-Konfiguration

# /etc/snapper/configs/home

SUBVOLUME="/home"
FSTYPE="btrfs"

# Anzahl Snapshots
NUMBER_LIMIT="50"
NUMBER_LIMIT_IMPORTANT="10"

# Zeitbasierte Snapshots
TIMELINE_CREATE="yes"
TIMELINE_LIMIT_HOURLY="10"
TIMELINE_LIMIT_DAILY="7"
TIMELINE_LIMIT_WEEKLY="4"
TIMELINE_LIMIT_MONTHLY="6"
TIMELINE_LIMIT_YEARLY="2"

RAID-Funktionen

RAID-Modi

| Modus | Daten | Metadaten | Min. Disks | |-------|-------|-----------|------------| | single | 1 Kopie | 1 Kopie | 1 | | dup | 1 Kopie | 2 Kopien | 1 | | raid0 | Striping | Striping | 2 | | raid1 | Mirror | Mirror | 2 | | raid10 | Stripe+Mirror | Mirror | 4 | | raid5 | Parity | Mirror | 3 | | raid6 | 2x Parity | Mirror | 4 |

Multi-Device

# Disk hinzufügen
btrfs device add /dev/sdc /mnt/data

# Balance ausführen
btrfs balance start -dconvert=raid1 -mconvert=raid1 /mnt/data

# Status
btrfs device stats /mnt/data

Disk entfernen

# Disk entfernen (Daten werden umverteilt)
btrfs device remove /dev/sdc /mnt/data

# Defekte Disk (offline)
btrfs device remove missing /mnt/data

Disk ersetzen

btrfs replace start /dev/sdb /dev/sdd /mnt/data
btrfs replace status /mnt/data

Balance und Scrub

Balance

# Vollständiges Balance
btrfs balance start /mnt/data

# Nur wenn Nutzung < 50%
btrfs balance start -dusage=50 /mnt/data

# Status/Abbrechen
btrfs balance status /mnt/data
btrfs balance cancel /mnt/data

Scrub

# Scrub starten (Datenintegrität prüfen)
btrfs scrub start /mnt/data

# Status
btrfs scrub status /mnt/data

# Im Hintergrund
btrfs scrub start -B /mnt/data

Automatischer Scrub

# /etc/cron.d/btrfs-scrub
0 3 * * 0 root /sbin/btrfs scrub start -B /mnt/data

Kompression

Kompression aktivieren

# Bei Mount
mount -o compress=zstd /dev/sdb /mnt/data

# Für einzelnes Verzeichnis
btrfs property set /mnt/data/logs compression zstd

# Kompression prüfen
btrfs property get /mnt/data compression

Kompressionsalgorithmen

| Algorithmus | Geschwindigkeit | Ratio | |-------------|----------------|-------| | lzo | Sehr schnell | Niedrig | | zstd | Schnell | Gut | | zstd:3 | Mittel | Besser | | zstd:9 | Langsam | Hoch | | zlib | Langsam | Gut |

Bestehende Daten komprimieren

# Defragmentieren mit Kompression
btrfs filesystem defragment -r -czstd /mnt/data

Quotas

Quotas aktivieren

btrfs quota enable /mnt/data

Quota setzen

# Gruppe (qgroup) für Subvolume
btrfs qgroup limit 100G /mnt/data/subvol1

# Status
btrfs qgroup show /mnt/data

Send/Receive

Snapshot übertragen

# Lokal
btrfs send /mnt/data/snapshots/snap1 | btrfs receive /mnt/backup/

# Remote
btrfs send /mnt/data/snapshots/snap1 | ssh remote btrfs receive /mnt/backup/

# Inkrementell
btrfs send -p /mnt/data/snapshots/snap1 /mnt/data/snapshots/snap2 | \
    btrfs receive /mnt/backup/

Backup-Skript

#!/bin/bash
# /usr/local/bin/btrfs-backup.sh

SRC="/mnt/data"
DEST="backup-server:/mnt/backup"
SNAP_DIR="$SRC/.snapshots"

# Neuer Snapshot
TIMESTAMP=$(date +%Y-%m-%d_%H-%M)
btrfs subvolume snapshot -r "$SRC" "$SNAP_DIR/$TIMESTAMP"

# Letzten Snapshot finden
LAST=$(ls -1 "$SNAP_DIR" | sort | tail -2 | head -1)

if [ "$LAST" != "$TIMESTAMP" ]; then
    # Inkrementell senden
    btrfs send -p "$SNAP_DIR/$LAST" "$SNAP_DIR/$TIMESTAMP" | \
        ssh backup-server btrfs receive /mnt/backup/
else
    # Initial senden
    btrfs send "$SNAP_DIR/$TIMESTAMP" | \
        ssh backup-server btrfs receive /mnt/backup/
fi

# Alte Snapshots aufräumen (behalte 7)
ls -1 "$SNAP_DIR" | head -n -7 | while read snap; do
    btrfs subvolume delete "$SNAP_DIR/$snap"
done

Dateisystem-Info

Status anzeigen

# Dateisystem-Info
btrfs filesystem show
btrfs filesystem show /mnt/data

# Speichernutzung
btrfs filesystem df /mnt/data
btrfs filesystem usage /mnt/data

# Geräte-Statistiken
btrfs device stats /mnt/data

Defragmentierung

# Einzelne Datei
btrfs filesystem defragment /mnt/data/large-file

# Rekursiv
btrfs filesystem defragment -r /mnt/data

Troubleshooting

Dateisystem reparieren

# Prüfen (unmounted)
btrfs check /dev/sdb

# Reparatur (Vorsicht!)
btrfs check --repair /dev/sdb

# Recovery-Mount
mount -o ro,recovery /dev/sdb /mnt

Häufige Probleme

# "No space left" trotz freiem Speicher
# → Balance ausführen
btrfs balance start -dusage=0 /mnt/data

# Metadaten voll
btrfs balance start -musage=0 /mnt/data

# Dateisystem-Fehler nach Crash
btrfs rescue zero-log /dev/sdb

Zusammenfassung

| Befehl | Funktion | |--------|----------| | mkfs.btrfs | Dateisystem erstellen | | btrfs subvolume create | Subvolume erstellen | | btrfs subvolume snapshot | Snapshot erstellen | | btrfs send/receive | Replikation | | btrfs scrub | Datenintegrität prüfen | | btrfs balance | Daten neu verteilen | | btrfs device add | Disk hinzufügen |

| Mount-Option | Funktion | |--------------|----------| | compress=zstd | Kompression | | noatime | Performance | | ssd | SSD-Optimierung | | subvol=@ | Subvolume wählen |

| Tool | Funktion | |------|----------| | snapper | Snapshot-Management | | btrfs-progs | Verwaltungstools | | compsize | Kompressionsstatistik |

Fazit

Btrfs ist ein leistungsfähiges Dateisystem für moderne Linux-Server. Subvolumes und Snapshots ermöglichen flexible Datenverwaltung. Die integrierte Kompression spart Speicherplatz. Copy-on-Write und Checksums schützen vor Datenkorruption. Für produktive Umgebungen empfiehlt sich RAID1 für wichtige Daten. Mit Snapper lässt sich das Snapshot-Management automatisieren.