Skip to content

Instantly share code, notes, and snippets.

@obadz
Last active March 29, 2020 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obadz/931d62ebf19f05f44363d2bd6f18e958 to your computer and use it in GitHub Desktop.
Save obadz/931d62ebf19f05f44363d2bd6f18e958 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -x
set -e
# Setup a RAID1 fs on 2 files
truncate -s 200M 1.img
truncate -s 200M 2.img
dev1=$(sudo losetup --find --show 1.img)
dev2=$(sudo losetup --find --show 2.img)
sudo mkfs.btrfs -f -M -L btrfstest -d raid1 -m raid1 $dev1 $dev2
# Create a few files
mkdir -p mnt
sudo mount $dev1 mnt
sudo btrfs filesystem df mnt
for i in `seq 3`
do
echo $i | sudo tee -a mnt/$i.txt
done
sync
sudo btrfs filesystem df mnt
sudo umount mnt
# Detach one device, mount degraded, create one more file
sudo losetup --detach $dev1
true "The missing loopback device $dev1 is not there"
sudo losetup --list
sudo losetup --find
sudo lsblk
sudo mount -o degraded $dev2 mnt
true "At this point, all blocks still say RAID1"
sudo btrfs filesystem df mnt
true "Scrubing is not a problem even in degraded mode"
sudo btrfs scrub start -B -d mnt
true "Because of scrub, some blocks are no longer RAID1"
sudo btrfs filesystem df mnt # At this point, all blocks still say RAID1"
true "Does not show any hint of degradation/missing devices ☹:"
sudo btrfs filesystem show
true "All the files and their content are here!"
ls -l mnt
head mnt/*
echo DEGRADED | sudo tee -a mnt/degraded.txt
sync
true "Now we ve got more non-RAID blocks"
sudo btrfs filesystem df mnt # Now we can see there are some non-RAID1 blocks. We will need to balance once both disks are back in!
sudo umount mnt
# Reattach missing device, mount properly, balance
dev1=$(sudo losetup --find --show 1.img)
sudo mount $dev1 mnt
ls -l mnt
sudo btrfs filesystem show
true "We can clearly see that there are still non-RAID blocks"
sudo btrfs filesystem df mnt
sudo btrfs balance start mnt --full-balance
true "Yay, no more non-RAID blocks!"
sudo btrfs filesystem df mnt # Everything back to RAID1
sudo umount mnt
# Detach other device, attempt to mount, see what's there
sudo losetup --detach $dev2
true "Would crash if you had not run the balance in the previous step"
sudo mount -o degraded $dev1 mnt
true "degraded.txt is there! It got copied over during the balance!"
ls -l mnt
sudo umount mnt
sudo losetup --detach $dev1
rmdir mnt
@obadz
Copy link
Author

obadz commented Mar 28, 2020

Extract from output:

+ sudo mount -o degraded /dev/loop1 mnt
+ sudo btrfs filesystem show
Label: 'btrfstest'  uuid: 2e6903f5-a0c8-4a0c-9974-a076a8c8352d
	Total devices 2 FS bytes used 128.00KiB
	devid    1 size 200.00MiB used 104.00MiB path /dev/loop0
	devid    2 size 200.00MiB used 104.00MiB path /dev/loop1

+ ls -l mnt

@obadz
Copy link
Author

obadz commented Mar 29, 2020

+ sudo losetup --detach /dev/loop0
+ sudo mount -o degraded /dev/loop1 mnt
+ sudo btrfs scrub start -B -d mnt
WARNING: device 1 not present
scrub device /dev/loop0 (id 1) canceled
Scrub started:    Sun Mar 29 11:37:47 2020
Status:           aborted
Duration:         0:00:00
Total to scrub:   72.00MiB
Rate:             0.00B/s
Error summary:    no errors found
scrub device /dev/loop1 (id 2) done
Scrub started:    Sun Mar 29 11:37:47 2020
Status:           finished
Duration:         0:00:00
Total to scrub:   72.00MiB
Rate:             0.00B/s
Error summary:    no errors found
+ sudo btrfs filesystem show
Label: 'btrfstest'  uuid: f048b964-f95e-4383-8861-497233ac4721
	Total devices 2 FS bytes used 32.00KiB
	devid    1 size 200.00MiB used 72.00MiB path /dev/loop0
	devid    2 size 200.00MiB used 199.00MiB path /dev/loop1

+ sudo btrfs filesystem df mnt
System, RAID1: total=8.00MiB, used=4.00KiB
System, DUP: total=32.00MiB, used=0.00B
Data+Metadata, RAID1: total=64.00MiB, used=28.00KiB
Data+Metadata, DUP: total=15.50MiB, used=0.00B
Data+Metadata, single: total=32.00MiB, used=0.00B
GlobalReserve, single: total=832.00KiB, used=0.00B
+ ls -l mnt
total 12
-rw-r--r-- 1 root root 2 Mar 29 11:37 1.txt
-rw-r--r-- 1 root root 2 Mar 29 11:37 2.txt
-rw-r--r-- 1 root root 2 Mar 29 11:37 3.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment