磁盘阵列(Redundant Arrays of Independent Disks,RAID),是把相同的数据存储在多个硬盘的不同的地方的方法。通过把数据放在多个硬盘上,输入输出操作能以平衡的方式交叠,改良性能,储存冗余数据也增加了容错。
磁盘阵列一般分两大类:一类是硬件设备组成的磁盘阵列,一种是利用软件来仿真模拟出来的。
常见RAID级别
RAID0:组建磁盘阵列中最简单的一种形式,只需要2块以上的硬盘即可,成本低,可以提高整个磁盘的性能和吞吐量。将数据分别存储在不同的磁盘上,没有提供冗余或错误修复能力,但实现成本是最低的。
RAID1:为磁盘镜像,原理是把一个磁盘的数据镜像到另一个磁盘上,也就是说数据在写入一块磁盘的同时,会在另一块闲置的磁盘上生成镜像文件,利用率为50%,成本较高。
RAID5:条带+分布校验,3块以上,利用率为(n-1)/n,有容错功能,最多可以坏一块磁盘,适合于大规模序列化读写操作。
mdadm命令
mdadm - manage MD devices aka Linux Software RAID
Currently, Linux supports LINEAR md devices, RAID0 (striping), RAID1
(mirroring), RAID4, RAID5, RAID6, RAID10, MULTIPATH, FAULTY, and CON‐
TAINER.
-C ##新建
-l ##raid级别
-a ##表示如果目录不存在则新建
-n ##使用磁盘的数量
-x ##备用磁盘的数量
-s ##扫描配置文件/etc/madam.conf
-D ##查看RAID设备信息
-S ##停用RAID
[root@localhost ~]# fdisk -l ##查看磁盘信息
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors ##磁盘vda,虚拟机操作系统磁盘
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: 0x00013f3e
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20970332 10484142+ 83 Linux ##虚拟机分区
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors ##vdb,第一块磁盘,测试用
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 /dev/vdc: 10.7 GB, 10737418240 bytes, 20971520 sectors ##vdc,第二块磁盘,测试用
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 /dev/vdd: 10.7 GB, 10737418240 bytes, 20971520 sectors ##vdd,第三块磁盘,测试用
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
[root@localhost ~]# cat /proc/partitions ##查看/proc/partitions也可以
major minor #blocks name
253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 32 10485760 vdc
253 48 10485760 vdd
[root@localhost ~]# fdisk /dev/vdb ##创建分区,类型更改为RAID
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): n ##新建分区
Partition type:
p primary (0 primary, 0 extended, 4 free) ##标准分区
e extended
Select (default p): ##默认选择标准分区
Using default response p
Partition number (1-4, default 1): ##分区号,默认1
First sector (2048-20971519, default 2048): ##分区起始扇区
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): ##分区结束扇区
Using default value 20971519
Partition 1 of type Linux and of size 10 GiB is set
Command (m for help): t ##修改磁盘类型
Selected partition 1
Hex code (type L to list all codes): l ##L查看所有类型
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
............................................................................
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
............................................................................
Hex code (type L to list all codes): fd ##fd:Linux raid autodetect
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.
#####依次配置三块磁盘vdb、vdc、vdd
创建RAID1
[root@localhost ~]# mdadm -C /dev/md0 -a yes -l 1 -n 2 -x 1 /dev/vd{b,c,d} ##创建RAID盘md0,-C 创建,-a 如果没有则新建
mdadm: /dev/vdb appears to be part of a raid array: ##-l 1,RAID1,-n 2 使用两块磁盘
level=raid0 devices=0 ctime=Wed Dec 31 19:00:00 1969 ##-x 1,一块备用盘
mdadm: partition table exists on /dev/vdb but will be lost or
meaningless after creating array
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
mdadm: /dev/vdc appears to be part of a raid array:
level=raid0 devices=0 ctime=Wed Dec 31 19:00:00 1969
mdadm: partition table exists on /dev/vdc but will be lost or
meaningless after creating array
mdadm: /dev/vdd appears to be part of a raid array:
level=raid0 devices=0 ctime=Wed Dec 31 19:00:00 1969
mdadm: partition table exists on /dev/vdd but will be lost or
meaningless after creating array
Continue creating array? yes ##确认创建
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
[root@localhost ~]# mdadm -D /dev/md0 ##查看md0的信息
/dev/md0:
Version : 1.2 ##版本
Creation Time : Thu May 3 08:06:05 2018 ##创建时间
Raid Level : raid1 ##RAID等级
Array Size : 10477440 (9.99 GiB 10.73 GB)
Used Dev Size : 10477440 (9.99 GiB 10.73 GB)
Raid Devices : 2 ##raid磁盘两块
Total Devices : 3 ##磁盘总数三块
Persistence : Superblock is persistent
Update Time : Thu May 3 08:08:25 2018
State : clean
Active Devices : 2
Working Devices : 3
Failed Devices : 0
Spare Devices : 1
Name : localhost:0 (local to host localhost)
UUID : a18c2a43:54273a69:7c12f00d:faba04d9
Events : 17
Number Major Minor RaidDevice State
0 253 16 0 active sync /dev/vdb ##vdb工作磁盘
1 253 32 1 active sync /dev/vdc ##vdc工作磁盘
2 253 48 - spare /dev/vdd ##vdd备用磁盘
####也可以查看/proc/mdstat
[root@localhost ~]# cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 vdd[2](S) vdc[1] vdb[0]
10477440 blocks super 1.2 [2/2] [UU]
unused devices: <none>
[root@localhost ~]# mdadm /dev/md0 -f /dev/vdc ##-f,毁坏一块磁盘,模拟磁盘损坏情景
mdadm: set /dev/vdc faulty in /dev/md0
[root@localhost ~]# mdadm -D /dev/md0 ##查看raid磁盘信息
Rebuild Status : 2% complete
Name : localhost:0 (local to host localhost)
UUID : a18c2a43:54273a69:7c12f00d:faba04d9
Events : 25
Number Major Minor RaidDevice State
0 253 16 0 active sync /dev/vdb
2 253 48 1 spare rebuilding /dev/vdd ##破坏一块磁盘,备用盘自动同步数据
1 253 32 - faulty /dev/vdc
[root@localhost ~]# mdadm /dev/md0 -r /dev/vdc ##移除坏掉的磁盘
mdadm: hot removed /dev/vdc from /dev/md0
[root@localhost ~]# mdadm /dev/md0 -a /dev/vdc ##添加新的raid磁盘
mdadm: added /dev/vdc
[root@localhost ~]# mdadm -D /dev/md0 ##查看raid磁盘信息
Name : localhost:0 (local to host localhost)
UUID : a18c2a43:54273a69:7c12f00d:faba04d9
Events : 80
Number Major Minor RaidDevice State
0 253 16 0 active sync /dev/vdb
2 253 48 1 active sync /dev/vdd
3 253 32 - spare /dev/vdc ##新备用盘添加成功
[root@localhost ~]# mkfs.xfs /dev/md0 ##格式化磁盘阵列
meta-data=/dev/md0 isize=256 agcount=4, agsize=654840 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=2619360, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]# mount /dev/md0 /mnt ##挂载磁盘阵列
[root@localhost ~]# cat /etc/mdstat ##查看设备信息
##可以通过编辑/etc/fstab文件添加自动挂载raid分区
###清理测试环境###
[root@localhost ~]# umount /dev/md0 ##解除挂载
[root@localhost ~]# mdadm -S /dev/md0 ##停用raid
mdadm: stopped /dev/md0
[root@localhost ~]# partprobe ##同步分区表
###删除更改过的/etc/fstab文件