Can I create a software RAID 1 with one device

46,468

Solution 1

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.

Solution 2

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

Solution 3

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)

Share:
46,468

Related videos on Youtube

Viktor
Author by

Viktor

Shoot first, so you don't have to ask the questions!

Updated on September 18, 2022

Comments

  • Viktor
    Viktor almost 2 years

    I have a single disk that I want to create a mirror of; let's call this disk sda. I have just bought another identically-sized disk, which we can call sdb. sda and sdb have one partition called sda1 and sdb1 respectively.

    When creating a raid, I don't want to wipe my sda clean and start again, I just want it to start mirroring with sdb. My train of thought was to do:

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

    ... to create the array without sdb disk, then run something like (I'm thinking the following command out loud, because I am not sure how to achieve this step)

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

    Note sdb1 is assumed to be formatted similarly to sda1

    Is this possible?

    • Gilles 'SO- stop being evil'
      Gilles 'SO- stop being evil' over 11 years
      Yes, you can. It's not as straightforward as --add, and you need to be careful, but it's not very complicated. See How to set up disk mirroring in Ubuntu? (I can't propose that question as a duplicate because of the new rules.)
  • eppesuig
    eppesuig over 11 years
    I think the order does not matter.
  • Viktor
    Viktor over 11 years
    Perfect exactly what I need :D
  • Stéphane Chazelas
    Stéphane Chazelas over 11 years
    Don't use cp to copy filesystems, you'll miss some things like hard links, extended attributes... Use tar or rsync with the proper options. When booting a Live CD, beware that uid-user mapping are different (see the --numeric-owner options of tar/rsync). See also clone2fs for cloning extx file systems.
  • StarNamer
    StarNamer over 11 years
    The options to cp handle preserving links (-d), mode, ownership and timestamps (-p), doing it recursively (-R) and staying on one filesystem (-x), but I think you may be right about hard links and extended attributes.
  • hausinho
    hausinho almost 5 years
    rsync -aH --delete /mnt/olddisk/ /mnt/newarray/ is a good option. Add -vih --progress if you want to really see what it's doing.