Using virsh commands to manage KVM virtual machines

Linux admins have a wealth of management options, but versatile virsh commands may be the best options for managing KVM virtual machines.

There are many management options available if you're using KVM on top of Linux. Managing virtual machines with the Linux Virtual Machine Manager is the easiest way, but it requires a graphical interface. If you need to work from a nongraphical environment, virsh commands offer all you need. In this article you'll learn how to get started with this versatile utility.

Before exploring the shell environment that virsh offers, you should know something about managing KVM virtual machines (VMs) in general. Linux uses libvirt, a virtualization application programming interface to provide a generic interface to manage VMs. Libvirt supports KVM, Xen and Linux containers (LXC). One of the most useful commands in libvirt is virsh.

Virsh command basics

If you just type virsh into the Linux shell, you'll open the virsh shell, from which you can use virtualization-specific commands. For example, from within the virsh shell, type list to get a list of all active VMs. When you're finished managing your VMs, type quit to return to a normal Linux shell.

You can also use the specific virsh command options directly from the Linux shell. So, instead of opening a virsh shell and typing the list command, you could also type virsh list from a Linux shell environment.

     [root@hnl ~]# virsh
setlocale: No such file or directory
Welcome to virsh, the virtualization interactive terminal.

Type: 'help' for help with commands
    'quit' to quit

virsh # list

 Id Name         State
----------------------------------
 3 vmdelft       running

virsh # quit

[root@hnl ~]# virsh list
setlocale: No such file or directory

 Id Name         State
----------------------------------
 3 vmdelft       running

The virsh command can be used for multiple purposes, including starting and shutting down VMs. To close a running VM, use virsh shutdown vmname, which will send a shutdown command to the VM's operating system. Alternatively, you can use virsh destroy vmname, which is like pulling the power plug and forces a VM to stop immediately.

After destroying a VM, you won't see it in the list of VMs any longer because the command also completely removes it  from libvert's  VM database. If you destroy a VM, but later want to get information about it,  use the virsh list -all command to obtain information about all VMs based on configuration in the directory /etc/libvirt/qemu. After stopping or destroying a VM, you can start it again using the virsh start vmname command.

To get an overview of all the virsh commands, use virsh help, which will show multiple virsh commands. The commands fall into the following categories:

  • Domain: Includes commands related to VM management.
  • Monitor: Commands used to display monitoring information about a domain.
  • Host: Commands related to host management.
  • Interface: Manage network interfaces with these commands.
  • Filter: Commands that help managing network filters that allow you to filter traffic coming from and going to specific VMs.
  • Networking: Commands to manage the available networks and create new networks if required.
  • Nodedev: Commands for managing node devices. These are devices on the host that need to be passed through to the VMs.
  • Secret: Commands for managing secret keys, which are used to prevent nodes from using hardware they are not allowed to use.
  • Snapshot: Use the snapshot command for managing snapshots.
  • Pool: Commands to create and manage storage pools, which are collections of all available storage.
  • Volume: Commands for managing storage devices within a storage pool.

The "domain" commands are especially useful for day to day administration work. In this category, you'll find many useful commands, like setmaxmem (which allows you to set the maximum memory limit for a VM), setmem (which allows you to change the current memory allocation) and vcpupin (which allows you to link a virtual CPU to a physical CPU on the host).

If, for instance, you want to change the maximum amount of memory allocated to a domain, it's a good idea to start with the virsh dominfo vmname command, which shows the properties currently in use:

     [root@hnl qemu]# virsh dominfo vmdelft
Id:       2
Name:      vmdelft
UUID:      639096c3-c731-01ac-f3cd-47f12147099f
OS Type:    hvm
State:     running
CPU(s):     2
CPU time:    88.9s
Max memory:   1048576 kB
Used memory:  1048576 kB
Persistent:   yes
Autostart:   disable
Managed save:  no
Security model: selinux
Security DOI:  0
Security label: system_u:system_r:svirt_t:s0:c817,c885 (enforcing)

This command shows that the current memory allocation is set to 1,048,576 kilobytes of RAM. To double the maximum amount of RAM the VM is able to use, you can use the virsh setmem vmname 2097152 command. This command only works if the VM is not currently active. After increasing the maximum amount of memory, you can now use virsh setmem vmname 1572864 to set the amount of RAM currently allocated to the VM to 1.5 GB. To verify that it all worked, use virsh dominfo vmname again, and you will see the changed memory parameters.

The virsh command allows you to manage any property of a VM in either KVM, Xen or LXC. For administrators that are responsible for a KVM VMs, virsh provides a useful management tool, especially because it can also easily be scripted.

This was first published in September 2013

Dig deeper on Open source virtualization

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

2 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchVMware

SearchWindowsServer

SearchCloudComputing

SearchVirtualDesktop

SearchDataCenter

Close