Latest update

6/recent/ticker-posts

KVM Cheat Sheet of virsh commands

 

 KVM cheat sheet 3

KVM is an excellent choice as a hypervisor to run in the home lab environment or production. After all, Amazon runs a variant of KVM for its AWS EC2 environment. KVM is free and open-source and available in all the latest Linux distros for the most part. If you are running KVM, it includes robust command line tools allowing you to manage the solution from the command line. Let’s look at a KVM cheat sheet of virsh commands to manage KVM from the command line as a management user interface.

 

Installing KVM

Installing the KVM hypervisor in Linux is straightforward. Below, we are installing the required components in Ubuntu 22.04.

sudo apt install libvirt-clients libvirt-daemon-system libvirt-daemon virtinst bridge-utils qemu qemu-kvm

You can check the status of your KVM installation by running the kvm-ok command.

Creating New Virtual Machines

To create a new guest virtual machine, you’ll need a disk image to store the guest operating system. The default location for the disk image is typically stored in the /var/lib/libvirt/images directory.

You can create a new disk image using the command line. You can create a new disk image using the qemu-img command. For instance, to create a 10GB disk image, you can run the following command:

qemu-img create -f qcow2 /var/lib/libvirt/images/new_vm.qcow2 10G

If you want to see all the available commands with qemu-img, you can run the qemu-img –help command.

With the disk image, you can create a new virtual machine using the virt-install command. This command requires several parameters, such as the virtual machine’s name, the amount of memory, and the disk image path. For example:

virt-install --name new_vm --memory 2048 --disk /var/lib/libvirt/images/new_vm.qcow2 --cdrom /path/to/your/iso-file.iso --os-type linux --os-variant ubuntu20.04 --network bridge:virbr0 --graphics vnc --noautoconsole

This command creates a new virtual machine called “new_vm” with 2GB of memory, using the disk image created earlier and an ISO file for the guest operating system installation.

Managing Guest Virtual Machines with Virsh

 

The virsh command line tool is a great way to manage and configure kvm virtual machines. The virtual shell or virsh program is the main interface for managing virsh guest domains. What types of tasks can you perform with KVM in regards to VMs?

  • listing

  • starting

  • stopping

  • deleting

You can view the options for the virsh command with the virsh help command.

List virtual machines

To list all defined virtual machines, you can use the guest virsh command:

virsh list --all 

This command will display a table with the virtual machine name, ID, and current state.

Starting virtual machines

To start a virtual machine, you can use the command:

virsh start <vm_name> 

Where <vm_name> is the name of the virtual machine you want to start. For example:

virsh start new_vm

Stopping virtual machines

To stop a virtual machine, use the command:

virsh shutdown <vm_name> 

If you need to stop a virtual machine forcefully, you can use the virsh destroy <vm_name> command instead.

Configuring Virtual Machines with XML Files

 

Each virtual machine has an associated XML configuration file. You can view the configuration of a virtual machine using the command:

virsh dumpxml <vm_name>

To edit the XML configuration file, use the command:

virsh edit <vm_name>

This command will open the XML file in your default text editor, allowing you to modify the virtual machine settings, such as memory, CPU, and network configurations.

Managing Storage and Networks

Storage in KVM is managed through storage pools and storage volumes. A storage pool is a collection of storage devices, while a storage volume is an individual storage unit within a pool. You can use virsh commands to create, list, and delete storage pools and volumes.

Create a new storage pool

To create a new storage pool, use the command:

virsh pool-create <pool_xml> 

Where <pool_xml> is the path to an XML file containing the storage pool configuration. You can also use the command:

virsh pool-define-as

This defines new storage pool without creating it.

List storage pools

To list all storage pools, use the command:

virsh pool-list

This command will display a table with the pool name, state, and other relevant information.

List storage volumes

You can also list the storage volumes in a pool using the command:

virsh vol-list <pool_name>

Create a new storage volume

To create a new storage volume within a pool, use the command:

virsh vol-create-as <pool_name> <vol_name> <size>

For example:

virsh vol-create-as default new_volume 10G

This command creates a new 10GB storage volume named “new_volume” in the “default” storage pool.

 

In addition to storage, KVM allows you to manage virtual networks. Virtual networks provide connectivity between virtual machines and the host system. To create a new virtual network, use the command:

virsh net-define <network_xml> 

The above command <network_xml> is the path to an XML file containing the network configuration.

List the virtual networks

To list all virtual networks, use the command:

virsh net-list 

This command will display a table with the network name, state, and other relevant information.

Taking Snapshots of Virtual Machines

Snapshots are an essential feature of virtualization platforms, allowing you to capture the current state of a virtual machine and revert back to it later. You can use virsh commands to create, list, and delete snapshots.

To create a new snapshot, use the virsh snapshot-create-as <vm_name> <snapshot_name> command. For example:

virsh snapshot-create-as new_vm snapshot1

This command creates a new snapshot named “snapshot1” for the “new_vm” virtual machine.

List virtual machine snapshots

To list all virtual machine snapshots, use the command:

virsh snapshot-list <vm_name>

This command will display a table with the snapshot name, creation time, and current state.

Revert a virtual machine snapshot

To revert a virtual machine to a specific snapshot, use the command:

virsh snapshot-revert <vm_name> <snapshot_name>

Optimizing Virtual Machine Performance

When working with KVM virtual machines, optimizing their performance for your home lab or test environment is essential. Here are some tips and tricks to help you get the most out of your KVM virtual machines.

Adjusting CPU and Memory Allocation

You can adjust the amount of CPU and memory allocated to each virtual machine by editing the XML configuration file with the command:

 virsh edit <vm_name> 

Within the XML file, you can modify 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>

These settings allocate 2 virtual CPUs and 2 GB of memory to the virtual machine.

Configuring CPU Affinity

 

In terms of performance, you can tweak and configure CPU affinity. CPU affinity allows you to assign specific physical CPU cores to a virtual machine, which can help prevent contention for CPU resources between multiple virtual machines. To configure CPU affinity, edit the XML configuration file and add the following <cputune> element:

<cputune> <vcpupin vcpu='0' cpuset='0'/> <vcpupin vcpu='1' cpuset='1'/> </cputune>

This example pins the first virtual CPU to the first physical CPU core and the second virtual CPU to the second physical CPU core.

Setting Up Network Interfaces

Properly configuring network interfaces ensures optimal communication between virtual machines and the host system. To configure a network interface, edit the XML configuration file and modify the <interface> element within the <devices> section. For example:

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

This example configures a network interface with a specific MAC address, connects it to a bridge named ‘br0’, and uses the ‘virtio’ network model for better performance.

Using VirtIO for Disk and Network Devices

VirtIO is a high-performance I/O model for virtual machines. By using VirtIO for disk and network devices, you can significantly improve the performance of your KVM virtual machines. To configure a virtual machine to use VirtIO for disk devices, edit the XML configuration file and modify 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>

This example configures the virtual machine to use the VirtIO disk bus and sets the cache mode to ‘none’ and the I/O mode to ‘native’ for better performance.

Accessing Virtual Machine Consoles

To access the console of a running virtual machine, you can use the command:

virsh console <vm_name> 

This command will connect you to the guest console, allowing you to interact with the guest operating system.

Automating Virtual Machine Management

You can automate virtual machine management tasks using virsh and shell scripting for more advanced users. Combining virsh commands in a script allows you to quickly perform complex tasks, such as creating multiple virtual machines, configuring network settings, or managing storage volumes.

 

Wrapping up

KVM is a great hypervisor with many solution management options, including from the command line. As we have seen, the virsh command line tool provides a great way to interact with virtual machines, storage, and other aspects of the virtual environment. It provides many command line possibilities for managing the environment, including scripting and automation.

Post a Comment

0 Comments