mdadm is a command line software raid tool for raid’ing multiple partitions/disks together without worrying about any hardware devices or drivers.

With mdadm, you can do any type of raid you like as well as monitor and recover from drive losses etc..

Its pretty much the ultimate tool for anything raid on linux!

In my situation, I wanted to create a sweet media server for high definition movies and media without having to spend a fortune on hardware raid devices.

Now to get down to business.

I had a lot of material on 2 1.5TB Seagate 1.5TB hard drives already with no other place I could back it up.  I wanted to have about 7 Terabytes of storage in RAID 5 meaning I would need about 6 1.5 drives total.

So I ordered 4 drives and this initial part of the guide will talk about the actual creating of a raid array with 4 partitions on 4 drives.


My drives were assignned by my kernel as follows:

Using fdisk, we must create a partition on each drive and assign the type of filesystem to “Linux Raid Autodetect” so that the kernel recognizes your devices as part of a raid array.
I won’t show all the responses back from the program but I will show you the appropriate commands to run.

root@scn: # fdisk /dev/sdb Command (m for help): n p 1

Then go ahead and push enter to accept the default values assuming you want to use the whole disk as part of the raid array. Note that no matter how different the size of the partitions you use, each partition will only be used to the maximum of the smallest partitions. Basically, if you have a 500GB, 1TB and 1.5TB drive, the raid software will make 3 raid devices that are 500GB’s each.

Now to assign the type:

Command (m for help): t Hex code (type L to list codes): fd Command (m for help): w

This will set the proper type and write all the changes to disk. To check before you write changes, use the “p” option to print out the pending changes.
**Note: **Your partitions will probably be /dev/sdbp1 instead of just /dev/sdb like mine were. That’s fine and make sure you specify that partition to mdadm.

Creating the Array

It’s preferred to be in single-user mode when you create the array. I personally was not when I did this process and everything still went smoothly. Its up to you.

You can either “apt -get install mdadm” or you can go download it and make sure to get the latest version (3.1.4) at the time of this writing.

Here we go:

mdadm –create –verbose /dev/md0 –level=5 –raid-devices=4 /dev/sdb /dev/sdc /dev/sdd /dev/sde

This command is pretty much all you need to create the raid array. It will not actually be very verbose, just tell you that it has started the array /dev/md0 and then return you to your prompt.

If you want to modify your chunk size (usually bigger is better if you are dealing with large files), then you can add that option in the command using –chunk (in KB)

mdadm –create –verbose /dev/md0 –chunk=128–level=5 –raid-devices=4 /dev/sdb /dev/sdc /dev/sdd /dev/sde

Top view the process of the creation, run a “cat /proc/mdstat”  It will take quite a bit of time.
After it finishes, run this command to view all the details about the array and make sure everything is checking out ok.

mdadm –detail /dev/md0

If everything checks out, its time to format it!  You can choose any filesystem you want.  I personally choose jfs for its fast foramt time and efficiency especially in low processor situations.

mkfs.jfs /dev/md0

This will format the raid array to the jfs filesystem specification. You may have to install jfs-utils first.  You could also format it ext3 if you wanted.

mkfs.ext3 /dev/md0

Pretty self explanatory. Your array is now synced and formatted. It is ready to mount. Make sure the directory exists first and has nothing in it.

mount -t jfs /dev/md0 /raid

Post Actions to Perform

Now that your array has been created, you must make sure that mdadm will be able to automatically create it upon boot time.

mdadm –detail –scan –verbose > /etc/mdadm/mdadm.conf

This will make sure mdadm will find your devices at boot. You can then proceed to add the array to the fstab file so it automatically mounts as well.

Things to remember

The most important thing to remember is the difference between starting the array and mounting the array.

The array must be actually started by mdadm itself before it can be mounted. If you have your mdadm.conf setup, use this command

mdadm –assemble –scan

If you don’t, run this

mdadm –assemble /dev/md0 /dev/sdb /dev/sdc /dev/sdd /dev/sde

Then you can continue to on to mounting /dev/md0 to some directory.  In the boot process, the array is started first, then fstab will mount the array granted you have this line in that file

/dev/md0 /raid ext3 defaults 1 2

Also note that by default, a cron job is setup for the first sunday of every month for a “checkarray” routine to start where mdadm will scan an fix parity information
Check out your system cron files to find this scheduled task and either remove it or set it for a different time. NOTE: You can still use the array while a “checkarray” is running. (Don’t be alarmed if cat /proc/mdstat says “recovery” It’s just trying to scare you.

Thats about it. Your raid array should be fully functional and hopefully pretty fast.  The process is basically the same with other raid levels granted you understand those levels before diving in and creating the array.

Mario Loria is a builder of diverse infrastructure with modern workloads on both bare-metal and cloud platforms. He's traversed roles in system administration, network engineering, and DevOps. You can learn more about him here.