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”.
md
andlvm2
if your disks a so large. With lvm2 you can easily grow and shrink filesystems which is a drama with partitioning. – jippie Nov 06 '12 at 08:17