virsh is a command line management interface for your virtual machines. Using this program you can manage your local or remote virtual machines.
virsh application comes with several commands and options, but for your day to day work you will be using small set of them.
Let’s walk through the commands you would need for managing your machines.
Obviously first thing we are going to do is connecting to our hypervisor. For connecting our local kvm hypervisor we can use the following command line.
$ virsh -c qemu+ssh://alis@192.168.5.90/system (连接到192.168.5.90上的kvm hypervisor)
Ok. Now we are connected let’s get the list of our virtual machines.
virsh # list
Id Name State
----------------------------------
1 feasiblecrm running
2 reposerver running
6 awordpress running
As you can see from the output above it doesn’t contain the inactive machines. Default behavior of list command is listing only the active machines. For listing all of the machines you can add –all option.
virsh # list --all
Id Name State
----------------------------------
1 feasiblecrm running
2 reposerver running
6 awordpress running
- baseserver shut off
- downloadServer shut off
Now we are connected and we know which machines are registered on hypervisor, so let’s start, stop, reboot some of our machines.
For starting and stopping the virtual machines you can use start and shutdown commands.
virsh # start baseserver
virsh # shutdown baseserver
Rebooting a machine:
virsh # reboot baseserver
Your virtual machines needs to accept ACPI events. (VM需要支持ACPI事件,才能被reboot)
All the commands addressing an machine require the domain (virtual machine name) as parameter.
If you want to do an immediate ungraceful shutdown you can use destroy command
virsh # destroy baseserver
If you want print the xml definition of a virtual machine you can use xmldump command. This command will print the xml machine definition.
virsh # xmldump baseserver
If you want to remove an existing domain you can use undefine command. This command will remove xml definition fom the
virsh # undefine baseserver (undefine = remove)
By default machines that you define will not be started automatically. I want my certain machines to be started automatically. We can use autostart command to mark those machines.
virsh # autostart baseserver (把VM标志成autostart)
I think with this command set you can find your way with virsh application. For more information you can consult the man pages or your use the virsh help command. (请看man页)
+ 转-Chapter 20. Managing guests with virsh
from: http://www.centos.org/docs/5/html/5.2/Virtualization/chap-Virtualization-Managing_guests_with_virsh.html
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 xend will run as a service. You can use virsh to load scripts for the guest machines. (需要root权限;需要xend作为service运行)
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 (从xml创建VM). You can copy existing XML from previously created guests or use the dumpxml option. 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.
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. (suspend时消耗内存)
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. (name和uuid保留,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:
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.
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:
virsh net-autostart [network name] — Autostart a network specified as [network name]
virsh net-create [XML file] — Generates and starts a new network using a preexisting XML file
virsh net-define [XML file] — Generates a new network from a preexisting XML file without starting it
virsh net-destroy [network name] — Destroy a network specified as [network name]
virsh net-name [network UUID] — Convert a specified [network UUID] to a network name
virsh net-uuid [network name — Convert a specified [network name] to a network UUID
virsh net-start [name of an inactive network] — Starts a previously undefined inactive network
virsh net-undefine [name of an inactive network] — Undefine an inactive network