[Libguestfs] [PATCH 09/18] rhv-upload: Extract create_disk() function

Nir Soffer nirsof at gmail.com
Sun Nov 17 23:04:31 UTC 2019


Creating a disk is complex and clumsy. Moving the code to a helper
function cleans open() and will make it easier to fix error handling.
---
 v2v/rhv-upload-plugin.py | 96 ++++++++++++++++++++++------------------
 1 file changed, 54 insertions(+), 42 deletions(-)

diff --git a/v2v/rhv-upload-plugin.py b/v2v/rhv-upload-plugin.py
index 809917562..9b433bd7e 100644
--- a/v2v/rhv-upload-plugin.py
+++ b/v2v/rhv-upload-plugin.py
@@ -77,50 +77,10 @@ def open(readonly):
         insecure = params['insecure'],
     )
 
-    system_service = connection.system_service()
-
-    # Create the disk.
-    disks_service = system_service.disks_service()
-    if params['disk_format'] == "raw":
-        disk_format = types.DiskFormat.RAW
-    else:
-        disk_format = types.DiskFormat.COW
-    disk = disks_service.add(
-        disk = types.Disk(
-            # The ID is optional.
-            id = params.get('rhv_disk_uuid'),
-            name = params['disk_name'],
-            description = "Uploaded by virt-v2v",
-            format = disk_format,
-            initial_size = params['disk_size'],
-            provisioned_size = params['disk_size'],
-            # XXX Ignores params['output_sparse'].
-            # Handling this properly will be complex, see:
-            # https://www.redhat.com/archives/libguestfs/2018-March/msg00177.html
-            sparse = True,
-            storage_domains = [
-                types.StorageDomain(
-                    name = params['output_storage'],
-                )
-            ],
-        )
-    )
-
-    # Wait till the disk is up, as the transfer can't start if the
-    # disk is locked:
-    disk_service = disks_service.disk_service(disk.id)
-    debug("disk.id = %r" % disk.id)
-
-    endt = time.time() + timeout
-    while True:
-        time.sleep(1)
-        disk = disk_service.get()
-        if disk.status == types.DiskStatus.OK:
-            break
-        if time.time() > endt:
-            raise RuntimeError("timed out waiting for disk to become unlocked")
+    disk = create_disk(connection)
 
     # Get a reference to the transfer service.
+    system_service = connection.system_service()
     transfers_service = system_service.image_transfers_service()
 
     # Create a new image transfer, using the local host is possible.
@@ -566,3 +526,55 @@ def find_host(connection):
     debug("host.id = %r" % host.id)
 
     return types.Host(id = host.id)
+
+def create_disk(connection):
+    """
+    Create a new disk for the transfer and wait until the disk is ready.
+
+    Returns disk object.
+    """
+    system_service = connection.system_service()
+    disks_service = system_service.disks_service()
+
+    if params['disk_format'] == "raw":
+        disk_format = types.DiskFormat.RAW
+    else:
+        disk_format = types.DiskFormat.COW
+
+    disk = disks_service.add(
+        disk = types.Disk(
+            # The ID is optional.
+            id = params.get('rhv_disk_uuid'),
+            name = params['disk_name'],
+            description = "Uploaded by virt-v2v",
+            format = disk_format,
+            initial_size = params['disk_size'],
+            provisioned_size = params['disk_size'],
+            # XXX Ignores params['output_sparse'].
+            # Handling this properly will be complex, see:
+            # https://www.redhat.com/archives/libguestfs/2018-March/msg00177.html
+            sparse = True,
+            storage_domains = [
+                types.StorageDomain(
+                    name = params['output_storage'],
+                )
+            ],
+        )
+    )
+
+    debug("disk.id = %r" % disk.id)
+
+    # Wait till the disk moved from LOCKED state to OK state, as the transfer
+    # can't start if the disk is locked.
+
+    disk_service = disks_service.disk_service(disk.id)
+    endt = time.time() + timeout
+    while True:
+        time.sleep(1)
+        disk = disk_service.get()
+        if disk.status == types.DiskStatus.OK:
+            break
+        if time.time() > endt:
+            raise RuntimeError("timed out waiting for disk to become unlocked")
+
+    return disk
-- 
2.21.0





More information about the Libguestfs mailing list