Tip

Using virsh to streamline KVM management

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.

    Requires Free Membership to View

More on KVM management with virsh
Managing virtual machines in Red Hat Enterprise Linux 5.4  

Red Hat Enterprise Linux virtualization architecture
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.

Virsh list
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

Id: 1

Name: sles10sp3

UUID: 0ecd18d7-dec7-0668-9fc4-9bf2fba1e1c7

OS Type: hvm

State: running

CPU(s): 1

CPU time: 172.4s

Max memory: 524288 kB

Used memory: 524288 kB

Autostart: disable

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 . With a state file, you can restore a VM using virsh restore . If you make a configuration mistake, for example, these commands can revert a VM to a previous, working state.

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

NAME

setmem - change memory allocation

SYNOPSIS/p>

setmem domain kilobytes

DESCRIPTION

Change the current memory allocation in the guest domain.

OPTIONS

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.


This was first published in August 2010

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.