From 553d21da6c3c14668f4bac2ee988363bda5515ca Mon Sep 17 00:00:00 2001 From: John Ferlan Date: Wed, 14 Dec 2016 14:49:27 -0500 Subject: [PATCH] storage: Introduce virStorageBackendDeviceIsEmpty 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 --- src/storage/storage_backend.c | 117 +++++++++++++++++++++++++++++++ src/storage/storage_backend.h | 3 + src/storage/storage_backend_fs.c | 90 +----------------------- 3 files changed, 121 insertions(+), 89 deletions(-) diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c index 116eb80711..a112ccd680 100644 --- a/src/storage/storage_backend.c +++ b/src/storage/storage_backend.c @@ -42,6 +42,10 @@ # endif #endif +#if WITH_BLKID +# include +#endif + #if WITH_SELINUX # include #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; +} diff --git a/src/storage/storage_backend.h b/src/storage/storage_backend.h index 82fbbbf6d2..c7b2e32d18 100644 --- a/src/storage/storage_backend.h +++ b/src/storage/storage_backend.h @@ -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; diff --git a/src/storage/storage_backend_fs.c b/src/storage/storage_backend_fs.c index 9de0c17c06..a85e39c547 100644 --- a/src/storage/storage_backend_fs.c +++ b/src/storage/storage_backend_fs.c @@ -37,10 +37,6 @@ #include #include -#if WITH_BLKID -# include -#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; } -- GitLab