軟RAID是在操作系統層面進行的RAID配置,也能對數據進行保護,實際生產環境中使用存儲中磁盤陣列和硬RAID實現冗餘的情況比較多。
此實驗在虛擬機中完成,在系統中添加5塊磁盤,每塊磁盤512MB,利用這5塊磁盤做RAID5實驗,模擬磁盤損壞及替換磁盤,模擬停止RAID陣列及啓動陣列,陣列中的數據情況。
1.添加磁盤,每塊磁盤512MB,共5塊,如圖1所示。
2.啓動系統並查看磁盤
ll /dev/sd*
出現/dev/sdb,/dev/sdc,…/dev/sdf,如圖2所示,說明系統已識別到新添加的5塊磁盤。
3.磁盤分區,以/dev/sdb爲例(fdisk/dev/sdb),其它磁盤做相同操作。
[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI orOSF disklabel
Building a new DOS disklabel with disk identifier 0x2646775b.
Changes will remain in memory only, until you decide to writethem.
After that, of course, the previous content won't berecoverable.
Warning: invalid flag 0x0000 of partition table 4 will be correctedby w(rite)
WARNING: DOS-compatible mode is deprecated. It's stronglyrecommended to
switch off the mode(command 'c') and change display units to
sectors (command 'u').
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-512, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-512, default 512):
Using default value 512
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): fd
Changed system type of partition 1 to fd (Linux raidautodetect)
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# fdisk -l |more
注:Linux分區默認的類型編號爲83,RAID的類型編號爲fd,所以要將磁盤分區的類型改爲fd。
5塊磁盤全部分區完畢,如圖3所示
4.生成RAID5陣列
陣列名字爲/dev/md0,組成陣列的磁盤有4塊,分別爲/dev/sdb1、/dev/sdc1、dev/sdd1、/dev/sde1。
mdadm –C /dev/md0 –l 5 –n 3 –x 1 /dev/sd[b-e]1
參數說明:
-C Create a new array
-l Set RAID level
-n Specify the number of active devices in the array
-x Specify the number of spare devices in the initial array
5.將陣列格式化,類型爲ext4
mkfs –t ext4 /dev/md0
6.查看RAID信息
mdadm –D /dev/md0
從圖4可知,陣列中共有4塊磁盤,/dev/sdb1、/dev/sdc1、/dev/sdd1爲active狀態,/dev/sde1爲spare狀態。
7.建立掛載點,並掛載。
mkdir /raid5
mount /dev/md0 /raid5
8.使用RAID設備
9.設置磁盤/dev/sdb1損壞
mdadm /dev/md0 –f /dev/sdb1
10.再次查看RAID信息
mdadm –D /dev/md0
從圖5可知,/dev/sde1、/dev/sdc1、/dev/sdd1爲active狀態,/dev/sdb1爲faulty狀態。
11.檢查原陣列中的數據
cat /raid5/hello.txt
12.移除損壞的磁盤
mdadm /dev/md0 –r /dev/sdb1
13.向陣列中添加新的備用磁盤
mdadm /dev/md0 –a /dev/sdf1
14.查看添加備用磁盤後的RAID信息
mdadm –D /dev/md0
從圖6可知,/dev/sde1、/dev/sdc1、/dev/sdd1爲active狀態,/dev/sdf1爲spare狀態。
處於faulty狀態的/dev/sdb1已被移除。
15.檢查陣列中的數據
cat /raid5/hello.txt
16.卸載RAID設備,並停止RAID陣列
umount /dev/md0
mdadm -S /dev/md0
17.再啓動RAID陣列,並查看啓動陣列後的RAID信息
mdadm –A /dev/md0 /dev/sd[c-f]1
mdadm –D /dev/md0
18.掛載RAID設備,並檢查陣列中的數據
由圖可知陣列中的數據沒有受到影響,至此實驗結束。