##This tutorial requires sudo or root privilleges.

##BACKUP ALL DATA ON YOUR DRIVES BEFORE ATTEMPTING ANYTHING ON THIS TUTORIAL. IF YOU LOOSE YOUR DATA, I CANNOT BE HELD RESPONSIBLE. IF YOU DO NOT BACKUP YOUR DATA, PROCEED AT YOUR OWN RISK.

Mdadm is a software raid solution for Linux. While many guides are available for the creation of the array itself, configuration beyond this is less documented. This tutorial will show you how to create a RAID 1 (mirrored) array. However, the commands for RAID 0 or RAID 5 should be obvious from the text. This tutorial is somewhat unusual in the intended use of the RAID drives. Many of the other guides address the usage of a RAID array on a server, as the main drives. Mine will be added after the installation of the system, to add additional storage space. This tutorial endeavor to take the reader from the creation of the array to the final configuration. In keeping with my standard methods, I intend to use command line methods as much as possible in this guide.This guide assumes some Linux skills.

All commands should be executable by prefacing the commands with sudo, however, it may be easier to login as root instead. To do this,depending on your system, use either sudo su which will require your regular password or su which will require your root password. It should be noted on my Ubuntu install I had to use sudo su in order to be able to access all the harddrives.

First the disks have to be partitioned. The entire disk can be used, or it can be split into multiple partitions. This tutorial will have all its examples done with two partitons. It should be obvious how to do more, or fewer, partitions. Additionally, you could use serveral hard drives rather than just partitions. To do this we will use cfidisk. If your disks are already partitioned skip to here. If one of the drives already has data on it as well, skip to here

Prior to partitioning anything, make sure your drives are umnouted.

To get a listing of the device names you can use fdisk -l. You will have to look at the size of the drives to discover the ones you wish to use. Now use cfidisk /dev/sda Replace /dev/sda with the device name you intend to use. Just do the cfdisk to one of the drive you intend to use for now.

Cfdisk will give you a sort of pseudo graphical environment. Select the disk you are going to use with the up and down arrow keys. Use the right arrow key to move through the options at the bottom of the terminal until you get to new, and select that. Make the partition the size you want, and enter that. Will still having that partiton selected, now sda1. Go further right in the options at the bottom, and select types, finding Linux Raid autodetect, it should be something like option FD. Now, on the main menu again, redo the steps for the remaining free space. This time, you should have sda2 as your partition. You can label the drives if you wish.

Now you need to clone the partitions on to your other disk.

sfdisk -d /dev/sda | sfdisk /dev/sdb

This will clone the partitions from sda to sdb, by piping the output of the partition data from sfdisk -d /dev/sda into sfdisk /dev/sdb. You can replace sda and sdb with the proper drives for your setup.

Creating Array with mdadm

Now you need to install the mdadm software.

apt-get install mdadm

Now, it is time to begin the creation of the RAID array.

To do this in one step, from blank partitions,if you have data on the drive, continue skip this command.

mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sda1 /dev/sdb1

mdadm --create /dev/md1 --level=1 --raid-devices=2 /dev/sda2 /dev/sdb2

If you use this command, you can go ahead and skip to formatting the drives, here

Obviously the previous code is the easiest method. In some cases you might already have data on your drives. In this case, you can use a multistage method to keep the data on your drives. --DISCLAIMER-- Make sure to backup your data before any operation with your hard drives. I cannot guarantee that this method will not delete your data. Also, the data can only be on one drive, the other must be blank, but with the same partitions.Make sure to note the md0 and md1 because they will become later on.

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

mdadm --create /dev/md1 --level=1 --raid-devices=2 /dev/sda2 missing

Put the drive, or partition, that has data in it in the place of the /dev/sda's.

Now, you add in the drive that you told mdadm was missing. This command should then mirror the data over to the new drive.

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

mdadm --manage --add /dev/md1 /dev/sdb2

For those of you with data already on your drives, you should be able to skip to assembling the partitions and automatically mounting.

Assemble the Array, Format the Array and Test it

Assemble Array

To test, you first must assemble the RAID array. To do this, use the following command

mdadm --assemble /dev/md0 /dev/sda1 /dev/sdb1

mdadm --assemble /dev/md1 /dev/sda2 /dev/sdb2

Having done this, the array can be treated as a single drive.

Format

mkfs.ext4 -m 0 /dev/md0 Make sure to use the same device (md0 or any others you have created).Now you see why you needed to note the device name.

You can use and format by replacing ext4 of mkfs.ext4 with the filesystem of your choice. You can add the switch -T largefile if you are going to have large files on the filesystem (it will improve performance in this case). This is usefully especially for things like videos and other large media.

Mount (Test)

Now you get to make sure that you can mount your drive. First we have to make a mount point.

mkdir /mnt/temp_test /mnt/temp_test2

You can replace the name and mount point with your own location.

Now we'll mount one of the drives.

mount -t ext4 /dev/md0 /mnt/temp_test and for the second array mount -t ext4 /dev/md1 /mnt/temp_test. Obviously you must replace options with the relevant ones for your setup. Change to the directories to make sure they are functioning. You can use df -h . to make sure the total size on the partition appears to be the same as the one you attempted to mount. Now you have to unmount the drives.

umount /mnt/temp_test and umount /mnt/temp_test2

By now, you should have a working array!

Assembling and Mounting Your Array on Boot

Editing mdadm.conf

The first step towards is editing the mdadm.conf file. This is the step in which I found poorly explained. Some tutorials fail to mention it all together, which cause a number of problems for me. First, a basic configuration, which we will look at, then change to a more resilant version. In you favorite text editor, open up /etc/mdadm/mdadm.conf. If the file has been autogenerated (which is probably will be) the compenets shown bellow should be in this order, but may have commented text between them.

DEVICE /dev/sda /dev/sdb

ARRAY /dev/md0 level=raid1 num-devices=2 devices=/dev/sda1,/dev/sdb1

ARRAY /dev/md1 level=raid1 num-devices=2 devices=/dev/sda2,/dev/sdb2

This is a basic configuration. Better is to use the UUID of the array, which will identify the drives specifically. This eans if you add a new drive, it will not disrupt the array.

To find the UUID, use the following command.

mdadm --detail --scan

This should give you an output similar to the following.

ARRAY /dev/md0 level=raid1 num-devices=2 metadata=00.90 UUID=472189f4:be6514e3:b04aa8e2:c2d848e8

ARRAY /dev/md1 level=raid1 num-devices=2 metadata=00.90 UUID=cd3cd071:075730a5:b04aa8e2:c2d848e8

Now, we will replace the devices section of the mdadm.conf file with the uuid. Copy the output resembling UUID=472189f4:be6514e3:b04aa8e2:c2d848e8. Use it to replace devices=/dev/sda1,/dev/sdb1. Your mdadm file may not look exactly like this, but it should be possible to identify and replace all the componets. If you've done this successfully, then hold on just a little bit longer, we're almost done.

Initializing the Array on Boot

The mdadm.conf file now must be added to the initramfs file. Use the following command.

update-initramfs -u

Now, when you boot the array will be assembled. If you want to have the mounted on boot, then you will have to add them to fstab, the final step

Adding Arrays to fstab

Automatically mounting array is very similar to mounting regular drives. You must have mount points created (disscussed in the testing section). These mount points are simply empty directories which once you mount the drive in will have the contents of the drive in them. The following is an example of a line that could mount a single line.

/dev/md0 /media/storage ext4 rw,noatime 0 0

Using your own detials, add this line to the bottom of the fstab file. You can do this by opening up /etc/fstab in your favorite text editor.

Troubleshooting

You should now have an RAID 1 array which assembles and mounts at boot. If you've had problems, did you enter all the commands as root? Make sure you had the right drive names.

Comments

in the nick of time -

just what I needed, thanks