Configuring software RAID (Level 1) on Linux

Introduction

This post is about creating a software RAID using the existing partitions. Usually for RAID device we should use different disk. But here since I don’t have that much hardware available, use partitions to demonstrate the software RAID configuration.

There are different level of RAID available. We can create level 0, level 1 and level 5 RAID using software RAID.

Following table gives a brief idea about different RAID level possible by software RAID.

Assuming that each disk is of 10G here we can have

RAID Level Min # Disk Required Effective Storage Technique
Level 0 2 20G Stripping
Level 1 2 10G Mirroring
Level 5 3 20G Stripping with parity
Level 6 6 20G Stripping with multiple parity

Configuring RAID

Following are the simple steps to be done as a system administrator to configure software RAID. I am creating Level 1 RAID here.

Step 1) Creating RAID partitions

RAID partitions are of type fd (Linux RAID Autodetect). Creating 5 partitions of type fd, each of size 1G.

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

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System

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

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): L

0  Empty           1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot
1  FAT12           24  NEC DOS         81  Minix / old Lin bf  Solaris
2  XENIX root      39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
3  XENIX usr       3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
4  FAT16 <32M      40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
5  Extended        41  PPC PReP Boot   85  Linux extended  c7  Syrinx
6  FAT16           42  SFS             86  NTFS volume set da  Non-FS data
7  HPFS/NTFS       4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
8  AIX             4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility
9  AIX bootable    4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt
a  OS/2 Boot Manag 50  OnTrack DM      93  Amoeba          e1  DOS access
b  W95 FAT32       51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O
c  W95 FAT32 (LBA) 52  CP/M            9f  BSD/OS          e4  SpeedStor
e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs
f  W95 Ext’d (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  EFI GPT
10  OPUS            55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor
14  Hidden FAT16 ❤ 61  SpeedStor       a9  NetBSD          f4  SpeedStor
16  Hidden FAT16    63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fd  Linux raid auto
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fe  LANstep
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid ff  BBT
1c  Hidden W95 FAT3 75  PC/IX
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: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         123      987966   fd  Linux raid autodetect

Like wise create 4 more partition

at the end it should look like as shown below

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         123      987966   fd  Linux raid autodetect
/dev/sdb2             124         246      987997+  fd  Linux raid autodetect
/dev/sdb3             247         369      987997+  fd  Linux raid autodetect
/dev/sdb4             370         652     2273197+   5  Extended
/dev/sdb5             370         492      987966   fd  Linux raid autodetect
/dev/sdb6             493         615      987966   fd  Linux raid autodetect

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

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

Step 2) Running partprobe

Running partprobe will ask kernel to read the partition table from disk into memory, so that new partition will be in effect.

[root@localhost ~]# partprobe
Warning: Unable to open /dev/hdc read-write (Read-only file system).  /dev/hdc has been opened read-only.
[root@localhost ~]# cat /proc/partitions
major minor  #blocks  name

8     0   10485760 sda
8     1     104391 sda1
8     2    3068415 sda2
8     3    3068415 sda3
8     4          1 sda4
8     5    1020096 sda5
8     6    1020096 sda6
8     7     514048 sda7
8     8     987966 sda8
8    16    5242880 sdb
8    17     987966 sdb1
8    18     987997 sdb2
8    19     987997 sdb3
8    20          1 sdb4
8    21     987966 sdb5
8    22     987966 sdb6

Step 3) Create a RAID device of defined level.

Here we are using /dev/sdb1 and /dev/sdb2 partition to create Level 1 RAID.

[root@localhost ~]# mdadm -C /dev/md0 -a yes -l 1 -n 2 /dev/sdb{1,2}
mdadm: array /dev/md0 started.

Here mdadm is the command to create raid device
-C is the create option
/dev/md0 is the device name
-a yes option is to create a RAID file if it doesnt exists
-l 1 is the RAID level 1 (mirroring)
-n 2 is number of device 2
/dev/sdb{1,2} are the device names (/dev/sdb1, /dev/sdb2)

Step 4) Format the RAID device

Once the RAID device is created, next step is to format the device. We will format for ext3 type.

[root@localhost ~]# 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)
123648 inodes, 246960 blocks
12348 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=255852544
8 block groups
32768 blocks per group, 32768 fragments per group
15456 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376

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

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

Step 5) Last step is mounting the file system

[root@localhost ~]# mkdir /raid1
[root@localhost ~]# mount /dev/md0 /raid1
[root@localhost ~]# cd /raid1
[root@localhost raid1]# df -h .

Filesystem            Size  Used Avail Use% Mounted on
/dev/md0              950M   18M  885M   2% /raid1

As we know that RAID 1 is mirroring, so effective disk space is half that of the provided disk. Here we provided 2 disk partitions of 1G each. So the effective disk space is approx 1G.

You can see the details of RAID device using mdadm –detail command as shown below

[root@localhost raid1]# mdadm –detail /dev/md0
/dev/md0:
Version : 00.90.03
Creation Time : Tue Sep 16 08:59:31 2008
Raid Level : raid1
Array Size : 987840 (964.85 MiB 1011.55 MB)
Device Size : 987840 (964.85 MiB 1011.55 MB)
Raid Devices : 2
Total Devices : 2
Preferred Minor : 0
Persistence : Superblock is persistent

Update Time : Tue Sep 16 09:02:11 2008
State : clean
Active Devices : 2
Working Devices : 2
Failed Devices : 0
Spare Devices : 0

UUID : dcf37c14:179f9a7a:ed1f46c6:a8160267
Events : 0.2

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

Step 6) Adding a RAID device to /etc/fstab

[root@localhost ~]# cat /etc/fstab | grep md
/dev/md0                /raid1                  ext3    defaults        0 0

Next post, we will simulate the failure of one of the RAID disk and try to replace the same.

Hope this helps !!

Advertisement