上次有台机器用来做了samba服务器,感觉软件很重要所以想了想在找了一块80g的硬盘做了raid1镜像,花费了一天的时间一边实验一边总结,自己做个笔记以免以后忘记。
1.[root@zh888 ~]# rpm -q mdadm//先查询是否有mdadm的软件包。
mdadm-2.6.9-3.el5//说明有了,如果没有请用yum -y install来安装即可。
2.[root@zh888 ~]# fdisk -l//查看分区
Disk /dev/hda: 40.0 GB, 40020664320 bytes
255 heads, 63 sectors/track, 4865 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/hda1 * 1 13 104391 83 Linux
/dev/hda2 14 144 1052257+ 82 Linux swap / Solaris
/dev/hda3 145 4865 37921432+ 83 Linux
Disk /dev/hdb: 80.0 GB, 80026361856 bytes
16 heads, 63 sectors/track, 155061 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Disk /dev/hdb doesn't contain a valid partition table
Disk /dev/hdd: 81.9 GB, 81964302336 bytes
16 heads, 63 sectors/track, 158816 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Disk /dev/hdd doesn't contain a valid partition table
[root@zh888 ~]# fdisk /dev/hdb//分区为fd类型/dev/hdd也是同样的操作。
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.
The number of cylinders for this disk is set to 155061.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help): n//new新分区
Command action
e extended
p primary partition (1-4)
p//主分区
Partition number (1-4): 1//第一个分区
First cylinder (1-155061, default 1): 回车
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-155061, default 155061): 回车
Using default value 155061
Command (m for help): t//type类型
Selected partition 1
Hex code (type L to list codes): fd//fd类型
Changed system type of partition 1 to fd (Linux raid autodetect)
Command (m for help): w//保存写入
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
3.[root@zh888 ~]# fdisk -l//在次查看硬盘类型
Disk /dev/hda: 40.0 GB, 40020664320 bytes
255 heads, 63 sectors/track, 4865 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/hda1 * 1 13 104391 83 Linux
/dev/hda2 14 144 1052257+ 82 Linux swap / Solaris
/dev/hda3 145 4865 37921432+ 83 Linux
Disk /dev/hdb: 80.0 GB, 80026361856 bytes
16 heads, 63 sectors/track, 155061 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Device Boot Start End Blocks Id System
/dev/hdb1 1 155061 78150712+ fd Linux raid autodetect//已经为linux raid了
Disk /dev/hdd: 81.9 GB, 81964302336 bytes
16 heads, 63 sectors/track, 158816 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Device Boot Start End Blocks Id System
/dev/hdd1 1 158816 80043232+ fd Linux raid autodetect
4.[root@zh888 ~]#mdadm --create --verbose /dev/md0 --level=1 --raid-devices=2 /dev/hdb1 /dev/hdd1 //创建raid1命令可以用简写如下:
mdadm: array /dev/md0 started.
//[root@zh888 ~]#mdadm -C /dev/md0 -l1 -n2 -c128 /dev/hd[bd]也可以用这种方法创建。
mdadm: array /dev/md0 started.
5.[root@zh888 ~]# echo DEVICE /dev/hd[bd]>/etc/mdadm.conf
或cp /usr/share/doc/mdadm-2.5.4/mdadm.conf-example /etc/mdadm.conf
或echo DEVICE /dev/hd[bd]1 >>/etc/mdadm.conf
6.[root@zh888 ~]# mdadm --detail --scan>>/etc/mdadm.conf
或mdadm -Ds >>/etc/mdadm.conf
7.[root@zh888 ~]# cat /etc/mdadm.conf//查看mdadm.conf文件如下:
DEVICE /dev/hdb /dev/hdd
ARRAY /dev/md0 level=raid1 num-devices=2 metadata=0.90 UUID=57319c2a:090717a6:92042ec3:e00720c4
8.[root@zh888 ~]# mkfs.ext3 /dev/md0//格式化raid1.
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
9781248 inodes, 19537664 blocks
976883 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
597 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424
Writing inode tables: done
Creating journal (32768 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.
9.[root@zh888 ~]# mount /dev/md0 /home/samba/software/ //挂载/dev/md0设备到/home/samba/software目录中。
[root@zh888 ~]# mount//查看挂载的目录。
/dev/hda3 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/hda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/md0 on /home/samba/software type ext3 (rw)//已经挂载上/dev/md0
10.[root@zh888 ~]# cat /proc/mdstat //查看mdstat状态。
Personalities : [raid1]
md0 : active raid1 hdd[1] hdb[0]
78150656 blocks [2/2] [UU]
[======>..............] resync = 30.0% (23462656/78150656) finish=31.6min speed=28792K/sec
unused devices: <none>//正在同步设备。
11.[root@zh888 ~]# mdadm --detail /dev/md0//查看状态可以是-D参数
/dev/md0:
Version : 0.90
Creation Time : Thu Sep 1 18:01:12 2011
Raid Level : raid1
Array Size : 740263618 (74.53 GiB 74.53 GB)
Raid Devices : 2
Total Devices : 2
Preferred Minor : 0
Persistence : Superblock is persistent
Update Time : Thu Sep 1 18:01:12 2011
State : clean
Active Devices : 2
Working Devices : 2
Failed Devices : 0
Spare Devices : 0
Chunk Size : 128K
UUID : a55d24d1:f8be2ab8:e606decc:41c96122
Events : 0.1
Number Major Minor RaidDevice State
0 3 64 0 active sync /dev/hdb
1 22 64 1 active sync /dev/hdd
11.创建LVM
lvm就是逻辑卷管理器
步骤分为:
1 创建pv(物理卷)
2 创建vg(卷组)
3 创建lv(逻辑卷)
好处,为文件系统提供一个透明的磁盘接口,利于扩容之类的。
12.[root@zh888 ~]# pvcreate /dev/md0//创建pv物理卷
Physical volume "/dev/md0" successfully created
13.[root@zh888 ~]# vgcreate datavg /dev/md0//常见datavg
Volume group "datavg" successfully created
14.[root@zh888 ~]# vgdisplay -v//显示datavg信息
Finding all volume groups
Finding volume group "datavg"
--- Volume group ---
VG Name datavg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 74.53 GB
PE Size 4.00 MB
Total PE 19079
Alloc PE / Size 0 / 0
Free PE / Size 19079 / 74.53 GB
VG UUID IfweOP-2jnU-4VME-25Dp-Uy2x-Bbfn-IDbjvH
--- Physical volumes ---
PV Name /dev/md0
PV UUID 2kshX3-T60n-DWwV-DjTF-DxLI-mrzz-Z6He7k
PV Status allocatable
Total PE / Free PE 19079 / 19079
从上面显示的卷信息得知,datavg卷只有74.53 GB可用。
15.[root@zh888 ~]# lvcreate datavg --name=datalv --size=74G
Logical volume "datalv" created
16.[root@zh888 ~]# vgdisplay -v
Finding all volume groups
Finding volume group "datavg"
--- Volume group ---
VG Name datavg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 74.53 GB
PE Size 4.00 MB
Total PE 19079
Alloc PE / Size 18944 / 74.00 GB
Free PE / Size 135 / 540.00 MB
VG UUID IfweOP-2jnU-4VME-25Dp-Uy2x-Bbfn-IDbjvH
--- Logical volume ---
LV Name /dev/datavg/datalv
VG Name datavg
LV UUID Ndzr97-yxOn-HP4P-CNZg-1jpG-RaS2-4EBgkh
LV Write Access read/write
LV Status available
# open 0
LV Size 74.00 GB
Current LE 18944
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
--- Physical volumes ---
PV Name /dev/md0
PV UUID 2kshX3-T60n-DWwV-DjTF-DxLI-mrzz-Z6He7k
PV Status allocatable
Total PE / Free PE 19079 / 135
17.[root@zh888 ~]# mkfs -t ext3 /dev/datavg/datalv//格式化datalv逻辑卷
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
9699328 inodes, 19398656 blocks
969932 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
592 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
18. [root@zh888 ~]# lvdisplay 查看lv逻辑卷然后 /dev/datavg/datalv /home/samba/software ext3 default 0 0 写入/etc/fstab中让系统开机启动挂载。
--- Logical volume ---
LV Name /dev/datavg/datalv
VG Name datavg
LV UUID Ndzr97-yxOn-HP4P-CNZg-1jpG-RaS2-4EBgkh
LV Write Access read/write
LV Status available
# open 0
LV Size 74.00 GB
Current LE 18944
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
raid1和raid0测试速度。
19.[root@zh888 software]# dd if=/dev/zero of=file bs=1024k count=1k//测试raid0的速度
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 15.3735 seconds, 69.8 MB/s
写了1.1G的数据花费了15.3秒,
20.[root@zh888 software]# dd if=/dev/zero of=file bs=1024k count=1k//raid1的速度
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 29.8514 seconds, 36.0 MB/s
有一些特殊的设备文件。例如/dev/zero文件代表一个永远输出 0的设备文件,使用它作输入可以得到全为空的文件。因此可用来创建新文件和以覆盖的方式清除旧文件。下面使用dd命令将从zero设备中创建一个10K大小(bs决定每次读写1024字节,count定义读写次数为10次),但内容全为0的文件。
# dd if=/dev/zero of=file count=10 bs=1024
10+0 records in
10+0 records out
10240 bytes transferred in 0.001408 secs (7267903 b ytes/sec)
可以通过使用dd if=/dev/zero of=/file 来测试磁盘的纯写入性能
使用dd if=/file of=/dev/null 来测试磁盘的纯读取性能
使用dd if=/file1 of=/file2 来测试磁盘的读写性能
21.补充点mdadm的知识点
dadm有7中模式,下面列出了7种模式的命令格式,详细的选项,请参考man手册。
ASSEMBLE MODE :madam --assemble md-device options-and-component-devices
mdadm --assembel --scan md-devices-and-options
mdamd --assembel --scan options
BUILD MODE: mdadm --build device --chunk=X --level=Y --raid-devices=Z devices
CREATE MODE: mdadm --create device --chunk=X --level=Y --raid-devices=Z devices
MANAGE MODE: mdadm device options devices
MISC MODE: mdadm options ... devices ...
MONITOR MODE: mdadm --monitor options... devices...
GROW MODE:
1.查看
MISC模式
#mdadm --detail /dev/md0
#mdadm -D /dev/md0
2.停止
MISC模式
#mdadm -S /dev/md0
3.启动
ASSEMBLE模式
#mdadm -A /dev/md0 /dev/sd[bcd]1
启动指定的RAID,可以理解为将一个raid重新装配到系统中。
如果在前面已经配置了/etc/mdadm.conf文件,可以使用:
#mdadm -As /dev/md0
4.添加删除磁盘
mdadm可以在Manage模式下,对运行中的阵列进行添加及删除磁盘。常用于标识failed磁盘,增加spare(冗余)磁盘,以及替换磁盘等。
# mdadm /dev/md0 --fail /dev/sdd --remove /dev/sdd
# mdadm -D /dev/md0
# mdadm /dev/md0 --add /dev/sdd
# mdadm -D /dev/md0
--fail指定坏磁盘,--remove移走。
需要注意的是,对于某些RAID级别,如RAID0,是不能用--fail --remove --add的。
5.监控
MONITOR模式
# nohup mdadm --monitor --mail root --delay 200 /dev/md0 &
每200秒监控一次,当RAID出现错误时,发送邮件给root用户。
6.增加spare磁盘
可以通过在创建的时候指定冗余磁盘
#mdadm --create --verbose /dev/md0 --level=3 --raid-devices=3 -x1 /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
-x(--spare-devices=)参数指定冗余磁盘的数量。另外,对于full的阵列(例如已经有2个磁盘的RAID1),则直接使用-add参数,mdadm会自动把冗余的磁盘作为spare disk。
7.删除RAID
#mdadm -S /dev/md0
或
#rm /dev/md0
删除/etc/mdadm.conf文件;去除/etc/fstab文件中相关的行。
最后,用fdisk对磁盘进行重新分区。
本文转自zh888 51CTO博客,原文链接:http://blog.51cto.com/zh888/656292,如需转载请自行联系原作者