虚拟机磁盘快照
介绍
- vhd格式的虚拟机磁盘可以进行快照操作。
- 快照就是对磁盘的当前状态进行快速的记录,当对虚拟机进行一系列读写操作之后,可以通过快照恢复到最初的状态。
- vhd-util是xen-tools提供的一系列辅助工具,其中具有磁盘快照的功能。
实验环境
虚拟化方式:xen(suse)
Guest OS:centos6.5
硬盘文件:centos.vhd (50G)
配置文件:centos.xml
<domain type='xen'>
<name>centos</name>
<description>None</description>
<uuid>360a92a1-f5d8-63d9-ab93-bbfa76e4c597</uuid>
<memory>2097152</memory>
<currentMemory>2097152</currentMemory>
<vcpu>1</vcpu>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<clock sync='utc'/>
<keymap>en-us</keymap>
<os>
<type>hvm</type>
<loader>/usr/lib/xen/boot/hvmloader</loader>
<boot dev='hd'/>
</os>
<features>
<apic/>
<acpi/>
<pae/>
</features>
<devices>
<emulator>/usr/lib/xen/bin/qemu-dm</emulator>
<disk type='file' device='disk'>
<driver name="tap2" type="vhd"/>
<source file='/cvm/test/centos.vhd'/>
<target bus="ide" dev="hda"/>
</disk>
<disk type='file' device='cdrom'>
<driver name='file'/>
<source file=''/>
<target dev='hdc'/>
<readonly/>
</disk>
<interface type='bridge'>
<model type='e1000'/>
<source bridge='biz'/>
<mac address='00:16:3e:0d:09:f6'/>
<script path='/etc/xen/scripts/vif-bridge'/>
</interface>
<graphics type="vnc" port="-1" listen="0.0.0.0" keymap="en-us"/>
</devices>
</domain>
注1:可以用virt-manager来创建一个虚拟机
注2:可以用qemu-img create -f vpc centos.vhd 50G
创建磁盘
快照方法
关闭虚拟机,创建两个快照
vhd-util snapshot -n centosSnap1.vhd -p centos.vhd
vhd-util snapshot -n centosSnap2.vhd -p centos.vhd
查看快照
vhd-util scan -m *.vhd -p
执行结果
vhd=centos.vhd capacity=53687091200 size=53687091712 hidden=0 parent=none
vhd=centosSnap1.vhd capacity=53687091200 size=77853184 hidden=0 parent=centos.vhd
vhd=centosSnap2.vhd capacity=53687091200 size=82055680 hidden=0 parent=centos.vhd
启动虚拟机
修改配置文件centos.xml 的磁盘路径为第二个快照
<disk type='file' device='disk'>
<driver name="tap2" type="vhd"/>
<source file='/cvm/test/centosSnap2.vhd'/>
<target bus="ide" dev="hda"/>
</disk>
开启虚拟机
virsh create centos.xml
在虚拟机内创建一些文件,例如
for i in {1..300}
do
echo $i >> $i
done
之后对虚拟机所有的修改,都会保存在centosSnap2.vhd这个快照内。
恢复快照
关闭虚拟机 virsh shutdown centos
修改配置文件centos.xml 的磁盘路径为第一个快照
<disk type='file' device='disk'>
<driver name="tap2" type="vhd"/>
<source file='/cvm/test/centosSnap1.vhd'/>
<target bus="ide" dev="hda"/>
</disk>
启动虚拟机virsh create centos.xml
查看刚才创建的文件都不存在了,又恢复了最初的样子。