硬件raid:主板上含有raid卡,要启用raid的时候必须进入bios启动raid卡得功能,并在里面指定raid的级别以及硬盘,当我们启用硬件raid,此时当我们装操作系统的时候,系统必须含有raid的驱动程序,因为此时在硬件级别上,并不会认为有硬盘,而是认为有raid。
软件raid:利用软件模拟raid
条件:内核中含有md(multi disk)模块(linux通过设备文件访问设备,但是我们硬盘在linux中被模拟成sda,hda等,并不是raid设置,于是md模块将硬盘模拟成/dev/md#文件,操作系统通过它操作raid设备,数据的存储访问过程由模块提供的功能完成)
性能:性能取决于cpu,cpu本身能力和空闲与否等等
注意:做软raid的硬盘必须标识为内核能识别的类型:fd类型,这样当操作系统使用raid设备的时候会在硬盘上存储一些元数据,用以记录软raid的信息,否则,当系统挂掉的时候,raid的里面的数据将不能再使用,从而导致数据丢失
使用方法:
mdadm:将任何块设备做成raid
模式化命令:
创建模式
-C
-l:级别
-n:设备个数
-a {yes|0}:是否自动为其创建设备文件
-c|--chunk:默认64kB(chunk指的是条带技术中每次分割数据块的大小)
-x:指定空闲盘个数
管理模式
--add --del
监控模式
-F
增长模式
-G
装配模式
-A
查看raid阵列的详细信息:
mdadm -D(--detail) /dev/md#
cat /proc/mdstat
停止阵列
mdadm -S /dev/md#
--stop
将当前raid信息保存至配置文件,以便后续进行装配:
mdamd -D --scan > /etc/mdadm.conf
watch:周期性地执行指定命令,并以全屏方式显示结果
-n #:指定周期长度,单位为秒,默认为2
格式: watch -n # ‘COMMAND’
mke2fs -j -E stride=16 -b 4096 /dev/md0
创建raid的时候如果没指定chunk大小,则默认为64k,但是1个chunk默认有几个数据块呢,这就要看block大小,假设block为4096,则默认就有64k/4k=16块,但是raid每次都要计算这个块数,因此我们在格式化的时候直接指定block和块数(也即stride),这样可以优化raid性能。
软raid例子:
[[email protected] ~]# mdadm -C /dev/md0 -a yes -n 2 -l 1 /dev/sdb1 /dev/sdb2 (创建raid1)
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
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
[[email protected] ~]# cat /proc/mdstat (查看raid状态)
Personalities : [raid1]
md0 : active raid1 sdb1[0] sdb2[1]
2102400 blocks super 1.2 [2/2] [UU]
unused devices: <none>
[[email protected] ~]# mke2fs -j -E stride=16 -b 4096 /dev/md0 (格式化raid)
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
(限于篇幅,以下次要命令,只截取部分信息用以说明问题)
[[email protected] ~]# mkdir /md0
[[email protected] ~]# mount /dev/md0 /md0/ (挂载raid)
[[email protected] ~]# cd /md0/
[[email protected] md0]# cp /etc/inittab .
[[email protected] md0]# cat inittab
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
[[email protected] md0]# mdadm -D /dev//md0 (查看raid1详细信息)
Number Major Minor RaidDevice State
0 8 17 0 active sync /dev/sdb1
1 8 18 1 active sync /dev/sdb2
[[email protected] md0]# mdadm /dev/md0 -f /dev/sdb1 (模拟sdb1挂掉)
mdadm: set /dev/sdb1 faulty in /dev/md0
[[email protected] md0]# !ca (此时文件仍可正常访问)
cat inittab
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
[[email protected] md0]# mdadm -D /dev//md0
Number Major Minor RaidDevice State
0 0 0 0 removed
1 8 18 1 active sync /dev/sdb2
0 8 17 - faulty /dev/sdb1
[[email protected] md0]# fdisk /dev/sdb
WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to
switch off the mode (command ‘c‘) and change display units to
sectors (command ‘u‘).
Command (m for help): n
Command action
e extended
p primary partition (1-4)
3
Invalid partition number for type `3‘
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (525-2610, default 525):
Using default value 525
Last cylinder, +cylinders or +size{K,M,G} (525-2610, default 2610): +2G
Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): fd
Changed system type of partition 3 to fd (Linux raid autodetect)
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xddd53329
Device Boot Start End Blocks Id System
/dev/sdb1 1 262 2104483+ fd Linux raid autodetect
/dev/sdb2 263 524 2104515 fd Linux raid autodetect
/dev/sdb3 525 786 2104515 fd Linux raid autodetect
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[[email protected] md0]# mdadm /dev/md0 -r /dev/sdb1 (移除sdb1)
mdadm: hot remove /dev/sdb1
[[email protected] md0]# mdadm /dev/md0 -a /dev/sdb3 (增加sdb3)
mdadm: added /dev/sdb3
[[email protected] md0]# mdadm -D /dev/md0
Number Major Minor RaidDevice State
2 8 19 0 active sync /dev/sdb3
1 8 18 1 active sync /dev/sdb2
[[email protected] /]# mdadm -S /dev/md0
mdadm: Cannot get exclusive access to /dev/md0:Perhaps a running process, mounted filesystem or active volume group?
[[email protected] /]# umount /dev/md0
[[email protected] /]# mdadm -S /dev/md0
mdadm: stopped /dev/md0
[[email protected] /]# rm /dev/md0 (到这里md0彻底移除)
rm: remove block special file `/dev/md0‘? y
[[email protected] ~]# mdadm -C /dev/md0 -a yes -n 2 -l 0 /dev/sdb1 /dev/sdb2 -x 1 /dev/sdb3
mdadm: spare-devices setting is incompatible with raid level 0
(创建raid的时候,设置冗余,一个盘坏掉的时候空闲盘自动替换)