diff --git a/dom0/qvm-core/qubes.py b/dom0/qvm-core/qubes.py index b50546a6..8f1ef813 100755 --- a/dom0/qvm-core/qubes.py +++ b/dom0/qvm-core/qubes.py @@ -1082,7 +1082,7 @@ class QubesVm(object): return xid - def shutdown(self): + def shutdown(self, force=False): if dry_run: return @@ -1495,6 +1495,16 @@ class QubesNetVm(QubesVm): return xid + def shutdown(self, force=False): + if dry_run: + return + + connected_vms = [vm for vm in self.connected_vms.values() if vm.is_running()] + if connected_vms and not force: + raise QubesException("There are other VMs connected to this VM: " + str([vm.name for vm in connected_vms])) + + super(QubesNetVm, self).shutdown(force=force) + def add_external_ip_permission(self, xid): if int(xid) < 0: return diff --git a/dom0/qvm-tools/qvm-run b/dom0/qvm-tools/qvm-run index 7c69bc95..7de43703 100755 --- a/dom0/qvm-tools/qvm-run +++ b/dom0/qvm-tools/qvm-run @@ -80,7 +80,14 @@ def vm_run_cmd(vm, cmd, options): if options.shutdown: if options.verbose: print >> sys.stderr, "Shutting down VM: '{0}'...".format(vm.name) - vm.shutdown() + try: + vm.shutdown(force=options.force) + except (QubesException) as err: + # "There are other VMs connected to this VM:" + print >> sys.stderr, "ERROR: {0}".format(err) + if str(err).startswith("There are other VMs connected"): + print >> sys.stderr, "Shutdown them first or use --force switch" + exit(1) return if options.pause: @@ -222,15 +229,6 @@ def main(): print >> sys.stderr, "A VM with the name '{0}' does not exist in the system!".format(vmname) exit(1) vms_list.append(vm) - - # If stopping NetVM - stop connected VMs too - if options.shutdown and vm.is_netvm(): - connected_vms = [vm for vm in qvm_collection.get_vms_connected_to(vm.qid) if vm.is_running()] - if connected_vms and not options.force: - print >> sys.stderr, "ERROR: There are other VMs connected to this VM, " - print >> sys.stderr, " shutdown them first or use --force option" - print >> sys.stderr, "VMs list: " + str([vm.name for vm in connected_vms]) - exit(1) if takes_cmd_argument: cmd = "{user}:{cmd}".format(user=options.user, cmd=cmdstr) diff --git a/dom0/qvm-tools/qvm-shutdown b/dom0/qvm-tools/qvm-shutdown index b188124c..a89ac900 100755 --- a/dom0/qvm-tools/qvm-shutdown +++ b/dom0/qvm-tools/qvm-shutdown @@ -27,6 +27,10 @@ import sys def main(): usage = "usage: %prog [options] " parser = OptionParser (usage) + + parser.add_option ("--force", action="store_true", dest="force", default=False, + help="Force operation, even if may damage other VMs (eg shutdown of NetVM)") + (options, args) = parser.parse_args () if (len (args) != 1): parser.error ("You must specify VM name!") @@ -43,7 +47,7 @@ def main(): exit(1) try: - vm.shutdown() + vm.shutdown(force=options.force) except (IOError, OSError) as err: print >> sys.stderr, "ERROR: {0}".format(err) exit (1)