标签:编辑 put vpd force ide his boot rest 自动
快照应该是所有虚拟化技术都不可或缺的一个部分吧,无论是VMware、Citrix还是开源的如KVM、Xen等,都提供了快照技术。快照技术提供了虚拟机某一时间点的映像,方便在其他时间我们可以回溯到该时间点,也给数据备份和恢复提供了便利。下面说说KVM虚拟化的快照功能:
1.快照实际上做的是虚拟机的XML配置文件,默认快照XML文件在/var/lib/libvirt/qemu/snapshot/[vmachine_name]/下
2.快照只支持qcow2磁盘格式的系统
KVM虚拟化快照的实现方式:
kvm快照有两种方式创建,分别是:
virsh snapshot 相关命令进行生成快照
qemu-img snapshot 命令进行生成快照
使用virsh进行KVM虚拟机快照相关命令:
iface-begin 创建一个当前网卡的快照,它可以用来稍后提交或者恢复
snapshot-create 创建一个快照
snapshot-create-as 使用一组参数创建一个快照
snapshot-current 取得当前快照的集合
snapshot-delete 删除一个快照
snapshot-dumpxml 从一个虚拟机的快照导出xml文件
snapshot-edit 编辑一个快照的xml文件
snapshot-info 快照信息
snapshot-list 列出虚拟机的快照列表
snapshot-parent 获取一个快照的父快照名称
snapshot-revert 将虚拟机恢复到一个快照
使用virsh管理虚拟机快照:
kvm虚拟机默认使用raw格式的镜像格式,性能最好,速度最快,它的缺点就是不支持一些新的功能,如支持镜像,zlib磁盘压缩,AES加密等。要使用镜像功能,磁盘格式必须为qcow2。
1、磁盘格式转换
查看磁盘映像格式:
[root@kvm-node1 ~]# qemu-img info /images/kvm/centos6.img image: /images/kvm/centos6.img file format: raw virtual size: 10G (10737418240 bytes) disk size: 1.5G
关闭虚拟机:
[root@kvm-node1 ~]# virsh list Id Name State ---------------------------------------------------- 1 centos6.5 running [root@kvm-node1 ~]# virsh destroy centos6.5 Domain centos6.5 destroyed [root@kvm-node1 ~]# virsh list Id Name State ----------------------------------------------------
将虚拟机磁盘映像转换为qcow2格式:
[root@kvm-node1 ~]# qemu-img convert -f raw -O qcow2 /images/kvm/centos6.img /images/kvm/centos6.qcow2 [root@kvm-node1 ~]# qemu-img info /images/kvm/centos6.qcow2 image: /images/kvm/centos6.qcow2 file format: qcow2 virtual size: 10G (10737418240 bytes) disk size: 1.3G cluster_size: 65536 [root@kvm-node1 ~]# ll /images/kvm/centos6* -rwxr-xr-x 1 root root 10737418240 Jul 1 20:13 /images/kvm/centos6.img -rw-r--r-- 1 root root 1394081792 Jul 1 20:17 /images/kvm/centos6.qcow2
使用qemu-img转换镜像只是对原有的镜像做了备份,原有的磁盘映像文件还在。
2、修改虚拟机xml配置文件
修改虚拟机的xml配置文件,主要是把虚拟机的磁盘映像文件修改为我们转换后的qcow2格式,然后启动虚拟机,才能执行快照操作。
[root@kvm-node1 ~]# virsh edit centos6.5 Domain centos6.5 XML configuration not changed. [root@kvm-node1 ~]# awk -F"'" '/<driver name(.*)cache=/{print $4}' /etc/libvirt/qemu/centos6.5.xml #获取磁盘映像的类型,原来是raw,已经修改为qcow2 qcow2 [root@kvm-node1 ~]# awk -F"'" '/<source file/{print $2}' /etc/libvirt/qemu/centos6.5.xml #获取磁盘映像文件路径,原来是centos6.img,这里已经修改为centos6.qcow2 /images/kvm/centos6.qcow2
3、启动虚拟机
[root@kvm-node1 ~]# virsh start centos6.5 Domain centos6.5 started
4、使用virsh命令创建快照
[root@kvm-node1 ~]# virsh snapshot-create centos6.5 Domain snapshot 1530448868 created
5、查看快照
[root@kvm-node1 ~]# virsh snapshot-list centos6.5 Name Creation Time State ------------------------------------------------------------ 1530448868 2018-07-01 20:41:08 +0800 running [root@kvm-node1 ~]# virsh snapshot-info --current centos6.5 Name: 1530448868 Domain: centos6.5 Current: yes State: running Location: internal Parent: - Children: 0 Descendants: 0 Metadata: yes
6、查看当前虚拟机快照的版本
[root@kvm-node1 ~]# virsh snapshot-current centos6.5 <domainsnapshot> <name>1530448868</name> <state>running</state> <creationTime>1530448868</creationTime> <memory snapshot='internal'/> <disks> <disk name='vda' snapshot='internal'/> <disk name='hdc' snapshot='no'/> </disks> <domain type='kvm'> <name>centos6.5</name> <uuid>be73fe50-fb80-d468-9e8a-e542f413d735</uuid> <memory unit='KiB'>1024000</memory> <currentMemory unit='KiB'>524288</currentMemory> <vcpu placement='static' current='1'>4</vcpu> <os> <type arch='x86_64' machine='rhel6.6.0'>hvm</type> <boot dev='hd'/> </os> <features> <acpi/> <apic/> <pae/> </features> <clock offset='utc'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>restart</on_crash> <devices> <emulator>/usr/libexec/qemu-kvm</emulator> <disk type='file' device='disk'> <driver name='qemu' type='qcow2' cache='none'/> <source file='/images/kvm/centos6.qcow2'/> <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> </disk> <disk type='block' device='cdrom'> <driver name='qemu' type='raw'/> <target dev='hdc' bus='ide'/> <readonly/> <address type='drive' controller='0' bus='1' target='0' unit='0'/> </disk> <controller type='usb' index='0' model='ich9-ehci1'> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/> </controller> <controller type='usb' index='0' model='ich9-uhci1'> <master startport='0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/> </controller> <controller type='usb' index='0' model='ich9-uhci2'> <master startport='2'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/> </controller> <controller type='usb' index='0' model='ich9-uhci3'> <master startport='4'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/> </controller> <controller type='ide' index='0'> <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/> </controller> <interface type='bridge'> <mac address='52:54:00:26:5a:8d'/> <source bridge='virbr0'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> <serial type='pty'> <target port='0'/> </serial> <console type='pty'> <target type='serial' port='0'/> </console> <input type='tablet' bus='usb'/> <input type='mouse' bus='ps2'/> <graphics type='vnc' port='5900' autoport='no' listen='0.0.0.0'> <listen type='address' address='0.0.0.0'/> </graphics> <video> <model type='cirrus' vram='9216' heads='1'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/> </video> <memballoon model='virtio'> <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> </memballoon> </devices> </domain> </domainsnapshot>
7、查看快照配置文件所在目录
[root@kvm-node1 ~]# ll /var/lib/libvirt/qemu/snapshot/ total 4 drwxr-xr-x 2 root root 4096 Jul 1 20:41 centos6.5 [root@kvm-node1 ~]# ll /var/lib/libvirt/qemu/snapshot/centos6.5/ total 4 -rw------- 1 root root 3492 Jul 1 20:41 1530448868.xml
8、删除一个快照
[root@kvm-node1 ~]# virsh snapshot-list centos6.5 Name Creation Time State ------------------------------------------------------------ 1530448868 2018-07-01 20:41:08 +0800 running [root@kvm-node1 ~]# virsh snapshot-delete centos6.5 1530448868 Domain snapshot 1530448868 deleted [root@kvm-node1 ~]# virsh snapshot-list centos6.5 Name Creation Time State ------------------------------------------------------------
9、恢复一个快照
1)首先,我们在centos6.5上创建一个文件,例如:
echo "This is a SNAPSHOT test." > /root/testfile
2)使用virsh snapshot-create-as创建一个快照
[root@kvm-node1 ~]# virsh snapshot-create-as --domain centos6.5 --name "centos6.5_snapshot_01" --description "snapshot with testfile saved." Domain snapshot centos6.5_snapshot_01 created
3)查看当前虚拟机的快照列表
[root@kvm-node1 ~]# virsh snapshot-list centos6.5 Name Creation Time State ------------------------------------------------------------ centos6.5_snapshot_01 2018-07-01 21:43:54 +0800 running
4)查看当前虚拟机的快照信息
[root@kvm-node1 ~]# virsh snapshot-info centos6.5 --current Name: centos6.5_snapshot_01 Domain: centos6.5 Current: yes State: running Location: internal Parent: - Children: 0 Descendants: 0 Metadata: yes
5)在虚拟机上删除testfile
6)使用virsh snapshot-revert恢复虚拟机快照
[root@kvm-node1 ~]# virsh snapshot-revert centos6.5 centos6.5_snapshot_01 --running
snapshot-revert用法:snapshot-revert <domain> [<snapshotname>] [--current] [--running] [--paused] [--force]
选项:
[--domain] <string> :指定domain名称,id或者uuid
[--snapshotname] <string> : 指定快照名称
--current :恢复到当前快照
--running :在恢复快照后自动运行虚拟机
--paused :在恢复快照后自动暂停虚拟机
--force :强制执行快照,有风险的恢复选项
[root@kvm-node1 ~]# virsh list Id Name State ---------------------------------------------------- 2 centos6.5 running
7)到虚拟机中查看testfile的内容
使用qemu-img管理虚拟机快照:
qemu-img snapshot选项:
-a:应用一个快照,即使用该快照还原(还原磁盘文件快照时的状态)1、为虚拟机创建快照
[root@kvm-node1 ~]# qemu-img snapshot -c centos6.5_snapshot_02 /images/kvm/centos6.qcow2
2、查看虚拟机快照
[root@kvm-node1 ~]# qemu-img snapshot -l /images/kvm/centos6.qcow2 Snapshot list: ID TAG VM SIZE DATE VM CLOCK 1 centos6.5_snapshot_01 166M 2018-07-01 21:43:54 01:08:54.228 2 centos6.5_snapshot_02 0 2018-07-01 22:01:34 00:00:00.000
上面的两个快照,第一个是使用vrish创建,第二个是使用qemu-img创建
3、恢复快照
[root@kvm-node1 ~]# qemu-img snapshot -a centos6.5_snapshot_02 /images/kvm/centos6.qcow2
4、删除快照
[root@kvm-node1 ~]# qemu-img snapshot -d centos6.5_snapshot_02 /images/kvm/centos6.qcow2
标签:编辑 put vpd force ide his boot rest 自动
原文地址:http://blog.51cto.com/jerry12356/2134814