aboutsummaryrefslogtreecommitdiff
path: root/docs/research-btrfs-expansion.org
blob: 8a6c1a6cb7e6105d0d1d6496f4d010a7dee22603 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#+TITLE: Research: Expanding Project to Support Btrfs
#+DATE: 2026-01-23
#+AUTHOR: Craig Jennings & Claude

* Executive Summary

This document covers research and architecture planning for expanding the archzfs
project to support both ZFS and Btrfs filesystems, providing equivalent functionality
for both.

* Project Renaming

Since "archzfs" implies ZFS-only, the project needs a new name.

** Naming Options

| Name | Pros | Cons |
|------+------+------|
| archsnap | Short, implies snapshots | Doesn't mention filesystems |
| archfs | Generic filesystem installer | Might be too generic |
| archroot | Implies root filesystem setup | Already used by other tools |
| arch-snap-install | Descriptive | Long |
| snaparch | Short, memorable | Sounds like "snap" package manager |
| archraid | Implies multi-disk | Doesn't mention snapshots |
| arch-resilient | Implies reliability | Long, vague |

** Recommendation
*archsnap* - Short, memorable, emphasizes the snapshot capability that's the core
value proposition of both ZFS and Btrfs configurations.

* Btrfs Equivalent Features

** Feature Parity Matrix

| Feature (ZFS) | Btrfs Equivalent | Tool/Method |
|---------------+------------------+-------------|
| ZFS native encryption | LUKS2 + dm-crypt | cryptsetup |
| zpool mirror | btrfs raid1 | mkfs.btrfs -d raid1 -m raid1 |
| zpool stripe | btrfs raid0 | mkfs.btrfs -d raid0 |
| zpool raidz1 | btrfs raid5 | NOT RECOMMENDED (unstable) |
| zpool raidz2 | btrfs raid6 | NOT RECOMMENDED (unstable) |
| ZFS datasets | Btrfs subvolumes | btrfs subvolume create |
| zfs snapshot | btrfs snapshot | btrfs subvolume snapshot |
| ZFSBootMenu | GRUB + grub-btrfs | grub-btrfs package |
| sanoid/syncoid | btrbk or snapper | snapper + snap-pac |
| zfs rollback | snapper rollback | snapper undochange |
| Pre-pacman snapshots | snap-pac | snap-pac + snapper |

** Important Btrfs Limitations

*** RAID 5/6 is Unstable
#+BEGIN_QUOTE
"Parity RAID (RAID 5/6) code has multiple serious data-loss bugs in it."
-- Btrfs Wiki
#+END_QUOTE

For multi-disk Btrfs, only offer:
- *raid1* (mirror) - Stable, recommended
- *raid10* (striped mirrors) - Requires 4+ disks
- *raid0* (stripe) - No redundancy, not recommended for root

*** No Native Encryption
Btrfs doesn't have native encryption. Use LUKS2 as layer beneath:
#+BEGIN_SRC bash
cryptsetup luksFormat /dev/sdX
cryptsetup open /dev/sdX cryptroot
mkfs.btrfs /dev/mapper/cryptroot
#+END_SRC

*** Bootloader Considerations
- *GRUB* is required for booting into snapshots (via grub-btrfs)
- *systemd-boot* can't boot from btrfs snapshots (kernel on FAT32 ESP)
- ZFSBootMenu equivalent: GRUB + grub-btrfs + grub-btrfsd daemon

* Recommended Btrfs Subvolume Layout

Based on research from multiple sources, this layout provides optimal snapshot
management and rollback capability:

#+BEGIN_SRC
btrfs volume (LABEL=archsnap)
│
├── @ (subvol) → mounted at /
├── @home (subvol) → mounted at /home
├── @snapshots (subvol) → mounted at /.snapshots
├── @var_log (subvol) → mounted at /var/log
├── @var_cache (subvol) → mounted at /var/cache
└── @var_tmp (subvol) → mounted at /var/tmp
#+END_SRC

** Why This Layout?

1. *@ for root* - Main system, snapshotted for rollbacks
2. *@home separate* - User data not affected by system rollbacks
3. *@snapshots separate* - Snapper requirement for snapshot storage
4. *@var_log separate* - Logs persist across rollbacks, required for read-only snapshots
5. *@var_cache separate* - Package cache excluded from snapshots (saves space)
6. *@var_tmp separate* - Temp files excluded from snapshots

** Mount Options

#+BEGIN_SRC bash
# Recommended mount options
BTRFS_OPTS="noatime,compress=zstd,space_cache=v2,discard=async"

# Example fstab entries (NO subvolid - allows rollback)
UUID=xxx  /           btrfs  $BTRFS_OPTS,subvol=@           0 0
UUID=xxx  /home       btrfs  $BTRFS_OPTS,subvol=@home       0 0
UUID=xxx  /.snapshots btrfs  $BTRFS_OPTS,subvol=@snapshots  0 0
UUID=xxx  /var/log    btrfs  $BTRFS_OPTS,subvol=@var_log    0 0
UUID=xxx  /var/cache  btrfs  $BTRFS_OPTS,subvol=@var_cache  0 0
UUID=xxx  /var/tmp    btrfs  $BTRFS_OPTS,subvol=@var_tmp    0 0
#+END_SRC

*CRITICAL*: Do NOT use subvolid= in fstab - it breaks rollbacks!

* Snapshot Management Stack

** Recommended: Snapper + snap-pac + grub-btrfs + btrfs-assistant

| Component | Purpose | Package |
|-----------+---------+---------|
| snapper | Snapshot creation, retention, rollback | snapper |
| snap-pac | Automatic pre/post pacman snapshots | snap-pac |
| grub-btrfs | Bootable snapshot menu in GRUB | grub-btrfs |
| grub-btrfsd | Auto-update GRUB on snapshot changes | grub-btrfs (service) |
| btrfs-assistant | GUI for snapshot management | btrfs-assistant (AUR) |
| snapper-gui | Alternative lightweight GUI | snapper-gui (AUR) |

** Alternative: btrbk

btrbk is simpler than snapper and handles both snapshots and remote backups:
#+BEGIN_SRC conf
# /etc/btrbk.conf
snapshot_preserve_min   2d
snapshot_preserve       24h 7d 4w 12m

volume /
  snapshot_dir /.snapshots
  subvolume @
#+END_SRC

** Retention Policy (Snapper)

Default snapper timeline for root:
#+BEGIN_SRC conf
# /etc/snapper/configs/root
TIMELINE_CREATE="yes"
TIMELINE_CLEANUP="yes"
TIMELINE_MIN_AGE="1800"
TIMELINE_LIMIT_HOURLY="5"
TIMELINE_LIMIT_DAILY="7"
TIMELINE_LIMIT_WEEKLY="0"
TIMELINE_LIMIT_MONTHLY="0"
TIMELINE_LIMIT_YEARLY="0"
#+END_SRC

Keeps: 5 hourly + 7 daily = ~12 snapshots at any time.

* Multi-Disk Btrfs Setup

** Mirror (2+ disks) - RECOMMENDED

#+BEGIN_SRC bash
# Create mirrored filesystem
mkfs.btrfs -L archsnap -d raid1 -m raid1 /dev/sda2 /dev/sdb2

# Mount using ANY device (btrfs auto-discovers others)
mount /dev/sda2 /mnt

# Create subvolumes
btrfs subvolume create /mnt/@
btrfs subvolume create /mnt/@home
# ... etc
#+END_SRC

** Stripe (2+ disks) - NOT RECOMMENDED for root

#+BEGIN_SRC bash
# Striped data, mirrored metadata (default)
mkfs.btrfs -L archsnap -d raid0 -m raid1 /dev/sda2 /dev/sdb2
#+END_SRC

Metadata is mirrored by default even in raid0 mode for safety.

** Converting Single to Mirror

#+BEGIN_SRC bash
# Add second device to existing filesystem
mount /dev/sda2 /mnt
btrfs device add /dev/sdb2 /mnt
btrfs balance start -dconvert=raid1 -mconvert=raid1 /mnt
#+END_SRC

* Reference Repositories Cloned

Located in =reference-repos/=:

| Repository | Purpose |
|------------+---------|
| grub-btrfs | GRUB snapshot menu integration |
| easy-arch | Automated Arch+Btrfs installer |
| arch-btrfs-installation | Manual install guide |
| btrfs-assistant | GUI snapshot manager |
| snap-pac | Pacman hook for snapper |
| btrbk | Alternative snapshot/backup tool |
| buttermanager | GUI btrfs management |
| alis | Arch Linux Install Script (multi-fs) |

* Installer Architecture Changes

** Current Structure (ZFS-only)

#+BEGIN_SRC
install-archzfs
├── Phase 1: Configuration gathering (fzf prompts)
├── Phase 2: Disk partitioning (EFI + ZFS)
├── Phase 3: ZFS pool/dataset creation
├── Phase 4: Pacstrap base system
├── Phase 5: System configuration
├── Phase 6: ZFSBootMenu installation
├── Phase 7: Genesis snapshot
└── Phase 8: Cleanup
#+END_SRC

** Proposed Structure (Multi-filesystem)

#+BEGIN_SRC
install-archsnap
├── Phase 1: Configuration gathering
│   ├── Hostname, timezone, locale, keymap
│   ├── Disk selection (multi-select)
│   ├── RAID level (if multi-disk)
│   ├── NEW: Filesystem selection (ZFS / Btrfs)
│   ├── Encryption passphrase
│   ├── Root password
│   ├── NEW: Initial user password
│   └── SSH configuration
│
├── Phase 2: Disk partitioning
│   ├── EFI partition (same for both)
│   └── Root partition (type depends on FS choice)
│
├── Phase 3: Filesystem creation
│   ├── IF ZFS: create pool + datasets
│   └── IF Btrfs: create volume + subvolumes
│
├── Phase 4: Pacstrap base system
│   ├── Common packages
│   ├── IF ZFS: zfs-dkms, zfs-utils
│   └── IF Btrfs: btrfs-progs, snapper, snap-pac, grub-btrfs
│
├── Phase 5: System configuration
│   ├── Common config (locale, timezone, hostname)
│   ├── IF ZFS: ZFS-specific configs
│   └── IF Btrfs: snapper configs, grub-btrfs configs
│
├── Phase 6: Bootloader installation
│   ├── IF ZFS: ZFSBootMenu
│   └── IF Btrfs: GRUB + grub-btrfs
│
├── Phase 7: Genesis snapshot
│   ├── IF ZFS: zfs snapshot -r zroot@genesis
│   └── IF Btrfs: snapper create -c root -d "genesis"
│
└── Phase 8: Cleanup
#+END_SRC

** Code Organization

#+BEGIN_SRC bash
install-archsnap
├── lib/
│   ├── common.sh      # Shared functions (colors, prompts, fzf)
│   ├── disk.sh        # Partitioning (shared)
│   ├── zfs.sh         # ZFS-specific functions
│   ├── btrfs.sh       # Btrfs-specific functions
│   └── config.sh      # Config file handling
├── configs/
│   ├── snapper-root.conf
│   └── btrbk.conf.example
└── install-archsnap   # Main script
#+END_SRC

* Testing Strategy

** VM Test Matrix

- Single ZFS (1 disk): Install, boot, reboot, rollback+reboot
- Single Btrfs (1 disk): Install, boot, reboot, rollback+reboot
- Mirror ZFS (2 disk): Install, fail disk, recover
- Mirror Btrfs (2 disk, raid1): Install, fail disk, recover
- RAIDZ1 ZFS (3 disk): Install, fail disk, recover

** Validation Checks

*** Fresh Install (all configs)
- Partitions created correctly (EFI + root)
- Filesystem created (pool/subvols)
- All mount points accessible
- Packages installed (pacman -Q zfs-dkms or btrfs-progs)
- Services enabled (zfs.target or snapper-timeline.timer)
- Bootloader installed (ZFSBootMenu or GRUB + grub-btrfs)
- fstab correct (no subvolid for btrfs)
- Can boot without ISO

*** Reboot Survival
Critical: Verify system survives reboot cleanly. Catches:
- Misconfigured services slamming CPU/network
- Services that fail to start properly
- ZFS import issues
- fstab mount failures

Checks after reboot:
- System reaches login prompt
- All filesystems mounted (findmnt)
- No failed services (systemctl --failed)
- CPU/memory/network normal (no runaway processes)
- Can SSH in (if network configured)

*** Snapshot Operations
- Manual snapshot creates successfully
- Snapshot appears in list (zfs list -t snap / snapper list)
- Pre-pacman snapshot created automatically (install package, verify)
- Snapshot visible in boot menu (ZFSBootMenu / GRUB)
- Can boot into snapshot

*** Rollback + Reboot
Important: Rollback MUST include reboot to validate fully.
Do NOT use ZFSBootMenu's built-in rollback (known bug, needs filing).
Use zfsrollback script or snapper rollback instead.

Checks:
- Rollback command completes without error
- Reboot completes successfully
- System state matches snapshot after reboot
- Previously installed test package is gone
- No orphaned/broken state

*** Failure Recovery (multi-disk only)
- Pool/volume shows correct redundancy (zpool status / btrfs fi show)
- Survives single disk removal (zpool offline / btrfs device delete)
- Boots with degraded array
- Warnings displayed about degraded state
- Can resilver/rebuild after disk replaced (zpool replace / btrfs replace)
- Pool returns to healthy state

*** Encryption
- Passphrase prompt appears at boot
- Correct unlock with right passphrase
- Fails gracefully with wrong passphrase (retry prompt, not panic)
- Data unreadable when mounted elsewhere without key
- Pool/volume not auto-imported without passphrase

** Known Issues

*** ZFSBootMenu Rollback Bug
ZFSBootMenu's built-in rollback feature has issues (to be filed).
Workaround: Use zfsrollback script from installed system or live ISO.

** Test Scripts

#+BEGIN_SRC
scripts/
├── test-zfs-single.sh
├── test-zfs-mirror.sh
├── test-zfs-raidz.sh
├── test-btrfs-single.sh
├── test-btrfs-mirror.sh
└── test-configs/
    ├── zfs-single.conf
    ├── zfs-mirror.conf
    ├── btrfs-single.conf
    └── btrfs-mirror.conf
#+END_SRC

* CLI and GUI Tools

** CLI Tools to Install (Both Filesystems)

| Tool | Purpose |
|------+---------|
| archsnap-snapshot | Create manual snapshot (wrapper) |
| archsnap-rollback | Interactive rollback (fzf-based) |
| archsnap-list | List snapshots |
| archsnap-prune | Manual pruning |

These would be thin wrappers that detect the filesystem and call the appropriate
underlying tool (zfs/snapper/btrbk).

** GUI Options

*** For Btrfs
- *btrfs-assistant* (recommended) - Full-featured, actively maintained
- *snapper-gui* - Lighter weight alternative

*** For ZFS
- No equivalent mature GUI exists
- Could build a simple zenity/yad wrapper

* Implementation Phases

** Phase 1: Refactor Current Installer
1. Extract common functions to lib/common.sh
2. Extract ZFS-specific code to lib/zfs.sh
3. Add filesystem selection prompt
4. Rename project to archsnap

** Phase 2: Implement Btrfs Support
1. Create lib/btrfs.sh with btrfs-specific functions
2. Implement subvolume creation
3. Implement snapper configuration
4. Implement GRUB + grub-btrfs installation
5. Create btrfs genesis snapshot

** Phase 3: Testing Infrastructure
1. Create test configs for all scenarios
2. Update test-vm.sh for btrfs testing
3. Create automated test suite
4. Test multi-disk configurations

** Phase 4: CLI Tools
1. Create archsnap-snapshot wrapper
2. Create archsnap-rollback wrapper
3. Update documentation

** Phase 5: Documentation & Polish
1. Update README for dual-filesystem support
2. Create BTRFS-specific documentation
3. Update troubleshooting guides

* Sources

** Btrfs Installation
- [[https://wiki.archlinux.org/title/Btrfs][Arch Wiki - Btrfs]]
- [[https://github.com/egara/arch-btrfs-installation][arch-btrfs-installation]]
- [[https://gist.github.com/mjkstra/96ce7a5689d753e7a6bdd92cdc169bae][Modern Arch+Btrfs Guide]]

** Snapshot Management
- [[https://github.com/Antynea/grub-btrfs][grub-btrfs]]
- [[https://wiki.archlinux.org/title/Snapper][Arch Wiki - Snapper]]
- [[https://github.com/wesbarnett/snap-pac][snap-pac]]
- [[https://github.com/digint/btrbk][btrbk]]

** GUI Tools
- [[https://gitlab.com/btrfs-assistant/btrfs-assistant][btrfs-assistant]]
- [[https://github.com/egara/buttermanager][buttermanager]]

** Multi-Disk
- [[https://archive.kernel.org/oldwiki/btrfs.wiki.kernel.org/index.php/Using_Btrfs_with_Multiple_Devices.html][Btrfs Wiki - Multiple Devices]]
- [[https://thelinuxcode.com/set-up-btrfs-raid/][How to Set Up Btrfs RAID]]