How to swap hard drive for a software RAID

Let’s say you have a two-drive raid1 and you want to replace the first drive /dev/sda.

First, fail the first drive.

mdadm /dev/md0 -f /dev/sda1

Now you can swap the hard drive.

dmesg | tail

You should see something like:

[38197155.944373] ata10.00: configured for UDMA/133
[38197155.958437] sas: --- Exit sas_scsi_recover_host: busy: 0 failed: 0 tries: 1
[38197155.970003] scsi 6:0:3:0: Direct-Access     ATA      HITACHI HUA72105 GK6O PQ: 0 ANSI: 5
[38197155.995394] sd 6:0:3:0: [sda] 976773168 512-byte logical blocks: (500 GB/465 GiB)
[38197155.995657] sd 6:0:3:0: Attached scsi generic sg0 type 0
[38197156.035535] sd 6:0:3:0: [sda] Write Protect is off
[38197156.050778] sd 6:0:3:0: [sda] Mode Sense: 00 3a 00 00
[38197156.050841] sd 6:0:3:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
[38197156.099496]  sda: unknown partition table
[38197156.112711] sd 6:0:3:0: [sda] Attached SCSI disk

Now let’s create the file system on /dev/sda.

First, get the partition table from /dev/sdb:

sfdisk -d /dev/sdb > test

Opens the file in vim and replace sdb into sda.

vim test

The file should be like this:

# partition table of /dev/sda
unit: sectors

/dev/sda1 : start=     2048, size=976771072, Id=fd
/dev/sda2 : start=        0, size=        0, Id= 0
/dev/sda3 : start=        0, size=        0, Id= 0
/dev/sda4 : start=        0, size=        0, Id= 0

Now write this file to sda.

cat test | sfdisk /dev/sda

Okay, let’s add this to the raid

mdadm /dev/md0 -a /dev/sda1

Check the raid status:

cat /proc/mdstat


grub-install --recheck --no-floppy /dev/sda

If you got “grub-install: command not found” use “grub2-install”.

The raid would start rebuilding on the newly added drive. Use cat /proc/mdstat to monitor the process.