# How to mirror hard drives? [SOLVED]

## Ph0eniX

I'm thinking of adding a second hard drive to a desktop Gentoo system in order to have some fault tolerance.  How should I go about setting up RAID-1 (mirroring)?

Thanks!  :Very Happy: 

J.Last edited by Ph0eniX on Wed Jul 22, 2009 6:00 pm; edited 1 time in total

----------

## theotherjoe

how about this doc ?

http://www.gentoo.org/doc/en/gentoo-x86+raid+lvm2-quickinstall.xml

----------

## wildhorse

Whatever you do, don't get into the FakeRAID business. I am trying to cope with an existing setup which I cannot change and it is such a pain in the you-know-what. https://forums.gentoo.org/viewtopic-t-780671-highlight-fakeraid.html Could still use some help with this one.   :Wink: 

Plain software RAID via LVM2 is fine, but it does not provide the best performance.

Consider using a true hardware RAID controller. If that is no option, then take a look at acard and dawicontrol. They sell small PCBs which convert a single PATA/SATA channel into a mirrored one (two disks). The latter solution is entirely software independent. If you decide to use a hardware solution, then make sure you have spare parts. Hardware sometimes disappears from the market and your controller will fail exactly one month after the last controller has been sold - that is a promise.   :Razz: 

http://www.dawicontrol.com/ (see DC-5220)

http://www.acard.com/

----------

## Ph0eniX

 *theotherjoe wrote:*   

> how about this doc ?
> 
> http://www.gentoo.org/doc/en/gentoo-x86+raid+lvm2-quickinstall.xml

 

Thanks!  That's exactly what I was looking for.  One question though.  I already have a drive with data on it.  Can I just add the second one without destroying the data on the first one using this method?

Thanks again!  :Very Happy: 

J.

----------

## Exil

From 

http://www.iaeste.or.at/doc/mdadm/README.recipes.gz

```

[...]

10. convert existing filesystem to RAID 1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    # The idea is to create a degraded RAID 1 on the second partition, move

    # data, then hot add the first. This seems safer to me than simply to 

    # force-add a superblock to the existing filesystem.

    #

    # Assume /dev/sda1 holds the data (and let's assume it's mounted on

    # /home) and /dev/sdb1 is empty and of the same size...

    #

    mdadm --create /dev/md0 -l1 -n2 /dev/sdb1 missing

    mkfs -t <type> /dev/md0

    mount /dev/md0 /mnt

    tar -cf- -C /home . | tar -xf- -C /mnt -p

    # consider verifying the data

    umount /home

    umount /mnt

    mount /dev/md0 /home    # also change /etc/fstab

    mdadm --add /dev/md0 /dev/sda1

10b. convert existing filesystem to RAID 1 in-place

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    In-place conversion of /dev/sda1 to /dev/md0 is effectively

      mdadm --create /dev/md0 -l1 -n2 /dev/sda1 missing

    however, do NOT do this, as you risk filesystem corruption.

  

    If you need to do this, first unmount and shrink the filesystem by

    a megabyte (if supported). Then run the above command, then (optionally)

    again grow the filesystem as much as possible.

    Do make sure you have backups. If you do not yet, consider method (10)

    instead (and make backups anyway!).

[...]

```

----------

## Ph0eniX

 *Exil wrote:*   

> From 
> 
> http://www.iaeste.or.at/doc/mdadm/README.recipes.gz
> 
> ```
> ...

 

Thank you but how does one shrink the files system by a megabyte? 

J.

----------

## Exil

from gparted livecd

----------

## Ph0eniX

Thank you!  I'll let you guys know how I made out!

----------

## Ph0eniX

Another question came up as I'm about to pull the trigger.  I'm going to be mirroring all the partitions including:

/dev/sda1 (to sdb1/md0) - /boot

/dev/sda2 (to sdb2/md1) - swap (not sure I need to mirror it but what the heck)

/dev/sda3 (to sdb3/md2) - /

/dev/sda4 - (to sdb4/md3) - /home

My question is about mirroring /boot.  I'm assuming the tar command does not copy the boot record/grub so I will have to reinstall grub in /dev/md0 when I'm done in order to boot?  Am I on the right track here?

Thanks!  :Very Happy: 

J.

----------

## Ph0eniX

So I got /home on a dmraid (/dev/md3).   It seems to work fine but I can't figure out how to make the root partition work.  It seems that dmraid gets initialized much further into the boot process than it should in order boot from /dev/md2.  I tried compiling the kernel with dmraid and adding "dodmraid" to the grub options but it's still failing to mount the root (/dev/md3) when it boots.   What did I do wrong?

Thanks  :Very Happy: 

J.

----------

## gentoo_ram

As far as I know, you need an initramfs to do dmraid on a root partition.  You don't need this if you are doing native Linux mirroring (mdadm).  Why don't you just use native Linux software mirroring?  With native mirroring you set the partition type to 'fd' and create the mirrors using mdadm.  When you boot, Linux will reconstruct the mirror during the kernel boot and mount the mirror before it even starts init.  Much simpler.  The only reason I can think of to use dmraid is if you need compatibility with Windows.  Otherwise, there's no reason for dmraid.

----------

## Ph0eniX

 *gentoo_ram wrote:*   

> As far as I know, you need an initramfs to do dmraid on a root partition.  You don't need this if you are doing native Linux mirroring (mdadm).  Why don't you just use native Linux software mirroring?  With native mirroring you set the partition type to 'fd' and create the mirrors using mdadm.  When you boot, Linux will reconstruct the mirror during the kernel boot and mount the mirror before it even starts init.  Much simpler.  The only reason I can think of to use dmraid is if you need compatibility with Windows.  Otherwise, there's no reason for dmraid.

 

I compiled the initrd with dmraid and mdadm, then added the following to my grub.conf:

md-mod.start_dirty_degraded=1 dodmraid domdadm

One of the above options did the trick.  All the partitions are mirrored now.

Thanks for the help everyone!  :Very Happy: 

J.

----------

## energyman76b

a) if you use md, you don't need dmraid

b) you don't need lvm either

c) you don't need an initrd

d) just install grub on md0

e) don't shrink. Go the long route, it is saver

f) enable Autodetect RAID arrays during kernel boot 

g) add this too grub: root=/dev/md1 (or md2,3 depending on your setup) md=1,/dev/sda3,/dev/sdb3 (or whatever partitions you used for your root md device)

----------

## Ph0eniX

 *energyman76b wrote:*   

> a) if you use md, you don't need dmraid
> 
> b) you don't need lvm either
> 
> c) you don't need an initrd
> ...

 

I decided to rename the md devices to match the sda parittions numbers (less confusion that way):

md1 = /boot (sda1, sdb1)

md2 = swap (sda2, sdb2)

md3 = / (sda3, sdb3)

md4 = /home (sda4, sdb4)

I removed the dodmraid option from grub.conf as well.  Here's what my grub.conf looks like:

```

Gentoo 2008.0

root (hd0,0)

kernel /kernel-genkernel-x86-2.6.29-gentoo-r5.jacks root=/dev/ram0 real_root=/dev/md3 init=/linuxrc md-mod.start_dirty_degraded=1 domdadm ramdisk=8192 vga=791

initrd /initramfs-genkernel-x86-2.6.29-gentoo-r5.jacks

```

I didn't install grub onto /dev/md1 though.  I installed it on each md1 device instead though (sda1 & sdb1).  It seems to actually work.  I shut the system down and unplugged the sda drive, turned the machine on and the system booted fine from sdb.  When I shut it down, plugged sda back in and booted, it came up fine but /proc/mdstat showed that only sdb devices were up for md3 and md4 so I ran:

mdadm --add /dev/md3 /dev/sda3

mdadm --add /dev/md4 /dev/sda4

That sync'ed the md's back up.  I'd prefer if the re-sync was initiated automatically if possible.

Also, my root points to /dev/ram0 as you can see in the grub options.

J.

----------

## Ph0eniX

Another question.  I have added my e-mail address to /etc/mdadm.conf so I get notified about RAID events but when I try to test it, it hangs until I press Ctrl-C and then it actually sends the notification:

```

mdadm --monitor /dev/md4 --test

mdadm: Monitor using email address myaddress@mydomain.com from config file

^C

```

How do I fix this?

Thanks!  :Very Happy: 

J.

----------

## energyman76b

are you sure your mail adress does work?

----------

## Ph0eniX

 *energyman76b wrote:*   

> are you sure your mail adress does work?

 

Yes, I am.  Like I said, the mail gets sent and I receive it fine after pressing Ctrl+C.  mdadm just hangs while sending though.

----------

## energyman76b

well, maybe because it does not hang? what happens, when you put a & at the end of the line?  :Wink: 

----------

## Ph0eniX

 *energyman76b wrote:*   

> well, maybe because it does not hang? what happens, when you put a & at the end of the line? 

 

It looks like mailx was hanging while connecting to a specific mailhub.  I made it connect to a different server and the problem went away.  I still would like to know if there is a way to initiate a re-sync automatically after one of the md devices comes back online.  Is there a way to do that?

Thanks!

----------

## energyman76b

man mdadm.conf:

   PROGRAM

              The  program  line  gives  the  name  of a program to be run when mdadm

              --monitor detects potentially interesting events on any of  the  arrays

              that  it  is monitoring.  This program gets run with two or three argu-

              ments, they being the Event, the md device, and  possibly  the  related

              component device.

              There  should  only  be one program line and it should be give only one

              program.

and then add a script?

edit:

  auto=  This  option  declares  to  mdadm  that it should try to create the

                  device file of the array if it doesn't already exist, or exists but

                  with the wrong device number.

                  The  value  of  this option can be "yes" or "md" to indicate that a

                  traditional, non-partitionable  md  array  should  be  created,  or

                  "mdp",  "part"  or  "partition" to indicate that a partitionable md

                  array (only available in linux 2.6 and later) should be used.  This

                  later set can also have a number appended to indicate how many par-

                  titions to create device files for, e.g.  auto=mdp5.   The  default

                  is 4.

----------

## Ph0eniX

So theoretically something like this should work:

```

if [ "$1" -eq "Fail" ];then

mdadm --remove $2 $3

mdadm --add $2 $3

fi

```

Correct?

----------

## Ph0eniX

This actually seems to work:

```

if [ "$1" == "Fail" ];then

mdadm --remove $2 $3

mdadm --add $2 $3

fi

```

----------

## nukem996

I run into the exact same situation awhile back and this is how I handled it.

I booted into a livecd and create a "mirrored" array or RAID1 with only 1 drive. Yes you can do this, you just have to force it. Once I setup my RAID1 I setup the files ystem and copied all the files from my old drive onto my new raid. I booted into my new raid to make sure everything was working. Once I confirmed that I added my old drive to my new raid. This gave me a true RAID1 setup without having to do anything to messy.

----------

## Ph0eniX

 *nukem996 wrote:*   

> I run into the exact same situation awhile back and this is how I handled it.
> 
> I booted into a livecd and create a "mirrored" array or RAID1 with only 1 drive. Yes you can do this, you just have to force it. Once I setup my RAID1 I setup the files ystem and copied all the files from my old drive onto my new raid. I booted into my new raid to make sure everything was working. Once I confirmed that I added my old drive to my new raid. This gave me a true RAID1 setup without having to do anything to messy.

 

Did you create the RAID using mdadm?

----------

