Liunx软Raid实现

                              Liunx软Raid实现

                                                    作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

  在配置软Raid之前,我们首先要了解一下什么是Raid,Raid又分哪几个系列?哪个又是最靠谱的组合?我们生产环境中有必要用Raid吗?各个级别的Raid适合什么样的生产场景等等,带着这一些列的问题,让我们一起先了解一下Raid.

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 '''
 8 仅用于标识磁盘组合方式的不同
 9 
10 Raid0:
11 工作原理:
12     Raid0是所有raid中存储性能最强的阵列形式。其工作原理就是在多个磁盘上分散存取连续的数据,这样,当需要存取数据是多个磁盘可以并排执行,每个磁盘执行属于它自己的那部分数据请求,显著提高磁盘整体存取性能。
13 适用场景:
14     至少需要两块磁盘,没有容错能力,读写性能都提示,  磁盘空间利用率提升了100%,两块磁盘型号最好要一样,一般存放swap,或者/tmp目录的,适用于低成本、低可靠性的台式系统。
15 
16 Raid1:
17 工作原理:
18     又称镜像盘,把一个磁盘的数据镜像到另一个磁盘上,采用镜像容错来提高可靠性,具有raid中最高的数据冗余能力。存数据时会将数据同时写入镜像盘内,读取数据则只从工作盘读出。发生故障时,系统将从镜像盘读取数据,然后再恢复工作盘正确数据。这种阵列方式可靠性极高,但是其容量会减去一半。
19 适用场景:
20     至少需要两块磁盘,镜像,具有硬件容错能力,读性能提升,写性能下降,磁盘空间利用率只有50%。广泛用于数据要求极严的应用场合,如商业金融、档案管理等领域。只允许一颗硬盘出故障。要注意的是,具有硬件容错能力 != 你可以对数据不进行备份。
21     因此对重要数据的备份一定要做好。
22 
23 Raid4:
24 工作原理:
25     至少需要三块磁盘,两块盘存数据,一块盘单独用来存另外两块磁盘的校验值。读写性能有所提升,读写性能(n-1)/n。而Raid5是缺吧数据和校验值打乱,分别存到3快磁盘上去。详情可以参考Raid5介绍。Raid生产环境很少人用。
26 
27 Raid5:
28 工作原理:
29     Raid5可以看成是Raid0+1的低成本方案。采用循环偶校验独立存取的阵列方式。将数据和相对应的奇偶校验信息分布存储到组成RAID5的各个磁盘上。当其中一个磁盘数据发生损坏后,利用剩下的磁盘和相应的奇偶校验信息 重新恢复/生成丢失的数据而不影响数据的可用性。
30 适用场景:
31     至少需要3个或以上的硬盘。适用于大数据量的操作。成本稍高、储存新强、可靠性强的阵列方式。适合用来安装操作系统。
32 
33 Raid6:
34 工作原理:
35     其实他就是在Raid5上做的一个优化,存储机制和Raid5类似,只不过多了一块磁盘做热备,当其中一块磁盘坏掉时,另外一块磁盘立即补位,完成存储功能。
36 适用场景:
37     至少需要四块磁盘,允许两块盘出错,读写性能提升,磁盘利用率(n-2)/n
38 
39 Raid10:
40 工作原理:
41     其实就是Raid1+Raid0的组合,至少需要四块磁盘,允许不同组内各坏一块磁盘,读写性能提升,磁盘使用率50%。
42 使用场景:
43     如果有重要数据的话,建议用这种模式,该模式是就有冗余能力的。不建议用Raid5或者Raid01来存取重要的数据,因为Raid5不靠谱,当一块磁盘坏掉的话,工作性能变得特别差!如果在坏一块的话就彻底不能工作了。
44 
45 Raid01:
46     将Raid0和Raid1技术结合在一起,兼顾两者的优势。在数据得到保障的同时,还能提供较强的存储性能。不过至少要求4个或以上的硬盘,也只运行一个磁盘出错。是一种高成本、高可靠性、高存储性能的三高阵列技术。
47 
48 
49 后记:
50     软Raid没有硬Raid性能好,不建议使用,因为软Raid是操作系统提供的一个MD模块来实现的,如果用软Raid去存取数据,对CPU消耗也是很大的,会降低服务器性能。虽然比不上硬Raid,但是有总比没有好,做了软Raid就是比不做Raid要强这是没话说的。
51     软Raid只能识别的文件系统表示是:FD,因此我们在配置的时候一定要注意。
52 
53 '''

  通过以上的知识点,我们了解到了Raid比较常见的几个级别,我以上还没有介绍完呢,还有很多类型。但是这不是重点,即使有我告诉你我也不会配置,我就会配置常见的几款Raid级别,一会我会一一揭晓的,在配置之前,我们需要做一个准备工作,比如,我们配置一个创建一个大小为6G的RAID0,根据其原理,我们可以创建2个分区,而且2个分区空间大小一样。好,让我们一起来做一下准备工作。

  1 #!/usr/bin/env python
  2 #_*_coding:utf-8_*_
  3 #@author :yinzhengjie
  4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
  5 #EMAIL:y1053419035@qq.com
  6 
  7 '''
  8 [root@yinzhengjie ~]# fdisk /dev/sdb  #对sdb这块新硬盘进行分区
  9 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
 10 Building a new DOS disklabel with disk identifier 0xc9f2a383.
 11 Changes will remain in memory only, until you decide to write them.
 12 After that, of course, the previous content won't be recoverable.
 13 
 14 Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
 15 
 16 WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
 17          switch off the mode (command 'c') and change display units to
 18          sectors (command 'u').
 19 
 20 Command (m for help): p  #查看分区信息
 21 
 22 Disk /dev/sdb: 21.5 GB, 21474836480 bytes
 23 255 heads, 63 sectors/track, 2610 cylinders
 24 Units = cylinders of 16065 * 512 = 8225280 bytes
 25 Sector size (logical/physical): 512 bytes / 512 bytes
 26 I/O size (minimum/optimal): 512 bytes / 512 bytes
 27 Disk identifier: 0xc9f2a383
 28 
 29    Device Boot      Start         End      Blocks   Id  System
 30 
 31 Command (m for help): n  #创建一个新的分区
 32 Command action
 33    e   extended
 34    p   primary partition (1-4)
 35 p  #指定分区类型为主分区
 36 Partition number (1-4): 1  #定义编号
 37 First cylinder (1-2610, default 1):  #指定其实位置,如果不指定,默认从1开始分配。
 38 Using default value 1
 39 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +3G   #指定结束位置为3G,因此这里是创建一个3G大小的分区
 40 
 41 Command (m for help): N  #新建一个分区
 42 Command action
 43    e   extended
 44    p   primary partition (1-4)
 45 P  #配置其模式为主分区
 46 Partition number (1-4): 2  #设置编号为2
 47 First cylinder (394-2610, default 394):
 48 Using default value 394
 49 Last cylinder, +cylinders or +size{K,M,G} (394-2610, default 2610): +3G
 50 
 51 Command (m for help): w  #保存以上的配置
 52 The partition table has been altered!
 53 
 54 Calling ioctl() to re-read partition table.
 55 Syncing disks.
 56 [root@yinzhengjie ~]# cat /proc/partitions  #查看分区信息,由于我这是款新硬盘,可以不用重读分区表系统就能自动识别出来。
 57 major minor  #blocks  name
 58 
 59    8       16   20971520 sdb
 60    8       17    3156741 sdb1
 61    8       18    3156772 sdb2
 62    8        0   20971520 sda
 63    8        1     307200 sda1
 64    8        2   18631680 sda2
 65    8        3    2031616 sda3
 66    8       32   20971520 sdc
 67 [root@yinzhengjie ~]# fdisk  /dev/sdb  #由于我们需要做Raid,因此我们需要对分区做修改,即修改分区的类型
 68 
 69 WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
 70          switch off the mode (command 'c') and change display units to
 71          sectors (command 'u').
 72 
 73 Command (m for help): p  #查看当前分区情况,请注意其ID信息
 74 
 75 Disk /dev/sdb: 21.5 GB, 21474836480 bytes
 76 255 heads, 63 sectors/track, 2610 cylinders
 77 Units = cylinders of 16065 * 512 = 8225280 bytes
 78 Sector size (logical/physical): 512 bytes / 512 bytes
 79 I/O size (minimum/optimal): 512 bytes / 512 bytes
 80 Disk identifier: 0xc9f2a383
 81 
 82    Device Boot      Start         End      Blocks   Id  System
 83 /dev/sdb1               1         393     3156741   83  Linux
 84 /dev/sdb2             394         786     3156772+  83  Linux
 85 
 86 Command (m for help): t  #修改ID
 87 Partition number (1-4): 1  #选择分区标号
 88 Hex code (type L to list codes): fd  #指定RAID可以识别的分区类型
 89 Changed system type of partition 1 to fd (Linux raid autodetect)
 90 
 91 Command (m for help): t  #原理同上
 92 Partition number (1-4): 2
 93 Hex code (type L to list codes): fd
 94 Changed system type of partition 2 to fd (Linux raid autodetect)
 95 
 96 Command (m for help): p  #注意观察IP情况和System的变化
 97 
 98 Disk /dev/sdb: 21.5 GB, 21474836480 bytes
 99 255 heads, 63 sectors/track, 2610 cylinders
100 Units = cylinders of 16065 * 512 = 8225280 bytes
101 Sector size (logical/physical): 512 bytes / 512 bytes
102 I/O size (minimum/optimal): 512 bytes / 512 bytes
103 Disk identifier: 0xc9f2a383
104 
105    Device Boot      Start         End      Blocks   Id  System
106 /dev/sdb1               1         393     3156741   fd  Linux raid autodetect
107 /dev/sdb2             394         786     3156772+  fd  Linux raid autodetect
108 
109 Command (m for help): w  #保存当前配置
110 The partition table has been altered!
111 
112 Calling ioctl() to re-read partition table.
113 Syncing disks.
114 [root@yinzhengjie ~]#
115 
116 '''

  做好了准备环境之后,配置起来就简单的多了,其实只需要一条命令就搞定了,我们如果配置呢?请往下看:

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 '''
 8 [root@yinzhengjie ~]# mdadm -C /dev/md0 -a yes -l 0 -n 2 /dev/sdb{1,2}  #创建一个RAID0
 9 mdadm: Defaulting to version 1.2 metadata
10 mdadm: array /dev/md0 started.
11 [root@yinzhengjie ~]# cat /proc/mdstat   #查看RAID信息
12 Personalities : [raid0] #提示属性是RAID0
13 md0 : active raid0 sdb2[1] sdb1[0]  #该状态为活跃状态,后面跟了2个设备名称,其实就是我们刚刚分的2个分区。
14       6308864 blocks super 1.2 512k chunks
15 
16 unused devices: <none>
17 [root@yinzhengjie ~]#
18 [root@yinzhengjie ~]# mdadm -D /dev/md0 #从上面我们看到了有个"md0"的标记,我们可以看它的具体详细信息系,当然这个-D就等价于--detail参数,下面我也有演示
19 /dev/md0:
20         Version : 1.2
21   Creation Time : Mon May 15 05:56:32 2017
22      Raid Level : raid0  #显示RAID的级别
23      Array Size : 6308864 (6.02 GiB 6.46 GB)  #显示RAID的磁盘大小
24    Raid Devices : 2  #表示当前RAID级别有几块设备
25   Total Devices : 2  #表示总数的设备
26     Persistence : Superblock is persistent
27 
28     Update Time : Mon May 15 05:56:32 2017  #更新时间
29           State : clean
30  Active Devices : 2  #活动状态的
31 Working Devices : 2  #处于工作状态的,如果这个数字不正常的话,那么你的RAID就真的出现问题了,因为我们知道RAID0是没有冗余效果的。
32  Failed Devices : 0
33   Spare Devices : 0
34 
35      Chunk Size : 512K
36 
37            Name : yinzhengjie:0  (local to host yinzhengjie)
38            UUID : 0fe39292:6216e860:6f92259a:38b22186
39          Events : 0
40 
41     Number   Major   Minor   RaidDevice State
42        0       8       17        0      active sync   /dev/sdb1
43        1       8       18        1      active sync   /dev/sdb2
44 [root@yinzhengjie ~]# mdadm --detail /dev/md0
45 /dev/md0:
46         Version : 1.2
47   Creation Time : Mon May 15 05:56:32 2017
48      Raid Level : raid0
49      Array Size : 6308864 (6.02 GiB 6.46 GB)
50    Raid Devices : 2
51   Total Devices : 2
52     Persistence : Superblock is persistent
53 
54     Update Time : Mon May 15 05:56:32 2017
55           State : clean
56  Active Devices : 2
57 Working Devices : 2
58  Failed Devices : 0
59   Spare Devices : 0
60 
61      Chunk Size : 512K
62 
63            Name : yinzhengjie:0  (local to host yinzhengjie)
64            UUID : 0fe39292:6216e860:6f92259a:38b22186
65          Events : 0
66 
67     Number   Major   Minor   RaidDevice State
68        0       8       17        0      active sync   /dev/sdb1
69        1       8       18        1      active sync   /dev/sdb2
70 [root@yinzhengjie ~]#
71 
72 '''

  好了,上面是配置RAID0的全过程,那么配置RAID1又是如何做到的呢?其实so easy,用法几乎一致,首先你得配置好环境,假如我们创建一个18G的RAID1我们应该如何做呢?

  1 #!/usr/bin/env python
  2 #_*_coding:utf-8_*_
  3 #@author :yinzhengjie
  4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
  5 #EMAIL:y1053419035@qq.com
  6 
  7 '''
  8 
  9 [root@yinzhengjie ~]# fdisk /dev/sdc #对新设备进行分区
 10 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
 11 Building a new DOS disklabel with disk identifier 0x7edec25e.
 12 Changes will remain in memory only, until you decide to write them.
 13 After that, of course, the previous content won't be recoverable.
 14 
 15 Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
 16 
 17 WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
 18          switch off the mode (command 'c') and change display units to
 19          sectors (command 'u').
 20 
 21 Command (m for help): P #查看当前分区情况
 22 
 23 Disk /dev/sdc: 21.5 GB, 21474836480 bytes
 24 255 heads, 63 sectors/track, 2610 cylinders
 25 Units = cylinders of 16065 * 512 = 8225280 bytes
 26 Sector size (logical/physical): 512 bytes / 512 bytes
 27 I/O size (minimum/optimal): 512 bytes / 512 bytes
 28 Disk identifier: 0x7edec25e
 29 
 30    Device Boot      Start         End      Blocks   Id  System
 31 
 32 Command (m for help): N #创建一个新的分区
 33 Command action
 34    e   extended
 35    p   primary partition (1-4)
 36 P
 37 Partition number (1-4): 1
 38 First cylinder (1-2610, default 1):
 39 Using default value 1
 40 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +9G
 41 
 42 Command (m for help): N  #在此创建一个新的分区
 43 Command action
 44    e   extended
 45    p   primary partition (1-4)
 46 P
 47 Partition number (1-4): 2
 48 First cylinder (1177-2610, default 1177):
 49 Using default value 1177
 50 Last cylinder, +cylinders or +size{K,M,G} (1177-2610, default 2610): +9G
 51 
 52 Command (m for help): P  #创建完毕后,看一下您创建的是否正确
 53 
 54 Disk /dev/sdc: 21.5 GB, 21474836480 bytes
 55 255 heads, 63 sectors/track, 2610 cylinders
 56 Units = cylinders of 16065 * 512 = 8225280 bytes
 57 Sector size (logical/physical): 512 bytes / 512 bytes
 58 I/O size (minimum/optimal): 512 bytes / 512 bytes
 59 Disk identifier: 0x7edec25e
 60 
 61    Device Boot      Start         End      Blocks   Id  System
 62 /dev/sdc1               1        1176     9446188+  83  Linux
 63 /dev/sdc2            1177        2352     9446220   83  Linux
 64 Command (m for help): t  #修改一下分区类型为FD
 65 Partition number (1-4): 1
 66 Hex code (type L to list codes): fd
 67 Changed system type of partition 1 to fd (Linux raid autodetect)
 68 
 69 Command (m for help): t
 70 Partition number (1-4): 2
 71 Hex code (type L to list codes): fd
 72 Changed system type of partition 2 to fd (Linux raid autodetect)
 73 
 74 Command (m for help): p  #修改完毕后请确认是否为FD,如果这里不是FD,做RAID会报错的。
 75 
 76 Disk /dev/sdc: 21.5 GB, 21474836480 bytes
 77 255 heads, 63 sectors/track, 2610 cylinders
 78 Units = cylinders of 16065 * 512 = 8225280 bytes
 79 Sector size (logical/physical): 512 bytes / 512 bytes
 80 I/O size (minimum/optimal): 512 bytes / 512 bytes
 81 Disk identifier: 0x7edec25e
 82 
 83    Device Boot      Start         End      Blocks   Id  System
 84 /dev/sdc1               1        1176     9446188+  fd  Linux raid autodetect
 85 /dev/sdc2            1177        2352     9446220   fd  Linux raid autodetect
 86 
 87 Command (m for help): w  #保存当前配置。
 88 The partition table has been altered!
 89 
 90 Calling ioctl() to re-read partition table.
 91 Syncing disks.
 92 [root@yinzhengjie ~]#
 93 [root@yinzhengjie ~]# cat /proc/partitions  #验证一下系统是否识别分区,如果没有识别用partx -a参数进行一下重读分区表即可,这里我用的是新磁盘实验,就不必了。
 94 major minor  #blocks  name
 95 
 96    8       16   20971520 sdb
 97    8       17    3156741 sdb1
 98    8       18    3156772 sdb2
 99    8        0   20971520 sda
100    8        1     307200 sda1
101    8        2   18631680 sda2
102    8        3    2031616 sda3
103    8       32   20971520 sdc
104    8       33    9446188 sdc1  #看到了自己的分区。
105    8       34    9446220 sdc2
106    9        0    6308864 md0
107 [root@yinzhengjie ~]#
108 
109 
110 '''
在/dev/sdb上配置好2个分区

   做好以上的配置环境之后,其实配置起来就很简单的,几乎和上面配置事一模一样的。

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 '''
 8 [root@yinzhengjie ~]# mdadm -C /dev/md1 -a yes -l 1 -n 2 /dev/sdc{1,2}  #创建RAID1
 9 mdadm: Note: this array has metadata at the start and
10     may not be suitable as a boot device.  If you plan to
11     store '/boot' on this device please ensure that
12     your boot-loader understands md/v1.x metadata, or use
13     --metadata=0.90
14 Continue creating array? yes  #这个消息忽略即可,输入yes
15 mdadm: Defaulting to version 1.2 metadata
16 mdadm: array /dev/md1 started.
17 [root@yinzhengjie ~]#
18 [root@yinzhengjie ~]# cat /proc/mdstat  #在此查看RAID信息
19 Personalities : [raid0] [raid1]
20 md1 : active raid1 sdc2[1] sdc1[0]
21       9437952 blocks super 1.2 [2/2] [UU]
22       [====>................]  resync = 23.3% (2200064/9437952) finish=0.5min speed=220006K/sec  #我们发现RAID1正在把2个分区制作成镜像的过程,目前已经完成了23.3%,我们可以过段时间在来查看,也可以用watch命令实时查看进度情况。
23 
24 md0 : active raid0 sdb2[1] sdb1[0]
25       6308864 blocks super 1.2 512k chunks
26 
27 unused devices: <none>
28 [root@yinzhengjie ~]#
29 [root@yinzhengjie ~]# watch -n 1  'cat /proc/mdstat'  #我们也可以通过监控命令实时查看进度
30 Every 1.0s: cat /proc/mdstat                                             Mon May 15 06:23:40 2017
31 
32 Personalities : [raid0] [raid1]
33 md1 : active raid1 sdc2[1] sdc1[0]
34       9437952 blocks super 1.2 [2/2] [UU]
35 
36 md0 : active raid0 sdb2[1] sdb1[0]
37       6308864 blocks super 1.2 512k chunks
38 
39 unused devices: <none>
40 [root@yinzhengjie ~]#
41 [root@yinzhengjie ~]# mdadm -D /dev/md1 #查看RAID1的信息
42 /dev/md1:
43         Version : 1.2
44   Creation Time : Mon May 15 06:19:24 2017
45      Raid Level : raid1
46      Array Size : 9437952 (9.00 GiB 9.66 GB)
47   Used Dev Size : 9437952 (9.00 GiB 9.66 GB)
48    Raid Devices : 2
49   Total Devices : 2
50     Persistence : Superblock is persistent
51 
52     Update Time : Mon May 15 06:20:11 2017
53           State : clean 
54  Active Devices : 2
55 Working Devices : 2
56  Failed Devices : 0
57   Spare Devices : 0
58 
59            Name : yinzhengjie:1  (local to host yinzhengjie)
60            UUID : cc10bfb8:cef2b77a:6a72b4b6:8c58484b
61          Events : 17
62 
63     Number   Major   Minor   RaidDevice State
64        0       8       33        0      active sync   /dev/sdc1
65        1       8       34        1      active sync   /dev/sdc2
66 [root@yinzhengjie ~]# 
67 '''

   我们已经配置好RAI0和RAID1啦,那么我们光创建不用不就白扯了么。所以我们来用一下这文件,在用之前,我们需要格式化,挂载,之后才能使用,

 1 [root@yinzhengjie ~]# mke2fs -t ext4 /dev/md0
 2 mke2fs 1.41.12 (17-May-2010)
 3 Filesystem label=
 4 OS type: Linux
 5 Block size=4096 (log=2)
 6 Fragment size=4096 (log=2)
 7 Stride=128 blocks, Stripe width=256 blocks
 8 394352 inodes, 1577216 blocks
 9 78860 blocks (5.00%) reserved for the super user
10 First data block=0
11 Maximum filesystem blocks=1619001344
12 49 block groups
13 32768 blocks per group, 32768 fragments per group
14 8048 inodes per group
15 Superblock backups stored on blocks: 
16         32768, 98304, 163840, 229376, 294912, 819200, 884736
17 
18 Writing inode tables: done                            
19 Creating journal (32768 blocks): done
20 Writing superblocks and filesystem accounting information: done
21 
22 This filesystem will be automatically checked every 39 mounts or
23 180 days, whichever comes first.  Use tune2fs -c or -i to override.
24 [root@yinzhengjie ~]# mke2fs -t ext4 /dev/md1
25 mke2fs 1.41.12 (17-May-2010)
26 warning: 192 blocks unused.
27 
28 Filesystem label=
29 OS type: Linux
30 Block size=4096 (log=2)
31 Fragment size=4096 (log=2)
32 Stride=0 blocks, Stripe width=0 blocks
33 590976 inodes, 2359296 blocks
34 117974 blocks (5.00%) reserved for the super user
35 First data block=0
36 Maximum filesystem blocks=2415919104
37 72 block groups
38 32768 blocks per group, 32768 fragments per group
39 8208 inodes per group
40 Superblock backups stored on blocks: 
41         32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
42 
43 Writing inode tables: done                            
44 Creating journal (32768 blocks): done
45 Writing superblocks and filesystem accounting information: done
46 
47 This filesystem will be automatically checked every 25 mounts or
48 180 days, whichever comes first.  Use tune2fs -c or -i to override.
49 [root@yinzhengjie ~]# 
格式化一下RAID设备
 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 '''
 8 [root@yinzhengjie ~]# mkdir /yinzhengjie/md{0,1}
 9 [root@yinzhengjie ~]# mount /dev/md0 /yinzhengjie/md0 #挂载一下RAID0
10 [root@yinzhengjie ~]# ll /yinzhengjie/md0 #查看该目录有“lost+found”文件,说明已经挂载成功了
11 total 16
12 drwx------. 2 root root 16384 May 15 06:29 lost+found
13 [root@yinzhengjie ~]# mount /dev/md1 /yinzhengjie/md1  #同理也挂载一下RAID1
14 [root@yinzhengjie ~]# ll /yinzhengjie/md1
15 total 16
16 drwx------. 2 root root 16384 May 15 06:29 lost+found
17 [root@yinzhengjie ~]# 
18 '''
挂载RAID设备并验证是否成功
 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 '''
 8 [root@yinzhengjie ~]# cp /etc/shadow /yinzhengjie/md1 #我们将数据放在RAID1的设备里。
 9 [root@yinzhengjie ~]# mdadm /dev/md1 -f /dev/sdc1  #我们模拟一下将RAI1的一个分区标记为坏掉
10 mdadm: set /dev/sdc1 faulty in /dev/md1
11 [root@yinzhengjie ~]# mdadm -D /dev/md1
12 /dev/md1:
13         Version : 1.2
14   Creation Time : Mon May 15 06:19:24 2017
15      Raid Level : raid1
16      Array Size : 9437952 (9.00 GiB 9.66 GB)
17   Used Dev Size : 9437952 (9.00 GiB 9.66 GB)
18    Raid Devices : 2
19   Total Devices : 2
20     Persistence : Superblock is persistent
21 
22     Update Time : Mon May 15 06:40:34 2017
23           State : clean, degraded
24  Active Devices : 1
25 Working Devices : 1
26  Failed Devices : 1
27   Spare Devices : 0
28 
29            Name : yinzhengjie:1  (local to host yinzhengjie)
30            UUID : cc10bfb8:cef2b77a:6a72b4b6:8c58484b
31          Events : 19
32 
33     Number   Major   Minor   RaidDevice State
34        0       0        0        0      removed
35        1       8       34        1      active sync   /dev/sdc2 #但是这里还有一个分区是活跃的状态,所以即使又快磁盘划掉也是还能工作的
36 
37        0       8       33        -      faulty   /dev/sdc1  #这里就是状态是非活跃的,即faulty
38 [root@yinzhengjie ~]#
39 [root@yinzhengjie ~]# cat /yinzhengjie/md1/shadow  #发现这个数据我们仍然能访问到。尽管坏掉一块磁盘照样好使。
40 root:$1$/c6cpJcF$RGntN1yDg5LqBA8jlikx31:17144:0:99999:7:::
41 bin:*:15980:0:99999:7:::
42 daemon:*:15980:0:99999:7:::
43 adm:*:15980:0:99999:7:::
44 lp:*:15980:0:99999:7:::
45 sync:*:15980:0:99999:7:::
46 shutdown:*:15980:0:99999:7:::
47 halt:*:15980:0:99999:7:::
48 mail:*:15980:0:99999:7:::
49 uucp:*:15980:0:99999:7:::
50 operator:*:15980:0:99999:7:::
51 games:*:15980:0:99999:7:::
52 gopher:*:15980:0:99999:7:::
53 ftp:*:15980:0:99999:7:::
54 nobody:*:15980:0:99999:7:::
55 dbus:!!:17137::::::
56 usbmuxd:!!:17137::::::
57 vcsa:!!:17137::::::
58 rtkit:!!:17137::::::
59 avahi-autoipd:!!:17137::::::
60 abrt:!!:17137::::::
61 haldaemon:!!:17137::::::
62 gdm:!!:17137::::::
63 ntp:!!:17137::::::
64 apache:!!:17137::::::
65 saslauth:!!:17137::::::
66 postfix:!!:17137::::::
67 pulse:!!:17137::::::
68 sshd:!!:17137::::::
69 tcpdump:!!:17137::::::
70 iptables:$1$wlgfZEy9$gxJIFTDNpFLrwVYOZAaIQ.:17137:0:99999:7:::
71 pegasus:!!:17149::::::
72 cimsrvr:!!:17149::::::
73 rpc:!!:17149:0:99999:7:::
74 rpcuser:!!:17149::::::
75 nfsnobody:!!:17149::::::
76 dhcpd:!!:17149::::::
77 zabbix:!!:17180::::::
78 bind:!!:17253:0:99999:7:::
79 [root@yinzhengjie ~]#
80 '''
模拟RAI1坏掉设备时服务照样是可以访问的。
  1 #!/usr/bin/env python
  2 #_*_coding:utf-8_*_
  3 #@author :yinzhengjie
  4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
  5 #EMAIL:y1053419035@qq.com
  6 
  7 '''
  8 [root@yinzhengjie ~]# mdadm /dev/md1 -r /dev/sdc1  #刚刚我们看到这个设备(这个分区,我们是模拟的设备)已经了,因此我们将这个坏掉的设备下架。
  9 mdadm: hot removed /dev/sdc1 from /dev/md1
 10 [root@yinzhengjie ~]# mdadm -D /dev/md1 #查看信息,目前仅仅有一个分区(设备)在工作。
 11 /dev/md1:
 12         Version : 1.2
 13   Creation Time : Mon May 15 06:19:24 2017
 14      Raid Level : raid1
 15      Array Size : 9437952 (9.00 GiB 9.66 GB)
 16   Used Dev Size : 9437952 (9.00 GiB 9.66 GB)
 17    Raid Devices : 2
 18   Total Devices : 1
 19     Persistence : Superblock is persistent
 20 
 21     Update Time : Mon May 15 06:45:26 2017
 22           State : clean, degraded
 23  Active Devices : 1
 24 Working Devices : 1
 25  Failed Devices : 0
 26   Spare Devices : 0
 27 
 28            Name : yinzhengjie:1  (local to host yinzhengjie)
 29            UUID : cc10bfb8:cef2b77a:6a72b4b6:8c58484b
 30          Events : 34
 31 
 32     Number   Major   Minor   RaidDevice State
 33        0       0        0        0      removed
 34        1       8       34        1      active sync   /dev/sdc2  #这个就是在正常工作的设备。
 35 [root@yinzhengjie ~]#
 36 [root@yinzhengjie ~]# mdadm  /dev/md1 -a /dev/sdc1  #这个时候作为运维的我们,需要更换新的设备来做生成环境种的镜像盘,“/dev/sdc1”这个其实就是一个新换掉的设备,大家别理解成我刚刚那个坏掉的设备就好,
 37 mdadm: added /dev/sdc1
 38 [root@yinzhengjie ~]# mdadm -D /dev/md1  #在此查看RAID配置信息
 39 /dev/md1:
 40         Version : 1.2
 41   Creation Time : Mon May 15 06:19:24 2017
 42      Raid Level : raid1
 43      Array Size : 9437952 (9.00 GiB 9.66 GB)
 44   Used Dev Size : 9437952 (9.00 GiB 9.66 GB)
 45    Raid Devices : 2
 46   Total Devices : 2
 47     Persistence : Superblock is persistent
 48 
 49     Update Time : Mon May 15 06:48:44 2017
 50           State : clean, degraded, recovering
 51  Active Devices : 1
 52 Working Devices : 2
 53  Failed Devices : 0
 54   Spare Devices : 1
 55 
 56  Rebuild Status : 9% complete  #由于我刚刚加载上,它在做镜像的时候,如果数据量过大的话是需要时间耐心等待多,当然您也可以用watch命令来监控一下进度的。
 57 
 58            Name : yinzhengjie:1  (local to host yinzhengjie)
 59            UUID : cc10bfb8:cef2b77a:6a72b4b6:8c58484b
 60          Events : 37
 61 
 62     Number   Major   Minor   RaidDevice State
 63        2       8       33        0      spare rebuilding   /dev/sdc1
 64        1       8       34        1      active sync   /dev/sdc2
 65 [root@yinzhengjie ~]#
 66 [root@yinzhengjie ~]# cat /proc/mdstat
 67 Personalities : [raid0] [raid1]
 68 md1 : active raid1 sdc1[2] sdc2[1]
 69       9437952 blocks super 1.2 [2/2] [UU]
 70 
 71 md0 : active raid0 sdb2[1] sdb1[0]
 72       6308864 blocks super 1.2 512k chunks
 73 
 74 unused devices: <none>
 75 [root@yinzhengjie ~]# mdadm
 76 Usage: mdadm --help
 77   for help
 78 [root@yinzhengjie ~]#
 79 [root@yinzhengjie ~]# mdadm --detail /dev/md1  #等过段时间,再次查看设备信息就更新完毕了。
 80 /dev/md1:
 81         Version : 1.2
 82   Creation Time : Mon May 15 06:19:24 2017
 83      Raid Level : raid1
 84      Array Size : 9437952 (9.00 GiB 9.66 GB)
 85   Used Dev Size : 9437952 (9.00 GiB 9.66 GB)
 86    Raid Devices : 2
 87   Total Devices : 2
 88     Persistence : Superblock is persistent
 89 
 90     Update Time : Mon May 15 06:49:28 2017
 91           State : clean
 92  Active Devices : 2
 93 Working Devices : 2
 94  Failed Devices : 0
 95   Spare Devices : 0
 96 
 97            Name : yinzhengjie:1  (local to host yinzhengjie)
 98            UUID : cc10bfb8:cef2b77a:6a72b4b6:8c58484b
 99          Events : 53
100 
101     Number   Major   Minor   RaidDevice State
102        2       8       33        0      active sync   /dev/sdc1  #此时设备以及恢复成功了,在生产环境中又可以正常使用了
103        1       8       34        1      active sync   /dev/sdc2
104 [root@yinzhengjie ~]#
105 
106 '''
模拟将坏掉的磁盘换掉,添加新的磁盘上去的过程。

   以上就是生产环境中比较老式的方式去干货,当磁盘坏掉的时候,需要我们监控到,监控到了之后还需要我们手动去更换,这个操作流程看起来是很麻烦的,那么如何让这个过程变得简单呢?这个时候又有人提出热备的功能,这个方法很靠谱啊,就是用3块磁盘做RAID1,让其中2块磁盘做镜像盘,其中一块磁盘休息,当其中任何一块正在工作的磁盘坏掉的时候,这个时候就是替补队员大显身手的时候,他会主动的去替换坏道的磁盘,自己主动去变为镜像磁盘去工作,这给运维人员也得到了缓解,当磁盘坏掉的时候,运维人员不用提心吊胆的担心什么时候去换它!好了,那么要如何实现呢?我这里还是用3个分区来代替三块磁盘,具体操作流程如下:

  1 #!/usr/bin/env python
  2 #_*_coding:utf-8_*_
  3 #@author :yinzhengjie
  4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
  5 #EMAIL:y1053419035@qq.com
  6 
  7 '''
  8 [root@yinzhengjie ~]# fdisk /dev/sdb  #对新磁盘进行分区
  9 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
 10 Building a new DOS disklabel with disk identifier 0x5101d254.
 11 Changes will remain in memory only, until you decide to write them.
 12 After that, of course, the previous content won't be recoverable.
 13 
 14 Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
 15 
 16 WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
 17          switch off the mode (command 'c') and change display units to
 18          sectors (command 'u').
 19 
 20 Command (m for help): P
 21 
 22 Disk /dev/sdb: 21.5 GB, 21474836480 bytes
 23 255 heads, 63 sectors/track, 2610 cylinders
 24 Units = cylinders of 16065 * 512 = 8225280 bytes
 25 Sector size (logical/physical): 512 bytes / 512 bytes
 26 I/O size (minimum/optimal): 512 bytes / 512 bytes
 27 Disk identifier: 0x5101d254
 28 
 29    Device Boot      Start         End      Blocks   Id  System
 30 
 31 Command (m for help): N
 32 Command action
 33    e   extended
 34    p   primary partition (1-4)
 35 P
 36 Partition number (1-4): 1
 37 First cylinder (1-2610, default 1): 
 38 Using default value 1
 39 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +5G
 40 
 41 Command (m for help): N
 42 Command action
 43    e   extended
 44    p   primary partition (1-4)
 45 P
 46 Partition number (1-4): 2
 47 First cylinder (655-2610, default 655): 
 48 Using default value 655
 49 Last cylinder, +cylinders or +size{K,M,G} (655-2610, default 2610): +5G
 50 
 51 Command (m for help): N
 52 Command action
 53    e   extended
 54    p   primary partition (1-4)
 55 P
 56 Partition number (1-4): 3
 57 First cylinder (1309-2610, default 1309): 
 58 Using default value 1309
 59 Last cylinder, +cylinders or +size{K,M,G} (1309-2610, default 2610): +5G
 60 
 61 Command (m for help): P
 62 
 63 Disk /dev/sdb: 21.5 GB, 21474836480 bytes
 64 255 heads, 63 sectors/track, 2610 cylinders
 65 Units = cylinders of 16065 * 512 = 8225280 bytes
 66 Sector size (logical/physical): 512 bytes / 512 bytes
 67 I/O size (minimum/optimal): 512 bytes / 512 bytes
 68 Disk identifier: 0x5101d254
 69 
 70    Device Boot      Start         End      Blocks   Id  System
 71 /dev/sdb1               1         654     5253223+  83  Linux
 72 /dev/sdb2             655        1308     5253255   83  Linux
 73 /dev/sdb3            1309        1962     5253255   83  Linux
 74 
 75 Command (m for help): t        
 76 Partition number (1-4): 1
 77 Hex code (type L to list codes): fd
 78 Changed system type of partition 1 to fd (Linux raid autodetect)
 79 
 80 Command (m for help): t
 81 Partition number (1-4): 2
 82 Hex code (type L to list codes): fd
 83 Changed system type of partition 2 to fd (Linux raid autodetect)
 84 
 85 Command (m for help): t
 86 Partition number (1-4): 3
 87 Hex code (type L to list codes): fd
 88 Changed system type of partition 3 to fd (Linux raid autodetect)
 89 
 90 Command (m for help): p  #最终创建3快ID=fd的分区即可。
 91 
 92 Disk /dev/sdb: 21.5 GB, 21474836480 bytes
 93 255 heads, 63 sectors/track, 2610 cylinders
 94 Units = cylinders of 16065 * 512 = 8225280 bytes
 95 Sector size (logical/physical): 512 bytes / 512 bytes
 96 I/O size (minimum/optimal): 512 bytes / 512 bytes
 97 Disk identifier: 0x5101d254
 98 
 99    Device Boot      Start         End      Blocks   Id  System
100 /dev/sdb1               1         654     5253223+  fd  Linux raid autodetect
101 /dev/sdb2             655        1308     5253255   fd  Linux raid autodetect
102 /dev/sdb3            1309        1962     5253255   fd  Linux raid autodetect
103 
104 Command (m for help): w
105 The partition table has been altered!
106 
107 Calling ioctl() to re-read partition table.
108 Syncing disks.
109 [root@yinzhengjie ~]# 
110 
111 '''
准备三块磁盘用于做RAID
 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 '''
 8 [root@yinzhengjie ~]# cat /proc/partitions  #查看系统是否识别你的分区
 9 major minor  #blocks  name
10 
11    8       16   20971520 sdb
12    8       17    5253223 sdb1
13    8       18    5253255 sdb2
14    8       19    5253255 sdb3
15    8        0   20971520 sda
16    8        1     307200 sda1
17    8        2   18631680 sda2
18    8        3    2031616 sda3
19 [root@yinzhengjie ~]#
20 [root@yinzhengjie ~]# mdadm -C /dev/md2 -a yes -l 1 -n 2 -x 1 /dev/sdb{1,2,3}  #指定RAID级别(-l)为1,创建设备个数(-n)为2,并且指定人热备设备(-x)个数为1,注意:-a :=yes(自动为创建的RAID设备创建设备文件。
21 mdadm: Note: this array has metadata at the start and
22     may not be suitable as a boot device.  If you plan to
23     store '/boot' on this device please ensure that
24     your boot-loader understands md/v1.x metadata, or use
25     --metadata=0.90
26 Continue creating array? yes #输入yes即可,此消息可以忽略。
27 mdadm: Defaulting to version 1.2 metadata
28 mdadm: array /dev/md2 started.
29 [root@yinzhengjie ~]#
30 [root@yinzhengjie ~]# cat /proc/mdstat #查看RAID信息
31 Personalities : [raid1]
32 md2 : active raid1 sdb3[2](S) sdb2[1] sdb1[0]
33       5249088 blocks super 1.2 [2/2] [UU]
34 
35 unused devices: <none>
36 [root@yinzhengjie ~]#
37 [root@yinzhengjie ~]# mdadm -D /dev/md2  #查看RAID1的详细信息。
38 /dev/md2:
39         Version : 1.2
40   Creation Time : Mon May 15 07:10:59 2017
41      Raid Level : raid1
42      Array Size : 5249088 (5.01 GiB 5.38 GB)
43   Used Dev Size : 5249088 (5.01 GiB 5.38 GB)
44    Raid Devices : 2
45   Total Devices : 3
46     Persistence : Superblock is persistent
47 
48     Update Time : Mon May 15 07:11:25 2017
49           State : clean 
50  Active Devices : 2
51 Working Devices : 3
52  Failed Devices : 0
53   Spare Devices : 1
54 
55            Name : yinzhengjie:2  (local to host yinzhengjie)
56            UUID : 6b4aff67:dbb66538:7276fa64:14137394
57          Events : 17
58 
59     Number   Major   Minor   RaidDevice State
60        0       8       17        0      active sync   /dev/sdb1  #正在工作的磁盘分区
61        1       8       18        1      active sync   /dev/sdb2  #正在工作的磁盘分区
62 
63        2       8       19        -      spare   /dev/sdb3  #磁盘就表示为热备份的磁盘分区
64 [root@yinzhengjie ~]# 
65 [root@yinzhengjie ~]# 
66 
67 '''
配置3块磁盘为RAID级别。
 1 [root@yinzhengjie ~]# mke2fs -t ext4 /dev/md2
 2 mke2fs 1.41.12 (17-May-2010)
 3 Filesystem label=
 4 OS type: Linux
 5 Block size=4096 (log=2)
 6 Fragment size=4096 (log=2)
 7 Stride=0 blocks, Stripe width=0 blocks
 8 328656 inodes, 1312272 blocks
 9 65613 blocks (5.00%) reserved for the super user
10 First data block=0
11 Maximum filesystem blocks=1346371584
12 41 block groups
13 32768 blocks per group, 32768 fragments per group
14 8016 inodes per group
15 Superblock backups stored on blocks: 
16         32768, 98304, 163840, 229376, 294912, 819200, 884736
17 
18 Writing inode tables: done                            
19 Creating journal (32768 blocks): done
20 Writing superblocks and filesystem accounting information: done
21 
22 This filesystem will be automatically checked every 28 mounts or
23 180 days, whichever comes first.  Use tune2fs -c or -i to override.
24 [root@yinzhengjie ~]# mkdir /yinzhengjie/md2
25 [root@yinzhengjie ~]# mount /dev/md2 /yinzhengjie/md2
26 [root@yinzhengjie ~]# ll /yinzhengjie/md2
27 total 16
28 drwx------. 2 root root 16384 May 15 07:19 lost+found
29 [root@yinzhengjie ~]# cp /etc/fstab /yinzhengjie/md2/
30 [root@yinzhengjie ~]# 
格式化RAID磁盘并挂载
 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 '''
 8 [root@yinzhengjie ~]# mdadm /dev/md2 -f /dev/sdb1  #我们可以模拟将/dev/sdb1这个设备标记为坏掉的磁盘分区
 9 mdadm: set /dev/sdb1 faulty in /dev/md2
10 [root@yinzhengjie ~]# mdadm -D /dev/md2  #查看RAID详细信息
11 /dev/md2:
12         Version : 1.2
13   Creation Time : Mon May 15 07:10:59 2017
14      Raid Level : raid1
15      Array Size : 5249088 (5.01 GiB 5.38 GB)
16   Used Dev Size : 5249088 (5.01 GiB 5.38 GB)
17    Raid Devices : 2
18   Total Devices : 3
19     Persistence : Superblock is persistent
20 
21     Update Time : Mon May 15 07:21:44 2017
22           State : clean, degraded, recovering
23  Active Devices : 1
24 Working Devices : 2
25  Failed Devices : 1
26   Spare Devices : 1
27 
28  Rebuild Status : 49% complete #这个需要等待一些时间
29 
30            Name : yinzhengjie:2  (local to host yinzhengjie)
31            UUID : 6b4aff67:dbb66538:7276fa64:14137394
32          Events : 26
33 
34     Number   Major   Minor   RaidDevice State
35        2       8       19        0      spare rebuilding   /dev/sdb3  #之前备份的那块磁盘瞬间就上去了
36        1       8       18        1      active sync   /dev/sdb2
37 
38        0       8       17        -      faulty   /dev/sdb1  #发现RAD1过着坏道了
39 [root@yinzhengjie ~]# mdadm  /dev/md2 -r /dev/sdb1  #我们可以将坏掉的设备从RAID1里面移除掉。
40 mdadm: hot removed /dev/sdb1 from /dev/md2
41 [root@yinzhengjie ~]# mdadm -D /dev/md2  #再次查看设备信息,发现工作的就只有2个设备了,因为坏掉的设备以及被我移除了。
42 /dev/md2:
43         Version : 1.2
44   Creation Time : Mon May 15 07:10:59 2017
45      Raid Level : raid1
46      Array Size : 5249088 (5.01 GiB 5.38 GB)
47   Used Dev Size : 5249088 (5.01 GiB 5.38 GB)
48    Raid Devices : 2
49   Total Devices : 2
50     Persistence : Superblock is persistent
51 
52     Update Time : Mon May 15 07:22:32 2017
53           State : clean
54  Active Devices : 2
55 Working Devices : 2
56  Failed Devices : 0
57   Spare Devices : 0
58 
59            Name : yinzhengjie:2  (local to host yinzhengjie)
60            UUID : 6b4aff67:dbb66538:7276fa64:14137394
61          Events : 37
62 
63     Number   Major   Minor   RaidDevice State
64        2       8       19        0      active sync   /dev/sdb3
65        1       8       18        1      active sync   /dev/sdb2
66 [root@yinzhengjie ~]#
67 '''
模拟其中一个正在工作的磁盘坏掉,热备的磁盘会自动补位。
 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 '''
 8 [root@yinzhengjie ~]# umount /yinzhengjie/md2
 9 [root@yinzhengjie ~]# mdadm -S /dev/md2  #停止使用阵列
10 mdadm: stopped /dev/md2
11 [root@yinzhengjie ~]#
12 [root@yinzhengjie ~]# cat /proc/mdstat  #查看阵列使用情况
13 Personalities : [raid1] 
14 unused devices: <none>
15 [root@yinzhengjie ~]# mdadm -A /dev/md2 /dev/sdb{1,2,3} #启用阵列
16 mdadm: /dev/md2 has been started with 2 drives.
17 [root@yinzhengjie ~]# cat /proc/mdstat #再次查看阵列使用情况
18 Personalities : [raid1] 
19 md2 : active raid1 sdb3[2] sdb2[1]
20       5249088 blocks super 1.2 [2/2] [UU]
21       
22 unused devices: <none>
23 [root@yinzhengjie ~]# 
24 '''
阵列的开启和关闭

  通过以上实验,我们可以总结一下mdadm命令的常用参数了:

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 '''
 8      md,可以组合任何块设备
 9      
10      mdadm,/etc/mdadm.conf
11 
12      mdadm:模式化的工具
13 
14     -A  Assemble 装配模式
15 
16     -C  Create 创建模式
17     -C:专用选项
18       -n #:用于创建RAID设备的个数
19       -x #: 热备磁盘的个数
20       -l :指定RAID级别
21       -a :=yes(自动为创建的RAID设备创建设备文件) md mdp part p 如何创建设备文件
22       -c:指定块的大小,默认为512KB
23     -F FOLLOW 监控
24     -S 停止RAID
25     -D --detail: 显示阵列详细信息
26 
27      Manage 管理模式专用项
28        -f:模拟损害
29        -r:模拟移除设备
30        -a:模拟添加新设备
31 
32    /proc/mdstat
33  创建一个大小为12G的RAID0:2*6G,3*4G 4*3G 6*2G
34  mdadm -C /dev/md0 -a yes -l 0 -n 2 /dev/sdb{1,2}
35 
36  watch ‘’ 
37    -n #:每个#执行一次指定的命令,单位为s
38 '''

 补充:

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 '''
 8 软RAID磁盘阵列管理
 9 
10 1. RAID技术简介
11     廉价冗余磁盘阵列,Redundant Arrays of Inexpensive Disks 
12     将多个较小/低速的磁盘整合成一个大磁盘,提供硬件容错、提高I/O性能
13 
14 2. 常见的RAID阵列级别
15 
16     RAID0,条带模式:
17     并行读写多块磁盘(最少需2块),提高读写效率
18     无冗余,磁盘利用率100%
19 
20     RAID1,镜像模式
21     并行读写多块磁盘(最少需2块),其中半数磁盘用来备份,提高读效率
22     有一倍冗余,磁盘利用率50%
23 
24     RAID10,条带+镜像模式
25     融合RAID0+RAID1的特点(最少需4块磁盘),提高读写效率
26     有一倍冗余,磁盘利用率50%
27 
28     RAID5,高性价比阵列
29     相当于RAID 0和RAID 1的折中方案(最少需3块磁盘)
30     校验盘:提供1块成员盘的容量存放校验信息,用于恢复数据,提高可靠性
31     冗余盘:可额外准备一块或多块冗余盘,以便当成员盘故障时实现自动替换(热备份)
32     冗余空间=校验盘+冗余盘
33     最大磁盘利用率=(N-1)/N
34 
35 
36 
37 3. RAID阵列的实现方式
38     硬RAID:由RAID控制卡管理阵列
39     主板-->阵列卡-->磁盘-->操作系统-->数据
40     软RAID:由操作系统来管理阵列
41     主板-->磁盘-->操作系统-->RAID软件-->数据
42 
43 '''
软RAID磁盘阵列管理

  

原文地址:https://www.cnblogs.com/yinzhengjie/p/6858302.html