From c509b5cd0b238baa8614865b877e5b4ec2893814 Mon Sep 17 00:00:00 2001 From: Daniel Veillard Date: Tue, 21 Feb 2006 14:15:32 +0000 Subject: [PATCH] * Makefile.am: extended make tests to run those in docs/examples * docs/examples/suspend.c docs/examples/*: added an example of suspend/resume and regenerated Daniel --- ChangeLog | 6 ++ Makefile.am | 1 + docs/examples/Makefile.am | 8 ++- docs/examples/examples.xml | 40 +++++++++++ docs/examples/index.html | 4 +- docs/examples/suspend.c | 134 +++++++++++++++++++++++++++++++++++++ 6 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 docs/examples/suspend.c diff --git a/ChangeLog b/ChangeLog index 4148997882..85cba21b2c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Tue Feb 21 09:14:07 EST 2006 Daniel Veillard + + * Makefile.am: extended make tests to run those in docs/examples + * docs/examples/suspend.c docs/examples/*: added an example of + suspend/resume and regenerated + Tue Feb 21 14:21:39 CET 2006 Daniel Veillard * TODO: updated diff --git a/Makefile.am b/Makefile.am index c71ce6e32d..04d89366ba 100644 --- a/Makefile.am +++ b/Makefile.am @@ -15,6 +15,7 @@ rpm: clean check-local: all tests tests: + @(cd docs/examples ; $(MAKE) MAKEFLAGS+=--silent tests) @(if [ "@PYTHON_SUBDIR@" != "" ] ; then cd python ; \ $(MAKE) MAKEFLAGS+=--silent tests ; fi) diff --git a/docs/examples/Makefile.am b/docs/examples/Makefile.am index 26844e9142..4866bfcd31 100644 --- a/docs/examples/Makefile.am +++ b/docs/examples/Makefile.am @@ -17,18 +17,24 @@ install-data-local: EXTRA_DIST=examples.xsl index.py examples.xml -noinst_PROGRAMS=info1 +noinst_PROGRAMS=info1 suspend info1_SOURCES=info1.c info1_LDFLAGS= info1_DEPENDENCIES= $(DEPS) info1_LDADD= $(LDADDS) +suspend_SOURCES=suspend.c +suspend_LDFLAGS= +suspend_DEPENDENCIES= $(DEPS) +suspend_LDADD= $(LDADDS) + valgrind: $(MAKE) CHECKER='valgrind' tests tests: $(noinst_PROGRAMS) @(echo '## examples regression tests') @($(CHECKER) ./info1) + @($(CHECKER) ./suspend) diff --git a/docs/examples/examples.xml b/docs/examples/examples.xml index 5d541334e5..84dbfe25f4 100644 --- a/docs/examples/examples.xml +++ b/docs/examples/examples.xml @@ -18,29 +18,69 @@ + + Suspend a domain and then resume its execution + 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. + suspend [id] + suspend + Daniel Veillard + see Copyright for the status of this software. +
Scheduling
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+
+ +
diff --git a/docs/examples/index.html b/docs/examples/index.html index 8cdeed7675..ceff46a43f 100644 --- a/docs/examples/index.html +++ b/docs/examples/index.html @@ -1,7 +1,7 @@ Libvirt set of C code examples

Libvirt set of C code examples

The examples are stored per section depending on the main focus - of the example:

Getting the compilation options and libraries dependancies needed + of the example:

Getting the compilation options and libraries dependancies needed to generate binaries from the examples is best done on Linux/Unix by using the pkg-config data which should have been installed as part of make -install step or when installing the libvirt development package:

gcc -o example example.c `pkg-config libvirt --libs`

Informations Examples

info1.c: Extract informations about Xen domain 0

Demonstrate the basic use of the library to connect to the hypervisor and extract domain informations.

Uses:

Usage:

info1

Author: Daniel Veillard

+install step or when installing the libvirt development package:

gcc -o example example.c `pkg-config libvirt --libs`

Informations Examples

info1.c: Extract informations about Xen domain 0

Demonstrate the basic use of the library to connect to the hypervisor and extract domain informations.

Uses:

Usage:

info1

Author: Daniel Veillard

Scheduling Examples

suspend.c: Suspend a domain and then resume its execution

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.

Uses:

Usage:

suspend [id]

Author: Daniel Veillard

diff --git a/docs/examples/suspend.c b/docs/examples/suspend.c new file mode 100644 index 0000000000..0c56786467 --- /dev/null +++ b/docs/examples/suspend.c @@ -0,0 +1,134 @@ +/** + * 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 +#include +#include + +virConnectPtr conn = NULL; /* the hypervisor connection */ + +/** + * checkDomainState: + * @dom: the domain + * + * Return the current state of a domain or -1 if non-exsitant + */ +static int +checkDomainState(virDomainPtr dom) { + virDomainInfo info; /* the informations being fetched */ + int ret; + + ret = virDomainGetInfo(dom, &info); + if (ret < 0) { + return(-1); + } + return(info.state); +} + +/** + * SuspendAndResumeDomain: + * @id: the id of the domain + * + * extract the domain 0 informations + */ +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); + goto error; + } + + /* Check state */ + state = checkDomainState(dom); + if ((state == VIR_DOMAIN_RUNNING) || + (state == VIR_DOMAIN_NOSTATE) || + (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); + } + } else { + fprintf(stderr, "Domain %d is not in a state where it should be suspended\n", id); + goto error; + } + +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"); + goto error; + } + + if (argc > 1) { + id = atoi(argv[1]); + } + if (id == 0) { + int i, j, ids[10]; + i = virConnectListDomains(conn, &ids[0], 10); + if (i < 0) { + fprintf(stderr, "Failed to list the domains\n"); + goto error; + } + for (j = 0;j < i;j++) { + if (ids[j] != 0) { + id = ids[j]; + break; + } + } + } + if (id == 0) { + fprintf(stderr, "Failed find a running guest domain\n"); + goto error; + } + + SuspendAndResumeDomain(id); + +error: + if (conn != NULL) + virConnectClose(conn); + return(0); +} -- GitLab