Show Table of Contents
22.10. プログラミング言語での API の使用
libguestfs の API は、 Red Hat Enterprise Linux 7 の C、C++、Perl、Python、Java、Ruby、OCaml の言語で直接使用することができます。
- C と C++ のバインディングをインストールするには、以下のコマンドを入力します。
#
yum install libguestfs-devel - Perl バインディングをインストールする場合
#
yum install 'perl(Sys::Guestfs)' - Python バインディングをインストールする場合
#
yum install python-libguestfs - Java バインディングをインストールする場合
#
yum install libguestfs-java libguestfs-java-devel libguestfs-javadoc - Ruby バインディングをインストールする場合
#
yum install ruby-libguestfs - OCaml バインディングをインストールする場合
#
yum install ocaml-libguestfs ocaml-libguestfs-devel
各言語のバインディングは基本的には同じですが、構造上の若干の変更点があります。以下に A C ステートメント示します。
guestfs_launch (g);
Perl では、以下のように表されます。
$g->launch ()
または、OCaml では以下のように表されます。
g#launch ()
このセクションでは C の API のみを詳しく説明します。
C と C++ バインディングでは、エラーのチェックは手作業で行う必要があります。他のバインディングでは、エラーは例外に変換されます。以下の例に示す追加のエラーチェックは他の言語では必要ありませんが、例外をキャッチするためにコードを追加したいと思われるかもしれません。libguestfs API のアーキテクチャーに関する注意点を以下に示します。
- libguestfs API は同期します。各呼び出しは完了するまでブロックします。非同期に呼び出しを行いたい場合はスレッドを作成する必要があります。
- libguestfs API はスレッドセーフではありません。1 スレッドにつき 1 ハンドルしか使用できません。複数のスレッドで 1 つのハンドルを共有したい場合は、2 つのスレッドが 1 つのハンドルで同時にコマンドを実行できないよう独自の相互排除を実装してください。
- 同じディスクイメージ上で複数のハンドルを開かないようにしてください。ハンドルがすべて読み取り専用の場合は許容されますが、推奨はされていません。
- 他の何かがディスクイメージを使用している可能性がある場合は書き込み用のディスクイメージは追加しないでください (ライブ仮想マシンなど)。これを行うとディスクの破損を招きます。
- 現在使用中のディスクイメージで読み取り専用のハンドルを開くことは可能ですが (ライブの仮想マシンなど)、予測できない結果を招いたり、整合性を失う場合があります。とくにディスクイメージを読み込んでいる最中に大量の書き込みがあった場合にこのような結果になる可能性があります。
22.10.1. C プログラムでの API との対話
C プログラムは
<guestfs.h> ヘッダーファイルを組み込み、ハンドルを作成するところから開始します。
#include <stdio.h>
#include <stdlib.h>
#include <guestfs.h>
int
main (int argc, char *argv[])
{
guestfs_h *g;
g = guestfs_create ();
if (g == NULL) {
perror ("failed to create libguestfs handle");
exit (EXIT_FAILURE);
}
/* ... */
guestfs_close (g);
exit (EXIT_SUCCESS);
}
このプログラムをファイル (
test.c) に保存します。 このプログラムをコンパイルしてから次の 2 つのコマンドで実行します。
gcc -Wall test.c -o test -lguestfs ./test
この時点では何も出力されないはずです。このセクションの以降の部分では、このプログラムを拡張して新規のディスクイメージの作成やパーティション設定、ext4 ファイルシステムでの形式、そのファイルシステム内でファイル作成を実行する方法の事例を示します。ディスクイメージ名は
disk.img であり、これは現行のディレクトリーに作成されます。
プログラムの概要を以下に示します。
- ハンドルの作成
- ディスクのハンドルへの追加
- libguestfs バックエンドの起動
- パーティション、ファイルシステムおよびファイル群の作成
- ハンドルを閉じて終了
以下に、修正したプログラムを示します。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <guestfs.h>
int
main (int argc, char *argv[])
{
guestfs_h *g;
size_t i;
g = guestfs_create ();
if (g == NULL) {
perror ("failed to create libguestfs handle");
exit (EXIT_FAILURE);
}
/* Create a raw-format sparse disk image, 512 MB in size. */
int fd = open ("disk.img", O_CREAT|O_WRONLY|O_TRUNC|O_NOCTTY, 0666);
if (fd == -1) {
perror ("disk.img");
exit (EXIT_FAILURE);
}
if (ftruncate (fd, 512 * 1024 * 1024) == -1) {
perror ("disk.img: truncate");
exit (EXIT_FAILURE);
}
if (close (fd) == -1) {
perror ("disk.img: close");
exit (EXIT_FAILURE);
}
/* Set the trace flag so that we can see each libguestfs call. */
guestfs_set_trace (g, 1);
/* Set the autosync flag so that the disk will be synchronized
* automatically when the libguestfs handle is closed.
*/
guestfs_set_autosync (g, 1);
/* Add the disk image to libguestfs. */
if (guestfs_add_drive_opts (g, "disk.img",
GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw", /* raw format */
GUESTFS_ADD_DRIVE_OPTS_READONLY, 0, /* for write */
-1 /* this marks end of optional arguments */ )
== -1)
exit (EXIT_FAILURE);
/* Run the libguestfs back-end. */
if (guestfs_launch (g) == -1)
exit (EXIT_FAILURE);
/* Get the list of devices. Because we only added one drive
* above, we expect that this list should contain a single
* element.
*/
char **devices = guestfs_list_devices (g);
if (devices == NULL)
exit (EXIT_FAILURE);
if (devices[0] == NULL || devices[1] != NULL) {
fprintf (stderr,
"error: expected a single device from list-devices\n");
exit (EXIT_FAILURE);
}
/* Partition the disk as one single MBR partition. */
if (guestfs_part_disk (g, devices[0], "mbr") == -1)
exit (EXIT_FAILURE);
/* Get the list of partitions. We expect a single element, which
* is the partition we have just created.
*/
char **partitions = guestfs_list_partitions (g);
if (partitions == NULL)
exit (EXIT_FAILURE);
if (partitions[0] == NULL || partitions[1] != NULL) {
fprintf (stderr,
"error: expected a single partition from list-partitions\n");
exit (EXIT_FAILURE);
}
/* Create an ext4 filesystem on the partition. */
if (guestfs_mkfs (g, "ext4", partitions[0]) == -1)
exit (EXIT_FAILURE);
/* Now mount the filesystem so that we can add files. */
if (guestfs_mount_options (g, "", partitions[0], "/") == -1)
exit (EXIT_FAILURE);
/* Create some files and directories. */
if (guestfs_touch (g, "/empty") == -1)
exit (EXIT_FAILURE);
const char *message = "Hello, world\n";
if (guestfs_write (g, "/hello", message, strlen (message)) == -1)
exit (EXIT_FAILURE);
if (guestfs_mkdir (g, "/foo") == -1)
exit (EXIT_FAILURE);
/* This uploads the local file /etc/resolv.conf into the disk image. */
if (guestfs_upload (g, "/etc/resolv.conf", "/foo/resolv.conf") == -1)
exit (EXIT_FAILURE);
/* Because 'autosync' was set (above) we can just close the handle
* and the disk contents will be synchronized. You can also do
* this manually by calling guestfs_umount_all and guestfs_sync.
*/
guestfs_close (g);
/* Free up the lists. */
for (i = 0; devices[i] != NULL; ++i)
free (devices[i]);
free (devices);
for (i = 0; partitions[i] != NULL; ++i)
free (partitions[i]);
free (partitions);
exit (EXIT_SUCCESS);
}
次の 2 つのコマンドを使用してこのプログラムをコンパイルしてから実行します。
gcc -Wall test.c -o test -lguestfs ./test
プログラムの実行が正しく終了すると、
disk.img という名前のディスクイメージが作成されるはずです。このイメージは guestfish で調べることができます。
guestfish --ro -a disk.img -m /dev/sda1 ><fs> ll / ><fs> cat /foo/resolv.conf
デフォルトでは (C と C++ のバインディングの場合のみ)、libguestfs は stderr にエラーを出力します。エラーハンドラーを設定すると、この動作を変更することができます。詳細については guestfs(3) の man ページに記載されています。

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.