1.RAID 5是一种存储性能、数据安全和存储成本兼顾的存储解决方案。RAID 5可以理解为是RAID 5可以理解为是RAID 0 和RAID 1的折中方案。RAID 5和RAID 4一样,数据以块为单位分布到各个磁盘上。RAID 5不对数据进行备份,而是把数据和其对应的奇偶校验信息存储到组成RAID5的各个磁盘上,并且奇偶校验信息和对应的数据分别存储于不同的磁盘上。当RAID5的一个磁盘数据损坏后,利用剩下的数据和相应的奇偶校验信息恢复被损坏的数据。RAID 5至少使用3块磁盘组成磁盘阵列。此处通过实验加深读RAID 5工作原理的了解。
2.mdadm是Linux下用于创建和管理软件RAID的命令,是一个模式化命令。但是由于现在服务器一般都带有RAID阵列卡,并且RAID阵列卡也很廉价,且由于软件RAID的自身缺陷(不能用作启动分区、使用CPU实现、降低CPU利用率),因此在生产环境下并不适用。但为了学习和了解RAID原理和管理,进行如下实验。
VMware Workstation 12.0.0 build-2985596 CentOS6.9 64位系统
1.开启VMware Workstation虚拟机中CentOS6.9 64位系统前,在虚拟机中新增4块新的虚拟磁盘,大小分别为20GB、25GB、30GB、35GB,添加方法:点击VM虚拟机的菜单“虚拟机(M)"-->"设置(S)Ctrl+D"-->”硬件“-->"添加(A)”-->"硬件类型:硬盘“-->"下一步(N)”-->"选择磁盘类型:默认不修改“-->”下一步(N)”-->"选择磁盘:勾选 创建新虚拟磁盘(V)“-->”下一步(N)”-->"指定磁盘容量 最大磁盘容量填需要增加的大小,例如20,表示20GB;立即分配所有空间(A)不勾选;将虚拟磁盘存储为单个文件(O)勾选-->”下一步(N)“-->”指定磁盘文件:默认不修改“-->点击”完成“,创建磁盘完成,按照此方法再创建剩下3个虚拟磁盘。
2.开启虚拟机中的CentOS系统,如果添加虚拟硬盘时系统正在运行,想不重启让虚拟机识别硬盘可以使用这个命令:echo '- - -' > /sys/class/scsi_host/host2/scan,让系统识别新增的硬盘。然后用命令lsblk查看当前系统已有块设备。如下:
[root@centos6 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 3.7G 0 rom /media/CentOS_6.9_Final # 已挂载光盘,此处可忽略
sda 8:0 0 200G 0 disk # 根目录所在磁盘,此处可忽略
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 100G 0 part /
├─sda3 8:3 0 50G 0 part /app
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 1G 0 part [SWAP]
sdb 8:16 0 20G 0 disk # 此处为新增的第一块磁盘
sdc 8:32 0 25G 0 disk # 此处为新增的第二块磁盘
sdd 8:48 0 30G 0 disk # 此处为新增的第三块磁盘
sde 8:64 0 35G 0 disk # 此处为新增的第四块磁盘
1.实验前的规划:sdb、sdc、sdd组成RAID5,sde作为备用盘,因为组成RAID5的每个磁盘取出的空间要一致,组成后RAID最大空间取硬盘中最小的空间20G,所以磁盘sdc、sdd、sde要进行划分分区处理,如下操作在sdc上创建大小为20G的主分区,并按同样的操作方法,在sdd、sde上创建同样为20G的主分区。
[root@centos6 ~]# fdisk /dev/sdc # 对磁盘sdc分区
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xe691aa35.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
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): m # 获取帮助
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help): n # 创建新的分区
Command action
e extended
p primary partition (1-4)
p # 选择创建主分区
Partition number (1-4): 1 # 选择分区编号
First cylinder (1-3263, default 1): # 按Enter键,获取默认扇区起始点
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-3263, default 3263): +20G # 输入创建的分区空间大小
Command (m for help): p # 查看分区信息
Disk /dev/sdc: 26.8 GB, 26843545600 bytes
255 heads, 63 sectors/track, 3263 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: 0xe691aa35
Device Boot Start End Blocks Id System
/dev/sdc1 1 2612 20980858+ 83 Linux
Command (m for help): t # 修改系统分区id
Selected partition 1
Hex code (type L to list codes): fd # 可用输入L,查看输入id编号,Linux Raid的输入fd
Changed system type of partition 1 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@centos6 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 3.7G 0 rom /media/CentOS_6.9_Final
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 100G 0 part /
├─sda3 8:3 0 50G 0 part /app
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 1G 0 part [SWAP]
sdb 8:16 0 20G 0 disk
sdc 8:32 0 25G 0 disk
└─sdc1 8:33 0 20G 0 part
sdd 8:48 0 30G 0 disk
└─sdd1 8:49 0 20G 0 part
sde 8:64 0 35G 0 disk
└─sde1 8:65 0 20G 0 part
3.使用命令mdadm创建RAID5阵列。
[root@centos6 ~]# mdadm -C /dev/md0 -a yes -l 5 -n 3 -x1 /dev/sd{b,c1,d1,e1} # 此条命令的详细解析请看备注
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
4.使用命令mdadm -D /dev/md0查看创建的RAID5阵列md0的信息,也可以使用命令cat /proc/mdstat查看md0的信息。
[root@centos6 ~]# mdadm -D /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Mon Jun 19 21:09:04 2017
Raid Level : raid5
Array Size : 41910272 (39.97 GiB 42.92 GB)
Used Dev Size : 20955136 (19.98 GiB 21.46 GB)
Raid Devices : 3
Total Devices : 4
Persistence : Superblock is persistent
Update Time : Mon Jun 19 21:10:51 2017
State : clean
Active Devices : 3
Working Devices : 4
Failed Devices : 0
Spare Devices : 1
Layout : left-symmetric
Chunk Size : 512K
Name : centos6.xh.com:0 (local to host centos6.xh.com)
UUID : 07feea7a:f29f82fd:a59063c3:6b52657a
Events : 18
Number Major Minor RaidDevice State
0 8 16 0 active sync /dev/sdb
1 8 33 1 active sync /dev/sdc1
4 8 49 2 active sync /dev/sdd1
3 8 65 - spare /dev/sde1
5.将阵列md0的配置保存到配置文件,以免系统重启,导致配置信息的丢失。
[root@centos6 ~]# ll /etc/mdadm.config # mdadm.conf本身不存在,要自己创建
ls: cannot access /etc/mdadm.config: No such file or directory
[root@centos6 ~]# mdadm -Ds /dev/md0 > /etc/mdadm.conf # 将生成的md0的配置信息保存到配置文件/etc/mdadm.conf
[root@centos6 ~]# cat /etc/mdadm.conf
ARRAY /dev/md0 metadata=1.2 spares=1 name=centos6.xh.com:0 UUID=07feea7a:f29f82fd:a59063c3:6b52657a
6.在阵列md0上创建文件系统。
[root@centos6 ~]# mkfs.ext4 /dev/md0
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
...(中间省略)...
This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
7.查看阵列md0上创建的文件系统。
[root@centos6 ~]# blkid
/dev/sda1: UUID="50d74642-6b7c-4df1-bb9c-d90f7a9849a3" TYPE="ext4"
/dev/sda2: UUID="11baefb7-52b3-4d71-a4b3-d054606c452b" TYPE="ext4"
/dev/sda3: UUID="f0f08ce9-77b9-4b4f-ba27-63583b24efea" TYPE="ext4"
/dev/sda5: UUID="50d2c8e4-c1bf-49f3-b925-e5a602967bdc" TYPE="swap"
/dev/sdb: UUID="07feea7a-f29f-82fd-a590-63c36b52657a" UUID_SUB="5cb22a99-15bb-1e91-d2fc-faf392669555" LABEL="centos6.xh.com:0" TYPE="linux_raid_member"
/dev/sdc1: UUID="07feea7a-f29f-82fd-a590-63c36b52657a" UUID_SUB="97571e1a-e7b3-f665-42f5-42701c111dae" LABEL="centos6.xh.com:0" TYPE="linux_raid_member"
/dev/sdd1: UUID="07feea7a-f29f-82fd-a590-63c36b52657a" UUID_SUB="e31d1b2b-8cdd-1c83-ff90-69ec0db5ccdf" LABEL="centos6.xh.com:0" TYPE="linux_raid_member"
/dev/sde1: UUID="07feea7a-f29f-82fd-a590-63c36b52657a" UUID_SUB="373332ef-e2c8-daac-8e5f-84d73b76cddb" LABEL="centos6.xh.com:0" TYPE="linux_raid_member"
/dev/md0: UUID="0e54fa3c-446f-4da4-a0fb-4f9dd927073c" TYPE="ext4" # md0上创建好的ext4文件系统
8.挂载阵列md0上创建好的文件系统/dev/md0
[root@centos6 ~]# mount /dev/md0 /mnt/raid5
[root@centos6 ~]# vim /etc/fstab # 将挂载信息写入配置文件
[root@centos6 ~]# tail -n 1 /etc/fstab
UUID=0e54fa3c-446f-4da4-a0fb-4f9dd927073c /mnt/raid5 ext4 defaults 0 0 # 在/etc/fstab最后一行追加内容,md0的UUID可以通过blkid命令查看,也可用设备名,不推荐
[root@centos6 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 99G 4.8G 89G 6% /
tmpfs 491M 76K 491M 1% /dev/shm
/dev/sda3 50G 1.1G 46G 3% /app
/dev/sda1 976M 35M 891M 4% /boot
/dev/sr0 3.7G 3.7G 0 100% /media/CentOS_6.9_Final
/dev/md0 40G 48M 38G 1% /mnt/raid5
9.使用文件系统
[root@centos6 ~]# cp /etc/man.config /mnt/rad5
[root@centos6 ~]# ls -l /mnt/raid5
total 24
drwx------. 2 root root 16384 Jun 19 21:35 lost+found
-rw-r--r--. 1 root root 4940 Jun 19 21:46 man.config
[root@centos6 ~]# head -n 5 /mnt/raid5/man.config
#
# Generated automatically from man.conf.in by the
# configure script.
#
# man.conf from man-1.6f
10.软件模拟磁盘/dev/sdc损坏,看md0的运行情况。
[root@centos6 ~]# mdadm -D /dev/md0 # 查看磁盘sdc损坏前,md0的情况
/dev/md0:
...(中间省略)...
Update Time : Tue Jun 20 07:23:30 2017
State : clean
Active Devices : 3 # 活动的设备有3个
Working Devices : 4 # 工作的设备有4个
Failed Devices : 0 # 失效的设备有0个
Spare Devices : 1
Layout : left-symmetric
Chunk Size : 512K
Name : centos6.xh.com:0 (local to host centos6.xh.com)
UUID : 07feea7a:f29f82fd:a59063c3:6b52657a
Events : 18
Number Major Minor RaidDevice State
0 8 16 0 active sync /dev/sdb
1 8 33 1 active sync /dev/sdc1
4 8 49 2 active sync /dev/sdd1
3 8 65 - spare /dev/sde1 # /dev/sde1处于备用状态
[root@centos6 ~]# mdadm /dev/md0 -f /dev/sdc1 # 模拟磁盘/dev/sdc损坏
mdadm: set /dev/sdc1 faulty in /dev/md0
[root@centos6 ~]# mdadm -D /dev/md0 -f /dev/sdc1 # 查看磁盘/dev/sdc损坏后,md0的运行情况
/dev/md0:
...(中间省略)...
Update Time : Tue Jun 20 07:40:55 2017
State : clean, degraded, recovering # 降级使用,正在恢复
Active Devices : 2 # 活动的设备有2个
Working Devices : 3 # 工作的设备有3个
Failed Devices : 1 # 失效的设备有1个
Spare Devices : 1
Layout : left-symmetric
Chunk Size : 512K
Rebuild Status : 42% complete # 正在重建md0
Name : centos6.xh.com:0 (local to host centos6.xh.com)
UUID : 07feea7a:f29f82fd:a59063c3:6b52657a
Events : 26
Number Major Minor RaidDevice State
0 8 16 0 active sync /dev/sdb
3 8 65 1 spare rebuilding /dev/sde1
4 8 49 2 active sync /dev/sdd1 # sdd1由备用状态转为活动状态
1 8 33 - faulty /dev/sdc1 # 失效设备sdc1
mdadm: /dev/sdc1 does not appear to be an md device
11.从阵列md0中移除sdc1.
[root@centos6 ~]# mdadm /dev/md0 -r /dev/sdc1
mdadm: hot removed /dev/sdc1 from /dev/md0
[root@centos6 ~]# mdadm -D /dev/md0
/dev/md0:
...(中间省略)...
Number Major Minor RaidDevice State
0 8 16 0 active sync /dev/sdb
3 8 65 1 active sync /dev/sde1
4 8 49 2 active sync /dev/sdd1
12.模拟硬盘物理损坏,关闭虚拟机,移除空间大小的为20G的硬盘,重启虚拟机,启动后,查看md0的信息
[root@centos6 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 99G 5.8G 88G 7% /
tmpfs 491M 76K 491M 1% /dev/shm
/dev/sda3 50G 2.1G 45G 5% /app
/dev/sda1 976M 35M 891M 4% /boot
/dev/md0 40G 1.1G 37G 3% /mnt/raid5
[root@centos6 ~]# mdadm -D /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Mon Jun 19 21:09:04 2017
Raid Level : raid5
Array Size : 41910272 (39.97 GiB 42.92 GB)
Used Dev Size : 20955136 (19.98 GiB 21.46 GB)
Raid Devices : 3
Total Devices : 2
Persistence : Superblock is persistent
Update Time : Tue Jun 20 08:20:17 2017
State : clean, degraded # 降级使用
Active Devices : 2
Working Devices : 2 # 只有2块硬盘在工作
Failed Devices : 0
Spare Devices : 0
Layout : left-symmetric
Chunk Size : 512K
Name : centos6.xh.com:0 (local to host centos6.xh.com)
UUID : 07feea7a:f29f82fd:a59063c3:6b52657a
Events : 40
Number Major Minor RaidDevice State
0 0 0 0 removed
3 8 49 1 active sync /dev/sdd1
4 8 33 2 active sync /dev/sdc1
[root@centos6 ~]# cd /mnt/raid5/
[root@centos6 raid5]# ls # md0可以正常访问
bigfile lost+found man.config
[root@centos6 raid5]# head -n 5 man.config
#
# Generated automatically from man.conf.in by the
# configure script.
#
# man.conf from man-1.6f
13.查看还有空闲磁盘,为RAID增加一块硬盘
[root@centos6 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 3.7G 0 rom /media/CentOS_6.9_Final
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 100G 0 part /
├─sda3 8:3 0 50G 0 part /app
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 1G 0 part [SWAP]
sdb 8:16 0 25G 0 disk # 对比之前查看块设备信息,可以发现设备名来标识设备是不稳定的,设备名会因设备的增减、系统重启发生改变
└─sdb1 8:17 0 20G 0 part # 设备sdb1是空闲的
sdc 8:32 0 30G 0 disk
└─sdc1 8:33 0 20G 0 part
└─md0 9:0 0 40G 0 raid5 /mnt/raid5
sdd 8:48 0 35G 0 disk
└─sdd1 8:49 0 20G 0 part
└─md0 9:0 0 40G 0 raid5 /mnt/raid5
[root@centos6 ~]# mdadm /dev/md0 -a /dev/sdb1 # 将磁盘sdb1添加到md0
mdadm: added /dev/sdb1
[root@centos6 ~]# cat /proc/mdstat # 查看md0信息
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sdb1[5] sdc1[4] sdd1[3]
41910272 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/2] [_UU]
[==>..................] recovery = 10.0% (2103556/20955136) finish=1.4min speed=210355K/sec # 正在重建md0
unused devices:
1.mdadm命令的简单讲解:
mdadm: 模式化的工具
命令的语法格式: mdadm [mode]
支持的RAID级别:LINEAR,RAID0, RAID1, RAID 4, RAID 5, RAID 6, RAID 10
模式:
创建: -C
装配:-A
监控: -F
管理:-f, -r, -a
-C: 创建模式
-n #: 使用#个设备来创建此RAID
-l #: 指明要创建的RAID的级别
-a {yes|no}: 自动创建目标RAID设备的设备文件
-c CHUNK_SIZE: 指明块大小
-x #: 指明空闲盘的个数
-D: 显示raid的详细信息:
mdadm -D /dev/md#
管理模式:
-f: 指定标记的磁盘为损坏
-a: 添加磁盘
-r: 移除磁盘
观察md的状态:
cat /proc/mdstat