When it comes to Kernel-based Virtual Machine (KVM) management, the learning curve for virsh is steep. But the...
alternative, virt-manager, is riddled with shortcomings.
Virt-manager must run within your environment, which wastes resources and provides an attack vector for unscrupulous individuals. Another pitfall of virt-manager is that you must run remote sessions on a graphical workstation to manage virtual machines (VMs) on a server.
Using virsh instead, you can execute a variety of administrative tasks through the command line. This article outlines the most important virsh commands.
For virsh beginners, it's a good practice to list the available VMs. To do so, enter the virsh list command. Here's an example of a virsh list output.
sander@boston:~$ sudo virsh list
Id Name State
1 sles10sp3 running
Unfortunately, this command doesn't provide many details. If you require more information, enter virsh dominfo. The following information about the sles10sp3 machine was retrieved by virsh dominfo:
sander@boston:~$ sudo virsh dominfo sles10sp3
OS Type: hvm
CPU time: 172.4s
Max memory: 524288 kB
Used memory: 524288 kB
Security model: apparmor
Security DOI: 0
Security label: libvirt-0ecd18d7-dec7-0668-9fc4-9bf2fba1e1c7 (enforcing)
VM shutdowns and restarts with virsh
Shutting down VMs is a common virtualization task. In KVM environments, you can use the following commands to facilitate this action:
- Virsh shutdown: This command terminates all active processes and shuts down a VM.
- Virsh destroy: This entry forces a VM to shut down and may cause data loss. It's akin to pulling the power plug from a physical machine.
To restart an offline VM, use the virsh start command, followed by the name of the guest machine.
Suspending, resuming and restoring VMs with virsh
Terminating a VM isn't always ideal. There are occasions where you may want to freeze a VM's current state and quickly reanimate it later. This approach is particularly useful for VMs that have numerous applications or windows open but need to be brought down.
In those situations, enter virsh suspend, followed by a VM name. Later, if you want to bring the VM online, use the virsh resume command.
It's also important to back up and restore guest machines. To save a VM's current state, use virsh save
Learning more about virsh commands
The previous commands are just a sampling of virsh's usefulness and versatility. If you want to learn more about virsh, enter virsh help, which lists every virsh command.
Imagine that you noticed the virsh setmem command from the help list, and you want to more information about it. Enter virsh help setmem. Here's the output:
sander@boston:~$ sudo virsh help setmem
setmem - change memory allocation
setmem domain kilobytes
Change the current memory allocation in the guest domain.
domain domain name, id or uuid
kilobytes number of kilobytes of memory
By exploring the command list, you can get a solid grasp of virsh's capabilities. For complex commands, however, you may need to visit outside resources for more in-depth explanations.
Sander van Vugt is an independent trainer and consultant based in the Netherlands. Van Vugt is an expert in Linux high availability, virtualization and performance and has completed several projects that implement all three. He is also the writer of various Linux-related books, such as Beginning the Linux Command Line, Beginning Ubuntu Server Administration and Pro Ubuntu Server Administration.