CentOS7 - 软raid

前言


参考链接:https://blog.csdn.net/u011130086/article/details/119172119

有台服务器没有 RAID 卡,虽然数据不太重要,但是真要丢失了还是会存在一些麻烦。因此考虑用 软 RAID 的方式实现数据的冗余。


实现过程


首先,在虚拟机上模拟这个过程,环境如下:

OS:	CentOS Linux release 7.9.2009
Kernel:	3.10.0-1160.el7.x86_64

Raid disk:
vdb             252:16   0   10G  0 disk
vdc             252:32   0   10G  0 disk

以上两块10G 的盘,做一个 RAID-1 :

yum install -y mdadm

### 执行创建命令:
# -C : create 创建
# -a : auto 
# -l : level RAID 组
# -n : 磁盘数量
# /dev/vd{b,c} : vdb/vdc

> mdadm -C /dev/md0 -a yes -l 1 -n 2 /dev/vd{b,c}
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
Continue creating array? yes
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

创建成功后,可以查看初始化执行进度:

### 静态查看:
> cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 vdc[1] vdb[0]
      10478400 blocks super 1.2 [2/2] [UU]
      [============>........]  resync = 62.4% (6549120/10478400) finish=0.8min speed=75277K/sec
     
### 动态查看:
> watch -n 1 cat /proc/mdstat

两个命令查看创建结果:

> mdadm -Q /dev/md0
/dev/md0: 9.99GiB raid1 2 devices, 0 spares. Use mdadm --detail for more detail.

> mdadm --detail /dev/md0
/dev/md0:
           Version : 1.2
     Creation Time : Mon Sep 26 15:03:23 2022
        Raid Level : raid1
        Array Size : 10478400 (9.99 GiB 10.73 GB)
     Used Dev Size : 10478400 (9.99 GiB 10.73 GB)
      Raid Devices : 2
     Total Devices : 2
       Persistence : Superblock is persistent

       Update Time : Mon Sep 26 15:06:02 2022
             State : clean
    Active Devices : 2
   Working Devices : 2
    Failed Devices : 0
     Spare Devices : 0

Consistency Policy : resync

              Name : localhost.localdomain:0  (local to host localhost.localdomain)
              UUID : 85f49180:c8da6dad:fe8b2f7d:d63baa9e
            Events : 17

    Number   Major   Minor   RaidDevice State
       0     252       16        0      active sync   /dev/vdb
       1     252       32        1      active sync   /dev/vdc

创建文件系统:

> mkfs.xfs /dev/md0
meta-data=/dev/md0               isize=512    agcount=4, agsize=654900 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=2619600, 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

挂载:

### 临时挂载:
> mkdir /test
> mount /dev/md0 /test/
> df | egrep test
/dev/md0                xfs        10G   33M   10G   1% /test

### 开机挂载:
> egrep test /etc/fstab
/dev/md0                /test                   xfs     defaults        0 0

创建并修改 RAID 开机启动文件:

> mdadm --detail /dev/md0 | egrep UUID > /etc/mdadm.conf
### 修改如下:
> cat /etc/mdadm.conf
### UUID 为上面命令获取的
ARRAY   /dev/md0              UUID=85f49180:c8da6dad:fe8b2f7d:d63baa9e

做完上面的配置后,重启主机尝试。

> lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE  MOUNTPOINT
sr0              11:0    1 1024M  0 rom
vda             252:0    0  100G  0 disk
├─vda1          252:1    0    1G  0 part  /boot
└─vda2          252:2    0   99G  0 part
  ├─centos-root 253:0    0 91.1G  0 lvm   /
  └─centos-swap 253:1    0  7.9G  0 lvm   [SWAP]
vdb             252:16   0   10G  0 disk
└─md0             9:0    0   10G  0 raid1 /test
vdc             252:32   0   10G  0 disk
└─md0             9:0    0   10G  0 raid1 /test

> reboot

>lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE  MOUNTPOINT
sr0              11:0    1 1024M  0 rom
vda             252:0    0  100G  0 disk
├─vda1          252:1    0    1G  0 part  /boot
└─vda2          252:2    0   99G  0 part
  ├─centos-root 253:0    0 91.1G  0 lvm   /
  └─centos-swap 253:1    0  7.9G  0 lvm   [SWAP]
vdb             252:16   0   10G  0 disk
└─md0             9:0    0   10G  0 raid1 /test
vdc             252:32   0   10G  0 disk
└─md0             9:0    0   10G  0 raid1 /test
>touch /test/abc
>ls /test/
abc

测试


模拟损坏一块硬盘,查看 RAID 状态

### 直接通过宿主机将这块盘模拟拔出
> virsh detach-disk 192_168_1_108 /kvm_images/192_168_1_108_vdc.qcow2 --live --config

查看 RAID状态:

>mdadm --detail /dev/md0
/dev/md0:
           Version : 1.2
     Creation Time : Mon Sep 26 15:03:23 2022
        Raid Level : raid1
        Array Size : 10478400 (9.99 GiB 10.73 GB)
     Used Dev Size : 10478400 (9.99 GiB 10.73 GB)
      Raid Devices : 2
     Total Devices : 1
       Persistence : Superblock is persistent

       Update Time : Mon Sep 26 15:17:36 2022
             State : clean, degraded	### 状态也出现了 degraded
    Active Devices : 1
   Working Devices : 1
    Failed Devices : 0
     Spare Devices : 0

Consistency Policy : resync

              Name : localhost.localdomain:0  (local to host localhost.localdomain)
              UUID : 85f49180:c8da6dad:fe8b2f7d:d63baa9e
            Events : 20

    Number   Major   Minor   RaidDevice State
       0     252       16        0      active sync   /dev/vdb
       -       0        0        1      removed			### 可以发现已经处于 removed状态

查看数据是否还存在:

>lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE  MOUNTPOINT
sr0              11:0    1 1024M  0 rom
vda             252:0    0  100G  0 disk
├─vda1          252:1    0    1G  0 part  /boot
└─vda2          252:2    0   99G  0 part
  ├─centos-root 253:0    0 91.1G  0 lvm   /
  └─centos-swap 253:1    0  7.9G  0 lvm   [SWAP]
vdb             252:16   0   10G  0 disk
└─md0             9:0    0   10G  0 raid1 /test

### 数据依然存在。
root@localhost(192.168.1.108)~>ls /test/
abc

模拟加入新硬盘:

> virsh attach-disk 192_168_1_108 /kvm_images/192_168_1_108_vdc.qcow2  vdc --config --live
> lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE  MOUNTPOINT
sr0              11:0    1 1024M  0 rom
vda             252:0    0  100G  0 disk
├─vda1          252:1    0    1G  0 part  /boot
└─vda2          252:2    0   99G  0 part
  ├─centos-root 253:0    0 91.1G  0 lvm   /
  └─centos-swap 253:1    0  7.9G  0 lvm   [SWAP]
vdb             252:16   0   10G  0 disk
└─md0             9:0    0   10G  0 raid1 /test
vdd             252:48   0   10G  0 disk	### 卸载的是vdc 再次装载变成 vdd


### 加入新RAID组
> mdadm --manage /dev/md0 --add /dev/vdd
> mdadm --detail /dev/md0
/dev/md0:
           Version : 1.2
     Creation Time : Mon Sep 26 18:22:08 2022
        Raid Level : raid1
        Array Size : 10478400 (9.99 GiB 10.73 GB)
     Used Dev Size : 10478400 (9.99 GiB 10.73 GB)
      Raid Devices : 2
     Total Devices : 2
       Persistence : Superblock is persistent

       Update Time : Mon Sep 26 18:30:43 2022
             State : clean, degraded, recovering
    Active Devices : 1
   Working Devices : 2
    Failed Devices : 0
     Spare Devices : 1

Consistency Policy : resync

    Rebuild Status : 6% complete

              Name : localhost.localdomain:0  (local to host localhost.localdomain)
              UUID : c9297ee4:5c04d6d9:d0552494:2f27df9a
            Events : 27

    Number   Major   Minor   RaidDevice State
       0     252       16        0      active sync   /dev/vdb
       2     252       48        1      spare rebuilding   /dev/vdd	### 处于 rebuilding 阶段


### 正在重建
> cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 vdd[2] vdb[0]
      10478400 blocks super 1.2 [2/1] [U_]
      [==========>..........]  recovery = 50.3% (5271168/10478400) finish=1.1min speed=76393K/sec
      


### 重建成功
> mdadm --detail /dev/md0
/dev/md0:
           Version : 1.2
     Creation Time : Mon Sep 26 18:22:08 2022
        Raid Level : raid1
        Array Size : 10478400 (9.99 GiB 10.73 GB)
     Used Dev Size : 10478400 (9.99 GiB 10.73 GB)
      Raid Devices : 2
     Total Devices : 2
       Persistence : Superblock is persistent

       Update Time : Mon Sep 26 18:32:57 2022
             State : clean
    Active Devices : 2
   Working Devices : 2
    Failed Devices : 0
     Spare Devices : 0

Consistency Policy : resync

              Name : localhost.localdomain:0  (local to host localhost.localdomain)
              UUID : c9297ee4:5c04d6d9:d0552494:2f27df9a
            Events : 43

    Number   Major   Minor   RaidDevice State
       0     252       16        0      active sync   /dev/vdb
       2     252       48        1      active sync   /dev/vdd
       
### 查看数据
> ls /test/
abc		### 数据未丢失

卸载软raid


  1. 卸载挂载
umount /test	### 取消挂载
  1. 停止软RAID
mdadm -S /dev/md5
  1. 查看是否停止
cat /proc/mdstat
  1. 清除 RAID所有磁盘
mdadm --misc --zero-superblock /dev/vdb
mdadm --misc --zero-superblock /dev/vdc
  1. 查看是否清除
lsblk
  1. 删除配置文件(防止系统启动时启动raid)
rm -rf /etc/mdadm.conf


--- EOF ---
posted @ 2022-09-26 19:02  hukey  阅读(673)  评论(0编辑  收藏  举报