qubes/vm: fix handling dynamic netvm change

Correctly detach network before attaching new one.
This commit is contained in:
Marek Marczykowski-Górecki 2016-09-06 13:36:07 +02:00
parent c534b68665
commit f6bc61997a
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724

View File

@ -217,7 +217,7 @@ class NetVMMixin(qubes.events.Emitter):
raise qubes.exc.QubesVMNotRunningError(self) raise qubes.exc.QubesVMNotRunningError(self)
assert self.netvm is not None assert self.netvm is not None
self.libvirt_domain.attachDevice( self.libvirt_domain.detachDevice(
self.app.env.get_template('libvirt/devices/net.xml').render( self.app.env.get_template('libvirt/devices/net.xml').render(
vm=self)) vm=self))
@ -273,24 +273,26 @@ class NetVMMixin(qubes.events.Emitter):
def on_property_pre_set_netvm(self, event, name, new_netvm, old_netvm=None): def on_property_pre_set_netvm(self, event, name, new_netvm, old_netvm=None):
''' Run sanity checks before setting a new NetVM ''' ''' Run sanity checks before setting a new NetVM '''
# pylint: disable=unused-argument # pylint: disable=unused-argument
if new_netvm is None: if new_netvm is not None:
return
if not new_netvm.provides_network: if not new_netvm.provides_network:
raise qubes.exc.QubesValueError( raise qubes.exc.QubesValueError(
'The {!s} qube does not provide network'.format(new_netvm)) 'The {!s} qube does not provide network'.format(new_netvm))
if new_netvm is self \ if new_netvm is self \
or new_netvm in self.app.domains.get_vms_connected_to(self): or new_netvm in self.app.domains.get_vms_connected_to(self):
raise qubes.exc.QubesValueError('Loops in network are unsupported') raise qubes.exc.QubesValueError(
'Loops in network are unsupported')
if not self.app.vmm.offline_mode and self.is_running() \
and not new_netvm.is_running():
if not self.app.vmm.offline_mode \
and self.is_running() and not new_netvm.is_running():
raise qubes.exc.QubesVMNotStartedError(new_netvm, raise qubes.exc.QubesVMNotStartedError(new_netvm,
'Cannot dynamically attach to stopped NetVM: {!r}'.format( 'Cannot dynamically attach to stopped NetVM: {!r}'.format(
new_netvm)) new_netvm))
if old_netvm is not None:
if self.is_running():
self.detach_network()
@qubes.events.handler('property-set:netvm') @qubes.events.handler('property-set:netvm')
def on_property_set_netvm(self, event, name, new_netvm, old_netvm=None): def on_property_set_netvm(self, event, name, new_netvm, old_netvm=None):
''' Replaces the current NetVM with a new one and fires ''' Replaces the current NetVM with a new one and fires
@ -298,10 +300,6 @@ class NetVMMixin(qubes.events.Emitter):
''' '''
# pylint: disable=unused-argument # pylint: disable=unused-argument
if self.netvm is not None:
if self.is_running():
self.detach_network()
if new_netvm is None: if new_netvm is None:
return return