LVM逻辑卷镜像实现方法

本文演示了在CentOS5系统中实现LVM逻辑卷镜像的方法。LVM的镜像功能,有点儿类似于Raid1,即多块儿磁盘互相同步,确保资料不会丢失。

创建物理卷,卷组的步骤这里就先省略了,网上大把的教程。本文的过程如下:
1,创建4个物理卷/dev/sda1、/dev/sda2、/dev/sda3、/dev/sda4,并且将sda1-sda3三个物理卷加入到卷组vg1中,并在卷组vg1中创建包含镜像功能的逻辑卷lv1;
2,使用dd命令对物理卷sda2进行破坏,然后从卷组中发现sda2挂了,但确认文件读取正常;
3,(在不解除挂载的情况下)去除坏掉的物理卷sda2,向卷组中加入新的物理卷sda4代替之;
4,确认LVM镜像功能正常,以及文件读取正常。

创建一个包含镜像功能的逻辑卷(命名为lv1):

[[email protected] ~]# lvcreate -L 3G -m1 -n lv1 vg1 /dev/sda1 /dev/sda2 /dev/sda3
  Logical volume "lv1" created

[[email protected] ~]# lvs -a -o +devices
  LV             VG   Attr   LSize Origin Snap%  Move Log      Copy%  Convert Devices                        
  lv1            vg1  mwi-a- 3.00G                    lv1_mlog   5.34         lv1_mimage_0(0),lv1_mimage_1(0)
  [lv1_mimage_0] vg1  Iwi-ao 3.00G                                            /dev/sda3(0)                   
  [lv1_mimage_1] vg1  Iwi-ao 3.00G                                            /dev/sda2(0)                   
  [lv1_mlog]     vg1  lwi-ao 4.00M                                            /dev/sda1(0)

LVM镜像需要用到-m1参数,从上面可以看出,/dev/sda2和/dev/sda3互为镜像,而/dev/sda1作为日志存储之用。

在逻辑卷上创建一个文件:

[[email protected] ~]# mkfs.ext3 /dev/vg1/lv1
……
[[email protected] ~]# mount /dev/vg1/lv1 /mnt/1
[[email protected] ~]# echo "hello world" > /mnt/1/hi.txt
[[email protected] ~]# cat /mnt/1/hi.txt
hello world

对/dev/sda2进行破坏:

[[email protected] ~]# dd if=/dev/zero of=/dev/sda2 count=10 bs=1M
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.0133215 seconds, 787 MB/s

确认/dev/sda2遭到了破坏:

[[email protected] ~]# lvs -a -o +devices
  Couldn't find device with uuid 'ML2wV3-wBIr-eOG3-bXYp-SH1B-WBYq-JWn5K4'.
  Couldn't find device with uuid 'ML2wV3-wBIr-eOG3-bXYp-SH1B-WBYq-JWn5K4'.
  Couldn't find device with uuid 'ML2wV3-wBIr-eOG3-bXYp-SH1B-WBYq-JWn5K4'.
  LV             VG   Attr   LSize Origin Snap%  Move Log      Copy%  Convert Devices                        
  lv1            vg1  mwi-ao 3.00G                    lv1_mlog 100.00         lv1_mimage_0(0),lv1_mimage_1(0)
  [lv1_mimage_0] vg1  iwi-ao 3.00G                                            /dev/sda3(0)                   
  [lv1_mimage_1] vg1  iwi-ao 3.00G                                            unknown device(0)              
  [lv1_mlog]     vg1  lwi-ao 4.00M                                            /dev/sda1(0)                   

[[email protected] ~]# lvscan
  Couldn't find device with uuid 'ML2wV3-wBIr-eOG3-bXYp-SH1B-WBYq-JWn5K4'.
  Couldn't find device with uuid 'ML2wV3-wBIr-eOG3-bXYp-SH1B-WBYq-JWn5K4'.
  Couldn't find device with uuid 'ML2wV3-wBIr-eOG3-bXYp-SH1B-WBYq-JWn5K4'.
  ACTIVE            '/dev/vg1/lv1' [3.00 GB] inherit

重新挂载逻辑卷,确认文件可以正常读取:

[[email protected] ~]# umount /mnt/1
[[email protected] ~]# mount /dev/vg1/lv1 /mnt/1
[[email protected] ~]# cat /mnt/1/hi.txt 
hello world

将卷组中坏掉的物理卷(/dev/sda2)移除:

[[email protected] ~]# vgreduce --removemissing --force vg1

移除之后发现卷组信息中Mirrored volumes选项消失了:

[[email protected] ~]# vgdisplay /dev/vg1
……

向卷组中加入新的物理卷(/dev/sda4):

[[email protected] ~]# vgextend vg1 /dev/sda4

进行数据恢复(过程中无须解除逻辑卷的挂载):

[[email protected] ~]# lvconvert -m1 /dev/vg1/lv1 /dev/sda1 /dev/sda3 /dev/sda4
  /dev/vg1/lv1: Converted: 9.8%
  /dev/vg1/lv1: Converted: 19.4%
  /dev/vg1/lv1: Converted: 48.4%
  /dev/vg1/lv1: Converted: 77.6%
  /dev/vg1/lv1: Converted: 96.9%
  /dev/vg1/lv1: Converted: 100.0%
  Logical volume lv1 converted.

[[email protected] ~]# lvs -a -o +devices
  LV             VG   Attr   LSize Origin Snap%  Move Log      Copy%  Convert Devices                        
  lv1            vg1  mwi-ao 3.00G                    lv1_mlog 100.00         lv1_mimage_0(0),lv1_mimage_1(0)
  [lv1_mimage_0] vg1  iwi-ao 3.00G                                            /dev/sda3(0)                   
  [lv1_mimage_1] vg1  iwi-ao 3.00G                                            /dev/sda4(0)                   
  [lv1_mlog]     vg1  lwi-ao 4.00M                                            /dev/sda1(0)                   

[[email protected] ~]# cat /mnt/1/hi.txt 
hello world

发表评论

您的电子邮箱地址不会被公开。