[gnome-boxes] Delete storage volume along with its VM



commit b203c6c489a2245f25cec825a309dded3f8001af
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Thu Jan 12 16:21:46 2012 +0200

    Delete storage volume along with its VM
    
    https://bugzilla.gnome.org/show_bug.cgi?id=666369

 src/app.vala             |    4 ++++
 src/libvirt-machine.vala |   44 ++++++++++++++++++++++++++++++++++++++------
 src/vm-creator.vala      |    4 +---
 3 files changed, 43 insertions(+), 9 deletions(-)
---
diff --git a/src/app.vala b/src/app.vala
index c3f9fc1..a7a1a78 100644
--- a/src/app.vala
+++ b/src/app.vala
@@ -152,6 +152,10 @@ private class Boxes.App: Boxes.UI {
             yield connection.open_async (null);
             yield connection.fetch_domains_async (null);
             yield connection.fetch_storage_pools_async (null);
+            var pool = connection.find_storage_pool_by_name (Config.PACKAGE_TARNAME);
+            if (pool != null)
+                // If default storage pool exists, we should refresh it already
+                yield pool.refresh_async (null);
         } catch (GLib.Error error) {
             warning (error.message);
         }
diff --git a/src/libvirt-machine.vala b/src/libvirt-machine.vala
index c7badc4..4590cb9 100644
--- a/src/libvirt-machine.vala
+++ b/src/libvirt-machine.vala
@@ -321,13 +321,20 @@ private class Boxes.LibvirtMachine: Boxes.Machine {
     public override void delete (bool by_user = true) {
         if (by_user) {
             try {
-                if (is_running ())
-                    domain.stop (0);
-            } catch (GLib.Error err) {
-                // ignore stop error
-            }
+                // The reason we fetch the volume before stopping the domain is that we need the domain's
+                // configuration for fechting its volume and transient domains stop existing after they are stopped.
+                // OTOH we can't just delete the volume from a running domain.
+                var volume = get_storage_volume ();
 
-            try {
+                try {
+                    if (is_running ())
+                        domain.stop (0);
+                } catch (GLib.Error err) {
+                    // ignore stop error
+                }
+
+                if (volume != null)
+                    volume.delete (0);
                 domain.delete (0);
             } catch (GLib.Error err) {
                 warning (err.message);
@@ -340,4 +347,29 @@ private class Boxes.LibvirtMachine: Boxes.Machine {
     public async void suspend () throws GLib.Error {
         (save_on_quit) ? yield domain.save_async (0, null) : domain.suspend ();
     }
+
+    private GVir.StorageVol? get_storage_volume () throws GLib.Error {
+        if (connection != app.default_connection)
+            return null;
+
+        var config = domain.get_config (0);
+        var pool = connection.find_storage_pool_by_name (Config.PACKAGE_TARNAME);
+        if (pool == null)
+            // Absence of our pool just means that disk was not created by us and therefore should not be deleted by
+            // us either.
+            return null;
+
+        foreach (var device in config.get_devices ()) {
+            if (!(device is GVirConfig.DomainDisk))
+                continue;
+
+            var path = (device as GVirConfig.DomainDisk).get_source ();
+
+            foreach (var volume in pool.get_volumes ())
+                if (volume.get_path () == path)
+                    return volume;
+        }
+
+        return null;
+    }
 }
diff --git a/src/vm-creator.vala b/src/vm-creator.vala
index 1dd7016..b4c7651 100644
--- a/src/vm-creator.vala
+++ b/src/vm-creator.vala
@@ -108,11 +108,9 @@ private class Boxes.VMCreator {
             pool = connection.create_storage_pool (config, 0);
             yield pool.build_async (0, null);
             yield pool.start_async (0, null);
+            yield pool.refresh_async (null);
         }
 
-        // This should be async
-        pool.refresh (null);
-
         return pool;
     }
 }



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