KVM--QEMU Guest Agent(十七)

2022-11-19 09:32:00 浏览数 (1)

QEMU Guest Agent概述

QEMU Guest Agent是运行在虚拟机内部的一个守护程序(qemu-guest-agent.service),用它来辅助Hypervisor实现对Guest 虚拟机的管理。QEMU为宿主机和虚拟机提供了一个数据通道(channel),这个通道的两端分别是在虚拟机内看到的串口和在宿主机上看到的unix socket文件。宿主机与虚拟机内的qga通讯就扩展了对虚拟机的控制能力,例如在宿主机上获取虚拟机的ip地址等。

libvrit提供了专门的 virDomainQemuAgentCommand API(对应virsh qemu-agent-command命令)来和qemu-guest-agent通讯,另外libvirt内置api也可以支持qga,例如reboot、shutdown等。

1,编辑虚拟机

代码语言:javascript复制
virsh edit vm01

2,宿主机上虚拟机xml添加channel

代码语言:javascript复制
<channel type='unix'>
 <source mode='bind' path='/var/lib/libvirt/qemu/org.qemu.guest_agent.0'/>
 <target type='virtio' name='org.qemu.guest_agent.0'/>
</channel>

3,开启虚拟机

代码语言:javascript复制
virsh start vm01

4,Linux虚拟机安装qemu-guest-agent

代码语言:javascript复制
yum install qemu-guest-agent
setenforce 0
systemctl restart qemu-guest-agent.service

5,宿主机上测试

代码语言:javascript复制
virsh qemu-agent-command vm01 '{"execute":"guest-info"}'
virsh qemu-agent-command vm01 '{"execute":"guest-network-get-interfaces"}'
virsh qemu-agent-command vm01 '{"execute":"guest-ping"}'
virsh  reboot --mode agent vm01
virsh  shutdown --mode agent vm01

Windows virtio-win iso 下载

https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/latest-virtio/

Windows qemu-ga 安装包下载

https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/latest-qemu-ga/

qemu-guest-agent 修改虚拟机用户密码

1.guest-set-user-password

(1)Windows虚拟机安装qemu-ga-x86_64.msi程序

(2)qemu-agent-command 密码必须是base64编码格式

代码语言:javascript复制
[root@kvm-node1 ~]#  python
Python 2.7.5 (default, Oct 14 2020, 14:45:30) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import base64
>>> print base64.b64encode('password') 
cGFzc3dvcmQ=

(3)关闭虚拟机

代码语言:javascript复制
virsh  destroy win10

(4)修改win10 xml文件添加如下配置

代码语言:javascript复制
<channel type='unix'>
 <source mode='bind' path='/var/lib/libvirt/qemu/org.qemu.guest_agent.0'/>
 <target type='virtio' name='org.qemu.guest_agent.0'/>
</channel>

(5)启动虚拟机

代码语言:javascript复制
virsh start win10

(6)qemu-agent-command命令guest-set-user-password修改密码

virsh qemu-agent-command win10

'{"execute":"guest-set-user-password","arguments":{"username":"admin","password":"cGFzc3dvcmQ=","crypted":false}}' {"return":{}}

2. virsh set-user-password

virsh set-user-password 修改

代码语言:javascript复制
virsh set-user-password --domain 6 --user root --password password       
Password set successfully for root in 6

QEMU Guest Agent 支持指令

  • guest-exec
  • guest-exec-status
  • guest-file-close
  • guest-file-flush
  • guest-file-open
  • guest-file-read
  • guest-file-seek
  • guest-file-write
  • guest-fsfreeze-freeze
  • guest-fsfreeze-freeze-list
  • guest-fsfreeze-status
  • guest-fsfreeze-thaw
  • guest-fstrim
  • guest-get-fsinfo
  • guest-get-host-name
  • guest-get-memory-block-info
  • guest-get-memory-blocks
  • guest-get-osinfo
  • guest-get-time
  • guest-get-timezone
  • guest-get-users
  • guest-get-vcpus
  • guest-info
  • guest-network-get-interfaces
  • guest-ping
  • guest-set-memory-blocks
  • guest-set-time
  • guest-set-user-password
  • guest-set-vcpus
  • guest-shutdown
  • guest-suspend-disk
  • guest-suspend-hybrid
  • guest-suspend-ram
  • guest-sync
  • guest-sync-delimited

0 人点赞