Linux RAID 5 – Redundant Array of Inexpensive Disks

Hi Unixmen readers, I am Vishal Vyas, a Linux System Administrator from India. I am new here, so please be nice with me.

In this quick how-to, let us see how to setup Raid level 5 on Linux. For more details about RAID 5, please refer this link.  Linux RAID 5 requires at least three disks.

RAID5Image credit: Wikipedia

First let us create 3 Partitions.

Create Partitions

To view the existing partitions use following command:

# fdisk -l

Sample output:

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 102400 7 HPFS/NTFS
Partition 1 does not end on cylinder boundary.
/dev/sda2 13 12748 102294528 7 HPFS/NTFS
/dev/sda3 12749 12773 200812+ 83 Linux
/dev/sda4 12774 60802 385786880 5 Extended
/dev/sda5 12774 12799 204800 83 Linux
/dev/sda6 12799 19174 51200000 83 Linux
/dev/sda7 19174 19429 2048000 82 Linux swap / Solaris

Now let us create three partions of each size 100MB using fdisk command

Use fdisk command to create and manage partitions

# fdisk /dev/sda
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

To display existing partition table:

Command (m for help): p
Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa54ba54b
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 102400 7 HPFS/NTFS
Partition 1 does not end on cylinder boundary.
/dev/sda2 13 12748 102294528 7 HPFS/NTFS
/dev/sda3 12749 12773 200812+ 83 Linux
/dev/sda4 12774 60802 385786880 5 Extended
/dev/sda5 12774 12799 204800 83 Linux
/dev/sda6 12799 19174 51200000 83 Linux
/dev/sda7 19174 19429 2048000 82 Linux swap / Solaris

Now let us create three partions of each size 100MB using fdisk command.

To Create new partition press n and enter the size (ex.+100M).

Command (m for help): n
 First cylinder (19429-60802, default 19429):
 Using default value 19429
 Last cylinder, +cylinders or +size{K,M,G} (19429-60802, default 60802): +100M
Command (m for help): n
 First cylinder (19443-60802, default 19443):
 Using default value 19443
 Last cylinder, +cylinders or +size{K,M,G} (19443-60802, default 60802): +100M
Command (m for help): n
 First cylinder (19457-60802, default 19457):
 Using default value 19457
 Last cylinder, +cylinders or +size{K,M,G} (19457-60802, default 60802): +100M

Change partition type

Press t to change the partition id of all the three partitions created by you to fd (linux raid auto).

Command (m for help): t
 Partition number (1-10): 8
 Hex code (type L to list codes): fd
 Changed system type of partition 8 to fd (Linux raid autodetect)
Command (m for help): t
 Partition number (1-10): 9
 Hex code (type L to list codes): fd
 Changed system type of partition 9 to fd (Linux raid autodetect)
Command (m for help): t
 Partition number (1-10): 10
 Hex code (type L to list codes): fd
 Changed system type of partition 10 to fd (Linux raid autodetect)

To Show partition table, press P :

Command (m for help): p
Disk /dev/sda: 500.1 GB, 500107862016 bytes
 255 heads, 63 sectors/track, 60801 cylinders
 Units = cylinders of 16065 * 512 = 8225280 bytes
 Sector size (logical/physical): 512 bytes / 512 bytes
 I/O size (minimum/optimal): 512 bytes / 512 bytes
 Disk identifier: 0xa54ba54b

Device Boot Start End Blocks Id System
 /dev/sda1 * 1 13 102400 7 HPFS/NTFS
 Partition 1 does not end on cylinder boundary.
 /dev/sda2 13 12748 102294528 7 HPFS/NTFS
 /dev/sda3 12749 12773 200812+ 83 Linux
 /dev/sda4 12774 60802 385786880 5 Extended
 /dev/sda5 12774 12799 204800 83 Linux
 /dev/sda6 12799 19174 51200000 83 Linux
 /dev/sda7 19174 19429 2048000 82 Linux swap / Solaris
 /dev/sda8 19429 19442 111257+ fd Linux raid autodetect
 /dev/sda9 19443 19456 112423+ fd Linux raid autodetect
 /dev/sda10 19457 19470 112423+ fd Linux raid autodetect

Save the newly created partitions.

Command (m for help): wq [Save change]

The partition table has been altered!
Calling ioctl() to re-read partition table.

Update the kernel to save the changes without restarting the system.

# partprobe
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy). As a result, it may not reflect all of your changes until after reboot.
Again we will check the existing partitions using fdisk command.

Again we will check the existing partitions using  fdisk command

# fdisk -l

Sample output:

 Device Boot Start End Blocks Id System
 /dev/sda1 * 1 13 102400 7 HPFS/NTFS
 Partition 1 does not end on cylinder boundary.
 /dev/sda2 13 12748 102294528 7 HPFS/NTFS
 /dev/sda3 12749 12773 200812+ 83 Linux
 /dev/sda4 12774 60802 385786880 5 Extended
 /dev/sda5 12774 12799 204800 83 Linux
 /dev/sda6 12799 19174 51200000 83 Linux
 /dev/sda7 19174 19429 2048000 82 Linux swap / Solaris
 /dev/sda8 19429 19442 111257+ fd Linux raid autodetect
 /dev/sda9 19443 19456 112423+ fd Linux raid autodetect
 /dev/sda10 19457 19470 112423+ fd Linux raid autodetect.

Creating RAID 5 with these partitions

# mdadm --create /dev/md0 --level=5 --raid-devices=3 /dev/sda8 /dev/sda9 /dev/sda10
 mdadm: Defaulting to version 1.2 metadata
 mdadm: array /dev/md0 started.

To see the details of raid use the following command:

# cat /proc/mdstat

Sample output:

 Personalities : [raid6] [raid5] [raid4]
 md0 : active raid5 sda10[3] sda9[1] sda8[0]
 222208 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]

To show raid devices:

# mdadm --detail /dev/md0

Sample output:

/dev/md0:
Version : 1.2
Creation Time : Thu Jan 30 19:41:32 2014
Raid Level : raid5
Array Size : 222208 (217.04 MiB 227.54 MB)
Used Dev Size : 111104 (108.52 MiB 113.77 MB)
Raid Devices : 3
Total Devices : 3
Persistence : Superblock is persistent
Update Time : Thu Jan 30 19:41:42 2014
State : clean
Active Devices : 3
Working Devices : 3
Failed Devices : 0
Spare Devices : 0
Layout : left-symmetric
Chunk Size : 512K
Name : localhost.localdomain:0 (local to host localhost.localdomain)
UUID : a4caa02d:8e6fd77c:e83fcdea:d471cee6
Events : 18
Number Major Minor RaidDevice State
0 8 8 0 active sync /dev/sda8
1 8 9 1 active sync /dev/sda9
3 8 10 2 active sync /dev/sda10

Now Format file system for your RAID devices

# mkfs.ext4 /dev/md0
 mke2fs 1.41.12 (17-May-2010)
 Filesystem label=
 OS type: Linux
 Block size=1024 (log=0)
 Fragment size=1024 (log=0)
 Stride=512 blocks, Stripe width=1024 blocks
 55552 inodes, 222208 blocks
 11110 blocks (5.00%) reserved for the super user
 First data block=1
 Maximum filesystem blocks=67371008
 28 block groups
 8192 blocks per group, 8192 fragments per group
 1984 inodes per group
 Superblock backups stored on blocks:
 8193, 24577, 40961, 57345, 73729, 204801, 221185

Writing inode tables: done
 Creating journal (4096 blocks): done
 Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 22 mounts or
 180 days, whichever comes first. Use tune2fs -c or -i to override.

Mounting the RAID partition

Once you create all of your partitions as Software RAID partitions, you must create the RAID device and mount point.

# mkdir raid5
# mount /dev/md0 raid5
# df -h ## Command is used to see the space allocation ##
Filesystem Size Used Avail Use% Mounted on
/dev/sda6 49G 21G 28G 44% /
tmpfs 999M 88K 999M 1% /dev/shm
/dev/sda5 194M 28M 156M 16% /boot
/dev/md0 211M 6.1M 194M 4% /root/raid5

Manage faulty raid devices

Let us damage any raid partitions.

# mdadm --manage /dev/md0 --fail /dev/sda9
 mdadm: set /dev/sda9 faulty in /dev/md0

Removing raid devices

# mdadm --manage /dev/md0 --remove /dev/sda9
 mdadm: hot removed /dev/sda9 from /dev/md0

Adding raid devices

# mdadm --manage /dev/md0 --add /dev/sda9
mdadm: added /dev/sda9

View failed and working raid devices

# cat /proc/mdstat
 Personalities : [raid6] [raid5] [raid4]
 md0 : active raid5 sda9[4] sda10[3] sda8[0]
 222208 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]
 unused devices:

To remove the RAID follow these steps: –

1) unmount the mounted directory where raid is mounted.

# umount data/

2) Stop the device

# mdadm --stop /dev/md0
 mdadm: stopped /dev/md0

3) View the details of your raid level using following command: –

# cat /proc/mdstat
# mdadm --detail /dev/md0

Learn more