Rashkae ubuntu at
Thu May 17 14:40:54 UTC 2007

Ryan Nichols wrote:
> What is the best way to make a RAID 1 set out of my existing system? 
> I've got a 320GB Sata Drive and I want to add a 2nd one and make it 
> RAID... What is the best way to do this? I want to keep the data intact.
> Thanks,
> Ryan

I'm going to paraphrase these commands.  I apologize if I make some 
slight syntax errors, but I'm sure you'll get through it with a bit of 
perseverance :)  Always have a backup before you start something like 
this, in case something goes wrong.

Lacking any other information, I'm going to assume, for example, that 
your current drive has 1 Linux partition, and one swap partition, sda1 
and  sda2 respectively.  Your new drive will be sdb.

First, you need to partition the drive using any partition tool you are 
familiar with.  fdisk, cfdisk, gparted, qtparted, etc.  The partition 
that is destined to be your new filesystem must *not* be any larger than 
your current sda1.  Smaller is ok, but any size difference will just be 
wasted space later.  Change the partition type to Linux Raid Autodetect 
(type FD).  While you're in the partition software, you might as well 
add a swap partition on the new drive as well.

Use mdadm to create a new Raid 1 Array, with 2 devices, one will be the 
newly created sdb1, the other is missing.

mdadm create /dev/md0 -l 1 /dev/sdb1 missing

Add a filesystem to the new md0 device:

mkfs.ext3 /dev/md0

Mount the new filesystem

mkdir /mnt/target
mount /dev/md0 /mnt/target

The next step is to copy your data from your old drive to the new.  But 
in order to copy all the static devices that are hidden under the Udev 
devices, first, we have to move the udev somewhere else.

mkdir /udev
mount --move /dev /udev

And now to actually copy the data:

cp -ax / /mnt/target

(You can add the -v switch to the cp command if you want to watch the 
progress, but this can slow things down considerably on some systems)

When the copy is done, you need to edit the /mnt/target/etc/fstab file 
as well as the /mnt/target/boot/grub/menu.lst file.  You need to change 
the device of the root device in both files to either /dev/md0 or the 
UUID of the new filesystem, depending on your preference.

Now enter the Grub Shell, the command is grub

in the grub shell, issue these commands

device (hd0) /dev/sdb
setup (hd0) (hd0,0)

exit (or quit)

If all went well, and neither of us made a mistake, you should now be 
able to remove your old drive, and connect the new drive in its place. 
Reboot like that to make sure everything works.  If the system boots up 
as normal, you might also consider running badblocks on your new drive 
to make sure there are no bad sectors creeping up.. The next stage will 
erase your old hard drive.

badblocks -v /dev/sda

Now connect your old harddrive back in the system so it would be /dev/sdb

Use a partition tool to change the partition type of /dev/sdb1 to FD 
(linux raid)

Use mdadm to add the partition to your existing md0 device.

mdadm manage /dev/md0 -a /dev/sdb1

This will silently build the RAID mirror you desired in the background. 
  you can check on the progress by reading the /proc/mdstat file

cat /proc/mdstat

Final Clean-up:

open the grub shell again and run the exact same commands as before. 
This should make your secondary drive bootable, and in theory should 
just boot as normal even if you have to remove the failed primary drive.

If you followed my advice and now have a swap partition on both drives, 
you'll want to make certain they are both referenced in in your 
/etc/fstab with a priority of 0

/dev/sda2         swap                    swap    pri=0        0 0
/dev/sdb2         swap                    swap    pri=0        0 0

(use use UUID's if you prefer)  After you reboot, you can verify all 
your swaps are working as intending by looking at the /proc/swaps file

cat /proc/swaps

Mine looks like this:
Filename                      Type            Size    Used    Priority
/dev/sda1                     partition       626492  64      0
/dev/sdc1                     partition       1951856 64      0

The important part is that they are both there and their priorities are 
the same.  This will cause linux to spread the load evenly between them, 
  potentially doubling your swap performance.

You can, if you like things neat, now delete the /mnt/target /udev 
directories.   They have served their purpose and are no longer needed, 
but they wont hurt anything by staying there either.

I've done this kind of procedure and/or parts thereof several times, but 
I confess, I never realized how complex the whole damn thing is until I 
had to type out all the steps.  It is, however, relatively easy to 
actually do. :)

More information about the ubuntu-users mailing list