linux磁盘管理软RAID的实现原理分析和方法分享
1 什么是RAID
RAID全称是独立磁盘冗余阵列(Redundant Array of Independent Disks),基本思想是把多个磁盘组合起来,组合一个磁盘阵列组,使得性能大幅提高。
RAID分为几个不同的等级,各个不同的等级均在数据可靠性及读写性能做了不同的权衡。实际工作中根据自己的业务需求选择不同的RAID方案。
2 RAID的实现方式
外接式磁盘阵列:通过扩展卡提供适配能力内接式RAID:主板集成RAID控制器安装OS前在BIOS里配置软件RAID:通过OS实现
3 标准的RAID
3.1 RAID0
RAID0称为条带化存储,将数据分段存储在各个磁盘中,读写均可以并行处理,因此读写速率为单个磁盘的N倍,没有冗余功能,任何一个磁盘的损坏就会导致的数据不可用。
3.2 RAID1
RADI1是镜像存储,没有数据校验,数据被同等的写入到2个或者多个磁盘中,写入速度相对慢, 但是读取速度比较快。
3.3 RAID 4
RADI4在RAID1的基础上,N个盘用于数据存储,另外加入了1个磁盘作为校验盘。一共N+1个盘,任何一个盘坏掉也不影响数据的访问
3.4 RAID 5
RAID5在RAID4的基础上,由原来的一个盘来存储校验数据,改为每个盘都有数据和校验信息的。
4 混合RAID
4.1 RAID01
先组成RAID0,然后组成RAID1.
4.2 RAID10
先组成RAID1,然后组成RAID0
5 软RAID的实现
5.1 RAID5的实现
创建由三块硬盘组成的可用空间为2G的RAID5设备,要求其chunk大小为256k,文件系统为ext4,开机可自动挂载至/mydata目录
5.1.1 先看看我们的磁盘情况
[root@centos7 Bash]$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 200G 0 disk ├─sda1 8:1 0 1G 0 part /boot ├─sda2 8:2 0 128G 0 part ├─sda3 8:3 0 48.8G 0 part / ├─sda4 8:4 0 512B 0 part └─sda5 8:5 0 19.5G 0 part /app sdb 8:16 0 100G 0 disk sdc 8:32 0 20G 0 disk sdd 8:48 0 20G 0 disk sde 8:64 0 20G 0 disk sdf 8:80 0 20G 0 disk sr0 11:0 1 8.1G 0 rom /run/media/root/CentOS 7 x86_64
这里我们使用sdb,sdc,sdd,每个盘创建一个主分区1G,构建RADI5.
5.1.2 根据实际情况分区
[root@centos7 Bash]$ 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. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0x93d380cf. Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): First sector (2048-209715199, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): +1G Partition 1 of type Linux and of size 1 GiB is set Command (m for help): t Selected partition 1 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): p Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors 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: 0x93d380cf Device Boot Start End Blocks Id System /dev/sdb1 2048 2099199 1048576 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@centos7 Bash]$ fdisk /dev/sdc 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. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0xc56b90d8. Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): First sector (2048-41943039, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +1G Partition 1 of type Linux and of size 1 GiB is set Command (m for help): t Selected partition 1 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): p Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors 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: 0xc56b90d8 Device Boot Start End Blocks Id System /dev/sdc1 2048 2099199 1048576 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@centos7 Bash]$ fdisk /dev/sdd 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. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0x7e0900d8. Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): First sector (2048-41943039, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +1G Partition 1 of type Linux and of size 1 GiB is set Command (m for help): p Disk /dev/sdd: 21.5 GB, 21474836480 bytes, 41943040 sectors 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: 0x7e0900d8 Device Boot Start End Blocks Id System /dev/sdd1 2048 2099199 1048576 83 Linux Command (m for help): t Selected partition 1 Hex code (type L to list all codes): 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.
5.1.3 创建raid
[root@centos7 Bash]$ mdadm -C /dev/md5 -a yes -l 5 -n 3 /dev/sd{b1,c1,d1} -c 256 # -C指定创建, -a yes 自动创建设备 , -l 设定level , -n 设定磁盘个数, -c chunk大小 Continue creating array"cnblogs_code">[root@centos7 md5]$ mdadm -D /dev/md5 #查看详细raid5详细信息,可以发现有3个都是working状态的 /dev/md5: Version : 1.2 Creation Time : Wed Dec 6 19:28:22 2017 Raid Level : raid5 Array Size : 2095104 (2046.00 MiB 2145.39 MB) Used Dev Size : 1047552 (1023.00 MiB 1072.69 MB) Raid Devices : 3 Total Devices : 3 Persistence : Superblock is persistent Update Time : Wed Dec 6 19:39:06 2017 State : clean Active Devices : 3 Working Devices : 3 Failed Devices : 0 Spare Devices : 0 Layout : left-symmetric Chunk Size : 256K Consistency Policy : resync Name : centos7.magedu.com:5 (local to host centos7.magedu.com) UUID : 2c8ae60d:a799fcb7:9008a046:ae6ea430 Events : 18 Number Major Minor RaidDevice State 0 8 17 0 active sync /dev/sdb1 1 8 33 1 active sync /dev/sdc1 3 8 49 2 active sync /dev/sdd1 [root@centos7 md5]$ man mdadm [root@centos7 md5]$ mdadm /dev/md5 -f /dev/sdc1 # -f 设定指定设备故障, 将/dev/sdc1 这个盘标记失败, 看是否数据能访问,我这里使用-f标记失败,工作中可以根据硬盘指示灯判断磁盘状态 mdadm: set /dev/sdc1 faulty in /dev/md5 [root@centos7 md5]$ mdadm -D /dev/md5 #在次查看信息,发现工作的是2个, 一个失败的设备 /dev/md5: Version : 1.2 Creation Time : Wed Dec 6 19:28:22 2017 Raid Level : raid5 Array Size : 2095104 (2046.00 MiB 2145.39 MB) Used Dev Size : 1047552 (1023.00 MiB 1072.69 MB) Raid Devices : 3 Total Devices : 3 Persistence : Superblock is persistent Update Time : Wed Dec 6 19:41:08 2017 State : clean, degraded # 这里注意了。 我们的一个盘坏掉了。 raid5状态为降级使用了。 Active Devices : 2 Working Devices : 2 Failed Devices : 1 Spare Devices : 0 Layout : left-symmetric Chunk Size : 256K Consistency Policy : resync Name : centos7.magedu.com:5 (local to host centos7.magedu.com) UUID : 2c8ae60d:a799fcb7:9008a046:ae6ea430 Events : 20 Number Major Minor RaidDevice State 0 8 17 0 active sync /dev/sdb1 - 0 0 1 removed 3 8 49 2 active sync /dev/sdd1 1 8 33 - faulty /dev/sdc1 [root@centos7 md5]$ cat a.txt # 发现我们的数据还是能访问的。没有问题。
5.1.5 替换设备
我这里是磁盘坏掉后的执行替换的, 完全可以多一个备用盘, 坏掉自动替换的。
[root@centos7 md5]$ mdadm /dev/md5 -a /dev/sde1 # 上面我们的sdc1数据损坏,我们需要更换新的磁盘来顶替他的位置。这里添加一个sde1的磁盘, fdisk操作这里省去了。 mdadm: added /dev/sde1 [root@centos7 md5]$ mdadm -Ds # 查看详细信息 ARRAY /dev/md5 metadata=1.2 name=centos7.magedu.com:5 UUID=2c8ae60d:a799fcb7:9008a046:ae6ea430 [root@centos7 md5]$ mdadm -D /dev/md5 # 查看详细信息 /dev/md5: Version : 1.2 Creation Time : Wed Dec 6 19:28:22 2017 Raid Level : raid5 Array Size : 2095104 (2046.00 MiB 2145.39 MB) Used Dev Size : 1047552 (1023.00 MiB 1072.69 MB) Raid Devices : 3 Total Devices : 4 Persistence : Superblock is persistent Update Time : Wed Dec 6 19:50:01 2017 State : clean # 状态恢复正常了。没有问题 Active Devices : 3 Working Devices : 3 Failed Devices : 1 Spare Devices : 0 Layout : left-symmetric Chunk Size : 256K Consistency Policy : resync Name : centos7.magedu.com:5 (local to host centos7.magedu.com) UUID : 2c8ae60d:a799fcb7:9008a046:ae6ea430 Events : 43 Number Major Minor RaidDevice State 0 8 17 0 active sync /dev/sdb1 4 8 65 1 active sync /dev/sde1 3 8 49 2 active sync /dev/sdd1 1 8 33 - faulty /dev/sdc1 # 这个盘是坏掉的,我们已经加入了新的磁盘, 这个盘可以干掉了 [root@centos7 md5]$ man mdadm [root@centos7 md5]$ mdadm /dev/md5 --remove /dev/sdc1 # 这个盘我们从raid5中移除去。 mdadm: hot removed /dev/sdc1 from /dev/md5