RAID(Redundant Array of Independent Disks,独立磁盘冗余阵列)可以提供较普通磁盘更高的速度、安全性,所以服务器在安装时都会选择创建RAID。
RAID的创建有两种方式:软RAID(通过操作系统软件来实现)和硬RAID(使用硬件阵列卡);在企业中用的最多的是:raid1、raid10和raid5。
一、 RAID常见的几种分类
模式 | 优点 | 缺点 | 备注 |
RAID 0 | 在RAID 0状态下,存储数据被分割成两部分,分别存储在两块硬盘上,此时移动硬盘的理论存储速度是单块硬盘的2倍,实际容量等于两块硬盘中较小一块硬盘的容量的2倍。 | 任何一块硬盘发生故障,整个RAID上的数据将不可恢复。 | 存储高清电影比较适合。 |
RAID 1 | 两块硬盘互为镜像。当一个硬盘受损时,换上一块全新硬盘(大于或等于原硬盘容量)替代原硬盘即可自动恢复资料和继续使用,移动硬盘的实际容量等于较小一块硬盘的容量,存储速度与单块硬盘相同。RAID 1的优势在于任何一块硬盘出现故障是,所存储的数据都不会丢失。 | 可使用的硬盘实际容量比较小,仅仅为两颗硬盘中最小硬盘的容量。 | 非常重要的资料,如数据库,个人资料,是万无一失的存储方案。 |
RAID 5 | RAID5不对存储的数据进行备份,而是把数据和相对应的奇偶校验信息存储到组成RAID5的各个磁盘上,并且奇偶校验信息和相对应的数据分别存储于不同的磁盘上。当RAID5的一个磁盘数据发生损坏后,利用剩下的数据和相应的奇偶校验信息去恢复被损坏的数据。 | 损失一定的磁盘空间 | 服务器常用方案 |
RAID 10 | 把2块硬盘组成一个RAID1,然后两组RAID1组成一个RAID0。它提供了200%的速度和单磁盘损坏的数据安全性。 | RAID10最少需要四块硬盘才能完成,50%的磁盘浪费。 | 服务器常用方案 |
二、 RAID使用简明注意事项
使用前请先备份硬盘的资料,一旦进行RAID设定或是变更RAID模式,将会清除硬盘里的所有资料,以及无法恢复。
建立RAID时,建议使用相同品牌、型号和容量的硬盘,以确保性能和稳定。
请勿随意更换或取出硬盘,如果取出了硬盘,请记下硬盘放入两个仓位的顺序不得更改,以及请勿只插入某一块硬盘使用,以避免造成资料损坏或丢失。
如果旧硬盘曾经在RAID模式
下使用,请先进清除硬盘RAID信息,让硬盘回复至出厂状态,以免RAID建立失败。
RAID0模式下,其中一个硬盘损坏时,其它硬盘所有资料都将丢失。
RAID1模式下,如果某一块硬盘受损,可以用一块大于或等于受损硬盘容量的新硬盘替换坏硬盘然后开机即可自动恢复和修复资料以及RAID模式。此过程需要一定时间,请耐心等待。
三、 软RAID搭建方法
mdadm是Linux下用于创建和管理软RAID的命令,是一个模块化命令。
常用参数如下:
-C 或 --creat | 建立一个新阵列 | -r | 移除设备 |
-A | 激活磁盘阵列 | -l 或 --level= | 设定磁盘阵列的级别 |
-D 或 --detail | 打印阵列设备的详细信息 | -n 或 --raid-devices= | 指定阵列成员(分区/次磁盘)的数量 |
-s 或 --scan | 扫面配置文件或/proc/mdstat得到阵列缺失信息 | -x 或 --spare-devicds= | 指定阵列中备用盘的数量 |
-f | 将设备状态定位故障 | -c 或--chunk= | 设定阵列的块 chunk 大小,单位为KB |
-a 或 --add | 添加设备到阵列 | -G 或 --grow | 改变阵型大小或形态 |
-v | 显示详细信息 |
组建RAID0
环境:关闭电源,添加两个硬盘。
启动Linux,将新加入的硬盘分区。使用命令:fdisk
[root@hyx ~]# fdisk /dev/sdb WARNING: DOS-compatible mode is deprecated. It's strongly recommended to #警告:DOS兼容模式已经过时。强烈建议 switch off the mode (command 'c') and change display units to 关闭模式(命令“c”)并将显示单元更 sectors (command 'u'). 改为扇区(命令“u”)。 Command (m for help): n #新建一个分区 Command action e extended #e为逻辑分区 p primary partition (1-4) #p为主分区 p Partition number (1-4): 1 #设置分区号为1 First cylinder (1-2610, default 1): #默认从第一块开始 Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +1G #将分区添加1G Command (m for help): n #新建分区 Command action e extended p primary partition (1-4) p #主分区 Partition number (1-4): 2 #设置分区号为2 First cylinder (133-2610, default 133): Using default value 133 Last cylinder, +cylinders or +size{K,M,G} (133-2610, default 2610): +1G #将分区添加1G Command (m for help): w #w保存退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
备注:已经将硬盘分为两个区,分别是sdb1和sdb2。
查询是否安装mdadm命令。
[root@hyx ~]# rpm -q mdadm mdadm-3.3.4-8.el6.x86_64
[root@hyx ~]# mdadm -C /dev/md0 -l 0 -n 2 /dev/sdb1 /dev/sdb2 #新建一个md0在/dev下,阵列等级为1,硬盘数量为2,分别是/dev/sdb1和/dev/sdb2 mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md0 started.
查询阵列信息
[root@hyx ~]# mdadm -Ds ARRAY /dev/md0 metadata=1.2 name=hyx:0 UUID=def743c7:9b129ae0:44f88da4:0b844d03
[root@hyx ~]# mdadm -D /dev/md0 /dev/md0: Version : 1.2 Creation Time : Thu Aug 3 15:38:53 2017 Raid Level : raid0 Array Size : 2117632 (2.02 GiB 2.17 GB) Raid Devices : 2 Total Devices : 2 Persistence : Superblock is persistent Update Time : Thu Aug 3 15:38:53 2017 State : clean Active Devices : 2 Working Devices : 2 Failed Devices : 0 Spare Devices : 0 Chunk Size : 512K Name : hyx:0 (local to host hyx) UUID : def743c7:9b129ae0:44f88da4:0b844d03 Events : 0 Number Major Minor RaidDevice State 0 8 17 0 active sync /dev/sdb1 1 8 18 1 active sync /dev/sdb2
已成功创建名为md0的RAID0阵列。
将配置信息保存到mdadm的配置文件,默认没有配置文件,需要手工创建。
[root@hyx ~]# mkdir /etc/mdadm.conf
[root@hyx ~]# mdadm -Ds > /etc/mdadm.conf
对创建的RAID0进行分区
[root@localhost ~]# fdisk /dev/md0 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xa899a63c. 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): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-529408, default 257): Using default value 257 Last cylinder, +cylinders or +size{K,M,G} (257-529408, default 529408): Using default value 529408 Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
格式化分区,并且将分区挂载到系统中
[root@localhost ~]# mkfs.ext4 /dev/md0p1 mke2fs 1.41.12 (17-May-2010) 文件系统标签= 操作系统:Linux 块大小=4096 (log=2) 分块大小=4096 (log=2) Stride=128 blocks, Stripe width=256 blocks 132464 inodes, 529152 blocks 26457 blocks (5.00%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=545259520 17 block groups 32768 blocks per group, 32768 fragments per group 7792 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 正在写入inode表: 完成 Creating journal (16384 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 31 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
创建一个目录并且将raid0挂载到该目录下
[root@localhost ~]# mkdir /raid0 [root@localhost ~]# mount /dev/md0p1 /raid0/
查看是否挂载成功
[root@localhost ~]# df -h | grep md0 /dev/md0p1 2.0G 3.1M 1.9G 1% /raid0
设置开机自动挂载
[root@localhost ~]# vim /etc/fstab
# /etc/fstab
# Created by anaconda on Wed Jul 26 06:20:11 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root / ext4 defaults
1 1
UUID=4c386817-8682-457f-925d-769a25c85b37 /boot ext4 def
aults 1 2
/dev/mapper/VolGroup-lv_swap swap swap defaults
0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/md0p1 /raid0 ext4 defaults 0 0
保存退出设置完毕!
组建RAID1
将新硬盘sdc分区
[root@hyx dev]# fdisk /dev/sdc 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): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +2G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (263-2610, default 263): Using default value 263 Last cylinder, +cylinders or +size{K,M,G} (263-2610, default 2610): +2G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (525-2610, default 525): Using default value 525 Last cylinder, +cylinders or +size{K,M,G} (525-2610, default 2610): +2G Command (m for help): p Disk /dev/sdc: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x94ab7db1 Device Boot Start End Blocks Id System /dev/sdc1 1 262 2104483+ 83 Linux /dev/sdc2 263 524 2104515 83 Linux /dev/sdc3 525 786 2104515 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
将硬盘分为三个区,分别是sdc1,sdc2,sdc3。其中一个是热备盘。
[root@hyx dev]# mdadm -C -v /dev/md1 -l 1 -n 2 -x 1 /dev/sdc{1,2,3} mdadm: /dev/sdc1 appears to be part of a raid array: level=raid1 devices=3 ctime=Fri Aug 4 05:21:15 2017 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: size set to 2102400K Continue creating array? y mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md1 started.
对RAID1进行分区
[root@hyx dev]# fdisk /dev/md1 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x9845350d. 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): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-525600, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-525600, default 525600): Using default value 525600 Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
将分区格式化为ext4格式
[root@hyx dev]# mkfs.ext4 /dev/md1p1 mke2fs 1.41.12 (17-May-2010) 文件系统标签= 操作系统:Linux 块大小=4096 (log=2) 分块大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 131648 inodes, 525600 blocks 26280 blocks (5.00%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=541065216 17 block groups 32768 blocks per group, 32768 fragments per group 7744 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 正在写入inode表: 完成 Creating journal (16384 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 39 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
查询是否成功创建,并且在根分区下创建一个目录,用于挂载RAID1。
[root@hyx dev]# cat /proc/mdstat Personalities : [raid1] md1 : active raid1 sdc3[2](S) sdc2[1] sdc1[0] 2102400 blocks super 1.2 [2/2] [UU] unused devices: <none>
[root@hyx ~]# mkdir /raid1
[root@hyx ~]# mount /dev/md1p1 /raid1
[root@hyx dev]# df -h | grep md1 /dev/md1p1 2.0G 3.1M 1.9G 1% /raid1
成功创建RAID1
组建RAID5
添加新硬盘sdb,将硬盘分区。
[root@hyx dev]# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xa671eab8. 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): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +1G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (133-2610, default 133): Using default value 133 Last cylinder, +cylinders or +size{K,M,G} (133-2610, default 2610): +1G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (265-2610, default 265): Using default value 265 Last cylinder, +cylinders or +size{K,M,G} (265-2610, default 2610): +1G Command (m for help): n Command action e extended p primary partition (1-4) e Selected partition 4 First cylinder (397-2610, default 397): Using default value 397 Last cylinder, +cylinders or +size{K,M,G} (397-2610, default 2610): Using default value 2610 Command (m for help): n First cylinder (397-2610, default 397): Using default value 397 Last cylinder, +cylinders or +size{K,M,G} (397-2610, default 2610): +1G Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0xa671eab8 Device Boot Start End Blocks Id System /dev/sdb1 1 132 1060258+ 83 Linux /dev/sdb2 133 264 1060290 83 Linux /dev/sdb3 265 396 1060290 83 Linux /dev/sdb4 397 2610 17783955 5 Extended /dev/sdb5 397 528 1060258+ 83 Linux Command (m for help): n First cylinder (529-2610, default 529): Using default value 529 Last cylinder, +cylinders or +size{K,M,G} (529-2610, default 2610): +1G Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0xa671eab8 Device Boot Start End Blocks Id System /dev/sdb1 1 132 1060258+ 83 Linux /dev/sdb2 133 264 1060290 83 Linux /dev/sdb3 265 396 1060290 83 Linux /dev/sdb4 397 2610 17783955 5 Extended /dev/sdb5 397 528 1060258+ 83 Linux /dev/sdb6 529 660 1060258+ 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
创建md5
[root@hyx dev]# mdadm -C -v /dev/md5 -l 5 -n 3 -c 32 -x1 /dev/sdb{1,2,3,5} #为什么要用1.2.3.5,而不是1.2.3.4,因为4是扩展分区,并不是真正的有效分区,所以用5. mdadm: layout defaults to left-symmetric mdadm: layout defaults to left-symmetric mdadm: size set to 1059232K mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md5 started.
将RAID5分区
[root@hyx dev]# fdisk /dev/md5 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x040e784b. 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): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-529616, default 17): Using default value 17 Last cylinder, +cylinders or +size{K,M,G} (17-529616, default 529616): Using default value 529616 Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
格式化RAID5
[root@hyx dev]# mkfs.ext4 /dev/md5p1 mke2fs 1.41.12 (17-May-2010) 文件系统标签= 操作系统:Linux 块大小=4096 (log=2) 分块大小=4096 (log=2) Stride=8 blocks, Stripe width=16 blocks 132464 inodes, 529600 blocks 26480 blocks (5.00%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=545259520 17 block groups 32768 blocks per group, 32768 fragments per group 7792 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 正在写入inode表: 完成 Creating journal (16384 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 28 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@hyx dev]# mkdir /raid5
[root@hyx dev]# mount /dev/md5p1 /raid5
[root@hyx dev]# df -h | grep md5 /dev/md5p1 2.0G 3.1M 1.9G 1% /raid5
RAID5创建完毕。
组建RAID10
建立RAID1+0双层架构的方法。
方法:先创建RAID1,再使用创建的RAID1设备创建RAID0.
先创建两个底层的RAID1
[root@hyx ~]# mdadm -C -v /dev/md11 -l 1 -n 2 /dev/sdd1 /dev/sdd2 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: size set to 2102400K Continue creating array? y mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md11 started. [root@hyx ~]# mdadm -C -v /dev/md12 -l 1 -n 2 /dev/sdd3 /dev/sdd4 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: size set to 2102464K Continue creating array? y mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md12 started.
再创建上层的RAID0
[root@hyx ~]# mdadm -C -v /dev/md10 -l 0 -n 2 /dev/md11 /dev/md12 mdadm: chunk size defaults to 512K mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md10 started.
将raid10分区,然后格式化raid10.
挂载,查询是否挂载成功
[root@hyx ~]# mount /dev/md10p1 /raid10 [root@hyx ~]# df -h | grep 10 /dev/md10p1 3.9G 8.1M 3.7G 1% /raid10
注意事项
要取消RAID之前先要取消挂载。
例如取消RAID10。
[root@hyx dev]# umount /raid10 [root@hyx dev]# mdadm -S /dev/md10 mdadm: stopped /dev/md10
查看raid状态
[root@localhost ~]# cat /proc/mdstat [root@hyx dev]# cat /proc/mdstat Personalities : [raid1] [raid6] [raid5] [raid4] [raid0] md10 : active raid0 md11[0] md12[1] 4200448 blocks super 1.2 512k chunks md11 : active raid1 sdd1[0] sdd2[1] 2102400 blocks super 1.2 [2/2] [UU] md1 : active raid1 sdc1[0] sdc3[2](S) sdc2[1] 2102400 blocks super 1.2 [2/2] [UU] md5 : active raid5 sdb1[0] sdb5[3](S) sdb3[4] sdb2[1] 2118464 blocks super 1.2 level 5, 32k chunk, algorithm 2 [3/3] [UUU] md12 : active raid1 sdd3[0] sdd4[1] 2102464 blocks super 1.2 [2/2] [UU] unused devices: <none>
重新启动raid
[root@localhost ~]# mdadm -As