1、RAID概述
RAID,Redundant Arrays of Inexpensive(Independent)Disks的简称,独立磁盘冗余阵列,简称磁盘阵列。简单来说,RAID是把多个硬盘组合成为一个逻辑扇区,因此,操作系统只会把它当作一个硬盘,经常被用在服务器电脑上。不同的RAID实现的功能也是不尽相同的,或增强处理速度,或增强容错能力,当然,更有甚者,是可以兼备这个特性的。
经典RAID5的实现
如果只是用作测试的话,软RAID可以在一块硬盘的不同分区上实现,此处用不同的硬盘实现
<1>添加五块硬盘,四块做成RAID阵列,一块做spare硬盘
<2>创建文件系统,并调整分区类型为fd
[root@CentOS7~]#fdisk /dev/sdb
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.
Command (m for help): t # 调整分区类型
Selected partition 1
Hex code (type L to list all codes): fd # 选择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.
<3>创建RAID设备
[root@centos7~]#mdadm -C /dev/md0 -a yes -l 5 -n 4 -x 1 /dev/sd{b,c,d,e,f}1
mdadm: /dev/sdb1 appears to contain an ext2fs file system
size=2097152K mtime=Thu Jan 1 08:00:00 1970
mdadm: /dev/sde1 appears to contain an ext2fs file system
size=2097152K mtime=Mon Aug 29 21:32:01 2016
Continue creating array? y # 确定创建
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started. # 创建完成并启动
<4>查看创建进度
[root@centos7/mnt]#cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sde1[5] sdf1[4](S) sdd1[2] sdc1[1] sdb1[0]
6285312 blocks super 1.2 level 5, 512k chunk, algorithm 2 [4/4] [UUUU]
# RAID5创建完成,正式启动
unused devices: <none>
<5>查看RAID配置
[root@centos7~]#mdadm -D /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Tue Aug 30 15:38:04 2016
Raid Level : raid5 # RAID级别
Array Size : 6285312 (5.99 GiB 6.44 GB) # 阵列大小,使用率为n-1/n
Used Dev Size : 2095104 (2046.34 MiB 2145.39 MB) # 备用的磁盘空间大小
Raid Devices : 4
Total Devices : 5
Persistence : Superblock is persistent
Update Time : Tue Aug 30 15:38:14 2016
State : clean
Active Devices : 4 # RAID工作状况
Working Devices : 5
Failed Devices : 0
Spare Devices : 1
Layout : left-symmetric
Chunk Size : 512K # Chunk默认大小
Name : localhost.localdomain:0 (local to host localhost.localdomain)
UUID : fe6313e4:bc14f451:01602e5e:13820d53
Events : 18
Number Major Minor RaidDevice State
0 8 17 0 active sync /dev/sdb1
1 8 33 1 active sync /dev/sdc1
2 8 49 2 active sync /dev/sdd1
5 8 65 3 active sync /dev/sde1
4 8 81 - spare /dev/sdf1 # 备用盘
<6>为RAID5创建文件系统
[root@centos7/mnt]#mkfs.ext4 /dev/md0
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=128 blocks, Stripe width=384 blocks
393216 inodes, 1571328 blocks
... # 文件系统创建成功
<7>挂载RAID
# 创建挂载点
[root@centos7~]#mkdir /mnt/raid
# 编辑/etc/fstab
[root@centos7~]#vim /etc/fstab
...
UUID="24adec89-2933-47e3-b824-11e25ef679cf" /mnt/raid ext4 defaults 0 0
...
# 挂载
[root@centos7~]#mount -a
# 查看挂载
[root@centos7~]#mount
...
/dev/md0 on /mnt/raid type ext4 (rw,relatime,seclabel,stripe=384,data=ordered)
<8>创建RAID配置文件
[root@centos7~]#mdadm -Ds /dev/md0 > /etc/mdadm.conf
[root@centos7~]#cat /etc/mdadm.conf
ARRAY /dev/md0 metadata=1.2 spares=1 name=localhost.localdomain:0 UUID=fe6313e4:bc14f451:01602e5e:13820d53
<9>测试
[root@centos7~]#cp /etc/* /mnt/raid/ -r # 数据已通过挂载点存储在RAID5上
[root@centos7~]#cat /mnt/raid/issue
\S
Kernel \r on an \m
<9>模拟损坏一块磁盘
# 模拟损坏
[root@centos7~]#mdadm /dev/md0 -f /dev/sdb1
mdadm: set /dev/sdb1 faulty in /dev/md0 # 已损坏
# 查看RAID运行状况
[root@centos7~]#mdadm -D /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Tue Aug 30 15:38:04 2016
Raid Level : raid5
Array Size : 6285312 (5.99 GiB 6.44 GB)
Used Dev Size : 2095104 (2046.34 MiB 2145.39 MB)
Raid Devices : 4
Total Devices : 5
Persistence : Superblock is persistent
Update Time : Tue Aug 30 16:25:52 2016
State : clean, degraded, recovering
Active Devices : 3
Working Devices : 4
Failed Devices : 1
Spare Devices : 1
Layout : left-symmetric
Chunk Size : 512K
Rebuild Status : 57% complete # 进度
Name : localhost.localdomain:0 (local to host localhost.localdomain)
UUID : fe6313e4:bc14f451:01602e5e:13820d53
Events : 29
Number Major Minor RaidDevice State
4 8 81 0 spare rebuilding /dev/sdf1 # 正在修复中
1 8 33 1 active sync /dev/sdc1
2 8 49 2 active sync /dev/sdd1
5 8 65 3 active sync /dev/sde1
0 8 17 - faulty /dev/sdb1 # faulty状态
# 查看数据
[root@centos7~]#cat /etc/issue
\S
Kernel \r on an \m
# 数据无损坏
<10>不修复faulty硬盘,再次模拟损坏一块硬盘
# 损坏硬盘
[root@centos7~]#mdadm /dev/md0 -f /dev/sdc1
mdadm: set /dev/sdc1 faulty in /dev/md0
# 查看状态
[root@centos7~]#mdadm -D /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Tue Aug 30 15:38:04 2016
Raid Level : raid5
Array Size : 6285312 (5.99 GiB 6.44 GB)
Used Dev Size : 2095104 (2046.34 MiB 2145.39 MB)
Raid Devices : 4
Total Devices : 5
Persistence : Superblock is persistent
Update Time : Tue Aug 30 16:33:01 2016
State : clean, degraded
Active Devices : 3
Working Devices : 3
Failed Devices : 2
Spare Devices : 0
Layout : left-symmetric
Chunk Size : 512K
# 也没有修复的进度
Name : localhost.localdomain:0 (local to host localhost.localdomain)
UUID : fe6313e4:bc14f451:01602e5e:13820d53
Events : 39
Number Major Minor RaidDevice State
4 8 81 0 active sync /dev/sdf1
2 0 0 2 removed # 没有硬盘顶替,显示remove状态
2 8 49 2 active sync /dev/sdd1
5 8 65 3 active sync /dev/sde1
0 8 17 - faulty /dev/sdb1 # 已损坏
1 8 33 - faulty /dev/sdc1 # 已损坏
# 再次查看数据
[root@centos7~]#cat /mnt/raid/issue
\S
Kernel \r on an \m
# 数据无损
<11>依然不修复,再次损坏
# 损坏硬盘
[root@centos7~]#mdadm /dev/md0 -f /dev/sdd1
mdadm: set /dev/sdd1 faulty in /dev/md0
[root@centos7~]#mdadm -D /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Tue Aug 30 15:38:04 2016
Raid Level : raid5
Array Size : 6285312 (5.99 GiB 6.44 GB)
Used Dev Size : 2095104 (2046.34 MiB 2145.39 MB)
Raid Devices : 4
Total Devices : 5
Persistence : Superbloc
k is persistent
Update Time : Tue Aug 30 16:39:41 2016
State : clean, FAILED
Active Devices : 2
Working Devices : 2
Failed Devices : 3
Spare Devices : 0
Layout : left-symmetric
Chunk Size : 512K
Name : localhost.localdomain:0 (local to host localhost.localdomain)
UUID : fe6313e4:bc14f451:01602e5e:13820d53
Events : 41
Number Major Minor RaidDevice State
4 8 81 0 active sync /dev/sdf1
2 0 0 2 removed # 移除
4 0 0 4 removed # 移除
5 8 65 3 active sync /dev/sde1
0 8 17 - faulty /dev/sdb1 # 已损坏
1 8 33 - faulty /dev/sdc1 # 已损坏
2 8 49 - faulty /dev/sdd1 # 已损坏
# RAID5中,已损坏俩块硬盘,校验数据失效,数据全部损坏。
[root@centos7~]#cat /mnt/raid/fstab
cat: /mnt/raid/fstab: Input/output error
此时数据已经损坏,不可恢复
RAID支持热插拔技术
<12>在RAID 不损坏之前也是可以增加RAID的磁盘的
[root@centos7~]#mdadm -G /dev/md0 -a -n 5 /dev/sdg1
RAID损坏后,移除RAID
[root@centos7~]#mdadm /dev/md0 -r /dev/sdb1
mdadm: hot removed /dev/sdb1 from /dev/md0
标准的卸载RAID的步骤
<13>取消挂载、停止RAID、删除配置文件
[root@centos7~]#umount /mnt/raid/
[root@centos7~]#mdadm -S /dev/md0 备注----->启用RAID:mdadm -A -s /dev/md5
mdadm: stopped /dev/md0
[root@centos7~]#rm -f /etc/mdadm.conf
<14>移除RAID中的磁盘
<15>删除磁盘上的RAID信息
[root@centos7~]#mdadm --zero-superblock /dev/sdf1 # 一定要先停止,否则报错