zoukankan      html  css  js  c++  java
  • 基于C语言libvirt API简单小程序

    libvirt API简单小程序

    1、程序代码如下

    #include<stdio.h>
    #include<libvirt/libvirt.h>
    
    int getDomainInfo(int id) {
    	virConnectPtr conn = NULL;
    	virDomainPtr dom = NULL;
    	virDomainInfo info;
    	
    	conn = virConnectOpenReadOnly(NULL);
    	if (conn == NULL) {
    		fprintf(stderr, "Failed to connect to hypervisor
    ");
    		return 1;
    	}
    	dom = virDomainLookupByID(conn, id);
    	if (dom == NULL) {
    		fprintf(stderr, "Failed to find Domain %d
    ", id);
    		virConnectClose(conn);
    		return 1;
    	}
    	if (virDomainGetInfo(dom, &info) < 0) {
    		fprintf(stderr, "Failed to get information for Domain %d
    ", id);
    		virDomainFree(dom);
    		virConnectClose(conn);
    		return 1;
    	}
    	printf("Domain ID: %d
    ", id);
    	printf("    vCPUs: %d
    ", info.nrVirtCpu);
    	printf("    maxMem: %d KB
    ", info.maxMem);
    	printf("    memory: %d KB
    ", info.memory);
    	
    	if (dom != NULL){
    		virDomainFree(dom);
    	}
    	if (conn != NULL){
    		virConnectClose(conn);
    	}
    	return 0;
    }
    int main(int argc, char **argv)
    {
    	int dom_id = 3;
    	printf("-----Get domain info by ID via libvirt C API -----
    ");
    	getDomainInfo(dom_id);
    	return 0;
    }
    

      

    2、程序编译并运行,运行结果如图1所示

    gcc dominfo.c -o dominfo -lvirt
    ./dominfo
    

      -lvirt指定程序链接时的共享库,在本文实验环境中依赖的共享库文件路径为/usr/lib/libvirt.so

    图 1

    将运行结果和virsh的运行的结果相比较,virsh的结果如图2所示

    图 2

     

     

  • 相关阅读:
    ZJOI2019 Day1 游记
    Codeforces Round #545 (Div. 1)
    AtCoder WTF 2019 C2. Triangular Lamps Hard
    析合树
    Codeforces Round #539 (Div. 1)
    Codeforces 1103 E. Radix sum
    Codeforces 1097 G. Vladislav and a Great Legend
    sts创建spring项目
    servlet项目demo
    IDEA中创建maven web项目
  • 原文地址:https://www.cnblogs.com/grglym/p/8059333.html
Copyright © 2011-2022 走看看