Table of Contents

Virtualization has become an integral part of modern computing, enabling efficient utilization of hardware resources and facilitating the management of multiple virtual machines (VMs) on a single physical host. KVM (Kernel-based Virtual Machine) is a popular open-source virtualization solution for Linux, providing a robust and flexible platform for hosting VMs.

When managing KVM-based guest VMs, administrators often rely on the command-line tool “virsh.” Virsh allows users to interact with the hypervisor and perform various operations, such as creating, modifying, and controlling VMs. However, mastering the numerous virsh commands can be daunting for newcomers and experienced users.

This article presents a comprehensive virsh commands cheatsheet that serves as a quick reference guide for managing KVM guest VMs.

Virsh Commands Cheatsheet For KVM

Virsh Commands Cheatsheet For KVM

Most virsh commands follow a standard structure: “$ virsh [OPTION]… <command> <domain> [ARG]…“.

1. Virsh: Display Node Information

Here’s the first command on our virsh commands cheatsheet, which allows you to view host node information and details about the virtualization-capable machines:

  • $ sudo virsh node info

2. List OS Variants

To obtain the value for the –os-variant flag, execute the following command:

  • $ osinfo-query os

3. Virsh: List All Domains

To display a list of both inactive and active domains, use the following command:

  • $ sudo virsh list –all

4. List only active domains

To view only the active domains using the virsh command, utilize:

  • $ sudo virsh list

5. Virsh rename the domain

Syntax: virsh dorename currentname newname

6. Starting virtual machines

To initiate a virtual machine, simply execute the following command: virsh start <vm_name>

Replace <vm_name> with the specific name of the virtual machine you wish to launch. As an example: virsh start new_vm

7. Stopping virtual machines

To halt a virtual machine, utilize the following commands: virsh shutdown <vm_name>

If you require a more immediate cessation, opt for the command: virsh destroy <vm_name>

8. Configuring Virtual Machines with XML Files

Every virtual machine possesses an XML configuration file. To review the configuration of a specific virtual machine, execute: virsh dumpxml <vm_name>

To edit the XML configuration file, apply the subsequent command: virsh edit <vm_name>

This command will open the XML file using your default text editor, enabling you to make adjustments to the virtual machine settings, such as memory, CPU, and network configurations.

9. Create a new storage pool

To establish a new storage pool, utilize either of the following commands:

To create and activate the storage pool using an XML file: virsh pool-create <pool_xml>
Here, <pool_xml> denotes the path to the XML file containing the storage pool configuration.

To define a new storage pool without immediate activation: virsh pool-define-as

Both commands offer options for configuring storage pools, allowing you to either create and activate a pool directly or define its specifications for future use without immediate activation.

10. List storage pools

To obtain a list of all storage pools, simply execute the following command: virsh pool-list
Upon execution, this command will present a table containing the pool name, state, and other pertinent information for each storage pool.

11. List storage volumes

To list the storage volumes within a pool, employ the following command: virsh vol-list <pool_name>

12. Create a new storage volume

To generate a new storage volume within a pool, execute the subsequent command: virsh vol-create-as <pool_name> <vol_name> <size>

For instance:virsh vol-create-as default new_volume 10G
The above command generates a fresh 10GB storage volume named “new_volume” in the “default” storage pool.

Aside from storage management, KVM also offers virtual network management. Virtual networks enable connectivity between virtual machines and the host system.

To create a new virtual network, utilize the following command: virsh net-define <network_xml>
In this command, <network_xml> denotes the path to an XML file containing the network configuration.

13. List the virtual networks

To retrieve a comprehensive list of all virtual networks, employ the following command: virsh net-list

Executing this command will present a table featuring the network name, state, and other pertinent information.

14. Taking Snapshots of Virtual Machines

Snapshots play a crucial role in virtualization platforms, enabling you to capture the current state of a virtual machine and revert to it later. Using virsh commands, you can effortlessly create, list, and delete snapshots.

To generate a fresh snapshot, use the virsh snapshot-create-as <vm_name> <snapshot_name> command.

For instance: virsh snapshot-create-as new_vm snapshot1
The above command creates a new snapshot named “snapshot1” for the virtual machine “new_vm.”

15. List virtual machine snapshots

To view a comprehensive list of all snapshots for a virtual machine, utilize the following command: virsh snapshot-list <vm_name>

Executing this command will present a table that includes the snapshot name, creation time, and current state of the snapshots associated with the specified <vm_name>.

16. Revert a virtual machine snapshot

To restore a virtual machine to a particular snapshot, execute the following command: virsh snapshot-revert <vm_name> <snapshot_name>

This command will roll back the specified <vm_name> virtual machine to the state captured in the <snapshot_name> snapshot.

17. Adjusting CPU and Memory Allocation

To customize the CPU and memory allocation for each virtual machine, you can modify the XML configuration file using the following command:virsh edit <vm_name>

Within the XML file, locate and adjust the <vcpu> and <memory> elements to change the number of virtual CPUs and memory size (in KiB) respectively. For example:

<vcpu placement=’static’>2</vcpu>
<memory unit=’KiB’>2097152</memory>

The above settings allocate 2 virtual CPUs and 2 GB of memory to the specified virtual machine.

18. Configuring CPU Affinity

For optimizing performance, you have the option to fine-tune and configure CPU affinity, which allows you to designate specific physical CPU cores to a virtual machine. This allocation helps in avoiding contention for CPU resources among multiple virtual machines. To configure CPU affinity, make adjustments in the XML configuration file and include the following <cputune> element:

<cputune>
<vcpupin vcpu=’0′ cpuset=’0’/>
<vcpupin vcpu=’1′ cpuset=’1’/>
</cputune>

In this example, the first virtual CPU is pinned to the first physical CPU core, and the second virtual CPU is pinned to the second physical CPU core.

19. Setting Up Network Interfaces

To enable seamless communication between virtual machines and the host system, it is essential to properly configure network interfaces. You can achieve this by editing the XML configuration file and adjusting the <interface> element within the <devices> section. For instance:

<interface type=’bridge’>
<mac address=’52:54:00:12:34:56’/>
<source bridge=’br0’/>
<model type=’virtio’/>
</interface>

In this example, a network interface is configured with a specific MAC address, connected to a bridge named ‘br0’, and utilizes the ‘virtio’ network model to enhance performance.

20. Using VirtIO for Disk and Network Devices

VirtIO is an exceptional high-performance I/O model for virtual machines, offering significant performance improvements for disk and network devices in KVM virtualization. To take advantage of VirtIO for disk devices, you can configure your virtual machine by editing the XML configuration file and modifying the <disk> element:

<disk type=’file’ device=’disk’>
<driver name=’qemu’ type=’qcow2′ cache=’none’ io=’native’/>
<source file=’/var/lib/libvirt/images/new_vm.qcow2’/>
<target dev=’vda’ bus=’virtio’/>
</disk>

In this example, the virtual machine is configured to utilize the VirtIO disk bus, with cache mode set to ‘none’ and I/O mode set to ‘native’ for optimal performance.

Conclusion

KVM stands out as an excellent hypervisor, offering a plethora of management solutions, including command-line interfaces. As we’ve observed, the virsh command-line tool offers a powerful means to interact with virtual machines, storage, and various facets of the virtual environment.

Its extensive array of command-line options empowers efficient management, enabling scripting and automation capabilities for seamless environment control.

Categorized in:

Technology,