≡ Menu

CentOS / Redhat: Create Software RAID 1 Array

Recently, I've added another 73GB SAS disk to my Linux server after the installation. I've 30GB empty partition on old hard disk. How do I convert old /dev/sda3 and new /dev/sdb1 (both 30GB) into RAID 1 to improve NFS server speed and reliability?

RAID devices are virtual devices created from two or more real block devices. Linux supports RAID1 and other levels. You need to have same size partition on both disks i.e. on second disk create partitions exactly the same size as those on the first disk, and set the type as fd (Linux raid autodetect). You need to use the following commands to create RAID 1.

WARNING! These examples may crash your computer or may result into data loss if not executed properly. These examples involves disk partition manipulation using the fdisk command and building (formatting) file systems using the mkfs.ext3 command. So make sure you backup all data before typing any one of the following command.

Our sample RAID -1 setup with two partitions:

+------------+
|            |
| /dev/sda3  | ===============+
|            |                |
+------------+                |
  37GB                        | =======> RAID-1 /dev/md0
                              |
+------------+                |
|            |                |
| /dev/sdb1  | ===============+
|            |
+------------+
  37GB

Step # 1: List Partitions

Type the following commands:
# fdisk -l
# fdisk -l /dev/sda

Sample outputs:

Disk /dev/sda: 73.2 GB, 73295462400 bytes
255 heads, 63 sectors/track, 8910 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        3929    31455270   83  Linux
/dev/sda3            3930        8779    38957625   83  Linux
/dev/sda4            8780        8910     1052257+   5  Extended
/dev/sda5            8780        8910     1052226   82  Linux swap / Solaris

For example, purpose you will use /dev/sda3 (size 37G). Now, list second disk partitions, enter:
# fdisk -l /dev/sdb
Sample outputs:

Disk /dev/sdb: 73.2 GB, 73295462400 bytes
255 heads, 63 sectors/track, 8910 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System

Step # 2: Create 37G Partitions For /dev/sdb

Type the following command:
# fdisk /dev/sdb
Sample outputs:

The number of cylinders for this disk is set to 8910.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): 

To add a new partition, type n command followed by p command as follows:

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-8910, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-8910, default 8910): +37G

To print the partition table type p command, enter:

Command (m for help): p
Disk /dev/sdb: 73.2 GB, 73295462400 bytes
255 heads, 63 sectors/track, 8910 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          37      297171   83  Linux

Finally, set partition type to "Software Raid" i.e. type 0xfd *(type t command followed by fd code to change partition type to Software RAID):

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): fd
Command (m for help): p
Disk /dev/sdb: 73.2 GB, 73295462400 bytes
255 heads, 63 sectors/track, 8910 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          37      297171   fd  Linux raid autodetect

Save the changes and exit to shell prompt by typing w command:

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

Step # 3: Create / Update 37G Partitions For /dev/sda

You need to delete /dev/sda3 (make sure you backup all data) and/or set it to software raid as follows using the fdisk command:
# fdisk /dev/sda
Sample outputs:

The number of cylinders for this disk is set to 8910.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): p
Disk /dev/sda: 73.2 GB, 73295462400 bytes
255 heads, 63 sectors/track, 8910 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        3929    31455270   83  Linux
/dev/sda3            3930        8779    38957625   83  Linux
/dev/sda4            8780        8910     1052257+   5  Extended
/dev/sda5            8780        8910     1052226   82  Linux swap / Solaris
Command (m for help): t
Partition number (1-5): 3
Hex code (type L to list codes): fd
Changed system type of partition 3 to fd (Linux raid autodetect)
Command (m for help): p
Disk /dev/sda: 73.2 GB, 73295462400 bytes
255 heads, 63 sectors/track, 8910 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        3929    31455270   83  Linux
/dev/sda3            3930        8779    38957625   fd  Linux raid autodetect
/dev/sda4            8780        8910     1052257+   5  Extended
/dev/sda5            8780        8910     1052226   82  Linux swap / Solaris
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

Reload Partition Table Changes

Use the partprobe command to reload the changes without rebooting the box:
# partprobe /dev/sda
OR
# echo 1 > /sys/block/sdb/device/rescan

Step #3: Create RAID 1 Array

To create RAID1 using /dev/sda3 and /dev/sdb1, enter:
# mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sda3 /dev/sdb1
To see RAID build progress, enter:
# watch cat /proc/mdstat
Sample outputs:

Fig.01: RAID 1 mirroring

Fig.01: RAID 1 mirroring


Once build completed create a filesystem on the new software raid devices, enter:
# mkfs.ext3 /dev/md0

mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
4872896 inodes, 9737360 blocks
486868 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
298 block groups
32768 blocks per group, 32768 fragments per group
16352 inodes per group
Superblock backups stored on blocks:
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
	4096000, 7962624
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 39 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

Mount RAID1 at /raid1

Type the following commands:
# mkdir /raid1
# mount /dev/md0 /raid1/
# df -H /raid1/

Sample outputs:

Filesystem             Size   Used  Avail Use% Mounted on
/dev/md0                40G   185M    38G   1% /raid1

How Do I Test My RAID 1 Array Speed?

Simply use the dd command as follows:
# cd /raid1/
# dd if=/dev/zero of=output bs=8k count=256k
# rm output

262144+0 records in
262144+0 records out
2147483648 bytes (2.1 GB) copied, 6.01068 seconds, 357 MB/s

Update /etc/fstab

Make sure RAID1 get mounted when you reboot the system (note >> append redirection):
# echo '/dev/md0 /raid1 ext3 noatime,rw 0 0' >> /etc/fstab

How Do I Get Detailed Information About RAID 1?

Type the following command:
# mdadm --query --detail /dev/md0
Sample outputs:

/dev/md0:
        Version : 0.90
  Creation Time : Wed May 26 01:58:53 2010
     Raid Level : raid1
     Array Size : 38949440 (37.15 GiB 39.88 GB)
  Used Dev Size : 38949440 (37.15 GiB 39.88 GB)
   Raid Devices : 2
  Total Devices : 2
Preferred Minor : 0
    Persistence : Superblock is persistent
    Update Time : Wed May 26 02:18:22 2010
          State : clean
 Active Devices : 2
Working Devices : 2
 Failed Devices : 0
  Spare Devices : 0
           UUID : b6f207e4:7f73484f:53f64bfd:e70b77d8
         Events : 0.2
    Number   Major   Minor   RaidDevice State
       0       8        3        0      active sync   /dev/sda3
       1       8       17        1      active sync   /dev/sdb1
Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 19 comments… add one }

  • kubrick May 27, 2010, 2:15 pm

    Nice article.
    Is it useful for a Debian based system ?
    Thanks.

    • nixCraft May 27, 2010, 2:32 pm

      Yes, the commands are same for all modern Linux distro. Under Debian / Ubuntu (and few more distro) you may to edit /etc/mdadm/mdadm.conf as follows and rest is same:

      ARRAY /dev/md0 devices=/dev/sdXY,/dev/sdAB level=1 num-devices=2 auto=yes
      

      HTH

      • Praveen Jerald April 19, 2011, 10:33 am

        mdadm –create /dev/md0 –level 0 –raid-devices=2 /dev/sda3 /dev/sdb1

    • V.Senthil Kumar August 30, 2010, 12:47 pm

      Great worked Keep it up

  • vishal vyas June 10, 2010, 1:34 pm

    hi
    greate yar ……..

    thnks for share….

  • wilson August 19, 2010, 6:26 pm

    Great Share!
    Thank you!

  • Md. Nazamul Hoque January 5, 2011, 6:48 am

    This tutorial is great…
    I had also created two partition..such /dev/hda5 and /dev/hda6
    and by this 2 partition make a raid partition /dev/md0 and mount this partition in /root/raid1 directory.
    At this time i created some files and folder in this directory where my raid partition mounted.
    But i want to see how my RAID1 is working. For that i try to mount my /dev/hda5 and /dev/hda6 partition in another two directory such as /root/part1 and /root/part2. But i am failed. show me /dev/hda5 and /dev/hda6 is already mounted or /root/part1 and /root//part2 is so busy..
    what can i do now..pls help me…
    i want to see real scenario of RAID1

  • Daison Antony April 6, 2011, 9:30 am

    great yaar .

    if faliure of one Hard disk , how we rebuild RAID 1 with new HDD

  • Alex November 21, 2011, 8:10 am

    Thank you, great tutorial. I followed it and everything worked. But the transfer speed using the test from the “How Do I Test My RAID 1 Array Speed?” shows me the 10Mb/s. And it took 24 hours to create the raid1 with mdadm. I’m using eSATA pcmcia card with two 1Tb external esata drives. I don’t know if this is eSATA controller issue or the drives. How can I further test it?

  • Flavio April 17, 2012, 12:48 pm

    Time Remaining to Create RAID 1 Array
    1800min = 30h

    Why ???
    Is it Correct ?

    TKs

  • Flavio May 2, 2012, 1:01 pm

    This tutorial has one IMPORTANT part this one hasn’t.

    Configure MDAM.CONF file to doesnt lost the array After format the set (mkfs.ext3 /dev/md0) you did this:

    mdadm --detail --scan > /etc/mdadm.conf
  • Navid October 6, 2012, 1:27 am

    Hi ,

    **** Please Help Me ****

    I face with an issue.
    When I put this command:

    # mdadm –create /dev/md0 –level=0 –raid-devices=2 /dev/sda2 /dev/sdb1

    I receive :
    mdadm: Cannot open /dev/sda2: Device or resource busy
    mdadm: create aborted

    Please note that there are two 2TB HDD on my server and I’m going to do RAID0 between: sda2 and sdb1

    Here is my HDD:

    fdisk -l

    Disk /dev/sda: 2000.3 GB, 2000398934016 bytes
    255 heads, 63 sectors/track, 243201 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes

    Device Boot Start End Blocks Id System
    /dev/sda1 * 1 13 104391 83 Linux
    /dev/sda2 14 242418 1947118162+ fd Linux raid autodetect
    /dev/sda3 242419 242940 4192965 82 Linux swap / Solaris
    /dev/sda4 242941 243201 2096482+ 5 Extended
    /dev/sda5 242941 243201 2096451 83 Linux

    Disk /dev/sdb: 2000.3 GB, 2000398934016 bytes
    255 heads, 63 sectors/track, 243201 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes

    Device Boot Start End Blocks Id System
    /dev/sdb1 1 243154 1953134473+ fd Linux raid autodetect

    Please help me :(

    • vahid February 16, 2013, 10:17 am

      Hi navid,

      when the device in use or mounted you can’t use for raid.
      you can use a device that not mount and use.

      nice day

  • Prashant December 14, 2012, 9:23 am

    Very NiceBlog..usefull

  • Miguel Ramirez May 14, 2014, 9:41 pm

    Very useful. The clue was NOT to use the FakeRAID form the motherboard.

  • clark kent rosal September 16, 2014, 3:03 pm

    the best site tutorial ever…every tutorial was so cleared and detailed keep it up guys :)(Y)

  • David Ramirez March 5, 2015, 2:18 pm

    Great how-to, thanks!
    Just made a variation of this on a CentOS 6.6 system which received 2 extra disks after OS installation. All was OK, but I was surprised that the /dev/md0 was later (by itself) renamed to /dev/md127 after reboot and thus the /etc/fstab entry didn’t work. Editing the file to update that fixed the issue.

Leave a Comment