Managing Virtual Machines with KVM Commands

Managing Virtual Machines with KVM Commands1. Basic KVM Function ManagementView command help

[root@kvm01 ~]# virsh -h
virsh [options]... [<command_string>]virsh [options]... <command> [args...]
  options:    -c | --connect=URI      hypervisor connection URI    -d | --debug=NUM        debug level [0-4]    -e | --escape <char>    set escape sequence for console……

View the directory where virtual machine configuration files are stored

[root@kvm01 ~]# ls /etc/libvirt/qemu/networks  vm1.xml

Check the status of the virtual machine

[root@kvm01 ~]# virsh list --all Id    Name                         State---------------------------------------------------- 2     vm1                            running

Shut down the virtual machine

[root@kvm01 ~]# virsh shutdown vm1Domain vm1 is being shut down

Start the virtual machine

[root@kvm01 ~]# virsh list --all Id    Name                         State---------------------------------------------------- -     vm1                            shut down
[root@kvm01 ~]# virsh start vm1Domain vm1 has started
[root@kvm01 ~]# virsh list --all Id    Name                         State---------------------------------------------------- 3     vm1                            running

Force shutdown the virtual machine (power off). Use this when normal shutdown fails; generally, avoid using this command.

[root@kvm01 ~]# virsh list --all Id    Name                         State---------------------------------------------------- 3     vm1                            running
[root@kvm01 ~]# virsh destroy vm1Domain vm1 has been deleted
[root@kvm01 ~]# virsh list --all Id    Name                         State---------------------------------------------------- -     vm1                            shut down

Start the virtual machine instance using the configuration file

[root@kvm01 ~]# virsh create /etc/libvirt/qemu/vm1.xmlDomain vm1 has been created (from /etc/libvirt/qemu/vm1.xml)
[root@kvm01 ~]# virsh list --all Id    Name                         State---------------------------------------------------- 5     vm1                            running

Pause the virtual machine

[root@kvm01 ~]# virsh list --all Id    Name                         State---------------------------------------------------- 5     vm1                            running
[root@kvm01 ~]# virsh suspend vm1Domain vm1 has been suspended
[root@kvm01 ~]# virsh list --all Id    Name                         State---------------------------------------------------- 5     vm1                            paused

Resume the virtual machine

[root@kvm01 ~]# virsh list --all Id    Name                         State---------------------------------------------------- 5     vm1                            paused
[root@kvm01 ~]# virsh resume vm1Domain vm1 has been resumed
[root@kvm01 ~]# virsh list --all Id    Name                         State---------------------------------------------------- 5     vm1                            running

Configure the virtual machine instance to start automatically with the host

[root@kvm01 ~]# virsh autostart vm1Domain vm1 marked for autostart

Export the virtual machine configuration

[root@kvm01 ~]# ls /etc/libvirt/qemu/autostart  networks  vm1.xml[root@kvm01 ~]# virsh dumpxml vm1 > /etc/libvirt/qemu/vm2.xml[root@kvm01 ~]# ls /etc/libvirt/qemu/autostart  networks  vm1.xml  vm2.xml

Delete the virtual machine

[root@kvm01 ~]# virsh list --all Id    Name                         State---------------------------------------------------- 5     vm1                            running
[root@kvm01 ~]# virsh shutdown vm1Domain vm1 is being shut down
[root@kvm01 ~]# virsh undefine vm1Domain vm1 has been undefined

Check the deletion result; the configuration file for vm1 has been deleted, but the disk file will not be deleted

[root@kvm01 ~]# ls /etc/libvirt/qemu/autostart  networks  vm2.xml

Using virsh list –all to check the virtual machine, vm1 is not visible, indicating that the virtual machine has been deleted

[root@kvm01 ~]# virsh list --all Id    Name                         State----------------------------------------------------

Redefine the virtual machine using the backup configuration file

[root@kvm01 ~]# cd /etc/libvirt/qemu/[root@kvm01 qemu]# lsautostart  networks  vm2.xml[root@kvm01 qemu]# mv vm2.xml vm1.xml[root@kvm01 qemu]# lsautostart  networks  vm1.xml[root@kvm01 qemu]# virsh define vm1.xmlDefining domain vm1 (from vm1.xml)[root@kvm01 qemu]# virsh list --all Id    Name                         State---------------------------------------------------- -     vm1                            shut down

2. Cloning Virtual MachinesCheck the disk file storage location of the vm1 virtual machine

[root@kvm01 ~]# virsh list --all Id    Name                         State---------------------------------------------------- -     vm1                            shut down
[root@kvm01 ~]# virsh domblklist vm1Target     Source------------------------------------------------hda        /home/storage/vm1.qcow2hdb        -

Clone vm1 to create vm2

[root@kvm01 ~]# virsh list --all Id    Name                         State---------------------------------------------------- -     vm1                            shut down
[root@kvm01 ~]# virt-clone -o vm1 -n vm2 -f /home/storage/vm2.qcow2Allocating 'vm2.qcow2'                                                             |  20 GB  00:00:04
Successfully cloned 'vm2'.[root@kvm01 ~]# virsh list --all Id    Name                         State---------------------------------------------------- -     vm1                            shut down -     vm2                            shut down

Check if the cloned virtual machine can start normally

[root@kvm01 ~]# virsh start vm1Domain vm1 has started
[root@kvm01 ~]# virsh start vm2Domain vm2 has started
[root@kvm01 ~]# virsh list --all Id    Name                         State---------------------------------------------------- 6     vm1                            running 7     vm2                            running

3. Virtual Machine SnapshotsCreate a snapshot for the vm2 virtual machine

[root@kvm01 ~]# virsh snapshot-create vm2Snapshot domain created 1754565426

View the snapshot

[root@kvm01 ~]# virsh snapshot-list vm2 Name               Creation Time              State------------------------------------------------------------ 1754565426           2025-08-07 19:17:06 +0800 running

Restore the snapshot

[root@kvm01 ~]# virsh snapshot-list vm2 Name               Creation Time              State------------------------------------------------------------ 1754565426           2025-08-07 19:17:06 +0800 running
[root@kvm01 ~]# virsh snapshot-revert vm2 1754565426

View the current snapshot version information

[root@kvm01 ~]# virsh snapshot-current vm2<domain snapshot>  <name>1754565426</name>  <state>running</state>  <creationTime>1754565426</creationTime>  <memory snapshot='internal'/>  <disks>    <disk name='hda' snapshot='internal'/>    <disk name='hdb' snapshot='no'/>  </disks>……

Delete the snapshot

[root@kvm01 ~]# virsh snapshot-list vm2 Name               Creation Time              State------------------------------------------------------------ 1754565426           2025-08-07 19:17:06 +0800 running
[root@kvm01 ~]# virsh snapshot-delete vm2 1754565426Deleted domain snapshot 1754565426
[root@kvm01 ~]# virsh snapshot-list vm2 Name               Creation Time              State------------------------------------------------------------

These are the core operations of KVM commands. We have completed the creation, management, and monitoring of virtual machines using the virsh command, and practiced key features such as snapshots and cloning. KVM, as an open-source virtualization solution, can help everyone efficiently utilize hardware resources and build flexible cloud environments.It is recommended to try command combinations in a test environment; mastering them will significantly improve operational efficiency.Thank you for your attention, and I wish everyone success in their virtualization practices!🚀

Leave a Comment