Skip to contentSkip to conversation

convert savevm to use qapi and introduce qmp command

PATCH Superseded Pavel Hrdina 2 participants 19 messages Mar 28, 2013 Block layer

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover convert savevm to use qapi and introduce qmp command

This patch series convert the savevm command into qapi and introduce QMP command vm-snapshot-save.
It also rewrite error report for functions used by this command.

The last patch introduce new functionality of savevm that you cannot override existing snapshot without using 'force' parameter.

If non-blocking behaviour of this command is required and we cannot wait until live snapshots will be finished, I could improve this basic command to be non-blocking.

Changes from v2:
- correct error messages
- introduce of 'force' option moved to qapi: Convert savevm
- update of return value for used functions
- drop of the speed improve because it isn't actually speed improve
- vm-snapshot-save and savevm now returns snapshot information

Changes from v1:
- rebase on current master branch
- improve the speed of savevm
- name parameter remains optionl for HMP and QMP

Pavel Hrdina (11):
block: add error parameter to bdrv_snapshot_create() and related
functions
block: add error parameter to del_existing_snapshots()
savevm: add error parameter to qemu_savevm_state_begin()
savevm: add error parameter to qemu_savevm_state_iterate()
savevm: add error parameter to qemu_savevm_state_complete()
savevm: add error parameter to qemu_savevm_state()
qapi: Convert savevm
qemu-img: introduce qemu_img_handle_error
block: update return value from bdrv_snapshot_create
savevm: update return value from qemu_savevm_state
savevm: add force parameter to HMP command and return snapshot info

 block.c                   | 24 +++++++-----
 block/qcow2-snapshot.c    | 15 +++++---
 block/qcow2.h             |  4 +-
 block/rbd.c               | 19 +++++-----
 block/sheepdog.c          | 20 +++++-----
 hmp-commands.hx           | 18 ++++-----
 hmp.c                     | 27 ++++++++++++++
 hmp.h                     |  1 +
 include/block/block.h     |  5 ++-
 include/block/block_int.h |  5 ++-
 include/sysemu/sysemu.h   |  8 ++--
 migration.c               |  6 +--
 qapi-schema.json          | 22 +++++++++++
 qemu-img.c                | 27 ++++++++------
 qmp-commands.hx           | 43 ++++++++++++++++++++++
 savevm.c                  | 93 ++++++++++++++++++++++++++++-------------------
 16 files changed, 235 insertions(+), 102 deletions(-)
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH v3 1/11 block: add error parameter to bdrv_snapshot_create() and related functions

---
 block.c                   | 22 ++++++++++++++++------
 block/qcow2-snapshot.c    |  9 ++++++++-
 block/qcow2.h             |  4 +++-
 block/rbd.c               |  8 ++++++--
 block/sheepdog.c          | 17 +++++++++--------
 include/block/block.h     |  3 ++-
 include/block/block_int.h |  3 ++-
 qemu-img.c                |  2 +-
 savevm.c                  |  2 +-
 9 files changed, 48 insertions(+), 22 deletions(-)
block.c +16 -6
index 16a92a4..69f996e 100644
@@ -3301,15 +3301,25 @@ BlockDriverState *bdrv_snapshots(void)
 }
 
 int bdrv_snapshot_create(BlockDriverState *bs,
-                         QEMUSnapshotInfo *sn_info)
+                         QEMUSnapshotInfo *sn_info,
+                         Error **errp)
 {
     BlockDriver *drv = bs->drv;
-    if (!drv)
+
+    if (!drv) {
+        error_setg(errp, "device '%s' has no medium",
+                   bdrv_get_device_name(bs));
         return -ENOMEDIUM;
-    if (drv->bdrv_snapshot_create)
-        return drv->bdrv_snapshot_create(bs, sn_info);
-    if (bs->file)
-        return bdrv_snapshot_create(bs->file, sn_info);
+    }
+    if (drv->bdrv_snapshot_create) {
+        return drv->bdrv_snapshot_create(bs, sn_info, errp);
+    }
+    if (bs->file) {
+        return bdrv_snapshot_create(bs->file, sn_info, errp);
+    }
+
+    error_setg(errp, "snapshot is not supported for '%s'",
+               bdrv_get_format_name(bs));
     return -ENOTSUP;
 }
 
block/qcow2-snapshot.c +8 -1
index 992a5c8..b34179e 100644
@@ -315,7 +315,9 @@ static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name)
 }
 
 /* if no id is provided, a new one is constructed */
-int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
+int qcow2_snapshot_create(BlockDriverState *bs,
+                          QEMUSnapshotInfo *sn_info,
+                          Error **errp)
 {
     BDRVQcowState *s = bs->opaque;
     QCowSnapshot *new_snapshot_list = NULL;
@@ -334,6 +336,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
 
     /* Check that the ID is unique */
     if (find_snapshot_by_id(bs, sn_info->id_str) >= 0) {
+        error_setg(errp, "parameter 'name' has to be unique ID");
         return -EEXIST;
     }
 
@@ -351,6 +354,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
     l1_table_offset = qcow2_alloc_clusters(bs, s->l1_size * sizeof(uint64_t));
     if (l1_table_offset < 0) {
         ret = l1_table_offset;
+        error_setg(errp, "failed to allocate L1 table");
         goto fail;
     }
 
@@ -365,6 +369,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
     ret = bdrv_pwrite(bs->file, sn->l1_table_offset, l1_table,
                       s->l1_size * sizeof(uint64_t));
     if (ret < 0) {
+        error_setg(errp, "failed to save L1 table");
         goto fail;
     }
 
@@ -378,6 +383,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
      */
     ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
     if (ret < 0) {
+        error_setg(errp, "failed to update snapshot refcount");
         goto fail;
     }
 
@@ -395,6 +401,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
     if (ret < 0) {
         g_free(s->snapshots);
         s->snapshots = old_snapshot_list;
+        error_setg(errp, "failed to write new snapshots");
         goto fail;
     }
 
block/qcow2.h +3 -1
index e4b5e11..35ef293 100644
@@ -353,7 +353,9 @@ int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
 int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors);
 
 /* qcow2-snapshot.c functions */
-int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
+int qcow2_snapshot_create(BlockDriverState *bs,
+                          QEMUSnapshotInfo *sn_info,
+                          Error **errp);
 int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
 int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
 int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
block/rbd.c +6 -2
index 1a8ea6d..cdbee18 100644
@@ -816,12 +816,14 @@ static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset)
 }
 
 static int qemu_rbd_snap_create(BlockDriverState *bs,
-                                QEMUSnapshotInfo *sn_info)
+                                QEMUSnapshotInfo *sn_info,
+                                Error **errp)
 {
     BDRVRBDState *s = bs->opaque;
     int r;
 
     if (sn_info->name[0] == '\0') {
+        error_setg(errp, "parameter 'name' cannot be empty");
         return -EINVAL; /* we need a name for rbd snapshots */
     }
 
@@ -831,16 +833,18 @@ static int qemu_rbd_snap_create(BlockDriverState *bs,
      */
     if (sn_info->id_str[0] != '\0' &&
         strcmp(sn_info->id_str, sn_info->name) != 0) {
+        error_setg(errp, "ID and name have to be equal");
         return -EINVAL;
     }
 
     if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
+        error_setg(errp, "parameter 'name' has to be shorter than 127 chars");
         return -ERANGE;
     }
 
     r = rbd_snap_create(s->image, sn_info->name);
     if (r < 0) {
-        error_report("failed to create snap: %s", strerror(-r));
+        error_setg_errno(errp, -r, "failed to create snapshot");
         return r;
     }
 
block/sheepdog.c +9 -8
index bb67c4c..e38bdf5 100644
@@ -1767,7 +1767,9 @@ static int coroutine_fn sd_co_flush_to_disk(BlockDriverState *bs)
     return acb->ret;
 }
 
-static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
+static int sd_snapshot_create(BlockDriverState *bs,
+                              QEMUSnapshotInfo *sn_info,
+                              Error **errp)
 {
     BDRVSheepdogState *s = bs->opaque;
     int ret, fd;
@@ -1780,9 +1782,8 @@ static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
             s->name, sn_info->vm_state_size, s->is_snapshot);
 
     if (s->is_snapshot) {
-        error_report("You can't create a snapshot of a snapshot VDI, "
-                     "%s (%" PRIu32 ").", s->name, s->inode.vdi_id);
-
+        error_setg(errp, "you can't create a snapshot '%s' of a snapshot VDI "
+                   "%s (%" PRIu32 ")", sn_info->name, s->name, s->inode.vdi_id);
         return -EINVAL;
     }
 
@@ -1801,21 +1802,21 @@ static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
     fd = connect_to_sdog(s);
     if (fd < 0) {
         ret = fd;
+        error_setg_errno(errp, errno, "failed to connect to sdog");
         goto cleanup;
     }
 
     ret = write_object(fd, (char *)&s->inode, vid_to_vdi_oid(s->inode.vdi_id),
                        s->inode.nr_copies, datalen, 0, false, s->cache_flags);
     if (ret < 0) {
-        error_report("failed to write snapshot's inode.");
+        error_setg_errno(errp, errno, "failed to write snapshot's inode");
         goto cleanup;
     }
 
     ret = do_sd_create(s, s->name, s->inode.vdi_size, s->inode.vdi_id, &new_vid,
                        1);
     if (ret < 0) {
-        error_report("failed to create inode for snapshot. %s",
-                     strerror(errno));
+        error_setg_errno(errp, errno, "failed to create inode for snapshot");
         goto cleanup;
     }
 
@@ -1825,7 +1826,7 @@ static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
                       s->inode.nr_copies, datalen, 0, s->cache_flags);
 
     if (ret < 0) {
-        error_report("failed to read new inode info. %s", strerror(errno));
+        error_setg_errno(errp, errno, "failed to read new inode info");
         goto cleanup;
     }
 
include/block/block.h +2 -1
index 9dc6aad..ee9399c 100644
@@ -332,7 +332,8 @@ int bdrv_can_snapshot(BlockDriverState *bs);
 int bdrv_is_snapshot(BlockDriverState *bs);
 BlockDriverState *bdrv_snapshots(void);
 int bdrv_snapshot_create(BlockDriverState *bs,
-                         QEMUSnapshotInfo *sn_info);
+                         QEMUSnapshotInfo *sn_info,
+                         Error **errp);
 int bdrv_snapshot_goto(BlockDriverState *bs,
                        const char *snapshot_id);
 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
include/block/block_int.h +2 -1
index 0986a2d..2feaa16 100644
@@ -154,7 +154,8 @@ struct BlockDriver {
                                  const uint8_t *buf, int nb_sectors);
 
     int (*bdrv_snapshot_create)(BlockDriverState *bs,
-                                QEMUSnapshotInfo *sn_info);
+                                QEMUSnapshotInfo *sn_info,
+                                Error **errp);
     int (*bdrv_snapshot_goto)(BlockDriverState *bs,
                               const char *snapshot_id);
     int (*bdrv_snapshot_delete)(BlockDriverState *bs, const char *snapshot_id);
qemu-img.c +1 -1
index 31627b0..21d02bf 100644
@@ -2012,7 +2012,7 @@ static int img_snapshot(int argc, char **argv)
         sn.date_sec = tv.tv_sec;
         sn.date_nsec = tv.tv_usec * 1000;
 
-        ret = bdrv_snapshot_create(bs, &sn);
+        ret = bdrv_snapshot_create(bs, &sn, NULL);
         if (ret) {
             error_report("Could not create snapshot '%s': %d (%s)",
                 snapshot_name, ret, strerror(-ret));
savevm.c +1 -1
index 406caa9..77c5291 100644
@@ -2332,7 +2332,7 @@ void do_savevm(Monitor *mon, const QDict *qdict)
         if (bdrv_can_snapshot(bs1)) {
             /* Write VM state size only to the image that contains the state */
             sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
-            ret = bdrv_snapshot_create(bs1, sn);
+            ret = bdrv_snapshot_create(bs1, sn, NULL);
             if (ret < 0) {
                 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
                                bdrv_get_device_name(bs1));
Signature · 1 line
1.8.1.4
Reviewed-by on v3 1/11
#2 lore raw

Re: [Qemu-devel] [PATCH v3 01/11] block: add error parameter to bdrv_snapshot_create() and related functions

Quoted 13 lines · Pavel Hrdina
---
 block.c                   | 22 ++++++++++++++++------
 block/qcow2-snapshot.c    |  9 ++++++++-
 block/qcow2.h             |  4 +++-
 block/rbd.c               |  8 ++++++--
 block/sheepdog.c          | 17 +++++++++--------
 include/block/block.h     |  3 ++-
 include/block/block_int.h |  3 ++-
 qemu-img.c                |  2 +-
 savevm.c                  |  2 +-
 9 files changed, 48 insertions(+), 22 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

PATCH v3 2/11 block: add error parameter to del_existing_snapshots()

In reply to Pavel Hrdina
---
 savevm.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
savevm.c +7 -5
index 77c5291..dc1f4a4 100644
@@ -2224,7 +2224,7 @@ static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
 /*
  * Deletes snapshots of a given name in all opened images.
  */
-static int del_existing_snapshots(Monitor *mon, const char *name)
+static int del_existing_snapshots(const char *name, Error **errp)
 {
     BlockDriverState *bs;
     QEMUSnapshotInfo sn1, *snapshot = &sn1;
@@ -2237,9 +2237,8 @@ static int del_existing_snapshots(Monitor *mon, const char *name)
         {
             ret = bdrv_snapshot_delete(bs, name);
             if (ret < 0) {
-                monitor_printf(mon,
-                               "Error while deleting snapshot on '%s'\n",
-                               bdrv_get_device_name(bs));
+                error_setg(errp, "error while deleting snapshot on '%s'",
+                           bdrv_get_device_name(bs));
                 return -1;
             }
         }
@@ -2259,6 +2258,7 @@ void do_savevm(Monitor *mon, const QDict *qdict)
     qemu_timeval tv;
     struct tm tm;
     const char *name = qdict_get_try_str(qdict, "name");
+    Error *local_err = NULL;
 
     /* Verify if there is a device that doesn't support snapshots and is writable */
     bs = NULL;
@@ -2307,7 +2307,9 @@ void do_savevm(Monitor *mon, const QDict *qdict)
     }
 
     /* Delete old snapshots of the same name */
-    if (name && del_existing_snapshots(mon, name) < 0) {
+    if (name && del_existing_snapshots(name, &local_err) < 0) {
+        monitor_printf(mon, "%s\n", error_get_pretty(local_err));
+        error_free(local_err);
         goto the_end;
     }
 
Signature · 1 line
1.8.1.4
#4 lore raw

PATCH v3 3/11 savevm: add error parameter to qemu_savevm_state_begin()

In reply to Pavel Hrdina
---
 include/sysemu/sysemu.h | 3 ++-
 migration.c             | 2 +-
 savevm.c                | 5 +++--
 3 files changed, 6 insertions(+), 4 deletions(-)
include/sysemu/sysemu.h +2 -1
index 6578782..2f35a05 100644
@@ -74,7 +74,8 @@ void qemu_announce_self(void);
 
 bool qemu_savevm_state_blocked(Error **errp);
 void qemu_savevm_state_begin(QEMUFile *f,
-                             const MigrationParams *params);
+                             const MigrationParams *params,
+                             Error **errp);
 int qemu_savevm_state_iterate(QEMUFile *f);
 void qemu_savevm_state_complete(QEMUFile *f);
 void qemu_savevm_state_cancel(void);
migration.c +1 -1
index 7fb2147..d517dd6 100644
@@ -505,7 +505,7 @@ static void *migration_thread(void *opaque)
     bool old_vm_running = false;
 
     DPRINTF("beginning savevm\n");
-    qemu_savevm_state_begin(s->file, &s->params);
+    qemu_savevm_state_begin(s->file, &s->params, NULL);
 
     while (s->state == MIG_STATE_ACTIVE) {
         int64_t current_time;
savevm.c +3 -2
index dc1f4a4..56da096 100644
@@ -1731,7 +1731,8 @@ bool qemu_savevm_state_blocked(Error **errp)
 }
 
 void qemu_savevm_state_begin(QEMUFile *f,
-                             const MigrationParams *params)
+                             const MigrationParams *params,
+                             Error **errp)
 {
     SaveStateEntry *se;
     int ret;
@@ -1921,7 +1922,7 @@ static int qemu_savevm_state(QEMUFile *f)
     }
 
     qemu_mutex_unlock_iothread();
-    qemu_savevm_state_begin(f, &params);
+    qemu_savevm_state_begin(f, &params, NULL);
     qemu_mutex_lock_iothread();
 
     while (qemu_file_get_error(f) == 0) {
Signature · 1 line
1.8.1.4
#5 lore raw

PATCH v3 4/11 savevm: add error parameter to qemu_savevm_state_iterate()

In reply to Pavel Hrdina
---
 include/sysemu/sysemu.h | 2 +-
 migration.c             | 2 +-
 savevm.c                | 4 ++--
 3 files changed, 4 insertions(+), 4 deletions(-)
include/sysemu/sysemu.h +1 -1
index 2f35a05..af4f699 100644
@@ -76,7 +76,7 @@ bool qemu_savevm_state_blocked(Error **errp);
 void qemu_savevm_state_begin(QEMUFile *f,
                              const MigrationParams *params,
                              Error **errp);
-int qemu_savevm_state_iterate(QEMUFile *f);
+int qemu_savevm_state_iterate(QEMUFile *f, Error **errp);
 void qemu_savevm_state_complete(QEMUFile *f);
 void qemu_savevm_state_cancel(void);
 uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size);
migration.c +1 -1
index d517dd6..e9d2f40 100644
@@ -516,7 +516,7 @@ static void *migration_thread(void *opaque)
             pending_size = qemu_savevm_state_pending(s->file, max_size);
             DPRINTF("pending size %lu max %lu\n", pending_size, max_size);
             if (pending_size && pending_size >= max_size) {
-                qemu_savevm_state_iterate(s->file);
+                qemu_savevm_state_iterate(s->file, NULL);
             } else {
                 DPRINTF("done iterating\n");
                 qemu_mutex_lock_iothread();
savevm.c +2 -2
index 56da096..575f1b2 100644
@@ -1784,7 +1784,7 @@ void qemu_savevm_state_begin(QEMUFile *f,
  *   0 : We haven't finished, caller have to go again
  *   1 : We have finished, we can go to complete phase
  */
-int qemu_savevm_state_iterate(QEMUFile *f)
+int qemu_savevm_state_iterate(QEMUFile *f, Error **errp)
 {
     SaveStateEntry *se;
     int ret = 1;
@@ -1926,7 +1926,7 @@ static int qemu_savevm_state(QEMUFile *f)
     qemu_mutex_lock_iothread();
 
     while (qemu_file_get_error(f) == 0) {
-        if (qemu_savevm_state_iterate(f) > 0) {
+        if (qemu_savevm_state_iterate(f, NULL) > 0) {
             break;
         }
     }
Signature · 1 line
1.8.1.4
#6 lore raw

PATCH v3 5/11 savevm: add error parameter to qemu_savevm_state_complete()

In reply to Pavel Hrdina
---
 include/sysemu/sysemu.h | 2 +-
 migration.c             | 2 +-
 savevm.c                | 5 +++--
 3 files changed, 5 insertions(+), 4 deletions(-)
include/sysemu/sysemu.h +1 -1
index af4f699..9e6a4a9 100644
@@ -77,7 +77,7 @@ void qemu_savevm_state_begin(QEMUFile *f,
                              const MigrationParams *params,
                              Error **errp);
 int qemu_savevm_state_iterate(QEMUFile *f, Error **errp);
-void qemu_savevm_state_complete(QEMUFile *f);
+void qemu_savevm_state_complete(QEMUFile *f, Error **errp);
 void qemu_savevm_state_cancel(void);
 uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size);
 int qemu_loadvm_state(QEMUFile *f);
migration.c +1 -1
index e9d2f40..c891a45 100644
@@ -525,7 +525,7 @@ static void *migration_thread(void *opaque)
                 old_vm_running = runstate_is_running();
                 vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
                 qemu_file_set_rate_limit(s->file, INT_MAX);
-                qemu_savevm_state_complete(s->file);
+                qemu_savevm_state_complete(s->file, NULL);
                 qemu_mutex_unlock_iothread();
                 if (!qemu_file_get_error(s->file)) {
                     migrate_finish_set_state(s, MIG_STATE_COMPLETED);
savevm.c +3 -2
index 575f1b2..7598934 100644
@@ -1823,7 +1823,7 @@ int qemu_savevm_state_iterate(QEMUFile *f, Error **errp)
     return ret;
 }
 
-void qemu_savevm_state_complete(QEMUFile *f)
+void qemu_savevm_state_complete(QEMUFile *f, Error **errp)
 {
     SaveStateEntry *se;
     int ret;
@@ -1848,6 +1848,7 @@ void qemu_savevm_state_complete(QEMUFile *f)
         trace_savevm_section_end(se->section_id);
         if (ret < 0) {
             qemu_file_set_error(f, ret);
+            error_setg(errp, "failed to complete vmstate save");
             return;
         }
     }
@@ -1933,7 +1934,7 @@ static int qemu_savevm_state(QEMUFile *f)
 
     ret = qemu_file_get_error(f);
     if (ret == 0) {
-        qemu_savevm_state_complete(f);
+        qemu_savevm_state_complete(f, NULL);
         ret = qemu_file_get_error(f);
     }
     if (ret != 0) {
Signature · 1 line
1.8.1.4
#7 lore raw

PATCH v3 6/11 savevm: add error parameter to qemu_savevm_state()

In reply to Pavel Hrdina
---
 savevm.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
savevm.c +6 -6
index 7598934..3c1ac9e 100644
@@ -1910,7 +1910,7 @@ void qemu_savevm_state_cancel(void)
     }
 }
 
-static int qemu_savevm_state(QEMUFile *f)
+static int qemu_savevm_state(QEMUFile *f, Error **errp)
 {
     int ret;
     MigrationParams params = {
@@ -1918,23 +1918,23 @@ static int qemu_savevm_state(QEMUFile *f)
         .shared = 0
     };
 
-    if (qemu_savevm_state_blocked(NULL)) {
+    if (qemu_savevm_state_blocked(errp)) {
         return -EINVAL;
     }
 
     qemu_mutex_unlock_iothread();
-    qemu_savevm_state_begin(f, &params, NULL);
+    qemu_savevm_state_begin(f, &params, errp);
     qemu_mutex_lock_iothread();
 
     while (qemu_file_get_error(f) == 0) {
-        if (qemu_savevm_state_iterate(f, NULL) > 0) {
+        if (qemu_savevm_state_iterate(f, errp) > 0) {
             break;
         }
     }
 
     ret = qemu_file_get_error(f);
     if (ret == 0) {
-        qemu_savevm_state_complete(f, NULL);
+        qemu_savevm_state_complete(f, errp);
         ret = qemu_file_get_error(f);
     }
     if (ret != 0) {
@@ -2321,7 +2321,7 @@ void do_savevm(Monitor *mon, const QDict *qdict)
         monitor_printf(mon, "Could not open VM state file\n");
         goto the_end;
     }
-    ret = qemu_savevm_state(f);
+    ret = qemu_savevm_state(f, NULL);
     vm_state_size = qemu_ftell(f);
     qemu_fclose(f);
     if (ret < 0) {
Signature · 1 line
1.8.1.4
#8 lore raw

PATCH v3 7/11 qapi: Convert savevm

In reply to Pavel Hrdina

QMP command "vm-snapshot-save" has also extra optional force parameter and name parameter isn't optional anymore. It also returns information about created snapshot.

---
 hmp-commands.hx         |  4 ++--
 hmp.c                   | 11 +++++++++
 hmp.h                   |  1 +
 include/sysemu/sysemu.h |  1 -
 qapi-schema.json        | 22 ++++++++++++++++++
 qmp-commands.hx         | 43 ++++++++++++++++++++++++++++++++++
 savevm.c                | 62 +++++++++++++++++++++++++++++++------------------
 7 files changed, 118 insertions(+), 26 deletions(-)
hmp-commands.hx +2 -2
index 3d98604..382b87d 100644
@@ -310,7 +310,7 @@ ETEXI
         .args_type  = "name:s?",
         .params     = "[tag|id]",
         .help       = "save a VM snapshot. If no tag or id are provided, a new snapshot is created",
-        .mhandler.cmd = do_savevm,
+        .mhandler.cmd = hmp_vm_snapshot_save,
     },
 
 STEXI
@@ -318,7 +318,7 @@ STEXI
 @findex savevm
 Create a snapshot of the whole virtual machine. If @var{tag} is
 provided, it is used as human readable identifier. If there is already
-a snapshot with the same tag or ID, it is replaced. More info at
+a snapshot with the same @var{tag} or @var{id}, it is replaced. More info at
 @ref{vm_snapshots}.
 ETEXI
 
hmp.c +11 -0
index dbe9b90..b38b6ce 100644
@@ -1433,3 +1433,14 @@ void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
     qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
     hmp_handle_error(mon, &local_err);
 }
+
+void hmp_vm_snapshot_save(Monitor *mon, const QDict *qdict)
+{
+    const char *name = qdict_get_try_str(qdict, "name");
+    Error *err = NULL;
+    SnapshotInfo *info = NULL;
+
+    info = qmp_vm_snapshot_save(!!name, name, true, true, &err);
+    qapi_free_SnapshotInfo(info);
+    hmp_handle_error(mon, &err);
+}
hmp.h +1 -0
index 80e8b41..1bb8590 100644
@@ -86,5 +86,6 @@ void hmp_nbd_server_add(Monitor *mon, const QDict *qdict);
 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict);
 void hmp_chardev_add(Monitor *mon, const QDict *qdict);
 void hmp_chardev_remove(Monitor *mon, const QDict *qdict);
+void hmp_vm_snapshot_save(Monitor *mon, const QDict *qdict);
 
 #endif
include/sysemu/sysemu.h +0 -1
index 9e6a4a9..87b82d7 100644
@@ -65,7 +65,6 @@ void qemu_remove_exit_notifier(Notifier *notify);
 
 void qemu_add_machine_init_done_notifier(Notifier *notify);
 
-void do_savevm(Monitor *mon, const QDict *qdict);
 int load_vmstate(const char *name);
 void do_delvm(Monitor *mon, const QDict *qdict);
 void do_info_snapshots(Monitor *mon, const QDict *qdict);
qapi-schema.json +22 -0
index f629a24..c00f782 100644
@@ -3453,3 +3453,25 @@
 # Since: 1.5
 ##
 { 'command': 'query-tpm', 'returns': ['TPMInfo'] }
+
+##
+# @vm-snapshot-save:
+#
+# Create a snapshot of the whole virtual machine. If tag is provided as @name,
+# it is used as human readable identifier. If there is already a snapshot
+# with the same tag or id, the force argument needs to be true to replace it.
+#
+# The VM is automatically stopped and resumed and saving a snapshot can take
+# a long time.
+#
+# @name: #optional tag of new snapshot or tag|id of existing snapshot
+#
+# @force: #optional specify whether existing snapshot is replaced or not,
+#         default is false
+#
+# Returns: Nothing on success
+#
+# Since: 1.5
+##
+{ 'command': 'vm-snapshot-save', 'data': {'*name': 'str', '*force': 'bool'},
+  'returns': 'SnapshotInfo' }
qmp-commands.hx +43 -0

49 lines not on this page · raw message

savevm.c +39 -23

124 lines not on this page · raw message

Signature · 1 line
1.8.1.4
#9 lore raw

Re: [Qemu-devel] [PATCH v3 07/11] qapi: Convert savevm

Quoted 3 lines · Pavel Hrdina

QMP command "vm-snapshot-save" has also extra optional force parameter and name parameter isn't optional anymore. It also returns information about created snapshot.

Commit message is a bit stale from an earlier version of the series, since name is still optional in this patch.

Quoted 12 lines

+++ b/qapi-schema.json

qapi-schema.json +7 -0
@@ -3453,3 +3453,25 @@
 # Since: 1.5
 ##
 { 'command': 'query-tpm', 'returns': ['TPMInfo'] }
+
+##
+# @vm-snapshot-save:
+#
+# Create a snapshot of the whole virtual machine. If tag is provided as @name,
+# it is used as human readable identifier. If there is already a snapshot
+# with the same tag or id, the force argument needs to be true to replace it.

qapi-schema.json line 3472

Quoted 10 lines
qapi-schema.json +10 -0
+#
+# The VM is automatically stopped and resumed and saving a snapshot can take
+# a long time.
+#
+# @name: #optional tag of new snapshot or tag|id of existing snapshot
+#
+# @force: #optional specify whether existing snapshot is replaced or not,
+#         default is false
+#
+# Returns: Nothing on success

Comment is stale, now that you return the SnapshotInfo of the newly-created snapshot.

Quoted 8 lines
qapi-schema.json +6 -0
+#
+# Since: 1.5
+##
+{ 'command': 'vm-snapshot-save', 'data': {'*name': 'str', '*force': 'bool'},
+  'returns': 'SnapshotInfo' }
 
+++ b/savevm.c

qapi-schema.json static int del_existing_snapshots(const char *name, Error **errp) · line 2253

Quoted 6 lines
qapi-schema.json +2 -1
@@ -2249,17 +2249,18 @@ static int del_existing_snapshots(const char *name, Error **errp)
     return 0;
 }
 
-void do_savevm(Monitor *mon, const QDict *qdict)
+SnapshotInfo *qmp_vm_snapshot_save(bool has_name, const char *name,
+                                  bool has_force, bool force, Error **errp)

Indentation is off by one (the two "bool has_" should be aligned).

savevm.c void do_savevm(Monitor *mon, const QDict *qdict) · line 2276

Quoted 9 lines
savevm.c +2 -3
@@ -2271,16 +2272,16 @@ void do_savevm(Monitor *mon, const QDict *qdict)
         }
 
         if (!bdrv_can_snapshot(bs)) {
-            monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
-                               bdrv_get_device_name(bs));
-            return;
+            error_setg(errp, "Device '%s' is writable but does not support "
+                       "snapshots.", bdrv_get_device_name(bs));

Missed a removal of a trailing '.' in the error message.

savevm.c void do_savevm(Monitor *mon, const QDict *qdict) · line 2283

Quoted 9 lines
savevm.c +2 -2
+            return NULL;
         }
     }
 
     bs = bdrv_snapshots();
     if (!bs) {
-        monitor_printf(mon, "No block device can accept snapshots\n");
-        return;
+        error_setg(errp, "No block device can accept snapshots.");

And completely added a trailing '.' here.

savevm.c void do_savevm(Monitor *mon, const QDict *qdict) · line 2312

Quoted 12 lines
savevm.c +12 -0
+            if (has_force && force) {
+                pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
+                pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
+
+                /* Delete old snapshots of the same name */
+                if (del_existing_snapshots(name, &local_err) < 0) {
+                    error_propagate(errp, local_err);
+                    goto the_end;
+                }
+            } else {
+                error_setg(errp, "Snapshot '%s' exists. For override use "
+                           "'force' parameter.", name);

New message with trailing '.'; maybe:

"cannot override existing snapshot '%s' without 'force' parameter"

savevm.c void do_savevm(Monitor *mon, const QDict *qdict) · line 2327

Quoted 5 lines
     /* save the VM state */
     f = qemu_fopen_bdrv(bs, 1);
     if (!f) {
savevm.c +1 -1
-        monitor_printf(mon, "Could not open VM state file\n");
+        error_setg(errp, "Failed to open '%s' file.", bdrv_get_device_name(bs));

don't add a trailing '.'

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#10 lore raw

PATCH v3 8/11 qemu-img: introduce qemu_img_handle_error

In reply to Pavel Hrdina
---
 qemu-img.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
qemu-img.c +12 -6
index 21d02bf..d5f81cc 100644
@@ -322,6 +322,17 @@ static int add_old_style_options(const char *fmt, QEMUOptionParameter *list,
     return 0;
 }
 
+static int qemu_img_handle_error(Error *err)
+{
+    if (error_is_set(&err)) {
+        error_report("%s", error_get_pretty(err));
+        error_free(err);
+        return 1;
+    }
+
+    return 0;
+}
+
 static int img_create(int argc, char **argv)
 {
     int c;
@@ -400,13 +411,8 @@ static int img_create(int argc, char **argv)
 
     bdrv_img_create(filename, fmt, base_filename, base_fmt,
                     options, img_size, BDRV_O_FLAGS, &local_err, quiet);
-    if (error_is_set(&local_err)) {
-        error_report("%s", error_get_pretty(local_err));
-        error_free(local_err);
-        return 1;
-    }
 
-    return 0;
+    return qemu_img_handle_error(local_err);
 }
 
 static void dump_json_image_check(ImageCheck *check, bool quiet)
Signature · 1 line
1.8.1.4
Reviewed-by on v3 8/11
#11 lore raw

Re: [Qemu-devel] [PATCH v3 08/11] qemu-img: introduce qemu_img_handle_error

Quoted 4 lines · Pavel Hrdina
---
 qemu-img.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

qemu-img.c static int add_old_style_options(const char *fmt, QEMUOptionParameter *list, · line 330

Quoted 6 lines
qemu-img.c +6 -0
+static int qemu_img_handle_error(Error *err)
+{
+    if (error_is_set(&err)) {
+        error_report("%s", error_get_pretty(err));
+        error_free(err);
+        return 1;

A non-bool positive number is unusual for errors (-1, or a true/false scheme, tend to be more common)...

qemu-img.c static int img_create(int argc, char **argv) · line 406

Quoted 4 lines
qemu-img.c +0 -4
-    if (error_is_set(&local_err)) {
-        error_report("%s", error_get_pretty(local_err));
-        error_free(local_err);
-        return 1;

...but it is accurate code motion, so it doesn't impact my review.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#12 lore raw

Re: [Qemu-devel] [PATCH v3 08/11] qemu-img: introduce qemu_img_handle_error

Quoted 18 lines · Eric Blake
Quoted 4 lines · Pavel Hrdina
---
 qemu-img.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

qemu-img.c static int add_old_style_options(const char *fmt, QEMUOptionParameter *list, · line 330

Quoted 6 lines
qemu-img.c +6 -0
+static int qemu_img_handle_error(Error *err)
+{
+    if (error_is_set(&err)) {
+        error_report("%s", error_get_pretty(err));
+        error_free(err);
+        return 1;

A non-bool positive number is unusual for errors (-1, or a true/false scheme, tend to be more common)...

Quoted 6 lines

qemu-img.c static int img_create(int argc, char **argv) · line 406

Quoted 4 lines
qemu-img.c +0 -4
-    if (error_is_set(&local_err)) {
-        error_report("%s", error_get_pretty(local_err));
-        error_free(local_err);
-        return 1;

...but it is accurate code motion, so it doesn't impact my review.

And in reading 9/11, I found out why it struck me as unusual. I usually spell it EXIT_FAILURE instead of 1, for a return status intended to be used in a final exit() or return from main(). But that's a pre-existing syndrome that affects much more of qemu-img.c, not worth changing here.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#13 lore raw

PATCH v3 9/11 block: update return value from bdrv_snapshot_create

In reply to Pavel Hrdina

If we provide error message we don't have to also provide return value because we could check if there is any error message or not.

---
 block.c                   | 24 ++++++++++--------------
 block/qcow2-snapshot.c    | 12 +++++-------
 block/qcow2.h             |  6 +++---
 block/rbd.c               | 15 ++++++---------
 block/sheepdog.c          |  9 ++++-----
 include/block/block.h     |  6 +++---
 include/block/block_int.h |  6 +++---
 qemu-img.c                |  9 ++++-----
 savevm.c                  |  4 ++--
 9 files changed, 40 insertions(+), 51 deletions(-)
block.c +10 -14
index 69f996e..e701c0c 100644
@@ -3300,27 +3300,23 @@ BlockDriverState *bdrv_snapshots(void)
     return NULL;
 }
 
-int bdrv_snapshot_create(BlockDriverState *bs,
-                         QEMUSnapshotInfo *sn_info,
-                         Error **errp)
+void bdrv_snapshot_create(BlockDriverState *bs,
+                          QEMUSnapshotInfo *sn_info,
+                          Error **errp)
 {
     BlockDriver *drv = bs->drv;
 
     if (!drv) {
         error_setg(errp, "device '%s' has no medium",
                    bdrv_get_device_name(bs));
-        return -ENOMEDIUM;
-    }
-    if (drv->bdrv_snapshot_create) {
-        return drv->bdrv_snapshot_create(bs, sn_info, errp);
-    }
-    if (bs->file) {
-        return bdrv_snapshot_create(bs->file, sn_info, errp);
+    } else if (drv->bdrv_snapshot_create) {
+        drv->bdrv_snapshot_create(bs, sn_info, errp);
+    } else if (bs->file) {
+        bdrv_snapshot_create(bs->file, sn_info, errp);
+    } else {
+        error_setg(errp, "snapshot is not supported for '%s'",
+                   bdrv_get_format_name(bs));
     }
-
-    error_setg(errp, "snapshot is not supported for '%s'",
-               bdrv_get_format_name(bs));
-    return -ENOTSUP;
 }
 
 int bdrv_snapshot_goto(BlockDriverState *bs,
block/qcow2-snapshot.c +5 -7
index b34179e..f49b95f 100644
@@ -315,9 +315,9 @@ static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name)
 }
 
 /* if no id is provided, a new one is constructed */
-int qcow2_snapshot_create(BlockDriverState *bs,
-                          QEMUSnapshotInfo *sn_info,
-                          Error **errp)
+void qcow2_snapshot_create(BlockDriverState *bs,
+                           QEMUSnapshotInfo *sn_info,
+                           Error **errp)
 {
     BDRVQcowState *s = bs->opaque;
     QCowSnapshot *new_snapshot_list = NULL;
@@ -337,7 +337,7 @@ int qcow2_snapshot_create(BlockDriverState *bs,
     /* Check that the ID is unique */
     if (find_snapshot_by_id(bs, sn_info->id_str) >= 0) {
         error_setg(errp, "parameter 'name' has to be unique ID");
-        return -EEXIST;
+        return;
     }
 
     /* Populate sn with passed data */
@@ -413,14 +413,12 @@ int qcow2_snapshot_create(BlockDriverState *bs,
       qcow2_check_refcounts(bs, &result, 0);
     }
 #endif
-    return 0;
+    return;
 
 fail:
     g_free(sn->id_str);
     g_free(sn->name);
     g_free(l1_table);
-
-    return ret;
 }
 
 /* copy the snapshot 'snapshot_name' into the current disk image */
block/qcow2.h +3 -3
index 35ef293..b23ea3f 100644
@@ -353,9 +353,9 @@ int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
 int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors);
 
 /* qcow2-snapshot.c functions */
-int qcow2_snapshot_create(BlockDriverState *bs,
-                          QEMUSnapshotInfo *sn_info,
-                          Error **errp);
+void qcow2_snapshot_create(BlockDriverState *bs,
+                           QEMUSnapshotInfo *sn_info,
+                           Error **errp);
 int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
 int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
 int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
block/rbd.c +6 -9
index cdbee18..5167f2c 100644
@@ -815,16 +815,16 @@ static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset)
     return 0;
 }
 
-static int qemu_rbd_snap_create(BlockDriverState *bs,
-                                QEMUSnapshotInfo *sn_info,
-                                Error **errp)
+static void qemu_rbd_snap_create(BlockDriverState *bs,
+                                 QEMUSnapshotInfo *sn_info,
+                                 Error **errp)
 {
     BDRVRBDState *s = bs->opaque;
     int r;
 
     if (sn_info->name[0] == '\0') {
         error_setg(errp, "parameter 'name' cannot be empty");
-        return -EINVAL; /* we need a name for rbd snapshots */
+        return; /* we need a name for rbd snapshots */
     }
 
     /*
@@ -834,21 +834,18 @@ static int qemu_rbd_snap_create(BlockDriverState *bs,
     if (sn_info->id_str[0] != '\0' &&
         strcmp(sn_info->id_str, sn_info->name) != 0) {
         error_setg(errp, "ID and name have to be equal");
-        return -EINVAL;
+        return;
     }
 
     if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
         error_setg(errp, "parameter 'name' has to be shorter than 127 chars");
-        return -ERANGE;
+        return;
     }
 
     r = rbd_snap_create(s->image, sn_info->name);
     if (r < 0) {
         error_setg_errno(errp, -r, "failed to create snapshot");
-        return r;
     }
-
-    return 0;
 }
 
 static int qemu_rbd_snap_remove(BlockDriverState *bs,
block/sheepdog.c +4 -5
index e38bdf5..cf11dfb 100644
@@ -1767,9 +1767,9 @@ static int coroutine_fn sd_co_flush_to_disk(BlockDriverState *bs)
     return acb->ret;
 }
 
-static int sd_snapshot_create(BlockDriverState *bs,
-                              QEMUSnapshotInfo *sn_info,
-                              Error **errp)
+static void sd_snapshot_create(BlockDriverState *bs,
+                               QEMUSnapshotInfo *sn_info,
+                               Error **errp)
 {
     BDRVSheepdogState *s = bs->opaque;
     int ret, fd;
@@ -1784,7 +1784,7 @@ static int sd_snapshot_create(BlockDriverState *bs,
     if (s->is_snapshot) {
         error_setg(errp, "you can't create a snapshot '%s' of a snapshot VDI "
                    "%s (%" PRIu32 ")", sn_info->name, s->name, s->inode.vdi_id);
-        return -EINVAL;
+        return;
     }
 
     dprintf("%s %s\n", sn_info->name, sn_info->id_str);
@@ -1836,7 +1836,6 @@ static int sd_snapshot_create(BlockDriverState *bs,
 
 cleanup:
     closesocket(fd);
-    return ret;
 }
 
 static int sd_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
include/block/block.h +3 -3
index ee9399c..1a6a6a7 100644
@@ -331,9 +331,9 @@ BlockStats *bdrv_query_stats(const BlockDriverState *bs);
 int bdrv_can_snapshot(BlockDriverState *bs);
 int bdrv_is_snapshot(BlockDriverState *bs);
 BlockDriverState *bdrv_snapshots(void);
-int bdrv_snapshot_create(BlockDriverState *bs,
-                         QEMUSnapshotInfo *sn_info,
-                         Error **errp);
+void bdrv_snapshot_create(BlockDriverState *bs,
+                          QEMUSnapshotInfo *sn_info,
+                          Error **errp);
 int bdrv_snapshot_goto(BlockDriverState *bs,
                        const char *snapshot_id);
 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
include/block/block_int.h +3 -3
index 2feaa16..8760517 100644
@@ -153,9 +153,9 @@ struct BlockDriver {
     int (*bdrv_write_compressed)(BlockDriverState *bs, int64_t sector_num,
                                  const uint8_t *buf, int nb_sectors);
 
-    int (*bdrv_snapshot_create)(BlockDriverState *bs,
-                                QEMUSnapshotInfo *sn_info,
-                                Error **errp);
+    void (*bdrv_snapshot_create)(BlockDriverState *bs,
+                                 QEMUSnapshotInfo *sn_info,
+                                 Error **errp);
     int (*bdrv_snapshot_goto)(BlockDriverState *bs,
                               const char *snapshot_id);
     int (*bdrv_snapshot_delete)(BlockDriverState *bs, const char *snapshot_id);
qemu-img.c +4 -5
index d5f81cc..154d913 100644
@@ -1942,6 +1942,7 @@ static int img_snapshot(int argc, char **argv)
     int action = 0;
     qemu_timeval tv;
     bool quiet = false;
+    Error *local_err;
 
     bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR;
     /* Parse commandline parameters */
@@ -2018,11 +2019,9 @@ static int img_snapshot(int argc, char **argv)
         sn.date_sec = tv.tv_sec;
         sn.date_nsec = tv.tv_usec * 1000;
 
-        ret = bdrv_snapshot_create(bs, &sn, NULL);
-        if (ret) {
-            error_report("Could not create snapshot '%s': %d (%s)",
-                snapshot_name, ret, strerror(-ret));
-        }
+        local_err = NULL;
+        bdrv_snapshot_create(bs, &sn, &local_err);
+        ret = qemu_img_handle_error(local_err);
         break;
 
     case SNAPSHOT_APPLY:
savevm.c +2 -2
index 38d6950..0dce456 100644
@@ -2342,8 +2342,8 @@ SnapshotInfo *qmp_vm_snapshot_save(bool has_name, const char *name,
         if (bdrv_can_snapshot(bs1)) {
             /* Write VM state size only to the image that contains the state */
             sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
-            ret = bdrv_snapshot_create(bs1, sn, &local_err);
-            if (ret < 0) {
+            bdrv_snapshot_create(bs1, sn, &local_err);
+            if (error_is_set(&local_err)) {
                 error_propagate(errp, local_err);
             }
         }
Signature · 1 line
1.8.1.4
Reviewed-by on v3 9/11
#14 lore raw

Re: [Qemu-devel] [PATCH v3 09/11] block: update return value from bdrv_snapshot_create

Quoted 19 lines · Pavel Hrdina

If we provide error message we don't have to also provide return value because we could check if there is any error message or not.

---
 block.c                   | 24 ++++++++++--------------
 block/qcow2-snapshot.c    | 12 +++++-------
 block/qcow2.h             |  6 +++---
 block/rbd.c               | 15 ++++++---------
 block/sheepdog.c          |  9 ++++-----
 include/block/block.h     |  6 +++---
 include/block/block_int.h |  6 +++---
 qemu-img.c                |  9 ++++-----
 savevm.c                  |  4 ++--
 9 files changed, 40 insertions(+), 51 deletions(-)
qemu-img.c +0 -0
@@ -2018,11 +2019,9 @@ static int img_snapshot(int argc, char **argv)
         sn.date_sec = tv.tv_sec;

qemu-img.c static int img_snapshot(int argc, char **argv) · line 2024

Quoted 10 lines
qemu-img.c +3 -5
         sn.date_nsec = tv.tv_usec * 1000;
 
-        ret = bdrv_snapshot_create(bs, &sn, NULL);
-        if (ret) {
-            error_report("Could not create snapshot '%s': %d (%s)",
-                snapshot_name, ret, strerror(-ret));
-        }
+        local_err = NULL;
+        bdrv_snapshot_create(bs, &sn, &local_err);
+        ret = qemu_img_handle_error(local_err);

This sets ret==1 on error, but bdrv_snapshot_create used to return <0 on error. Then again, the tail of img_snapshot() normalizes all errors to a 'return 1'. Took me a few minutes to see, but no problem after all.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#15 lore raw

PATCH v3 10/11 savevm: update return value from qemu_savevm_state

In reply to Pavel Hrdina
---
 savevm.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
savevm.c +4 -5
index 0dce456..1e9ab4b 100644
@@ -1910,7 +1910,7 @@ void qemu_savevm_state_cancel(void)
     }
 }
 
-static int qemu_savevm_state(QEMUFile *f, Error **errp)
+static void qemu_savevm_state(QEMUFile *f, Error **errp)
 {
     int ret;
     MigrationParams params = {
@@ -1919,7 +1919,7 @@ static int qemu_savevm_state(QEMUFile *f, Error **errp)
     };
 
     if (qemu_savevm_state_blocked(errp)) {
-        return -EINVAL;
+        return;
     }
 
     qemu_mutex_unlock_iothread();
@@ -1940,7 +1940,6 @@ static int qemu_savevm_state(QEMUFile *f, Error **errp)
     if (ret != 0) {
         qemu_savevm_state_cancel();
     }
-    return ret;
 }
 
 static int qemu_save_device_state(QEMUFile *f)
@@ -2327,10 +2326,10 @@ SnapshotInfo *qmp_vm_snapshot_save(bool has_name, const char *name,
         error_setg(errp, "Failed to open '%s' file.", bdrv_get_device_name(bs));
         goto the_end;
     }
-    ret = qemu_savevm_state(f, &local_err);
+    qemu_savevm_state(f, &local_err);
     vm_state_size = qemu_ftell(f);
     qemu_fclose(f);
-    if (ret < 0) {
+    if (error_is_set(&local_err)) {
         error_propagate(errp, local_err);
         goto the_end;
     }
Signature · 1 line
1.8.1.4
Reviewed-by on v3 10/11
#16 lore raw

Re: [Qemu-devel] [PATCH v3 10/11] savevm: update return value from qemu_savevm_state

Quoted 5 lines · Pavel Hrdina
---
 savevm.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#17 lore raw

PATCH v3 11/11 savevm: add force parameter to HMP command and return snapshot info

In reply to Pavel Hrdina

HMP command "savevm" now takes extra optional force parameter to specify whether replace existing snapshot or not. It also returns information about created snapshot.

---
 hmp-commands.hx | 16 ++++++++--------
 hmp.c           | 18 +++++++++++++++++-
 2 files changed, 25 insertions(+), 9 deletions(-)
hmp-commands.hx +8 -8
index 382b87d..9719cc0 100644
@@ -307,19 +307,19 @@ ETEXI
 
     {
         .name       = "savevm",
-        .args_type  = "name:s?",
-        .params     = "[tag|id]",
-        .help       = "save a VM snapshot. If no tag or id are provided, a new snapshot is created",
+        .args_type  = "force:-f,name:s?",
+        .params     = "[-f] [tag|id]",
+        .help       = "save a VM snapshot, to replace existing snapshot use force flag",
         .mhandler.cmd = hmp_vm_snapshot_save,
     },
 
 STEXI
-@item savevm [@var{tag}|@var{id}]
+@item savevm [@var{-f}] @var{tag}|@var{id}
 @findex savevm
-Create a snapshot of the whole virtual machine. If @var{tag} is
-provided, it is used as human readable identifier. If there is already
-a snapshot with the same @var{tag} or @var{id}, it is replaced. More info at
-@ref{vm_snapshots}.
+Create a snapshot of the whole virtual machine. Parameter "name" is optional.
+If @var{tag} is provided, it is used as human readable identifier. If there is
+already a snapshot with the same @var{tag} or @var{id}, @var{-f} flag needs to
+be specified. More info at @ref{vm_snapshots}.
 ETEXI
 
     {
hmp.c +17 -1
index b38b6ce..adf586b 100644
@@ -1437,10 +1437,26 @@ void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
 void hmp_vm_snapshot_save(Monitor *mon, const QDict *qdict)
 {
     const char *name = qdict_get_try_str(qdict, "name");
+    bool force = qdict_get_try_bool(qdict, "force", 0);
     Error *err = NULL;
     SnapshotInfo *info = NULL;
 
-    info = qmp_vm_snapshot_save(!!name, name, true, true, &err);
+    info = qmp_vm_snapshot_save(!!name, name, !!force, force, &err);
+
+    if (info) {
+        char buf[256];
+        QEMUSnapshotInfo sn = {
+            .vm_state_size = info->vm_state_size,
+            .date_sec = info->date_sec,
+            .date_nsec = info->date_nsec,
+            .vm_clock_nsec = info->vm_clock_sec * 1000000000 +
+                             info->vm_clock_nsec,
+        };
+        pstrcpy(sn.id_str, sizeof(sn.id_str), info->id);
+        pstrcpy(sn.name, sizeof(sn.name), info->name);
+        monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), &sn));
+    }
+
     qapi_free_SnapshotInfo(info);
     hmp_handle_error(mon, &err);
 }
Signature · 1 line
1.8.1.4
Reviewed-by on v3 11/11
#18 lore raw

Re: [Qemu-devel] [PATCH v3 11/11] savevm: add force parameter to HMP command and return snapshot info

Quoted 10 lines · Pavel Hrdina

HMP command "savevm" now takes extra optional force parameter to specify whether replace existing snapshot or not. It also returns information about created snapshot.

---
 hmp-commands.hx | 16 ++++++++--------
 hmp.c           | 18 +++++++++++++++++-
 2 files changed, 25 insertions(+), 9 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org