2009-02-26 16:14:50 +00:00
/* This file contains trivial example code to connect to the running
2013-02-26 09:11:10 -05:00
* hypervisor and gather a few bits of information about domains .
* Similar API ' s exist for storage pools , networks , and interfaces . */
2009-02-26 16:14:50 +00:00
# include <config.h>
# include <stdio.h>
# include <stdlib.h>
# include <libvirt/libvirt.h>
# include <libvirt/virterror.h>
static int
showHypervisorInfo ( virConnectPtr conn )
{
int ret = 0 ;
unsigned long hvVer , major , minor , release ;
const char * hvType ;
/* virConnectGetType returns a pointer to a static string, so no
* allocation or freeing is necessary ; it is possible for the call
* to fail if , for example , there is no connection to a
* hypervisor , so check what it returns . */
hvType = virConnectGetType ( conn ) ;
2013-02-26 09:11:10 -05:00
if ( ! hvType ) {
2009-02-26 16:14:50 +00:00
ret = 1 ;
2013-05-10 18:43:38 +01:00
printf ( " Failed to get hypervisor type: %s \n " ,
virGetLastErrorMessage ( ) ) ;
2009-02-26 16:14:50 +00:00
goto out ;
}
if ( 0 ! = virConnectGetVersion ( conn , & hvVer ) ) {
ret = 1 ;
2013-05-10 18:43:38 +01:00
printf ( " Failed to get hypervisor version: %s \n " ,
virGetLastErrorMessage ( ) ) ;
2009-02-26 16:14:50 +00:00
goto out ;
}
major = hvVer / 1000000 ;
hvVer % = 1000000 ;
minor = hvVer / 1000 ;
release = hvVer % 1000 ;
printf ( " Hypervisor: \" %s \" version: %lu.%lu.%lu \n " ,
hvType ,
major ,
minor ,
release ) ;
2014-03-25 07:48:10 +01:00
out :
2009-02-26 16:14:50 +00:00
return ret ;
}
static int
showDomains ( virConnectPtr conn )
{
Convert 'int i' to 'size_t i' in examples/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 15:09:33 +01:00
int ret = 0 , numNames , numInactiveDomains , numActiveDomains ;
size_t i ;
2013-02-26 09:11:10 -05:00
int flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
VIR_CONNECT_LIST_DOMAINS_INACTIVE ;
virDomainPtr * nameList = NULL ;
/* NB: The return from the virConnectNum*() APIs is only useful for
* the current call . A domain could be started or stopped and any
* assumptions made purely on these return values could result in
* unexpected results */
2009-02-26 16:14:50 +00:00
numActiveDomains = virConnectNumOfDomains ( conn ) ;
2013-02-26 09:11:10 -05:00
if ( numActiveDomains = = - 1 ) {
2009-02-26 16:14:50 +00:00
ret = 1 ;
2013-05-10 18:43:38 +01:00
printf ( " Failed to get number of active domains: %s \n " ,
virGetLastErrorMessage ( ) ) ;
2009-02-26 16:14:50 +00:00
goto out ;
}
numInactiveDomains = virConnectNumOfDefinedDomains ( conn ) ;
2013-02-26 09:11:10 -05:00
if ( numInactiveDomains = = - 1 ) {
2009-02-26 16:14:50 +00:00
ret = 1 ;
2013-05-10 18:43:38 +01:00
printf ( " Failed to get number of inactive domains: %s \n " ,
virGetLastErrorMessage ( ) ) ;
2009-02-26 16:14:50 +00:00
goto out ;
}
printf ( " There are %d active and %d inactive domains \n " ,
numActiveDomains , numInactiveDomains ) ;
2013-02-26 09:11:10 -05:00
/* Return a list of all active and inactive domains. Using this API
* instead of virConnectListDomains ( ) and virConnectListDefinedDomains ( )
* is preferred since it " solves " an inherit race between separated API
* calls if domains are started or stopped between calls */
numNames = virConnectListAllDomains ( conn ,
& nameList ,
flags ) ;
2013-07-11 07:19:37 -04:00
if ( numNames = = - 1 ) {
ret = 1 ;
printf ( " Failed to get a list of all domains: %s \n " ,
virGetLastErrorMessage ( ) ) ;
goto out ;
}
2013-02-26 09:11:10 -05:00
for ( i = 0 ; i < numNames ; i + + ) {
int active = virDomainIsActive ( nameList [ i ] ) ;
printf ( " %8s (%s) \n " ,
virDomainGetName ( nameList [ i ] ) ,
( active = = 1 ? " active " : " non-active " ) ) ;
/* must free the returned named per the API documentation */
virDomainFree ( nameList [ i ] ) ;
2009-02-26 16:14:50 +00:00
}
2013-02-26 09:11:10 -05:00
free ( nameList ) ;
2009-02-26 16:14:50 +00:00
2014-03-25 07:48:10 +01:00
out :
2009-02-26 16:14:50 +00:00
return ret ;
}
int
main ( int argc , char * argv [ ] )
{
int ret = 0 ;
virConnectPtr conn ;
char * uri ;
printf ( " Attempting to connect to hypervisor \n " ) ;
uri = ( argc > 0 ? argv [ 1 ] : NULL ) ;
/* virConnectOpenAuth is called here with all default parameters,
* except , possibly , the URI of the hypervisor . */
conn = virConnectOpenAuth ( uri , virConnectAuthPtrDefault , 0 ) ;
2013-02-26 09:11:10 -05:00
if ( ! conn ) {
2009-02-26 16:14:50 +00:00
ret = 1 ;
2013-05-10 18:43:38 +01:00
printf ( " No connection to hypervisor: %s \n " ,
virGetLastErrorMessage ( ) ) ;
2009-02-26 16:14:50 +00:00
goto out ;
}
uri = virConnectGetURI ( conn ) ;
2013-02-26 09:11:10 -05:00
if ( ! uri ) {
2009-02-26 16:14:50 +00:00
ret = 1 ;
2013-05-10 18:43:38 +01:00
printf ( " Failed to get URI for hypervisor connection: %s \n " ,
virGetLastErrorMessage ( ) ) ;
2009-02-26 16:14:50 +00:00
goto disconnect ;
}
printf ( " Connected to hypervisor at \" %s \" \n " , uri ) ;
free ( uri ) ;
if ( 0 ! = showHypervisorInfo ( conn ) ) {
ret = 1 ;
goto disconnect ;
}
if ( 0 ! = showDomains ( conn ) ) {
ret = 1 ;
goto disconnect ;
}
2014-03-25 07:48:10 +01:00
disconnect :
2009-02-26 16:14:50 +00:00
if ( 0 ! = virConnectClose ( conn ) ) {
2013-05-10 18:43:38 +01:00
printf ( " Failed to disconnect from hypervisor: %s \n " ,
virGetLastErrorMessage ( ) ) ;
2009-02-26 16:14:50 +00:00
ret = 1 ;
} else {
printf ( " Disconnected from hypervisor \n " ) ;
}
2014-03-25 07:48:10 +01:00
out :
2009-02-26 16:14:50 +00:00
return ret ;
}