Centos7 configuration RAID and management

1. What is RAID
RAID is called a disk array, which is mainly used to improve storage availability and performance, while greatly improving the fault tolerance of disks. Disk arrays are composed of multiple physical disks, but for operating systems , It is a logical disk, and the data stored on the system will be distributed across multiple physical disks in the array.
2. Several levels of RAID

level Description Minimum number of physical disks Maximum fault tolerance I/O performance Disk capacity utilization
RAID 1 Level 1 is also called disk mirroring. It also requires 2 disks, but the disks are fault-tolerant. 2 disks required Allow one hard drive to fail Improved read performance, average write performance 50%
RAID 5 Level 5 is called a distributed parity check array. The check bit of the data is used to ensure the security of the data, but the check bit of the data is not stored in a separate disk, but is stored interactively on the disk in each array. If any disk in the array fails, the damaged data can be reconstructed based on the parity bit of other disks At least 3 disks Allow one hard drive to fail Better read and write performance 90%
RAID 6 Level 6 is similar to level 5. Level 5 will store the data verification information on the hard disk, and level 6 will save one more verification information than level 5. At least 4 hard drives are required Allow two hard drives to fail Better read and write performance 80%
RAID 10 It is called high reliability and efficient disk structure, which has the advantages of level 0 and level 1. This level first uses two disks to establish mirroring, and then the mirrored logical disk is made level 0 At least 4 hard drives are required Allow one disk to fail Good read and write performance 50%

3. RAID types

species Description
Soft RAID Use the software technology provided by the operating system to realize the RAID function. The advantages are cheap and there is almost no cost. The disadvantages are limited to the stability of the operating system and occupy certain system resources.
Hard RAID Realized by hardware RAID controller, the advantage is that it does not need to occupy other hardware resources, while providing a cache mechanism, greatly improving the read and write performance of the disk, and the stability is also very high

4. Realize software RAID
simulation platform on Linux system : VMware workstation
Test environment: a centos7 computer, add a hard disk
Use tool: mdadm
[raid1] Implementation process:
1. First check the disk label type.
If it is MBR, use fdisk to create the same size
If the disk partition is GPT, you can use gdisk to create disk partitions of the same size

[root@lab-236 ~]# fdisk -l /dev/sdb
磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:gpt              #硬盘类型GPT
Disk identifier: 037192CD-B94D-4F5D-A559-A90B74EECA9D
  Start          End    Size  Type            Name

2. Create 2 partitions and change the partition system type to Linux RAID.
Because our disk label type is GPT, we use gdisk to execute

[root@lab-236 ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): n     #创建新的分区
Partition number (1-128, default 1): 1      #分区编号1
First sector (34-41943006, default = 2048) or {+-}size{KMGTP}:     #指定起始扇区,默认即可
Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: +5G   #指定分区大小
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): fd00      #更改分区系统类型为LinuxRAID
Changed type of partition to 'Linux RAID'

Command (? for help): n   #继续创建第二分区
Partition number (2-128, default 2): 2     #分区编号2
First sector (34-41943006, default = 10487808) or {+-}size{KMGTP}:  #指定起始扇区,默认即可
Last sector (10487808-41943006, default = 41943006) or {+-}size{KMGTP}: +5G  #指定分区大小
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): fd00   #更改分区系统类型为LinuxRAID
Changed type of partition to 'Linux RAID'

Command (? for help): p     #显示分区信息
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 037192CD-B94D-4F5D-A559-A90B74EECA9D
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 20971453 sectors (10.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        10487807   5.0 GiB     FD00  Linux RAID
   2        10487808        20973567   5.0 GiB     FD00  Linux RAID

Command (? for help): w         #保存分区信息
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): Y        #分区发生改变,提示是否继续保存
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.

3. Execute the mdadm tool to create RAID1.
Note that the creation of RAID device numbers should start from 0

[root@lab-236 ~]# mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdb2 
mdadm: /dev/sdb1 appears to be part of a raid array:
       level=raid1 devices=2 ctime=Thu Dec 10 20:52:02 2020
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: /dev/sdb2 appears to be part of a raid array:
       level=raid1 devices=2 ctime=Thu Dec 10 20:52:02 2020
Continue creating array? Y      #提示创建是否继续,是Y
mdadm: Fail to create md0 when using /sys/module/md_mod/parameters/new_array, fallback to creation via node
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

Note: During the creation process, there is a message that this array is not suitable for use as a boot disk. This is determined by the type of super block. The type of super block can be set using the –metadata option. The default is 1.2, and only when this value is not greater than 1.0. As a boot disk
4. Check the detailed information and operating status of the created RAID

[root@lab-236 ~]# mdadm --detail /dev/md0       #查看RAID详细信息
/dev/md0:
           Version : 1.2
     Creation Time : Fri Dec 11 16:46:33 2020
        Raid Level : raid1
        Array Size : 5237760 (5.00 GiB 5.36 GB)
     Used Dev Size : 5237760 (5.00 GiB 5.36 GB)
      Raid Devices : 2
     Total Devices : 2
       Persistence : Superblock is persistent
       Update Time : Fri Dec 11 16:46:59 2020
             State : clean 
    Active Devices : 2
   Working Devices : 2
    Failed Devices : 0
     Spare Devices : 0
Consistency Policy : resync
              Name : lab-236.com:0  (local to host lab-236.com)
              UUID : 52f68880:b4d10435:d271aaa2:261ed9cb
            Events : 17
    Number   Major   Minor   RaidDevice State
       0       8       17        0      active sync   /dev/sdb1
       1       8       18        1      active sync   /dev/sdb2
[root@lab-236 ~]# cat /proc/mdstat          #查看运行状态
Personalities : [raid1] 
md0 : active raid1 sdb2[1] sdb1[0]
      5237760 blocks super 1.2 [2/2] [UU]
unused devices: <none>

5. Create a RAID partition, and then format the file system before it can be used

[root@lab-236 ~]# mkfs.xfs   /dev/md0       #格式化为xfs文件系统
meta-data=/dev/md0               isize=512    agcount=4, agsize=327360 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1309440, 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

6. Mount the RAID partition to the specified directory, where it is mounted to /mnt

[root@lab-236 ~]# mount /dev/md0 /mnt

View disk usage through the df command

[root@lab-236 ~]# df  -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 471M     0  471M    0% /dev
tmpfs                    488M  8.1M  480M    2% /run
tmpfs                    488M     0  488M    0% /sys/fs/cgroup
/dev/mapper/centos-root   17G  4.5G   12G   28% /
/dev/md0                 5.0G   33M  5.0G    1% /mnt        #可以看到挂载成功

7. Simulate disk failure and repair disk RAID.
First, we create a file in the directory where the RAID partition is mounted

[root@lab-236 ~]# echo "hello world" > /mnt/hello.txt

At this time, we use the mdadm tool to set a partition (/dev/sdb1) as a fault partition

[root@lab-236 ~]# mdadm /dev/md0 --fail /dev/sdb1
mdadm: set /dev/sdb1 faulty in /dev/md0

Then we are viewing the RAID configuration information

[root@lab-236 ~]# mdadm --detail /dev/md0 
/dev/md0:
           Version : 1.2
     Creation Time : Fri Dec 11 16:46:33 2020
        Raid Level : raid1
        Array Size : 5237760 (5.00 GiB 5.36 GB)
     Used Dev Size : 5237760 (5.00 GiB 5.36 GB)
      Raid Devices : 2
     Total Devices : 2
       Persistence : Superblock is persistent
       Update Time : Fri Dec 11 17:05:32 2020
             State : clean, degraded 
    Active Devices : 1
   Working Devices : 1
    Failed Devices : 1
     Spare Devices : 0
Consistency Policy : resync
              Name : lab-236.com:0  (local to host lab-236.com)
              UUID : 52f68880:b4d10435:d271aaa2:261ed9cb
            Events : 21
    Number   Major   Minor   RaidDevice State
       -       0        0        0      removed
       1       8       18        1      active sync   /dev/sdb2
       0       8       17        -      faulty   /dev/sdb1     #此时/dev/sdb1状态时有缺陷的

We can see that the status of /dev/sdb1 is faulty, it is defective, and there is a fault.
Check the operation

[root@lab-236 ~]# cat  /proc/mdstat 
Personalities : [raid1] 
md0 : active raid1 sdb2[1] sdb1[0](F)
      5237760 blocks super 1.2 [2/1] [_U]   #发现超级块数量变成[2/1],缺失1个

unused devices: <none>

8. At this time, we check whether the files in the mount directory are normal

[root@lab-236 ~]# cat  /mnt/hello.txt 
hello world

We found that the file is normal, which is the fault tolerance of RAID1. A disk failure will not affect the continued use of data files.
9. Next, we repair the RAID.
First, we must remove the defective disk partition

[root@lab-236 ~]# mdadm /dev/md0 --remove /dev/sdb1
mdadm: hot removed /dev/sdb1 from /dev/md0

Then we will add the purchased new disk to this RAID. If it is in the actual physical environment, just replace the physical disk and add it. Here we simulate the environment. Just add it back when /dev/sdb1 is repaired

[root@lab-236 ~]# mdadm /dev/md0 --add /dev/sdb1
mdadm: added /dev/sdb1

At this time, we check the RAID information and we can see that the newly added disk is undergoing spare rebuilding RAID. This process takes time, and the length of time is related to the size of the space data.

[root@lab-236 ~]# mdadm --detail /dev/md0 
/dev/md0:
           Version : 1.2
     Creation Time : Fri Dec 11 16:46:33 2020
        Raid Level : raid1
        Array Size : 5237760 (5.00 GiB 5.36 GB)
     Used Dev Size : 5237760 (5.00 GiB 5.36 GB)
      Raid Devices : 2
     Total Devices : 2
       Persistence : Superblock is persistent
       Update Time : Fri Dec 11 17:24:08 2020
             State : clean, degraded, recovering 
    Active Devices : 1
   Working Devices : 2
    Failed Devices : 0
     Spare Devices : 1
Consistency Policy : resync
    Rebuild Status : 57% complete
              Name : lab-236.com:0  (local to host lab-236.com)
              UUID : 52f68880:b4d10435:d271aaa2:261ed9cb
            Events : 41
    Number   Major   Minor   RaidDevice State
       2       8       17        0      spare rebuilding   /dev/sdb1
       1       8       18        1      active sync   /dev/sdb2

Summary:
1. If you have created a soft RAID before in the configuration process, you need to stop the original RAID device first

mdadm  --stop  /dev/md0

If you don’t stop, the following error will appear:
mdadm: cannot open /dev/sdb1: Device or resource busy
2. If you want to rebuild the software RAID, the steps should be as follows:
1. Unmount the directory first
2. Stop the RAID device
3. Remove the RAID device Disk

Guess you like

Origin blog.51cto.com/6461704/2563020