本文内容来自 http://docs.fedoraproject.org/zh-CN/Fedora/12/html/Virtualization_Guide/chap-Virtualization_Guide-Managing_guests_with_virsh.html
virsh 命令快速参考
下表提供所有 virsh 命令行选项的快速参考。
命令
Description
help
打印基本帮助信息。
list
列出所有客户端。
dumpxml
输出客户端 XML 配置文件。
create
从 XML 配置文件生成客户端并启动新客户端。
start
启动未激活的客户端。
destroy
强制客户端停止。
define
为客户端输出 XML 配置文件。
domid
显示客户端 ID。
domuuid
显示客户端 UUID。
dominfo
显示客户端信息。
domname
显示客户端名称。
domstate
显示客户端状态。
quit
退出这个互动终端。
reboot
重新启动客户端。
restore
恢复以前保存在文件中的客户端。
resume
恢复暂停的客户端。
save
将客户端当前状态保存到某个文件中。
shutdown
关闭某个域。
suspend
暂停客户端。
undefine
删除与客户端关联的所有文件。
migrate
将客户端迁移到另一台主机中。
表 15.1. 客户端管理命令
使用以下 virsh 命令管理客户端及管理程序资源:
命令
Description
setmem
为客户端设定分配的内存。
setmaxmem
为管理程序设定内存上限。
setvcpus
修改为客户端分配的虚拟 CPU 数目。
vcpuinfo
显示客户端的虚拟 CPU 信息。
vcpupin
控制客户端的虚拟 CPU 亲和性。
domblkstat
显示正在运行的客户端的块设备统计。
domifstat
显示正在运行的客户端的网络接口统计。
attach-device
使用 XML 文件中的设备定义在客户端中添加设备。
attach-disk
在客户端中附加新磁盘设备。
attach-interface
在客户端中附加新网络接口。
detach-device
从客户端中分离设备,使用同样的 XML 描述作为命令 attach-device。
detach-disk
从客户端中分离磁盘设备。
detach-interface
从客户端中分离网络接口。
表 15.2. 资源管理选项
这些是其它 virsh 选项:
命令
Description
version
显示 virsh 版本
nodeinfo
有关管理程序的输出信息
表 15.3. 其它选项
连接至管理程序
使用 virsh 连接到管理程序会话:
# virsh connect {hostname OR URL}
Where <name> is the machine name of the hypervisor. To initiate a read-only connection, append the above command with -readonly.
创建虚拟机 XML 转储(配置文件)
使用 virsh 输出客户端 XML 配置文件:
# virsh dumpxml {domain-id, domain-name or domain-uuid}
This command outputs the guest's XML configuration file to standard out (stdout). You can save the data by piping the output to a file. An example of piping the output to a file called guest.xml:
# virsh dumpxml GuestID > guest.xml
This file guest.xml can recreate the guest (refer to 编辑客户端配置文件. You can edit this XML configuration file to configure additional devices or to deploy additional guests. Refer to 第 18.1 节 “使用带 virsh 的 XML 配置文件” for more information on modifying files created with virsh dumpxml.
virsh dumpxml 输出示例:
# virsh dumpxml r5b2-mySQL01
<domain type='xen' id='13'>
<name>r5b2-mySQL01</name>
<uuid>4a4c59a7ee3fc78196e4288f2862f011</uuid>
<bootloader>/usr/bin/pygrub</bootloader>
<os>
<type>linux</type>
<kernel>/var/lib/libvirt/vmlinuz.2dgnU_</kernel>
<initrd>/var/lib/libvirt/initrd.UQafMw</initrd>
<cmdline>ro root=/dev/VolGroup00/LogVol00 rhgb quiet</cmdline>
</os>
<memory>512000</memory>
<vcpu>1</vcpu>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<interface type='bridge'>
<source bridge='xenbr0'/>
<mac address='00:16:3e:49:1d:11'/>
<script path='vif-bridge'/>
</interface>
<graphics type='vnc' port='5900'/>
<console tty='/dev/pts/4'/>
</devices>
</domain>
使用配置文件创建客户端
Guests can be created from XML configuration files. You can copy existing XML from previously created guests or use the dumpxml option (refer to 创建虚拟机 XML 转储(配置文件)). To create a guest with virsh from an XML file:
# virsh create configuration_file.xml
编辑客户端配置文件
Instead of using the dumpxml option (refer to 创建虚拟机 XML 转储(配置文件)) guests can be edited either while they run or while they are offline. The virsh edit command provides this functionality. For example, to edit the guest named softwaretesting:
# virsh edit softwaretesting
这样可打开文本编辑器。默认文本编辑器为 $EDITOR shell 参数(默认将其设定为 vi)。
挂起客户端
使用 virsh 挂起客户端:
# virsh suspend {domain-id, domain-name or domain-uuid}
When a guest is in a suspended state, it consumes system RAM but not processor resources. Disk and network I/O does not occur while the guest is suspended. This operation is immediate and the guest can be restarted with the resume (恢复客户端) option.
恢复客户端
使用 virsh 的 resume 选项恢复客户端:
# virsh resume {domain-id, domain-name or domain-uuid}
这个操作是立即生效的,客户端状态将处于 suspend 和 resume 的循环中。
保存客户端
使用 virsh 命令将客户端的当前状态保存到文件中:
# 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. Save is similar to pause, instead of just pausing a guest the present state of the guest is saved.
恢复客户端
Restore a guest previously saved with the virsh save command (保存客户端) using virsh:
# virsh restore filename
这个命令重新启动了保存的客户端,这会需要一段时间。客户端名称和 UUID 都会被保留,但会分配一个新的 id。
关闭客户端
使用 virsh 命令关闭客户端:
# virsh shutdown {domain-id, domain-name or domain-uuid}
修改客户端配置文件中的 on_shutdown 参数可控制重启客户端的行为。
重新启动客户端
使用 virsh 重启客户端:
#virsh reboot {domain-id, domain-name or domain-uuid}
修改客户端配置文件中的 on_reboot 参数控制重启客户端的行为。
强制客户端停止
使用 virsh 强制客户端停止:
# virsh destroy {domain-id, domain-name or domain-uuid}
This command does an immediate ungraceful shutdown and stops the specified guest. Using virsh destroy can corrupt guest file systems . Use the destroy option only when the guest is unresponsive. For para-virtualized guests, use the shutdown option(关闭客户端) instead.
获得客户端域 ID
要获得客户端域 ID:
# virsh domid {domain-name or domain-uuid}
获得客户端域名称
要获得客户端域名称:
# virsh domname {domain-id or domain-uuid}
获得客户端 UUID
要获得客户端全局唯一识别符号(UUID):
# virsh domuuid {domain-id or domain-name}
virsh domuuid 命令输出示例:
# virsh domuuid r5b2-mySQL01
4a4c59a7-ee3f-c781-96e4-288f2862f011
显示客户端信息
使用带客户端域 ID、域名或者 UUID 的 virsh 命令可显示指定客户端的信息:
# virsh dominfo {domain-id, domain-name or domain-uuid}
以下是 virsh dominfo 命令的输出示例:
# virsh dominfo r5b2-mySQL01
id: 13
name: r5b2-mysql01
uuid: 4a4c59a7-ee3f-c781-96e4-288f2862f011
os type:
linux
state: blocked
cpu(s): 1
cpu time:
11.0s
max memory: 512000 kb
used memory: 512000 kb
显示主机信息
要显示主机信息:
# virsh nodeinfo
virsh nodeinfo 命令输出示例:
# virsh nodeinfo
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
这显示了节点信息和支持虚拟化进程的机器。
显示客户端
使用 virsh 显示客户端列表以及当前状态:
# virsh list
其它可用选项包括:
--inactive 选项列出不活动的域(已经被定义但目前不活动的域),以及
--all 选项列出所有客户端。例如:
# virsh list --all
Id Name State
----------------------------------
0 Domain-0 running
1 Domain202 paused
2 Domain010 inactive
3 Domain9600 crashed
将 virsh list 输出结果分类成以下 6 个状态之一(如下)。
running 状态是指目前在 CPU 中活跃的客户端。
列为 blocked 的客户端是被阻断的,且目前没有运行或者无法运行。这是由于客户端正在等待 I/O(传统的等待状态)或者客户端处于睡眠模式。
paused 状态列出暂停的域。如果管理员在 virt-manager、xm pause 或者 virsh suspend 中使用 pause 标记就会出现这种状态。当客户端处于暂停状态时,它仍会消耗内存和其它资源,但无法从管理程序中调度 CPU 资源。
shutdown 状态是用于处于关闭过程中的客户端。向该客户端发送关闭信号并使其温和地停止操作。这可能不适用于所有客户端操作系统,有些操作系统对这些信号没有响应。
处于 dying 状态的域是处于濒死状态,即该域还没有完全关闭或者崩溃。
处于 crashed 状态的客户端是在运行时失败且无法再运行。这个状态只在将客户端配置为崩溃时不重启时出现。
显示虚拟 CPU 信息
使用 virsh 显示客户端中虚拟 CPU 的信息:
# virsh vcpuinfo {domain-id, domain-name or domain-uuid}
virsh vcpuinfo 命令输出示例:
# virsh vcpuinfo r5b2-mySQL01
VCPU: 0
CPU: 0
State: blocked
CPU time: 0.0s
CPU Affinity: yy
配置虚拟 CPU 亲和性
使用物理 CPU 配置虚拟 CPU 的亲和性:
# virsh vcpupin {domain-id, domain-name or domain-uuid} vcpu, cpulist
其中 vcpu 是虚拟 VCPU 号而 cpulist 列出了 CPU 的物理序号。
配置虚拟 CPU 计数
使用 virsh 修改分配给客户端的 CPU 数:
# virsh setvcpus {domain-name, domain-id or domain-uuid} count
新 count 值不能超过创建客户端时指定的数目。
配置内存分配
使用 virsh 修改客户端内存分配:
# virsh setmem {domain-id or domain-name} count
您必须以 KB 为单位指定 count。新 count 值不能超过您创建客户端时指定的数值。大多数客户端操作系统需要不低于 64MB 的值。较高的最大内存值不会影响活跃的客户端,除非新值较小,这会降低可用内存量。
显示客户端块设备信息
使用 virsh domblkstat 显示运行的客户端的块设备统计。
# virsh domblkstat GuestName block-device
显示客户端网络设备信息
virsh domifstat 为运行的客户端显示网络接口统计。
# virsh domifstat GuestName interface-device
使用 virsh 迁移客户端
可使用 virsh 将某个客户端迁移到另一台主机中。将域迁移到另一台主机中。添加 --live 进行实时迁移。migrate 命令接受以下格式的参数:
# virsh migrate --live GuestName DestinationURL
--live 参数是可选的。实时迁移需要添加 --live 参数。
The GuestName parameter represents the name of the guest which you want to migrate.
The DestinationURL parameter is the URL or hostname of the destination system. The destination system must run the same version of Fedora, be using the same hypervisor and have libvirt running.
Once the command is entered you will be prompted for the root password of the destination system.
管理虚拟网络
这部分包括使用 virsh 管理虚拟网络。要列出虚拟网络:
# virsh net-list
这个命令产生的输出类似如下:
# virsh net-list
Name State Autostart
-----------------------------------------
default active yes
vnet1
active yes
vnet2
active yes
要查看某一特定虚拟网络的信息:
# virsh net-dumpxml NetworkName
这以 XML 格式显示指定虚拟网络的信息:
# 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>
其它用来管理虚拟网络的 virsh 命令:
virsh net-autostart network-name — 自动启动指定为 network-name 的网络。
virsh net-create XMLfile — 使用现有 XML 文件创建并启动新网络。
virsh net-define XMLfile — 使用现有 XML 文件创建新网络设备但不启动。
virsh net-destroy network-name — 销毁指定为 network-name 的网络。
virsh net-name networkUUID — 将指定的 networkUUID 转换为网络名称。
virsh net-uuid network-name — 将指定的 network-name 转换为网络 UUID。
virsh net-start nameOfInactiveNetwork — 启动不活跃的网络。
virsh net-undefine nameOfInactiveNetwork — 删除不活跃网络的定义。