Linux下使用mdadm創(chuàng)建和管理軟raid
注:本次操作以RHEL4為例,但應(yīng)該可以應(yīng)用到其它大部分的distro上(guess)。
mdadm的幾個常用參數(shù)
-C 創(chuàng)建Raid,后面跟參數(shù),代表raid設(shè)備的名稱。比如:/dev/md0,/dev/md1。 -n 用于創(chuàng)建磁盤陣列的磁盤個數(shù)。 -l Raid的級別。 -x 指定用于hotspare(熱備盤)的磁盤個數(shù)。如果陣列中有一塊硬盤壞了,它會立刻頂上,并rebuild; -D 顯示軟raid的詳細信息; -s 掃描配置文件(/etc/mdadm.conf)或'/proc/mdstat'來查看遺漏的信息f; 創(chuàng)建軟raid的大體流程
使用fdisk工具為新磁盤創(chuàng)建分區(qū);
使用mkfs.XXXX工具將剛才劃分好的分區(qū)格式化成某種格式的文件系統(tǒng)。比如:ext3,reiserfs等; 使用mdadm來創(chuàng)建軟raid; 創(chuàng)建/etc/mdadm.conf文件(注意文件的格式,包括是否有逗號等等。該文件是為了系統(tǒng)在重啟后能 夠自動啟用軟raid??梢圆榭?etc/rc.sysinit腳本,搜索'mdadm'字符串就明白了); 示例:創(chuàng)建軟raid5(+hotspare)
以下是我的一次實際操作的完整過程:
這是用'fdisk -l'命令查看到的我當前的磁盤和分區(qū)情況(只有/dev/sda在使用,
其它四個都是新磁盤,沒有分區(qū),沒有格式化): # fdisk -l Disk /dev/sda: 6442 MB, 6442450944 bytes
255 heads, 63 sectors/track, 783 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System
/dev/sda1 * 1 720 5783368+ 83 Linux /dev/sda2 721 783 506047+ 82 Linux swap Disk /dev/sdb: 214 MB, 214748160 bytes
64 heads, 32 sectors/track, 204 cylinders Units = cylinders of 2048 * 512 = 1048576 bytes Disk /dev/sdb doesn't contain a valid partition table
Disk /dev/sdc: 214 MB, 214748160 bytes
64 heads, 32 sectors/track, 204 cylinders Units = cylinders of 2048 * 512 = 1048576 bytes Disk /dev/sdc doesn't contain a valid partition table
Disk /dev/sdd: 214 MB, 214748160 bytes
64 heads, 32 sectors/track, 204 cylinders Units = cylinders of 2048 * 512 = 1048576 bytes Disk /dev/sdd doesn't contain a valid partition table
Disk /dev/sde: 214 MB, 214748160 bytes
64 heads, 32 sectors/track, 204 cylinders Units = cylinders of 2048 * 512 = 1048576 bytes Disk /dev/sde doesn't contain a valid partition table
使用fdisk創(chuàng)建分區(qū)(本例中將整塊磁盤劃分為一個主分區(qū)。其余幾塊磁盤也做相同的操作。): # fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected
by w(rite)
Command (m for help): n
Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-204, default 1): Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-204, default 204): Using default value 204 Command (m for help): w
The partition table has been altered! Calling ioctl() to re-read partition table.
Syncing disks. 為剛才新建的分區(qū)建立文件系統(tǒng)(其余幾個分區(qū)依次做相同的操作): # mkfs.ext3 /dev/sdb1 mke2fs 1.35 (28-Feb-2004) Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) 52416 inodes, 208880 blocks 10444 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=67371008 26 block groups 8192 blocks per group, 8192 fragments per group 2016 inodes per group Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729, 204801 Writing inode tables: done
Creating journal (4096 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override. 所有磁盤都操作完后,再次用'fdisk -l'查看磁盤及分區(qū)狀態(tài): # fdisk -l
Disk /dev/sda: 6442 MB, 6442450944 bytes
255 heads, 63 sectors/track, 783 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System
/dev/sda1 * 1 720 5783368+ 83 Linux /dev/sda2 721 783 506047+ 82 Linux swap Disk /dev/sdb: 214 MB, 214748160 bytes
64 heads, 32 sectors/track, 204 cylinders Units = cylinders of 2048 * 512 = 1048576 bytes Device Boot Start End Blocks Id System
/dev/sdb1 1 204 208880 83 Linux Disk /dev/sdc: 214 MB, 214748160 bytes
64 heads, 32 sectors/track, 204 cylinders Units = cylinders of 2048 * 512 = 1048576 bytes Device Boot Start End Blocks Id System
/dev/sdc1 1 204 208880 83 Linux Disk /dev/sdd: 214 MB, 214748160 bytes
64 heads, 32 sectors/track, 204 cylinders Units = cylinders of 2048 * 512 = 1048576 bytes Device Boot Start End Blocks Id System
/dev/sdd1 1 204 208880 83 Linux Disk /dev/sde: 214 MB, 214748160 bytes
64 heads, 32 sectors/track, 204 cylinders Units = cylinders of 2048 * 512 = 1048576 bytes Device Boot Start End Blocks Id System
/dev/sde1 1 204 208880 83 Linux 使用mdadm創(chuàng)建一個軟raid,raid級別:5;并有一個hotspare盤: # mdadm -C /dev/md0 -l5 -n3 -x1 /dev/sd[b-e]1 mdadm: array /dev/md0 started. 輸出信息顯示軟raid(/dev/md0)已經(jīng)啟用了。使用mdadm的-D參數(shù)(--detail)可以查 看軟raid狀態(tài): # mdadm -D /dev/md0
/dev/md0: Version : 00.90.01 Creation Time : Wed Aug 23 15:10:19 2006 Raid Level : raid5 Array Size : 417536 (407.75 MiB 427.56 MB) Device Size : 208768 (203.88 MiB 213.78 MB) Raid Devices : 3 Total Devices : 4 Preferred Minor : 0 Persistence : Superblock is persistent Update Time : Wed Aug 23 15:10:21 2006
State : clean Active Devices : 3 Working Devices : 4 Failed Devices : 0 Spare Devices : 1 Layout : left-symmetric
Chunk Size : 64K Number Major Minor RaidDevice State
0 8 17 0 active sync /dev/sdb1 1 8 33 1 active sync /dev/sdc1 2 8 49 2 active sync /dev/sdd1 3 8 65 -1 spare /dev/sde1 UUID : f8283de5:39c73d89:b9fbc266:fdceb416 Events : 0.2 生成配置文件(/etc/mdadm.conf):
# mdadm -D -s >/etc/mdadm.conf
查看一下: # cat /etc/mdadm.conf
ARRAY /dev/md0 level=raid5 num-devices=3 UUID=f8283de5:39c73d89:b9fbc266:fdceb416 devices=/dev/sdb1,/dev/sdc1,/dev/sdd1,/dev/sde1 修改(把上面devices=后面的磁盤,都放到DEVICE后面,并且不要逗號。而/dev/md0之后 的內(nèi)容,都要用逗號來分隔): DEVICE /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
ARRAY /dev/md0 level=raid5,num-devices=3,UUID=f8283de5:39c73d89:b9fbc266:fdceb416 重啟一下,檢測配置好的軟raid是否能夠在系統(tǒng)重啟后自動啟用。 重啟后,查看'/proc/mdstat'文件就可以看到軟raid的狀態(tài):
# cat /proc/mdstat
Personalities : [raid5] md0 : active raid5 sdb1[0] sde1[3] sdd1[2] sdc1[1] 417536 blocks level 5, 64k chunk, algorithm 2 [3/3] [UUU] unused devices:
That's all. 出現(xiàn)故障后的恢復
這里指的出現(xiàn)故障,是指raid中的一塊磁盤出現(xiàn)了故障,無法使用。這時候需要使用額外的 磁盤來代替它。這里以強制將某塊磁盤標記為已損壞,來模擬實際出現(xiàn)故障(注:新的磁盤 的容量最好和已損壞的磁盤一致): 將/dev/sdb1標記為已損壞:
# mdadm /dev/md0 -f /dev/sdb1
mdadm: set /dev/sdb1 faulty in /dev/md0 這時候使用mdadm的-D參數(shù)來查看狀態(tài),可以看到/dev/sdb1已經(jīng)被認為是faulty,而 hotspare(熱備)盤'/dev/sde1'已經(jīng)頂替了它的位置(這就是hotspare的作用): # mdadm -D /dev/md0
/dev/md0: Version : 00.90.01 Creation Time : Wed Aug 23 15:10:19 2006 Raid Level : raid5 Array Size : 417536 (407.75 MiB 427.56 MB) Device Size : 208768 (203.88 MiB 213.78 MB) Raid Devices : 3 Total Devices : 4 Preferred Minor : 0 Persistence : Superblock is persistent Update Time : Wed Aug 23 15:42:24 2006
State : clean Active Devices : 3 Working Devices : 3 Failed Devices : 1 Spare Devices : 0 Layout : left-symmetric
Chunk Size : 64K Number Major Minor RaidDevice State
0 8 65 0 active sync /dev/sde1 1 8 33 1 active sync /dev/sdc1 2 8 49 2 active sync /dev/sdd1 3 8 17 -1 faulty /dev/sdb1 UUID : f8283de5:39c73d89:b9fbc266:fdceb416 Events : 0.4 既然'/dev/sdb1'出現(xiàn)了故障,當然就要將它移除: # mdadm /dev/md0 -r /dev/sdb1
mdadm: hot removed /dev/sdb1 現(xiàn)在可以關(guān)機了。關(guān)機之后拔下這塊已損壞的磁盤了,換上你的新磁盤。換好之 后,分區(qū),mkfs.XXXX。然后將它加入到軟raid中: # mdadm /dev/md0 -a /dev/sdb1
mdadm: hot added /dev/sdb1 這時候再使用mdadm的'-D'參數(shù),可以看到sdb1已經(jīng)作為hotspare盤了: # mdadm -D /dev/md0
/dev/md0: Version : 00.90.01 Creation Time : Wed Aug 23 15:10:19 2006 Raid Level : raid5 Array Size : 417536 (407.75 MiB 427.56 MB) Device Size : 208768 (203.88 MiB 213.78 MB) Raid Devices : 3 Total Devices : 4 Preferred Minor : 0 Persistence : Superblock is persistent Update Time : Wed Aug 23 16:19:36 2006
State : clean Active Devices : 3 Working Devices : 4 Failed Devices : 0 Spare Devices : 1 Layout : left-symmetric
Chunk Size : 64K Number Major Minor RaidDevice State
0 8 65 0 active sync /dev/sde1 1 8 33 1 active sync /dev/sdc1 2 8 49 2 active sync /dev/sdd1 3 8 17 -1 spare /dev/sdb1 UUID : f8283de5:39c73d89:b9fbc266:fdceb416 Events : 0.6 misc 假如創(chuàng)建了RAID,但是沒有生成 /etc/mdadm.conf 文件,那么系統(tǒng)重啟后是 不會啟用RAID的,這時候需要這樣做: # mdadm -A /dev/md0 /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1 |
|