dom0: move NetVM shutdown sanity check code to qvm-core

This commit is contained in:
Marek Marczykowski 2011-10-17 22:15:10 +02:00
parent e6bbc83e0b
commit 3063ef35b7
3 changed files with 24 additions and 12 deletions

View File

@ -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

View File

@ -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)

View File

@ -27,6 +27,10 @@ import sys
def main():
usage = "usage: %prog [options] <vm-name>"
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)