Chapter 20. Managing guests with virsh

Chapter 20. Managing guests with virsh

virsh is a command line interface tool for managing guests and the hypervisor.

The virsh tool is built on the libvirt management API and operates as an alternative to the xm tool and the graphical guest Manager(virt-manager). Unprivileged users can employ this utility for read-only operations. If you plan on running the xend, you should enable xend to run as a service. After modifying the respective configuration file, reboot the system, and xendwill run as a service. You can use virsh to load scripts for the guest machines.

Connecting to the hypervisor

To initiate a hypervisor session with virsh :

virsh connect <name>

Where <name> is the machine name of the hypervisor. If you want to initiate a read-only connection, append the above command with -readonly.

Creating a guest

Guests can be created from XML configuration files. You can copy existing XML from previously created guests or use the dumpxml option(refer to Creating a virtual machine XML dump(configuration file)). To create a guest with virsh from an XML file:

virsh create configuration_file.xml

Creating a virtual machine XML dump(configuration file)

To perform a data dump for an existing guest with virsh:

virsh dumpxml [domain-id, domain-name or domain-uuid]

This command outputs the domain information (in XML) to stdout. You save the data by piping the output to a file.

virsh dumpxml GuestID > guest.xml

The file guest.xml can then be used to recreate the guest (refer to Creating a guest. You can edit this XML configuration file to configure additional devices or to deploy additional guests. Refer to Section 27.1, “Using XML configuration files with virsh for more information on modifying files created with virsh dumpxml.

Suspending a guest

To suspend a guest with virsh:

virsh suspend [domain-id, domain-name or domain-uuid]

When a domain is in a suspended state, it still consumes system RAM. Disk and network I/O will not occur while the guest is suspended. This operation is immediate and the guest must be restarted with the resume option .

Resuming a guest

To restore a suspended guest with virsh using the resume option:

virsh resume [domain-id, domain-name or domain-uuid]

This operation is immediate and the guest parameters are preserved for suspend and resume operations.

Saving a guest

To save the current state of a guest to a file using the virsh command :

virsh save [domain-name] [domain-id or domain-uuid] [filename]

This stops the guest you specify and saves the data to a file, which may take some time given the amount of memory in use by your guest. You can restore the state of the guest with the restore option.

Restoring a guest

To restore a guest that you previously saved with the virsh save option using the virsh command:

virsh restore [filename]

This restarts the saved guest, which may take some time. The guest's name and UUID are preserved but are allocated for a new id.

Shutting Down a guest

To shut down a guest using the virsh command:

virsh shutdown [domain-id, domain-name or domain-uuid]

You can control the behavior of the rebooting guest by modifying the on_shutdown parameter of the xmdomain.cfg file.

Rebooting a guest

To reboot a guest using virsh command:

virsh reboot [domain-id, domain-name or domain-uuid]

You can control the behavior of the rebooting guest by modifying the on_reboot parameter of the xmdomain.cfg file.

Terminating a guest

To terminate, destroy or delete guest use the virsh command with destroy:

virsh destroy [domain-id, domain-name or domain-uuid]

This command does an immediate ungraceful shutdown and stops any guest domain sessions (which could potentially lead to file corrupted file systems still in use by the guest). You should use the destroy option only when the guest is non-responsive. For a para-virtualized guest, you should use the shutdown option .

Converting a Domain Name to a Domain ID

To convert a domain name or UUID to a domain id with virsh:

virsh domid [domain-name or domain-uuid]

Converting a Domain ID to a Domain Name

To convert a domain id or UUID to a domain name with virsh:

virsh domname [domain-id or domain-uuid]

Converting a Domain Name to a UUID

To convert a domain name to a UUID with virsh :

virsh domuuid [domain-id or domain-name]

Displaying guest Information

Using virsh with the guest's domain ID, domain name or UUID you can display information on the specified guest:

virsh dominfo [domain-id, domain-name or domain-uuid]

Displaying node information

To display node information virsh :

virsh nodeinfo

The outputs displays something similar to:

CPU model                    x86_64
CPU (s)                      8
CPU frequency                2895 Mhz
CPU socket(s)                2      
Core(s) per socket           2
Threads per core:            2
Numa cell(s)                 1
Memory size:                 1046528 kb

This displays the node information and the machines that support the virtualization process.

Displaying the guests

To display the guest list and their current states with virsh:

virsh list [ --inactive  |  --all]

The --inactive option lists inactive domains (domains that have been defined but are not currently active). The --all domain lists all domains, whether active or not. Your output should resemble the this example:

 Id Name                 State
----------------------------------
  0 Domain-0             running
  1 Domain202            paused
  2 Domain010            inactive
  3 Domain9600           crashed

Using virsh list will output domains with the following states:

Displaying virtual CPU information

To display virtual CPU information from a guest with virsh:

virsh vcpuinfo [domain-id, domain-name or domain-uuid]

Configuring virtual CPU affinity

To configure the affinity of virtual CPUs with physical CPUs:

virsh vcpupin [domain-id, domain-name or domain-uuid] [vcpu] , [cpulist]

Where [vcpu] is the virtual VCPU number and [cpulist] lists the physical number of CPUs.

Configuring virtual CPU count

To modify a domain's number of CPUs with virsh:

virsh setvcpus [domain-name, domain-id or domain-uuid] [count]

You cannot increase the count above the amount you specified when you created the guest.

Configuring memory allocation

To modify a guest's memory allocation with virsh :

virsh setmem [domain-id or domain-name]  [count]

You must specify the [count] in kilobytes. The new count value cannot exceed the amount you specified when you created the guest. Values lower than 64 MB are unlikely to work with most guest operating systems. A higher maximum memory value will not affect the an active guest unless the new value is lower which will shrink the available memory usage.

Managing virtual networks

This section covers managing virtual networks with the virsh command. To list virtual networks:

virsh net-list

This command generates output similar to:

[root@domain ~]# virsh net-list
Name                 State      Autostart
-----------------------------------------
default              active     yes      
vnet1	             active     yes      
vnet2	             active     yes

To view network information for a specific virtual network:

virsh net-dumpxml [vnet name]

This displays information about a specified virtual network in XML format:

	
# virsh net-dumpxml vnet1
<network>
  <name>vnet1</name>
  <uuid>98361b46-1581-acb7-1643-85a412626e70</uuid>
  <forward dev='eth0'/>
  <bridge name='vnet0' stp='on' forwardDelay='0' />
  <ip address='192.168.100.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.100.128' end='192.168.100.254' />
    </dhcp>
  </ip>
</network>

Other virsh commands used in managing virtual networks are:


Note: This documentation is provided {and copyrighted} by Red Hat®, Inc. and is released via the Open Publication License. The copyright holder has added the further requirement that Distribution of substantively modified versions of this document is prohibited without the explicit permission of the copyright holder. The CentOS project redistributes these original works (in their unmodified form) as a reference for CentOS-5 because CentOS-5 is built from publicly available, open source SRPMS. The documentation is unmodified to be compliant with upstream distribution policy. Neither CentOS-5 nor the CentOS Project are in any way affiliated with or sponsored by Red Hat®, Inc.