1

I had a 500GB disk with BTRFS on it in my NAS. I recently got a 4TB disk and I wanted to use just the new disk, without any downtime. So I ran btrfs replace start 1 /dev/sdb ., waited for it to finish and unplugged the 500GB drive.

Now as I lost access to the 4TB disk, I want to recover all the data off the replaced 500GB disk, but the BTRFS partition on it is no longer recognized as such. I tried to mount it normally, with -o degraded, -o recovery,ro, I even tried btrfs restore and btrfs check, all of which failed. What are my options here?

1 Answer 1

3

This is a past question, but I encountered the same situation and will answer.
I ran btrfs replace for RAID1 filesystem on debian bullseye(5.10.0-13-amd64) and recovered on Manjaro(5.13.19-2) using the following procedure.

Cause

At least for kernel version 5.10 the btrfs replace command eventually calling btrfs_scratch_superblocks function at the this location: GitHub
This function remove "magic string" on the superblocks of the original disk.

I actually got the following results in my environment:

# btrfs inspect-internal dump-super -F /dev/sdb1
superblock: bytenr=65536, device=/dev/sdb1
---------------------------------------------------------
csum_type       0 (crc32c)
csum_size       4
csum            0x7d4435b9 [DON'T MATCH]
bytenr          65536
flags           0x1
            ( WRITTEN )
magic           ........ [DON'T MATCH]

According to the btrfs Wiki, "magic" (= "magic string") must be _BHRfS_M.

Recovery procedure

Referring to What if I don't have wipefs at hand? on the btrfs Wiki, execute the following on a shell.
It is probably safer to run it on a different PC than the one to which the replaced disk is connected.

Please rewrite the device and mount point location for your environment.
(In my environment it succeeded as /dev/sdb1 !)

# # backup magic strings just in case
# dd bs=1 count=8 if=/dev/sdb skip=$((64*1024+64)) of=~/magic1
# dd bs=1 count=8 if=/dev/sdb skip=$((64*1024*1024+64)) of=~/magic2
# dd bs=1 count=8 if=/dev/sdb skip=$((256*1024*1024*1024+64)) of=~/magic3

# # write magic string
# echo "_BHRfS_M" | dd bs=1 count=8 of=/dev/sdb seek=$((64*1024+64))
# echo "_BHRfS_M" | dd bs=1 count=8 of=/dev/sdb seek=$((64*1024*1024+64))
# echo "_BHRfS_M" | dd bs=1 count=8 of=/dev/sdb seek=$((256*1024*1024*1024+64))

# btrfs device scan
# mkdir -p /mnt/before_replace
# mount -o degraded,ro /dev/sdb /mnt/before_replace

I'm not good at English, so sorry if it's hard to read. But I hope this helps others in the same situation.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .