Can I create a software RAID 1 with one device

The simple answer to the question in the title is "Yes". But what you really want to do is the next step, which is getting the existing data mirrored.

It's possible to convert the existing disk, but it's risky, as mentioned, due the the metadata location. Much better to create an empty (broken) mirror with the new disk and copy the existing data onto it. Then, if it doesn't work, you just boot back to the un-mirrored original.

First, initialize /dev/sdb1 as the new /dev/md0 with a missing drive and initialize the filesystem (I'm assuming ext3, but the choice is yours)

mdadm --create --verbose /dev/md0 --level=mirror --raid-devices=2 /dev/sdb1 missing
mkfs -text3 /dev/md0

Now, /dev/sda1 is most likely your root file system (/) so for safety you should do the next step from a live CD, rescue disk or other bootable system which can access both /dev/sda1 and /dev/md0 although I have successfully done this by dropping to single user mode.

Copy the entire contents of the filesystem on /dev/sda1 to /dev/md0. For example:

mount /dev/sda1 /mnt/a       # only do this if /dev/sda1 isn't mounted as root
mount /dev/md0 /mnt/b
cd /mnt/a                    # or "cd /" if it's the root filesystem
cp -dpRxv . /mnt/b

Edit /etc/fstab or otherwise ensure that on the next boot, /dev/md0 is mounted instead of /dev/sda1. Your system is probably set to boot from /dev/sda1 and the boot parameters probably specify this as the root device, so when rebooting you should manually change this so that the root is /dev/md0 (assuming /dev/sda1 was root). After reboot, check that/dev/md0 is now mounted (df) and that it is running as a degraded mirror (cat /proc/mdstat). Add /dev/sda1 to the array:

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

Since the rebuild will overwrite /dev/sda1, which metadata version you use is irrelevant. As always when making major changes, take a full backup (if possible) or at least ensure that anything which can't be recreated is safe.

You will need to regenerate your boot config to use /dev/md0 as root (if /dev/sda1 was root) and probably need to regenerate mdadm.conf to ensure /dev/md0 is always started.


Sure, you may create it specifying that second disk is currently missing:

mdadm --create --verbose /dev/md0 --level=mirror --raid-devices=2 missing /dev/sda1

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.

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. So you'll need to shrink the filesystem on /dev/sda1. If this is an ext2/ext3/ext4 filesystem, obtain the current filesystem size with tune2fs /dev/sda1, then run resize2fs /dev/sda1 NNN where NNN is that size minus 128kB. You can do this with Parted instead. If you need to shrink an extN filesystem, you'll need to unmount it first; a btrfs filesystem can be shrunk live.

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.

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

You can now activate the array and add other components.

mdadm --add /dev/md0 /dev/sdb1

A note on bootloaders: 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”.

Reposted and slightly adapted from How to set up disk mirroring (RAID-1)

Tags:

Disk

Raid

Mdadm