kvm虚拟机快照

一.虚拟机快照管理

1.查看虚拟机的快照信息

[root@yinzhengjie-kvm189 ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 4     yinzhengjie-kvm01              running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh snapshot-list yinzhengjie-kvm01  # 很明显,默认情况下我的虚拟机是不会自动创建快照的
 Name                 Creation Time             State
------------------------------------------------------------

[root@yinzhengjie-kvm189 ~]# 

2.创建快照的两种方式

[root@yinzhengjie-kvm189 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 4     yinzhengjie-kvm01              running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh snapshot-create yinzhengjie-kvm01  # 创建快照不指定名称
Domain snapshot 1712523887 created
[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh snapshot-list yinzhengjie-kvm01  # 查看虚拟机的快照列表
 Name                 Creation Time             State
------------------------------------------------------------
 1712523887           2024-04-08 05:04:47 +0800 running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh snapshot-create-as yinzhengjie-kvm01 --name yinzhengjie_kvm_os_installd  # 创建虚拟机指定快照名称
Domain snapshot yinzhengjie_kvm_os_installd created
[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh snapshot-list yinzhengjie-kvm01 
 Name                 Creation Time             State
------------------------------------------------------------
 1712523887           2024-04-08 05:04:47 +0800 running
 yinzhengjie_kvm_os_installd 2024-04-08 05:05:18 +0800 running

[root@yinzhengjie-kvm189 ~]# 

二.基于快照恢复数据

1.模拟破坏系统环境

[root@yinzhengjie-kvm189 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 4     yinzhengjie-kvm01              running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01 
Connected to domain yinzhengjie-kvm01
Escape character is ^]

CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64

localhost login: root
Password: 
Last login: Sun Apr  7 19:28:05 on ttyS0
[root@localhost ~]# 
[root@localhost ~]# ldd /bin/ls  # 查看ls命令依赖的库文件
	linux-vdso.so.1 =>  (0x00007ffe669bd000)
	libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f5c2dba7000)
	libcap.so.2 => /lib64/libcap.so.2 (0x00007f5c2d9a2000)
	libacl.so.1 => /lib64/libacl.so.1 (0x00007f5c2d799000)
	libc.so.6 => /lib64/libc.so.6 (0x00007f5c2d3cb000)
	libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f5c2d169000)
	libdl.so.2 => /lib64/libdl.so.2 (0x00007f5c2cf65000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f5c2ddce000)
	libattr.so.1 => /lib64/libattr.so.1 (0x00007f5c2cd60000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f5c2cb44000)
[root@localhost ~]# 
[root@localhost ~]# rm -f /lib64/ld-linux-x86-64.so.2 
[root@localhost ~]# 
[root@localhost ~]# ls
-bash: /bin/ls: /lib64/ld-linux-x86-64.so.2: bad ELF interpreter: No such file or directory
[root@localhost ~]# 
[root@localhost ~]# rm
-bash: /bin/rm: /lib64/ld-linux-x86-64.so.2: bad ELF interpreter: No such file or directory
[root@localhost ~]# 

2.基于快照恢复虚拟机

[root@yinzhengjie-kvm189 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 4     yinzhengjie-kvm01              running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh snapshot-list yinzhengjie-kvm01 
 Name                 Creation Time             State
------------------------------------------------------------
 1712523887           2024-04-08 05:04:47 +0800 running
 yinzhengjie_kvm_os_installd 2024-04-08 05:05:18 +0800 running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh snapshot-revert yinzhengjie-kvm01 --snapshotname yinzhengjie_kvm_os_installd 

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh snapshot-list yinzhengjie-kvm01 
 Name                 Creation Time             State
------------------------------------------------------------
 1712523887           2024-04-08 05:04:47 +0800 running
 yinzhengjie_kvm_os_installd 2024-04-08 05:05:18 +0800 running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01 
Connected to domain yinzhengjie-kvm01
Escape character is ^]

CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64

localhost login: root
Password: 
Last login: Sun Apr  7 19:28:05 on ttyS0
[root@localhost ~]# date 
Sun Apr  7 21:05:37 CST 2024
[root@localhost ~]# 
[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]#  

3.使用快照注意事项

- raw的磁盘格式不支持快照,qcow2磁盘格式支持快照功能,并且快照就保存在qcow2的磁盘文件中;
- 虚拟机快照会额外占用磁盘空间,当删除快照时,其磁盘的使用空间大小并不会缩小哟,但快照的数据的确会被标记删除啦;


[root@yinzhengjie-kvm189 ~]# ll -h /opt/yinzhengjie-kvm01.qcow2 
-rw-r--r--. 1 qemu qemu 1.4G Apr  8 05:28 /opt/yinzhengjie-kvm01.qcow2
[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# qemu-img info /opt/yinzhengjie-kvm01.qcow2
image: /opt/yinzhengjie-kvm01.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.0G
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
2         yinzhengjie_kvm_os_installd   282M 2024-04-08 05:05:18   00:15:26.803
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@yinzhengjie-kvm189 ~]# 

三.删除快照

[root@yinzhengjie-kvm189 ~]# virsh snapshot-list yinzhengjie-kvm01 
 Name                 Creation Time             State
------------------------------------------------------------
 1712523887           2024-04-08 05:04:47 +0800 running
 yinzhengjie_kvm_os_installd 2024-04-08 05:05:18 +0800 running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh snapshot-delete yinzhengjie-kvm01 --snapshotname 1712523887
Domain snapshot 1712523887 deleted

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh snapshot-list yinzhengjie-kvm01 
 Name                 Creation Time             State
------------------------------------------------------------
 yinzhengjie_kvm_os_installd 2024-04-08 05:05:18 +0800 running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# 
posted @ 2024-03-04 23:48  尹正杰  阅读(82)  评论(0编辑  收藏  举报