QEMU Guest Agent,简称 QGA,是运⾏在 QEMU 虚拟机内部的⼀个守护程序 qemu-guest-agent.service,类似于 VMware tools,主要⽤于辅助 Hypervisor 实现对 Guest 的管理。
官⽅⽹站:
https://wiki.qemu.org/Features/GuestAgent
https://wiki.libvirt.org/page/Qemu_guest_agent
QEMU 通过建⽴ Host 和 Guest 之间的⼀个数据通道(channel)来实现两者之间的通讯功能,继⽽增强 Host 对 Guest 的控制能⼒。这种通讯⽅式是不依赖与⽹络的,⽽是依赖于 virtio-serial(默认⾸选⽅式)或者 isa-serial,在 Domain XML ⽂件中称为 org.qemu.guest_agent.0。QEMU 提供了串⼝设备的模拟及数据交换的通道,最终呈现出来的是⼀个串⼝设备(Guest)和⼀个 UNIX Socket ⽂件(Host)。
<channel type=‘unix‘> <source mode=‘bind‘
path=‘/var/lib/libvirt/qemu/org.qemu.guest_agent.0.instance-00000011.sock‘/>
<target type=‘virtio‘ name=‘org.qemu.guest_agent.0‘/>
<address type=‘virtio-serial‘ controller=‘0‘ bus=‘0‘ port=‘1‘/>
</channel>
QGA 通过读写串⼝设备与 Host UNIX Socket 进⾏交互,在宿主机上则通过常规的⽅式对 UNIX Socket⽂件进⾏读写,最终实现两者的交互,交互的协议与 qmp(QEMU Monitor Protocol)相同,简单的说就是使⽤ JSON 格式进⾏数据交换。串⼝设备的速率通常都较低,所以⽐较适合⼩数据量的交换。
Libvrit也提供了专⻔的virDomainQemuAgentCommand API,在终端中通过virsh qemu-agent-command指令暴露,与QGA进⾏通信和操作,常⽤于实现 QEMU Guest 的监控和后台管理,例如:修改虚拟机的密码。
virsh qemu-agent-command <domain> '{"execute":"guest-ping"}'
virsh qemu-agent-command <domain> '{"execute":"guest-info"}'
virsh qemu-agent-command <domain> '{"execute":"guest-network-get-interfaces"}'
virsh reboot --mode agent <domain>
# 修改密码
virsh qemu-agent-command <domain> '{"execute":"guest-set-userpassword","arguments":
{"username":"admin","password":"cGFzc3cwcmQ=","crypted":false}}'
# or
virsh set-user-password --domain <domain> --user admin --password cGFzc3cwcmQ=
安装 QGA
⼿动安装:
yum install qemu-guest-agent
setenforce 0
systemctl restart qemu-guest-agent.service
在 OpenStack 环境中,⾃ L 版只需要为 Image 设置 hw_qemu_guest_agent 属性即可启⽤ QGA,前提是 image 以及安装了相应的软件包:
nova image-meta <image-id> set hw_qemu_guest_agent=yes
# or
glance image-create --name cirros
--disk-format raw
--container-format bare
--file cirros-0.3.3-x86_64-disk.raw
--public
--property hw_qemu_guest_agent=yes
--progress
# or
openstack image set --property hw_qemu_guest_agent=yes <image-id>
然后就可以使⽤ novaclient 修改虚拟机密码了,详⻅:
https://specs.openstack.org/openstack/nova-specs/specs/liberty/implemented/libvirt-set-admin-password.html。
openstack server set --root-password <instance-uuid>
QGA 接⼝
NOTE:不同的 GuestOS 具有不同的指令⽀持程度,这⾥需要注意,Windows OS 因为是闭源的操作系统,所以⽀持有限,详⻅:https://fedoraproject.org/wiki/Windows_Virtio_Drivers.
-
guest-sync-delimited:宿主机发送⼀个 Int 数字给 QGA,QGA 返回这个数字,并且在后续返回字符串响应中加⼊ ascii 码为 0xff 的字符,其作⽤是检查宿主机与 QGA 通信的同步状态,主要⽤在宿主机上多客户端与 QGA 通信的情况下客户端间切换过程的状态同步检查,⽐如:有两个客户端A、B,QGA 发送给 A 的响应,由于 A 已经退出,⽬前 B 连接到 QGA,所以这个响应可能被 B 收到,如果 B 连接后,⽴即发送该请求给 QGA,响应中加⼊了这个同步码就能区分是 A 的响应还是B 的响应。在 QGA 返回宿主机客户端发送的 Int 数字之前,QGA 返回的所有响应都要忽略。
-
guest-sync:同上,只是不在响应中加⼊ 0xff 字符。
-
guest-ping:Ping the guest agent, a non-error return implies success。
-
guest-get-time:获取虚拟机时间(返回值为相对于 1970-01-01 in UTC,Time in
-
nanoseconds)。
-
guest-set-time:设置虚拟机时间(输⼊为相对于 1970-01-01 in UTC,Time in nanoseconds)。
-
guest-info:返回 QGA ⽀持的所有命令。
-
guest-shutdown:关闭虚拟机,⽀持 halt、powerdown、reboot ⽅式,默认为 powerdown。
-
guest-file-open:打开虚拟机内的某个⽂件(返回⽂件句柄)。
-
guest-file-close:关闭打开的虚拟机内的⽂件。
-
guest-file-read:根据⽂件句柄读取虚拟机内的⽂件内容(返回 base64 格式的⽂件内容)。
-
guest-file-write:根据⽂件句柄写⼊⽂件内容到虚拟机内的⽂件。
-
guest-file-seek:Seek to a position in the file, as with fseek(), and return the current file position afterward. Also encapsulates ftell()’s functionality, just Set offset=0, whence=SEEK_CUR。
-
guest-file-flush:Write file changes bufferred in userspace to disk/kernel buffers。
-
guest-fsfreeze-status:Get guest fsfreeze state. error state indicates。
-
guest-fsfreeze-freeze:Sync and freeze all freezable, local guest filesystems。
-
guest-fsfreeze-thaw:Unfreeze all frozen guest filesystems。
-
guest-fstrim:Discard (or “trim”) blocks which are not in use by the filesystem。
-
guest-suspend-disk*:Suspend guest to disk。
-
guest-suspend-ram*:Suspend guest to ram。
-
guest-suspend-hybrid:Save guest state to disk and suspend to ram(This command requires the pm-utils package to be installed in the guest.)。
-
guest-network-get-interfaces:Get list of guest IP addresses, MAC addresses and netmasks。guest-get-vcpus:Retrieve the list of the guest’s logical processors。
-
guest-set-vcpus:Attempt to reconfigure (currently: enable/disable) logical processors inside the guest。
关于九州云99Cloud
