[libvirt PATCH 1/7] qemu_saveimage: extract starting process to qemuSaveImageStartProcess

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

 



Part of qemuSaveImageStartVM() function will be used when reverting
external snapshots. To avoid duplicating code and logic extract the
shared bits into separate function.

Signed-off-by: Pavel Hrdina <phrdina@xxxxxxxxxx>
---
 src/qemu/qemu_saveimage.c | 103 ++++++++++++++++++++++++++------------
 src/qemu/qemu_saveimage.h |  12 +++++
 2 files changed, 83 insertions(+), 32 deletions(-)

diff --git a/src/qemu/qemu_saveimage.c b/src/qemu/qemu_saveimage.c
index 41310d6a9a..86f31d1820 100644
--- a/src/qemu/qemu_saveimage.c
+++ b/src/qemu/qemu_saveimage.c
@@ -565,42 +565,46 @@ qemuSaveImageOpen(virQEMUDriver *driver,
     return ret;
 }
 
+/**
+ * qemuSaveImageStartProcess:
+ * @conn: connection object
+ * @driver: qemu driver object
+ * @vm: domain object
+ * @fd: FD pointer of memory state file
+ * @path: path to memory state file
+ * @header: header from memory state file
+ * @cookie: cookie from memory state file
+ * @asyncJob: type of asynchronous job
+ * @start_flags: flags to start QEMU process with
+ * @started: boolean to store if QEMU process was started
+ *
+ * Start VM with existing memory state. Make sure that the stored memory state
+ * is correctly decompressed so it can be loaded by QEMU process.
+ *
+ * Returns 0 on success, -1 on error.
+ */
 int
-qemuSaveImageStartVM(virConnectPtr conn,
-                     virQEMUDriver *driver,
-                     virDomainObj *vm,
-                     int *fd,
-                     virQEMUSaveData *data,
-                     const char *path,
-                     bool start_paused,
-                     bool reset_nvram,
-                     virDomainAsyncJob asyncJob)
+qemuSaveImageStartProcess(virConnectPtr conn,
+                          virQEMUDriver *driver,
+                          virDomainObj *vm,
+                          int *fd,
+                          const char *path,
+                          virQEMUSaveHeader *header,
+                          qemuDomainSaveCookie *cookie,
+                          virDomainAsyncJob asyncJob,
+                          unsigned int start_flags,
+                          bool *started)
 {
     qemuDomainObjPrivate *priv = vm->privateData;
-    int ret = -1;
-    bool started = false;
-    virObjectEvent *event;
     VIR_AUTOCLOSE intermediatefd = -1;
     g_autoptr(virCommand) cmd = NULL;
     g_autofree char *errbuf = NULL;
-    g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
-    virQEMUSaveHeader *header = &data->header;
-    g_autoptr(qemuDomainSaveCookie) cookie = NULL;
     int rc = 0;
-    unsigned int start_flags = VIR_QEMU_PROCESS_START_PAUSED |
-        VIR_QEMU_PROCESS_START_GEN_VMID;
-
-    if (reset_nvram)
-        start_flags |= VIR_QEMU_PROCESS_START_RESET_NVRAM;
-
-    if (virSaveCookieParseString(data->cookie, (virObject **)&cookie,
-                                 virDomainXMLOptionGetSaveCookie(driver->xmlopt)) < 0)
-        goto cleanup;
 
     if ((header->version == 2) &&
         (header->compressed != QEMU_SAVE_FORMAT_RAW)) {
         if (!(cmd = qemuSaveImageGetCompressionCommand(header->compressed)))
-            goto cleanup;
+            return -1;
 
         intermediatefd = *fd;
         *fd = -1;
@@ -613,7 +617,7 @@ qemuSaveImageStartVM(virConnectPtr conn,
         if (virCommandRunAsync(cmd, NULL) < 0) {
             *fd = intermediatefd;
             intermediatefd = -1;
-            goto cleanup;
+            return -1;
         }
     }
 
@@ -622,7 +626,7 @@ qemuSaveImageStartVM(virConnectPtr conn,
      */
     if (cookie &&
         qemuDomainFixupCPUs(vm, &cookie->cpu) < 0)
-        goto cleanup;
+        return -1;
 
     if (cookie && !cookie->slirpHelper)
         priv->disableSlirp = true;
@@ -631,12 +635,12 @@ qemuSaveImageStartVM(virConnectPtr conn,
                          asyncJob, "stdio", *fd, path, NULL,
                          VIR_NETDEV_VPORT_PROFILE_OP_RESTORE,
                          start_flags) == 0)
-        started = true;
+        *started = true;
 
     if (intermediatefd != -1) {
         virErrorPtr orig_err = NULL;
 
-        if (!started) {
+        if (!*started) {
             /* if there was an error setting up qemu, the intermediate
              * process will wait forever to write to stdout, so we
              * must manually kill it and ignore any error related to
@@ -656,15 +660,50 @@ qemuSaveImageStartVM(virConnectPtr conn,
         rc = -1;
     }
 
-    virDomainAuditStart(vm, "restored", started);
-    if (!started || rc < 0)
-        goto cleanup;
+    virDomainAuditStart(vm, "restored", *started);
+    if (!*started || rc < 0)
+        return -1;
 
     /* qemuProcessStart doesn't unset the qemu error reporting infrastructure
      * in case of migration (which is used in this case) so we need to reset it
      * so that the handle to virtlogd is not held open unnecessarily */
     qemuMonitorSetDomainLog(qemuDomainGetMonitor(vm), NULL, NULL, NULL);
 
+    return 0;
+}
+
+int
+qemuSaveImageStartVM(virConnectPtr conn,
+                     virQEMUDriver *driver,
+                     virDomainObj *vm,
+                     int *fd,
+                     virQEMUSaveData *data,
+                     const char *path,
+                     bool start_paused,
+                     bool reset_nvram,
+                     virDomainAsyncJob asyncJob)
+{
+    int ret = -1;
+    bool started = false;
+    virObjectEvent *event;
+    g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
+    virQEMUSaveHeader *header = &data->header;
+    g_autoptr(qemuDomainSaveCookie) cookie = NULL;
+    unsigned int start_flags = VIR_QEMU_PROCESS_START_PAUSED |
+        VIR_QEMU_PROCESS_START_GEN_VMID;
+
+    if (reset_nvram)
+        start_flags |= VIR_QEMU_PROCESS_START_RESET_NVRAM;
+
+    if (virSaveCookieParseString(data->cookie, (virObject **)&cookie,
+                                 virDomainXMLOptionGetSaveCookie(driver->xmlopt)) < 0)
+        goto cleanup;
+
+    if (qemuSaveImageStartProcess(conn, driver, vm, fd, path, header, cookie,
+                                  asyncJob, start_flags, &started) < 0) {
+        goto cleanup;
+    }
+
     event = virDomainEventLifecycleNewFromObj(vm,
                                      VIR_DOMAIN_EVENT_STARTED,
                                      VIR_DOMAIN_EVENT_STARTED_RESTORED);
diff --git a/src/qemu/qemu_saveimage.h b/src/qemu/qemu_saveimage.h
index 30cf4b1ee0..af30b7f2ec 100644
--- a/src/qemu/qemu_saveimage.h
+++ b/src/qemu/qemu_saveimage.h
@@ -57,6 +57,18 @@ qemuSaveImageUpdateDef(virQEMUDriver *driver,
                        virDomainDef *def,
                        const char *newxml);
 
+int
+qemuSaveImageStartProcess(virConnectPtr conn,
+                          virQEMUDriver *driver,
+                          virDomainObj *vm,
+                          int *fd,
+                          const char *path,
+                          virQEMUSaveHeader *header,
+                          qemuDomainSaveCookie *cookie,
+                          virDomainAsyncJob asyncJob,
+                          unsigned int start_flags,
+                          bool *started);
+
 int
 qemuSaveImageStartVM(virConnectPtr conn,
                      virQEMUDriver *driver,
-- 
2.41.0




[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]

  Powered by Linux