qemu-ga

阅读更多
bty:git 提交需要xwindow的办法
unset SSH_ASKPASS


1.在virsh的xml定义中加入channel指定host的socket映射到guest的dev
用virsh启动虚拟机的时候
注意加channel
   
        
        
        
      

2.在guest中启动qemu-ga:
yum install qemu-guest-agent qemu-kvm-tools
qemu-ga -v -p /dev/virtio-ports/com.haoning.spice.0

3.在host上使用python连socket或者socat
socat unix-connect:/var/lib/libvirt/qemu/test.agent readline







具体的control.xml

  control
  d07d7290-9b1d-f33e-f54a-70a821252410
  4194304
  4194304
  1
  
    hvm
    
  
  
    
    
    
  
  
  destroy
  restart
  restart
  
    /usr/libexec/qemu-kvm
    
      
      
      
      
      
      

virsh define control.xml
virsh start control

在guest中
[root@control ~]# qemu-ga -v -p /dev/virtio-ports/com.haoning.spice.0 
1437616495.53279: debug: received EOF
1437616495.153410: debug: received EOF
1437616495.253627: debug: received EOF
1437616495.353849: debug: received EOF
1437616495.454057: debug: received EOF
1437616495.554266: debug: received EOF
1437616495.654507: debug: received EOF
1437616495.754719: debug: received EOF
1437616495.854928: debug: received EOF
1437616495.955151: debug: received EOF
1437616496.55375: debug: received EOF
1437616496.155490: debug: received EOF
^C1437616496.170249: debug: received signal num 2, quitting
[root@control ~]# 




[root@node11 ~]# ls
poll_qemu_guest_agent.py
[root@node11 ~]# cat poll_qemu_guest_agent.py 
#! /usr/bin/python

import base64
import json
import select
import socket

#sock_file = "/tmp/foo.sock"
sock_file = "/var/lib/libvirt/qemu/test.agent"

#import pdb;pdb.set_trace()
READ_LEN = 1024
read_file = "/proc/cpuinfo"
send_cmds = [{"execute": "guest-file-open", "arguments": {"path": read_file, "mode": "r"}},
             {"execute": "guest-file-read", "arguments": {"handle": None, "count": READ_LEN}},
             {"execute": "guest-file-close", "arguments": {"handle": None}}]

serversocket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
serversocket.connect(sock_file)
serversocket.setblocking(0)

vm_fileno = serversocket.fileno()
instances = {}
instances[vm_fileno] = "instance-000004e8"

epoll = select.epoll()
epoll.register(vm_fileno, select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)

try:
    #import ipdb;ipdb.set_trace()
    cmd = 0
    stop = False
    while True:
        print '-'*10 + 'start epoll' + '-'*10
        events = epoll.poll(1)
        for fileno, event in events:
            if fileno in instances.keys():# new client is connecting
                print 'instance: ' + instances[fileno]
            if event & select.EPOLLIN:    # new msg is coming in
                print "<<<<<<<<<<>>>>>>>>send"
                if len(send_cmds) <= cmd:
                    print 'no data to send'
                    break
                try:
                    byteswritten = 0
                    data = json.dumps(send_cmds[cmd])
                    while True:
                        byteswritten += serversocket.send(data[byteswritten:])
                        if byteswritten == len(data):
                            print '*'*10 + 'send over' + '*'*10
                            break
                except socket.error:
                    pass

                epoll.modify(fileno, select.EPOLLIN | select.EPOLLET)
                cmd += 1

            elif event & select.EPOLLHUP:   # connect closed
                epoll.unregister(serversocket.fileno())
                serversocket.close()

        print '-'*10 + 'end epoll' + '-'*10 + '\n'
        if stop:
            print 'stop now'
            break
finally:
    epoll.unregister(serversocket.fileno())
    epoll.close()
    serversocket.close()
[root@node11 ~]# 



用socat调用
socat unix-connect:/tmp/qga.sock readline
 {"execute":"guest-sync", "arguments":{"id":1234}}
 {"return": 1234}

socat参考
http://wiki.qemu.org/Features/QAPI/GuestAgent
libvirt配置参考
http://wiki.libvirt.org/page/Qemu_guest_agent
其他参考
http://aspirer2004.blog.163.com/blog/static/1067647201352423628885/


参考
http://www.cloudcraft.cn/use-qemu-guest-agent-to-change-openstack-instance-root-password/

已有功能
http://www.cnblogs.com/biangbiang/p/3222458.html
已有功能

 

目前qga最新版本为1.5.50,linux已经实现下面的所有功能,windows仅支持加*的那些功能:

 
复制代码

Ø guest-sync-delimited*:宿主机发送一个int数字给qga,qga返回这个数字,并且在后续返回字符串响应中加入ascii码为0xff的字符,其作用是检查宿主机与qga通信的同步状态,主要用在宿主机上多客户端与qga通信的情况下客户端间切换过程的状态同步检查,比如有两个客户端A、B,qga发送给A的响应,由于A已经退出,目前B连接到qga的socket,所以这个响应可能被B收到,如果B连接到socket之后,立即发送该请求给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。


参考
http://wiki.qemu.org/Features/QAPI/GuestAgent
 mdroth@illuin:~$ sudo socat unix-connect:/tmp/qga.sock readline
 {"execute":"guest-sync", "arguments":{"id":1234}}
 {"return": 1234}

 {"execute":"guest-ping"}
 {"return": {}}

 {"execute": "guest-info"}
 {"return": {"version": "1.0"}}

 // write "hello world!\n" to /tmp/testqga
 {"execute":"guest-file-open", "arguments":{"path":"/tmp/testqga","mode":"w+"}}
 {"return": 0}
 {"execute":"guest-file-write", "arguments":{"handle":0,"buf-b64":"aGVsbG8gd29ybGQhCg=="}}
 {"return": {"count": 13, "eof": false}}
 {"execute":"guest-file-close", "arguments":{"handle":0}}
 {"return": {}}

 // read back the "hello world!\n" from /tmp/testqga
 {"execute":"guest-file-open", "arguments":{"path":"/tmp/testqga","mode":"r"}}
 {"return": 1}
 {"execute":"guest-file-read", "arguments":{"handle":1,"count":1024}}
 {"return": {"buf-b64": "aGVsbG8gd29ybGQhCg==", "count": 13, "eof": true}}
 {"execute":"guest-file-close","arguments":{"handle":1}}
 {"return": {}}

 // freeze and unfreeze freezable guest filesystems
 {"execute":"guest-fsfreeze-status"}
 {"return": "thawed"}
 {"execute":"guest-fsfreeze-freeze"}
 {"return": 3}
 {"execute":"guest-fsfreeze-status"}
 {"return": "frozen"}
 {"execute":"guest-fsfreeze-thaw"}
 {"return": 3}
 {"execute":"guest-fsfreeze-status"}
 {"return": "thawed"}

你可能感兴趣的:(socket,qemu)