====== btrfs ======
Status: 
  * https://btrfs.readthedocs.io/en/stable/Status.html
  * https://silvenga.com/posts/btrfs-and-lessons-learned/
Paket: 
  * Debian: ''btrfs-progs btrfsmaintenance''
===== Dateisystem anlegen =====
ext? nach btrfs konvertieren:
  man btrfs-convert
oder besser:
Dateisystem anlegen und mounten:
  mkfs.btrfs /dev/sda2
  btrfs filesystem show
  mount -m -o noatime /dev/sda2 /mnt/btrfs
(( TODO: [[https://wiki.tnonline.net/w/Blog/The_case_for_(no)_atime_on_Linux|noatime]] nachvollziehen
))
  cd /mnt/btrfs
Beispieldaten hinein kopieren:
  time cp -a /usr/share/ .
===== Reflink / CoW =====
Schnelle Kopien dank reflink/CoW:
  time cp -a share/ share2
===== Daten komprimieren =====
Daten komprimieren: (( Data loss on rotated journal files on BTRFS volumes using compression: https://github.com/systemd/systemd/issues/9112 )) ((Mehr Datenverbrauch durch Defragmentation: [[https://btrfs.readthedocs.io/en/latest/Defragmentation.html|Defragmentation does not preserve extent sharing, e.g. files created by cp --reflink or existing on multiple snapshots. Due to that the data space consumption may increase.]] ))
  btrfs filesystem df -h .
-> ''Data, single: total=2.01GiB, used=1.53GiB''
  btrfs filesystem defragment -czstd -r share/
  btrfs filesystem df -h .
-> ''Data, single: total=3.01GiB, used=740.17MiB''
  btrfs filesystem usage .
===== Subvolumes =====
Informationen über das top-level Subvolume anzeigen:
  btrfs subvolume show .
-> ''Subvolume ID'': 5
  btrfs subvolume list .
-> noch keine Subvolumes
Subvolume anlegen und Daten hinein verschieben:
  btrfs subvolume create subvol1
  btrfs subvolume list .
  btrfs subvolume show subvol1
-> ''Subvolume ID'': 256
-> ''Parent ID'': 5
  time mv ./share/ ./subvol1/
  
Subvolume umbenennen:
  mv subvol1/ subvol2
Subvolume (erneut) mounten:
  mkdir /mnt/subvol2
  mount -o subvol=/subvol2 /dev/sda2 /mnt/subvol2  
===== Snapshots =====
Snapshot erstellen:
  time btrfs subvolume snapshot subvol2 snap1
  btrfs subvolume list .
  btrfs subvolume show snap1
-> Subvolume ID: 257
-> Parent ID: 5
Read-only Snapshot erstellen:
  btrfs subvolume snapshot -r subvol2 snap_ro1
  btrfs filesystem usage .
  btrfs filesystem du -s --human-readable .
  du -sh .
  du -shx .
  btrfs filesystem du -s --human-readable subvol2
  du -sh subvol2
  btrfs filesystem du -s --human-readable snap1
===== Backup mit send und receive =====
==== Initiales Vollbackup ====
Read-only Snapshot erstellen:
  btrfs subvolume snapshot -r subvol2 snap_ro2
Backup übertragen:
  btrfs send snap_ro2 | ssh 192.168.122.74 btrfs receive /mnt/btrfs
((
Mit ''pv'' Datenmenge anzeigen:
  btrfs send -p snap_ro2 | pv | ssh 192.168.122.74 btrfs receive /mnt/btrfs
))
==== Inkrement ====
Daten ändern:
  touch subvol2/status
  date -r subvol2/status
Read-only Snapshot erstellen:
  btrfs subvolume snapshot -r subvol2 snap_ro3
Backup übertragen:
  btrfs send -p snap_ro2 snap_ro3 | ssh 192.168.122.74 btrfs receive /mnt/btrfs
Testen, ob Daten angekommen sind:
  ssh 192.168.122.74 date -r /mnt/btrfs/snap_ro3/status
===== Subvolume zum default-Volume machen =====
  cd /mnt/btrfs
Snapshot des root-Volumes erstellen:
  btrfs subvolume snapshot . 2025011300
  btrfs subvolume show 2025011300
-> ''Subvolume ID'' kopieren, z.B. 258
  btrfs subvolume set-default 256 2025011300/
Daten (nicht Subvolumes!) aus root-Volume löschen:
  rm -rf --one-file-system /mnt/btrfs
testen:
  ls
  cd ..
  umount btrfs
  mount btrfs
  ls btrfs
===== Top-Subvolume mounten =====
  mkdir /mnt/btrfs
  mount -o subvolid=5,subvol=/ /dev/sda2 /mnt/btrfs
UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /mnt/btrfs btrfs noauto,subvolid=5,subvol=/ 0 0
===== Datenträger tauschen =====
==== langsam ====
  btrfs device usage .
  btrfs device add /dev/sda6 .
  btrfs device usage .
  btrfs device del /dev/sda2 .
  watch -d btrfs filesystem usage -T .
==== schneller & fehlertoleranter ====
  # btrfs replace start   
  btrfs   replace start 1    /dev/sda6  .
  btrfs replace status .
== Doku ==
  * https://wiki.tnonline.net/w/Btrfs/Replacing_a_disk
  * [[https://btrfs.readthedocs.io/en/latest/btrfs-replace.html|man btrfs-replace]]
===== Dateisystem vergrößern =====
Datenträger/Volume auf dem das BTRFS liegt vergrößern, dann:
  btrfs device usage .
  btrfs filesystem resize max .
  btrfs device usage .
===== zum RAID1 erweitern =====
  btrfs device add /dev/vde .
  btrfs -v balance start -mconvert=raid1,soft -dconvert=raid1,soft .
  btrfs device usage .
====== BTRFS bei SuSE ======
++++ SuSE default root layout |
SuSE default:
  mkfs.btrfs ${BOOT_DEVICE}2
  uuid=$(lsblk -n -o uuid ${BOOT_DEVICE}2)
  mkdir $target
  mount UUID="$uuid" $target || exit
  btrfs subvolume create ${target%/}/@
  btrfs subvolume create ${target%/}/@/.snapshots
  mkdir -p               ${target%/}/@/.snapshots/1
  btrfs subvolume create ${target%/}/@/.snapshots/1/snapshot
  mkdir -p               ${target%/}/@/boot/grub2
  btrfs subvolume create ${target%/}/@/boot/grub2/i386-pc
  btrfs subvolume create ${target%/}/@/boot/grub2/x86_64-efi
  btrfs subvolume create ${target%/}/@/opt
  btrfs subvolume create ${target%/}/@/srv
  btrfs subvolume create ${target%/}/@/tmp
  mkdir -p               ${target%/}/@/usr
  btrfs subvolume create ${target%/}/@/usr/local
  mkdir -p               ${target%/}/@/var
  btrfs subvolume create ${target%/}/@/var/crash
  mkdir -p               ${target%/}/@/var/lib/libvirt
  btrfs subvolume create ${target%/}/@/var/lib/libvirt/images
  btrfs subvolume create ${target%/}/@/var/lib/mailman
  btrfs subvolume create ${target%/}/@/var/lib/mariadb
  btrfs subvolume create ${target%/}/@/var/lib/mysql
  btrfs subvolume create ${target%/}/@/var/lib/named
  btrfs subvolume create ${target%/}/@/var/lib/pgsql
  btrfs subvolume create ${target%/}/@/var/log
  btrfs subvolume create ${target%/}/@/var/opt
  btrfs subvolume create ${target%/}/@/var/spool
  btrfs subvolume create ${target%/}/@/var/tmp
  subvolid=$(btrfs subvolume show $target/@/.snapshots/1/snapshot | sed -rn 's/.*Object ID:\s+//p')
  btrfs subvolume set-default "$subvolid" "$target"
++++