Skip to contentSkip to conversation

monitor: make 'info snapshots' show only fully available snapshots

PATCH In review Miguel Di Ciurcio Filho 2 participants 3 messages Jul 27, 2010 – Jul 28, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 monitor: make 'info snapshots' show only fully available snapshots

The output generated by 'info snapshots' shows only snapshots that exist on the block device that saves the VM state. This output can cause an user to erroneously try to load an snapshot that is not available on all block devices.

$ qemu-img snapshot -l xxtest.qcow2
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1                                1.5M 2010-07-26 16:51:52   00:00:08.599
2                                1.5M 2010-07-26 16:51:53   00:00:09.719
3                                1.5M 2010-07-26 17:26:49   00:00:13.245
4                                1.5M 2010-07-26 19:01:00   00:00:46.763
$ qemu-img snapshot -l xxtest2.qcow2
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
3                                   0 2010-07-26 17:26:49   00:00:13.245
4                                   0 2010-07-26 19:01:00   00:00:46.763

Current output:

$ qemu -hda xxtest.qcow2 -hdb xxtest2.qcow2 -monitor stdio -vnc :0
QEMU 0.12.4 monitor - type 'help' for more information
(qemu) info snapshots
Snapshot devices: ide0-hd0
Snapshot list (from ide0-hd0):
ID        TAG                 VM SIZE                DATE       VM CLOCK
1                                1.5M 2010-07-26 16:51:52   00:00:08.599
2                                1.5M 2010-07-26 16:51:53   00:00:09.719
3                                1.5M 2010-07-26 17:26:49   00:00:13.245
4                                1.5M 2010-07-26 19:01:00   00:00:46.763

Snapshots 1 and 2 do not exist on xxtest2.qcow, but they are displayed anyway.

This patch sumarizes the output to only show fully available snapshots.

New output:
(qemu) info snapshots
ID TAG VM SIZE DATE VM CLOCK

3                                1.5M 2010-07-26 17:26:49   00:00:13.245
4                                1.5M 2010-07-26 19:01:00   00:00:46.763
---
 savevm.c |   65 ++++++++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 45 insertions(+), 20 deletions(-)
savevm.c +45 -20
index 7a1de3c..be83878 100644
@@ -1997,37 +1997,62 @@ void do_delvm(Monitor *mon, const QDict *qdict)
 
 void do_info_snapshots(Monitor *mon)
 {
-    BlockDriverState *bs, *bs1;
-    QEMUSnapshotInfo *sn_tab, *sn;
-    int nb_sns, i;
+    BlockDriverState *bs_vm_state, *bs;
+    QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
+    int nb_sns, i, ret, available;
+    int total;
+    int *available_snapshots;
     char buf[256];
 
-    bs = bdrv_snapshots();
-    if (!bs) {
+    bs_vm_state = bdrv_snapshots();
+    if (!bs_vm_state) {
         monitor_printf(mon, "No available block device supports snapshots\n");
         return;
     }
-    monitor_printf(mon, "Snapshot devices:");
-    bs1 = NULL;
-    while ((bs1 = bdrv_next(bs1))) {
-        if (bdrv_can_snapshot(bs1)) {
-            if (bs == bs1)
-                monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
-        }
-    }
-    monitor_printf(mon, "\n");
 
-    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
+    nb_sns = bdrv_snapshot_list(bs_vm_state, &sn_tab);
     if (nb_sns < 0) {
         monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
         return;
+    } else if (nb_sns == 0) {
+        monitor_printf(mon, "There is no snapshot available.\n");
     }
-    monitor_printf(mon, "Snapshot list (from %s):\n",
-                   bdrv_get_device_name(bs));
-    monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
-    for(i = 0; i < nb_sns; i++) {
+
+    available_snapshots = qemu_mallocz(sizeof(int) * nb_sns);
+    total = 0;
+    for (i = 0; i < nb_sns; i++) {
         sn = &sn_tab[i];
-        monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
+        available = 1;
+        bs = NULL;
+
+        while ((bs = bdrv_next(bs))) {
+            if (bdrv_can_snapshot(bs) && bs != bs_vm_state) {
+                ret = bdrv_snapshot_find(bs, sn_info, sn->id_str);
+                if (ret < 0) {
+                    available = 0;
+                    break;
+                }
+            }
+        }
+
+        if (available) {
+            available_snapshots[total] = i;
+            total++;
+        }
     }
+
+    if (total > 0) {
+        monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
+        for (i = 0; i < total; i++) {
+            sn = &sn_tab[available_snapshots[i]];
+            monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
+        }
+
+        qemu_free(available_snapshots);
+
+    } else {
+        monitor_printf(mon, "There is no suitable snapshot available to be loaded.\n");
+    }
+
     qemu_free(sn_tab);
 }
Signature · 1 line
1.7.1
#1 lore raw
Quoted 3 lines · Miguel Di Ciurcio Filho

The output generated by 'info snapshots' shows only snapshots that exist on the block device that saves the VM state. This output can cause an user to erroneously try to load an snapshot that is not available on all block devices.

What happens when you try that?

Quoted 30 lines
$ qemu-img snapshot -l xxtest.qcow2
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1                                1.5M 2010-07-26 16:51:52   00:00:08.599
2                                1.5M 2010-07-26 16:51:53   00:00:09.719
3                                1.5M 2010-07-26 17:26:49   00:00:13.245
4                                1.5M 2010-07-26 19:01:00   00:00:46.763
$ qemu-img snapshot -l xxtest2.qcow2
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
3                                   0 2010-07-26 17:26:49   00:00:13.245
4                                   0 2010-07-26 19:01:00   00:00:46.763

Current output:

$ qemu -hda xxtest.qcow2 -hdb xxtest2.qcow2 -monitor stdio -vnc :0
QEMU 0.12.4 monitor - type 'help' for more information
(qemu) info snapshots
Snapshot devices: ide0-hd0
Snapshot list (from ide0-hd0):
ID        TAG                 VM SIZE                DATE       VM CLOCK
1                                1.5M 2010-07-26 16:51:52   00:00:08.599
2                                1.5M 2010-07-26 16:51:53   00:00:09.719
3                                1.5M 2010-07-26 17:26:49   00:00:13.245
4                                1.5M 2010-07-26 19:01:00   00:00:46.763

Snapshots 1 and 2 do not exist on xxtest2.qcow, but they are displayed anyway.

This patch sumarizes the output to only show fully available snapshots.

Quoted 7 lines

New output:
(qemu) info snapshots
ID TAG VM SIZE DATE VM CLOCK

3                                1.5M 2010-07-26 17:26:49   00:00:13.245
4                                1.5M 2010-07-26 19:01:00   00:00:46.763

No information on "partial" snapshots. I doubt anybody will miss it.

Quoted 37 lines
---
 savevm.c |   65 ++++++++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 45 insertions(+), 20 deletions(-)
savevm.c +7 -13
index 7a1de3c..be83878 100644
@@ -1997,37 +1997,62 @@ void do_delvm(Monitor *mon, const QDict *qdict)
 
 void do_info_snapshots(Monitor *mon)
 {
-    BlockDriverState *bs, *bs1;
-    QEMUSnapshotInfo *sn_tab, *sn;
-    int nb_sns, i;
+    BlockDriverState *bs_vm_state, *bs;
+    QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
+    int nb_sns, i, ret, available;
+    int total;
+    int *available_snapshots;
     char buf[256];
 
-    bs = bdrv_snapshots();
-    if (!bs) {
+    bs_vm_state = bdrv_snapshots();
+    if (!bs_vm_state) {
         monitor_printf(mon, "No available block device supports snapshots\n");
         return;
     }
-    monitor_printf(mon, "Snapshot devices:");
-    bs1 = NULL;
-    while ((bs1 = bdrv_next(bs1))) {
-        if (bdrv_can_snapshot(bs1)) {
-            if (bs == bs1)
-                monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
-        }
-    }

savevm.c void do_delvm(Monitor *mon, const QDict *qdict) · line 2019

Quoted 10 lines
savevm.c +3 -2
-    monitor_printf(mon, "\n");
 
-    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
+    nb_sns = bdrv_snapshot_list(bs_vm_state, &sn_tab);
     if (nb_sns < 0) {
         monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
         return;
+    } else if (nb_sns == 0) {
+        monitor_printf(mon, "There is no snapshot available.\n");
     }

This changes output for the "no snapshots available" case from the empty table

ID TAG VM SIZE DATE VM CLOCK

to

There is no snapshot available.

I'd prefer that as separate patch, if at all.

Nitpick: I don't like "return; else".

savevm.c void do_delvm(Monitor *mon, const QDict *qdict) · line 2021

Quoted 6 lines
savevm.c +2 -4
-    monitor_printf(mon, "Snapshot list (from %s):\n",
-                   bdrv_get_device_name(bs));
-    monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
-    for(i = 0; i < nb_sns; i++) {
+
+    available_snapshots = qemu_mallocz(sizeof(int) * nb_sns);

This can die due to the nonsensical semantics of qemu_mallocz(0).

Quoted 24 lines
savevm.c +21 -1
+    total = 0;
+    for (i = 0; i < nb_sns; i++) {
         sn = &sn_tab[i];
-        monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
+        available = 1;
+        bs = NULL;
+
+        while ((bs = bdrv_next(bs))) {
+            if (bdrv_can_snapshot(bs) && bs != bs_vm_state) {
+                ret = bdrv_snapshot_find(bs, sn_info, sn->id_str);
+                if (ret < 0) {
+                    available = 0;
+                    break;
+                }
+            }
+        }
+
+        if (available) {
+            available_snapshots[total] = i;
+            total++;
+        }
     }
+
+    if (total > 0) {

savevm.c void do_delvm(Monitor *mon, const QDict *qdict) · line 2054

Quoted 10 lines
savevm.c +10 -0
+        monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
+        for (i = 0; i < total; i++) {
+            sn = &sn_tab[available_snapshots[i]];
+            monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
+        }
+
+        qemu_free(available_snapshots);
+
+    } else {
+        monitor_printf(mon, "There is no suitable snapshot available to be loaded.\n");

Where is available_snapshots freed when control flows through this point?

Quoted 4 lines
savevm.c +2 -0
+    }
+
     qemu_free(sn_tab);
 }
#2 lore raw
Quoted 8 lines · Markus Armbruster
Quoted 3 lines · Miguel Di Ciurcio Filho

The output generated by 'info snapshots' shows only snapshots that exist on the block device that saves the VM state. This output can cause an user to erroneously try to load an snapshot that is not available on all block devices.

What happens when you try that?

I've sent a patch that will protect that from happening [1]. With that patch, the VM stays stopped, without it the VM keeps running with a failed bdrv_snapshot_goto().

Quoted 52 lines
Quoted 38 lines

---
 savevm.c |   65 ++++++++++++++++++++++++++++++++++++++++++-------------------

 1 files changed, 45 insertions(+), 20 deletions(-)
savevm.c +7 -13
index 7a1de3c..be83878 100644
@@ -1997,37 +1997,62 @@ void do_delvm(Monitor *mon, const QDict *qdict)
 
  void do_info_snapshots(Monitor *mon)
  {
-    BlockDriverState *bs, *bs1;
-    QEMUSnapshotInfo *sn_tab, *sn;
-    int nb_sns, i;
+    BlockDriverState *bs_vm_state, *bs;
+    QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
+    int nb_sns, i, ret, available;
+    int total;
+    int *available_snapshots;
      char buf[256];
 
-    bs = bdrv_snapshots();
-    if (!bs) {
+    bs_vm_state = bdrv_snapshots();
+    if (!bs_vm_state) {
          monitor_printf(mon, "No available block device supports snapshots\n");
          return;
      }
-    monitor_printf(mon, "Snapshot devices:");
-    bs1 = NULL;
-    while ((bs1 = bdrv_next(bs1))) {
-        if (bdrv_can_snapshot(bs1)) {
-            if (bs == bs1)
-                monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
-        }
-    }

savevm.c void do_delvm(Monitor *mon, const QDict *qdict) · line 2019

Quoted 10 lines
savevm.c +3 -2
-    monitor_printf(mon, "\n");
 
-    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
+    nb_sns = bdrv_snapshot_list(bs_vm_state, &sn_tab);
      if (nb_sns < 0) {
          monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
          return;
+    } else if (nb_sns == 0) {
+        monitor_printf(mon, "There is no snapshot available.\n");
      }

This changes output for the "no snapshots available" case from the empty table

Quoted 7 lines

   ID        TAG                 VM SIZE                DATE       VM CLOCK

to

   There is no snapshot available.

I'd prefer that as separate patch, if at all.

I think a clear message saying "there is nothing" is better than an empty table. I'm already changing the output to something more reasonable, so.

Quoted 3 lines

Nitpick: I don't like "return; else".

Yeah, kinda ugly. I will fix it.

Quoted 9 lines

savevm.c void do_delvm(Monitor *mon, const QDict *qdict) · line 2021

Quoted 6 lines
savevm.c +2 -4
-    monitor_printf(mon, "Snapshot list (from %s):\n",
-                   bdrv_get_device_name(bs));
-    monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
-    for(i = 0; i < nb_sns; i++) {
+
+    available_snapshots = qemu_mallocz(sizeof(int) * nb_sns);

This can die due to the nonsensical semantics of qemu_mallocz(0).

Will fix that, so this code will be reached only if nb_sns > 0 and qemu_mallocz(0) will never be executed.

Quoted 28 lines
Quoted 28 lines
savevm.c +25 -1
+    total = 0;
+    for (i = 0; i < nb_sns; i++) {
          sn = &sn_tab[i];
-        monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
+        available = 1;
+        bs = NULL;
+
+        while ((bs = bdrv_next(bs))) {
+            if (bdrv_can_snapshot(bs) && bs != bs_vm_state) {
+                ret = bdrv_snapshot_find(bs, sn_info, sn->id_str);
+                if (ret < 0) {
+                    available = 0;
+                    break;
+                }
+            }
+        }
+
+        if (available) {
+            available_snapshots[total] = i;
+            total++;
+        }
      }
+
+    if (total > 0) {
+        monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
+        for (i = 0; i < total; i++) {
+            sn = &sn_tab[available_snapshots[i]];
+            monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
Quoted 9 lines

savevm.c void do_delvm(Monitor *mon, const QDict *qdict) · line 2054

Quoted 6 lines
savevm.c +6 -0
+        }
+
+        qemu_free(available_snapshots);
+
+    } else {
+        monitor_printf(mon, "There is no suitable snapshot available to be loaded.\n");

Where is available_snapshots freed when control flows through this point?

Oops. I will fix that too.

Thanks for the feedback.

Regards,

Miguel

[1] http://lists.gnu.org/archive/html/qemu-devel/2010-07/msg01065.html
(Kevin has applied it to this block branch)