1. 程式人生 > >Linux中建立軟raid

Linux中建立軟raid

mkf 磁盤故障 不變 設備信息 mda sym 狀態 size 標簽

  Linux內核中有一個md(multiple devices)模塊在底層管理RAID設備,它會在應用層給我們提供一個應用程序的工具mdadm。

  mdadm用於構建、管理和監視Linux MD設備(即RAID陣列)

(1).mdadm的常用選項

-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

[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的格式化和掛載和正常的設備沒有什麽區別,當成正常的設備即可,如下:

[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

[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

  模擬磁盤故障,自動頂替故障盤

[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  //更新配置文件

  移除損壞的盤

[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  //更新配置文件

  添加新盤

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

3)創建raid5

[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   //更新配置文件

  停止陣列,註意請確認數據已同步完成

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

  激活陣列,註意配置文件內必須存在

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

  擴展raid5自盤陣列

[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

[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的所有信息及註意事項

[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

  

Linux中建立軟raid