您的位置:首页 > 运维架构 > Linux

linux自学笔记——RAID级别特性以及软RAID的实现

2017-08-09 17:03 288 查看
RAID,Redundant Arrays of Inexpensive Disks 廉价冗余磁盘阵列,又称为Redundant Arrays of Independent Disks,独立冗余磁盘阵列。其基本原理就是利用多块较小的磁盘通过不同的组织方式,组成一个大的磁盘组,以提高磁盘的IO能力和耐用性。由于组织方式不同,所以把RAID分为多个级别。而最常用的是RAID0,RAID1,RAID5,RAID6,RAID10,RAID01.下面我们将对以上的级别进行比较分析。级别:level RAID 0 :RAID 0又称为条带存储,基本原理就是将数据划分为数据块分别存于各个磁盘中。 读写性能得到提升,由于数据在写入时被划分为N块,理论上,读写速度将是之前的N倍; 可用空间为:N*min(min为容量最小的那块磁盘容量); 无容错能力,当一块磁盘损坏时,磁盘内的数据将会发生损坏; 最少磁盘数:2,2+; RAID 0如右下图所示:

RAID 1:RAID 1又称为镜像存储,基本原理就是将数据分别存入磁盘阵列中的每一块磁盘中,实现数据备份。 读性能提升,写性能略有下降,由于数据被百分之百的复制到了其他磁盘,所以写性能下降,而读取性能提升; 可用空间为:1*min(min为容量最小的磁盘的容量); 有冗余能力,当一块磁盘发生损坏时,数据不会发生损坏; 最少磁盘数:2,2+; RAID 1如下图所示:

RAID 5:RAID 5采用校验码和并行传送技术,把数据和对应的XOR校验码分别存储在不同的磁盘上,其中全部的的XOR 校验码占据的磁盘空间为一块磁盘的大小,剩余的N-1块磁盘存放数据。 读,写性能提升,由于在写入数据时需要不断计算校验码的关系,其写入速率略有下降; 可用空间:(N-1)*min(min为容量最小的磁盘的容量); 有容错能力:一块磁盘,当一块磁盘放生损坏时,通过校验码可以恢复磁盘的数据,数据安全性得到了保证; 最少磁盘数:3,3+; RAID 5如右下图所示:

RAID 6:RAID 6,采用两个独立分布式校验码,在RAID 5的基础上多了一个校验码; 读写性能提升,同样写入数据需要计算校验码关系,写入速率下降; 可用空间:(N-2)*min(容量最小的磁盘的容量) 有容错能力:2,采用两个分布式校验码,就算有两块磁盘同时损坏,数据也能恢复,数据非常安全; 最少磁盘数:4,4+; RAID 6如右下图所示:

混合类型:RAID 10:RAID 10就是先做RAID 1,通过控制器将磁盘划分为镜像卷,进行冗余,再做RAID 0,通过控制器将数据 分为条带存储。 读写性能提升; 可用空间:N*min/2,空间利用率降低了50%; 有容错能力:每组镜像最多只能坏一块; 最少磁盘数:4,4+; RAID 10如右下图所示

RAID 01:RAID 01与RAID 10相反,先做RAID 0,后做RAID 1,读写性能得到了提升,空间利用率为50%;以上为RAID常用的几个级别的特性,下面我们将对其实现方式做一些说明。RAID的实现方式可分为两种,一种是硬件实现方式,另外一种是软件实现方式。硬件实现方式是基于硬件RAID卡实现,而软件的RAID是通过madm工具来实现的,我们先对模式化工具mdadm进行简单的了解。mdadm命令: mdadm - manage MD devices aka Linux Software RAID 语句格式为mdadm [mode] <raiddevice> [options]<component-devices>模式:创建:-C 装配:-A监控:-F管理:-f,-r,-a<raiddevice>:/dev/md#<component-devices>:任意块设备 -C:创建模式 -n #:使用#块磁盘来创建此设备; -l #:指明要创建的RAID的级别; -a {yes|no}:自动创建目标RAID的设备文件; -c CHUNK SIZE:指明块大小; -x #:指明空闲盘的个数; -D:显示RAID的详细信息: mdadm –D /dev/md#管理模式: -f:标记指定的磁盘为损坏; -a:添加磁盘; -r:移除磁盘;观察md设备的状态: cat /proc/mdstat停止md设备: mdadm –S /dev/md#Watch命令: -n #:刷新间隔,单位为秒; Watch –n# ‘COMMAND’以上为mdadm的基础用法,下面我们将通过实例建一个软RAID:1. 使用fdisk命令划分4个分区,其中3个做RAID 5,还有一个做空闲盘;[root@claude ~]# fdisk/dev/sdbDevice contains neither a valid DOSpartition table, nor Sun, SGI or OSF disklabelBuilding a new DOS disklabel with diskidentifier 0x262f7c1e.Changes will remain in memory only, untilyou decide to write them.After that, of course, the previous contentwon't be recoverable. Warning: invalid flag 0x0000 of partitiontable 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): nCommand action e extended p primary partition (1-4)ePartition number (1-4): 1First cylinder (1-2610, default 1): Using default value 1Last cylinder, +cylinders or +size{K,M,G}(1-2610, default 2610): Using default value 2610 Command (m for help): nCommand action l logical (5 or over) p primary partition (1-4)lFirst cylinder (1-2610, default 1): Using default value 1Last cylinder, +cylinders or +size{K,M,G}(1-2610, default 2610): +2G Command (m for help): NCommand action l logical (5 or over) p primary partition (1-4)LFirst cylinder (263-2610, default 263): Using default value 263Last cylinder, +cylinders or +size{K,M,G}(263-2610, default 2610): +2G Command (m for help): NCommand action l logical (5 or over) p primary partition (1-4)LFirst cylinder (525-2610, default 525): Using default value 525Last cylinder, +cylinders or +size{K,M,G}(525-2610, default 2610): +2G Command (m for help): NCommand action l logical (5 or over) p primary partition (1-4)LFirst cylinder (787-2610, default 787): Using default value 787Last cylinder, +cylinders or +size{K,M,G}(787-2610, default 2610): +2G Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280bytesSector size (logical/physical): 512 bytes /512 bytesI/O size (minimum/optimal): 512 bytes / 512bytesDisk identifier: 0x262f7c1e Device Boot Start End Blocks Id System/dev/sdb1 1 2610 20964793+ 5 Extended/dev/sdb5 1 262 2104452 83 Linux/dev/sdb6 263 524 2104483+ 83 Linux/dev/sdb7 525 786 2104483+ 83 Linux/dev/sdb8 787 1048 2104483+ 83 Linux2. 分区都创建好以后将分区的id都改为fd格式,并保存退出:Command (m for help): tPartitionnumber (1-8): 5Hex code(type L to list codes): fdChangedsystem type of partition 5 to fd (Linux raid autodetect) Command (m for help): tPartitionnumber (1-8): 6Hex code(type L to list codes): fdChangedsystem type of partition 6 to fd (Linux raid autodetect) Command (m for help): tPartitionnumber (1-8): 7Hex code(type L to list codes): fdChangedsystem type of partition 7 to fd (Linux raid autodetect) Command (m for help): tPartitionnumber (1-8): 8Hex code(type L to list codes): fdChangedsystem type of partition 8 to fd (Linux raid autodetect) Command (m for help): p Disk/dev/sdb: 21.5 GB, 21474836480 bytes255 heads,63 sectors/track, 2610 cylindersUnits =cylinders of 16065 * 512 = 8225280 bytesSectorsize (logical/physical): 512 bytes / 512 bytesI/O size(minimum/optimal): 512 bytes / 512 bytesDiskidentifier: 0x262f7c1e Device Boot Start End Blocks Id System/dev/sdb1 1 2610 20964793+ 5 Extended/dev/sdb5 1 262 2104452 fd Linux raid autodetect/dev/sdb6 263 524 2104483+ fd Linux raid autodetect/dev/sdb7 525 786 2104483+ fd Linux raid autodetect/dev/sdb8 787 1048 2104483+ fd Linux raid autodetectCommand (m for help): w3. 所有分区都已建好,效果如下:[root@claude~]# fdisk -l /dev/sdb Disk/dev/sdb: 21.5 GB, 21474836480 bytes255 heads,63 sectors/track, 2610 cylindersUnits =cylinders of 16065 * 512 = 8225280 bytesSectorsize (logical/physical): 512 bytes / 512 bytesI/O size(minimum/optimal): 512 bytes / 512 bytesDiskidentifier: 0x262f7c1e Device Boot Start End Blocks Id System/dev/sdb1 1 2610 20964793+ 5 Extended/dev/sdb5 1 262 2104452 fd Linux raid autodetect/dev/sdb6 263 524 2104483+ fd Linux raid autodetect/dev/sdb7 525 786 2104483+ fd Linux raid autodetect/dev/sdb8 787 1048 2104483+ fd Linux raid autodetect4. 对磁盘做RAID 5;[root@claude~]# mdadm -C /dev/md0 -a yes -l 5-n 3 -x 1 /dev/sdb{5,6,7,8}mdadm:Defaulting to version 1.2 metadatamdadm:array /dev/md0 started.5. 查看md状态;[root@claude~]# watch -n1 cat /proc/mdstatPersonalities: [raid6] [raid5] [raid4] md0 :active raid5 sdb7[4] sdb8[3](S) sdb6[1] sdb5[0] 4204544 blocks super 1.2 level 5, 512kchunk, algorithm 2 [3/3] [UUU] unuseddevices: <none>6. 格式化磁盘;[root@claude~]# mke2fs -t ext4 /dev/md0mke2fs1.41.12 (17-May-2010)Filesystemlabel=OS type:LinuxBlocksize=4096 (log=2)Fragmentsize=4096 (log=2)Stride=128blocks, Stripe width=256 blocks262944inodes, 1051136 blocks52556blocks (5.00%) reserved for the super userFirst datablock=0Maximumfilesystem blocks=107793612833 blockgroups32768blocks per group, 32768 fragments per group7968inodes per groupSuperblockbackups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200,884736 Writinginode tables: done Creatingjournal (32768 blocks): doneWritingsuperblocks and filesystem accounting information: done Thisfilesystem will be automatically checked every 30 mounts or180 days,whichever comes first. Use tune2fs -c or-i to override.7. 挂载检验RAID是否成功创建[root@claude~]# mkdir mydata #创建挂载点[root@claude~]# lsanaconda-ks.cfg Desktop Downloads install.log.syslog mydata Public Videosbin Documents install.log Music Pictures Templates[root@claude~]# mount /dev/md0 /mydata #挂载RAID[root@claude~]# mount/dev/sda2on / type ext4 (rw)proc on/proc type proc (rw)sysfs on/sys type sysfs (rw)devpts on/dev/pts type devpts (rw,gid=5,mode=620)tmpfs on /dev/shmtype tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")none on/proc/sys/fs/binfmt_misc type binfmt_misc (rw)/dev/md0on /mydata type ext4 (rw)[root@claude~]# mdadm -D /dev/md0/dev/md0: Version : 1.2 Creation Time : Tue Aug 8 23:56:04 2017 Raid Level : raid5 Array Size : 4204544 (4.01 GiB 4.31 GB) Used Dev Size : 2102272 (2.00 GiB 2.15 GB) Raid Devices : 3 Total Devices : 4 Persistence : Superblock is persistent Update Time : Wed Aug 9 00:02:23 2017 State : clean Active Devices : 3WorkingDevices : 4 Failed Devices : 0 Spare Devices : 1 Layout : left-symmetric Chunk Size : 512K Name : claude.amax:0 (local to host claude.amax) UUID :31cf9e3b:5296ea45:b78b0df8:c0a5f09b Events : 18 Number Major Minor RaidDevice State 0 8 21 0 active sync /dev/sdb5 1 8 22 1 active sync /dev/sdb6 4 8 23 2 active sync /dev/sdb7 3 8 24 - spare /dev/sdb8[root@claude~]# df -lh /mydata #查看磁盘容量Filesystem Size Used Avail Use% Mounted on/dev/md0 3.9G 8.1M 3.7G 1% /mydata如果要让此文件系统自动挂载,需要在/etc/fstab中更改配置文件;模拟RAID5其中一块磁盘损坏,空闲盘会自动补充到RAID5中去;[root@claude~]# mdadm /dev/md0 -f /dev/sdb5 #模拟磁盘损坏mdadm: set/dev/sdb5 faulty in /dev/md0[root@claude~]# mdadm -D /dev/md0 #查看md0状态/dev/md0: Version : 1.2 Creation Time : Tue Aug 8 23:56:04 2017 Raid Level : raid5 Array Size : 4204544 (4.01 GiB 4.31 GB) Used Dev Size : 2102272 (2.00 GiB 2.15 GB) Raid Devices : 3 Total Devices : 4 Persistence : Superblock is persistent Update Time : Wed Aug 9 00:11:00 2017 State : clean Active Devices : 3WorkingDevices : 3 Failed Devices : 1 Spare Devices : 0 Layout : left-symmetric Chunk Size : 512K Name : claude.amax:0 (local to host claude.amax) UUID :31cf9e3b:5296ea45:b78b0df8:c0a5f09b Events : 37 Number Major Minor RaidDevice State 3 8 24 0 active sync /dev/sdb8 1 8 22 1 active sync /dev/sdb6 4 8 23 2 active sync /dev/sdb7 0 8 21 - faulty /dev/sdb5[root@claude~]# mdadm /dev/md0 -r /dev/sdb5 #移除磁盘mdadm: hotremoved /dev/sdb5 from /dev/md0[root@claude~]# mdadm /dev/md0 -a /dev/sdb5 #增加磁盘mdadm:added /dev/sdb5
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  RAID LINUX