virsh
是用来管理客户端及其管理程序的命令行界面工具。
virsh
工具是构建在
libvirt
管理 API 上,可作为
xm 命令和图形客户端管理程序(
virt-manager
)的替代工具使用。非特权用户只能以只读模式使用
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
选项:
# virsh connect <span class="parameter" style="font-style:italic"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; font-weight:bold; display:inline; word-wrap:break-word">{hostname OR URL}</code></span>
Where
<name>
is the machine name of the hypervisor. To initiate a read-only connection, append the above command with
-readonly
.
# virsh dumpxml <span class="parameter" style="font-style:italic"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; font-weight:bold; display:inline; word-wrap:break-word">{domain-id, domain-name or domain-uuid}</code></span>
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 <span class="replaceable" style="color:#444444;"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; white-space:nowrap; font-weight:bold">GuestID</code></span> > <span class="replaceable" style="color:#444444;"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; white-space:nowrap; font-weight:bold">guest.xml</code></span>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 thedumpxml
option (refer to
创建虚拟机 XML 转储(配置文件)). To create a guest with
virsh
from an XML file:
# virsh create <span class="replaceable" style="color:#444444;"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; white-space:nowrap; font-weight:bold">configuration_file.xml</code></span>
编辑客户端配置文件
Instead of using thedumpxml
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 <span class="parameter" style="font-style:italic"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; font-weight:bold; display:inline; word-wrap:break-word">softwaretesting</code></span>
这样可打开文本编辑器。默认文本编辑器为
$EDITOR
shell 参数(默认将其设定为
vi
)。
# virsh suspend <span class="parameter" style="font-style:italic"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; font-weight:bold; display:inline; word-wrap:break-word">{domain-id, domain-name or domain-uuid}</code></span>
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 <span class="parameter" style="font-style:italic"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; font-weight:bold; display:inline; word-wrap:break-word">{domain-id, domain-name or domain-uuid}</code></span>
这个操作是立即生效的,客户端状态将处于
suspend
和
resume
的循环中。
# virsh save <span class="parameter" style="font-style:italic"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; font-weight:bold; display:inline; word-wrap:break-word">{domain-name, domain-id or domain-uuid} filename</code></span>
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.
# virsh restore <span class="parameter" style="font-style:italic"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; font-weight:bold; display:inline; word-wrap:break-word">filename</code></span>
这个命令重新启动了保存的客户端,这会需要一段时间。客户端名称和 UUID 都会被保留,但会分配一个新的 id。
# virsh shutdown <span class="parameter" style="font-style:italic"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; font-weight:bold; display:inline; word-wrap:break-word">{domain-id, domain-name or domain-uuid}</code></span>
修改客户端配置文件中的
on_shutdown
参数可控制重启客户端的行为。
#virsh reboot <span class="parameter" style="font-style:italic"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; font-weight:bold; display:inline; word-wrap:break-word">{domain-id, domain-name or domain-uuid}</code></span>
修改客户端配置文件中的
on_reboot
参数控制重启客户端的行为。
# virsh destroy <span class="parameter" style="font-style:italic"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; font-weight:bold; display:inline; word-wrap:break-word">{domain-id, domain-name or domain-uuid}</code></span>
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.
# virsh domid <span class="parameter" style="font-style:italic"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; font-weight:bold; display:inline; word-wrap:break-word">{domain-name or domain-uuid}</code></span>
# virsh domname <span class="parameter" style="font-style:italic"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; font-weight:bold; display:inline; word-wrap:break-word">{domain-id or domain-uuid}</code></span>
# virsh domuuid <span class="replaceable" style="color:#444444;"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; white-space:nowrap; font-weight:bold">{domain-id or domain-name}</code></span>
virsh domuuid
命令输出示例:
# virsh domuuid r5b2-mySQL01 4a4c59a7-ee3f-c781-96e4-288f2862f011
# virsh dominfo <span class="parameter" style="font-style:italic"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; font-weight:bold; display:inline; word-wrap:break-word">{domain-id, domain-name or domain-uuid}</code></span>
以下是
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 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
状态的客户端是在运行时失败且无法再运行。这个状态只在将客户端配置为崩溃时不重启时出现。
# virsh vcpuinfo <span class="replaceable" style="color:#444444;"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; white-space:nowrap; font-weight:bold">{domain-id, domain-name or domain-uuid}</code></span>
virsh vcpuinfo
命令输出示例:
# virsh vcpuinfo r5b2-mySQL01 VCPU: 0 CPU: 0 State: blocked CPU time: 0.0s CPU Affinity: yy
# virsh vcpupin <span class="replaceable" style="color:#444444;"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; white-space:nowrap; font-weight:bold">{domain-id, domain-name or domain-uuid} vcpu, cpulist</code></span>
其中
vcpu
是虚拟 VCPU 号而
cpulist
列出了 CPU 的物理序号。
# virsh setvcpus <span class="parameter" style="font-style:italic"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; font-weight:bold; display:inline; word-wrap:break-word">{domain-name, domain-id or domain-uuid} count</code></span>
新
count
值不能超过创建客户端时指定的数目。
# virsh setmem {domain-id or domain-name} count
您必须以 KB 为单位指定
count。新 count 值不能超过您创建客户端时指定的数值。大多数客户端操作系统需要不低于 64MB 的值。较高的最大内存值不会影响活跃的客户端,除非新值较小,这会降低可用内存量。
# virsh domblkstat <span class="replaceable" style="color:#444444;"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; white-space:nowrap; font-weight:bold">GuestName block-device</code></span>
# virsh domifstat <span class="replaceable" style="color:#444444;"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; white-space:nowrap; font-weight:bold">GuestName interface-device </code></span>
使用 virsh 迁移客户端
可使用virsh
将某个客户端迁移到另一台主机中。将域迁移到另一台主机中。添加 --live 进行实时迁移。
migrate
命令接受以下格式的参数:
# virsh migrate --live <span class="parameter" style="font-style:italic"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; font-weight:bold; display:inline; word-wrap:break-word">GuestName DestinationURL</code></span>
--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 net-list
这个命令产生的输出类似如下:
# virsh net-list Name State Autostart ----------------------------------------- default active yes vnet1 active yes vnet2 active yes
要查看某一特定虚拟网络的信息:
# virsh net-dumpxml <span class="replaceable" style="color:#444444;"><code style="font-family:'liberation mono','bitstream vera mono','dejavu mono',monospace; white-space:nowrap; font-weight:bold">NetworkName</code></span>
这以 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
— 使用现有 XML 文件创建并启动新网络。XMLfile
-
virsh net-define
— 使用现有 XML 文件创建新网络设备但不启动。XMLfile
-
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