一般libguestfs的调用模式是这样的(以执行类似于virt-df命令的一段代码为例):
#include #include#include#include
#define STRNEQ(a, b) (strcmp((a),(b)) != 0)
int main (int argc, char *argv[])
{
guestfs_h*g;inti, j;char *dom;char **devices =NULL;char **fses =NULL;if ((dom = argv[1]) ==NULL) {
perror("domain name is NULL");
exit (EXIT_FAILURE);
}
g=guestfs_create ();if (g ==NULL) {
perror ("failed to create libguestfs handle");
exit (EXIT_FAILURE);
}
guestfs_add_domain(g,(const char *)dom,GUESTFS_ADD_DOMAIN_READONLY,1,-1);
guestfs_set_identifier (g,dom);
guestfs_set_trace (g,0);
guestfs_set_verbose (g,0);if (guestfs_launch (g) == -1) {
perror ("guestfs_launch\n");return -1;
}
devices=guestfs_list_devices (g);if (devices ==NULL){
perror ("guesfs_list_devices(g) == NULL");return -1;
}
fses=guestfs_list_filesystems (g);if (fses ==NULL){
perror ("guestfs_list_filesystem(g)");return -1;
}for (i = 0; fses[i] != NULL; i += 2) {if (STRNEQ (fses[i+1], "") &&STRNEQ (fses[i+1]