virsh 管理KVM
来源:互联网 发布:守望先锋卡a卡优化设置 编辑:程序博客网 时间:2024/05/04 04:34
virsh
是用来管理客户端及其管理程序的命令行界面工具。virsh
工具是构建在 libvirt
管理 API 上,可作为 xm 命令和图形客户端管理程序(virt-manager
)的替代工具使用。非特权用户只能以只读模式使用 virsh
。您可使用 virsh
为客户端机器执行脚本。virsh 命令快速参考
下表提供所有 virsh 命令行选项的快速参考。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
命令管理客户端及管理程序资源: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
选项:version
显示 virsh
版本nodeinfo
有关管理程序的输出信息表 15.3. 其它选项
连接至管理程序
使用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
.创建虚拟机 XML 转储(配置文件)
使用virsh
输出客户端 XML 配置文件:# 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
挂起客户端:# 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
选项恢复客户端:# 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
命令将客户端的当前状态保存到文件中:# 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.恢复客户端
Restore a guest previously saved with thevirsh save
command (保存客户端) using virsh
:# 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
命令关闭客户端:# 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
重启客户端:#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
强制客户端停止:# 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.获得客户端域 ID
要获得客户端域 ID:# 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>
获得客户端 UUID
要获得客户端全局唯一识别符号(UUID):# 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-mySQL014a4c59a7-ee3f-c781-96e4-288f2862f011
显示客户端信息
使用带客户端域 ID、域名或者 UUID 的virsh
命令可显示指定客户端的信息:# 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-mySQL01id: 13name: r5b2-mysql01uuid: 4a4c59a7-ee3f-c781-96e4-288f2862f011os type: linuxstate: blockedcpu(s): 1cpu time: 11.0smax memory: 512000 kbused memory: 512000 kb
显示主机信息
要显示主机信息:# virsh nodeinfo
virsh nodeinfo
命令输出示例:# virsh nodeinfoCPU model x86_64CPU (s) 8CPU frequency 2895 MhzCPU socket(s) 2 Core(s) per socket 2Threads per core: 2Numa cell(s) 1Memory 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 <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-mySQL01VCPU: 0CPU: 0State: blockedCPU time: 0.0sCPU Affinity: yy
配置虚拟 CPU 亲和性
使用物理 CPU 配置虚拟 CPU 的亲和性:# 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 的物理序号。配置虚拟 CPU 计数
使用virsh
修改分配给客户端的 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
修改客户端内存分配:# virsh setmem {domain-id or domain-name} count
您必须以 KB 为单位指定 count。新 count 值不能超过您创建客户端时指定的数值。大多数客户端操作系统需要不低于 64MB 的值。较高的最大内存值不会影响活跃的客户端,除非新值较小,这会降低可用内存量。
显示客户端块设备信息
使用virsh domblkstat
显示运行的客户端的块设备统计。# 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
为运行的客户端显示网络接口统计。# 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
管理虚拟网络。要列出虚拟网络:# virsh net-list
这个命令产生的输出类似如下:
# virsh net-listName 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
0 0
- virsh 管理KVM
- virsh 管理KVM
- Kvm管理工具之virsh
- KVM-Virsh指令详解
- KVM之Virsh工具使用
- kvm (virsh) save和restore
- qemu/kvm/qemu-kvm/virsh的区别
- 为KVM客户机添加virsh console支持
- ubuntu上virsh+kvm安装虚拟机
- qemu kvm libvirt virsh之间的关系!
- 为KVM客户机添加virsh console支持
- 使用libvirt管理kvm(virsh篇)
- 使用libvirt管理kvm(virsh篇)
- qemu,kvm,vir-manager,virsh,libvirt相关内容
- 为KVM客户机添加virsh console支持
- Virsh管理kvm虚拟机认证和加密
- (转载)qemu/kvm/qemu-kvm/virsh的区别
- (转载)qemu/kvm/qemu-kvm/virsh的区别
- while 循环3
- extjs做判断items:[{ xtype:'textfield', fieldLabel:'登录帐号', value: name, }] 已经得到了value的值
- Python学习之路(一)
- switch
- Java继承和多态===Java instanceof 运算符
- virsh 管理KVM
- H323协议和polycom,华为视频会议终端调试出现的问题
- 单片机论坛 学习单片机用
- Atom编辑器打代码特效傻瓜安装方法(啪啪啪)
- The HR user creates a stand-alone procedure as follows and grants the EXECUTE privilege on the proce
- HTML开发过程中一些注意事项
- Android数据存储
- if...else
- JQuery学习笔记(一):basic knowledge