[Libguestfs] [PATCH 2/3] mllib: Add Common_utils.is_partition function.

Richard W.M. Jones rjones at redhat.com
Thu Jun 2 15:11:36 UTC 2016


Returns true if the device is a host partition.

The only tedious bit of this patch is that now Common_utils depends on
Dev_t so we have to add extra objects every time something links with
Common_utils.
---
 builder/Makefile.am    |  2 ++
 customize/Makefile.am  |  2 ++
 dib/Makefile.am        |  2 ++
 get-kernel/Makefile.am |  2 ++
 mllib/common_utils.ml  | 13 +++++++++++++
 mllib/common_utils.mli |  4 ++++
 resize/Makefile.am     |  2 ++
 sparsify/Makefile.am   |  2 ++
 sysprep/Makefile.am    |  2 ++
 v2v/Makefile.am        |  4 ++++
 10 files changed, 35 insertions(+)

diff --git a/builder/Makefile.am b/builder/Makefile.am
index 7f363ab..974e80c 100644
--- a/builder/Makefile.am
+++ b/builder/Makefile.am
@@ -89,6 +89,7 @@ SOURCES_ML = \
 	builder.ml
 
 SOURCES_C = \
+	../mllib/dev_t-c.c \
 	../mllib/fsync-c.c \
 	../mllib/uri-c.c \
 	../mllib/mkdtemp-c.c \
@@ -134,6 +135,7 @@ BOBJECTS = \
 	$(top_builddir)/mllib/libdir.cmo \
 	$(top_builddir)/mllib/guestfs_config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
+	$(top_builddir)/mllib/dev_t.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(top_builddir)/mllib/fsync.cmo \
 	$(top_builddir)/mllib/planner.cmo \
diff --git a/customize/Makefile.am b/customize/Makefile.am
index e921c5c..34b40d5 100644
--- a/customize/Makefile.am
+++ b/customize/Makefile.am
@@ -69,6 +69,7 @@ SOURCES_C = \
 	../fish/uri.c \
 	../fish/file-edit.c \
 	../fish/file-edit.h \
+	../mllib/dev_t-c.c \
 	../mllib/uri-c.c \
 	crypt-c.c \
 	perl_edit-c.c
@@ -93,6 +94,7 @@ virt_customize_CFLAGS = \
 BOBJECTS = \
 	$(top_builddir)/mllib/guestfs_config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
+	$(top_builddir)/mllib/dev_t.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(top_builddir)/mllib/regedit.cmo \
 	$(top_builddir)/mllib/URI.cmo \
diff --git a/dib/Makefile.am b/dib/Makefile.am
index 5a3a7c7..d3917db 100644
--- a/dib/Makefile.am
+++ b/dib/Makefile.am
@@ -33,6 +33,7 @@ SOURCES_ML = \
 	dib.ml
 
 SOURCES_C = \
+	../mllib/dev_t-c.c \
 	../mllib/mkdtemp-c.c
 
 bin_PROGRAMS =
@@ -57,6 +58,7 @@ BOBJECTS = \
 	$(top_builddir)/mllib/libdir.cmo \
 	$(top_builddir)/mllib/guestfs_config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
+	$(top_builddir)/mllib/dev_t.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(top_builddir)/mllib/mkdtemp.cmo \
 	$(SOURCES_ML:.ml=.cmo)
diff --git a/get-kernel/Makefile.am b/get-kernel/Makefile.am
index dd13e63..b6e924f 100644
--- a/get-kernel/Makefile.am
+++ b/get-kernel/Makefile.am
@@ -27,6 +27,7 @@ SOURCES_ML = \
 	get_kernel.ml
 
 SOURCES_C = \
+	../mllib/dev_t-c.c \
 	../mllib/uri-c.c \
 	../fish/uri.c
 
@@ -56,6 +57,7 @@ BOBJECTS = \
 	$(top_builddir)/mllib/libdir.cmo \
 	$(top_builddir)/mllib/guestfs_config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
+	$(top_builddir)/mllib/dev_t.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(top_builddir)/mllib/URI.cmo \
 	$(SOURCES_ML:.ml=.cmo)
diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index 983a2e6..dfffae3 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -817,6 +817,19 @@ let is_char_device file =
   try (Unix.stat file).Unix.st_kind = Unix.S_CHR
   with Unix.Unix_error _ -> false
 
+let is_partition dev =
+  try
+    if not (is_block_device dev) then false
+    else (
+      let rdev = (Unix.stat dev).Unix.st_rdev in
+      let major = Dev_t.major rdev in
+      let minor = Dev_t.minor rdev in
+      let path = sprintf "/sys/dev/block/%d:%d/partition" major minor in
+      Unix.access path [F_OK];
+      true
+    )
+  with Unix.Unix_error _ -> false
+
 (* Annoyingly Sys.is_directory throws an exception on failure
  * (RHBZ#1022431).
  *)
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
index 5475b3e..3fcb602 100644
--- a/mllib/common_utils.mli
+++ b/mllib/common_utils.mli
@@ -293,6 +293,10 @@ val is_char_device : string -> bool
 val is_directory : string -> bool
 (** These don't throw exceptions, unlike the [Sys] functions. *)
 
+val is_partition : string -> bool
+(** Return true if the host device [dev] is a partition.  If it's
+    anything else, or missing, returns false. *)
+
 val absolute_path : string -> string
 (** Convert any path to an absolute path. *)
 
diff --git a/resize/Makefile.am b/resize/Makefile.am
index 06e5839..929f61c 100644
--- a/resize/Makefile.am
+++ b/resize/Makefile.am
@@ -30,6 +30,7 @@ SOURCES_ML = \
 	resize.ml
 
 SOURCES_C = \
+	../mllib/dev_t-c.c \
 	../mllib/fsync-c.c \
 	../fish/progress.c \
 	../mllib/progress-c.c \
@@ -58,6 +59,7 @@ BOBJECTS = \
 	$(top_builddir)/mllib/URI.cmo \
 	$(top_builddir)/mllib/guestfs_config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
+	$(top_builddir)/mllib/dev_t.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(SOURCES_ML:.ml=.cmo)
 XOBJECTS = $(BOBJECTS:.cmo=.cmx)
diff --git a/sparsify/Makefile.am b/sparsify/Makefile.am
index 68f7fe7..7fd27cb 100644
--- a/sparsify/Makefile.am
+++ b/sparsify/Makefile.am
@@ -37,6 +37,7 @@ SOURCES_ML = \
 
 SOURCES_C = \
 	../fish/progress.c \
+	../mllib/dev_t-c.c \
 	../mllib/progress-c.c \
 	statvfs-c.c
 
@@ -57,6 +58,7 @@ virt_sparsify_CFLAGS = \
 BOBJECTS = \
 	$(top_builddir)/mllib/guestfs_config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
+	$(top_builddir)/mllib/dev_t.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(top_builddir)/mllib/progress.cmo \
 	$(SOURCES_ML:.ml=.cmo)
diff --git a/sysprep/Makefile.am b/sysprep/Makefile.am
index 7913484..3783a0b 100644
--- a/sysprep/Makefile.am
+++ b/sysprep/Makefile.am
@@ -80,6 +80,7 @@ SOURCES_ML = \
 	main.ml
 
 SOURCES_C = \
+	../mllib/dev_t-c.c \
 	../mllib/uri-c.c \
 	../mllib/mkdtemp-c.c \
 	../customize/crypt-c.c \
@@ -106,6 +107,7 @@ virt_sysprep_CFLAGS = \
 BOBJECTS = \
 	$(top_builddir)/mllib/guestfs_config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
+	$(top_builddir)/mllib/dev_t.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(top_builddir)/mllib/URI.cmo \
 	$(top_builddir)/mllib/mkdtemp.cmo \
diff --git a/v2v/Makefile.am b/v2v/Makefile.am
index f4bed83..e1f016b 100644
--- a/v2v/Makefile.am
+++ b/v2v/Makefile.am
@@ -114,6 +114,7 @@ SOURCES_ML = \
 	v2v.ml
 
 SOURCES_C = \
+	../mllib/dev_t-c.c \
 	../mllib/mkdtemp-c.c \
 	domainxml-c.c \
 	changeuid-c.c \
@@ -138,6 +139,7 @@ virt_v2v_CFLAGS = \
 BOBJECTS = \
 	$(top_builddir)/mllib/guestfs_config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
+	$(top_builddir)/mllib/dev_t.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(top_builddir)/mllib/regedit.cmo \
 	$(top_builddir)/mllib/mkdtemp.cmo \
@@ -187,6 +189,7 @@ virt_v2v_LINK = \
 	  $(OBJECTS) -o $@
 
 virt_v2v_copy_to_local_SOURCES = \
+	../mllib/dev_t-c.c \
 	domainxml-c.c \
 	utils-c.c \
 	xml-c.c
@@ -203,6 +206,7 @@ virt_v2v_copy_to_local_CFLAGS = \
 COPY_TO_LOCAL_BOBJECTS = \
 	$(top_builddir)/mllib/guestfs_config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
+	$(top_builddir)/mllib/dev_t.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(top_builddir)/mllib/JSON.cmo \
 	xml.cmo \
-- 
2.7.4




More information about the Libguestfs mailing list