[CentOS] software raid - better management advice needed

Thu Jun 10 13:40:16 UTC 2010
Bowie Bailey <Bowie_Bailey at BUC.com>

Robert Heller wrote:
> At Wed, 9 Jun 2010 16:50:53 -0700 CentOS mailing list <centos at centos.org> wrote:
>
>   
>> Hi,
>>
>> I've used mdadm for years now to manage software raids.
>>
>> The task of using fdisk to first create partitions on a spare drive  
>> sitting on a shelf (raid 0 were my 1st of 2 drives failed) is kind of  
>> bugging me now.
>>
>> After using fdisk to create the same partition layout on the new drive  
>> as is on the existing drive and then using mdadm to finish every thing  
>> up is a little tedious.
>>
>> Any one have an idea how to have a sort of hot plug were I just swap  
>> out the drive and it rebuilds?
>>     
>
> sfdisk is your friend (from man sfdisk):
>
>        -d     Dump the partitions of a device in a format useful as input  to
>               sfdisk. For example,
>                   % sfdisk -d /dev/hda > hda.out
>                   % sfdisk /dev/hda < hda.out
>               will correct the bad last extended partition that the OS/2 fdisk
>               creates.
>
> So:
>
> 1) plug in replacement disk.
> 2) partition it:
>
> # sfdisk -d /dev/sdX | sfdisk /dev/sdY
>
> Where /dev/sdX is an existing disk and /dev/sdY is the replacement disk
>
> 3) add the partition(s) to the array(s):
>
> # mdadm /dev/mdI ... -a /dev/sdYI
> # mdadm /dev/mdJ ... -a /dev/sdYJ
> # mdadm /dev/mdK ... -a /dev/sdYK
> # mdadm /dev/mdL ... -a /dev/sdYL
>
> No reason not to put all of the above in a script...

Agreed.  And I have... :)

The script is designed to add a third drive to a raid1 set.  I use this
with a removable drive to get a backup of the system that can be taken
off-site.

The sleeps in this script are probably a bit excessive, but they are
designed to let the system fully process each command before the next
one is given.  I found that certain things would not work properly
without a pause in there.  Since I only run this once a month, it's not
a big deal if it takes a couple of minutes to run.

Just posting this in case it proves useful to anyone.

-- 
Bowie
-------------- next part --------------
#!/bin/bash

# This script expects a device name such as "sdc".  It will partition the device based on
# the partition info in the partitions.(device) file in the current directory, add the
# partitions to the appropriate mdraid devices, and fix grub so the disk is bootable.

if [ "$1" == "" ]
then
  echo "ERROR - Please enter a device name"
  exit
fi
if [ ! -b /dev/$1 ]
then
  echo "ERROR - Device \"$1\" does not exist"
  exit
fi
if [ -b /dev/${1}1 ]
then
  echo "ERROR - Device \"$1\" already has partitions"
  exit
fi
if [ ! -e partitions.$1 ]
then
  echo "ERROR - \"partitions.$1\" file does not exist"
  exit
fi

read -p "This will erase all data on /dev/$1, are you sure? (yes|no) " yesno
if [ "$yesno" != "yes" ]
then
  exit
fi

echo -e "\n"

echo "Creating partition table"
sfdisk /dev/$1 < partitions.$1

sleep 30

echo "Adding partitions to raid arrays"
mdadm -a /dev/md0 /dev/${1}1
sleep 2
mdadm --grow -n 3 /dev/md0

sleep 30

mdadm -a /dev/md1 /dev/${1}2
sleep 2
mdadm --grow -n 3 /dev/md1

sleep 30

mdadm -a /dev/md2 /dev/${1}3
sleep 2
mdadm --grow -n 3 /dev/md2

sleep 30

echo "Fixing grub on the new drive"
grub --batch << EOF
device (hd0) /dev/$1
root (hd0,0)
setup (hd0)
quit
EOF