Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
16.11. Using the API from Programming Languages
The libguestfs API can be used directly from the following languages in Red Hat Enterprise Linux 6.2: C, C++, Perl, Python, Java, Ruby and OCaml.
- To install C and C++ bindings, enter the following command:
yum install libguestfs-devel
# yum install libguestfs-devel
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - To install Perl bindings:
yum install 'perl(Sys::Guestfs)'
# yum install 'perl(Sys::Guestfs)'
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - To install Python bindings:
yum install python-libguestfs
# yum install python-libguestfs
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - To install Java bindings:
yum install libguestfs-java libguestfs-java-devel libguestfs-javadoc
# yum install libguestfs-java libguestfs-java-devel libguestfs-javadoc
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - To install Ruby bindings:
yum install ruby-libguestfs
# yum install ruby-libguestfs
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - To install OCaml bindings:
yum install ocaml-libguestfs ocaml-libguestfs-devel
# yum install ocaml-libguestfs ocaml-libguestfs-devel
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
The binding for each language is essentially the same, but with minor syntactic changes. A C statement:
guestfs_launch (g);
guestfs_launch (g);
Would appear like the following in Perl:
$g->launch ()
$g->launch ()
Or like the following in OCaml:
g#launch ()
g#launch ()
Only the API from C is detailed in this section.
In the C and C++ bindings, you must manually check for errors. In the other bindings, errors are converted into exceptions; the additional error checks shown in the examples below are not necessary for other languages, but conversely you may wish to add code to catch exceptions. Refer to the following list for some points of interest regarding the architecture of the libguestfs API:
- The libguestfs API is synchronous. Each call blocks until it has completed. If you want to make calls asynchronously, you have to create a thread.
- The libguestfs API is not thread safe: each handle should be used only from a single thread, or if you want to share a handle between threads you should implement your own mutex to ensure that two threads cannot execute commands on one handle at the same time.
- You should not open multiple handles on the same disk image. It is permissible if all the handles are read-only, but still not recommended.
- You should not add a disk image for writing if anything else could be using that disk image (eg. a live VM). Doing this will cause disk corruption.
- Opening a read-only handle on a disk image which is currently in use (eg. by a live VM) is possible; however, the results may be unpredictable or inconsistent particularly if the disk image is being heavily written to at the time you are reading it.
16.11.1. Interaction with the API through a C Program Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
Your C program should start by including the <guestfs.h> header file, and creating a handle:
Save this program to a file (
test.c
). Compile this program and run it with the following two commands:
gcc -Wall test.c -o test -lguestfs ./test
gcc -Wall test.c -o test -lguestfs
./test
At this stage it should print no output. The rest of this section demonstrates an example showing how to extend this program to create a new disk image, partition it, format it with an ext4 file system, and create some files in the file system. The disk image will be called
disk.img
and be created in the current directory.
The outline of the program is:
- Create the handle.
- Add disk(s) to the handle.
- Launch the libguestfs back end.
- Create the partition, file system and files.
- Close the handle and exit.
Here is the modified program:
Compile and run this program with the following two commands:
gcc -Wall test.c -o test -lguestfs ./test
gcc -Wall test.c -o test -lguestfs
./test
If the program runs to completion successfully then you should be left with a disk image called
disk.img
, which you can examine with guestfish:
guestfish --ro -a disk.img -m /dev/sda1 ><fs> ll / ><fs> cat /foo/resolv.conf
guestfish --ro -a disk.img -m /dev/sda1
><fs> ll /
><fs> cat /foo/resolv.conf
By default (for C and C++ bindings only), libguestfs prints errors to stderr. You can change this behavior by setting an error handler. The guestfs(3) man page discusses this in detail.