我正在编写一个C程序,我想获取KVM虚拟机的域名。有没有简单的方法可以做到这一点?我有机器的虚拟机 ID,并且可以获得指向 virDomainInfo 结构的指针。我知道我可以使用命令 virsh domname <id>
来做到这一点,但我似乎找不到 libvirt API,所以我可以通过编程来完成它。经过一番挖掘,我找到了 virDomain 的结构信息。可以选择从那里获取它吗?
struct _virDomain {
virObject object;
virConnectPtr conn; /* pointer back to the connection */
char *name; /* the domain external name */
int id; /* the domain ID */
unsigned char uuid[VIR_UUID_BUFLEN]; /* the domain unique identifier */
};
下面是我正在使用的代码。
virConnectPtr conn = virConnectOpen(connString);
virDomainPtr domainPtr = virDomainLookupByID(conn, vmid);
不知道从这里去哪里。预先感谢您的帮助。
最佳答案
只是浏览 libvirt API 我发现 virDomainGetName ,这看起来可能正是您想要的:
virDomainGetName
const char * virDomainGetName (virDomainPtr domain)
Get the public name for that domain
domain a domain object Returns a pointer to the name or NULL, the string need not be deallocated its lifetime will be the same as the domain object.
我一起浏览了一些示例代码:
int main(int argc, char **argv) {
virConnectPtr c;
virDomainPtr d;
char *name;
c = virConnectOpen(NULL);
d = virDomainLookupByID(c, 2);
name = virDomainGetName(d);
printf("name of domain %d is %s\n", 2, name);
return 0;
}
关于c - 使用libvirt API在C程序中获取KVM虚拟机域名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24101071/