3️⃣ 将Base64编码写入/root/.ssh/authorized_keys
# 打开文件(以读写方式打开),获得句柄 virsh qemu-agent-command ${DOMAIN} '{"execute":"guest-file-open", "arguments":{"path":"/root/.ssh/authorized_keys","mode":"w+"}}' # 写文件,假设上一步返回{"return":1000},1000就是句柄 virsh qemu-agent-command ${DOMAIN} '{"execute":"guest-file-write", "arguments":{"handle":1000,"buf-b64":"c3NoLXJzYSBBQUFBQjNOemFDMXljMkVBQUFBREFRQUJBQUFCQVFEVktvZzA0cGJiTGFhcmpicHZLN0NSYUl1VXdXeGVoSklIOHRxdFgvb1Y0R1lONVdHWVBGYTF0enNkNFZ5b2JsbTRMZVBYNzlXZUk0a0ZIZ1NiSDVQNkg5aThsM0tDVEZISGVKVC9nMFA1NS9jNjB5RGIzbzZscXBXdTlJS0UzSTRsc1RwMDVZL1cwS3M3VzI3Sm5kcjE2Mm5pMFlidGhnZDlDUXlvaWJ1cm9oMzVFQ2lQR3dXVU9CVko0SUVwU3BPWmREVUpMUy92VnVTUWd2RUgwZnEvRzFEUDNTT3lSK0ROYXNKMDBtd29uZmFVS0haWG1XQWxIOG1hck53UG1XYXB5VFNRd0NGS0toMUh3bEpFV0VUVjRmWXVGd20zaWVubmI4Y1gxeTRhWDlBSlduQTJjYzM1cnB1bGl2TWlqZVhzL3NzVDVpRmxqWFhHWXpta1g2blIgcm9vdEBsb2NhbGhvc3QubG9jYWxkb21haW4K"}}' # 关闭文件 virsh qemu-agent-command ${DOMAIN} '{"execute":"guest-file-close", "arguments":{"handle":1000}}'查看效果:此时到VM里查看/root/.ssh/authorized_keys,应该能看到新增加的一行
附1. qemu-ga支持的所有指令不同的qemu-ga版本、不同的操作系统,支持的指令都会有所差异,下面是从官网上看到的当前所有参数
▪ 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
具体使用方法,请参考官网文档
https://qemu.weilnetz.de/doc/qemu-ga-ref.html 附2. 配置多个channel1️⃣ 可以在XML里配置多个channel,这样就可以创建多个设备通道
<channel type='unix'> <source mode='bind' path='/tmp/channel.sock'/> <target type='virtio'/> </channel> <channel type='unix'> <source mode='bind' path='/tmp/channel.sock-1'/> <target type='virtio'/> </channel>2️⃣ 在VM里要启动2个qemu-ga守护进程,可以将原有的service文件拷贝一份出来进行修改
cd /usr/lib/systemd/system cp qemu-guest-agent.service qemu-guest-agent-1.service然后修改qemu-guest-agent-1.service
3️⃣ 启动服务
systemctl start qemu-guest-agent-1