linux下用mdadm命令实现软raid

Raid是当前存储提高安全和性能的主要技术手段,实现raid一般用raid卡实现,也就是硬raid。除此之外,我们还可以用软件来实现raid技术。

这篇文章就简单介绍如何用软件实现raid技术(以raid0为例)。

有两个可以实现软raid的工具:raidtools, mdadm。
raidtool,这是在RHEL3中所使用的,但是我在RHEL4中没有找到raidtool,只有mdadm,看来RH也是偏向于使用mdadm的。
本文也以mdadm为例讲述。

一、查看当前硬盘情况
[root@primary /]# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 1114 8843782+ 83 Linux
/dev/sda3 1115 1305 1534207+ 82 Linux swap

Disk /dev/sdb: 107 MB, 107374080 bytes
64 heads, 32 sectors/track, 102 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes

Disk /dev/sdb doesn’t contain a valid partition table

Disk /dev/sdc: 107 MB, 107374080 bytes
64 heads, 32 sectors/track, 102 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes

Disk /dev/sdc doesn’t contain a valid partition table

二、为硬盘分区
raid一般多个硬盘来组成,你也可以用同一个硬盘的多个分区组成raid,但这样是没有意义的。

[root@primary /]# 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-102, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-102, default 102):
Using default value 102

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@primary /]# fdisk /dev/sdc
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-102, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-102, default 102):
Using default value 102

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

此时硬盘的分区情况:
[root@primary /]# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 1114 8843782+ 83 Linux
/dev/sda3 1115 1305 1534207+ 82 Linux swap

Disk /dev/sdb: 107 MB, 107374080 bytes
64 heads, 32 sectors/track, 102 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 102 104432 83 Linux

Disk /dev/sdc: 107 MB, 107374080 bytes
64 heads, 32 sectors/track, 102 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes

Device Boot Start End Blocks Id System
/dev/sdc1 1 102 104432 83 Linux

三、创建raid0

[root@primary /]# mdadm –create /dev/md0 –level=raid0 –chunk=8 –raid-devices=2 /dev/sdb1 /dev/sdc1

四、格式化raid
mdadm: array /dev/md0 started.
[root@primary /]# mkfs.ext3 /dev/md0
mke2fs 1.35 (28-Feb-2004)
max_blocks 213647360, rsv_groups = 26080, rsv_gdb = 256
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
52208 inodes, 208640 blocks
10432 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
2008 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801

Writing inode tables: done
inode.i_blocks = 3586, i_size = 67383296
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

五、挂载raid分区
[root@primary /]# mount /dev/md0 /opt
[root@primary /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 8.4G 5.7G 2.3G 73% /
/dev/sda1 99M 8.4M 86M 9% /boot
none 252M 0 252M 0% /dev/shm
/dev/hdc 161M 161M 0 100% /media/cdrom
/dev/md0 198M 5.8M 182M 4% /opt

六、查看raid的信息
[root@primary opt]# mdadm –detail /dev/md0
/dev/md0:
Version : 00.90.01
Creation Time : Sun Jul 8 22:54:28 2007
Raid Level : raid0
Array Size : 208640 (203.75 MiB 213.65 MB)
Raid Devices : 2
Total Devices : 2
Preferred Minor : 0
Persistence : Superblock is persistent

Update Time : Sun Jul 8 22:54:29 2007
State : clean
Active Devices : 2
Working Devices : 2
Failed Devices : 0
Spare Devices : 0

Chunk Size : 8K

Number Major Minor RaidDevice State
0 8 17 0 active sync /dev/sdb1
1 8 33 1 active sync /dev/sdc1
UUID : a86f0502:df5715c0:fd871bbc:9f75e0ad
Events : 0.1

七、生成mdadm配置文件
mdadm的缺省配置文件为/etc/mdadm.conf,它主要是为了方便阵列的日常管理而设置的,对于阵列而言不是必须的,但是为了减少日后管理中不必要的麻烦,还是应该坚持把这一步做完。

在mdadm.conf文件中要包含两种类型的行:一种是以DEVICE开头的行,它指明在阵列中的设备列表;另一种是以ARRAY开头的行,它详细地说明了阵列的名称、模式、阵列中活动设备的数目以及设备的UUID号。
我们可以用mdadm -Ds来得到mdadm.conf文件需要的信息:
[root@primary ~]# mdadm -Ds
ARRAY /dev/md0 level=raid0 num-devices=2 UUID=63f24968:d367038d:f207e458:9a803df9
devices=/dev/sdb1,/dev/sdc1

根据上面的信息编辑/etc/mdadm.conf,如下:
[root@primary ~]# more /etc/mdadm.conf
ARRAY /dev/md0 level=raid0 num-devices=2 UUID=63f24968:d367038d:f207e458:9a803df9
device /dev/sdb1 /dev/sdc1

如果不配置这个文件,在重启后尝试mount raid设备的时候会报错:
[root@primary ~]# mount /dev/md0 /opt
/dev/md0: Invalid argument
mount: /dev/md0: can’t read superblock

八、设置开机自动挂载
为了让系统开机后能自动挂载raid设备,需要在/etc/fstab添加如下行:
vi /etc/fstab
/dev/md0 /opt ext3 defaults 0 0

至此,一个raid0就算配置完毕了。其他级别的raid也可以用类似的方法配置,具体可以看帮助。

用mdadm创建raid10

raid0 + raid1的也称raid10,它提供了足够的数据安全和性能,是一般企业最常用的raid级别。
今天我们讨论如何用mdadm创建软raid10.

本次实验的测试环境是: vmware + linux as4

1、首先在vmware添加四个虚拟SCSI硬盘,作为实验所用硬盘
略过。

2、为四个虚拟硬盘分区
略过

3、创建raid
创建raid10的顺序是:先创建2个raid0,再把2个raid0配置成一个raid1.
–创建第一个raid0
[root@primary ~]# mdadm –create /dev/md0 –level=raid0 –chunk=8 –raid-devices=2 /dev/sdb1 /dev/sdc1
mdadm: array /dev/md0 started.
–创建第二个raid0
[root@primary ~]# mdadm –create /dev/md1 –level=raid0 –chunk=8 –raid-devices=2 /dev/sdd1 /dev/sde1
mdadm: array /dev/md1 started.
–利用上面的两个raid0创建raid1
[root@primary ~]# mdadm –create /dev/md2 –level=raid1 –chunk=8 –raid-devices=2 /dev/md0 /dev/md1
mdadm: array /dev/md2 started.

4、格式话raid设备
注意:对于raid设备,只需要格式化最顶层的设备,不管该raid包含了多少层。
在本例中,md2是最顶层的设备,因此我们要把它格式化了就可以了。
[root@primary ~]# mkfs.ext3 /dev/md2
mke2fs 1.35 (28-Feb-2004)
max_blocks 213581824, rsv_groups = 26072, rsv_gdb = 256
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
52208 inodes, 208576 blocks
10428 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
2008 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801

Writing inode tables: done
inode.i_blocks = 3586, i_size = 67383296
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 21 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

5、查看当前raid信息
[root@primary opt]# cat /proc/mdstat
Personalities : [raid0] [raid1]
md2 : active raid1 md1[1] md0[0]
208576 blocks [2/2] [UU]

md1 : active raid0 sde1[1] sdd1[0]
208640 blocks 8k chunks

md0 : active raid0 sdc1[1] sdb1[0]
208640 blocks 8k chunks

unused devices:

6、挂载raid设备
[root@primary ~]# mount /dev/md2 /opt
[root@primary ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 8.4G 5.8G 2.2G 73% /
/dev/sda1 99M 8.4M 86M 9% /boot
none 252M 0 252M 0% /dev/shm
/dev/md2 198M 5.8M 182M 4% /opt

7、配置/etc/mdadm.conf
首先得出raid的信息:
[root@primary opt]# mdadm -Ds
ARRAY /dev/md2 level=raid1 num-devices=2 UUID=97e0cb8d:3613c0eb:6d2b2a87:be1c8030
devices=/dev/md0,/dev/md1
ARRAY /dev/md1 level=raid0 num-devices=2 UUID=634ab4f9:92d40a05:3b6d00ca:d28a2683
devices=/dev/sdd1,/dev/sde1
ARRAY /dev/md0 level=raid0 num-devices=2 UUID=fe4f0d31:32580633:45d6f507:d0b7d41a
devices=/dev/sdb1,/dev/sdc1

然后编辑/etc/mdadm.conf,添加如下内容:
[root@primary opt]# vi /etc/mdadm.conf

ARRAY /dev/md2 level=raid1 num-devices=2 UUID=97e0cb8d:3613c0eb:6d2b2a87:be1c8030
device /dev/md0 /dev/md1
ARRAY /dev/md1 level=raid0 num-devices=2 UUID=634ab4f9:92d40a05:3b6d00ca:d28a2683
device /dev/sdd1 /dev/sde1
ARRAY /dev/md0 level=raid0 num-devices=2 UUID=fe4f0d31:32580633:45d6f507:d0b7d41a
device /dev/sdb1 /dev/sdc1

8、设置开机自动挂载
为了让系统开机后能自动挂载raid设备,需要在/etc/fstab添加如下行:
vi /etc/fstab
/dev/md2 /opt ext3 defaults 0 0

至此,raid10配置完毕。

这里附上mdadm的man内容,以下内容来自 manpage 页:

基本语法 : mdadm [mode] [options]

[mode] 有7种:

Assemble:将以前定义的某个阵列加入当前在用阵列。

Build:Build a legacy array ,每个device 没有 superblocks

Create:创建一个新的阵列,每个device 具有 superblocks

Manage: 管理阵列,比如 add 或 remove

Misc:允许单独对阵列中的某个 device 做操作,比如抹去superblocks 或 终止在用的阵列。

Follow or Monitor:监控 raid 1,4,5,6 和 multipath 的状态

Grow:改变raid 容量或 阵列中的 device 数目

可用的 [options]:

-A, –assemble:加入一个以前定义的阵列

-B, –build:Build a legacy array without superblocks.

-C, –create:创建一个新的阵列

-Q, –query:查看一个device,判断它为一个 md device 或是 一个 md 阵列的一部分

-D, –detail:打印一个或多个 md device 的详细信息

-E, –examine:打印 device 上的 md superblock 的内容

-F, –follow, –monitor:选择 Monitor 模式

-G, –grow:改变在用阵列的大小或形态

-h, –help:帮助信息,用在以上选项后,则显示该选项信息

–help-options

-V, –version

-v, –verbose:显示细节

-b, –brief:较少的细节。用于 –detail 和 –examine 选项

-f, –force

-c, –config= :指定配置文件,缺省为 /etc/mdadm/mdadm.conf

-s, –scan:扫描配置文件或 /proc/mdstat以搜寻丢失的信息。配置文件/etc/mdadm/mdadm.conf

create 或 build 使用的选项:

-c, –chunk=:Specify chunk size of kibibytes. 缺省为 64.

–rounding=: Specify rounding factor for linear array (==chunk size)

-l, –level=:设定 raid level.

–create可用:linear, raid0, 0, stripe, raid1,1, mirror, raid4, 4, raid5, 5, raid6, 6, multipath, mp.

–build可用:linear, raid0, 0, stripe.

-p, –parity=:设定 raid5 的奇偶校验规则:eft-asymmetric, left-symmetric, right-asymmetric, right-symmetric, la, ra, ls, rs.缺省为left-symmetric

–layout=:类似于–parity

-n, –raid-devices=:指定阵列中可用 device 数目,这个数目只能由 –grow 修改

-x, –spare-devices=:指定初始阵列的富余device 数目

-z, –size=:组建RAID1/4/5/6后从每个device获取的空间总数

–assume-clean:目前仅用于 –build 选项

-R, –run:阵列中的某一部分出现在其他阵列或文件系统中时,mdadm会确认该阵列。此选项将不作确认。

-f, –force:通常mdadm不允许只用一个device 创建阵列,而且创建raid5时会使用一个device作为missing drive。此选项正相反。

-a, –auto{=no,yes,md,mdp,part,p}{NN}:



发表评论

电子邮件地址不会被公开。 必填项已用*标注

+ 30 = 40