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