1. 程式人生 > 實用技巧 >轉載【Linux中建立軟raid】

轉載【Linux中建立軟raid】

 原文地址:https://www.cnblogs.com/diantong/p/10547081.html

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 existson/dev/sdb mdadm: partition table existson/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 : Superblockispersistent 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 =internallog 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:thisarray has metadata at the start and may not be suitableasa boot device. If you plan to store'/boot'onthisdevice please ensure that your boot-loader understands md/v1.x metadata, or use --metadata=0.90 mdadm: sizesetto 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 : Superblockispersistent 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 =internallog 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 : Superblockispersistent 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 faultyin/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 : Superblockispersistent 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/sdefrom/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: sizesetto 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 : Superblockispersistent 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 : Superblockispersistent 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 (defaultp): 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 (defaultp): 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 (defaultp): 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 (defaulte): 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: sizesetto 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