Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a computer with two WD 1TB drives and I want to configure disk mirroring on it.

I tried setting up RAID during the installation by creating RAID partitions but that does not work for me.

Is there a convenient software that I can install to do the job for me? If no, what shell commands can be used?

Once it is set up how should I verify that it is working?

share|improve this question
Why does "setting up RAID during the installation by creating RAID partitions" not work for you? – jippie Nov 6 '12 at 8:13
I'd go for a combination of md and lvm2 if your disks a so large. With lvm2 you can easily grow and shrink filesystems which is a drama with partitioning. – jippie Nov 6 '12 at 8:17

2 Answers

Warning: Read this whole post before proceeding

You can setup disk mirroring with MDADM, so assuming your two 1TB disks are /dev/sda1 and /dev/sdb1 your command would look something like:

mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sda1 /dev/sdb1

What this means is...Create a new devices called "md0" that will run at RAID-1 and will consist of two devices, "/dev/sda1" and "/dev/sdb1".

Now, let me warn you, there is an inherent flaw with this if 1 of those two harddrives has your /boot mount. You SHOULD NOT put your /boot partition in the Mirror. This is important. You can get into a weird state and things won't boot. It is best to use a single disk that isn't in the RAID to handle the boot partition. I use a USB Flash drive...it holds both my Swap partition and my Boot partition. Also I really recommend that you set this up on blank drives in order to ensure that you don't lose anything by accident. For more information look at man mdadm

Edit: Simulating Disk Failure

Warning: Don't do this until your mirror is in sync

You can easily test your mirror by doing the following:

mdadm -f /dev/md0 /dev/sda1
mdadm --remove /dev/md0 /dev/sda1

This will set the faulty flag in the array for the partition, and then you remove it. Then run mdadm --detail /dev/md0 and you should see 1 Active Device. Access some data in the RAID to show that it worked, then go ahead and add the disk back with

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

Run mdadm --detail /dev/md0 again to see that the spare is being rebuilt (it should complete quickly since you have the data already).

share|improve this answer
Any advise for them on how to test after this is set up? – vgoff Nov 6 '12 at 4:25
In this setup if one drive fails then the boot partition is gone and system becomes unusable (since it isn't mirrored) That defeats the purpose of mirroring. I'm curious as to why one shouldn't put boot partition in mirror? – Kshitiz Sharma Nov 6 '12 at 4:42
Negative. If you put the boot partition on a separate disk, say a USB drive, it should't become corrupt. If you are concerned that it may, duplicate the drive and keep one in the closet. And I wouldn't say it defeats the purpose of mirroring. I don't want to lose data. If I lose the boot partition, I can pop in a live-cd and fix that and still have all my important data. – sparticvs Nov 6 '12 at 4:49
vgoff, I will update the original post. – sparticvs Nov 6 '12 at 4:55
If md0 is to be containing the root filesystem, this will only work before installing the Operating System. Or you need an extra drive. – jippie Nov 6 '12 at 8:15
show 1 more comment

You can do that. You need to be a bit careful, but this is not dangerous¹ if you are very careful not to mistype anything and it doesn't leave any gotchas in the setup.

I highly recommend not doing any of the manipulations on a live system. It's possible in some cases but requires extra care. Boot from a liveCD/liveUSB such as Parted or SystemRescueCD.

Assumption: you have a block device that contains something Linux recognizes, for example:

  • a disk containing one or more partitions;
  • a partition containing a filesystem;
  • a partition containing an LVM physical volume.

Objective: make that block device a component of an mdraid (Linux software RAID) RAID-1 (mirroring) volume. The RAID volume will initially be in a degraded state with all but one components missing.

First, you need to shrink the volume a bit, to make room for mdraid metadata (the superblock). There are several metadata formats, you must use one that puts the metadata at the end of the disk. (In some setups, you may have enough space to put the superblock at the beginning, but that's more complicated and risk-prone so I go into that.)

You must ensure that the last 128kB from the block device are unused, to make room for the superblock.

  • If the block device is a disk containing partitions, shrink the partition that comes last (this may not be the partition with the highest number). You'll need to shrink whatever the partition contains as well.
  • If the block device contains a filesystem, shrink that filesystem.
  • If the block device contains an LVM physical volume, call pvreduce to reduce the size of the physical volume. This may or may not reduce the usable size since physical volumes have a granularity of 4MB (more precisely, one extent: 4MB is the rarely-changed default extent size).

Parted can handle filesystems and partitions. If you need to shrink an ext4 filesystem, you'll need to unmount it first; a btrfs filesystem can be shrunk live. If you've modified the partition table on a disk where some partitions are in use, reboot.

Once you have ensured that the last 128kB of the block device are free, call mdadm --create to create a RAID-1 volume. This doesn't touch any part of the volume aside from the superblock. Initially, the volume will have a single component: all the others are set as failed. You must pass --level=1 (or equivalently -n 1) (this approach only works for RAID-1) and --metadata=0.9 or --metadata=1.0 (the default superblock format 1.2 puts the superblock near the beginning of the device, which may overwrite data). The argument to --raid-devices (-n) is the number of components (included missing ones) in the RAID volume. Replace /dev/sdz99 by the designation of the block device (e.g. /dev/sda for a whole disk or /dev/sda1 for a partition).

mdadm --create /dev/md0 --level=1 --raid-devices=2 --metadata=1.0 /dev/sdz99 missing

You can now activate the array and add other components.

mdadm --add /dev/md0 /dev/sdy98

Grub2 understands Linux RAID-1 and can boot from it. Bootloaders such as Grub1 that don't understand RAID read transparently from mirror volumes, but your system won't boot if the drive the bootloader is reading from fails. If the RAID volume is on a partition, be sure to install Grub's boot sector on both drives.

¹ Be sure to have backups. “Not dangerous” means “you probably won't need them”, not “gamble your data”.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.