2014-11-13 14:38:41 +01:00
|
|
|
#!/usr/bin/python2 -O
|
2015-01-19 17:06:30 +01:00
|
|
|
# vim: fileencoding=utf-8
|
|
|
|
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2011-2015 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2014-2015 Wojtek Porczyk <woju@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.
|
|
|
|
#
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2014-11-13 18:10:27 +01:00
|
|
|
'''Qubes Virtual Machines
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
2015-01-19 18:14:15 +01:00
|
|
|
import datetime
|
|
|
|
import os
|
|
|
|
import subprocess
|
2014-11-13 14:38:41 +01:00
|
|
|
import sys
|
2015-01-19 18:14:15 +01:00
|
|
|
import xml.parsers.expat
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
import lxml.etree
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
import qubes
|
2016-03-02 12:17:29 +01:00
|
|
|
import qubes.devices
|
2014-12-09 14:14:24 +01:00
|
|
|
import qubes.events
|
2016-04-28 16:00:29 +02:00
|
|
|
import qubes.log
|
2015-01-23 18:37:40 +01:00
|
|
|
import qubes.tools.qvm_ls
|
2014-11-13 14:38:41 +01:00
|
|
|
|
|
|
|
|
2016-03-03 13:03:27 +01:00
|
|
|
class Features(dict):
|
|
|
|
'''Manager of the features.
|
|
|
|
|
2016-03-09 16:53:36 +01:00
|
|
|
Features can have three distinct values: no value (not present in mapping,
|
|
|
|
which is closest thing to :py:obj:`None`), empty string (which is
|
|
|
|
interpreted as :py:obj:`False`) and non-empty string, which is
|
|
|
|
:py:obj:`True`. Anything assigned to the mapping is coerced to strings,
|
|
|
|
however if you assign instances of :py:class:`bool`, they are converted as
|
|
|
|
described above. Be aware that assigning the number `0` (which is considered
|
|
|
|
false in Python) will result in string `'0'`, which is considered true.
|
|
|
|
|
2016-03-03 13:03:27 +01:00
|
|
|
This class inherits from dict, but has most of the methods that manipulate
|
|
|
|
the item disarmed (they raise NotImplementedError). The ones that are left
|
|
|
|
fire appropriate events on the qube that owns an instance of this class.
|
|
|
|
'''
|
|
|
|
|
|
|
|
#
|
|
|
|
# Those are the methods that affect contents. Either disarm them or make
|
|
|
|
# them report appropriate events. Good approach is to rewrite them carefully
|
|
|
|
# using official documentation, but use only our (overloaded) methods.
|
|
|
|
#
|
|
|
|
def __init__(self, vm, other=None, **kwargs):
|
|
|
|
super(Features, self).__init__()
|
|
|
|
self.vm = vm
|
|
|
|
self.update(other, **kwargs)
|
|
|
|
|
|
|
|
def __delitem__(self, key):
|
|
|
|
super(Features, self).__delitem__(key)
|
|
|
|
self.vm.fire_event('domain-feature-delete', key)
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
2016-04-03 03:43:01 +02:00
|
|
|
if value is None or isinstance(value, bool):
|
2016-03-09 16:53:36 +01:00
|
|
|
value = '1' if value else ''
|
|
|
|
else:
|
|
|
|
value = str(value)
|
2016-03-03 13:03:27 +01:00
|
|
|
self.vm.fire_event('domain-feature-set', key, value)
|
|
|
|
super(Features, self).__setitem__(key, value)
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
for key in self:
|
|
|
|
del self[key]
|
|
|
|
|
|
|
|
def pop(self):
|
|
|
|
'''Not implemented
|
|
|
|
:raises: NotImplementedError
|
|
|
|
'''
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def popitem(self):
|
|
|
|
'''Not implemented
|
|
|
|
:raises: NotImplementedError
|
|
|
|
'''
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def setdefault(self):
|
|
|
|
'''Not implemented
|
|
|
|
:raises: NotImplementedError
|
|
|
|
'''
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def update(self, other=None, **kwargs):
|
|
|
|
if other is not None:
|
|
|
|
if hasattr(other, 'keys'):
|
|
|
|
for key in other:
|
|
|
|
self[key] = other[key]
|
|
|
|
else:
|
|
|
|
for key, value in other:
|
|
|
|
self[key] = value
|
|
|
|
|
|
|
|
for key in kwargs:
|
|
|
|
self[key] = kwargs[key]
|
|
|
|
|
|
|
|
#
|
|
|
|
# end of overriding
|
|
|
|
#
|
|
|
|
|
|
|
|
_NO_DEFAULT = object()
|
2016-06-16 14:43:18 +02:00
|
|
|
|
2016-03-03 13:03:27 +01:00
|
|
|
def check_with_template(self, feature, default=_NO_DEFAULT):
|
2016-06-16 14:43:39 +02:00
|
|
|
''' Check if the vm's template has the specified feature. '''
|
2016-03-03 13:03:27 +01:00
|
|
|
if feature in self:
|
|
|
|
return self[feature]
|
|
|
|
|
2016-08-17 00:47:32 +02:00
|
|
|
if hasattr(self.vm, 'template') and self.vm.template is not None:
|
2016-10-31 02:04:27 +01:00
|
|
|
return self.vm.template.features.check_with_template(feature,
|
|
|
|
default)
|
2016-03-03 13:03:27 +01:00
|
|
|
|
|
|
|
if default is self._NO_DEFAULT:
|
|
|
|
raise KeyError(feature)
|
|
|
|
|
|
|
|
return default
|
|
|
|
|
|
|
|
|
2016-03-04 13:03:43 +01:00
|
|
|
class BaseVMMeta(qubes.events.EmitterMeta):
|
2014-11-13 18:10:27 +01:00
|
|
|
'''Metaclass for :py:class:`.BaseVM`'''
|
2015-01-23 18:37:40 +01:00
|
|
|
def __init__(cls, name, bases, dict_):
|
|
|
|
super(BaseVMMeta, cls).__init__(name, bases, dict_)
|
|
|
|
qubes.tools.qvm_ls.process_class(cls)
|
|
|
|
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
class BaseVM(qubes.PropertyHolder):
|
2014-11-18 17:35:05 +01:00
|
|
|
'''Base class for all VMs
|
|
|
|
|
2014-12-09 14:14:24 +01:00
|
|
|
:param app: Qubes application context
|
|
|
|
:type app: :py:class:`qubes.Qubes`
|
2014-11-18 17:35:05 +01:00
|
|
|
:param xml: xml node from which to deserialise
|
|
|
|
:type xml: :py:class:`lxml.etree._Element` or :py:obj:`None`
|
|
|
|
|
2014-12-18 14:36:09 +01:00
|
|
|
This class is responsible for serializing and deserialising machines and
|
2014-11-18 17:35:05 +01:00
|
|
|
provides basic framework. It contains no management logic. For that, see
|
|
|
|
:py:class:`qubes.vm.qubesvm.QubesVM`.
|
|
|
|
'''
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=no-member
|
2014-11-18 17:35:05 +01:00
|
|
|
|
2014-12-09 14:14:24 +01:00
|
|
|
__metaclass__ = BaseVMMeta
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2016-03-03 13:03:27 +01:00
|
|
|
def __init__(self, app, xml, features=None, devices=None, tags=None,
|
2015-01-21 15:24:29 +01:00
|
|
|
**kwargs):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=redefined-outer-name
|
2015-01-21 15:24:29 +01:00
|
|
|
|
2015-07-01 17:10:10 +02:00
|
|
|
# self.app must be set before super().__init__, because some property
|
|
|
|
# setters need working .app attribute
|
2015-01-19 18:03:23 +01:00
|
|
|
#: mother :py:class:`qubes.Qubes` object
|
2014-12-05 14:58:05 +01:00
|
|
|
self.app = app
|
2015-01-19 18:03:23 +01:00
|
|
|
|
2015-07-01 17:10:10 +02:00
|
|
|
super(BaseVM, self).__init__(xml, **kwargs)
|
|
|
|
|
2016-03-03 13:03:27 +01:00
|
|
|
#: dictionary of features of this qube
|
|
|
|
self.features = Features(self, features)
|
2015-01-19 18:03:23 +01:00
|
|
|
|
2015-01-22 19:25:00 +01:00
|
|
|
#: :py:class:`DeviceManager` object keeping devices that are attached to
|
2015-01-19 18:03:23 +01:00
|
|
|
#: this domain
|
2016-03-02 12:17:29 +01:00
|
|
|
self.devices = devices or qubes.devices.DeviceManager(self)
|
2015-01-19 18:03:23 +01:00
|
|
|
|
|
|
|
#: user-specified tags
|
2015-01-20 16:32:25 +01:00
|
|
|
self.tags = tags or {}
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-21 17:03:17 +01:00
|
|
|
#: logger instance for logging messages related to this VM
|
|
|
|
self.log = None
|
|
|
|
|
2015-09-23 16:25:53 +02:00
|
|
|
if hasattr(self, 'name'):
|
|
|
|
self.init_log()
|
|
|
|
|
2016-06-26 02:18:13 +02:00
|
|
|
def load_extras(self):
|
|
|
|
# features
|
|
|
|
for node in self.xml.xpath('./features/feature'):
|
|
|
|
self.features[node.get('name')] = node.text
|
|
|
|
|
|
|
|
# devices (pci, usb, ...)
|
|
|
|
for parent in self.xml.xpath('./devices'):
|
|
|
|
devclass = parent.get('class')
|
|
|
|
for node in parent.xpath('./device'):
|
2016-06-26 04:07:15 +02:00
|
|
|
device = self.devices[devclass].devclass(
|
|
|
|
self.app.domains[node.get('backend-domain')],
|
|
|
|
node.get('id')
|
|
|
|
)
|
|
|
|
self.devices[devclass].attach(device)
|
2016-06-26 02:18:13 +02:00
|
|
|
|
|
|
|
# tags
|
|
|
|
for node in self.xml.xpath('./tags/tag'):
|
|
|
|
self.tags[node.get('name')] = node.text
|
|
|
|
|
|
|
|
# SEE:1815 firewall, policy.
|
|
|
|
|
2015-01-21 17:03:17 +01:00
|
|
|
def init_log(self):
|
|
|
|
'''Initialise logger for this domain.'''
|
|
|
|
self.log = qubes.log.get_vm_logger(self.name)
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
def __xml__(self):
|
2015-01-12 18:57:37 +01:00
|
|
|
element = lxml.etree.Element('domain')
|
|
|
|
element.set('id', 'domain-' + str(self.qid))
|
|
|
|
element.set('class', self.__class__.__name__)
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-13 15:56:10 +01:00
|
|
|
element.append(self.xml_properties())
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2016-03-03 13:03:27 +01:00
|
|
|
features = lxml.etree.Element('features')
|
|
|
|
for feature in self.features:
|
2016-04-03 03:43:01 +02:00
|
|
|
node = lxml.etree.Element('feature', name=feature)
|
2016-03-09 16:53:36 +01:00
|
|
|
node.text = self.features[feature]
|
2016-03-03 13:03:27 +01:00
|
|
|
features.append(node)
|
|
|
|
element.append(features)
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
for devclass in self.devices:
|
|
|
|
devices = lxml.etree.Element('devices')
|
|
|
|
devices.set('class', devclass)
|
2016-06-26 04:07:15 +02:00
|
|
|
for device in self.devices[devclass].attached(persistent=True):
|
2014-12-05 14:58:05 +01:00
|
|
|
node = lxml.etree.Element('device')
|
2016-06-26 04:07:15 +02:00
|
|
|
node.set('backend-domain', device.backend_domain.name)
|
|
|
|
node.set('id', device.ident)
|
2014-12-05 14:58:05 +01:00
|
|
|
devices.append(node)
|
|
|
|
element.append(devices)
|
|
|
|
|
|
|
|
tags = lxml.etree.Element('tags')
|
|
|
|
for tag in self.tags:
|
|
|
|
node = lxml.etree.Element('tag', name=tag)
|
|
|
|
node.text = self.tags[tag]
|
|
|
|
tags.append(node)
|
|
|
|
element.append(tags)
|
|
|
|
|
|
|
|
return element
|
2014-11-13 14:38:41 +01:00
|
|
|
|
|
|
|
def __repr__(self):
|
2014-12-17 13:32:58 +01:00
|
|
|
proprepr = []
|
2015-01-21 12:50:00 +01:00
|
|
|
for prop in self.property_list():
|
2014-12-17 13:32:58 +01:00
|
|
|
try:
|
2016-03-07 01:00:15 +01:00
|
|
|
proprepr.append('{}={!s}'.format(
|
2014-12-17 13:32:58 +01:00
|
|
|
prop.__name__, getattr(self, prop.__name__)))
|
|
|
|
except AttributeError:
|
|
|
|
continue
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2014-12-17 13:32:58 +01:00
|
|
|
return '<{} object at {:#x} {}>'.format(
|
|
|
|
self.__class__.__name__, id(self), ' '.join(proprepr))
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
#
|
|
|
|
# xml serialising methods
|
|
|
|
#
|
|
|
|
|
2016-06-16 14:44:56 +02:00
|
|
|
def create_config_file(self, prepare_dvm=False):
|
2014-12-29 12:46:16 +01:00
|
|
|
'''Create libvirt's XML domain config file
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
:param bool prepare_dvm: If we are in the process of preparing \
|
|
|
|
DisposableVM
|
2014-12-29 12:46:16 +01:00
|
|
|
'''
|
2016-10-04 11:30:29 +02:00
|
|
|
domain_config = self.app.env.select_template([
|
|
|
|
'libvirt/xen/by-name/{}.xml'.format(self.name),
|
|
|
|
'libvirt/xen-user.xml',
|
|
|
|
'libvirt/xen-dist.xml',
|
|
|
|
'libvirt/xen.xml',
|
|
|
|
]).render(vm=self, prepare_dvm=prepare_dvm)
|
2014-12-29 12:46:16 +01:00
|
|
|
return domain_config
|
|
|
|
|
2016-04-28 16:00:29 +02:00
|
|
|
|
|
|
|
class VMProperty(qubes.property):
|
|
|
|
'''Property that is referring to a VM
|
|
|
|
|
|
|
|
:param type vmclass: class that returned VM is supposed to be instance of
|
|
|
|
|
|
|
|
and all supported by :py:class:`property` with the exception of ``type`` \
|
|
|
|
and ``setter``
|
|
|
|
'''
|
|
|
|
|
|
|
|
_none_value = ''
|
|
|
|
|
|
|
|
def __init__(self, name, vmclass=BaseVM, allow_none=False,
|
|
|
|
**kwargs):
|
|
|
|
if 'type' in kwargs:
|
|
|
|
raise TypeError(
|
|
|
|
"'type' keyword parameter is unsupported in {}".format(
|
|
|
|
self.__class__.__name__))
|
|
|
|
if 'setter' in kwargs:
|
|
|
|
raise TypeError(
|
|
|
|
"'setter' keyword parameter is unsupported in {}".format(
|
|
|
|
self.__class__.__name__))
|
|
|
|
if not issubclass(vmclass, BaseVM):
|
|
|
|
raise TypeError(
|
|
|
|
"'vmclass' should specify a subclass of qubes.vm.BaseVM")
|
|
|
|
|
|
|
|
super(VMProperty, self).__init__(name,
|
|
|
|
saver=(lambda self_, prop, value:
|
|
|
|
self._none_value if value is None else value.name),
|
|
|
|
**kwargs)
|
|
|
|
self.vmclass = vmclass
|
|
|
|
self.allow_none = allow_none
|
|
|
|
|
|
|
|
def __set__(self, instance, value):
|
|
|
|
if value is self.__class__.DEFAULT:
|
|
|
|
self.__delete__(instance)
|
|
|
|
return
|
|
|
|
|
|
|
|
if value == self._none_value:
|
|
|
|
value = None
|
|
|
|
if value is None:
|
|
|
|
if self.allow_none:
|
|
|
|
super(VMProperty, self).__set__(instance, value)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
raise ValueError(
|
|
|
|
'Property {!r} does not allow setting to {!r}'.format(
|
|
|
|
self.__name__, value))
|
|
|
|
|
|
|
|
app = instance if isinstance(instance, qubes.Qubes) else instance.app
|
|
|
|
|
|
|
|
try:
|
|
|
|
vm = app.domains[value]
|
|
|
|
except KeyError:
|
|
|
|
raise qubes.exc.QubesVMNotFoundError(value)
|
|
|
|
|
|
|
|
if not isinstance(vm, self.vmclass):
|
2016-08-17 02:13:59 +02:00
|
|
|
raise TypeError('wrong VM class: domains[{!r}] is of type {!s} '
|
2016-04-28 16:00:29 +02:00
|
|
|
'and not {!s}'.format(value,
|
|
|
|
vm.__class__.__name__,
|
|
|
|
self.vmclass.__name__))
|
|
|
|
|
|
|
|
super(VMProperty, self).__set__(instance, vm)
|