linux下磁盘阵列配置方法

一、准备好4块磁盘,如:/dev/sdb、/dev/sdc、/dev/sdd、/dev/sde

1、创建 raid5

[root@docker ~]# mdadm --create /dev/md0 --level=5 --raid-devices=3 --spare-devices=1 /dev/sd[b-e]
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

设定/dev/sde为备用设备,其余为活动设备,备用设备的作用是一旦某一设备损坏可立即使用备用设备替换

2、查看 详情

[root@docker ~]# mdadm --detail /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Sat Nov 21 14:20:48 2015
     Raid Level : raid5
     Array Size : 104792064 (99.94 GiB 107.31 GB)
  Used Dev Size : 52396032 (49.97 GiB 53.65 GB)
   Raid Devices : 3
  Total Devices : 4
    Persistence : Superblock is persistent

    Update Time : Sat Nov 21 14:25:21 2015
          State : clean 
 Active Devices : 3
Working Devices : 4
 Failed Devices : 0
  Spare Devices : 1

         Layout : left-symmetric
     Chunk Size : 512K

           Name : docker.example.com:0  (local to host docker.example.com)
           UUID : ac110faf:5f345efc:8540013c:cd7f95bc
         Events : 46

    Number   Major   Minor   RaidDevice State
       0       8       16        0      active sync   /dev/sdb
       1       8       32        1      active sync   /dev/sdc
       4       8       48        2      active sync   /dev/sdd

       3       8       64        -      spare   /dev/sde

3、格式化阵列

[root@docker ~]# mkfs.ext4 /dev/md0
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=128 blocks, Stripe width=256 blocks
6553600 inodes, 26198016 blocks
1309900 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=4294967296
800 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000, 7962624, 11239424, 20480000, 23887872

正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

4、挂载 raid5

[root@docker ~]# mount /dev/md0 /mnt

[root@docker ~]# df -Th
Filesystem        Type      Size  Used Avail Use% Mounted on
/dev/sda1         ext4       98G  4.2G   89G   5% /
tmpfs             tmpfs      99M  1.5M   98M   2% /run
/dev/md0          ext4      107G  218M  106G   1% /mnt

二、模拟磁盘故障

1、将/dev/sdc标记为出现故障的磁盘

[root@docker ~]# mdadm /dev/md0 --fail /dev/sdc
mdadm: set /dev/sdc faulty in /dev/md0

[root@docker ~]# mdadm --detail /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Sat Nov 21 14:20:48 2015
     Raid Level : raid5
     Array Size : 104792064 (99.94 GiB 107.31 GB)
  Used Dev Size : 52396032 (49.97 GiB 53.65 GB)
   Raid Devices : 3
  Total Devices : 4
    Persistence : Superblock is persistent

    Update Time : Sat Nov 21 14:40:45 2015
          State : clean, degraded, recovering 
 Active Devices : 2
Working Devices : 3
 Failed Devices : 1
  Spare Devices : 1

         Layout : left-symmetric
     Chunk Size : 512K

 Rebuild Status : 20% complete

           Name : docker.example.com:0  (local to host docker.example.com)
           UUID : ac110faf:5f345efc:8540013c:cd7f95bc
         Events : 51

    Number   Major   Minor   RaidDevice State
       0       8       16        0      active sync   /dev/sdb
       3       8       64        1      spare rebuilding   /dev/sde
       4       8       48        2      active sync   /dev/sdd

       1       8       32        -      faulty   /dev/sdc

[root@docker ~]# cat /proc/mdstat 
Personalities : [raid6] [raid5] [raid4] 
md0 : active raid5 sdd[4] sde[3] sdc[1](F) sdb[0]
      104792064 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/2] [U_U]
      [==============>......]  recovery = 72.4% (37981412/52396032) finish=1.2min speed=189532K/sec

unused devices: <none>
注意:以上信息表明阵列正在重建,当设备出现故障或被标记故障时,相应设备的方括号后将被标以(F)

2、重建完数据后,再次查看阵列状态时,就会发现当前的RAID设备又恢复了正常

[root@docker ~]# cat /proc/mdstat 
Personalities : [raid6] [raid5] [raid4] 
md0 : active raid5 sdd[4] sde[3] sdc[1](F) sdb[0]
      104792064 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]

unused devices: <none>

3、移除故障磁盘

[root@docker ~]# mdadm /dev/md0 --remove /dev/sdc
mdadm: hot removed /dev/sdc from /dev/md0

[root@docker ~]# mdadm --detail /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Sat Nov 21 14:20:48 2015
     Raid Level : raid5
     Array Size : 104792064 (99.94 GiB 107.31 GB)
  Used Dev Size : 52396032 (49.97 GiB 53.65 GB)
   Raid Devices : 3
  Total Devices : 3
    Persistence : Superblock is persistent

    Update Time : Sat Nov 21 14:46:33 2015
          State : clean 
 Active Devices : 3
Working Devices : 3
 Failed Devices : 0
  Spare Devices : 0

         Layout : left-symmetric
     Chunk Size : 512K

           Name : docker.example.com:0  (local to host docker.example.com)
           UUID : ac110faf:5f345efc:8540013c:cd7f95bc
         Events : 68

    Number   Major   Minor   RaidDevice State
       0       8       16        0      active sync   /dev/sdb
       3       8       64        1      active sync   /dev/sde
       4       8       48        2      active sync   /dev/sdd
注意:此时发现没有备用磁盘 

4、添加备用磁盘

[root@docker ~]# mdadm /dev/md0 --add /dev/sdc
mdadm: added /dev/sdc

[root@docker ~]# mdadm --detail /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Sat Nov 21 14:20:48 2015
     Raid Level : raid5
     Array Size : 104792064 (99.94 GiB 107.31 GB)
  Used Dev Size : 52396032 (49.97 GiB 53.65 GB)
   Raid Devices : 3
  Total Devices : 4
    Persistence : Superblock is persistent

    Update Time : Sat Nov 21 14:48:11 2015
          State : clean 
 Active Devices : 3
Working Devices : 4
 Failed Devices : 0
  Spare Devices : 1

         Layout : left-symmetric
     Chunk Size : 512K

           Name : docker.example.com:0  (local to host docker.example.com)
           UUID : ac110faf:5f345efc:8540013c:cd7f95bc
         Events : 69

    Number   Major   Minor   RaidDevice State
       0       8       16        0      active sync   /dev/sdb
       3       8       64        1      active sync   /dev/sde
       4       8       48        2      active sync   /dev/sdd

       5       8       32        -      spare   /dev/sdc

你可能感兴趣的:(Linux)