Linux RAID1 starting from single disk

Just found some notes I did take some time ago about creating a software RAID1 starting with only one disk: it’s a bit more complicated than starting with two, but doable anyway. First thing, partition your disk, you should have at least one partition marked as FD type (Linux Raid Autodetect):

# cfdisk /dev/vda
[create new partition, change partition type to FD, write to disk]

Next, you should build your array, with the only disk you have and save the configuration:

# mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/vda1 missing
# mdadm --detail --scan >> /etc/mdadm/mdadm.conf

After the array is started, it should appear as “degraded” in /proc/mdstat, but we can use it right away; let’s format and mount it:

# mkfs.ext4 /dev/md0
# mkdir /mnt/raid
# mount /dev/md0 /mnt/raid

Next we can save some data to the new filesystem, so that we can make sure it’s ok after the rebuild

# date > /mnt/raid/current-date
# cat /mnt/raid/current-date

Now we will add the second disk to the array. First thing, we would copy the partitioning scheme from vda to vdb and check to make sure everything is fine:

# sfdisk -d /dev/vda | sfdisk /dev/vdb
# fdisk -l 2>/dev/null | grep -B1 '^\/dev'

Next step, is to add the new disk. Let’s umount the raid first, then add the disk and check if the array is rebuilding itself (use CTRL+C to quit watch command):

# umount /dev/md0
# mdadm --add /dev/md0 /dev/vdb1
# watch cat /proc/mdstat
# mount /dev/md0 /mnt/raid
# cat /mnt/raid/current-date

When the rebuild will end, you’ll be ready to go :)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.