From 4a3772bb5700e9ce7d802afc6110c8d9313fb327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Wed, 27 Feb 2019 05:50:26 +0100 Subject: [PATCH] vm: move DVM template specific code into separate mixin No functional change. --- qubes/vm/appvm.py | 22 +++-------------- qubes/vm/mix/dvmtemplate.py | 47 +++++++++++++++++++++++++++++++++++++ rpm_spec/core-dom0.spec.in | 1 + 3 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 qubes/vm/mix/dvmtemplate.py diff --git a/qubes/vm/appvm.py b/qubes/vm/appvm.py index df69569f..6d2f82d4 100644 --- a/qubes/vm/appvm.py +++ b/qubes/vm/appvm.py @@ -24,10 +24,12 @@ import copy import qubes.events import qubes.vm.qubesvm +import qubes.vm.mix.dvmtemplate from qubes.config import defaults -class AppVM(qubes.vm.qubesvm.QubesVM): +class AppVM(qubes.vm.mix.dvmtemplate.DVMTemplateMixin, + qubes.vm.qubesvm.QubesVM): '''Application VM''' template = qubes.VMProperty('template', @@ -35,11 +37,6 @@ class AppVM(qubes.vm.qubesvm.QubesVM): vmclass=qubes.vm.templatevm.TemplateVM, doc='Template, on which this AppVM is based.') - template_for_dispvms = qubes.property('template_for_dispvms', - type=bool, - default=False, - doc='Should this VM be allowed to start as Disposable VM') - default_volume_config = { 'root': { 'name': 'root', @@ -97,15 +94,6 @@ class AppVM(qubes.vm.qubesvm.QubesVM): super(AppVM, self).__init__(app, xml, **kwargs) - @property - def dispvms(self): - ''' Returns a generator containing all Disposable VMs based on the - current AppVM. - ''' - for vm in self.app.domains: - if hasattr(vm, 'template') and vm.template is self: - yield vm - @qubes.events.handler('domain-load') def on_domain_loaded(self, event): ''' When domain is loaded assert that this vm has a template. @@ -126,10 +114,6 @@ class AppVM(qubes.vm.qubesvm.QubesVM): if not self.is_halted(): raise qubes.exc.QubesVMNotHaltedError(self, 'Cannot change template while qube is running') - if any(self.dispvms): - raise qubes.exc.QubesVMInUseError(self, - 'Cannot change template ' - 'while there are DispVMs based on this qube') @qubes.events.handler('property-set:template') def on_property_set_template(self, event, name, newvalue, oldvalue=None): diff --git a/qubes/vm/mix/dvmtemplate.py b/qubes/vm/mix/dvmtemplate.py new file mode 100644 index 00000000..8c46125f --- /dev/null +++ b/qubes/vm/mix/dvmtemplate.py @@ -0,0 +1,47 @@ +# -*- encoding: utf-8 -*- +# +# The Qubes OS Project, http://www.qubes-os.org +# +# Copyright (C) 2017 Marek Marczykowski-Górecki +# +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, see . + +import qubes.events + +class DVMTemplateMixin(qubes.events.Emitter): + '''VM class capable of being DVM template''' + + template_for_dispvms = qubes.property('template_for_dispvms', + type=bool, + default=False, + doc='Should this VM be allowed to start as Disposable VM') + + @qubes.events.handler('property-pre-set:template') + def __on_property_pre_set_template(self, event, name, newvalue, + oldvalue=None): + # pylint: disable=unused-argument + if any(self.dispvms): + raise qubes.exc.QubesVMInUseError(self, + 'Cannot change template ' + 'while there are DispVMs based on this qube') + + @property + def dispvms(self): + ''' Returns a generator containing all Disposable VMs based on the + current AppVM. + ''' + for vm in self.app.domains: + if getattr(vm, 'template', None) == self: + yield vm diff --git a/rpm_spec/core-dom0.spec.in b/rpm_spec/core-dom0.spec.in index 1e4c5987..07f672a4 100644 --- a/rpm_spec/core-dom0.spec.in +++ b/rpm_spec/core-dom0.spec.in @@ -249,6 +249,7 @@ fi %dir %{python3_sitelib}/qubes/vm/mix/__pycache__ %{python3_sitelib}/qubes/vm/mix/__pycache__/* %{python3_sitelib}/qubes/vm/mix/__init__.py +%{python3_sitelib}/qubes/vm/mix/dvmtemplate.py %{python3_sitelib}/qubes/vm/mix/net.py %dir %{python3_sitelib}/qubes/storage