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

     

     

  • 相关阅读:
    ArcGIS影像配准与空间配准
    去除右键菜单opendlg
    Windows环境下Android Studio v1.0安装教程
    OpenGL入门
    Fetching android sdk component information
    事件的委托处理(Event Delegation)
    Javascript模块化编程:模块的写法
    10种排序算法总结
    GitHub-修改以下host-ip可加快访问速度
    this
  • 原文地址:https://www.cnblogs.com/grglym/p/8059333.html
Copyright © 2011-2022 走看看