Last updated on May 21st, 2016 at 11:33 pm
How to create Software Raid 1 in Linux(Mirroring)
This is a step by step tutorial on how to create software raid 1 (Mirroring). As you are aware Raid 1 means it mirrors all the data written in say disk 1 to disk 2. So apparently if you have 2 disk with 20 GB storage, Once you have configured raid 1 then in effect you will get storage capacity of only 20 GB and not 40 GB.
Commands we are going to use
- fdisk for manipulating disk partition
- mdadm for managing MD device also known as Linux software raid.
Let us start :- I am having 2 disk of 8GB in size, named xvdb and xvdc
xvdb 202:16 0 8G 0 disk
xvdc 202:32 0 8G 0 disk
First let us examine the disk using mdadm
[root@ip-172-31-19-221 ~]# mdadm --examine /dev/xvdc /dev/xvdb mdadm: No md superblock detected on /dev/xvdc. mdadm: No md superblock detected on /dev/xvdb.
Next step is to use fdisk command to change type of partition from ‘Linux‘ to ‘Linux raid autodetect‘ using hexcode fd
On disk /dev/xvdc
[root@ip-172-31-19-221 ~]# fdisk /dev/xvdc Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0x0b8b7316. Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): First sector (2048-16777215, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-16777215, default 16777215): Using default value 16777215 Partition 1 of type Linux and of size 8 GiB is set Command (m for help): t Selected partition 1 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
On disk /dev/xvdb
[root@ip-172-31-19-221 ~]# fdisk /dev/xvdb Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0x0b8b7316. Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): First sector (2048-16777215, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-16777215, default 16777215): Using default value 16777215 Partition 1 of type Linux and of size 8 GiB is set Command (m for help): t Selected partition 1 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): p Disk /dev/xvdc: 8589 MB, 8589934592 bytes, 16777216 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x0b8b7316 Device Boot Start End Blocks Id System /dev/xvdc1 2048 16777215 8387584 fd Linux raid autodetect Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
Now we have created Linux raid autodetect for both disk. The next step is to use mdadm to create a Raid 1 software configuration.
[root@ip-172-31-19-221 ~]# mdadm --create /dev/md1 --level=1 --raid-devices=2 /dev/xvdb1 /dev/xvdc1 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/md1 started. [root@ip-172-31-19-221 ~]#
Since the new raid device md1 is not formatted to any File system type we can use mkfs.ext3 for making it a ext3 type file system. Then I am mounting the device to a mount point named myraid1
[root@ip-172-31-19-221 ~]# mkfs.ext3 /dev/md1 mke2fs 1.42.12 (29-Aug-2014) Creating filesystem with 2095856 4k blocks and 524288 inodes Filesystem UUID: 76007c40-036f-404d-8672-adf07f276fc1 Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done [root@ip-172-31-19-221 ~]# mkdir /myraid1 [root@ip-172-31-19-221 ~]# mount /dev/md1 /myraid1 [root@ip-172-31-19-221 ~]# df -H Filesystem Size Used Avail Use% Mounted on /dev/xvda1 8.4G 1.2G 7.1G 15% / devtmpfs 513M 82k 513M 1% /dev tmpfs 522M 0 522M 0% /dev/shm /dev/md1 8.4G 19M 7.9G 1% /myraid1 [root@ip-172-31-19-221 ~]#
Let us check the status of the newly created Raid
[root@ip-172-31-19-221 myraid1]# mdadm --detail /dev/md1 /dev/md1: Version : 1.2 Creation Time : Sun Jan 3 19:00:31 2016 Raid Level : raid1 Array Size : 8383424 (8.00 GiB 8.58 GB) Used Dev Size : 8383424 (8.00 GiB 8.58 GB) Raid Devices : 2 Total Devices : 2 Persistence : Superblock is persistent Update Time : Sun Jan 3 19:04:17 2016 State : clean Active Devices : 2 Working Devices : 2 Failed Devices : 0 Spare Devices : 0 Name : ip-172-31-19-221:1 (local to host ip-172-31-19-221) UUID : e37f36f8:ed6af148:59e4ff44:87f11299 Events : 19 Number Major Minor RaidDevice State 0 202 17 0 active sync /dev/sdb1 1 202 33 1 active sync /dev/sdc1 [root@ip-172-31-19-221 myraid1]#
The main advantage of using Raid 1 is that it offers excellent Read / Write speed compared to having a single disk. Data can also be easily rebuilt. For a Raid 1 setup we should have at least 2 disk.