2

I have a Linux raid (those md device), it only contains one drive, and it has a btrfs patition on it.

I accidently do the following thing:

fdisk /dev/md126

I didn't see the warning

The old btrfs signature will be removed by a write command.

Device does not contain a recognized partition table. Created a new
DOS disklabel with disk identifier 0x3cf67d6f

and type w to exit.

Now the partition table is gone.

I try to use testdisk /dev/md126 , and it pick up my partition, but

Disk /dev/md126 - 1993 GB / 1856 GiB - CHS 486640640 2 4                                                                                                                                                                                       

The harddisk (1993 GB / 1856 GiB) seems too small! (< 1993 GB / 1856 GiB)                                                                                                                                                                      
Check the harddisk size: HD jumpers settings, BIOS detection...                                                                                                                                                                                

The following partition can't be recovered:                                                                                                                                                                                                    
     Partition               Start        End    Size in sectors                                                                                                                                                                               
>  Linux                16368   0  1 486657007   1  4 3893125120 [2018.10.18-11:37:13 v15254]                                                                                                                                                  










[ Continue ]                                                                                                                                                                                                                                   
btrfs blocksize=4096, 1993 GB / 1856 GiB

It doesn't seem to let me keep the partition.

And parted rescue doesn't show the partition at all

What should I do next?


This is the fdisk -l output before I break the partition table:

Disk /dev/sdb: 1.8 TiB, 2000365380096 bytes, 3906963633 sectors
Disk model: EARX-00PASB0    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xcca96a8e

Device     Boot   Start        End    Sectors  Size Id Type
/dev/sdb1          2048    4982527    4980480  2.4G fd Linux raid autodetect
/dev/sdb2       4982528    9176831    4194304    2G fd Linux raid autodetect
/dev/sdb3       9437184 3902564351 3893127168  1.8T fd Linux raid autodetect

Disk /dev/md126: 1.8 TiB, 19933000280061440 bytes, 3893125120 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

After:

Disk /dev/sdb: 1.8 TiB, 2000365380096 bytes, 3906963633 sectors
Disk model: EARX-00PASB0    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xcca96a8e

Device     Boot   Start        End    Sectors  Size Id Type
/dev/sdb1          2048    4982527    4980480  2.4G fd Linux raid autodetect
/dev/sdb2       4982528    9176831    4194304    2G fd Linux raid autodetect
/dev/sdb3       9437184 3902564351 3893127168  1.8T fd Linux raid autodetect


Disk /dev/md126: 1.8 TiB, 1993280061440 bytes, 3893125120 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x3cf67d6f

Here is the parted print output of current situation:

Model: Linux Software RAID Array (md)
Disk /dev/md126: 1993GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start  End  Size  Type  File system  Flags

This is the output of another drive with the similar config:

Model: Linux Software RAID Array (md)
Disk /dev/md123: 1995GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: 

Number  Start  End     Size    File system  Flags
 1      0.00B  1995GB  1995GB  btrfs
1
  • You've not broken the partition table - you've broken the BTFS filesystem on /dev/md126. Using fdisk and parted is not relevant here. (Unfortunately I know nothing about BTFS so I can't guide you further.) Commented Jun 6, 2020 at 14:33

1 Answer 1

1

You don't have a partition table on that device. So trying to restore partitions (when you have none) is bound to fail. Writing a partition table onto a device that holds a filesystem, likely damages that filesystem.

Trying to re-create your situation:

# file -s /dev/md100
/dev/md100: BTRFS Filesystem sectorsize 4096, nodesize 16384, leafsize 16384, UUID=fbae0a54-9d6b-4d20-9981-a1a385a0a91f, 120848384/205520896 bytes used, 1 devices
# fdisk /dev/md100
# file -s /dev/md100
/dev/md100: DOS/MBR boot sector

The steps below worked for me to recover.

Use wipefs to remove the msdos partition header that you wrote:

# wipefs --all --types dos /dev/md100
/dev/md100: 2 bytes were erased at offset 0x000001fe (dos): 55 aa
/dev/md100: calling ioctl to re-read partition table: Success
# file -s /dev/md100
/dev/md100: data

Use btrfs-select-super to restore backup superblock:

Note: I ended up using btrfs-select-super since btrfs check for some reason was unable / unwilling to fix this issue. Please read the man page first before proceeding.

# btrfs-select-super -s 1 /dev/md100
using SB copy 1, bytenr 67108864
# file -s /dev/md100
/dev/md100: BTRFS Filesystem sectorsize 4096, nodesize 16384, leafsize 16384, UUID=fbae0a54-9d6b-4d20-9981-a1a385a0a91f, 120848384/205520896 bytes used, 1 devices

Afterwards the filesystem was mountable with intact files.

However I can't guarantee the same will work for you. If possible you should not run this experiment on the drive directly but use a copy on write overlay.

1
  • 1
    Thanks, this did help me recover the partition. The thing that I understand wrong is that I think the /dev/md* device is a drive, but actually it is a partition. You and the comment by roaima really help me realize that. If someone need to fix this, use btrfs inspect-internal dump-super -s 1 /dev/md0 or -s 2 to check if the 1st or 2nd backups of the superblock still exist. If it all shows like ERROR: bad magic on superblock on, btrfs-select-super won't work. Commented Jun 6, 2020 at 16:23

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.