[Kimchi-devel] [PATCH v2 09/10] snapshot: Delete snapshots when deleting a VM

Crístian Viana vianac at linux.vnet.ibm.com
Wed Nov 12 19:12:11 UTC 2014


If a virtual machine is deleted, its snapshots should also be deleted.

Update the function "VMModel.delete" in order to delete snapshots
before deleting the virtual machine.

Signed-off-by: Crístian Viana <vianac at linux.vnet.ibm.com>
---
 src/kimchi/model/vms.py | 18 ++++++++++++++++++
 tests/test_model.py     |  3 +--
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/src/kimchi/model/vms.py b/src/kimchi/model/vms.py
index d194049..211e438 100644
--- a/src/kimchi/model/vms.py
+++ b/src/kimchi/model/vms.py
@@ -268,6 +268,10 @@ class VMModel(object):
         self.storagepool = model.storagepools.StoragePoolModel(**kargs)
         self.storagevolume = model.storagevolumes.StorageVolumeModel(**kargs)
         self.storagevolumes = model.storagevolumes.StorageVolumesModel(**kargs)
+        cls = import_class('kimchi.model.vmsnapshots.VMSnapshotModel')
+        self.vmsnapshot = cls(**kargs)
+        cls = import_class('kimchi.model.vmsnapshots.VMSnapshotsModel')
+        self.vmsnapshots = cls(**kargs)
 
     def update(self, name, params):
         dom = self.get_vm(name, self.conn)
@@ -752,6 +756,20 @@ class VMModel(object):
         if info['state'] == 'running':
             self.poweroff(name)
 
+        # delete existing snapshots before deleting VM
+
+        # libvirt's Test driver does not support the function
+        # "virDomainListAllSnapshots", so "VMSnapshots.get_list" will raise
+        # "OperationFailed" in that case.
+        try:
+            snapshot_names = self.vmsnapshots.get_list(name)
+        except OperationFailed, e:
+            kimchi_log.error('cannot list snapshots: %s; '
+                             'skipping snapshot deleting...' % e.message)
+        else:
+            for s in snapshot_names:
+                self.vmsnapshot.delete(name, s)
+
         try:
             dom.undefine()
         except libvirt.libvirtError as e:
diff --git a/tests/test_model.py b/tests/test_model.py
index cbcaed7..86ab24a 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -122,10 +122,9 @@ class ModelTests(unittest.TestCase):
             self.assertRaises(NotFoundError, inst.currentvmsnapshot_lookup,
                               u'kimchi-vm')
 
+            # this snapshot should be deleted when its VM is deleted
             params = {'name': u'mysnap'}
             task = inst.vmsnapshots_create(u'kimchi-vm', params)
-            rollback.prependDefer(inst.vmsnapshot_delete,
-                                  u'kimchi-vm', params['name'])
             inst.task_wait(task['id'])
             task = inst.task_lookup(task['id'])
             self.assertEquals('finished', task['status'])
-- 
1.9.3




More information about the Kimchi-devel mailing list