]> xenbits.xensource.com Git - libvirt.git/commitdiff
storage: Introduce virStorageBackendDeviceIsEmpty
authorJohn Ferlan <jferlan@redhat.com>
Wed, 14 Dec 2016 19:49:27 +0000 (14:49 -0500)
committerJohn Ferlan <jferlan@redhat.com>
Tue, 10 Jan 2017 13:44:50 +0000 (08:44 -0500)
Rename virStorageBackendFileSystemProbe and to virStorageBackendBLKIDFindFS
and move to the more common storage_backend module.

Create a shim virStorageBackendDeviceIsEmpty which will make the call
to the virStorageBackendBLKIDFindFS and check the return value.

Signed-off-by: John Ferlan <jferlan@redhat.com>
src/storage/storage_backend.c
src/storage/storage_backend.h
src/storage/storage_backend_fs.c

index 116eb80711761126b5730d0ad2767bbb77174834..a112ccd680c04d68e7bdff8b4fb9f59bc1acafcd 100644 (file)
 # endif
 #endif
 
+#if WITH_BLKID
+# include <blkid/blkid.h>
+#endif
+
 #if WITH_SELINUX
 # include <selinux/selinux.h>
 #endif
@@ -2632,3 +2636,116 @@ virStorageBackendFindGlusterPoolSources(const char *host ATTRIBUTE_UNUSED,
     return 0;
 }
 #endif /* #ifdef GLUSTER_CLI */
+
+
+#if WITH_BLKID
+/*
+ * @device: Path to device
+ * @format: Desired format
+ *
+ * Use the blkid_ APIs in order to get details regarding whether a file
+ * system exists on the disk already.
+ *
+ * Returns @virStoragePoolProbeResult value, where any error will also
+ * set the error message.
+ */
+static virStoragePoolProbeResult
+virStorageBackendBLKIDFindFS(const char *device,
+                             const char *format)
+{
+
+    virStoragePoolProbeResult ret = FILESYSTEM_PROBE_ERROR;
+    blkid_probe probe = NULL;
+    const char *fstype = NULL;
+    char *names[2], *libblkid_format = NULL;
+
+    VIR_DEBUG("Probing for existing filesystem of type %s on device %s",
+              format, device);
+
+    if (blkid_known_fstype(format) == 0) {
+        virReportError(VIR_ERR_STORAGE_PROBE_FAILED,
+                       _("Not capable of probing for "
+                         "filesystem of type %s"),
+                       format);
+        goto error;
+    }
+
+    probe = blkid_new_probe_from_filename(device);
+    if (probe == NULL) {
+        virReportError(VIR_ERR_STORAGE_PROBE_FAILED,
+                       _("Failed to create filesystem probe "
+                         "for device %s"),
+                       device);
+        goto error;
+    }
+
+    if (VIR_STRDUP(libblkid_format, format) < 0)
+        goto error;
+
+    names[0] = libblkid_format;
+    names[1] = NULL;
+
+    blkid_probe_filter_superblocks_type(probe,
+                                        BLKID_FLTR_ONLYIN,
+                                        names);
+
+    if (blkid_do_probe(probe) != 0) {
+        VIR_INFO("No filesystem of type '%s' found on device '%s'",
+                 format, device);
+        ret = FILESYSTEM_PROBE_NOT_FOUND;
+    } else if (blkid_probe_lookup_value(probe, "TYPE", &fstype, NULL) == 0) {
+        virReportError(VIR_ERR_STORAGE_POOL_BUILT,
+                       _("Existing filesystem of type '%s' found on "
+                         "device '%s'"),
+                       fstype, device);
+        ret = FILESYSTEM_PROBE_FOUND;
+    }
+
+    if (blkid_do_probe(probe) != 1) {
+        virReportError(VIR_ERR_STORAGE_PROBE_FAILED, "%s",
+                       _("Found additional probes to run, "
+                         "filesystem probing may be incorrect"));
+        ret = FILESYSTEM_PROBE_ERROR;
+    }
+
+ error:
+    VIR_FREE(libblkid_format);
+
+    if (probe != NULL)
+        blkid_free_probe(probe);
+
+    return ret;
+}
+
+#else /* #if WITH_BLKID */
+
+static virStoragePoolProbeResult
+virStorageBackendBLKIDFindFS(const char *device ATTRIBUTE_UNUSED,
+                             const char *format ATTRIBUTE_UNUSED)
+{
+    virReportError(VIR_ERR_OPERATION_INVALID, "%s",
+                   _("probing for filesystems is unsupported "
+                     "by this build"));
+    return FILESYSTEM_PROBE_ERROR;
+}
+
+#endif /* #if WITH_BLKID */
+
+
+/* virStorageBackendDeviceIsEmpty:
+ * @devpath: Path to the device to check
+ * @format: Desired format string
+ *
+ * Check if the @devpath has some sort of known file system using the
+ * BLKID API if available.
+ *
+ * Returns true if the probe deems the device has nothing valid on it
+ * and returns false if the probe finds something
+ */
+bool
+virStorageBackendDeviceIsEmpty(const char *devpath,
+                               const char *format)
+{
+    return virStorageBackendBLKIDFindFS(devpath, format) ==
+        FILESYSTEM_PROBE_NOT_FOUND;
+}
index 82fbbbf6d2c66df7be3e59c55d2c2ca760bc3aa1..c7b2e32d184bd83be46e06bfda7bd835b5819762 100644 (file)
@@ -158,6 +158,9 @@ int virStorageBackendVolWipeLocal(virConnectPtr conn,
                                   unsigned int algorithm,
                                   unsigned int flags);
 
+bool virStorageBackendDeviceIsEmpty(const char *devpath,
+                                    const char *format);
+
 typedef struct _virStorageBackend virStorageBackend;
 typedef virStorageBackend *virStorageBackendPtr;
 
index 9de0c17c06718c0cff1b22fbd24bbc756f63ee5c..a85e39c54734b1b27b395101606cc91ea89e6805 100644 (file)
 #include <libxml/tree.h>
 #include <libxml/xpath.h>
 
-#if WITH_BLKID
-# include <blkid/blkid.h>
-#endif
-
 #include "virerror.h"
 #include "storage_backend_fs.h"
 #include "storage_conf.h"
@@ -617,89 +613,6 @@ virStorageBackendFileSystemStart(virConnectPtr conn ATTRIBUTE_UNUSED,
 }
 #endif /* WITH_STORAGE_FS */
 
-#if WITH_BLKID
-static virStoragePoolProbeResult
-virStorageBackendFileSystemProbe(const char *device,
-                                 const char *format)
-{
-
-    virStoragePoolProbeResult ret = FILESYSTEM_PROBE_ERROR;
-    blkid_probe probe = NULL;
-    const char *fstype = NULL;
-    char *names[2], *libblkid_format = NULL;
-
-    VIR_DEBUG("Probing for existing filesystem of type %s on device %s",
-              format, device);
-
-    if (blkid_known_fstype(format) == 0) {
-        virReportError(VIR_ERR_STORAGE_PROBE_FAILED,
-                       _("Not capable of probing for "
-                         "filesystem of type %s"),
-                       format);
-        goto error;
-    }
-
-    probe = blkid_new_probe_from_filename(device);
-    if (probe == NULL) {
-        virReportError(VIR_ERR_STORAGE_PROBE_FAILED,
-                       _("Failed to create filesystem probe "
-                         "for device %s"),
-                       device);
-        goto error;
-    }
-
-    if (VIR_STRDUP(libblkid_format, format) < 0)
-        goto error;
-
-    names[0] = libblkid_format;
-    names[1] = NULL;
-
-    blkid_probe_filter_superblocks_type(probe,
-                                        BLKID_FLTR_ONLYIN,
-                                        names);
-
-    if (blkid_do_probe(probe) != 0) {
-        VIR_INFO("No filesystem of type '%s' found on device '%s'",
-                 format, device);
-        ret = FILESYSTEM_PROBE_NOT_FOUND;
-    } else if (blkid_probe_lookup_value(probe, "TYPE", &fstype, NULL) == 0) {
-        virReportError(VIR_ERR_STORAGE_POOL_BUILT,
-                       _("Existing filesystem of type '%s' found on "
-                         "device '%s'"),
-                       fstype, device);
-        ret = FILESYSTEM_PROBE_FOUND;
-    }
-
-    if (blkid_do_probe(probe) != 1) {
-        virReportError(VIR_ERR_STORAGE_PROBE_FAILED, "%s",
-                       _("Found additional probes to run, "
-                         "filesystem probing may be incorrect"));
-        ret = FILESYSTEM_PROBE_ERROR;
-    }
-
- error:
-    VIR_FREE(libblkid_format);
-
-    if (probe != NULL)
-        blkid_free_probe(probe);
-
-    return ret;
-}
-
-#else /* #if WITH_BLKID */
-
-static virStoragePoolProbeResult
-virStorageBackendFileSystemProbe(const char *device ATTRIBUTE_UNUSED,
-                                 const char *format ATTRIBUTE_UNUSED)
-{
-    virReportError(VIR_ERR_OPERATION_INVALID, "%s",
-                   _("probing for filesystems is unsupported "
-                     "by this build"));
-
-    return FILESYSTEM_PROBE_ERROR;
-}
-
-#endif /* #if WITH_BLKID */
 
 /* some platforms don't support mkfs */
 #ifdef MKFS
@@ -780,8 +693,7 @@ virStorageBackendMakeFileSystem(virStoragePoolObjPtr pool,
     if (flags & VIR_STORAGE_POOL_BUILD_OVERWRITE) {
         ok_to_mkfs = true;
     } else if (flags & VIR_STORAGE_POOL_BUILD_NO_OVERWRITE &&
-               virStorageBackendFileSystemProbe(device, format) ==
-               FILESYSTEM_PROBE_NOT_FOUND) {
+               virStorageBackendDeviceIsEmpty(device, format)) {
         ok_to_mkfs = true;
     }