2006-02-21 17:15:32 +03:00
/**
* section : Scheduling
* synopsis : Suspend a domain and then resume its execution
* purpose : Demonstrate the basic use of the library to suspend and
* resume a domain . If no id is given on the command line
* this script will suspend and resume the first domain found
* which is not Domain 0.
* usage : suspend [ id ]
* test : suspend
* author : Daniel Veillard
* copy : see Copyright for the status of this software .
*/
# include <stdlib.h>
# include <stdio.h>
2006-06-26 19:02:18 +04:00
# include <libvirt/libvirt.h>
2006-02-21 17:15:32 +03:00
2008-01-14 17:04:33 +03:00
static virConnectPtr conn = NULL ; /* the hypervisor connection */
2006-02-21 17:15:32 +03:00
/**
* checkDomainState :
* @ dom : the domain
*
* Return the current state of a domain or - 1 if non - exsitant
*/
static int
checkDomainState ( virDomainPtr dom ) {
2008-03-14 14:08:03 +03:00
virDomainInfo info ; /* the information being fetched */
2006-02-21 17:15:32 +03:00
int ret ;
2008-02-05 22:27:37 +03:00
2006-02-21 17:15:32 +03:00
ret = virDomainGetInfo ( dom , & info ) ;
if ( ret < 0 ) {
2012-03-22 15:33:35 +04:00
return - 1 ;
2006-02-21 17:15:32 +03:00
}
2012-03-22 15:33:35 +04:00
return info . state ;
2006-02-21 17:15:32 +03:00
}
/**
* SuspendAndResumeDomain :
* @ id : the id of the domain
*
2008-03-14 14:08:03 +03:00
* extract the domain 0 information
2006-02-21 17:15:32 +03:00
*/
static void
SuspendAndResumeDomain ( int id ) {
virDomainPtr dom = NULL ; /* the domain being checked */
int ret , state ;
/* Find the domain of the given id */
dom = virDomainLookupByID ( conn , id ) ;
if ( dom = = NULL ) {
fprintf ( stderr , " Failed to find Domain %d \n " , id ) ;
2008-04-10 20:54:54 +04:00
goto error ;
2006-02-21 17:15:32 +03:00
}
/* Check state */
state = checkDomainState ( dom ) ;
if ( ( state = = VIR_DOMAIN_RUNNING ) | |
( state = = VIR_DOMAIN_NOSTATE ) | |
2008-04-10 20:54:54 +04:00
( state = = VIR_DOMAIN_BLOCKED ) ) {
printf ( " Suspending domain... \n " ) ;
ret = virDomainSuspend ( dom ) ;
if ( ret < 0 ) {
fprintf ( stderr , " Failed to suspend Domain %d \n " , id ) ;
goto error ;
}
state = checkDomainState ( dom ) ;
if ( state ! = VIR_DOMAIN_PAUSED ) {
fprintf ( stderr , " Domain %d state is not suspended \n " , id ) ;
} else {
printf ( " Domain suspended, resuming it... \n " ) ;
}
ret = virDomainResume ( dom ) ;
if ( ret < 0 ) {
fprintf ( stderr , " Failed to resume Domain %d \n " , id ) ;
goto error ;
}
state = checkDomainState ( dom ) ;
if ( ( state = = VIR_DOMAIN_RUNNING ) | |
( state = = VIR_DOMAIN_NOSTATE ) | |
( state = = VIR_DOMAIN_BLOCKED ) ) {
printf ( " Domain resumed \n " ) ;
} else {
fprintf ( stderr , " Domain %d state indicate it is not resumed \n " , id ) ;
}
2006-02-21 17:15:32 +03:00
} else {
fprintf ( stderr , " Domain %d is not in a state where it should be suspended \n " , id ) ;
2008-04-10 20:54:54 +04:00
goto error ;
2006-02-21 17:15:32 +03:00
}
error :
if ( dom ! = NULL )
virDomainFree ( dom ) ;
}
int main ( int argc , char * * argv ) {
int id = 0 ;
/* NULL means connect to local Xen hypervisor */
conn = virConnectOpenReadOnly ( NULL ) ;
if ( conn = = NULL ) {
fprintf ( stderr , " Failed to connect to hypervisor \n " ) ;
2008-04-10 20:54:54 +04:00
goto error ;
2006-02-21 17:15:32 +03:00
}
if ( argc > 1 ) {
id = atoi ( argv [ 1 ] ) ;
}
if ( id = = 0 ) {
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 18:09:33 +04:00
int n ;
size_t i ;
int ids [ 10 ] ;
n = virConnectListDomains ( conn , & ids [ 0 ] , 10 ) ;
if ( n < 0 ) {
2008-04-10 20:54:54 +04:00
fprintf ( stderr , " Failed to list the domains \n " ) ;
goto error ;
}
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 18:09:33 +04:00
for ( i = 0 ; i < n ; i + + ) {
if ( ids [ i ] ! = 0 ) {
id = ids [ i ] ;
2008-04-10 20:54:54 +04:00
break ;
}
}
2006-02-21 17:15:32 +03:00
}
if ( id = = 0 ) {
2008-04-10 20:54:54 +04:00
fprintf ( stderr , " Failed find a running guest domain \n " ) ;
goto error ;
2006-02-21 17:15:32 +03:00
}
2008-02-05 22:27:37 +03:00
2006-02-21 17:15:32 +03:00
SuspendAndResumeDomain ( id ) ;
error :
if ( conn ! = NULL )
2008-04-10 20:54:54 +04:00
virConnectClose ( conn ) ;
2012-03-22 15:33:35 +04:00
return 0 ;
2006-02-21 17:15:32 +03:00
}