如何使用 virsh 命令创建、还原和删除 KVM 虚拟机快照
KVM virsh
![](/data/attachment/album/201805/07/222002g2mmtixiilftxxhh.jpg)


在虚拟化平台上进行系统管理工作时，经常需要在开始重大操作比如部署补丁和代码前先设置一个虚拟机<ruby> 快照 <rt>  snapshot </rt></ruby>。


虚拟机**快照**是特定时间点的虚拟机磁盘的副本。换句话说，快照保存了给定的时间点虚拟机的状态和数据。


### 我们可以在哪里使用虚拟机快照？


如果你在使用基于 **KVM** 的<ruby> 虚拟机管理程序 <rp>  （ </rp> <rt>  hypervisor </rt> <rp>  ） </rp></ruby>，那么可以使用 `virsh` 命令获取虚拟机或域快照。快照在一种情况下变得非常有用，当你已经在虚拟机上安装或应用了最新的补丁，但是由于某些原因，虚拟机上的程序变得不稳定，开发团队想要还原所有的更改和补丁。如果你在应用补丁之前设置了虚拟机的快照，那么可以使用快照将虚拟机恢复到之前的状态。


**注意：**我们只能对磁盘格式为 **Qcow2** 的虚拟机的进行快照，并且 kvm 的 `virsh` 命令不支持 raw 磁盘格式，请使用以下命令将原始磁盘格式转换为 qcow2。



```
# qemu-img convert -f raw -O qcow2 image-name.img image-name.qcow2

```

### 创建 KVM 虚拟机（域）快照


我假设 KVM 管理程序已经在 CentOS 7 / RHEL 7 机器上配置好了，并且有虚拟机正在运行。我们可以使用下面的 `virsh` 命令列出虚拟机管理程序中的所有虚拟机，



```
[root@kvm-hypervisor ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 94    centos7.0                      running
 101   overcloud-controller           running
 102   overcloud-compute2             running
 103   overcloud-compute1             running
 114   webserver                      running
 115   Test-MTN                       running

```

假设我们想创建 webserver 虚拟机的快照，运行下面的命令，


**语法：**



```
# virsh snapshot-create-as –domain {vm_name} –name {snapshot_name} –description “enter description here”

```


```
[root@kvm-hypervisor ~]# virsh snapshot-create-as --domain webserver --name webserver_snap --description "snap before patch on 4Feb2018"
Domain snapshot webserver_snap created

```

创建快照后，我们可以使用下面的命令列出与虚拟机相关的快照：



```
[root@kvm-hypervisor ~]# virsh snapshot-list webserver
 Name                 Creation Time             State
------------------------------------------------------------
 webserver_snap       2018-02-04 15:05:05 +0530 running
[root@kvm-hypervisor ~]#

```

要列出虚拟机快照的详细信息，请运行下面的 `virsh` 命令：



```
[root@kvm-hypervisor ~]# virsh snapshot-info --domain webserver --snapshotname webserver_snap
Name:           webserver_snap
Domain:         webserver
Current:        yes
State:          running
Location:       internal
Parent:         -
Children:       0
Descendants:    0
Metadata:       yes

```

我们可以使用下面的 `qemu-img` 命令查看快照的大小：



```
[root@kvm-hypervisor ~]# qemu-img info /var/lib/libvirt/images/snaptestvm.img

```

![qemu-img-command-output-kvm](/data/attachment/album/201805/07/221938zeb4l14vctecl70o.jpg)


### 还原 KVM 虚拟机快照


假设我们想要将 webserver 虚拟机还原到我们在上述步骤中创建的快照。使用下面的 `virsh` 命令将 Webserver 虚拟机恢复到其快照 webserver\_snap 时。


**语法：**



```
# virsh snapshot-revert {vm_name} {snapshot_name}

```


```
[root@kvm-hypervisor ~]# virsh snapshot-revert webserver webserver_snap

```

### 删除 KVM 虚拟机快照


要删除 KVM 虚拟机快照，首先使用 `virsh snapshot-list` 命令获取虚拟机的快照详细信息，然后使用 `virsh snapshot-delete` 命令删除快照。如下示例所示：



```
[root@kvm-hypervisor ~]# virsh snapshot-list --domain webserver
 Name                 Creation Time             State
------------------------------------------------------------
 webserver_snap       2018-02-04 15:05:05 +0530 running
[root@kvm-hypervisor ~]# virsh snapshot-delete --domain webserver --snapshotname webserver_snap
Domain snapshot webserver_snap deleted

```

这就是本文的全部内容，我希望你们能够了解如何使用 `virsh` 命令来管理 KVM 虚拟机快照。请分享你的反馈，并不要犹豫地分享给你的技术朋友


OK
In this blog post, we will explain how to create KVM virtual machine (vm) snapshot with virsh command.

While working on the virtualization platform system administrators usually take the snapshot of virtual machine before doing any major activity like deploying the latest patch and code.

Virtual machine snapshot is a copy of virtual machine’s disk at the specific point of time. In other words, snapshots are like frozen moments in time, capturing the state of a virtual machine at a particular point.

#### Why VM Snapshots Matter?

If you are working on KVM based hypervisors we can take virtual machines or domain snapshot using the virsh command. Snapshot becomes very helpful in a situation where you have installed or apply the latest patches on the VM but due to some reasons, application hosted in the VMs becomes unstable and application team wants to revert all the changes or patches. If you had taken the snapshot of the VM before applying patches then we can restore or revert the VM to its previous state using snapshot.

**Note:** We can only take the snapshot of the VMs whose disk format is Qcow2 and raw disk format is not supported by kvm virsh command, Use below command to convert the raw disk format to qcow2

# qemu-img convert -f raw -O qcow2 image-name.img image-name.qcow2

## Create KVM Virtual Machine Snapshot

We are assuming KVM hypervisor is already configured on RHEL 8/9 or Rocky Linux or Ubuntu and VMs are running on it. We can list the all the VMs on hypervisor using below virsh command,

# virsh list --all Id Name State ---------------------------------------------------- 94 centos7.0 running 101 overcloud-controller running 102 overcloud-compute2 running 103 overcloud-compute1 running 114 webserver running 115 Test-MTN running #

Let’s suppose we want to create the snapshot of ‘webserver‘ VM, run the below command,

#### Syntax:


# virsh snapshot-create-as –domain {vm_name} –name {snapshot_name} –description “enter description here”

# virsh snapshot-create-as --domain webserver --name webserver_snap --description "snap before patch on 4Feb2018" Domain snapshot webserver_snap created #

Once the snapshot is created then we can list snapshots related to the VM using beneath command,

# virsh snapshot-list webserver Name Creation Time State ------------------------------------------------------------ webserver_snap 2018-02-04 15:05:05 +0530 running #

To list the detailed info of VM’s snapshot, run the beneath virsh command,

# virsh snapshot-info --domain webserver --snapshotname webserver_snap Name: webserver_snap Domain: webserver Current: yes State: running Location: internal Parent: - Children: 0 Descendants: 0 Metadata: yes #

We can view the size of snapshot using below qemu-img command,

# qemu-img info /var/lib/libvirt/images/snaptestvm.img

## Revert / Restore KVM Virtual Machine to snapshot

Let’s assume we want to revert or restore webserver VM to the snapshot that we have created in above step. Use below virsh command to restore Webserver VM to its snapshot “**webserver_snap**”

**Syntax :**

# virsh snapshot-revert {vm_name} {snapshot_name}

# virsh snapshot-revert webserver webserver_snap

## Delete KVM Virtual Machine Snapshots

To delete KVM virtual machine snapshots, first get the VM’s snapshot details using “virsh snapshot-list” command and then use “virsh snapshot-delete” command to delete the snapshot. Example is shown below:

# virsh snapshot-list --domain webserver Name Creation Time State ------------------------------------------------------------ webserver_snap 2018-02-04 15:05:05 +0530 running #

# virsh snapshot-delete --domain webserver --snapshotname webserver_snap Domain snapshot webserver_snap deleted #

#### Conclusion:

Creating KVM virtual machine snapshots with the Virsh command provides an essential layer of flexibility and security when working with virtualization. Snapshots allow you to experiment, troubleshoot, and maintain system stability effortlessly. By following the steps in this guide, you can harness the power of KVM snapshots to take control of your virtual machines, ensuring your work is safe, efficient, and well-managed.

Also Read : [How to Create and Manage KVM Virtual Machines via Command Line](https://www.linuxtechi.com/create-manage-kvm-virtual-machine-cli/)

sampathThanks dude for the article.

GiriQuestion:

Lets say you have 3 snapshots, A B and C, taken in that order (thats C taken after B taken after A) with C as the current snapshot. If I want to delete the snapshots A and B, will the command still work. If yes, do you see potential problems of doing these?

Samanta Ujjalhi ,

if i destroy the vm Can i recover it from the snapahot

shirishNot able to merge snapshot to existing disk

**KVM and qemu RPM info :-**

[root@shirish_rhel ~]# rpm -qa | grep -i qemu

qemu-kvm-common-1.5.3-167.el7.x86_64

qemu-img-1.5.3-167.el7.x86_64

ipxe-roms-qemu-20180825-2.git133f4c.el7.noarch

libvirt-daemon-driver-qemu-4.5.0-23.el7.x86_64

qemu-kvm-1.5.3-167.el7.x86_64

qemu-kvm-tools-1.5.3-167.el7.x86_64

[root@shirish_rhel ~]# virsh snapshot-list –domain centos7.0

Name Creation Time State

————————————————————

testVM2-firstSNAP35 2019-08-08 13:22:11 -0400 shutoff

testVM2-firstSNAP36 2019-08-08 13:24:48 -0400 shutoff

testVM2-firstSNAP37 2019-08-08 13:27:13 -0400 shutoff

[root@shirish_rhel ~]# virsh snapshot-info –domain centos7.0 –snapshotname testVM2-firstSNAP37

Name: testVM2-firstSNAP37

Domain: centos7.0

Current: yes

State: shutoff

Location: external

Parent: –

Children: 0

Descendants: 0

Metadata: yes

**if VM is shutdown :-**

[root@shirish_rhel ~]# virsh blockcommit centos7.0 vda –active –verbose –pivot

error: Requested operation is not valid: domain is not running

**if VM is Running :-**

[root@shirish_rhel ~]# virsh blockcommit centos7.0 vda –active –verbose –pivot

error: unsupported configuration: online commit not supported with this QEMU binary

Pradeep KumarQEMU libraries are old that’s why online commit is not possible in your current qemu library, You need to install QEMU 2.1 ( and above), libvirt-1.2.9 (and above)