kvm 虚拟化技术 1.3之kvm克隆以及快照
1.kvm虚拟机克隆
克隆kvm虚拟机 ,克隆前需要提前关机
语法: virt-clone -o 原虚拟机 -n 新虚拟机 -f 新虚拟机镜像存放路径
选项中-o 表示 old -n 表示new 虚拟机镜像: 就是整个虚拟机文件。 不是操作系统光盘镜像
[root@hd1 opt]# virt-clone -o centos7.5 -n hd11.com -f /opt/hd11.com.img
正在分配 'hd11.com.img' | 10 GB 00:00:18
成功克隆 'hd11.com'。
2。kvm快照
快照作用:1.热备 2.灾难恢复 3.回到历史中某个状态
(1)创建kvm快照
先将虚拟机关机 virsh shutdown centos7.5
(2)查看要备份的磁盘镜像格式
qemu-img info /opt/centos7.raw

(3)raw不支持快照,需要把raw改成qcow2格式
[root@hd1 opt]# qemu-img convert -f raw -O qcow2 /opt/centos7.raw /opt/centos7.qcow2
(4)查看下备份的centos7.qcow2的磁盘镜像格式,
qemu-img info /opt/centos7.qcow2
file format:qcow2
(5)编辑centos7.5文件改他的主配置文件
[root@hd1 opt]# virsh edit centos7.5 <driver name='qemu' type='qcow2'/> <source file='/opt/centos7.qcow2'/>
(6)重新重启下libvirtd
systemctl restart libvirtd
(7)创建快照
virsh snapshot-create centos7.5
(8)查看快照的版本
virsh snapshot-list centos7.5 名称 生成时间 状态 ------------------------------------------------------------ 1629708007 2021-08-23 16:40:07 +0800 shutoff
(9)查看快照配置文件
[root@hd1 opt]# virsh snapshot-current centos7.5
注意:该配置文件在目录下/var/lib/libvirt/qemu/snapshot
[root@hd1 opt]# cd /var/lib/libvirt/qemu/snapshot/centos7.5/
[root@hd1 centos7.5]# ls
1629708007.xml
7 恢复到指定的版本 1629708007 ,没有任何输出则代表恢复成功
root@hd1 centos7.5]# virsh snapshot-revert centos7.5 1629708007
确定一下
[root@hd1 centos7.5]# virsh snapshot-current centos7.5|grep creation
<creationTime>1629708007</creationTime>
8 删除虚拟机快照。在删除快照之前,先看下快照信息:
[root@hd1 centos7.5]# qemu-img info /opt/centos7.qcow2
image: /opt/centos7.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.4G
cluster_size: 65536
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
1 1629708007 0 2021-08-23 16:40:07 00:00:00.000
2 1629708490 0 2021-08-23 16:48:10 00:00:00.000
删除第一个快照
[root@hd1 centos7.5]# virsh snapshot-delete centos7.5 1629708007
已删除域快照 1629708007
9 )快照只有编号吗?可以起名字吗 ?
启动kvm中的虚拟机
[root@hd1 centos7.5]# virsh start centos7.5
登录192.168.1.120 创建一个a.txt
[root@localhost ~]# ls
anaconda-ks.cfg a.txt
root@hd1 centos7.5]# virsh snapshot-create-as centos7.5 c-snap1
已生成域快照 c-snap1
查看当前的快照
root@hd1 ~]# virsh snapshot-current centos7.5|grep name
[root@localhost ~]# touch b.xt
[root@localhost ~]# ls
anaconda-ks.cfg a.txt b.xt
再次创建快照
[root@hd1 centos7.5]# virsh snapshot-create-as centos7.5 c-snap2
已生成域快照 c-snap2
查看快照
[root@hd1 centos7.5]# virsh snapshot-list centos7.5
名称 生成时间 状态
------------------------------------------------------------
1629708490 2021-08-23 16:48:10 +0800 shutoff
c-snap1 2021-08-23 17:03:23 +0800 running
c-snap2 2021-08-23 17:07:10 +0800 running
查看快照信息
[root@hd1 centos7.5]# qemu-img info /opt/centos7.
centos7.iso centos7.qcow2 centos7.raw
[root@hd1 centos7.5]# qemu-img info /opt/centos7.qcow2
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
2 1629708490 0 2021-08-23 16:48:10 00:00:00.000
3 c-snap1 329M 2021-08-23 17:03:23 00:06:27.656
4 c-snap2 329M 2021-08-23 17:07:11 00:10:13.462
10 恢复快照 (恢复快照必须要关闭计算机?)
关机
[root@hd1 centos7.5]# virsh shutdown centos7.5
执行恢复snap1快照
[root@hd1 centos7.5]# virsh snapshot-revert centos7.5 c-snap1
[root@hd1 centos7.5]# virsh list
Id 名称 状态
----------------------------------------------------
4 hd11.com running
7 centos7.5 running
确认恢复到了c-snap1
root@hd1 centos7.5]# virsh snapshot-current centos7.5|grep name|more
<name>c-snap1</name>
<disk name='vda' snapshot='internal'/>
<disk name='hda' snapshot='no'/>
<name>centos7.5</name>
重新登录192.168.1.120 查看当前目录下的文件

11 删除指定虚拟机快照
[root@hd1 centos7.5]# virsh snapshot-delete centos7.5 c-snap2
已删除域快照 c-snap2
查看快照配置文件
[root@hd1 ~]# cd /var/lib/libvirt/qemu/snapshot/centos7.5/
[root@hd1 centos7.5]# ls
1629708490.xml c-snap1.xml
12 查看开机创建的快照占用的磁盘空间:
[root@hd1 centos7.5]# qemu-img info /opt/centos7.qcow2
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
2 1629708490 0 2021-08-23 16:48:10 00:00:00.000
3 c-snap1 329M 2021-08-23 17:03:23 00:06:27.656
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具