Linux Create Software RAID 1 (Mirror) Array

How do I create Software RAID 1 arrays on Linux systems without using GUI tools or installer options? How do I setup RAID 1 array under Linux systems?

You need to install mdadm which is used to create, manage, and monitor Linux software MD (RAID) devices. RAID devices are virtual devices created from two or more real block devices. This allows multiple devices (typically disk drives or partitions) to be combined into a single device to hold (for example) a single filesystem. Some RAID levels include redundancy and can survive some degree of device failure.

Linux Support For Software RAID

Currently, Linux supports the following RAID levels (quoting from the man page):

  1. LINEAR
  2. RAID0 (striping)
  3. RAID1 (mirroring)
  4. RAID4
  5. RAID5
  6. RAID6
  7. RAID10
  8. MULTIPATH, and FAULTY.

MULTIPATH is not a Software RAID mechanism, but does involve multiple devices: each device is a path to one common physical storage device. FAULTY is also not true RAID, and it only involves one device. It provides a layer over a true device that can be used to inject faults.

Install mdadm

Type the following command under RHEL / CentOS / Fedora Linux:
# yum install mdadm
Type the following command under Debian / Ubuntu Linux:
# apt-get update && apt-get install mdadm

How Do I Create RAID1 Using mdadm?

Type the following command to create RAID1 using /dev/sdc1 and /dev/sdd1 (20GB size each). First run fdisk on /dev/sdc and /dev/sdd with “Software Raid” type i.e. type 0xfd:
# fdisk /dev/sdc
# fdisk /dev/sdd

See fdisk(8) man page to setup partition type. Do not format partition. Just create the same. Now, create RAID-1 as follows.

If the device contains a valid md superblock, the block is overwritten with zeros:

# mdadm --zero-superblock /dev/sdc /dev/sdd

Create RAID1 using /dev/sdc1 and /dev/sdd1

# mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdc1 /dev/sdd1

Format /dev/md0 as ext3:

# mkfs.ext3 /dev/md0

Mount /dev/md0

# mkdir /raid1
# mount /dev/md0 /raid1
# df -H

Edit /etc/fstab

Make sure RAID1 get mounted automatically. Edit /etc/fstab and append the following line:

/dev/md0 /raid1 ext3 noatime,rw 0 0

Save and close the file.

How Do I See RAID Array Building Progress and Current Status?

Type the following command:
# watch -n 2 cat /proc/mdstat
OR
# tail -f /proc/mdstat

Update /etc/mdadm.conf File

Update or edit /etc/mdadm/mdadm.conf or /etc/mdadm.conf (distro specific location) file as follows:

ARRAY /dev/md0 devices=/dev/sdc1,/dev/sdd1 level=1 num-devices=2 auto=yes

This config file lists which devices may be scanned to see if they contain MD super block, and gives identifying information (e.g. UUID) about known MD arrays. Please note that Linux kernel v2.6.xx above can use both /dev/mdX or /dev/md/XX names. You can also create partitions for /dev/md/XX as /dev/md/d1/p2.

How Do I Get Information On Existing Array?

Type the following command
# mdadm --query /dev/md0
This will find out if a given device is a raid array, or is part of one, and will provide brief information about the device.

References:

  • See man pages: mdadm(8) and mdadm.conf(5)
  • RAID 5 vs RAID 10: Recommended RAID For Safety and Performance

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

32 comments… add one
  • krishna Mar 29, 2016 @ 16:07

    I’m not able to find the /etc/mdadm.conf or /etc/mdadm/mdadm.conf. why is it so ?

    /dev/md0:
    Version : 1.2
    Creation Time : Tue Mar 29 21:16:39 2016
    Raid Level : raid1
    Array Size : 20948288 (19.98 GiB 21.45 GB)
    Used Dev Size : 20948288 (19.98 GiB 21.45 GB)
    Raid Devices : 2
    Total Devices : 2
    Persistence : Superblock is persistent

    Update Time : Tue Mar 29 21:30:07 2016
    State : active
    Active Devices : 2
    Working Devices : 2
    Failed Devices : 0
    Spare Devices : 0

    Name : localhost.localdomain:0 (local to host localhost.localdomain)
    UUID : ff2aa97d:6aae2570:0d44f49a:735d94b9
    Events : 18

    Number Major Minor RaidDevice State
    0 8 17 0 active sync /dev/sdb1
    1 8 33 1 active sync /dev/sdc1

    [root@localhost etc]# ls -ld /etc/md*
    ls: cannot access /etc/md*: No such file or directory
    [root@localhost etc]#

    Personalities : [raid1]
    md0 : active raid1 sdc1[1] sdb1[0]
    20948288 blocks super 1.2 [2/2] [UU]

    unused devices:

  • DylanF May 4, 2016 @ 14:41

    I only ran into two problems, one, I had two different size HD’s, so I took the smallest one in the fdisk readout and copied the ‘end’ to the larger when fdisking it, and magic, the sizes are now the same.
    Second, I could not fdisk the partition id type fd. I found out, that I had to totally delete the existing partition and use the ‘Create a new label’ and use the ‘o’ to create a new empty DOS partition table, as the partition I was creating was a ‘g’ GPT partition for some unknown reason. Once I got past that, all is well, the finished partition in fdisk should look similar to this: “Device Boot Start End Sectors Size Id Type
    /dev/sda1 2048 1953458142 1953456095 931.5G fd Linux raid autodetect” , after getting the raid setup , I then was able to successfull for the first time ever, get DLNA working, woohoo !

    • DylanF May 4, 2016 @ 14:44

      Oh, and thanks for the instructions, it would have taken me quite a long time, if ever, for me to figure it all out. I did this on an Raspberry Pi vi win7 putty client.

  • krygz Oct 16, 2016 @ 14:36

    Hello,

    If I do RAID configuration using Setup Utility of my server, will I lose any existing data on the server?

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.