KVM快照详解
- KVM快照
- 内存快照 save|restore
- 磁盘内部快照
- 创建快照
- 查看快照
- 快照回滚
- 快照删除
- 磁盘内部快照有2个缺点
KVM快照
KVM虚拟机的快照用来保存虚拟机在某个时间点的内存、磁盘或者设备状态,如果将来有需要可以把虚拟机的状态回滚到这个时间点。根据被做快照的对象不同,快照可以分为
- 磁盘快照
- 内存快照
两者加起来构成了一个系统还原点,记录虚拟机在某个时间点的全部状态;
根据做快照时虚拟机是否在运行,快照又可以分为
- 在线快照
- 离线快照。
磁盘快照根据存储方式的不同,又分为
- 内部快照:内部快照只支持qcow2格式的虚拟机镜像,把快照及后续变动都保存在原来的qcow2文件内;
- 外部快照:外部快照在创建时,快照被保存在单独一个文件中,创建快照时间点之后的数据被记录到一个新的qcow2文件中,原镜像文件成为新的qcow2文件的backing file(只读),在创建多个快照后,这些文件将形成一个链——backing chain。外部快照同时支持raw和qcow2格式的虚拟机镜像
安装KVM查看:
命令工具查看:
磁盘格式查看:
内存快照 save|restore
创建快照:
对nginx-1执行,命令执行完成后nginx-1虚拟机会进入到shutoff状态
[root@kvm-1 ~]# virsh save nginx-1 nginx-1.snap1 --verbose
Save: [100 %]
Domain nginx-1 saved to nginx-1.snap1
[root@kvm-1 ~]# virsh list --all
Id Name State
----------------------------------------------------
1 Ubuntu-4 running
- nginx-1 shut off
查看快照信息:
[root@kvm-1 ~]# qemu-img info nginx-1.snap1
image: nginx-1.snap1
file format: raw
virtual size: 384M (402965504 bytes)
disk size: 384M
快照回滚:
回滚后设备恢复到上次运行时的状态!
[root@kvm-1 ~]# virsh restore nginx-1.snap1
Domain restored from nginx-1.snap1
[root@kvm-1 ~]# virsh list
Id Name State
----------------------------------------------------
1 Ubuntu-4 running
9 nginx-1 running
磁盘内部快照
创建快照
查看帮助
[root@kvm-1 ~]# virsh snapshot-create-as --help
OPTIONS
[--domain] <string> domain name, id or uuid
[--name] <string> name of snapshot
[--description] <string> description of snapshot
--print-xml print XML document rather than create
--no-metadata take snapshot but create no metadata #创建的快照不带任何元数据
--halt halt domain after snapshot is created #快照创建后虚机会关闭
--disk-only capture disk state but not vm state #只对磁盘做快照,忽略其它参数
--reuse-external reuse any existing external files
--quiesce quiesce guest file systems #libvirt 会通过 QEMU GA 尝试去freeze和unfreeze客户机已经mounted的文件系统;如果客户机没有安装QEMU GA,则操作会失败。
--atomic require atomic operation #快照要么完全成功要么完全失败,不允许部分成果。不是所有的VMM都支持。
--live take a live snapshot #当客户机处于运行状态下做快照
列出当前的虚拟机
[root@kvm-1 ~]# virsh list
Id Name State
----------------------------------------------------
10 Ubuntu-1 running
为Ubuntu-1
创建快照
[root@kvm-1 ~]# virsh snapshot-create-as --domain Ubuntu-1 --name Ubuntu-1-first-snap
Domain snapshot Ubuntu-1-first-snap created
创建创建虚拟机的所有磁盘和内存做内部快照,创建快照时虚机处于 paused
状态,快照完成后变为 running
状态。
[root@kvm-1 ~]# virsh list
Id Name State
----------------------------------------------------
10 Ubuntu-1 paused
查看快照
[root@kvm-1 ~]# virsh snapshot-list --domain Ubuntu-1
Name Creation Time State
------------------------------------------------------------
Ubuntu-1-first-snap 2020-05-11 19:49:26 +0800 running
输出解释:
- Name:查看时要指定
--domain
为哪个虚拟机创建的快照,指定虚拟机的名字,也就是查看当前虚拟机的快照有哪些! - Creation Time:创建此快照的时间!
- State:分为两种,running为再虚拟机运行状态下创建的尽快找,shutoff为虚拟机再关机状态下创建的快照!
其他命令参数:
查看Ubuntu-1这个虚拟机中有几个快照!
[root@kvm-1 ~]# virsh snapshot-list --domain Ubuntu-1 --tree
Ubuntu-1-first-snap
查看快照详情!
[root@kvm-1 ~]# virsh snapshot-info --domain Ubuntu-1 --snapshotname Ubuntu-1-first-snap
Name: Ubuntu-1-first-snap
Domain: Ubuntu-1
Current: yes
State: running
Location: internal
Parent: -
Children: 0
Descendants: 0
Metadata: yes
在虚拟机Ubuntu-1上测试
安装一个nginx,然后测试通过后,进行回滚动作!还原到刚安装时的样子!
千万别搞错了!在刚创建的虚拟机中安装nginx,不是在KVM主机创建
执行:
# sed -i -r 's#(.*)cn.archive.ubuntu.com(.*)$#\1mirrors.aliyun.com\2#' /etc/apt/sources.list&& cat /etc/apt/sources.list | grep "^[a-Z]" #修改源为aliyun的
# apt-get update
# apt-get install nginx
# echo 'test nginx' > /var/www/html/index.nginx-debian.html
# curl 127.0.0.1
test nginx
快照回滚
[root@kvm-1 ~]# virsh snapshot-revert --domain Ubuntu-1 --snapshotname Ubuntu-1-first-snap
# 没有输出内容!
到虚拟机上看一下是不是回滚完成!
回滚后虚拟机会掉线!重新登录查看
# 源路径又变回了最开始的!
root@ubuntu-1:~# grep "^[a-Z]" /etc/apt/sources.list
deb http://cn.archive.ubuntu.com/ubuntu bionic main restricted
deb http://cn.archive.ubuntu.com/ubuntu bionic-updates main restricted
deb http://cn.archive.ubuntu.com/ubuntu bionic universe
deb http://cn.archive.ubuntu.com/ubuntu bionic-updates universe
deb http://cn.archive.ubuntu.com/ubuntu bionic multiverse
deb http://cn.archive.ubuntu.com/ubuntu bionic-updates multiverse
deb http://cn.archive.ubuntu.com/ubuntu bionic-backports main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu bionic-security main restricted
deb http://cn.archive.ubuntu.com/ubuntu bionic-security universe
deb http://cn.archive.ubuntu.com/ubuntu bionic-security multiverse
# 没有nginx,就会提醒你安装
root@ubuntu-1:~# nginx
Command 'nginx' not found, but can be installed with:
apt install nginx-core
apt install nginx-extras
apt install nginx-full
apt install nginx-light
回滚一次后快照依然存在!
[root@kvm-1 ~]# virsh snapshot-list --domain Ubuntu-1
Name Creation Time State
------------------------------------------------------------
Ubuntu-1-first-snap 2020-05-11 19:49:26 +0800 running
快照删除
# virsh snapshot-delete --domain Ubuntu-1 --snapshotname Ubuntu-1-first-snap
Domain snapshot Ubuntu-1-first-snap deleted
再次查看没有了!
[root@kvm-1 ~]# virsh snapshot-list --domain Ubuntu-1
Name Creation Time State
------------------------------------------------------------
磁盘内部快照有2个缺点
- 只支持qcow2格式的镜像文件;
- 创建快照虚拟机会paused,有停机时间,对于不能停机的线上业务来说是无法接受的。