mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone
Blob Blame History Raw
From 53ea0055668938fc7b77431d54f36f9daa6da03f Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Thu, 2 Jun 2016 15:10:04 +0100
Subject: [PATCH] mllib: Add Common_utils.is_partition function.

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.

(cherry picked from commit df7a33768eab2381b958ede2c8a40bc9cbc87928)
---
 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        |  5 +++++
 10 files changed, 36 insertions(+)

diff --git a/builder/Makefile.am b/builder/Makefile.am
index f31aef9..54aa10b 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 033816c..661917a 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 b969677..d1674a9 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 da94ece..9d8fc61 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 2ba8f9a..c29dfa0 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -819,6 +819,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 ae89fd6..4cfe7d0 100644
--- a/mllib/common_utils.mli
+++ b/mllib/common_utils.mli
@@ -327,6 +327,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 f4d1626..e9f48da 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 3da7a7a..9df3e1f 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 b890bca..e439a88 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 5d0ce95..5bffbfe 100644
--- a/v2v/Makefile.am
+++ b/v2v/Makefile.am
@@ -113,6 +113,7 @@ SOURCES_ML = \
 	v2v.ml
 
 SOURCES_C = \
+	../mllib/dev_t-c.c \
 	../mllib/mkdtemp-c.c \
 	domainxml-c.c \
 	changeuid-c.c \
@@ -137,6 +138,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 \
 	$(top_builddir)/mllib/curl.cmo \
@@ -361,6 +365,7 @@ endif
 v2v_unit_tests_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 \
 	stringMap.cmo \
-- 
1.8.3.1