Browse Source

Better information on error in qvm-remove

If qvm-remove fails because the VM is in use, it will display
information about where it is used.

fixes QubesOS/qubes-issues#3193
Marta Marczykowska-Górecka 5 years ago
parent
commit
ad2a6e3408
2 changed files with 14 additions and 0 deletions
  1. 4 0
      qubesadmin/exc.py
  2. 10 0
      qubesadmin/tools/qvm_remove.py

+ 4 - 0
qubesadmin/exc.py

@@ -85,6 +85,10 @@ class QubesNoTemplateError(QubesVMError):
     '''Cannot start domain, because there is no template'''
 
 
+class QubesVMInUseError(QubesVMError):
+    '''VM is in use, cannot remove.'''
+
+
 class QubesValueError(QubesException, ValueError):
     '''Cannot set some value, because it is invalid, out of bounds, etc.'''
     pass

+ 10 - 0
qubesadmin/tools/qvm_remove.py

@@ -25,6 +25,7 @@ import sys
 
 import qubesadmin.exc
 from qubesadmin.tools import QubesArgumentParser
+import qubesadmin.utils
 
 parser = QubesArgumentParser(description=__doc__,
                              want_app=True,
@@ -47,6 +48,15 @@ def main(args=None, app=None):  # pylint: disable=missing-docstring
         for vm in args.domains:
             try:
                 del args.app.domains[vm.name]
+            except qubesadmin.exc.QubesVMInUseError:
+                dependencies = qubesadmin.utils.vm_dependencies(vm.app, vm)
+                print("VM {} cannot be removed. It is in use as:".format(
+                    vm.name))
+                for (holder, prop) in dependencies:
+                    if holder:
+                        print(" - {} for {}".format(prop, holder.name))
+                    else:
+                        print(" - global property {}".format(prop))
             except qubesadmin.exc.QubesException as e:
                 parser.error_runtime(e)
         retcode = 0