KVM管理-快照

创建快照

为虚拟机vm1创建一个快照

kvm 创建实例快照 kvm快照原理_Domain

[root@myserver ~]# virsh snapshot-create-as vm1 vm1.snap
Domain snapshot vm1.snap created

快照只能使用qcow2创建,raw格式一般无法创建快照

查看磁盘镜像信息

[root@myserver ~]# qemu-img info /var/lib/libvirt/images/vm1.qcow2
image: /var/lib/libvirt/images/vm1.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 11G
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         vm1.snap               284M 2023-05-15 22:53:00   00:04:29.314
Format specific information:
    compat: 1.1
    lazy refcounts: true

查看vm1的快照

[root@myserver ~]# virsh snapshot-list vm1
 Name                 Creation Time             State
------------------------------------------------------------
 vm1.snap             2023-05-15 22:53:00 -0400 running

创建raw磁盘文件

[root@myserver ~]# qemu-img create -f raw /var/lib/libvirt/images/vm1-1.raw 2G
Formatting '/var/lib/libvirt/images/vm1-1.raw', fmt=raw size=2147483648
[root@myserver ~]# ll -h /var/lib/libvirt/images/vm1-1.raw
-rw-r--r-- 1 root root 2.0G May 15 23:00 /var/lib/libvirt/images/vm1-1.raw

修改配置文件

[root@myserver ~]# cd /etc/libvirt/qemu/
[root@myserver qemu]# ls
networks  vm1.xml
[root@myserver qemu]# vim vm1.xml


[root@myserver qemu]# virsh define vm1.xml
Domain vm1 defined from vm1.xml

在vm1.xml将这段内容修改成你需要的,slot不能相同,复制粘贴到文件中此格式相同文本的位置附近

<disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/var/lib/libvirt/images/vm1-1.raw'/>
      <target dev='vdb' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x0a' function='0x0'/>
    </disk>

创建一个快照

[root@myserver qemu]# virsh snapshot-create-as vm1 vm1.snap1
error: unsupported configuration: internal snapshot for disk vdb unsupported for storage type raw

刚刚放进去的raw磁盘不支持拍摄快照

将这个磁盘文件格式做一个转换

[root@myserver qemu]# qemu-img convert -O qcow2 /var/lib/libvirt/images/vm1-1.raw /var/lib/libvirt/images/vm1-1.qcow2
[root@myserver qemu]# cd /var/lib/libvirt/images/
[root@myserver images]# ll -h
total 11G
-rw-r--r-- 1 root root 193K May 15 23:19 vm1-1.qcow2
-rw-r--r-- 1 root root 2.0G May 15 23:00 vm1-1.raw
-rw------- 1 qemu qemu  11G May 15 23:19 vm1.qcow2
[root@myserver images]# qemu-img info vm1-1.qcow2
image: vm1-1.qcow2
file format: qcow2
virtual size: 2.0G (2147483648 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false

再次修改配置文件,将磁盘修改为qcow2格式

[root@myserver images]# vim /etc/libvirt/qemu/vm1.xml
[root@myserver images]# virsh de
define               destroy              detach-device-alias  detach-interface
desc                 detach-device        detach-disk
[root@myserver images]# virsh define /etc/libvirt/qemu/vm1.xml
Domain vm1 defined from /etc/libvirt/qemu/vm1.xml
<disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/var/lib/libvirt/images/vm1-1.qcow2'/>
      <target dev='vdb' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x0a' function='0x0'/>
    </disk>

恢复快照

创建一个快照测试

[root@myserver images]# virsh snapshot-create-as vm1 vm1.snap2
Domain snapshot vm1.snap2 created

成功创建

启动虚拟机vm1

kvm 创建实例快照 kvm快照原理_Domain_02


kvm 创建实例快照 kvm快照原理_运维_03

在Linux主机上给vm1创建第三个快照

[root@myserver ~]# virsh snapshot-create-as vm1 vm1.snap3
Domain snapshot vm1.snap3 created

回到虚拟机vm1中,给/test中复制两个文件

kvm 创建实例快照 kvm快照原理_Domain_04

在Linux主机上给vm1创建第四个快照

[root@myserver ~]# virsh snapshot-create-as vm1 vm1.snap4
Domain snapshot vm1.snap4 created

恢复到第三个快照

[root@myserver ~]# virsh snapshot-revert vm1 vm1.snap3

查看/test目录

kvm 创建实例快照 kvm快照原理_kvm 创建实例快照_05

恢复到第四个快照

[root@myserver ~]# virsh snapshot-revert vm1 vm1.snap4

kvm 创建实例快照 kvm快照原理_linux_06

删除虚拟机快照

[root@myserver ~]# virsh snapshot-list vm1
 Name                 Creation Time             State
------------------------------------------------------------
 vm1.snap             2023-05-15 22:53:00 -0400 running
 vm1.snap2            2023-05-15 23:35:28 -0400 shutoff
 vm1.snap3            2023-05-15 23:46:23 -0400 running
 vm1.snap4            2023-05-15 23:50:32 -0400 running
[root@myserver ~]# virsh snapshot-delete --snapshotname vm1.snap4 vm1
Domain snapshot vm1.snap4 deleted

[root@myserver ~]# virsh snapshot-list vm1
 Name                 Creation Time             State
------------------------------------------------------------
 vm1.snap             2023-05-15 22:53:00 -0400 running
 vm1.snap2            2023-05-15 23:35:28 -0400 shutoff
 vm1.snap3            2023-05-15 23:46:23 -0400 running
一个批量删除虚拟机快照的小脚本

进入Linux主机创建一个脚本,直接删除所有快照

查看所有guestos虚拟机名
[root@myserver test]# virsh list --all --name
vm1


获取某个虚拟机的所有快照名
[root@myserver test]# virsh snapshot-list vm1 --name
vm1.snap
vm1.snap2
vm1.snap3

测试过程

[root@myserver test]# ./rm-all-snap
获取所有虚拟机
vm1
请输入你要删除所有快照的虚拟机名称 :
vm1
获取 vm1 所有快照列表
vm1.snap1 vm1.snap2 vm1.snap3 vm1.snap4 vm1.snap5
确定要删除 vm1 的所有快照吗[y/n]?
n
取消删除
脚本执行完毕
[root@myserver test]# virsh snapshot-list vm1
 Name                 Creation Time             State
------------------------------------------------------------
 vm1.snap1            2023-05-16 01:59:29 -0400 shutoff
 vm1.snap2            2023-05-16 01:59:32 -0400 shutoff
 vm1.snap3            2023-05-16 01:59:38 -0400 shutoff
 vm1.snap4            2023-05-16 01:59:40 -0400 shutoff
 vm1.snap5            2023-05-16 01:59:43 -0400 shutoff

[root@myserver test]# ./rm-all-snap
获取所有虚拟机
vm1
请输入你要删除所有快照的虚拟机名称 :
vm1
获取 vm1 所有快照列表
vm1.snap1 vm1.snap2 vm1.snap3 vm1.snap4 vm1.snap5
确定要删除 vm1 的所有快照吗[y/n]?
y
Domain snapshot vm1.snap1 deleted

vm1.snap1 已被删除
Domain snapshot vm1.snap2 deleted

vm1.snap2 已被删除
Domain snapshot vm1.snap3 deleted

vm1.snap3 已被删除
Domain snapshot vm1.snap4 deleted

vm1.snap4 已被删除
Domain snapshot vm1.snap5 deleted

vm1.snap5 已被删除
脚本执行完毕
[root@myserver test]# virsh snapshot-list vm1
 Name                 Creation Time             State
------------------------------------------------------------

注意执行脚本前首先要给脚本赋予执行权限

查看脚本代码:

[root@myserver test]# cat rm-all-snap
#!/bin/bash
echo "获取所有虚拟机"
vm_list=$(virsh list --all --name)
echo $vm_list

echo "请输入你要删除所有快照的虚拟机名称 :"
read vm_name
snap_list=$(virsh snapshot-list "$vm_name" --name)
echo "获取" $vm_name "所有快照列表"
echo $snap_list
echo "确定要删除" $vm_name "的所有快照吗[y/n]?"
read chocie
if [ "$chocie" == "y" ];then
  for snap_name in $snap_list;do
    virsh snapshot-delete --snapshotname "$snap_name" "$vm_name"
    echo "$snap_name" "已被删除"
  done
else
  echo "取消删除"
fi

echo "脚本执行完毕"