linux磁盘管理软RAID的实现原理分析和方法分享

教程大全 2026-01-08 07:10:12 浏览

1 什么是raid

raid全称是独立磁盘冗余阵列(redundant array of independent disks),基本思想是把多个磁盘组合起来,组合一个磁盘阵列组,使得性能大幅提高。

raid分为几个不同的等级,各个不同的等级均在数据可靠性及读写性能做了不同的权衡。实际工作中根据自己的业务需求选择不同的raid方案。

2 raid的实现方式

外接式磁盘阵列:通过扩展卡提供适配能力内接式raid:主板集成raid控制器安装os前在bios里配置软件raid:通过os实现

3 标准的raid

raid0称为条带化存储,将数据分段存储在各个磁盘中,读写均可以并行处理,因此读写速率为单个磁盘的n倍,没有冗余功能,任何一个磁盘的损坏就会导致的数据不可用。

radi1是镜像存储,没有数据校验,数据被同等的写入到2个或者多个磁盘中,写入速度相对慢, 但是读取速度比较快。

radi4在raid1的基础上,n个盘用于数据存储,另外加入了1个磁盘作为校验盘。一共n+1个盘,任何一个盘坏掉也不影响数据的访问

raid5在raid4的基础上,由原来的一个盘来存储校验数据,改为每个盘都有数据和校验信息的。

4 混合raid

先组成raid0,然后组成raid1.

先组成raid1,然后组成raid0

5 软raid的实现

5.1 raid5的实现

创建由三块硬盘组成的可用空间为2g的raid5设备,要求其chunk大小为256k,文件系统为ext4,开机可自动挂载至/mydata目录

5.1.1 先看看我们的磁盘情况

[root@centos7 name maj:min mountpoint sda 8:0 0 200g 0 disk ├─sda1 8:1 0 1g 0 part ├─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 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 软RAID /run/media/root/centos

这里我们使用sdb,sdc,sdd,每个盘创建一个主分区1g,构建radi5.

5.1.2 根据实际情况分区

[root@centos7 bash]$ fdisk /dev/sdbwelcome 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 tablebuilding a new dos disklabel with disk identifier 0x93d380cf.command (m for help): npartition type: p primary (0 primary, 0 extended, 4 free) e extendedselect (default p): ppartition number (1-4, default 1):first sector (2048-209715199, default 2048):using default value 2048last sector, +sectors or +size{k,m,g} (2048-209715199, default 209715199): +1gpartition 1 of type linux and of size 1 gib is setcommand (m for help): tselected partition 1hex code (type l to list all codes): fdchanged type of partition 'linux' to 'linux raid autodetect'command (m for help): pdisk /dev/sdb: 107.4 gb, 107374182400 bytes, 209715200 sectorsunits = sectors of 1 * 512 = 512 bytessector size (logical/physical): 512 bytes / 512 bytesi/o size (minimum/optimal): 512 bytes / 512 bytesdisk label type: dosdisk identifier: 0x93d380cf device boot startend blocks id system/dev/sdb12048 2099199 1048576 fd linux raid autodetectcommand (m for help): wthe partition table has been altered!calling ioctl() to re-read partition table.syncing disks.[root@centos7 bash]$ fdisk /dev/sdcwelcome 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 tablebuilding a new dos disklabel with disk identifier 0xc56b90d8.command (m for help): npartition type: p primary (0 primary, 0 extended, 4 free) e extendedselect (default p): ppartition number (1-4, default 1):first sector (2048-41943039, default 2048):using default value 2048last sector, +sectors or +size{k,m,g} (2048-41943039, default 41943039): +1gpartition 1 of type linux and of size 1 gib is setcommand (m for help): tselected partition 1hex code (type l to list all codes): fdchanged type of partition 'linux' to 'linux raid autodetect'command (m for help): pdisk /dev/sdc: 21.5 gb, 21474836480 bytes, 41943040 sectorsunits = sectors of 1 * 512 = 512 bytessector size (logical/physical): 512 bytes / 512 bytesi/o size (minimum/optimal): 512 bytes / 512 bytesdisk label type: dosdisk identifier: 0xc56b90d8 device boot startend blocks id system/dev/sdc12048 2099199 1048576 fd linux raid autodetectcommand (m for help): wthe partition table has been altered!calling ioctl() to re-read partition table.syncing disks.[root@centos7 bash]$ fdisk /dev/sddwelcome 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 tablebuilding a new dos disklabel with disk identifier 0x7e0900d8.command (m for help): npartition type: p primary (0 primary, 0 extended, 4 free) e extendedselect (default p): ppartition number (1-4, default 1):first sector (2048-41943039, default 2048):using default value 2048last sector, +sectors or +size{k,m,g} (2048-41943039, default 41943039): +1gpartition 1 of type linux and of size 1 gib is setcommand (m for help): pdisk /dev/sdd: 21.5 gb, 21474836480 bytes, 41943040 sectorsunits = sectors of 1 * 512 = 512 bytessector size (logical/physical): 512 bytes / 512 bytesi/o size (minimum/optimal): 512 bytes / 512 bytesdisk label type: dosdisk identifier: 0x7e0900d8 device boot startend blocks id system/dev/sdd12048 2099199 1048576 83 linuxcommand (m for help): tselected partition 1hex code (type l to list all codes): fdchanged type of partition 'linux' to 'linux raid autodetect'command (m for help): wthe 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? ymdadm: defaulting to version 1.2 metadatamdadm: array /dev/md5 started.[root@centos7 bash]$ mdadm -ds# 查看信息array /dev/md5 metadata=1.2 name=centos7.magedu.com:5 uuid=2c8ae60d:a799fcb7:9008a046:ae6ea430[root@centos7 bash]$ mdadm -ds >/etc/mdadm.conf# 将软raid信息写入到配置文件中去[root@centos7 bash]$ mkdir /mnt/md5# 创建挂载点目录[root@centos7 bash]$ mkfs.ext4 /dev/md5# 创建文件系统mke2fs 1.42.9 (28-dec-2013)filesystem label=os type: linuxblock size=4096 (log=2)fragment size=4096 (log=2)stride=64 blocks, stripe width=128 blocks131072 inodes, 523776 blocks26188 blocks (5.00%) reserved for the super userfirst>
[root@centos7 md5]$ mdadm -d /dev/md5#查看详细raid5详细信息,可以发现有3个都是working状态的/dev/md5:version : 1.2creation time : wed dec 6 19:28:22 2017raid level : raid5array size : 2095104 (2046.00 mib 2145.39 mb)used dev size : 1047552 (1023.00 mib 1072.69 mb)raid devices : 3total devices : 3persistence : superblock is persistentupdate time : wed dec 6 19:39:06 2017state : cleanactive devices : 3working devices : 3failed devices : 0spare devices : 0layout : left-symmetricchunk size : 256kconsistency policy : resyncname : centos7.magedu.com:5 (local to host centos7.magedu.com)uuid : 2c8ae60d:a799fcb7:9008a046:ae6ea430events : 18number 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.2creation time : wed dec 6 19:28:22 2017raid level : raid5array size : 2095104 (2046.00 mib 2145.39 mb)used dev size : 1047552 (1023.00 mib 1072.69 mb)raid devices : 3total devices : 3persistence : superblock is persistentupdate time : wed dec 6 19:41:08 2017state : clean, degraded# 这里注意了。 我们的一个盘坏掉了。 raid5状态为降级使用了。active devices : 2working devices : 2failed devices : 1spare devices : 0layout : left-symmetricchunk size : 256kconsistency policy : resyncname : centos7.magedu.com:5 (local to host centos7.magedu.com)uuid : 2c8ae60d:a799fcb7:9008a046:ae6ea430events : 20number major minor raiddevice state0 8 17 0 active sync /dev/sdb1- 0 0 1 removed3 8 49 2 active sync /dev/sdd11 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.2creation time : wed dec 6 19:28:22 2017raid level : raid5array size : 2095104 (2046.00 mib 2145.39 mb)used dev size : 1047552 (1023.00 mib 1072.69 mb)raid devices : 3total devices : 4persistence : superblock is persistentupdate time : wed dec 6 19:50:01 2017state : clean# 状态恢复正常了。没有问题active devices : 3working devices : 3failed devices : 1spare devices : 0layout : left-symmetricchunk size : 256kconsistency policy : resyncname : centos7.magedu.com:5 (local to host centos7.magedu.com)uuid : 2c8ae60d:a799fcb7:9008a046:ae6ea430events : 43number major minor raiddevice state0 8 17 0 active sync /dev/sdb14 8 65 1 active sync /dev/sde13 8 49 2 active sync /dev/sdd11 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

5.1.6扩展raid

我们上面使用的是2+1构成的raid5,磁盘利用率为66%,如果我们想改成3+1 可以执行类似如下命令

[root@centos7 mnt]$ mkadm -g -r /dev/md5 -n 4 -a /dev/sdxx# 这里我就不测试了。使用/dev/sdxx代替一个设备。-g 是grown增长的意思,-r 是resizefs的意思,

5.1.7 清空raid信息

[root@centos7 mnt]$ umount /dev/md5# 卸载设备[root@centos7 mnt]$ mdadm -s /dev/md5# 停止raid5mdadm: stopped /dev/md5[root@centos7 mnt]$ sed -i '$d' /etc/fstab# 删除fstab中关于raid5挂载的行[root@centos7 mnt]$ cat /etc/fstab# 确保fstab没有大问题## /etc/fstab# created by anaconda on tue nov 7 16:07:01 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#uuid=59ccea87-3c4e-4bbc-9e2f-3fadb1dcf2e6 /ext4 defaults 1 1uuid=f4e867e8-bcde-43a2-adc7-c80b0948e85f /appext4 noatime,usrquota,grpquota 1 2uuid=1d6cbe88-ffb4-4adf-bacf-76be1fa75708 /bootext4 defaults 1 2#uuid=b2c064f5-1ee5-4b5c-9e75-ed41cb99c5aa swapswap defaults 0 0#uuid=a0516c4f-40e6-4919-905a-8b44db12ff7b swapswap defaults,pri=0 0 0#/dev/sdb2 /test ext4 rw,seclabel,relatime,data=ordered 0 0#/dev/sdb1 /home xfs rw,seclabel,relatime,attr2,inode64,usrquota,grpquota 0 0[root@centos7 mnt]$ rm -rf /etc/mdadm.conf# 删除raid默认配置文件
[root@centos7 mnt]$ mdadm --zero-superblock /dev/sd{b1,e1,d1,c1}# 清空设置上的超级块信息

5.2 raid10的实现

raid10 ,6个分区,2个一组raid1,3组raid0

5.2.1 案例分析

分析下,我们创建一个raid10设置,2个设备组成一个raid1,6个设备2个一组可以组成3个raid1, 然后把3个raid1组成一个raid0即可

5.2.2 先创建6个设备

[root@centos7 mnt]$ lsblk# 就是使用fdisk 创建的设备, 具体这里就不写了。 最终使用lsblk显示,我们可以看到sdb1,sdb2,sdd1,sde1一共6个磁盘name maj:min rm size ro type mountpointsda 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 /appsdb 8:16 0 100g 0 disk├─sdb1 8:17 0 1g 0 part└─sdb2 8:18 0 1g 0 partsdc 8:32 0 20g 0 disk├─sdc1 8:33 0 1g 0 part└─sdc2 8:34 0 1g 0 partsdd 8:48 0 20g 0 disk└─sdd1 8:49 0 1g 0 partsde 8:64 0 20g 0 disk└─sde1 8:65 0 1g 0 partsdf 8:80 0 20g 0 disksr0 11:0 1 8.1g 0 rom /run/media/root/centos 7 x86_64

5.2.3 创建raid

[root@centos7 mnt]$ mdadm -c /dev/md11 -a yes -l 1 -n 2 /dev/sd{b1,c1}# 创建第一个raid1mdadm: /dev/sdb1 appears to be part of a raid array: level=raid5 devices=3 ctime=wed dec 6 19:28:22 2017mdadm: 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.90mdadm: /dev/sdc1 appears to be part of a raid array: level=raid5 devices=3 ctime=wed dec 6 19:28:22 2017continue creating array? ymdadm: defaulting to version 1.2 metadatamdadm: array /dev/md11 started.[root@centos7 mnt]$ mdadm -c /dev/md12 -a yes -l 1 -n 2 /dev/sd{b2,c2}#创建第二个raid1mdadm: 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.90continue creating array? ymdadm: defaulting to version 1.2 metadatamdadm: array /dev/md12 started.[root@centos7 mnt]$ mdadm -c /dev/md13 -a yes -l 1 -n 2 /dev/sd{d1,e1}# 创建第三个raid1mdadm: /dev/sdd1 appears to be part of a raid array:level=raid5 devices=3 ctime=wed dec 6 19:28:22 2017mdadm: note: this array has metadata at the start andmay not be suitable as a boot device. if you plan tostore '/boot' on this device please ensure thatyour boot-loader understands md/v1.x metadata, or use--metadata=0.90mdadm: /dev/sde1 appears to be part of a raid array:level=raid5 devices=3 ctime=wed dec 6 19:28:22 2017continue creating array? ymdadm: defaulting to version 1.2 metadatamdadm: array /dev/md13 started.[root@centos7 mnt]$ mdadm -c /dev/md10 -a yes -l 0 -n 3 /dev/md{11,12,13}# 将3个raid1 合并为一个raid0mdadm: /dev/md11 appears to contain an ext2fs file systemsize=2095104k mtime=wed dec 6 19:29:45 2017mdadm: /dev/md13 appears to contain an ext2fs file systemsize=2095104k mtime=wed dec 6 19:29:45 2017continue creating array? ymdadm: defaulting to version 1.2 metadatamdadm: array /dev/md10 started.[root@centos7 mnt]$ mkfs.extmkfs.ext2 mkfs.ext3 mkfs.ext4[root@centos7 mnt]$ mkfs.ext4 /dev/md10# 创建文件系统mke2fs 1.42.9 (28-dec-2013)filesystem label=os type: linuxblock size=4096 (log=2)fragment size=4096 (log=2)stride=128 blocks, stripe width=384 blocks196224 inodes, 784896 blocks39244 blocks (5.00%) reserved for the super userfirst>
[root@centos7 mnt]$ umount /dev/md10# 取消挂载[root@centos7 mnt]$ rm -rf /etc/mdadm.conf# 删除mdadm的默认配置[root@centos7 mnt]$ mdadm -s /dev/md10# 停止raid0设置mdadm: stopped /dev/md10[root@centos7 mnt]$ mdadm -s /dev/md11# 停止raid1设置mdadm: stopped /dev/md11[root@centos7 mnt]$ mdadm -s /dev/md12# 停止radi1 设置mdadm: stopped /dev/md12[root@centos7 mnt]$ mdadm -s /dev/md13# 停止raid 1 设置mdadm: stopped /dev/md13[root@centos7 mnt]$ sed -i '$d' /etc/fstab# 删除fstab的挂载[root@centos7 mnt]$ cat /etc/fstab# 确保正确## /etc/fstab# created by anaconda on tue nov 7 16:07:01 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#uuid=59ccea87-3c4e-4bbc-9e2f-3fadb1dcf2e6 /ext4 defaults 1 1uuid=f4e867e8-bcde-43a2-adc7-c80b0948e85f /appext4 noatime,usrquota,grpquota 1 2uuid=1d6cbe88-ffb4-4adf-bacf-76be1fa75708 /bootext4 defaults 1 2#uuid=b2c064f5-1ee5-4b5c-9e75-ed41cb99c5aa swapswap defaults 0 0#uuid=a0516c4f-40e6-4919-905a-8b44db12ff7b swapswap defaults,pri=0 0 0#/dev/sdb2 /test ext4 rw,seclabel,relatime,data=ordered 0 0#/dev/sdb1 /home xfs rw,seclabel,relatime,attr2,inode64,usrquota,grpquota 0 0[root@centos7 mnt]$ mdadm -d# 再次查看下mdadm信息,确保没有了mdadm: no devices given.[root@centos7 mnt]$ mdadm --zero-superblock /dev/sd{b1,b2,c1,c2,d1,e1}# 请求md的元数据信息

以上就是我们给大家整理的在linux磁盘管理中实现软raid的方法讲解,大家有不明白的可以在下方的留言区讨论。

原文链接:

本文版权声明本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请联系本站客服,一经查实,本站将立刻删除。

发表评论

热门推荐