From bf26360e5e91148a4479f812c2b43acce4f5884e Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Wed, 8 Apr 2009 23:06:51 +0100 Subject: [PATCH] Added OCaml examples. --- .gitignore | 4 ++++ configure.ac | 4 +++- libguestfs.spec.in | 7 +++++++ ocaml/Makefile.am | 2 ++ ocaml/examples/LICENSE | 2 ++ ocaml/examples/Makefile.am | 10 ++++++++++ ocaml/examples/README | 9 +++++++++ ocaml/examples/lvs.ml | 26 ++++++++++++++++++++++++++ 8 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 ocaml/examples/LICENSE create mode 100644 ocaml/examples/Makefile.am create mode 100644 ocaml/examples/README create mode 100644 ocaml/examples/lvs.ml diff --git a/.gitignore b/.gitignore index 6f8d612..90a3b52 100644 --- a/.gitignore +++ b/.gitignore @@ -50,6 +50,10 @@ ocaml/*.cma ocaml/*.cmxa ocaml/*.a ocaml/*.so +ocaml/examples/*.cmi +ocaml/examples/*.cmo +ocaml/examples/*.cmx +ocaml/examples/lvs perl/Guestfs.c perl/Guestfs.bs perl/Makefile-pl diff --git a/configure.ac b/configure.ac index 29ab3ce..e0cdcde 100644 --- a/configure.ac +++ b/configure.ac @@ -120,7 +120,9 @@ AC_CONFIG_SUBDIRS([daemon]) dnl Produce output files. AC_CONFIG_HEADERS([config.h]) AC_CONFIG_FILES([Makefile src/Makefile fish/Makefile examples/Makefile - images/Makefile ocaml/Makefile perl/Makefile + images/Makefile + ocaml/Makefile ocaml/examples/Makefile + perl/Makefile python/Makefile make-initramfs.sh update-initramfs.sh libguestfs.spec diff --git a/libguestfs.spec.in b/libguestfs.spec.in index f719c05..bc611ef 100644 --- a/libguestfs.spec.in +++ b/libguestfs.spec.in @@ -151,6 +151,12 @@ rm Makefile* rm -rf .deps .libs popd +# Same for ocaml/examples. +pushd ocaml/examples +make clean +rm Makefile* +popd + find $RPM_BUILD_ROOT -name perllocal.pod -delete find $RPM_BUILD_ROOT -name .packlist -delete @@ -200,6 +206,7 @@ rm -rf $RPM_BUILD_ROOT %files ocaml-devel %defattr(-,root,root,-) +%doc ocaml/examples %{_libdir}/ocaml/guestfs/*.a %{_libdir}/ocaml/guestfs/*.cmxa %{_libdir}/ocaml/guestfs/*.cmx diff --git a/ocaml/Makefile.am b/ocaml/Makefile.am index ca8a0ac..00e8027 100644 --- a/ocaml/Makefile.am +++ b/ocaml/Makefile.am @@ -20,6 +20,8 @@ EXTRA_DIST = \ guestfs_c.c guestfs_c.h guestfs_c_actions.c \ .depend META.in +SUBDIRS = examples + if HAVE_OCAML noinst_DATA = mlguestfs.cma mlguestfs.cmxa META diff --git a/ocaml/examples/LICENSE b/ocaml/examples/LICENSE new file mode 100644 index 0000000..8f0b20b --- /dev/null +++ b/ocaml/examples/LICENSE @@ -0,0 +1,2 @@ +All the examples in the ocaml/examples/ subdirectory may be freely +copied without any restrictions. diff --git a/ocaml/examples/Makefile.am b/ocaml/examples/Makefile.am new file mode 100644 index 0000000..48191e7 --- /dev/null +++ b/ocaml/examples/Makefile.am @@ -0,0 +1,10 @@ +EXTRA_DIST = LICENSE README lvs.ml + +if HAVE_OCAML + +lvs: lvs.ml + $(OCAMLFIND) ocamlopt -I .. mlguestfs.cmxa $< -o $@ + +CLEANFILES = *.cmi *.cmo *.cmx *.o *~ lvs + +endif \ No newline at end of file diff --git a/ocaml/examples/README b/ocaml/examples/README new file mode 100644 index 0000000..679f506 --- /dev/null +++ b/ocaml/examples/README @@ -0,0 +1,9 @@ +This directory contains various example programs which use the OCaml +Guestfs bindings to the libguestfs API. + +As they are examples, these are licensed so they can be freely copied +and used without any restrictions. + +Tips: + +(1) To enable verbose messages, set environment variable LIBGUESTFS_DEBUG=1 diff --git a/ocaml/examples/lvs.ml b/ocaml/examples/lvs.ml new file mode 100644 index 0000000..12c7c15 --- /dev/null +++ b/ocaml/examples/lvs.ml @@ -0,0 +1,26 @@ +(* An example using the OCaml bindings. *) + +open Printf + +let () = + if Array.length Sys.argv <= 1 || not (Sys.file_exists Sys.argv.(1)) then ( + eprintf "Usage: lvs guest.img\n"; + exit 1 + ); + + let h = Guestfs.create () in + Guestfs.add_drive h Sys.argv.(1); + Guestfs.launch h; + Guestfs.wait_ready h; + + let pvs = Guestfs.pvs h in + printf "PVs found: [ %s ]\n" (String.concat "; " (Array.to_list pvs)); + + let vgs = Guestfs.vgs h in + printf "VGs found: [ %s ]\n" (String.concat "; " (Array.to_list vgs)); + + let lvs = Guestfs.lvs h in + printf "LVs found: [ %s ]\n" (String.concat "; " (Array.to_list lvs)); + + (* Helps to find any allocation errors. *) + Gc.compact () -- 1.8.3.1