How To's

How to replace a MBR drive in Linux (RAID)

1,982 views · 0 found this helpful

Was this helpful?

This guide is for drives under 2TB formatted as MBR (If you're running Ubuntu 20 or newer, please follow the guide at the very bottom)

In this example I have two hard drives, /dev/sda and /dev/sdb, with the partitions /dev/sda1 and /dev/sda2 as well as /dev/sdb1 and /dev/sdb2. In this example we are acting as if SDB has failed.


/dev/sda1 and /dev/sdb1 make up the RAID1 array /dev/md0

/dev/sda2 and /dev/sdb2 make up the RAID1 array /dev/md1

_________________________

1) How to tell if a hard disk has failed?

If a disk has failed, you will probably find a lot of error messages in the log files, e.g.

/var/log/messages or /var/log/syslog

You can also run:

cat /proc/mdstat

Instead of the string [UU] you will see [U_] if you have a degraded RAID1 array. 

You can also run:

fdisk -l | less

This checks what drive is missing from the array. An easy way to tell is look for a drive with no MD0 or MD1 partitions.


2) Removing the failed disk

To remove /dev/sdb, we will mark /dev/sdb1 and /dev/sdb2 as failed and remove them from their respective RAID arrays (/dev/md0 and /dev/md1).

First we mark /dev/sdb1 and /dev/sdb2 as failed:

mdadm --manage /dev/md0 --fail /dev/sdb1

mdadm --manage /dev/md1 --fail /dev/sdb2

Now we want to remove the drive from the array

mdadm --manage /dev/md0 --remove /dev/sdb1

mdadm --manage /dev/md1 --remove /dev/sdb2

Once you finish that check to see what drives serial # is with:

hdparm -I /dev/sdb | less

Remove the corresponding drive then shutdown the server and replace the drive. If your a client, please inform support the drive serial # and we can replace it for you.


3) Adding the new hard disk

Now we are going to use fdisk to copy over the partitions from the good drive SDA to the new drive SDB.

sfdisk -d /dev/sda | sfdisk /dev/sdb

If you get an error try:

sudo sfdisk –d /dev/sda | sudo sfdisk /dev/sdb


4) Adding and syncing

The final step is to add mdo to sdb1 and md1 to sdb2 which will initialize the syncing process

mdadm --manage /dev/md0 --add /dev/sdb1

mdadm --manage /dev/md1 --add /dev/sdb2

That's it! you can run:

cat /proc/mdstat

To see the status of the syncing if needed.

____________________

IF YOU'RE RUNNING UBUNTU 20 OR NEWER YOU NEED TO FOLLOW THIS GUIDE

1) Change the source to the existing drive and dest to the new one: MAKE SURE TO CHANGE THESE AS NEEDED!

source=/dev/sda
dest=/dev/sdb

 

2) Create a replica of the source partition table and then generate new UUIDs for the new drive:

sudo sgdisk --replicate=$dest $source
sudo sgdisk -G $dest

 

3) Start syncing the raid and replace the X with the correct partition (it’s 2 for me):

sudo mdadm --manage /dev/md0 -a $(echo "$dest"X)

 

4) Now, copy over the ESP and replace X with the correct partition (it’s 1 for me):

sudo dd if=$(echo "$source"X) of=$(echo "$dest"X)

 

5) Then list the current drive UUIDs:

ls -la /dev/disk/by-partuuid/

 

6) Then show the boot-list:

efibootmgr -v

 

7) Take note of the BootOrder in case you want to change it. If any of the Ubuntu entries point to a UUID that currently don’t exist, delete it (replace XXXX with the ID from the boot-list):

sudo efibootmgr -B -b XXXX

 

8) If any of the current UUIDs for partition 1 on the drives don’t exist in the boot-list, add it (replace the X with the drive that is missing):

sudo efibootmgr --create --disk /dev/sdX --part 1 --label "ubuntu" --loader "\EFI\ubuntu\shimx64.efi"


9) Verify that it’s correct:

efibootmgr -v


All good? Great! You now have a working RAID again.

  • Updated

← All articles

Still stuck?

Our engineers answer tickets directly, 24/7.