[PATCH v7 3/8] parallels: implement functions for domain life cycle management

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

 



Add functions for create/shutdown/destroy and suspend/resume domain.

Signed-off-by: Dmitry Guryanov <dguryanov@xxxxxxxxxxxxx>
---
 src/parallels/parallels_driver.c |  149 ++++++++++++++++++++++++++++++++++++++
 src/parallels/parallels_driver.h |    1 +
 src/parallels/parallels_utils.c  |   18 +++++
 3 files changed, 168 insertions(+), 0 deletions(-)

diff --git a/src/parallels/parallels_driver.c b/src/parallels/parallels_driver.c
index 69bc966..8278071 100644
--- a/src/parallels/parallels_driver.c
+++ b/src/parallels/parallels_driver.c
@@ -59,6 +59,11 @@
 static void parallelsFreeDomObj(void *p);
 static virCapsPtr parallelsBuildCapabilities(void);
 static int parallelsClose(virConnectPtr conn);
+static int parallelsPause(virDomainObjPtr privdom);
+static int parallelsResume(virDomainObjPtr privdom);
+static int parallelsStart(virDomainObjPtr privdom);
+static int parallelsKill(virDomainObjPtr privdom);
+static int parallelsStop(virDomainObjPtr privdom);
 
 static void
 parallelsDriverLock(parallelsConnPtr driver)
@@ -84,6 +89,12 @@ parallelsFreeDomObj(void *p)
     VIR_FREE(p);
 };
 
+static void
+parallelsDomainEventQueue(parallelsConnPtr driver, virDomainEventPtr event)
+{
+    virDomainEventStateQueue(driver->domainEventState, event);
+}
+
 static virCapsPtr
 parallelsBuildCapabilities(void)
 {
@@ -761,6 +772,139 @@ parallelsDomainGetAutostart(virDomainPtr domain, int *autostart)
     return ret;
 }
 
+typedef int (*parallelsChangeState)    (virDomainObjPtr privdom);
+#define PARALLELS_UUID(x)     (((parallelsDomObjPtr)(x->privateData))->uuid)
+
+static int
+parallelsDomainChangeState(virDomainPtr domain,
+                     virDomainState req_state, const char *req_state_name,
+                     parallelsChangeState chstate,
+                     virDomainState new_state, int reason,
+                     int event_type, int event_detail)
+{
+    parallelsConnPtr privconn = domain->conn->privateData;
+    virDomainObjPtr privdom;
+    virDomainEventPtr event = NULL;
+    int state;
+    int ret = -1;
+
+    parallelsDriverLock(privconn);
+    privdom = virDomainFindByName(&privconn->domains, domain->name);
+    parallelsDriverUnlock(privconn);
+
+    if (privdom == NULL) {
+        parallelsError(VIR_ERR_NO_DOMAIN,
+                 _("no domain with matching name '%s'"), domain->name);
+        goto cleanup;
+    }
+
+    state = virDomainObjGetState(privdom, NULL);
+    if (state != req_state) {
+        parallelsError(VIR_ERR_INTERNAL_ERROR, _("domain '%s' not %s"),
+                 privdom->def->name, req_state_name);
+        goto cleanup;
+    }
+
+    if (chstate(privdom))
+        goto cleanup;
+
+    virDomainObjSetState(privdom, new_state, reason);
+
+    event = virDomainEventNewFromObj(privdom, event_type, event_detail);
+    ret = 0;
+
+  cleanup:
+    if (privdom)
+        virDomainObjUnlock(privdom);
+
+    if (event) {
+        parallelsDriverLock(privconn);
+        parallelsDomainEventQueue(privconn, event);
+        parallelsDriverUnlock(privconn);
+    }
+    return ret;
+}
+
+static int parallelsPause(virDomainObjPtr privdom)
+{
+    return parallelsCmdRun(PRLCTL, "pause", PARALLELS_UUID(privdom), NULL);
+}
+
+static int
+parallelsPauseDomain(virDomainPtr domain)
+{
+    return parallelsDomainChangeState(domain,
+                                VIR_DOMAIN_RUNNING, "running",
+                                parallelsPause,
+                                VIR_DOMAIN_PAUSED, VIR_DOMAIN_PAUSED_USER,
+                                VIR_DOMAIN_EVENT_SUSPENDED,
+                                VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
+}
+
+static int parallelsResume(virDomainObjPtr privdom)
+{
+    return parallelsCmdRun(PRLCTL, "resume", PARALLELS_UUID(privdom), NULL);
+}
+
+static int
+parallelsResumeDomain(virDomainPtr domain)
+{
+    return parallelsDomainChangeState(domain,
+                                VIR_DOMAIN_PAUSED, "paused",
+                                parallelsResume,
+                                VIR_DOMAIN_RUNNING, VIR_DOMAIN_RUNNING_UNPAUSED,
+                                VIR_DOMAIN_EVENT_RESUMED,
+                                VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
+}
+
+static int parallelsStart(virDomainObjPtr privdom)
+{
+    return parallelsCmdRun(PRLCTL, "start", PARALLELS_UUID(privdom), NULL);
+}
+
+static int
+parallelsDomainCreate(virDomainPtr domain)
+{
+    return parallelsDomainChangeState(domain,
+                                VIR_DOMAIN_SHUTOFF, "stopped",
+                                parallelsStart,
+                                VIR_DOMAIN_RUNNING, VIR_DOMAIN_EVENT_STARTED_BOOTED,
+                                VIR_DOMAIN_EVENT_STARTED,
+                                VIR_DOMAIN_EVENT_STARTED_BOOTED);
+}
+
+static int parallelsKill(virDomainObjPtr privdom)
+{
+    return parallelsCmdRun(PRLCTL, "stop", PARALLELS_UUID(privdom), "--kill", NULL);
+}
+
+static int
+parallelsDestroyDomain(virDomainPtr domain)
+{
+    return parallelsDomainChangeState(domain,
+                                VIR_DOMAIN_RUNNING, "running",
+                                parallelsKill,
+                                VIR_DOMAIN_SHUTOFF, VIR_DOMAIN_SHUTOFF_DESTROYED,
+                                VIR_DOMAIN_EVENT_STOPPED,
+                                VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
+}
+
+static int parallelsStop(virDomainObjPtr privdom)
+{
+    return parallelsCmdRun(PRLCTL, "stop", PARALLELS_UUID(privdom), NULL);
+}
+
+static int
+parallelsShutdownDomain(virDomainPtr domain)
+{
+    return parallelsDomainChangeState(domain,
+                                VIR_DOMAIN_RUNNING, "running",
+                                parallelsStop,
+                                VIR_DOMAIN_SHUTOFF, VIR_DOMAIN_SHUTOFF_SHUTDOWN,
+                                VIR_DOMAIN_EVENT_STOPPED,
+                                VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
+}
+
 static virDriver parallelsDriver = {
     .no = VIR_DRV_PARALLELS,
     .name = "PARALLELS",
@@ -783,6 +927,11 @@ static virDriver parallelsDriver = {
     .domainGetXMLDesc = parallelsDomainGetXMLDesc,    /* 0.9.12 */
     .domainIsPersistent = parallelsDomainIsPersistent,        /* 0.9.12 */
     .domainGetAutostart = parallelsDomainGetAutostart,        /* 0.9.12 */
+    .domainSuspend = parallelsPauseDomain,    /* 0.9.12 */
+    .domainResume = parallelsResumeDomain,    /* 0.9.12 */
+    .domainDestroy = parallelsDestroyDomain,  /* 0.9.12 */
+    .domainShutdown = parallelsShutdownDomain, /* 0.9.12 */
+    .domainCreate = parallelsDomainCreate,    /* 0.9.12 */
 };
 
 /**
diff --git a/src/parallels/parallels_driver.h b/src/parallels/parallels_driver.h
index 8398c02..2b3c956 100644
--- a/src/parallels/parallels_driver.h
+++ b/src/parallels/parallels_driver.h
@@ -61,5 +61,6 @@ typedef struct _parallelsConn *parallelsConnPtr;
 int parallelsRegister(void);
 
 virJSONValuePtr parallelsParseOutput(const char *binary, ...) ATTRIBUTE_NONNULL(1) ATTRIBUTE_SENTINEL;
+int parallelsCmdRun(const char *binary, ...) ATTRIBUTE_NONNULL(1) ATTRIBUTE_SENTINEL;
 
 #endif
diff --git a/src/parallels/parallels_utils.c b/src/parallels/parallels_utils.c
index 845adf4..e4220e9 100644
--- a/src/parallels/parallels_utils.c
+++ b/src/parallels/parallels_utils.c
@@ -87,3 +87,21 @@ parallelsParseOutput(const char *binary, ...)
     VIR_FREE(outbuf);
     return jobj;
 }
+
+/*
+ * Run prlctl command and check for errors
+ *
+ * Return value is 0 in case of success, else - -1
+ */
+int
+parallelsCmdRun(const char *binary, ...)
+{
+    int ret;
+    va_list list;
+
+    va_start(list, binary);
+    ret = parallelsDoCmdRun(NULL, binary, list);
+    va_end(list);
+
+    return ret;
+}
-- 
1.7.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]