一块新增的7G硬盘,将其分成6个分区,每个分区1G空间。sdb{1,2,3,5,6,7}就是我们接下来实现RAID所要用到的分区。用sdb{1,2,3,5}四个分区组成RAID5,利用sdb6作为sparedisk,sdb7作为备用分区。
一、 mdadm命令创建RAID
1.mdadm命令用于创建软件磁盘阵列,命令格式为:mdadm [模式选项] [RAID设备名] [子选项…] [组件设备名…]
--create为mdadm的创建模式
md0为创建的磁盘阵列名
--level=[015]设置磁盘阵列的等级,我们这里设置为5
--raid-devices=N表明使用几块磁盘作为磁盘阵列使用的设备,我们这里使用了4块,也就是sdb{1,2,3,5}
--spare-devices=N表明使用几块磁盘作为备用设备,我们这里使用了一块,也就是sdb6
2.查看md0阵列的详细信息
[root@minimal ~]# mdadm --detail /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Wed Oct 21 11:36:44 2015
Raid Level : raid5
Array Size : 3176448 (3.03 GiB 3.25 GB)
Used Dev Size : 1058816 (1034.17 MiB 1084.23 MB)
Raid Devices : 4
Total Devices : 5
Persistence : Superblock is persistent
Update Time : Wed Oct 21 11:36:58 2015
State : clean
Active Devices : 4
Working Devices : 5
Failed Devices : 0
Spare Devices : 1
Layout : left-symmetric
Chunk Size : 512K
Name : minimal:0 (local to host minimal)
UUID : f99d657f:0db77911:9870b07b:92c4c1d9
Events : 18
Number Major Minor RaidDevice State
0 8 17 0 active sync /dev/sdb1
1 8 18 1 active sync /dev/sdb2
2 8 19 2 active sync /dev/sdb3
5 8 21 3 active sync /dev/sdb5
4 8 22 - spare /dev/sdb6
3.格式化并且挂载RAID
4.记录阵列信息,防止系统重启后阵列丢失
[root@minimal ~]# mdadm -Ds >> /etc/mdadm.conf
5.查看配置文件里面的信息
[root@minimal ~]# cat /etc/mdadm.conf
ARRAY /dev/md0 metadata=1.2 spares=1 name=minimal:0 UUID=f99d657f:0db77911:9870b07b:92c4c1d9
二、模拟磁盘阵列出错并且恢复
1.设置sdb5出错
[root@minimal ~]# mdadm --manage /dev/md0 --fail /dev/sdb5
2.查看磁盘阵列当前信息
[root@minimal ~]# mdadm --detail /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Wed Oct 21 11:36:44 2015
Raid Level : raid5
Array Size : 3176448 (3.03 GiB 3.25 GB)
Used Dev Size : 1058816 (1034.17 MiB 1084.23 MB)
Raid Devices : 4
Total Devices : 5
Persistence : Superblock is persistent
Update Time : Wed Oct 21 12:02:50 2015
State : clean, degraded, recovering
Active Devices : 3
Working Devices : 4
Failed Devices : 1
Spare Devices : 1
Layout : left-symmetric
Chunk Size : 512K
Rebuild Status : 61% complete
Name : minimal:0 (local to host minimal)
UUID : f99d657f:0db77911:9870b07b:92c4c1d9
Events : 29
Number Major Minor RaidDevice State
0 8 17 0 active sync /dev/sdb1
1 8 18 1 active sync /dev/sdb2
2 8 19 2 active sync /dev/sdb3
4 8 22 3 spare rebuilding /dev/sdb6
5 8 21 - faulty /dev/sdb5
快速输入mdadm命令可以看到sdb6正在重建数据。如果过一段时间再使用mdadm查看的话会产生下面的结果。
[root@minimal ~]# mdadm --detail /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Wed Oct 21 11:36:44 2015
Raid Level : raid5
Array Size : 3176448 (3.03 GiB 3.25 GB)
Used Dev Size : 1058816 (1034.17 MiB 1084.23 MB)
Raid Devices : 4
Total Devices : 5
Persistence : Superblock is persistent
Update Time : Wed Oct 21 12:02:56 2015
State : clean
Active Devices : 4
Working Devices : 4
Failed Devices : 1
Spare Devices : 0
Layout : left-symmetric
Chunk Size : 512K
Name : minimal:0 (local to host minimal)
UUID : f99d657f:0db77911:9870b07b:92c4c1d9
Events : 37
Number Major Minor RaidDevice State
0 8 17 0 active sync /dev/sdb1
1 8 18 1 active sync /dev/sdb2
2 8 19 2 active sync /dev/sdb3
4 8 22 3 active sync /dev/sdb6
5 8 21 - faulty /dev/sdb5
3.删除出错的磁盘并且加入新的磁盘
[root@minimal ~]# mdadm --manage /dev/md0 --remove /dev/sdb5 --add /dev/sdb7
命令执行成功后状态:
4.查看当前磁盘阵列信息
[root@minimal ~]# mdadm --detail /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Wed Oct 21 11:36:44 2015
Raid Level : raid5
Array Size : 3176448 (3.03 GiB 3.25 GB)
Used Dev Size : 1058816 (1034.17 MiB 1084.23 MB)
Raid Devices : 4
Total Devices : 5
Persistence : Superblock is persistent
Update Time : Wed Oct 21 12:08:34 2015
State : clean
Active Devices : 4
Working Devices : 5
Failed Devices : 0
Spare Devices : 1
Layout : left-symmetric
Chunk Size : 512K
Name : minimal:0 (local to host minimal)
UUID : f99d657f:0db77911:9870b07b:92c4c1d9
Events : 39
Number Major Minor RaidDevice State
0 8 17 0 active sync /dev/sdb1
1 8 18 1 active sync /dev/sdb2
2 8 19 2 active sync /dev/sdb3
4 8 22 3 active sync /dev/sdb6
5 8 23 - spare /dev/sdb7
三、磁盘阵列扩容
1.向raid5中添加新设备(分区或者磁盘)
[root@minimal ~]# mdadm /dev/md0 --add /dev/sdb5
mdadm: added /dev/sdb5
2.改变raid5工作盘的个数
[root@minimal ~]# mdadm --grow /dev/md0 --raid-devices=5
mdadm: Need to backup 6144K of critical section..
3.调整文件系统大小
[root@minimal ~]# resize2fs /dev/md0
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/md0 is mounted on /mnt/raid; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/md0 to 1058816 (4k) blocks.
The filesystem on /dev/md0 is now 1058816 blocks long.
使用df查看挂载设备,会发现md0已经增加了1G的空间
[root@minimal ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_minimal-LogVol00
18G 1.5G 15G 9% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 477M 61M 391M 14% /boot
/dev/md0 4.0G 6.1M 3.8G 1% /mnt/raid
4.查看当前磁盘阵列信息
[root@minimal ~]# mdadm --detail /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Wed Oct 21 11:36:44 2015
Raid Level : raid5
Array Size : 3176448 (3.03 GiB 3.25 GB)
Used Dev Size : 1058816 (1034.17 MiB 1084.23 MB)
Raid Devices : 5
Total Devices : 6
Persistence : Superblock is persistent
Update Time : Wed Oct 21 12:31:14 2015
State : clean, reshaping
Active Devices : 5
Working Devices : 6
Failed Devices : 0
Spare Devices : 1
Layout : left-symmetric
Chunk Size : 512K
Reshape Status : 33% complete
Delta Devices : 1, (4->5)
Name : minimal:0 (local to host minimal)
UUID : f99d657f:0db77911:9870b07b:92c4c1d9
Events : 67
Number Major Minor RaidDevice State
0 8 17 0 active sync /dev/sdb1
1 8 18 1 active sync /dev/sdb2
2 8 19 2 active sync /dev/sdb3
4 8 22 3 active sync /dev/sdb6
6 8 21 4 active sync /dev/sdb5
5 8 23 - spare /dev/sdb7
[root@minimal ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sdb5[6] sdb7[5](S) sdb6[4] sdb3[2] sdb2[1] sdb1[0]
4235264 blocks super 1.2 level 5, 512k chunk, algorithm 2 [5/5] [UUUUU]
unused devices:
四、关闭软件磁盘阵列
1.卸载磁盘并且删除与磁盘挂载的内容
[root@minimal ~]# umount /dev/md0
如果设置了磁盘整列开机自动挂载的话,还需要进入/etc/fstab删除自动挂载的内容
2.停止磁盘阵列服务
[root@minimal ~]# mdadm --stop /dev/md0
mdadm: stopped /dev/md0
3.删除磁盘阵列配置文件
[root@minimal ~]# vim /etc/mdadm.conf
ARRAY /dev/md0 metadata=1.2 spares=1 name=minimal:0 UUID=f99d657f:0db77911:9870b07b:92c4c1d9 #删除或者注释掉