测试环境:CentOS 5.10 x64,sda 20G,sdb 1G,sdc 2G.
一、创建逻辑卷镜像关系的步骤:
1)通过pvcreate命令将linux分区处理成物理卷(PV);
2)通过vgcreate命令将创建好的物理卷处理成卷组(Vg);
3)通过lvcreate命令使用–m1 参数将卷若干个PV组成一个逻辑镜像卷(Lv);
之后我们可以对镜像逻辑卷进行格式化,挂载,删除等操作。
二、创建磁盘分区
首先将sbc和sdc分区,并创建三个pv(sdb1、sdc1、sdc2),其中sdb1作为数据盘,sdc1作为镜像盘,sdc2作为日志盘,日志盘不应太小(一般10G左右)。
1)将sdb分区
[root@Centos~]# fdisk /dev/sdb
Devicecontains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Buildinga new DOS disklabel. Changes will remain in memory only,
until youdecide to write them. After that, of course, the previous
contentwon't be recoverable.
Warning:invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command(m for help): n
Commandaction
e extended
p primary partition (1-4)
p
Partitionnumber (1-4): 1
Firstcylinder (1-130, default 1):
Usingdefault value 1
Lastcylinder or +size or +sizeM or +sizeK (1-130, default 130): +1024M
Command(m for help): p
Disk/dev/sdb: 1073 MB, 1073741824 bytes
255heads, 63 sectors/track, 130 cylinders
Units =cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 125 1004031 83 Linux
Command(m for help): w
Thepartition table has been altered!
Callingioctl() to re-read partition table.
Syncingdisks.
[root@Centos ~]#
2)将sdc分区
[root@Centos ~]#fdisl /dev/sdc
bash: fdisl:command not found
[root@Centos ~]#fdisk /dev/sdc
Device containsneither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a newDOS disklabel. Changes will remain in memory only,
until you decideto write them. After that, of course, the previous
content won't berecoverable.
Warning: invalidflag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m forhelp): n
Command action
e extended
p primary partition (1-4)
p
Partition number(1-4): 1
First cylinder(1-261, default 1): 1
Last cylinder or+size or +sizeM or +sizeK (1-261, default 261): +1024M
Command (m forhelp): n
Command action
e extended
p primary partition (1-4)
p
Partition number(1-4): 2
First cylinder(126-261, default 126):
Using defaultvalue 126
Last cylinder or+size or +sizeM or +sizeK (126-261, default 261): +1000M
Command (m forhelp): p
Disk /dev/sdc:2147 MB, 2147483648 bytes
255 heads, 63sectors/track, 261 cylinders
Units =cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdc1 1 125 1004031 83 Linux
/dev/sdc2 126 248 987997+ 83 Linux
Command (m forhelp): w
The partitiontable has been altered!
Calling ioctl()to re-read partition table.
Syncing disks.
3)重读分区表
[root@centos ~]#partprobe
如果sdb、sdc是FC、iscsi、DAS存储,且配置了多路径,则使用如下命令更新分区表,若还未更新就只能init 6 重启服务器了:
kpartx –a/dev/mapper/mpathbp1 然后 ls/dev/mapper/ 查看分区设备
4)pvcreate 创建pv 设备
[root@Centos ~]#pvcreate /dev/sdb1 /dev/sdc1 /dev/sdc2
Writing physical volume data to disk"/dev/sdb1"
Physical volume "/dev/sdb1"successfully created
Writing physical volume data to disk"/dev/sdc1"
Physical volume "/dev/sdc1"successfully created
Writing physical volume data to disk"/dev/sdc2"
Physical volume "/dev/sdc2"successfully created
[root@Centos ~]#
查看pv设备表:
[root@centos ~]#pvscan (或者pvs,或者 pvdisplay查看详细信息包括pvuuid)
PV /dev/sda2 VG VolGroup00 lvm2 [19.88 GB/ 0 free]
PV /dev/sdb1 lvm2 [980.50 MB]
PV /dev/sdc1 lvm2 [980.50 MB]
PV /dev/sdc2 lvm2 [964.84 MB]
Total: 4 [22.73 GB] / in use: 1 [19.88 GB] /in no VG: 3 [2.86 GB]
[root@Centos ~]#
5)vgcreate创建 vg0物理卷组
[root@Centos ~]#vgcreate vg0 /dev/sdb1 /dev/sdc1 /dev/sdc2
Volume group "vg0" successfullycreated
[root@Centos ~]#
三、创建LVM Mirror镜像卷
[root@Centos ~]# lvcreate -L 970M -m1 -n mirror vg0 /dev/sdb1/dev/sdc1
Rounding up size to full physical extent972.00 MB
Logical volume "mirror" created
[root@Centos ~]#lvdisplay /dev/vg0/mirror 查看/dev/vg0/mirror
--- Logical volume ---
LV Name /dev/vg0/mirror
VG Name vg0
LV UUID vX60P8-MmjC-tNX8-w2jI-t2qt-AZnD-Rnj0IN
LV Write Access read/write
LV Status available
# open 0
LV Size 972.00 MB
Current LE 243
Mirrored volumes 2
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:5
[root@Centos ~]#lvs -a -o +devices 查看当前LVM中设备状态
[root@Centos ~]# mkfs.ext3 /dev/vg0/mirror 格式化lvm,创建文件系统
四、管理lvm mirror卷
1)破坏一个pv设备(sdb1),然后再查看lvs状态,发现少了一个pv设备sdb1:
[root@Centos ~]#dd if=/dev/zero of=/dev/sdb1 count=10
10+0 records in
10+0 records out
5120 bytes (5.1 kB) copied, 0.000856 seconds, 6.0 MB/s
2)把损坏的pv从vg0卷组删除(缩小vg0卷组):
[root@Centos ~]#vgreduce --removemissing --force vg0
Couldn't find device with uuidkJc1oG-Ua0R-RDiC-KaJ5-oomD-cH9Q-2DbWQ2.
Wrote out consistent volume group vg0
[root@Centos ~]#
因为上面我们是手动破坏pv,如果是pv离线或者减少vg的pv设备,使用如下指令:
[root@Centos ~]#vgreduce vg0 /dev/sdb1
移除之后发现vg0详细信息中没有了Mirrored volumes选项
[root@Centos ~]#vgdisplay /dev/vg0
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 9
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 1.91 GB
PE Size 4.00 MB
Total PE 490
Alloc PE / Size 243 / 972.00 MB
Free PE / Size 247 / 988.00 MB
VG UUID q620jv-Oc9a-pUlg-q6wM-TFXl-wAa7-jh40Fs
[root@Centos ~]#
3)扩展vg0卷组:
[root@Centos ~]#vgextend vg0 /dev/sdb1 扩展vg0卷组
Writing physical volume data to disk"/dev/sdb1"
Physical volume "/dev/sdb1"successfully created
Volume group "vg0" successfullyextended
[root@Centos ~]#pvs 查看pv状态
PV VG Fmt Attr PSize PFree
/dev/sda2 VolGroup00 lvm2 a-- 19.88G 0
/dev/sdb1 vg0 lvm2 a-- 980.00M 980.00M
/dev/sdc1 vg0 lvm2 a-- 980.00M 8.00M
/dev/sdc2 vg0 lvm2 a-- 980.00M 980.00M
4)镜像卷组数据恢复:
[root@Centos ~]#lvconvert -m1 /dev/vg0/mirror /dev/sdb1 /dev/sdc1 /dev/sdc2
vg0/mirror: Converted: 0.0%
vg0/mirror: Converted: 43.6%
vg0/mirror: Converted: 83.5%
vg0/mirror: Converted: 100.0%
[root@Centos ~]#lvs-a -o +devices 查看mirror卷组状态,会看到三个pv
[root@Centos ~]#lvdisplay /dev/vg0/mirror 查看lvm mirror详细信息,Mirrored
volumes再次出现
--- Logical volume ---
LV Name /dev/vg0/mirror
VG Name vg0
LV UUID vX60P8-MmjC-tNX8-w2jI-t2qt-AZnD-Rnj0IN
LV Write Access read/write
LV Status available
# open 0
LV Size 972.00 MB
Current LE 243
Mirrored volumes 2
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:5
[root@Centos ~]#