[virt-tools-list] [PATCH] Added fix for deleting disk when removing disk from a VM

Abhijeet Kasurde akasurde at redhat.com
Thu Dec 3 12:26:27 UTC 2015


This fix adds a user dialog box for confirmation specific to disk,
also deletes disk from storage.

Signed-off-by: Abhijeet Kasurde <akasurde at redhat.com>
---
 virtManager/details.py | 86 ++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 79 insertions(+), 7 deletions(-)

diff --git a/virtManager/details.py b/virtManager/details.py
index 742448d..90f605f 100644
--- a/virtManager/details.py
+++ b/virtManager/details.py
@@ -1,5 +1,5 @@
 #
-# Copyright (C) 2006-2008, 2013, 2014 Red Hat, Inc.
+# Copyright (C) 2006-2008, 2013-2015 Red Hat, Inc.
 # Copyright (C) 2006 Daniel P. Berrange <berrange at redhat.com>
 #
 # This program is free software; you can redistribute it and/or modify
@@ -38,10 +38,12 @@ from .netlist import vmmNetworkList
 from .snapshots import vmmSnapshotPage
 from .storagebrowse import vmmStorageBrowser
 from .graphwidgets import Sparkline
+from .asyncjob import vmmAsyncJob
 
 import virtinst
 from virtinst import util
 from virtinst import VirtualRNGDevice
+import os
 
 
 # Parameters that can be edited in the details window
@@ -2232,15 +2234,27 @@ class vmmDetails(vmmGObjectUI):
                                           kwargs, self.vm, self.err,
                                           devobj=devobj)
 
-
     # Device removal
     def remove_device(self, devobj):
         logging.debug("Removing device: %s", devobj)
-
-        if not self.err.chkbox_helper(self.config.get_confirm_removedev,
-            self.config.set_confirm_removedev,
-            text1=(_("Are you sure you want to remove this device?"))):
-            return
+        path = None
+
+        if devobj.virtual_device_type == "disk":
+            path = devobj.path
+            title = _("Are you sure you want to delete the storage?")
+            message = "The following path will be deleted:\n%s\n" % (path)
+            ret = self.err.chkbox_helper(
+                self.config.get_confirm_delstorage,
+                self.config.set_confirm_delstorage,
+                text1=title, text2=message)
+            if not ret:
+                return
+        else:
+            if not self.err.chkbox_helper(self.config.get_confirm_removedev,
+                                          self.config.set_confirm_removedev,
+                                          text1=(_("Are you sure you want to "
+                                                   "remove this device?"))):
+                return
 
         # Define the change
         try:
@@ -2249,6 +2263,19 @@ class vmmDetails(vmmGObjectUI):
             self.err.show_err(_("Error Removing Device: %s") % str(e))
             return
 
+        # Remove file path associated with disk
+        if devobj.virtual_device_type == "disk" and path:
+            self.topwin.set_sensitive(False)
+            self.topwin.get_window().set_cursor(Gdk.Cursor.new(Gdk.CursorType.WATCH))
+
+            title = "Deleting selected storage"
+            text = title + " (this may take a while)"
+
+            progWin = vmmAsyncJob(self._async_delete, [path],
+                                  self._finish_cb, [],
+                                  title, text, self.topwin)
+            progWin.run()
+
         # Try to hot remove
         detach_err = False
         try:
@@ -2270,6 +2297,51 @@ class vmmDetails(vmmGObjectUI):
             buttons=Gtk.ButtonsType.OK,
             dialog_type=Gtk.MessageType.INFO)
 
+    def _async_delete(self, asyncjob, path):
+        storage_errors = []
+        storage_errstr = ""
+        conn = self.conn.get_backend()
+        meter = asyncjob.get_meter()
+        try:
+            logging.debug("Deleting path: %s", path)
+            meter.start(text=_("Deleting path '%s'") % path)
+            self._async_delete_path(conn, path, meter)
+        except Exception, e:
+            storage_errors.append((str(e), "".join(traceback.format_exc())))
+            meter.end(0)
+        for errinfo in storage_errors:
+            storage_errstr += "%s\n%s\n" % (errinfo[0], errinfo[1])
+
+        if not storage_errstr:
+            return
+
+        error = _("Errors encountered while removing certain storage devices.")
+        details = storage_errstr
+
+        if error:
+            asyncjob.set_error(error, details)
+
+    def _async_delete_path(self, conn, path, ignore):
+        vol = None
+
+        try:
+            vol = conn.storageVolLookupByPath(path)
+        except:
+            logging.debug("Path '%s' is not managed. Deleting locally", path)
+
+        if vol:
+            vol.delete(0)
+        else:
+            os.unlink(path)
+
+    def _finish_cb(self, error, details):
+        self.topwin.set_sensitive(True)
+        self.topwin.get_window().set_cursor(
+            Gdk.Cursor.new(Gdk.CursorType.TOP_LEFT_ARROW))
+        if error is not None:
+            self.err.show_err(error, details=details)
+
+        self.conn.schedule_priority_tick(pollvm=True)
 
     ########################
     # Details page refresh #
-- 
2.4.3




More information about the virt-tools-list mailing list