[libvirt] [PATCH 07/11] qemu: block: Add generator for creating storage with blockdev-create

Peter Krempa pkrempa at redhat.com
Thu Jul 4 14:26:31 UTC 2019


QEMU allows us to create storage on certain network protocols which
allow image creation through their API. Wire up the generator for using
it with libvirt as well as for local files.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/qemu/qemu_block.c | 88 +++++++++++++++++++++++++++++++++++++++++++
 src/qemu/qemu_block.h |  5 +++
 2 files changed, 93 insertions(+)

diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c
index 54dd2b5328..709ed29375 100644
--- a/src/qemu/qemu_block.c
+++ b/src/qemu/qemu_block.c
@@ -2187,3 +2187,91 @@ qemuBlockStorageSourceCreateGetFormatProps(virStorageSourcePtr src,
     virReportEnumRangeError(virStorageFileFormat, src->format);
     return -1;
 }
+
+
+/**
+ * qemuBlockStorageSourceCreateGetStorageProps:
+ * @src: storage source to create
+ * @props: filled with props to be used with 'blockdev-create' to create @src
+ *
+ * This function should be used only if @src->type is VIR_STORAGE_TYPE_NETWORK.
+ * Note that @props may be NULL if qemu does not support creation storage
+ * on given protocol. @src->physical is used as size for the storage.
+ */
+int
+qemuBlockStorageSourceCreateGetStorageProps(virStorageSourcePtr src,
+                                            virJSONValuePtr *props)
+{
+    int actualType = virStorageSourceGetActualType(src);
+    VIR_AUTOPTR(virJSONValue) location = NULL;
+    const char *driver = NULL;
+    const char *filename = NULL;
+
+    switch ((virStorageType) actualType) {
+    case VIR_STORAGE_TYPE_FILE:
+        driver = "file";
+        filename = src->path;
+        break;
+
+    case VIR_STORAGE_TYPE_NETWORK:
+        switch ((virStorageNetProtocol) src->protocol) {
+        case VIR_STORAGE_NET_PROTOCOL_GLUSTER:
+            driver = "gluster";
+            if (!(location = qemuBlockStorageSourceGetGlusterProps(src, false, false)))
+                return -1;
+            break;
+
+        case VIR_STORAGE_NET_PROTOCOL_RBD:
+            driver = "rbd";
+            if (!(location = qemuBlockStorageSourceGetRBDProps(src, false)))
+                return -1;
+            break;
+
+        case VIR_STORAGE_NET_PROTOCOL_SHEEPDOG:
+            driver = "sheepdog";
+            if (!(location = qemuBlockStorageSourceGetSheepdogProps(src)))
+                return -1;
+            break;
+
+        case VIR_STORAGE_NET_PROTOCOL_SSH:
+            driver = "ssh";
+            if (!(location = qemuBlockStorageSourceGetSshProps(src)))
+                return -1;
+            break;
+
+            /* unsupported/impossible */
+        case VIR_STORAGE_NET_PROTOCOL_NBD:
+        case VIR_STORAGE_NET_PROTOCOL_ISCSI:
+        case VIR_STORAGE_NET_PROTOCOL_VXHS:
+        case VIR_STORAGE_NET_PROTOCOL_HTTP:
+        case VIR_STORAGE_NET_PROTOCOL_HTTPS:
+        case VIR_STORAGE_NET_PROTOCOL_FTP:
+        case VIR_STORAGE_NET_PROTOCOL_FTPS:
+        case VIR_STORAGE_NET_PROTOCOL_TFTP:
+        case VIR_STORAGE_NET_PROTOCOL_NONE:
+        case VIR_STORAGE_NET_PROTOCOL_LAST:
+            return 0;
+        }
+        break;
+
+    case VIR_STORAGE_TYPE_BLOCK:
+    case VIR_STORAGE_TYPE_DIR:
+    case VIR_STORAGE_TYPE_VOLUME:
+        return 0;
+
+    case VIR_STORAGE_TYPE_NONE:
+    case VIR_STORAGE_TYPE_LAST:
+         virReportEnumRangeError(virStorageType, actualType);
+         return -1;
+    }
+
+    if (virJSONValueObjectCreate(props,
+                                 "s:driver", driver,
+                                 "S:filename", filename,
+                                 "A:location", &location,
+                                 "u:size", src->physical,
+                                 NULL) < 0)
+        return -1;
+
+    return 0;
+}
diff --git a/src/qemu/qemu_block.h b/src/qemu/qemu_block.h
index 738ef9e8b0..a5f6a3c75b 100644
--- a/src/qemu/qemu_block.h
+++ b/src/qemu/qemu_block.h
@@ -168,3 +168,8 @@ qemuBlockStorageSourceCreateGetFormatProps(virStorageSourcePtr src,
                                            virStorageSourcePtr backing,
                                            virJSONValuePtr *props)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
+
+int
+qemuBlockStorageSourceCreateGetStorageProps(virStorageSourcePtr src,
+                                            virJSONValuePtr *props)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
-- 
2.21.0




More information about the libvir-list mailing list