[Libguestfs] [PATCH-v3] blkid: start using libblkid directly instead

Wanlong Gao gaowanlong at cn.fujitsu.com
Mon Feb 20 01:40:53 UTC 2012


Hi Rich:
This is the v3 version of the starting using libblkid in libguestfs.

Now, use the pkg-config for libblkid.
I also tested on Fedora 16, and all tests passed.

This also avoid the cache like the existing code as Kzak said.

So, any further comments?

Thanks
-Wanlong Gao

---------------------------------------------------------------------------------
Use libblkid directly instead of the binary command in blkid.

Signed-off-by: Wanlong Gao <gaowanlong at cn.fujitsu.com>
---
 appliance/packagelist.in |    2 +
 configure.ac             |    9 +++++++
 daemon/Makefile.am       |    4 ++-
 daemon/blkid.c           |   59 +++++++++++++++++++++++++---------------------
 4 files changed, 46 insertions(+), 28 deletions(-)

diff --git a/appliance/packagelist.in b/appliance/packagelist.in
index 2ab6b80..1dedb58 100644
--- a/appliance/packagelist.in
+++ b/appliance/packagelist.in
@@ -45,6 +45,7 @@
   vim-minimal
   xz
   zfs-fuse
+  libblkid
 #endif /* REDHAT */
 
 #ifdef DEBIAN
@@ -70,6 +71,7 @@
   vim-tiny
   xz-utils
   zfs-fuse
+  libblkid1
 #endif /* DEBIAN */
 
 #ifdef ARCHLINUX
diff --git a/configure.ac b/configure.ac
index 31a2cc5..396d050 100644
--- a/configure.ac
+++ b/configure.ac
@@ -675,6 +675,15 @@ PKG_CHECK_MODULES([HIVEX], [hivex],
         [AC_MSG_WARN([hivex not found, some core features will be disabled])])
 AM_CONDITIONAL([HAVE_HIVEX],[test "x$HIVEX_LIBS" != "x"])
 
+dnl libblkid (highly recommended)
+PKG_CHECK_MODULES([LIBBLKID], [blkid],
+        [AC_SUBST([LIBBLKID_CFLAGS])
+         AC_SUBST([LIBBLKID_LIBS])
+         AC_DEFINE([HAVE_LIBBLKID],[1],[libblkid found at compile time.])
+        ],
+        [AC_MSG_WARN([libblkid not found, some features will be disabled])])
+AM_CONDITIONAL([HAVE_LIBBLKID],[test "x$LIBBLKID_LIBS" != "x"])
+
 dnl FUSE is optional to build the FUSE module.
 AC_ARG_ENABLE([fuse],
         AS_HELP_STRING([--disable-fuse], [Disable FUSE (guestmount) support]),
diff --git a/daemon/Makefile.am b/daemon/Makefile.am
index 3a698cc..8764b41 100644
--- a/daemon/Makefile.am
+++ b/daemon/Makefile.am
@@ -167,6 +167,7 @@ guestfsd_LDADD = \
 	liberrnostring.a \
 	libprotocol.a \
 	$(SELINUX_LIB) \
+	$(LIBBLKID_LIBS) \
 	$(AUGEAS_LIBS) \
 	$(top_builddir)/gnulib/lib/.libs/libgnu.a \
 	$(GETADDRINFO_LIB) \
@@ -178,6 +179,7 @@ guestfsd_LDADD = \
 	$(SERVENT_LIB)
 
 guestfsd_CPPFLAGS = -I$(top_srcdir)/gnulib/lib -I$(top_builddir)/gnulib/lib
-guestfsd_CFLAGS = $(WARN_CFLAGS) $(WERROR_CFLAGS) $(AUGEAS_CFLAGS)
+guestfsd_CFLAGS = $(WARN_CFLAGS) $(WERROR_CFLAGS) $(AUGEAS_CFLAGS) \
+                  $(LIBBLKID_CFLAGS)
 
 .PHONY: force
diff --git a/daemon/blkid.c b/daemon/blkid.c
index 8728c29..bc4c03d 100644
--- a/daemon/blkid.c
+++ b/daemon/blkid.c
@@ -23,6 +23,8 @@
 #include <string.h>
 #include <unistd.h>
 #include <limits.h>
+#include <fcntl.h>
+#include <blkid.h>
 
 #include "daemon.h"
 #include "actions.h"
@@ -30,40 +32,43 @@
 static char *
 get_blkid_tag (const char *device, const char *tag)
 {
-  char *out, *err;
-  int r;
+  int fd, rc;
+  const char *data = NULL;
+  blkid_probe blkprobe;
 
-  r = commandr (&out, &err,
-                "blkid",
-                /* Adding -c option kills all caching, even on RHEL 5. */
-                "-c", "/dev/null",
-                "-o", "value", "-s", tag, device, NULL);
-  if (r != 0 && r != 2) {
-    if (r >= 0)
-      reply_with_error ("%s: %s (blkid returned %d)", device, err, r);
-    else
-      reply_with_error ("%s: %s", device, err);
-    free (out);
-    free (err);
+  fd = open (device, O_RDONLY);
+  if (fd < 0) {
+    reply_with_perror ("open %s", device);
     return NULL;
   }
 
-  free (err);
+  blkprobe = blkid_new_probe ();
+  if (!blkprobe)
+    goto done;
+  if (blkid_probe_set_device (blkprobe, fd, 0, 0))
+    goto done;
 
-  if (r == 2) {                 /* means UUID etc not found */
-    free (out);
-    out = strdup ("");
-    if (out == NULL)
-      reply_with_perror ("strdup");
-    return out;
-  }
+  blkid_probe_enable_superblocks (blkprobe, 1);
+  blkid_probe_enable_partitions (blkprobe, 1);
 
-  /* Trim trailing \n if present. */
-  size_t len = strlen (out);
-  if (len > 0 && out[len-1] == '\n')
-    out[len-1] = '\0';
+  blkid_probe_set_superblocks_flags (blkprobe, BLKID_SUBLKS_LABEL |
+                                     BLKID_SUBLKS_UUID | BLKID_SUBLKS_TYPE);
+
+  rc = blkid_do_safeprobe (blkprobe);
+  if (!rc)
+    blkid_probe_lookup_value (blkprobe, tag, &data, NULL);
+
+done:
+  close (fd);
+  if (data)
+    data = strdup ((char *) data);
+  else
+    data = strdup ("");
+  blkid_free_probe (blkprobe);
 
-  return out;                   /* caller frees */
+  if (!data)
+    reply_with_perror ("strdup");
+  return (char *) data;
 }
 
 char *
-- 
1.7.9




More information about the Libguestfs mailing list