返回上一页首页 - 服务支持 - 帮助中心
帮助中心

Linux中建立软RAID

发布日期:2022-03-19浏览次数:613关键字:LINUXRAID

  Linux内核中有一个md(multiple devices)模块在底层管理RAID设备,它会在应用层给我们提供一个应用程序的工具mdadm。

  mdadm用于构建、管理和监视Linux MD设备(即RAID阵列)

(1).mdadm的常用选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
-C,--create 新建一个阵列
-r,--remove 删除列出的设备,设备不可处于活动状态
-A,--assemble 激活阵列
-l,--level== 设置阵列级别
-D,--detail 打印一个或多个阵列设备信息
-n,--raid-devices= 指定阵列成员信息
-s,-scan 扫描配置文件或/proc/mdstat得到阵列缺失信息
-x或--spare-devices= 指定阵列中备用盘的数量
-f,--fail 将列出的设备标记为故障
-c,--chunk= 指定阵列的块大小。默认512K
-a,--add 添加设备到阵列
-G,--grow改变阵列大小和形态
-v,--verbose 显示详细信息
-S,--stop 停止阵列,释放所有资源

(2).实例

实验环境:CentOS7

raid类型 磁盘或分区 热备盘
raid0 sdb、sdc  
raid1 sdd、sde sdf
raid5 sdg、sdh、sdi sdj
raid10 sdk1、sdk2、sdk3、sdk4  

 

1)创建raid0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[root@xuexi ~]# ls /dev/sd*
/dev/sda   /dev/sda2  /dev/sdb  /dev/sdd  /dev/sdf  /dev/sdh  /dev/sdj
/dev/sda1  /dev/sda3  /dev/sdc  /dev/sde  /dev/sdg  /dev/sdi  /dev/sdk
[root@xuexi ~]# mdadm -v -C /dev/md0 -l 0 -n 2 /dev/sdb /dev/sdc
mdadm: chunk size defaults to 512K
mdadm: partition table exists on /dev/sdb
mdadm: partition table exists on /dev/sdb but will be lost or
       meaningless after creating array
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
[root@xuexi ~]# mdadm -D  //提示没有给出设备
mdadm: No devices given.
[root@xuexi ~]# mdadm -Ds  //指定从配置文件或内存文件(/proc/mdadm)中读取
ARRAY /dev/md0 metadata=1.2 name=xuexi:0 UUID=d1143d41:be8e61b8:d368f0a1:8df95826
[root@xuexi ~]# mdadm -Dsv  //显示更详细一些
ARRAY /dev/md0 level=raid0 num-devices=2 metadata=1.2 name=xuexi:0 UUID=d1143d41:be8e61b8:d368f0a1:8df95826
   devices=/dev/sdb,/dev/sdc
[root@xuexi ~]# mdadm -D /dev/md0  //也可以直接指定设备
/dev/md0:
           Version : 1.2
     Creation Time : Sun Mar 17 21:51:29 2019
        Raid Level : raid0  //raid级别
        Array Size : 41908224 (39.97 GiB 42.91 GB)  //GiB是用1024计算,GB是用1000计算
      Raid Devices : 2  //raid盘
     Total Devices : 2  //总共拥有的盘(raid盘+热备盘)
       Persistence : Superblock is persistent
 
       Update Time : Sun Mar 17 21:51:29 2019
             State : clean
    Active Devices : 2
   Working Devices : 2
    Failed Devices : 0
     Spare Devices : 0
 
        Chunk Size : 512K  //chunk块大小
 
Consistency Policy : none
 
              Name : xuexi:0  (local to host xuexi)
              UUID : d1143d41:be8e61b8:d368f0a1:8df95826
            Events : 0
 
    Number   Major   Minor   RaidDevice State
       0       8       16        0      active sync   /dev/sdb  //组成的设备信息
       1       8       32        1      active sync   /dev/sdc  //组成的设备信息
[root@xuexi ~]# mdadm -Dsv > /etc/mdadm.conf  //生成配置文件,配置文件名固定。

  raid的格式化和挂载和正常的设备没有什么区别,当成正常的设备即可,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@xuexi ~]# mkfs.xfs /dev/md0
meta-data=/dev/md0               isize=512    agcount=16, agsize=654720 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=10475520, imaxpct=25
         =                       sunit=128    swidth=256 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=5120, version=2
         =                       sectsz=512   sunit=8 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@xuexi ~]# mkdir /raid0
[root@xuexi ~]# mount /dev/md0 /raid0/
[root@xuexi ~]# df -h /dev/md0
文件系统        容量  已用  可用 已用% 挂载点
/dev/md0         40G   33M   40G    1% /raid0

  开机自动挂载也是如正常设备一样。(blkid /dev/md0可以获得UUID)

2)创建raid1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
[root@xuexi ~]# mdadm -v -C /dev/md1 -l 1 -n 2 -x 1 /dev/sd[d,e,f]
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 20954112K
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.
[root@xuexi ~]# mdadm -Dsv
ARRAY /dev/md0 level=raid0 num-devices=2 metadata=1.2 name=xuexi:0 UUID=d1143d41:be8e61b8:d368f0a1:8df95826
   devices=/dev/sdb,/dev/sdc
ARRAY /dev/md1 level=raid1 num-devices=2 metadata=1.2 spares=1 name=xuexi:1 UUID=4b922235:5a26daf2:2eed2067:959a76ee
   devices=/dev/sdd,/dev/sde,/dev/sdf
[root@xuexi ~]# mdadm -Dsv > /etc/mdadm.conf   //生成配置文件
[root@xuexi ~]# mdadm -D /dev/md1
/dev/md1:
           Version : 1.2
     Creation Time : Sun Mar 17 22:13:13 2019
        Raid Level : raid1  //raid级别
        Array Size : 20954112 (19.98 GiB 21.46 GB)
     Used Dev Size : 20954112 (19.98 GiB 21.46 GB)
      Raid Devices : 2  //raid盘
     Total Devices : 3  //总共拥有的盘
       Persistence : Superblock is persistent
 
       Update Time : Sun Mar 17 22:13:40 2019
             State : clean
    Active Devices : 2
   Working Devices : 3
    Failed Devices : 0
     Spare Devices : 1
 
Consistency Policy : resync  //如果此处显示百分比,则代表正在同步
 
              Name : xuexi:1  (local to host xuexi)
              UUID : 4b922235:5a26daf2:2eed2067:959a76ee
            Events : 17
 
    Number   Major   Minor   RaidDevice State
       0       8       48        0      active sync   /dev/sdd
       1       8       64        1      active sync   /dev/sde
 
       2       8       80        -      spare   /dev/sdf

  模拟磁盘故障,自动顶替故障盘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
[root@xuexi ~]# mkfs.xfs /dev/md1  //格式化
meta-data=/dev/md1               isize=512    agcount=4, agsize=1309632 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=5238528, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@xuexi ~]# mkdir /raid1
[root@xuexi ~]# mount /dev/md1 /raid1  //挂载
[root@xuexi ~]# cp /etc/passwd /raid1/  //拷贝数据
[root@xuexi ~]# cp -r /boot/grub /raid1/
[root@xuexi ~]# df -h /dev/md1
文件系统        容量  已用  可用 已用% 挂载点
/dev/md1         20G   33M   20G    1% /raid1
[root@xuexi ~]# mdadm -D /dev/md1
/dev/md1:
           Version : 1.2
     Creation Time : Sun Mar 17 22:13:13 2019
        Raid Level : raid1
        Array Size : 20954112 (19.98 GiB 21.46 GB)
     Used Dev Size : 20954112 (19.98 GiB 21.46 GB)
      Raid Devices : 2
     Total Devices : 3
       Persistence : Superblock is persistent
 
       Update Time : Sun Mar 17 22:31:04 2019
             State : clean
    Active Devices : 2
   Working Devices : 3
    Failed Devices : 0
     Spare Devices : 1
 
Consistency Policy : resync
 
              Name : xuexi:1  (local to host xuexi)
              UUID : 4b922235:5a26daf2:2eed2067:959a76ee
            Events : 17
 
    Number   Major   Minor   RaidDevice State
       0       8       48        0      active sync   /dev/sdd
       1       8       64        1      active sync   /dev/sde
 
       2       8       80        -      spare   /dev/sdf
[root@xuexi ~]# mdadm /dev/md1 -f /dev/sde  //将/dev/sde标记为坏盘
mdadm: set /dev/sde faulty in /dev/md1
[root@xuexi ~]# mdadm -D /dev/md1
/dev/md1:
           Version : 1.2
     Creation Time : Sun Mar 17 22:13:13 2019
        Raid Level : raid1
        Array Size : 20954112 (19.98 GiB 21.46 GB)
     Used Dev Size : 20954112 (19.98 GiB 21.46 GB)
      Raid Devices : 2
     Total Devices : 3
       Persistence : Superblock is persistent
 
       Update Time : Sun Mar 17 22:33:36 2019
             State : clean, degraded, recovering
    Active Devices : 1
   Working Devices : 2
    Failed Devices : 1
     Spare Devices : 1
 
Consistency Policy : resync
 
    Rebuild Status : 71% complete  //正在同步
 
              Name : xuexi:1  (local to host xuexi)
              UUID : 4b922235:5a26daf2:2eed2067:959a76ee
            Events : 30
 
    Number   Major   Minor   RaidDevice State
       0       8       48        0      active sync   /dev/sdd
       2       8       80        1      spare rebuilding   /dev/sdf  //热备盘正在重建
 
       1       8       64        -      faulty   /dev/sde  //损坏标记
[root@xuexi ~]# mdadm -Dsv > /etc/mdadm.conf  //更新配置文件

  移除损坏的盘

1
2
3
4
[root@xuexi ~]# umount /raid1/  //卸载挂载
[root@xuexi ~]# mdadm -r /dev/md1 /dev/sde  //移除坏盘
mdadm: hot removed /dev/sde from /dev/md1
[root@xuexi ~]# mdadm -Dsv > /etc/mdadm.conf  //更新配置文件

  添加新盘

1
2
3
[root@xuexi ~]# mdadm -a /dev/md1 /dev/sde  //添加新盘
mdadm: added /dev/sde
[root@xuexi ~]# mdadm -Dsv > /etc/mdadm.conf   //更新配置文件

3)创建raid5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
[root@xuexi ~]# mdadm -v -C /dev/md5 -l 5 -n 3 -x 1 -c32 /dev/sd{g,h,i,j}
mdadm: layout defaults to left-symmetric
mdadm: layout defaults to left-symmetric
mdadm: size set to 20954112K
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md5 started.
[root@xuexi ~]# mdadm -D /dev/md5
/dev/md5:
           Version : 1.2
     Creation Time : Sun Mar 17 22:51:42 2019
        Raid Level : raid5  //raid级别
        Array Size : 41908224 (39.97 GiB 42.91 GB)
     Used Dev Size : 20954112 (19.98 GiB 21.46 GB)
      Raid Devices : 3
     Total Devices : 4
       Persistence : Superblock is persistent
 
       Update Time : Sun Mar 17 22:52:01 2019
             State : clean, degraded, recovering
    Active Devices : 2
   Working Devices : 4
    Failed Devices : 0
     Spare Devices : 2
 
            Layout : left-symmetric
        Chunk Size : 32K  //chunk块大小
 
Consistency Policy : resync
 
    Rebuild Status : 32% complete  //正在同步
 
              Name : xuexi:5  (local to host xuexi)
              UUID : fa44697c:20726a38:fcf7c1d5:f584b82f
            Events : 6
 
    Number   Major   Minor   RaidDevice State
       0       8       96        0      active sync   /dev/sdg
       1       8      112        1      active sync   /dev/sdh
       4       8      128        2      spare rebuilding   /dev/sdi
 
       3       8      144        -      spare   /dev/sdj  //热备盘
[root@xuexi ~]# mdadm -Dsv > /etc/mdadm.conf   //更新配置文件

  停止阵列,注意请确认数据已同步完成

1
2
[root@xuexi ~]# mdadm -S /dev/md5
mdadm: stopped /dev/md5

  激活阵列,注意配置文件内必须存在

1
2
[root@xuexi ~]# mdadm -As
mdadm: /dev/md5 has been started with 3 drives and 1 spare.

  扩展raid5自盘阵列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[root@xuexi ~]# mdadm -G /dev/md5 -n 4 -c 32  //将热备盘扩展进raid5
[root@xuexi ~]# mdadm -D /dev/md5
/dev/md5:
           Version : 1.2
     Creation Time : Sun Mar 17 22:51:42 2019
        Raid Level : raid5
        Array Size : 41908224 (39.97 GiB 42.91 GB)  //如果没有同不好,raid阵列大小会暂时不变
     Used Dev Size : 20954112 (19.98 GiB 21.46 GB)
      Raid Devices : 4
     Total Devices : 4
       Persistence : Superblock is persistent
 
       Update Time : Sun Mar 17 23:02:11 2019
             State : clean, reshaping
    Active Devices : 4
   Working Devices : 4
    Failed Devices : 0
     Spare Devices : 0
 
            Layout : left-symmetric
        Chunk Size : 32K
 
Consistency Policy : resync
 
    Reshape Status : 17% complete  //正在同步
     Delta Devices : 1, (3->4)  //扩展中
 
              Name : xuexi:5  (local to host xuexi)
              UUID : fa44697c:20726a38:fcf7c1d5:f584b82f
            Events : 48
 
    Number   Major   Minor   RaidDevice State
       0       8       96        0      active sync   /dev/sdg
       1       8      112        1      active sync   /dev/sdh
       4       8      128        2      active sync   /dev/sdi
       3       8      144        3      active sync   /dev/sdj
[root@xuexi ~]# mdadm -Dsv > /etc/mdadm.conf   //更新配置文件

  注意:raid5只能增加不能减少,raid1可增加可减少。(少于两块盘,那就不叫raid1了啊)

4)创建raid10

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
[root@xuexi ~]# fdisk /dev/sdk  //创建4个分区
欢迎使用 fdisk (util-linux 2.23.2)。
 
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
 
Device does not contain a recognized partition table
使用磁盘标识符 0x4b57a9c6 创建新的 DOS 磁盘标签。
 
命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (2048-41943039,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):+1G
分区 1 已设置为 Linux 类型,大小设为 1 GiB
 
命令(输入 m 获取帮助):n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
分区号 (2-4,默认 2):
起始 扇区 (2099200-41943039,默认为 2099200):
将使用默认值 2099200
Last 扇区, +扇区 or +size{K,M,G} (2099200-41943039,默认为 41943039):+1G
分区 2 已设置为 Linux 类型,大小设为 1 GiB
 
命令(输入 m 获取帮助):n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
分区号 (3,4,默认 3):
起始 扇区 (4196352-41943039,默认为 4196352):
将使用默认值 4196352
Last 扇区, +扇区 or +size{K,M,G} (4196352-41943039,默认为 41943039):+1G
分区 3 已设置为 Linux 类型,大小设为 1 GiB
 
命令(输入 m 获取帮助):n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): p
已选择分区 4
起始 扇区 (6293504-41943039,默认为 6293504):
将使用默认值 6293504
Last 扇区, +扇区 or +size{K,M,G} (6293504-41943039,默认为 41943039):+1G
分区 4 已设置为 Linux 类型,大小设为 1 GiB
 
命令(输入 m 获取帮助):w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@xuexi ~]# ls /dev/sdk*
/dev/sdk  /dev/sdk1  /dev/sdk2  /dev/sdk3  /dev/sdk4
[root@xuexi ~]# mdadm -v -C /dev/md10 -l 10 -n 4 /dev/sdk[1-4]
mdadm: layout defaults to n2
mdadm: layout defaults to n2
mdadm: chunk size defaults to 512K
mdadm: size set to 1046528K
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md10 started.
[root@xuexi ~]# mdadm -Dsv > /etc/mdadm.conf   //更新配置文件

5)删除raid的所有信息及注意事项

1
2
3
4
5
6
7
8
9
[root@xuexi ~]# df -h /raid*
文件系统        容量  已用  可用 已用% 挂载点
/dev/md0         40G   33M   40G    1% /raid0
/dev/sda2        10G  4.7G  5.4G   47% /
[root@xuexi ~]# umount /raid0/  //卸载挂载
[root@xuexi ~]# mdadm -Ss  //停止raid设备
[root@xuexi ~]# rm -rf /etc/mdadm.conf   //删除配置文件
[root@xuexi ~]# mdadm --zero-superblock /dev/sdb   //擦除设备中的raid标识(超级块)
[root@xuexi ~]# mdadm --zero-superblock /dev/sdc
粤ICP备16075015号 COPYRIGHT © 2018 深圳市联瑞信息技术有限公司.ALL RIGHTS RESERVED. 
关注官方微信