core-admin/qubes/vm/mix/dvmtemplate.py
Marek Marczykowski-Górecki 8041b72e42
vm: do not allow setting template_for_dispvms=False if there are any DispVMs
Setting template_for_dispvms=False will at least prevent starting
(already existing) DispVMs based on it. Those should be first removed.
Add also tests for this case.
2019-03-01 05:56:57 +01:00

65 lines
2.4 KiB
Python

# -*- encoding: utf-8 -*-
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2017 Marek Marczykowski-Górecki
# <marmarek@invisiblethingslab.com>
#
# 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 <http://www.gnu.org/licenses/>.
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_for_dispvms')
def __on_pre_set_dvmtemplate(self, event, name,
newvalue, oldvalue=None):
# pylint: disable=unused-argument
if newvalue:
return
if any(self.dispvms):
raise qubes.exc.QubesVMInUseError(self,
'Cannot change template_for_dispvms to False while there are '
'some DispVMs based on this DVM template')
@qubes.events.handler('property-pre-del:template_for_dispvms')
def __on_pre_del_dvmtemplate(self, event, name,
oldvalue=None):
self.__on_pre_set_dvmtemplate(
event, name, False, oldvalue)
@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