[PATCH 03/18] vz: reuse edit config frame in for attach/detach functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Attach/detach functions for disk/net are quite trivial and
typically call a few functions in begin/end edit frame. Having
in mind update function too adding configuring for another
device (like graphics) will introduce 3 trivial functions more.
Let's replace current approach by attach/detach functions for
device.

Signed-off-by: Nikolay Shirokovskiy <nshirokovskiy@xxxxxxxxxxxxx>
---
 src/vz/vz_driver.c |  60 ++++-----------------
 src/vz/vz_sdk.c    | 156 +++++++++++++++++++++++------------------------------
 src/vz/vz_sdk.h    |   8 +--
 3 files changed, 79 insertions(+), 145 deletions(-)

diff --git a/src/vz/vz_driver.c b/src/vz/vz_driver.c
index ffa6f45..ab4aa74 100644
--- a/src/vz/vz_driver.c
+++ b/src/vz/vz_driver.c
@@ -1114,6 +1114,7 @@ static int vzDomainAttachDeviceFlags(virDomainPtr dom, const char *xml,
     vzConnPtr privconn = dom->conn->privateData;
     virDomainDeviceDefPtr dev = NULL;
     virDomainObjPtr privdom = NULL;
+    vzDriverPtr driver = privconn->driver;
 
     virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                   VIR_DOMAIN_AFFECT_CONFIG, -1);
@@ -1124,34 +1125,13 @@ static int vzDomainAttachDeviceFlags(virDomainPtr dom, const char *xml,
     if (vzCheckConfigUpdateFlags(privdom, &flags) < 0)
         goto cleanup;
 
-    dev = virDomainDeviceDefParse(xml, privdom->def, privconn->driver->caps,
-                                  privconn->driver->xmlopt, VIR_DOMAIN_XML_INACTIVE);
+    dev = virDomainDeviceDefParse(xml, privdom->def, driver->caps,
+                                  driver->xmlopt, VIR_DOMAIN_XML_INACTIVE);
     if (dev == NULL)
         goto cleanup;
 
-    switch (dev->type) {
-    case VIR_DOMAIN_DEVICE_DISK:
-        ret = prlsdkAttachVolume(privconn->driver, privdom, dev->data.disk);
-        if (ret) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("disk attach failed"));
-            goto cleanup;
-        }
-        break;
-    case VIR_DOMAIN_DEVICE_NET:
-        ret = prlsdkAttachNet(privconn->driver, privdom, dev->data.net);
-        if (ret) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("network attach failed"));
-            goto cleanup;
-        }
-        break;
-    default:
-        virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
-                       _("device type '%s' cannot be attached"),
-                       virDomainDeviceTypeToString(dev->type));
-        break;
-    }
+    if (prlsdkAttachDevice(driver, privdom, dev) < 0)
+        goto cleanup;
 
     ret = 0;
  cleanup:
@@ -1172,6 +1152,7 @@ static int vzDomainDetachDeviceFlags(virDomainPtr dom, const char *xml,
     vzConnPtr privconn = dom->conn->privateData;
     virDomainDeviceDefPtr dev = NULL;
     virDomainObjPtr privdom = NULL;
+    vzDriverPtr driver = privconn->driver;
 
     virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                   VIR_DOMAIN_AFFECT_CONFIG, -1);
@@ -1183,34 +1164,13 @@ static int vzDomainDetachDeviceFlags(virDomainPtr dom, const char *xml,
     if (vzCheckConfigUpdateFlags(privdom, &flags) < 0)
         goto cleanup;
 
-    dev = virDomainDeviceDefParse(xml, privdom->def, privconn->driver->caps,
-                                  privconn->driver->xmlopt, VIR_DOMAIN_XML_INACTIVE);
+    dev = virDomainDeviceDefParse(xml, privdom->def, driver->caps,
+                                  driver->xmlopt, VIR_DOMAIN_XML_INACTIVE);
     if (dev == NULL)
         goto cleanup;
 
-    switch (dev->type) {
-    case VIR_DOMAIN_DEVICE_DISK:
-        ret = prlsdkDetachVolume(privdom, dev->data.disk);
-        if (ret) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("disk detach failed"));
-            goto cleanup;
-        }
-        break;
-    case VIR_DOMAIN_DEVICE_NET:
-        ret = prlsdkDetachNet(privconn->driver, privdom, dev->data.net);
-        if (ret) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("network detach failed"));
-            goto cleanup;
-        }
-        break;
-    default:
-        virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
-                       _("device type '%s' cannot be detached"),
-                       virDomainDeviceTypeToString(dev->type));
-        break;
-    }
+    if (prlsdkDetachDevice(driver, privdom, dev) < 0)
+        goto cleanup;
 
     ret = 0;
  cleanup:
diff --git a/src/vz/vz_sdk.c b/src/vz/vz_sdk.c
index 0f05dfd..cb624dc 100644
--- a/src/vz/vz_sdk.c
+++ b/src/vz/vz_sdk.c
@@ -3066,34 +3066,6 @@ prlsdkCleanupBridgedNet(vzDriverPtr driver, virDomainNetDefPtr net)
     PrlHandle_Free(vnet);
 }
 
-int prlsdkAttachNet(vzDriverPtr driver,
-                    virDomainObjPtr dom,
-                    virDomainNetDefPtr net)
-{
-    int ret = -1;
-    vzDomObjPtr privdom = dom->privateData;
-    PRL_HANDLE job = PRL_INVALID_HANDLE;
-
-    if (!IS_CT(dom->def)) {
-        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
-                       _("network device cannot be attached"));
-        return ret;
-    }
-
-    job = PrlVm_BeginEdit(privdom->sdkdom);
-    if (PRL_FAILED(waitJob(job)))
-        return ret;
-
-    ret = prlsdkAddNet(driver, privdom->sdkdom, net, IS_CT(dom->def));
-    if (ret == 0) {
-        job = PrlVm_CommitEx(privdom->sdkdom, PVCF_DETACH_HDD_BUNDLE);
-        if (PRL_FAILED(waitJob(job)))
-            return -1;
-    }
-
-    return ret;
-}
-
 static PRL_HANDLE
 prlsdkFindNetByMAC(PRL_HANDLE sdkdom, virMacAddrPtr mac)
 {
@@ -3129,46 +3101,6 @@ prlsdkFindNetByMAC(PRL_HANDLE sdkdom, virMacAddrPtr mac)
     return adapter;
 }
 
-int prlsdkDetachNet(vzDriverPtr driver,
-                    virDomainObjPtr dom,
-                    virDomainNetDefPtr net)
-{
-    int ret = -1;
-    vzDomObjPtr privdom = dom->privateData;
-    PRL_HANDLE job = PRL_INVALID_HANDLE;
-    PRL_HANDLE sdknet = PRL_INVALID_HANDLE;
-    PRL_RESULT pret;
-
-    if (!IS_CT(dom->def)) {
-        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
-                       _("network device cannot be detached"));
-        goto cleanup;
-    }
-
-    job = PrlVm_BeginEdit(privdom->sdkdom);
-    if (PRL_FAILED(waitJob(job)))
-        goto cleanup;
-
-    sdknet = prlsdkFindNetByMAC(privdom->sdkdom, &net->mac);
-    if (sdknet == PRL_INVALID_HANDLE)
-        goto cleanup;
-
-    prlsdkCleanupBridgedNet(driver, net);
-
-    pret = PrlVmDev_Remove(sdknet);
-    prlsdkCheckRetGoto(pret, cleanup);
-
-    job = PrlVm_CommitEx(privdom->sdkdom, PVCF_DETACH_HDD_BUNDLE);
-    if (PRL_FAILED(waitJob(job)))
-        goto cleanup;
-
-    ret = 0;
-
- cleanup:
-    PrlHandle_Free(sdknet);
-    return ret;
-}
-
 static int prlsdkAddDisk(vzDriverPtr driver,
                          PRL_HANDLE sdkdom,
                          virDomainDiskDefPtr disk)
@@ -3349,50 +3281,96 @@ prlsdkGetDisk(PRL_HANDLE sdkdom, virDomainDiskDefPtr disk, bool isCt)
 }
 
 int
-prlsdkAttachVolume(vzDriverPtr driver,
+prlsdkAttachDevice(vzDriverPtr driver,
                    virDomainObjPtr dom,
-                   virDomainDiskDefPtr disk)
+                   virDomainDeviceDefPtr dev)
 {
-    int ret = -1;
     vzDomObjPtr privdom = dom->privateData;
     PRL_HANDLE job = PRL_INVALID_HANDLE;
 
     job = PrlVm_BeginEdit(privdom->sdkdom);
     if (PRL_FAILED(waitJob(job)))
-        goto cleanup;
+        return -1;
 
-    ret = prlsdkAddDisk(driver, privdom->sdkdom, disk);
-    if (ret == 0) {
-        job = PrlVm_CommitEx(privdom->sdkdom, PVCF_DETACH_HDD_BUNDLE);
-        if (PRL_FAILED(waitJob(job))) {
-            ret = -1;
-            goto cleanup;
+    switch (dev->type) {
+    case VIR_DOMAIN_DEVICE_DISK:
+        if (prlsdkAddDisk(driver, privdom->sdkdom, dev->data.disk) < 0)
+            return -1;
+
+        break;
+    case VIR_DOMAIN_DEVICE_NET:
+        if (!IS_CT(dom->def)) {
+            virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
+                           _("attaching network device to VM is unsupported"));
+            return -1;
         }
+
+        if (prlsdkAddNet(driver, privdom->sdkdom, dev->data.net, IS_CT(dom->def)) < 0)
+            return -1;
+
+        break;
+    default:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("attaching device type '%s' is unsupported"),
+                       virDomainDeviceTypeToString(dev->type));
+        return -1;
     }
 
- cleanup:
-    return ret;
+    job = PrlVm_CommitEx(privdom->sdkdom, PVCF_DETACH_HDD_BUNDLE);
+    if (PRL_FAILED(waitJob(job)))
+        return -1;
+
+    return 0;
 }
 
 int
-prlsdkDetachVolume(virDomainObjPtr dom, virDomainDiskDefPtr disk)
+prlsdkDetachDevice(vzDriverPtr driver,
+                   virDomainObjPtr dom,
+                   virDomainDeviceDefPtr dev)
 {
     int ret = -1;
     vzDomObjPtr privdom = dom->privateData;
     PRL_HANDLE job = PRL_INVALID_HANDLE;
-    PRL_HANDLE sdkdisk;
+    PRL_HANDLE sdkdev = PRL_INVALID_HANDLE;
     PRL_RESULT pret;
 
-    sdkdisk = prlsdkGetDisk(privdom->sdkdom, disk, IS_CT(dom->def));
-    if (sdkdisk == PRL_INVALID_HANDLE)
-        goto cleanup;
-
     job = PrlVm_BeginEdit(privdom->sdkdom);
     if (PRL_FAILED(waitJob(job)))
         goto cleanup;
 
-    pret = PrlVmDev_Remove(sdkdisk);
-    prlsdkCheckRetGoto(pret, cleanup);
+    switch (dev->type) {
+    case VIR_DOMAIN_DEVICE_DISK:
+        sdkdev = prlsdkGetDisk(privdom->sdkdom, dev->data.disk, IS_CT(dom->def));
+        if (sdkdev == PRL_INVALID_HANDLE)
+            goto cleanup;
+
+        pret = PrlVmDev_Remove(sdkdev);
+        prlsdkCheckRetGoto(pret, cleanup);
+
+        break;
+    case VIR_DOMAIN_DEVICE_NET:
+        if (!IS_CT(dom->def)) {
+            virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
+                           _("detaching network device from VM is unsupported"));
+            goto cleanup;
+        }
+
+        sdkdev = prlsdkFindNetByMAC(privdom->sdkdom, &dev->data.net->mac);
+        if (sdkdev == PRL_INVALID_HANDLE)
+            goto cleanup;
+
+        prlsdkCleanupBridgedNet(driver, dev->data.net);
+
+        pret = PrlVmDev_Remove(sdkdev);
+        prlsdkCheckRetGoto(pret, cleanup);
+
+        break;
+    default:
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("detaching device type '%s' is unsupported"),
+                       virDomainDeviceTypeToString(dev->type));
+        goto cleanup;
+    }
 
     job = PrlVm_CommitEx(privdom->sdkdom, PVCF_DETACH_HDD_BUNDLE);
     if (PRL_FAILED(waitJob(job)))
@@ -3402,7 +3380,7 @@ prlsdkDetachVolume(virDomainObjPtr dom, virDomainDiskDefPtr disk)
 
  cleanup:
 
-    PrlHandle_Free(sdkdisk);
+    PrlHandle_Free(sdkdev);
     return ret;
 }
 
diff --git a/src/vz/vz_sdk.h b/src/vz/vz_sdk.h
index e562f98..e68a710 100644
--- a/src/vz/vz_sdk.h
+++ b/src/vz/vz_sdk.h
@@ -63,16 +63,12 @@ prlsdkUnregisterDomain(vzDriverPtr driver, virDomainObjPtr dom, unsigned int fla
 int
 prlsdkDomainManagedSaveRemove(virDomainObjPtr dom);
 int
-prlsdkAttachVolume(vzDriverPtr driver, virDomainObjPtr dom, virDomainDiskDefPtr disk);
+prlsdkAttachDevice(vzDriverPtr driver, virDomainObjPtr dom, virDomainDeviceDefPtr disk);
 int
-prlsdkDetachVolume(virDomainObjPtr dom, virDomainDiskDefPtr disk);
+prlsdkDetachDevice(vzDriverPtr driver, virDomainObjPtr dom, virDomainDeviceDefPtr disk);
 int
 prlsdkGetBlockStats(virDomainObjPtr dom, virDomainDiskDefPtr disk, virDomainBlockStatsPtr stats);
 int
-prlsdkAttachNet(vzDriverPtr driver, virDomainObjPtr dom, virDomainNetDefPtr net);
-int
-prlsdkDetachNet(vzDriverPtr driver, virDomainObjPtr dom, virDomainNetDefPtr net);
-int
 prlsdkGetNetStats(virDomainObjPtr dom, const char *path, virDomainInterfaceStatsPtr stats);
 int
 prlsdkGetVcpuStats(virDomainObjPtr dom, int idx, unsigned long long *time);
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]