How to setup RAID on Centos


RAID (redundant array of independent/inexpensive disks) is used to provides a way of storing the same data in different places on multiple hard disks.

With the help of raid data on multiple disks, I/O operations on disk can be balanced and gives good performance.

Types of Raid:

Raid 0: Stripping ( Blocks are striped. No mirroring. No parity)
Raid 1: Mirroring ( Blocks are mirrored. No stripping. No parity)
Raid 5: Blocks are striped with distributed parity
Raid 10: Combination of mirroring and stripping

Here we are going to setup Raid 1 – Mirroring ( Blocks are mirrored. No stripping. No parity)

For this method, we need two hard disk (/dev/sdb and /dev/sdc). Because in Raid 1, the same data will be stored in both disk (datas are synched together).

So if eiher one disk fails then we can retrieve the data from another disk. There is no data loss in this method.

Step 1: Create two partition from each hard drive.

[root@server ~]# fdisk /dev/sdb

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').

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-130, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): +100M

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): fd
Changed system type of partition 1 to fd (Linux raid autodetect)

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x850dd785

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

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

[root@server ~]# fdisk /dev/sdc

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').

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-130, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): +100M

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): fd
Changed system type of partition 1 to fd (Linux raid autodetect)

Command (m for help): p

Disk /dev/sdc: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x4f9e5673

Device Boot Start End Blocks Id System
/dev/sdc1 1 14 112423+ fd Linux raid autodetect

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

[root@server ~]# mdadm -E /dev/sd{b,c}
/dev/sdb:
MBR Magic : aa55
Partition[0] : 224847 sectors at 63 (type fd)
/dev/sdc:
MBR Magic : aa55
Partition[0] : 224847 sectors at 63 (type fd)

Step 2: Create and format raid 1 file system

[root@server ~]# mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdc1
mdadm: Note: this array has metadata at the start and
may not be suitable as a boot device. If you plan to
store '/boot' on this device please ensure that
your boot-loader understands md/v1.x metadata, or use
--metadata=0.90
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

Check the status of Raid

[root@server ~]# cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 sdc1[1] sdb1[0]
112320 blocks super 1.2 [2/2] [UU]

unused devices:

Format the Raid File System

[root@server ~]# 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=0 blocks, Stripe width=0 blocks
28112 inodes, 112320 blocks
5616 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
14 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729

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

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

Step 3: Mount the raid file system and create some files

[root@server ~]# mkdir /raid1
[root@server ~]# mount /dev/md0 /raid1/
[root@server ~]# df -h /raid1/
Filesystem Size Used Avail Use% Mounted on
/dev/md0 107M 5.6M 96M 6% /raid1

[root@server ~]# cd /raid1/
[root@server raid1]# touch a{1,2,3}.txt
[root@server raid1]# echo "Hello World" > a1.txt
[root@server raid1]# cat a1.txt
Hello World

Step 4: View the detailed status of Raid

[root@server raid1]# mdadm --detail /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Thu May 14 11:32:16 2015
Raid Level : raid1
Array Size : 112320 (109.71 MiB 115.02 MB)
Used Dev Size : 112320 (109.71 MiB 115.02 MB)
Raid Devices : 2
Total Devices : 2
Persistence : Superblock is persistent

Update Time : Thu May 14 11:35:31 2015
State : clean
Active Devices : 2
Working Devices : 2
Failed Devices : 0
Spare Devices : 0

Name : server:0 (local to host server)
UUID : d83fb584:20e3cd26:488bd098:87c44063
Events : 17

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

So as of now, the files a1.txt, a2.txt, a3.txt are replicated in both disk /dev/sdb1 and /dev/sdc1.

Step 5: Corrupt one hard disk

“Please do not do this in your production environment”. As this is for example purpose i am doing it here.

Now we are going to corrupt /dev/sdc1 disk and to check the raid file system is working fine. To do this execute the below command.

[root@server raid1]# mdadm --manage --set-faulty /dev/md0 /dev/sdc1
mdadm: set /dev/sdc1 faulty in /dev/md0

[root@server raid1]# mdadm --detail /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Thu May 14 11:32:16 2015
Raid Level : raid1
Array Size : 112320 (109.71 MiB 115.02 MB)
Used Dev Size : 112320 (109.71 MiB 115.02 MB)
Raid Devices : 2
Total Devices : 2
Persistence : Superblock is persistent

Update Time : Thu May 14 11:39:29 2015
State : clean, degraded
Active Devices : 1
Working Devices : 1
Failed Devices : 1
Spare Devices : 0

Name : server:0 (local to host server)
UUID : d83fb584:20e3cd26:488bd098:87c44063
Events : 18

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

1 8 33 - faulty spare /dev/sdc1

Step 6: Remove the faulty raid disk

[root@server raid1]# mdadm /dev/md0 -r /dev/sdc1
mdadm: hot removed /dev/sdc1 from /dev/md0

After the disk removed, check whether we are able to retrieve the datas from /dev/sdb1 disk.

[root@server ~]# cat /raid1/a1.txt
Hello World

Step 7: Attach the new disk in existing Raid file sytem

For this create a new disk /dev/sdd and attach the same in existing raid.

[root@server ~]# fdisk /dev/sdd

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').

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (15-130, default 15):
Using default value 15
Last cylinder, +cylinders or +size{K,M,G} (15-130, default 130): +100M

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

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

[root@server ~]# mdadm /dev/md0 -a /dev/sdd1
mdadm: added /dev/sdd1

[root@server ~]# cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 sdd1[1] sdb1[0]
112320 blocks super 1.2 [2/2] [UU]

unused devices:

That’s it…. Hope you enjoy this discussion. Will keep you post some intersting topics.

Cheers 🙂