Software RAID

tags: storage

mdadm RAID management tool

mdadm 是 Linux 上易於管理 RAID 的工具,可以用來建立、刪除、重組磁碟陣列,平常使用時會自動同步硬碟陣列的資料。運轉過程中如有硬碟故障,會將該硬碟標示為故障並移出陣列,待使用者手動更換新的硬碟並重新加入陣列後繼續同步。

Block Device

Disk

在 Unix-like 的 file system 中,硬碟本身以及硬碟裡面劃分的分割區都會虛擬化成 block device,操作時只需要對 device file 讀寫,device driver 會幫你將 file 的內容與對應的儲存裝置 address 同步,不需要自己面對雜七雜八的儲存裝置。

Path

  • 第 1 硬碟 -> /dev/sda
    • 第 1 分割區 -> /dev/sda1
    • 第 2 分割區 -> /dev/sda2
  • 第 2 硬碟 -> /dev/sdb
    • 第 1 分割區 -> /dev/sdb1
    • 第 2 分割區 -> /dev/sdb2

RAID

當然磁碟陣列也可以虛擬化成 block device,這樣我們一樣只需要對這個 device file 讀寫,device driver 就會處理後續的工作,使用者完全可以當作一般儲存裝置使用。

Path

  • 陣列叢集
    • 第 0 陣列 -> /dev/md0 (link to /dev/md/0)
    • 第 1 陣列 -> /dev/md1 (link to /dev/md/1)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
md 其實是 multiple devices 的縮寫

Basic

RAID 1 (mirror) 是最簡單的備份方式,將一模一樣的資料放在所有陣列中的硬碟,這邊以此為例介紹如何從 0 開始建立及管理 RAID。

Install mdadm

第一步就是安裝 mdadm,使用各自發行版的套件管理工具。

# Debian based
sudo apt-get install mdadm

Create disk array

這邊建立一個新的 RAID 1 在 2 顆硬碟的第 1 個分割區上,且有第 3 顆硬碟的第 1 個分割區作為備援。

  • --level= : RAID level,這邊是 RAID 1。
  • --raid-devices= : 使用硬碟數量,這邊是 2 個。
  • --spare-devices= : 備用硬碟數量,這邊是 1 個。
mdadm --create --level=1 --raid-devices=2 /dev/md0 /dev/sda1 /dev/sdb1 --spare-devices=1 /dev/sdc1

Lookup status

想知道目前所有 RAID 狀態或是各種進度資訊可以查看 /proc/mdstat

cat /proc/mdstat

想知道某個 md device 的資訊,以 md0 為例可以使用以下命令。

mdadm --detail /dev/md0

Assemble disk array

開機時需要根據設定重新組合 volume,或是設定更新也可以用這個指令更新硬碟對應到的 volume 分配。

mdadm --assemble --scan

Delete disk array

雖然是刪除 volume,但硬碟資料以及 RAID 設定還是存在的,跟剛開機的狀態是一樣的,只要重新 assemble disk array 就可以組回 volume 了。

mdadm --stop /dev/md0

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
刪除前必須要先 umount volume。
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
如果 volume 正在使用中導致無法 umountfuser -m 可查詢使用中的 process。

Settings

mdadm 設定檔位於 /etc/mdadm.conf,修改後必須重新 assemble disk array 才會生效。