lvm快照的创建恢复

首先,不谈理论知识,直接看过程和结果

lvs                   <--查看lv
  LV   VG   Attr       LSize  Pool Origin Data%  Move Log Cpy%Sync Convert
  vm1  vg   -wi-ao---- 50.47g                                             
  vm2  vg   -wi-a----- 30.00g                                             
  vm3  vg   -wi-ao---- 30.00g                                             
  vm4  vg   -wi-a----- 30.00g                                             
  vm5  vg   -wi-a----- 30.00g

=============================================================

df -h                <--查看挂载情况
Filesystem          Size  Used Avail Use% Mounted on
/dev/sda2            58G   40G   16G  72% /
tmpfs               3.9G  236K  3.9G   1% /dev/shm
/dev/sda1           194M   59M  126M  32% /boot
/dev/mapper/vg-vm3   30G   29G   16M 100% /vm/vm3
/dev/mapper/vg-vm1   50G  1.5G   46G   4% /vm/vm1  <--实验目标

=============================================================

ls -lh /vm/vm1        <--查看lv内容
总用量 1.3G
-rwxr-xr-x 1 qemu qemu 100G 5月   7 18:17 os
-rw-r--r-- 1 root root 2.2K 5月   7 16:30 vm1.xml

virsh start vm1        <--启动vm(kvm),os为虚拟机的镜像img

ssh 192.168.122.100    <--远程SSH连接虚拟机(IP手动配置好)

The authenticity of host '192.168.122.100 (192.168.122.100)' can't be established.RSA key fingerprint is 18:49:fa:bc:6b:01:27:25:c5:66:f4:f8:d6:1c:b9:3c.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.122.100' (RSA) to the list of known hosts.

[email protected]'s password:

Last login: Thu May  7 18:31:11 2015

[root@localhost ~]# df -h 文件系统          容量  已用  可用 已用%% 挂载点

/dev/mapper/VolGroup-lv_root                 48G  728M   45G   2% /

tmpfs                                       246M     0  246M   0% /dev/shm

/dev/sda1                                   485M   32M  429M   7% /boot

/dev/mapper/VolGroup-lv_home                 50G  180M   47G   1% /home

=============================================================

此时回到物理机

lvcreate -n vm1_snap -s /dev/mapper/vg-vm1 -L 5G   <--创建快照大小5G 

Logical volume "vm1_snap" created

[root@eric ~]#

[root@eric ~]#

[root@eric ~]# lvs 

LV       VG   Attr       LSize  Pool Origin Data%  Move Log Cpy%Sync Convert 

vm1      vg   owi-aos--- 50.47g   

vm1_snap vg   swi-a-s---  5.00g      vm1      0.00                         

vm2      vg   -wi-a----- 30.00g                                             

vm3      vg   -wi-ao---- 30.00g                                             

vm4      vg   -wi-a----- 30.00g                                             

vm5      vg   -wi-a----- 30.00g

=============================================================

现在可以开搞了

cd /vm/vm1 rm * rm:是否删除普通文件 "os"?y

rm:是否删除普通文件 "vm1.xml"?y

ls -l总用量 0

=============================================================

回到虚拟机终端    

[root@localhost ~]# ls          <--哎哟,还能动

anaconda-ks.cfg  install.log  install.log.syslog

[root@localhost ~]# shutdown -h now

=============================================================

回到物理机再次启动

virsh start vm1

错误:开始域 vm1 失败

错误:cannot open file '/vm/vm1/os': No such file or directory

=============================================================

还原        <--恢复快照时,目标逻辑卷不能被挂载使用

umount /vm/vm1 lvconvert --merge /dev/vg/vm1_snap 

Merging of volume vm1_snap started. 

vm1: Merged: 90.0% 

vm1: Merged: 100.0%  Merge of snapshot into logical volume vm1 has finished.  Logical volume "vm1_snap" successfully removed

这是正常情况下显示

lvs
  LV   VG   Attr       LSize  Pool Origin Data%  Move Log Cpy%Sync Convert
  vm1  vg   -wi-a----- 50.47g                                             
  vm2  vg   -wi-a----- 30.00g                                             
  vm3  vg   -wi-a----- 30.00g                                             
  vm4  vg   -wi-a----- 30.00g                                             
  vm5  vg   -wi-a----- 30.00g

还原时同时删除快照

=============================================================

验证

mount /dev/vg/vm1 /vm/vm1

ls /vm/vm1

os  vm1.xml        <--os出来了。

virsh start vm1
域 vm1 已开始        <--好像正常启动的样子

ssh 192.168.122.100
[email protected]'s password:
Last login: Fri May  8 10:33:28 2015
[root@localhost ~]# df -h
文件系统          容量  已用  可用 已用%% 挂载点
/dev/mapper/VolGroup-lv_root
                       48G  728M   45G   2% /
tmpfs                 246M     0  246M   0% /dev/shm
/dev/sda1             485M   32M  429M   7% /boot
/dev/mapper/VolGroup-lv_home
                       50G  180M   47G   1% /home

呵呵,真的恢复了。

=============================================================

恢复失败的例子

lvcreate -n vm1_snap -s /dev/vg/vm1  -L 5G
  Logical volume "vm1_snap" created
lvs
  LV       VG   Attr       LSize  Pool Origin Data%  Move Log Cpy%Sync Convert
  vm1      vg   owi-aos--- 50.47g                                             
  vm1_snap vg   swi-a-s---  5.00g      vm1      0.00                          
  vm2      vg   -wi-a----- 30.00g                                             
  vm3      vg   -wi-a----- 30.00g                                             
  vm4      vg   -wi-a----- 30.00g                                             
  vm5      vg   -wi-a----- 30.00g

rm -f /vm/vm1/os    <--删除img

lvconvert --merge /dev/vg/vm1_snap         <--虚拟机没关机,系统没卸载lv,直接恢复 

Can't merge over open origin volume
  Merging of snapshot vm1_snap will start next activation.

lvconvert --merge /dev/vg/vm1_snap
  Snapshot vm1_snap is already merging
  Unable to merge LV "vm1_snap" into its origin.

lvs            
  LV   VG   Attr       LSize  Pool Origin Data%  Move Log Cpy%Sync Convert
  vm1  vg   Owi-aos--- 50.47g                                             
  vm2  vg   -wi-a----- 30.00g                                             
  vm3  vg   -wi-a----- 30.00g                                             
  vm4  vg   -wi-a----- 30.00g                                             
  vm5  vg   -wi-a----- 30.00g

我去,快照没了!

virsh destroy vm1        <--关闭虚拟机

virsh start vm1

错误:开始域 vm1 失败 错误:cannot open file '/vm/vm1/os': No such file or directory

感觉完蛋了 ....

=============================================================

再来

lvconvert --merge /dev/vg/vm1_snap
  Snapshot vm1_snap is already merging
  Unable to merge LV "vm1_snap" into its origin.

呵呵哒...

回头看之前执行后的描述,不能打开原始逻辑卷,恢复快照会在下次激活时执行

脑补了一下,尝试把lv停用(inactive)

umount /vm/vm1

lvchange -an /dev/vg/vm1        <--停用

lvdisplay /dev/vg/vm1       
  --- Logical volume ---
  LV Path                /dev/vg/vm1
  LV Name                vm1
  VG Name                vg
  LV UUID                ki7kEy-lxeo-We6Q-Jkgb-H0Ox-Nbsh-2Tytkq
  LV Write Access        read/write
  LV Creation host, time eric, 2014-07-24 22:24:47 +0800
  LV snapshot status     source of
                         vm1_snap [INACTIVE]
  LV Status              NOT available
  LV Size                50.47 GiB
  Current LE             12921
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto

lvchange -ay /dev/vg/vm1        <--启用

lvs

LV   VG   Attr       LSize  Pool Origin Data%  Move Log Cpy%Sync Convert
  vm1  vg   -wi-a----- 50.47g                                             
  vm2  vg   -wi-a----- 30.00g                                             
  vm3  vg   -wi-a----- 30.00g                                             
  vm4  vg   -wi-a----- 30.00g                                             
  vm5  vg   -wi-a----- 30.00g

看到木有,状态都变正常了。

[root@eric ~]# mount /dev/vg/vm1 /vm/vm1
[root@eric ~]# ls /vm/vm1
os  vm1.xml        <--有了

virsh start vm1
域 vm1 已开始

ssh 192.168.122.100
[email protected]'s password:
Last login: Fri May  8 11:45:03 2015

[root@localhost ~]# df -h
文件系统          容量  已用  可用 已用%% 挂载点
/dev/mapper/VolGroup-lv_root
                       48G  728M   45G   2% /
tmpfs                 246M     0  246M   0% /dev/shm
/dev/sda1             485M   32M  429M   7% /boot
/dev/mapper/VolGroup-lv_home
                       50G  180M   47G   1% /home

=============================================================

实验完毕

你可能感兴趣的:(快照,lvm)