Simple applet based on C language Libvirt API

Source: Internet
Author: User

Libvirt API Simple Applet

1, the program code is as follows

 #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\n"); return 1;} Dom = Virdomainlookupbyid (conn, id), if (dom = = NULL) {fprintf (stderr, "Failed to find Domain%d\n", id); Virconnectclose (CO NN); return 1;} if (Virdomaingetinfo (DOM, &info) < 0) {fprintf (stderr, "Failed to get information for Domain%d\n", id); VIRDOMAINFR EE (DOM); Virconnectclose (conn); return 1;} printf ("Domain ID:%d\n", id);p rintf ("Vcpus:%d\n", info.nrvirtcpu);p rintf ("MaxMem:%d kb\n", Info.maxmem);p rintf ( "Memory:%d kb\n", 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-----\ n"); Getdomaininf O (dom_id); return 0;} 

  

2, the program compiles and runs, the result of running 1 shows

GCC Dominfo.c-o dominfo-lvirt./dominfo

-lvirt the shared library that is specified when the program is linked, the shared library file path that is dependent on the lab environment in this article is/usr/lib/libvirt.so

Figure 1

Compare the result of the run with the results of the Virsh run, as shown in result 2 of Virsh

Figure 2

Simple applet based on C language Libvirt API

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.