2016-06-16 14:13:46 +02:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014-2016 Wojtek Porczyk <woju@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2016 Marek Marczykowski <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, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
#
|
|
|
|
|
|
|
|
''' A disposable vm implementation '''
|
2017-01-18 22:16:46 +01:00
|
|
|
|
2017-07-08 02:53:09 +02:00
|
|
|
import asyncio
|
|
|
|
|
2014-11-13 14:38:41 +01:00
|
|
|
import qubes.vm.qubesvm
|
2016-05-20 03:58:57 +02:00
|
|
|
import qubes.vm.appvm
|
|
|
|
import qubes.config
|
2014-11-13 14:38:41 +01:00
|
|
|
|
|
|
|
class DispVM(qubes.vm.qubesvm.QubesVM):
|
2014-11-13 18:10:27 +01:00
|
|
|
'''Disposable VM'''
|
2016-05-20 03:58:57 +02:00
|
|
|
|
|
|
|
template = qubes.VMProperty('template',
|
|
|
|
load_stage=4,
|
|
|
|
vmclass=qubes.vm.appvm.AppVM,
|
|
|
|
doc='AppVM, on which this DispVM is based.')
|
|
|
|
|
|
|
|
dispid = qubes.property('dispid', type=int, write_once=True,
|
|
|
|
clone=False,
|
|
|
|
doc='''Internal, persistent identifier of particular DispVM.''')
|
|
|
|
|
2017-08-06 12:27:35 +02:00
|
|
|
auto_cleanup = qubes.property('auto_cleanup', type=bool, default=False,
|
|
|
|
doc='automatically remove this VM upon shutdown')
|
|
|
|
|
|
|
|
def __init__(self, app, xml, *args, **kwargs):
|
2016-05-20 03:58:57 +02:00
|
|
|
self.volume_config = {
|
|
|
|
'root': {
|
|
|
|
'name': 'root',
|
2016-07-12 18:03:14 +02:00
|
|
|
'snap_on_start': True,
|
|
|
|
'save_on_stop': False,
|
|
|
|
'rw': False,
|
2017-07-08 02:36:39 +02:00
|
|
|
'source': None,
|
2016-05-20 03:58:57 +02:00
|
|
|
},
|
|
|
|
'private': {
|
|
|
|
'name': 'private',
|
2016-07-12 18:03:14 +02:00
|
|
|
'snap_on_start': True,
|
|
|
|
'save_on_stop': False,
|
|
|
|
'rw': True,
|
2017-07-08 02:36:39 +02:00
|
|
|
'source': None,
|
2016-05-20 03:58:57 +02:00
|
|
|
},
|
|
|
|
'volatile': {
|
|
|
|
'name': 'volatile',
|
2017-07-01 20:47:08 +02:00
|
|
|
'snap_on_start': False,
|
|
|
|
'save_on_stop': False,
|
2016-08-17 00:42:17 +02:00
|
|
|
'rw': True,
|
2016-05-20 03:58:57 +02:00
|
|
|
'size': qubes.config.defaults['root_img_size'] +
|
|
|
|
qubes.config.defaults['private_img_size'],
|
|
|
|
},
|
|
|
|
'kernel': {
|
|
|
|
'name': 'kernel',
|
2017-07-01 20:47:08 +02:00
|
|
|
'snap_on_start': False,
|
|
|
|
'save_on_stop': False,
|
2016-07-12 18:03:14 +02:00
|
|
|
'rw': False,
|
2016-05-20 03:58:57 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-06 12:27:35 +02:00
|
|
|
|
2016-08-17 00:42:17 +02:00
|
|
|
template = kwargs.get('template', None)
|
|
|
|
|
2017-08-06 12:27:35 +02:00
|
|
|
if xml is None:
|
2017-09-03 03:04:56 +02:00
|
|
|
assert template is not None
|
|
|
|
|
2017-09-03 03:11:48 +02:00
|
|
|
if not template.template_for_dispvms:
|
2017-09-03 03:04:56 +02:00
|
|
|
raise qubes.exc.QubesValueError(
|
|
|
|
'template for DispVM ({}) needs to have '
|
2017-09-03 03:11:48 +02:00
|
|
|
'template_for_dispvms=True'.format(template.name))
|
2017-09-03 03:04:56 +02:00
|
|
|
|
2017-08-06 12:27:35 +02:00
|
|
|
if 'dispid' not in kwargs:
|
|
|
|
kwargs['dispid'] = app.domains.get_new_unused_dispid()
|
|
|
|
if 'name' not in kwargs:
|
|
|
|
kwargs['name'] = 'disp' + str(kwargs['dispid'])
|
|
|
|
|
2016-08-17 00:42:17 +02:00
|
|
|
if template is not None:
|
|
|
|
# template is only passed if the AppVM is created, in other cases we
|
|
|
|
# don't need to patch the volume_config because the config is
|
|
|
|
# coming from XML, already as we need it
|
|
|
|
for name, config in template.volume_config.items():
|
|
|
|
# in case the template vm has more volumes add them to own
|
|
|
|
# config
|
|
|
|
if name not in self.volume_config:
|
2017-07-12 10:43:48 +02:00
|
|
|
self.volume_config[name] = config.copy()
|
2016-08-17 00:42:17 +02:00
|
|
|
if 'vid' in self.volume_config[name]:
|
|
|
|
del self.volume_config[name]['vid']
|
|
|
|
|
2017-08-06 12:27:35 +02:00
|
|
|
super(DispVM, self).__init__(app, xml, *args, **kwargs)
|
2016-06-02 17:20:13 +02:00
|
|
|
|
2017-08-06 12:27:35 +02:00
|
|
|
if xml is None:
|
2017-09-03 03:04:56 +02:00
|
|
|
# by default inherit properties from the DispVM template
|
|
|
|
proplist = [prop.__name__ for prop in template.property_list()
|
|
|
|
if prop.clone and prop.__name__ not in ['template']]
|
|
|
|
self_props = [prop.__name__ for prop in self.property_list()]
|
|
|
|
self.clone_properties(template, set(proplist).intersection(
|
|
|
|
self_props))
|
|
|
|
|
2017-08-06 12:27:35 +02:00
|
|
|
self.firewall.clone(template.firewall)
|
|
|
|
self.features.update(template.features)
|
|
|
|
self.tags.update(template.tags)
|
2016-05-20 03:58:57 +02:00
|
|
|
|
|
|
|
@qubes.events.handler('domain-load')
|
|
|
|
def on_domain_loaded(self, event):
|
2016-06-16 14:13:46 +02:00
|
|
|
''' When domain is loaded assert that this vm has a template.
|
|
|
|
''' # pylint: disable=unused-argument
|
2016-05-20 03:58:57 +02:00
|
|
|
assert self.template
|
2016-06-02 17:20:13 +02:00
|
|
|
|
2017-06-03 12:22:36 +02:00
|
|
|
@qubes.events.handler('property-pre-set:template')
|
|
|
|
def on_property_pre_set_template(self, event, name, newvalue,
|
|
|
|
oldvalue=None):
|
|
|
|
''' Disposable VM cannot have template changed '''
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
raise qubes.exc.QubesValueError(self,
|
|
|
|
'Cannot change template of Disposable VM')
|
|
|
|
|
2017-08-06 12:27:35 +02:00
|
|
|
@asyncio.coroutine
|
|
|
|
def on_domain_shutdown_coro(self):
|
|
|
|
'''Coroutine for executing cleanup after domain shutdown.
|
|
|
|
|
|
|
|
This override default action defined in QubesVM.on_domain_shutdown_coro
|
|
|
|
'''
|
|
|
|
with (yield from self.startup_lock):
|
|
|
|
yield from self.storage.stop()
|
2017-09-22 22:45:47 +02:00
|
|
|
if self.auto_cleanup and self in self.app.domains:
|
2017-08-06 12:27:35 +02:00
|
|
|
yield from self.remove_from_disk()
|
|
|
|
del self.app.domains[self]
|
|
|
|
self.app.save()
|
|
|
|
|
2016-06-02 17:20:13 +02:00
|
|
|
@classmethod
|
2017-07-08 02:53:09 +02:00
|
|
|
@asyncio.coroutine
|
2016-06-02 17:20:13 +02:00
|
|
|
def from_appvm(cls, appvm, **kwargs):
|
|
|
|
'''Create a new instance from given AppVM
|
|
|
|
|
|
|
|
:param qubes.vm.appvm.AppVM appvm: template from which the VM should \
|
2017-03-20 22:18:10 +01:00
|
|
|
be created
|
2016-06-02 17:20:13 +02:00
|
|
|
:returns: new disposable vm
|
|
|
|
|
|
|
|
*kwargs* are passed to the newly created VM
|
|
|
|
|
|
|
|
>>> import qubes.vm.dispvm.DispVM
|
|
|
|
>>> dispvm = qubes.vm.dispvm.DispVM.from_appvm(appvm).start()
|
|
|
|
>>> dispvm.run_service('qubes.VMShell', input='firefox')
|
|
|
|
>>> dispvm.cleanup()
|
|
|
|
|
2017-03-20 22:18:10 +01:00
|
|
|
This method modifies :file:`qubes.xml` file.
|
2016-06-02 17:20:13 +02:00
|
|
|
The qube returned is not started.
|
|
|
|
'''
|
2017-09-03 03:11:48 +02:00
|
|
|
if not appvm.template_for_dispvms:
|
2017-03-21 12:46:51 +01:00
|
|
|
raise qubes.exc.QubesException(
|
2017-08-06 12:27:35 +02:00
|
|
|
'Refusing to create DispVM out of this AppVM, because '
|
2017-09-22 22:26:32 +02:00
|
|
|
'template_for_dispvms=False')
|
2017-03-20 22:18:10 +01:00
|
|
|
app = appvm.app
|
2016-06-02 17:20:13 +02:00
|
|
|
dispvm = app.add_new_vm(
|
|
|
|
cls,
|
2017-08-06 12:27:35 +02:00
|
|
|
template=appvm,
|
|
|
|
auto_cleanup=True,
|
2016-06-02 17:20:13 +02:00
|
|
|
**kwargs)
|
2017-07-08 02:53:09 +02:00
|
|
|
yield from dispvm.create_on_disk()
|
2016-06-02 17:20:13 +02:00
|
|
|
app.save()
|
|
|
|
return dispvm
|
|
|
|
|
2017-07-08 02:53:09 +02:00
|
|
|
@asyncio.coroutine
|
2016-06-02 17:20:13 +02:00
|
|
|
def cleanup(self):
|
|
|
|
'''Clean up after the DispVM
|
|
|
|
|
|
|
|
This stops the disposable qube and removes it from the store.
|
|
|
|
This method modifies :file:`qubes.xml` file.
|
|
|
|
'''
|
2016-11-04 13:25:09 +01:00
|
|
|
try:
|
2017-07-08 02:53:09 +02:00
|
|
|
# pylint: disable=not-an-iterable
|
|
|
|
yield from self.kill()
|
2016-11-04 13:25:09 +01:00
|
|
|
except qubes.exc.QubesVMNotStartedError:
|
|
|
|
pass
|
2017-08-06 12:27:35 +02:00
|
|
|
# if auto_cleanup is set, this will be done automatically
|
|
|
|
if not self.auto_cleanup:
|
|
|
|
yield from self.remove_from_disk()
|
|
|
|
del self.app.domains[self]
|
|
|
|
self.app.save()
|
2017-09-03 03:04:56 +02:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def start(self, **kwargs):
|
|
|
|
# pylint: disable=arguments-differ
|
|
|
|
|
2017-09-22 22:45:47 +02:00
|
|
|
try:
|
|
|
|
# sanity check, if template_for_dispvm got changed in the meantime
|
|
|
|
if not self.template.template_for_dispvms:
|
|
|
|
raise qubes.exc.QubesException(
|
|
|
|
'template for DispVM ({}) needs to have '
|
|
|
|
'template_for_dispvms=True'.format(self.template.name))
|
2017-09-03 03:04:56 +02:00
|
|
|
|
2017-09-22 22:45:47 +02:00
|
|
|
yield from super(DispVM, self).start(**kwargs)
|
|
|
|
except:
|
|
|
|
# cleanup also on failed startup; there is potential race with
|
|
|
|
# self.on_domain_shutdown_coro, so check if wasn't already removed
|
|
|
|
if self.auto_cleanup and self in self.app.domains:
|
|
|
|
yield from self.remove_from_disk()
|
|
|
|
del self.app.domains[self]
|
|
|
|
self.app.save()
|
|
|
|
raise
|