libvirt 报connect peer的错误
上一篇文章阐述了虚拟机链接时报错到问题,即一个循环监控虚拟机到程序,每次运行到20次到时候即报错,显示链接失败。
一开始以为是调用virConnectClose函数释放结构virConnectPtr结构时没有成功导致的。后来查阅相关资料。发现,virConnectClose函数到返回值分两种。
执行成功则返回剩余的链接数,执行失败则返回-1。对于我自己到程序,将virConnectClose函数到返回值输出之后,发现都是1.即每次都没有释放干净链接,还剩一个链接没有释放。
后来在谷歌上搜索一番,找到了原因所在。
all libvirt objects like connection, domain, storage pool etc are reference counted. When you lookup a domain over a connection then the domain adds an additional reference to the connection. This ensures that the connection is open as long as there are objects that have been looked up over it. You don't need to free all objects before you can close the connection. You just need to match all virConnectOpen calls by a virConnectClose call and all lookup calls for other objects by the matching free calls. virConnectPtr conn = virConnectOpen("qemu:///system", 0); // conn.refs = 1 virDomainPtr domain = virDomainLookupByName(conn, "vm1"); // conn.refs = 2, domain.refs = 1 ... virConnectClose(conn); // conn.refs = 1, but conn is still open because domain has a reference to it virDomainFree(domain); // conn.refs = 0, domain.refs = 0, domain gets freed and releases its reference to conn, no conn gets closed So virConnectClose returning -1 has nothing to do with the order of close/free calls. I think you can only make virConnectClose return -1 when you call it on an invalid or already closed connection.
即除了函数virConnectRef,函数virDomainLookByName等等函数都会增加连接数,而对于每一个链接,都要调用一次virConnectClose才行。
在我的程序中,函数virDomainLookByID函数造成了这种情况。由此可见,会造成这种情况到函数不止这几个。只要根据virConnectClose 的返回值
增加调用virConnectClose函数的调用次数即可解决这一问题。
参考网址:
http://www.redhat.com/archives/libvir-list/2010-October/msg00627.html