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.
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.
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
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 dumpxmlGuestID>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.
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 .
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.
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.
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.
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.
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.
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 .
To convert a domain name or UUID to a domain id with virsh:
virsh domid [domain-name or domain-uuid]
To convert a domain id or UUID to a domain name with virsh:
virsh domname [domain-id or domain-uuid]
To convert a domain name to a UUID with virsh :
virsh domuuid [domain-id or domain-name]
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]
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.
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:
The running state refers to domains which are currently active on a CPU.
Domains listed as blocked or blocking are presently idle, waiting for I/O, waiting for the hypervisor or dom0 or .
The paused state lists domains which are suspended.
The shutdown state is for domains in the process of shutting down.
Domains in the shutoff state are off and not using system resources.
crashed domains have failed while running and are no longer running.
To display virtual CPU information from a guest with virsh:
virsh vcpuinfo [domain-id, domain-name or domain-uuid]
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.
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.
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.
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:
virsh net-autostart — Autostart a network specified as [network name]
[network name]
virsh net-create — Generates and starts a new network using a preexisting XML file
[XML file]
virsh net-define — Generates a new network from a preexisting XML file without starting it
[XML file]
virsh net-destroy — Destroy a network specified as [network name]
[network name]
virsh net-name — Convert a specified [network UUID] to a network name
[network UUID]
virsh net-uuid — Convert a specified [network name] to a network UUID
[network name
virsh net-start — Starts a previously undefined inactive network
[name of an inactive network]
virsh net-undefine — Undefine an inactive network
[name of an inactive network]