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?

  • 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

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”.

  • One can also re-build a superblock-less array on each boot, employing the --assume-clean option (assuming the array is indeed clean).
    – peterph
    Jun 27 '13 at 8:09
  • What are the implications of using a whole disk (e.g. sda) as a RAID-1 member instead of a partition (e.g. sda1 with raid signature 0xFD), the latter of which I mostly read in tutorials?
    – user86969
    Oct 25 '17 at 7:49
  • 1
    @Nasha Mainly that you won't be able to partition it later. Possibly, if you install some other OS, that it might think that the disk only contains garbage, rather than noticing a partition of a type it doesn't support and warning you before overwriting it. See also unix.stackexchange.com/questions/14010/… Oct 25 '17 at 19:28
  • Thanks @Gilles. The context I'm considering indeed is when I only have one filesystem and one OS. One last question I have is: does software RAID using the whole disk have an impact on disk hotplugging after a failure for instance?
    – user86969
    Oct 26 '17 at 12:06
  • 1
    @Nasha No, there's no difference in normal operation (including disk substitutions), only for maintenance. Oct 26 '17 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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