2014-11-13 14:38:41 +01:00
|
|
|
#!/usr/bin/python2 -O
|
2015-01-19 18:03:23 +01:00
|
|
|
# vim: fileencoding=utf-8
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-19 18:03:23 +01:00
|
|
|
#
|
|
|
|
# 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 OS
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-19 18:03:23 +01:00
|
|
|
:copyright: © 2010-2015 Invisible Things Lab
|
2014-11-13 18:10:27 +01:00
|
|
|
'''
|
|
|
|
|
2015-01-19 18:03:23 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2014-11-13 18:10:27 +01:00
|
|
|
__author__ = 'Invisible Things Lab'
|
|
|
|
__license__ = 'GPLv2 or later'
|
|
|
|
__version__ = 'R3'
|
|
|
|
|
2014-11-21 16:51:59 +01:00
|
|
|
import ast
|
2014-12-05 14:58:05 +01:00
|
|
|
import atexit
|
|
|
|
import collections
|
2015-01-15 12:57:44 +01:00
|
|
|
import errno
|
2014-12-05 14:58:05 +01:00
|
|
|
import grp
|
2015-01-15 12:57:44 +01:00
|
|
|
import logging
|
2014-12-05 14:58:05 +01:00
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import time
|
|
|
|
import warnings
|
|
|
|
|
|
|
|
import __builtin__
|
|
|
|
|
2015-01-12 16:56:14 +01:00
|
|
|
import docutils.core
|
|
|
|
import docutils.io
|
2014-12-05 14:58:05 +01:00
|
|
|
import lxml.etree
|
|
|
|
|
2015-06-23 22:27:20 +02:00
|
|
|
|
|
|
|
import qubes.config
|
2015-10-14 22:02:11 +02:00
|
|
|
import qubes.events
|
|
|
|
import qubes.exc
|
2014-12-09 14:14:24 +01:00
|
|
|
import qubes.ext
|
|
|
|
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
if os.name == 'posix':
|
|
|
|
import fcntl
|
|
|
|
elif os.name == 'nt':
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=import-error
|
2014-12-05 14:58:05 +01:00
|
|
|
import win32con
|
|
|
|
import win32file
|
|
|
|
import pywintypes
|
|
|
|
else:
|
2015-01-07 14:22:12 +01:00
|
|
|
raise RuntimeError("Qubes works only on POSIX or WinNT systems")
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
import libvirt
|
|
|
|
try:
|
|
|
|
import xen.lowlevel.xs
|
2015-01-08 19:13:51 +01:00
|
|
|
import xen.lowlevel.xc
|
2014-12-05 14:58:05 +01:00
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
class VMMConnection(object):
|
2014-11-14 15:41:27 +01:00
|
|
|
'''Connection to Virtual Machine Manager (libvirt)'''
|
2015-01-07 14:22:12 +01:00
|
|
|
|
2014-11-14 15:41:27 +01:00
|
|
|
def __init__(self):
|
|
|
|
self._libvirt_conn = None
|
|
|
|
self._xs = None
|
|
|
|
self._xc = None
|
|
|
|
self._offline_mode = False
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
@__builtin__.property
|
2014-11-14 15:41:27 +01:00
|
|
|
def offline_mode(self):
|
|
|
|
'''Check or enable offline mode (do not actually connect to vmm)'''
|
|
|
|
return self._offline_mode
|
|
|
|
|
|
|
|
@offline_mode.setter
|
|
|
|
def offline_mode(self, value):
|
2014-11-17 13:46:53 +01:00
|
|
|
if value and self._libvirt_conn is not None:
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesException(
|
|
|
|
'Cannot change offline mode while already connected')
|
2014-11-14 15:41:27 +01:00
|
|
|
|
|
|
|
self._offline_mode = value
|
|
|
|
|
|
|
|
def _libvirt_error_handler(self, ctx, error):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def init_vmm_connection(self):
|
|
|
|
'''Initialise connection
|
|
|
|
|
|
|
|
This method is automatically called when getting'''
|
|
|
|
if self._libvirt_conn is not None:
|
|
|
|
# Already initialized
|
|
|
|
return
|
|
|
|
if self._offline_mode:
|
|
|
|
# Do not initialize in offline mode
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesException(
|
|
|
|
'VMM operations disabled in offline mode')
|
2014-11-14 15:41:27 +01:00
|
|
|
|
|
|
|
if 'xen.lowlevel.xs' in sys.modules:
|
|
|
|
self._xs = xen.lowlevel.xs.xs()
|
2015-01-08 19:13:51 +01:00
|
|
|
if 'xen.lowlevel.cs' in sys.modules:
|
|
|
|
self._xc = xen.lowlevel.xc.xc()
|
2015-01-15 12:57:44 +01:00
|
|
|
self._libvirt_conn = libvirt.open(qubes.config.defaults['libvirt_uri'])
|
2014-12-18 14:54:46 +01:00
|
|
|
if self._libvirt_conn is None:
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesException('Failed connect to libvirt driver')
|
2014-11-14 15:41:27 +01:00
|
|
|
libvirt.registerErrorHandler(self._libvirt_error_handler, None)
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
@__builtin__.property
|
2014-11-14 15:41:27 +01:00
|
|
|
def libvirt_conn(self):
|
|
|
|
'''Connection to libvirt'''
|
2014-11-17 13:46:53 +01:00
|
|
|
self.init_vmm_connection()
|
|
|
|
return self._libvirt_conn
|
2014-11-14 15:41:27 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
@__builtin__.property
|
2014-11-14 15:41:27 +01:00
|
|
|
def xs(self):
|
|
|
|
'''Connection to Xen Store
|
|
|
|
|
2015-01-08 19:13:51 +01:00
|
|
|
This property in available only when running on Xen.
|
|
|
|
'''
|
2014-11-14 15:41:27 +01:00
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
# XXX what about the case when we run under KVM,
|
|
|
|
# but xen modules are importable?
|
2014-11-17 13:46:53 +01:00
|
|
|
if 'xen.lowlevel.xs' not in sys.modules:
|
2015-01-19 17:06:30 +01:00
|
|
|
raise AttributeError(
|
|
|
|
'xs object is available under Xen hypervisor only')
|
2015-01-08 19:13:51 +01:00
|
|
|
|
|
|
|
self.init_vmm_connection()
|
|
|
|
return self._xs
|
|
|
|
|
|
|
|
@__builtin__.property
|
|
|
|
def xc(self):
|
|
|
|
'''Connection to Xen
|
|
|
|
|
|
|
|
This property in available only when running on Xen.
|
|
|
|
'''
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
# XXX what about the case when we run under KVM,
|
|
|
|
# but xen modules are importable?
|
2015-01-08 19:13:51 +01:00
|
|
|
if 'xen.lowlevel.xc' not in sys.modules:
|
2015-01-19 17:06:30 +01:00
|
|
|
raise AttributeError(
|
|
|
|
'xc object is available under Xen hypervisor only')
|
2014-11-14 15:41:27 +01:00
|
|
|
|
2014-11-17 13:46:53 +01:00
|
|
|
self.init_vmm_connection()
|
|
|
|
return self._xs
|
|
|
|
|
2016-02-10 16:44:49 +01:00
|
|
|
def __del__(self):
|
|
|
|
if self._libvirt_conn:
|
|
|
|
self._libvirt_conn.close()
|
|
|
|
|
2014-11-17 17:07:08 +01:00
|
|
|
|
|
|
|
class QubesHost(object):
|
2014-12-29 12:46:16 +01:00
|
|
|
'''Basic information about host machine
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
:param qubes.Qubes app: Qubes application context (must have \
|
|
|
|
:py:attr:`Qubes.vmm` attribute defined)
|
2014-12-29 12:46:16 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self, app):
|
2015-01-15 12:57:44 +01:00
|
|
|
self.app = app
|
2015-01-08 19:13:51 +01:00
|
|
|
self._no_cpus = None
|
2015-01-22 11:24:23 +01:00
|
|
|
self._total_mem = None
|
|
|
|
self._physinfo = None
|
2015-01-08 19:13:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _fetch(self):
|
|
|
|
if self._no_cpus is not None:
|
|
|
|
return
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=unused-variable
|
2014-12-29 12:46:16 +01:00
|
|
|
(model, memory, cpus, mhz, nodes, socket, cores, threads) = \
|
2015-01-15 12:57:44 +01:00
|
|
|
self.app.vmm.libvirt_conn.getInfo()
|
2015-01-07 14:22:12 +01:00
|
|
|
self._total_mem = long(memory) * 1024
|
2014-11-17 17:07:08 +01:00
|
|
|
self._no_cpus = cpus
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
self.app.log.debug('QubesHost: no_cpus={} memory_total={}'.format(
|
|
|
|
self.no_cpus, self.memory_total))
|
2015-01-08 19:13:51 +01:00
|
|
|
try:
|
2015-01-19 17:06:30 +01:00
|
|
|
self.app.log.debug('QubesHost: xen_free_memory={}'.format(
|
|
|
|
self.get_free_xen_memory()))
|
2015-01-08 19:13:51 +01:00
|
|
|
except NotImplementedError:
|
|
|
|
pass
|
|
|
|
|
2014-11-17 17:07:08 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
@__builtin__.property
|
2014-11-17 17:07:08 +01:00
|
|
|
def memory_total(self):
|
|
|
|
'''Total memory, in bytes'''
|
2015-01-08 19:13:51 +01:00
|
|
|
|
|
|
|
self._fetch()
|
2014-11-17 17:07:08 +01:00
|
|
|
return self._total_mem
|
|
|
|
|
2015-01-08 19:13:51 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
@__builtin__.property
|
2014-11-17 17:07:08 +01:00
|
|
|
def no_cpus(self):
|
2015-01-08 19:13:51 +01:00
|
|
|
'''Number of CPUs'''
|
|
|
|
|
|
|
|
self._fetch()
|
2014-11-17 17:07:08 +01:00
|
|
|
return self._no_cpus
|
|
|
|
|
2015-01-08 19:13:51 +01:00
|
|
|
|
2014-11-17 17:07:08 +01:00
|
|
|
def get_free_xen_memory(self):
|
2015-01-08 19:13:51 +01:00
|
|
|
'''Get free memory from Xen's physinfo.
|
2014-11-17 17:07:08 +01:00
|
|
|
|
2015-01-08 19:13:51 +01:00
|
|
|
:raises NotImplementedError: when not under Xen
|
|
|
|
'''
|
|
|
|
try:
|
|
|
|
self._physinfo = self.app.xc.physinfo()
|
|
|
|
except AttributeError:
|
|
|
|
raise NotImplementedError('This function requires Xen hypervisor')
|
|
|
|
return long(self._physinfo['free_memory'])
|
|
|
|
|
|
|
|
|
|
|
|
def measure_cpu_usage(self, previous_time=None, previous=None,
|
2014-11-17 17:07:08 +01:00
|
|
|
wait_time=1):
|
2015-01-08 19:13:51 +01:00
|
|
|
'''Measure cpu usage for all domains at once.
|
|
|
|
|
|
|
|
This function requires Xen hypervisor.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.0
|
|
|
|
argument order to match return tuple
|
|
|
|
|
|
|
|
:raises NotImplementedError: when not under Xen
|
|
|
|
'''
|
|
|
|
|
2014-11-17 17:07:08 +01:00
|
|
|
if previous is None:
|
|
|
|
previous_time = time.time()
|
|
|
|
previous = {}
|
2015-01-08 19:13:51 +01:00
|
|
|
try:
|
2015-01-20 14:09:47 +01:00
|
|
|
info = self.app.vmm.xc.domain_getinfo(0, qubes.config.max_qid)
|
2015-01-08 19:13:51 +01:00
|
|
|
except AttributeError:
|
|
|
|
raise NotImplementedError(
|
|
|
|
'This function requires Xen hypervisor')
|
|
|
|
|
2014-11-17 17:07:08 +01:00
|
|
|
for vm in info:
|
|
|
|
previous[vm['domid']] = {}
|
|
|
|
previous[vm['domid']]['cpu_time'] = (
|
2015-01-07 14:22:12 +01:00
|
|
|
vm['cpu_time'] / vm['online_vcpus'])
|
2014-11-17 17:07:08 +01:00
|
|
|
previous[vm['domid']]['cpu_usage'] = 0
|
|
|
|
time.sleep(wait_time)
|
|
|
|
|
|
|
|
current_time = time.time()
|
|
|
|
current = {}
|
2015-01-08 19:13:51 +01:00
|
|
|
try:
|
2015-01-22 11:24:23 +01:00
|
|
|
info = self.app.vmm.xc.domain_getinfo(0, qubes.config.max_qid)
|
2015-01-08 19:13:51 +01:00
|
|
|
except AttributeError:
|
|
|
|
raise NotImplementedError(
|
|
|
|
'This function requires Xen hypervisor')
|
2014-11-17 17:07:08 +01:00
|
|
|
for vm in info:
|
|
|
|
current[vm['domid']] = {}
|
|
|
|
current[vm['domid']]['cpu_time'] = (
|
2015-01-07 14:22:12 +01:00
|
|
|
vm['cpu_time'] / max(vm['online_vcpus'], 1))
|
2014-11-17 17:07:08 +01:00
|
|
|
if vm['domid'] in previous.keys():
|
|
|
|
current[vm['domid']]['cpu_usage'] = (
|
|
|
|
float(current[vm['domid']]['cpu_time'] -
|
|
|
|
previous[vm['domid']]['cpu_time']) /
|
2015-01-07 14:22:12 +01:00
|
|
|
long(1000 ** 3) / (current_time - previous_time) * 100)
|
2014-11-17 17:07:08 +01:00
|
|
|
if current[vm['domid']]['cpu_usage'] < 0:
|
|
|
|
# VM has been rebooted
|
|
|
|
current[vm['domid']]['cpu_usage'] = 0
|
|
|
|
else:
|
|
|
|
current[vm['domid']]['cpu_usage'] = 0
|
|
|
|
|
|
|
|
return (current_time, current)
|
2014-11-17 19:09:25 +01:00
|
|
|
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
class Label(object):
|
2014-11-17 19:09:25 +01:00
|
|
|
'''Label definition for virtual machines
|
|
|
|
|
|
|
|
Label specifies colour of the padlock displayed next to VM's name.
|
|
|
|
When this is a :py:class:`qubes.vm.dispvm.DispVM`, padlock is overlayed
|
|
|
|
with recycling pictogram.
|
|
|
|
|
|
|
|
:param int index: numeric identificator of label
|
|
|
|
:param str color: colour specification as in HTML (``#abcdef``)
|
|
|
|
:param str name: label's name like "red" or "green"
|
|
|
|
'''
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
def __init__(self, index, color, name):
|
2014-11-17 19:09:25 +01:00
|
|
|
#: numeric identificator of label
|
|
|
|
self.index = index
|
|
|
|
|
|
|
|
#: colour specification as in HTML (``#abcdef``)
|
|
|
|
self.color = color
|
|
|
|
|
|
|
|
#: label's name like "red" or "green"
|
|
|
|
self.name = name
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
#: freedesktop icon name, suitable for use in
|
|
|
|
#: :py:meth:`PyQt4.QtGui.QIcon.fromTheme`
|
2014-12-05 14:58:05 +01:00
|
|
|
self.icon = 'appvm-' + name
|
2014-11-17 19:09:25 +01:00
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
#: freedesktop icon name, suitable for use in
|
|
|
|
#: :py:meth:`PyQt4.QtGui.QIcon.fromTheme` on DispVMs
|
2014-12-05 14:58:05 +01:00
|
|
|
self.icon_dispvm = 'dispvm-' + name
|
|
|
|
|
2014-11-17 19:09:25 +01:00
|
|
|
|
2014-11-21 16:51:59 +01:00
|
|
|
@classmethod
|
|
|
|
def fromxml(cls, xml):
|
|
|
|
'''Create label definition from XML node
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
:param lxml.etree._Element xml: XML node reference
|
|
|
|
:rtype: :py:class:`qubes.Label`
|
2014-11-21 16:51:59 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
index = int(xml.get('id').split('-', 1)[1])
|
|
|
|
color = xml.get('color')
|
|
|
|
name = xml.text
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
return cls(index, color, name)
|
|
|
|
|
|
|
|
|
|
|
|
def __xml__(self):
|
2015-01-07 14:22:12 +01:00
|
|
|
element = lxml.etree.Element(
|
2015-06-23 22:46:56 +02:00
|
|
|
'label', id='label-{}'.format(self.index), color=self.color)
|
2014-12-05 14:58:05 +01:00
|
|
|
element.text = self.name
|
|
|
|
return element
|
|
|
|
|
2014-11-21 16:51:59 +01:00
|
|
|
|
2014-11-17 19:09:25 +01:00
|
|
|
def __repr__(self):
|
2015-05-31 11:10:12 +02:00
|
|
|
return '{}({!r}, {!r}, {!r})'.format(
|
2014-11-17 19:09:25 +01:00
|
|
|
self.__class__.__name__,
|
|
|
|
self.index,
|
|
|
|
self.color,
|
2014-12-05 14:58:05 +01:00
|
|
|
self.name)
|
2014-11-17 19:09:25 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
@__builtin__.property
|
2014-11-17 19:09:25 +01:00
|
|
|
def icon_path(self):
|
|
|
|
'''Icon path
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
.. deprecated:: 2.0
|
|
|
|
use :py:meth:`PyQt4.QtGui.QIcon.fromTheme` and :py:attr:`icon`
|
|
|
|
'''
|
2015-01-20 14:09:47 +01:00
|
|
|
return os.path.join(qubes.config.system_path['qubes_icon_dir'],
|
|
|
|
self.icon) + ".png"
|
2014-11-17 19:09:25 +01:00
|
|
|
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
@__builtin__.property
|
|
|
|
def icon_path_dispvm(self):
|
|
|
|
'''Icon path
|
|
|
|
|
|
|
|
.. deprecated:: 2.0
|
|
|
|
use :py:meth:`PyQt4.QtGui.QIcon.fromTheme` and :py:attr:`icon_dispvm`
|
|
|
|
'''
|
2015-01-20 14:09:47 +01:00
|
|
|
return os.path.join(qubes.config.system_path['qubes_icon_dir'],
|
|
|
|
self.icon_dispvm) + ".png"
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
class VMCollection(object):
|
|
|
|
'''A collection of Qubes VMs
|
|
|
|
|
|
|
|
VMCollection supports ``in`` operator. You may test for ``qid``, ``name``
|
|
|
|
and whole VM object's presence.
|
|
|
|
|
|
|
|
Iterating over VMCollection will yield machine objects.
|
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self, app):
|
|
|
|
self.app = app
|
|
|
|
self._dict = dict()
|
|
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
2015-01-07 14:22:12 +01:00
|
|
|
return '<{} {!r}>'.format(
|
|
|
|
self.__class__.__name__, list(sorted(self.keys())))
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
'''Iterate over ``(qid, vm)`` pairs'''
|
|
|
|
for qid in self.qids():
|
|
|
|
yield (qid, self[qid])
|
|
|
|
|
|
|
|
|
|
|
|
def qids(self):
|
|
|
|
'''Iterate over all qids
|
|
|
|
|
|
|
|
qids are sorted by numerical order.
|
|
|
|
'''
|
|
|
|
|
|
|
|
return iter(sorted(self._dict.keys()))
|
|
|
|
|
|
|
|
keys = qids
|
|
|
|
|
|
|
|
|
|
|
|
def names(self):
|
|
|
|
'''Iterate over all names
|
|
|
|
|
|
|
|
names are sorted by lexical order.
|
|
|
|
'''
|
|
|
|
|
|
|
|
return iter(sorted(vm.name for vm in self._dict.values()))
|
|
|
|
|
|
|
|
|
|
|
|
def vms(self):
|
|
|
|
'''Iterate over all machines
|
|
|
|
|
|
|
|
vms are sorted by qid.
|
|
|
|
'''
|
|
|
|
|
|
|
|
return iter(sorted(self._dict.values()))
|
|
|
|
|
|
|
|
__iter__ = vms
|
|
|
|
values = vms
|
|
|
|
|
|
|
|
|
|
|
|
def add(self, value):
|
|
|
|
'''Add VM to collection
|
|
|
|
|
|
|
|
:param qubes.vm.BaseVM value: VM to add
|
|
|
|
:raises TypeError: when value is of wrong type
|
|
|
|
:raises ValueError: when there is already VM which has equal ``qid``
|
|
|
|
'''
|
|
|
|
|
2015-01-08 19:35:59 +01:00
|
|
|
# this violates duck typing, but is needed
|
|
|
|
# for VMProperty to function correctly
|
2014-12-05 14:58:05 +01:00
|
|
|
if not isinstance(value, qubes.vm.BaseVM):
|
2015-01-19 17:06:30 +01:00
|
|
|
raise TypeError('{} holds only BaseVM instances'.format(
|
|
|
|
self.__class__.__name__))
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
if value.qid in self:
|
2015-01-19 17:06:30 +01:00
|
|
|
raise ValueError('This collection already holds VM that has '
|
|
|
|
'qid={!r} ({!r})'.format(value.qid, self[value.qid]))
|
2014-12-05 14:58:05 +01:00
|
|
|
if value.name in self:
|
2015-01-19 17:06:30 +01:00
|
|
|
raise ValueError('This collection already holds VM that has '
|
|
|
|
'name={!r} ({!r})'.format(value.name, self[value.name]))
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
self._dict[value.qid] = value
|
2015-06-24 15:02:49 +02:00
|
|
|
value.events_enabled = True
|
2014-12-09 18:34:00 +01:00
|
|
|
self.app.fire_event('domain-added', value)
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-06-26 11:08:26 +02:00
|
|
|
return value
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
if isinstance(key, int):
|
|
|
|
return self._dict[key]
|
|
|
|
|
|
|
|
if isinstance(key, basestring):
|
|
|
|
for vm in self:
|
2015-01-19 17:06:30 +01:00
|
|
|
if vm.name == key:
|
2014-12-05 14:58:05 +01:00
|
|
|
return vm
|
|
|
|
raise KeyError(key)
|
|
|
|
|
|
|
|
if isinstance(key, qubes.vm.BaseVM):
|
|
|
|
if key in self:
|
|
|
|
return key
|
|
|
|
raise KeyError(key)
|
|
|
|
|
|
|
|
raise KeyError(key)
|
|
|
|
|
|
|
|
|
|
|
|
def __delitem__(self, key):
|
2014-12-09 18:34:00 +01:00
|
|
|
vm = self[key]
|
|
|
|
del self._dict[vm.qid]
|
|
|
|
self.app.fire_event('domain-deleted', vm)
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
def __contains__(self, key):
|
2015-01-07 14:22:12 +01:00
|
|
|
return any((key == vm or key == vm.qid or key == vm.name)
|
|
|
|
for vm in self)
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self._dict)
|
|
|
|
|
|
|
|
|
|
|
|
def get_vms_based_on(self, template):
|
|
|
|
template = self[template]
|
|
|
|
return set(vm for vm in self if vm.template == template)
|
|
|
|
|
|
|
|
|
|
|
|
def get_vms_connected_to(self, netvm):
|
2015-01-20 16:32:25 +01:00
|
|
|
new_vms = set([self[netvm]])
|
2015-01-20 14:41:19 +01:00
|
|
|
dependent_vms = set()
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
# Dependency resolving only makes sense on NetVM (or derivative)
|
|
|
|
# if not self[netvm_qid].is_netvm():
|
|
|
|
# return set([])
|
|
|
|
|
|
|
|
while len(new_vms) > 0:
|
|
|
|
cur_vm = new_vms.pop()
|
|
|
|
for vm in cur_vm.connected_vms.values():
|
2015-01-20 14:41:19 +01:00
|
|
|
if vm in dependent_vms:
|
2014-12-05 14:58:05 +01:00
|
|
|
continue
|
2015-01-20 14:41:19 +01:00
|
|
|
dependent_vms.add(vm.qid)
|
2014-12-05 14:58:05 +01:00
|
|
|
# if vm.is_netvm():
|
2015-01-22 11:24:23 +01:00
|
|
|
new_vms.add(vm.qid)
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
return dependent_vms
|
|
|
|
|
|
|
|
|
|
|
|
# XXX with Qubes Admin Api this will probably lead to race condition
|
|
|
|
# whole process of creating and adding should be synchronised
|
|
|
|
def get_new_unused_qid(self):
|
|
|
|
used_ids = set(self.qids())
|
2015-01-20 14:09:47 +01:00
|
|
|
for i in range(1, qubes.config.max_qid):
|
2014-12-05 14:58:05 +01:00
|
|
|
if i not in used_ids:
|
|
|
|
return i
|
|
|
|
raise LookupError("Cannot find unused qid!")
|
|
|
|
|
|
|
|
|
|
|
|
def get_new_unused_netid(self):
|
2015-01-07 14:22:12 +01:00
|
|
|
used_ids = set([vm.netid for vm in self]) # if vm.is_netvm()])
|
2015-01-20 14:09:47 +01:00
|
|
|
for i in range(1, qubes.config.max_netid):
|
2014-12-05 14:58:05 +01:00
|
|
|
if i not in used_ids:
|
|
|
|
return i
|
|
|
|
raise LookupError("Cannot find unused netid!")
|
|
|
|
|
|
|
|
|
2015-01-19 19:02:28 +01:00
|
|
|
class property(object): # pylint: disable=redefined-builtin,invalid-name
|
2014-12-05 14:58:05 +01:00
|
|
|
'''Qubes property.
|
|
|
|
|
|
|
|
This class holds one property that can be saved to and loaded from
|
|
|
|
:file:`qubes.xml`. It is used for both global and per-VM properties.
|
|
|
|
|
2015-01-08 17:42:34 +01:00
|
|
|
Property can be unset by ordinary ``del`` statement or assigning
|
|
|
|
:py:attr:`DEFAULT` special value to it. After deletion (or before first
|
|
|
|
assignment/load) attempting to read a property will get its default value
|
|
|
|
or, when no default, py:class:`exceptions.AttributeError`.
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
:param str name: name of the property
|
2015-01-19 17:06:30 +01:00
|
|
|
:param collections.Callable setter: if not :py:obj:`None`, this is used to \
|
|
|
|
initialise value; first parameter to the function is holder instance \
|
|
|
|
and the second is value; this is called before ``type``
|
|
|
|
:param collections.Callable saver: function to coerce value to something \
|
|
|
|
readable by setter
|
2014-12-05 14:58:05 +01:00
|
|
|
:param type type: if not :py:obj:`None`, value is coerced to this type
|
2015-01-19 17:06:30 +01:00
|
|
|
:param object default: default value; if callable, will be called with \
|
|
|
|
holder as first argument
|
|
|
|
:param int load_stage: stage when property should be loaded (see \
|
|
|
|
:py:class:`Qubes` for description of stages)
|
2014-12-05 14:58:05 +01:00
|
|
|
:param int order: order of evaluation (bigger order values are later)
|
2015-01-23 18:37:40 +01:00
|
|
|
:param str ls_head: column head for :program:`qvm-ls`
|
|
|
|
:param int ls_width: column width in :program:`qvm-ls`
|
2015-01-19 17:06:30 +01:00
|
|
|
:param str doc: docstring; this should be one paragraph of plain RST, no \
|
|
|
|
sphinx-specific features
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
Setters and savers have following signatures:
|
|
|
|
|
|
|
|
.. :py:function:: setter(self, prop, value)
|
|
|
|
:noindex:
|
|
|
|
|
|
|
|
:param self: instance of object that is holding property
|
|
|
|
:param prop: property object
|
|
|
|
:param value: value being assigned
|
|
|
|
|
|
|
|
.. :py:function:: saver(self, prop, value)
|
|
|
|
:noindex:
|
|
|
|
|
|
|
|
:param self: instance of object that is holding property
|
|
|
|
:param prop: property object
|
|
|
|
:param value: value being saved
|
|
|
|
:rtype: str
|
|
|
|
:raises property.DontSave: when property should not be saved at all
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
'''
|
|
|
|
|
2015-01-08 17:42:34 +01:00
|
|
|
#: Assigning this value to property means setting it to its default value.
|
|
|
|
#: If property has no default value, this will unset it.
|
|
|
|
DEFAULT = object()
|
|
|
|
|
2015-01-08 19:35:59 +01:00
|
|
|
# internal use only
|
|
|
|
_NO_DEFAULT = object()
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
def __init__(self, name, setter=None, saver=None, type=None,
|
2015-09-25 21:36:35 +02:00
|
|
|
default=_NO_DEFAULT, write_once=False, load_stage=2, order=0,
|
|
|
|
save_via_ref=False,
|
2015-01-23 18:37:40 +01:00
|
|
|
ls_head=None, ls_width=None, doc=None):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=redefined-builtin
|
2014-12-05 14:58:05 +01:00
|
|
|
self.__name__ = name
|
|
|
|
self._setter = setter
|
2015-01-07 14:22:12 +01:00
|
|
|
self._saver = saver if saver is not None else (
|
|
|
|
lambda self, prop, value: str(value))
|
2014-12-05 14:58:05 +01:00
|
|
|
self._type = type
|
|
|
|
self._default = default
|
2015-09-25 21:36:35 +02:00
|
|
|
self._write_once = write_once
|
2014-12-05 14:58:05 +01:00
|
|
|
self.order = order
|
|
|
|
self.load_stage = load_stage
|
|
|
|
self.save_via_ref = save_via_ref
|
|
|
|
self.__doc__ = doc
|
|
|
|
self._attr_name = '_qubesprop_' + name
|
|
|
|
|
2015-01-23 18:37:40 +01:00
|
|
|
if ls_head is not None or ls_width is not None:
|
|
|
|
self.ls_head = ls_head or self.__name__.replace('_', '-').upper()
|
|
|
|
self.ls_width = max(ls_width or 0, len(self.ls_head) + 1)
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
def __get__(self, instance, owner):
|
|
|
|
if instance is None:
|
|
|
|
return self
|
|
|
|
|
|
|
|
# XXX this violates duck typing, shall we keep it?
|
|
|
|
if not isinstance(instance, PropertyHolder):
|
2015-01-19 17:06:30 +01:00
|
|
|
raise AttributeError('qubes.property should be used on '
|
|
|
|
'qubes.PropertyHolder instances only')
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
return getattr(instance, self._attr_name)
|
|
|
|
|
|
|
|
except AttributeError:
|
2015-01-08 19:35:59 +01:00
|
|
|
if self._default is self._NO_DEFAULT:
|
2015-01-07 14:22:12 +01:00
|
|
|
raise AttributeError(
|
|
|
|
'property {!r} not set'.format(self.__name__))
|
2014-12-05 14:58:05 +01:00
|
|
|
elif isinstance(self._default, collections.Callable):
|
|
|
|
return self._default(instance)
|
|
|
|
else:
|
|
|
|
return self._default
|
|
|
|
|
|
|
|
|
|
|
|
def __set__(self, instance, value):
|
2015-09-25 21:36:35 +02:00
|
|
|
self._enforce_write_once(instance)
|
|
|
|
|
2015-01-08 17:42:34 +01:00
|
|
|
if value is self.__class__.DEFAULT:
|
|
|
|
self.__delete__(instance)
|
|
|
|
return
|
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
try:
|
|
|
|
oldvalue = getattr(instance, self.__name__)
|
|
|
|
has_oldvalue = True
|
|
|
|
except AttributeError:
|
|
|
|
has_oldvalue = False
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
if self._setter is not None:
|
|
|
|
value = self._setter(instance, self, value)
|
2015-09-21 22:48:19 +02:00
|
|
|
if self._type not in (None, type(value)):
|
2014-12-05 14:58:05 +01:00
|
|
|
value = self._type(value)
|
2014-12-09 18:34:00 +01:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
if has_oldvalue:
|
2015-06-24 15:36:09 +02:00
|
|
|
instance.fire_event_pre('property-pre-set:' + self.__name__,
|
|
|
|
self.__name__, value, oldvalue)
|
2014-12-29 12:46:16 +01:00
|
|
|
else:
|
2015-06-24 15:36:09 +02:00
|
|
|
instance.fire_event_pre('property-pre-set:' + self.__name__,
|
|
|
|
self.__name__, value)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2015-01-21 12:50:00 +01:00
|
|
|
instance._property_init(self, value) # pylint: disable=protected-access
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
if has_oldvalue:
|
2015-06-24 15:36:09 +02:00
|
|
|
instance.fire_event('property-set:' + self.__name__, self.__name__,
|
|
|
|
value, oldvalue)
|
2014-12-09 18:34:00 +01:00
|
|
|
else:
|
2015-06-24 15:36:09 +02:00
|
|
|
instance.fire_event('property-set:' + self.__name__, self.__name__,
|
|
|
|
value)
|
2014-12-09 18:34:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
def __delete__(self, instance):
|
2015-09-25 21:36:35 +02:00
|
|
|
self._enforce_write_once(instance)
|
|
|
|
|
2015-01-08 17:45:34 +01:00
|
|
|
try:
|
|
|
|
oldvalue = getattr(instance, self.__name__)
|
|
|
|
has_oldvalue = True
|
|
|
|
except AttributeError:
|
|
|
|
has_oldvalue = False
|
|
|
|
|
|
|
|
if has_oldvalue:
|
2015-01-19 17:06:30 +01:00
|
|
|
instance.fire_event_pre('property-pre-deleted:' + self.__name__,
|
2015-06-24 15:36:09 +02:00
|
|
|
self.__name__, oldvalue)
|
2015-01-08 17:45:34 +01:00
|
|
|
else:
|
2015-06-24 15:36:09 +02:00
|
|
|
instance.fire_event_pre('property-pre-deleted:' + self.__name__,
|
|
|
|
self.__name__)
|
2015-01-08 17:45:34 +01:00
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
delattr(instance, self._attr_name)
|
|
|
|
|
2015-01-08 17:45:34 +01:00
|
|
|
if has_oldvalue:
|
2015-01-19 17:06:30 +01:00
|
|
|
instance.fire_event('property-deleted:' + self.__name__,
|
2015-06-24 15:36:09 +02:00
|
|
|
self.__name__, oldvalue)
|
2015-01-08 17:45:34 +01:00
|
|
|
else:
|
2015-06-24 15:36:09 +02:00
|
|
|
instance.fire_event('property-deleted:' + self.__name__,
|
|
|
|
self.__name__)
|
2015-01-08 17:45:34 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
def __repr__(self):
|
2015-07-03 17:06:24 +02:00
|
|
|
default = ' default={!r}'.format(self._default) \
|
|
|
|
if self._default is not self._NO_DEFAULT \
|
|
|
|
else ''
|
|
|
|
return '<{} object at {:#x} name={!r}{}>'.format(
|
|
|
|
self.__class__.__name__, id(self), self.__name__, default) \
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
2016-02-10 16:49:46 +01:00
|
|
|
return isinstance(other, property) and self.__name__ == other.__name__
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
2015-09-25 21:36:35 +02:00
|
|
|
def _enforce_write_once(self, instance):
|
|
|
|
if self._write_once and not instance.property_is_default(self):
|
|
|
|
raise AttributeError(
|
|
|
|
'property {!r} is write-once and already set'.format(
|
|
|
|
self.__name__))
|
|
|
|
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
#
|
|
|
|
# exceptions
|
|
|
|
#
|
|
|
|
|
|
|
|
class DontSave(Exception):
|
2015-09-17 12:08:03 +02:00
|
|
|
'''This exception may be raised from saver to sign that property should
|
2014-12-29 12:46:16 +01:00
|
|
|
not be saved.
|
|
|
|
'''
|
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def dontsave(self, prop, value):
|
|
|
|
'''Dummy saver that never saves anything.'''
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=bad-staticmethod-argument,unused-argument
|
2015-01-20 14:09:47 +01:00
|
|
|
raise property.DontSave()
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
#
|
|
|
|
# some setters provided
|
|
|
|
#
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def forbidden(self, prop, value):
|
2014-12-29 12:46:16 +01:00
|
|
|
'''Property setter that forbids loading a property.
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
This is used to effectively disable property in classes which inherit
|
|
|
|
unwanted property. When someone attempts to load such a property, it
|
|
|
|
|
|
|
|
:throws AttributeError: always
|
2015-01-19 19:02:28 +01:00
|
|
|
''' # pylint: disable=bad-staticmethod-argument,unused-argument
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
raise AttributeError(
|
|
|
|
'setting {} property on {} instance is forbidden'.format(
|
|
|
|
prop.__name__, self.__class__.__name__))
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
@staticmethod
|
|
|
|
def bool(self, prop, value):
|
|
|
|
'''Property setter for boolean properties.
|
|
|
|
|
|
|
|
It accepts (case-insensitive) ``'0'``, ``'no'`` and ``false`` as
|
|
|
|
:py:obj:`False` and ``'1'``, ``'yes'`` and ``'true'`` as
|
|
|
|
:py:obj:`True`.
|
2015-01-19 19:02:28 +01:00
|
|
|
''' # pylint: disable=bad-staticmethod-argument,unused-argument
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2015-09-25 21:49:59 +02:00
|
|
|
if isinstance(value, basestring):
|
|
|
|
lcvalue = value.lower()
|
|
|
|
if lcvalue in ('0', 'no', 'false', 'off'):
|
|
|
|
return False
|
|
|
|
if lcvalue in ('1', 'yes', 'true', 'on'):
|
|
|
|
return True
|
|
|
|
raise ValueError(
|
|
|
|
'Invalid literal for boolean property: {!r}'.format(value))
|
|
|
|
|
|
|
|
return bool(value)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
class PropertyHolder(qubes.events.Emitter):
|
|
|
|
'''Abstract class for holding :py:class:`qubes.property`
|
|
|
|
|
|
|
|
Events fired by instances of this class:
|
|
|
|
|
|
|
|
.. event:: property-load (subject, event)
|
|
|
|
|
|
|
|
Fired once after all properties are loaded from XML. Individual
|
|
|
|
``property-set`` events are not fired.
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
.. event:: property-set:<propname> \
|
|
|
|
(subject, event, name, newvalue[, oldvalue])
|
2014-12-09 18:34:00 +01:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
Fired when property changes state. Signature is variable,
|
|
|
|
*oldvalue* is present only if there was an old value.
|
|
|
|
|
|
|
|
:param name: Property name
|
|
|
|
:param newvalue: New value of the property
|
|
|
|
:param oldvalue: Old value of the property
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
.. event:: property-pre-set:<propname> \
|
|
|
|
(subject, event, name, newvalue[, oldvalue])
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
Fired before property changes state. Signature is variable,
|
|
|
|
*oldvalue* is present only if there was an old value.
|
2014-12-09 18:34:00 +01:00
|
|
|
|
|
|
|
:param name: Property name
|
|
|
|
:param newvalue: New value of the property
|
|
|
|
:param oldvalue: Old value of the property
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
.. event:: property-del:<propname> \
|
|
|
|
(subject, event, name[, oldvalue])
|
2015-01-08 17:45:34 +01:00
|
|
|
|
|
|
|
Fired when property gets deleted (is set to default). Signature is
|
|
|
|
variable, *oldvalue* is present only if there was an old value.
|
|
|
|
|
|
|
|
:param name: Property name
|
|
|
|
:param oldvalue: Old value of the property
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
.. event:: property-pre-del:<propname> \
|
|
|
|
(subject, event, name[, oldvalue])
|
2015-01-08 17:45:34 +01:00
|
|
|
|
|
|
|
Fired before property gets deleted (is set to default). Signature
|
|
|
|
is variable, *oldvalue* is present only if there was an old value.
|
|
|
|
|
|
|
|
:param name: Property name
|
|
|
|
:param oldvalue: Old value of the property
|
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
Members:
|
|
|
|
'''
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-21 15:24:29 +01:00
|
|
|
def __init__(self, xml, **kwargs):
|
2014-12-05 14:58:05 +01:00
|
|
|
self.xml = xml
|
|
|
|
|
2015-09-25 21:36:35 +02:00
|
|
|
propvalues = {}
|
2015-01-15 12:57:44 +01:00
|
|
|
|
2015-01-21 15:24:29 +01:00
|
|
|
all_names = set(prop.__name__ for prop in self.property_list())
|
|
|
|
for key in list(kwargs.keys()):
|
|
|
|
if not key in all_names:
|
|
|
|
continue
|
2015-09-25 21:36:35 +02:00
|
|
|
propvalues[key] = kwargs.pop(key)
|
|
|
|
|
|
|
|
super(PropertyHolder, self).__init__(**kwargs)
|
|
|
|
|
|
|
|
for key, value in propvalues.items():
|
|
|
|
setattr(self, key, value)
|
2015-01-21 15:24:29 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-09 15:09:56 +01:00
|
|
|
@classmethod
|
2015-01-21 12:50:00 +01:00
|
|
|
def property_list(cls, load_stage=None):
|
2015-01-09 15:09:56 +01:00
|
|
|
'''List all properties attached to this VM's class
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
:param load_stage: Filter by load stage
|
|
|
|
:type load_stage: :py:func:`int` or :py:obj:`None`
|
|
|
|
'''
|
|
|
|
|
|
|
|
props = set()
|
2015-01-09 15:09:56 +01:00
|
|
|
for class_ in cls.__mro__:
|
2014-12-05 14:58:05 +01:00
|
|
|
props.update(prop for prop in class_.__dict__.values()
|
|
|
|
if isinstance(prop, property))
|
|
|
|
if load_stage is not None:
|
|
|
|
props = set(prop for prop in props
|
|
|
|
if prop.load_stage == load_stage)
|
2015-01-07 14:22:12 +01:00
|
|
|
return sorted(props,
|
|
|
|
key=lambda prop: (prop.load_stage, prop.order, prop.__name__))
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
2015-01-21 12:50:00 +01:00
|
|
|
def _property_init(self, prop, value):
|
2014-12-05 14:58:05 +01:00
|
|
|
'''Initialise property to a given value, without side effects.
|
|
|
|
|
|
|
|
:param qubes.property prop: property object of particular interest
|
|
|
|
:param value: value
|
|
|
|
'''
|
|
|
|
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=protected-access
|
2015-01-21 12:50:00 +01:00
|
|
|
setattr(self, self.property_get_def(prop)._attr_name, value)
|
2015-01-08 19:35:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
def property_is_default(self, prop):
|
|
|
|
'''Check whether property is in it's default value.
|
|
|
|
|
|
|
|
Properties when unset may return some default value, so
|
|
|
|
``hasattr(vm, prop.__name__)`` is wrong in some circumstances. This
|
|
|
|
method allows for checking if the value returned is in fact it's
|
|
|
|
default value.
|
|
|
|
|
|
|
|
:param qubes.property prop: property object of particular interest
|
|
|
|
:rtype: bool
|
2015-07-03 17:06:24 +02:00
|
|
|
''' # pylint: disable=protected-access
|
2015-01-08 19:35:59 +01:00
|
|
|
|
2015-07-03 17:06:24 +02:00
|
|
|
# both property_get_def() and ._attr_name may throw AttributeError,
|
|
|
|
# which we don't want to catch
|
|
|
|
attrname = self.property_get_def(prop)._attr_name
|
|
|
|
return not hasattr(self, attrname)
|
2015-01-08 19:35:59 +01:00
|
|
|
|
|
|
|
|
2015-01-09 15:09:56 +01:00
|
|
|
@classmethod
|
2015-01-21 12:50:00 +01:00
|
|
|
def property_get_def(cls, prop):
|
2015-01-08 19:35:59 +01:00
|
|
|
'''Return property definition object.
|
|
|
|
|
|
|
|
If prop is already :py:class:`qubes.property` instance, return the same
|
|
|
|
object.
|
|
|
|
|
|
|
|
:param prop: property object or name
|
|
|
|
:type prop: qubes.property or str
|
|
|
|
:rtype: qubes.property
|
|
|
|
'''
|
|
|
|
|
|
|
|
if isinstance(prop, qubes.property):
|
|
|
|
return prop
|
|
|
|
|
2015-01-21 12:50:00 +01:00
|
|
|
for p in cls.property_list():
|
2015-01-08 19:35:59 +01:00
|
|
|
if p.__name__ == prop:
|
|
|
|
return p
|
|
|
|
|
|
|
|
raise AttributeError('No property {!r} found in {!r}'.format(
|
2015-01-09 15:09:56 +01:00
|
|
|
prop, cls))
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
def load_properties(self, load_stage=None):
|
|
|
|
'''Load properties from immediate children of XML node.
|
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
``property-set`` events are not fired for each individual property.
|
|
|
|
|
2015-01-21 15:24:29 +01:00
|
|
|
:param int load_stage: Stage of loading.
|
2014-12-05 14:58:05 +01:00
|
|
|
'''
|
|
|
|
|
2015-12-23 14:17:23 +01:00
|
|
|
if self.xml is None:
|
|
|
|
return
|
2015-01-07 14:22:12 +01:00
|
|
|
all_names = set(
|
2015-01-21 12:50:00 +01:00
|
|
|
prop.__name__ for prop in self.property_list(load_stage))
|
2014-12-05 14:58:05 +01:00
|
|
|
for node in self.xml.xpath('./properties/property'):
|
|
|
|
name = node.get('name')
|
|
|
|
value = node.get('ref') or node.text
|
|
|
|
|
|
|
|
if not name in all_names:
|
2015-01-21 15:24:29 +01:00
|
|
|
continue
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
setattr(self, name, value)
|
2014-12-09 18:34:00 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-13 15:56:10 +01:00
|
|
|
def xml_properties(self, with_defaults=False):
|
2014-12-05 14:58:05 +01:00
|
|
|
'''Iterator that yields XML nodes representing set properties.
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
:param bool with_defaults: If :py:obj:`True`, then it also includes \
|
|
|
|
properties which were not set explicite, but have default values \
|
|
|
|
filled.
|
2014-12-05 14:58:05 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
properties = lxml.etree.Element('properties')
|
|
|
|
|
2015-01-21 12:50:00 +01:00
|
|
|
for prop in self.property_list():
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=protected-access
|
2014-12-05 14:58:05 +01:00
|
|
|
try:
|
2015-01-07 14:22:12 +01:00
|
|
|
value = getattr(
|
|
|
|
self, (prop.__name__ if with_defaults else prop._attr_name))
|
2015-01-20 14:41:19 +01:00
|
|
|
except AttributeError:
|
2014-12-05 14:58:05 +01:00
|
|
|
continue
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
try:
|
|
|
|
value = prop._saver(self, prop, value)
|
|
|
|
except property.DontSave:
|
|
|
|
continue
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
element = lxml.etree.Element('property', name=prop.__name__)
|
|
|
|
if prop.save_via_ref:
|
|
|
|
element.set('ref', value)
|
|
|
|
else:
|
|
|
|
element.text = value
|
|
|
|
properties.append(element)
|
|
|
|
|
|
|
|
return properties
|
|
|
|
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
# this was clone_attrs
|
|
|
|
def clone_properties(self, src, proplist=None):
|
|
|
|
'''Clone properties from other object.
|
|
|
|
|
|
|
|
:param PropertyHolder src: source object
|
2015-01-19 17:06:30 +01:00
|
|
|
:param list proplist: list of properties \
|
|
|
|
(:py:obj:`None` for all properties)
|
2014-12-29 12:46:16 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
if proplist is None:
|
2015-01-21 12:50:00 +01:00
|
|
|
proplist = self.property_list()
|
2014-12-29 12:46:16 +01:00
|
|
|
else:
|
2015-01-21 12:50:00 +01:00
|
|
|
proplist = [prop for prop in self.property_list()
|
2014-12-29 12:46:16 +01:00
|
|
|
if prop.__name__ in proplist or prop in proplist]
|
|
|
|
|
2016-02-10 16:49:08 +01:00
|
|
|
for prop in proplist:
|
2014-12-29 12:46:16 +01:00
|
|
|
try:
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=protected-access
|
2015-01-21 12:50:00 +01:00
|
|
|
self._property_init(prop, getattr(src, prop._attr_name))
|
2014-12-29 12:46:16 +01:00
|
|
|
except AttributeError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
self.fire_event('cloned-properties', src, proplist)
|
|
|
|
|
|
|
|
|
2015-01-21 12:50:00 +01:00
|
|
|
def property_require(self, prop, allow_none=False, hard=False):
|
2015-01-08 19:35:59 +01:00
|
|
|
'''Complain badly when property is not set.
|
|
|
|
|
|
|
|
:param prop: property name or object
|
|
|
|
:type prop: qubes.property or str
|
2015-01-19 17:06:30 +01:00
|
|
|
:param bool allow_none: if :py:obj:`True`, don't complain if \
|
|
|
|
:py:obj:`None` is found
|
|
|
|
:param bool hard: if :py:obj:`True`, raise :py:class:`AssertionError`; \
|
|
|
|
if :py:obj:`False`, log warning instead
|
2015-01-08 19:35:59 +01:00
|
|
|
'''
|
|
|
|
|
2015-06-24 15:45:19 +02:00
|
|
|
if isinstance(prop, qubes.property):
|
2015-01-08 19:35:59 +01:00
|
|
|
prop = prop.__name__
|
|
|
|
|
|
|
|
try:
|
|
|
|
value = getattr(self, prop)
|
|
|
|
if value is None and not allow_none:
|
|
|
|
raise AttributeError()
|
|
|
|
except AttributeError:
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=no-member
|
2015-01-08 19:35:59 +01:00
|
|
|
msg = 'Required property {!r} not set on {!r}'.format(prop, self)
|
|
|
|
if hard:
|
|
|
|
raise AssertionError(msg)
|
|
|
|
else:
|
2015-01-22 11:24:23 +01:00
|
|
|
# pylint: disable=no-member
|
|
|
|
self.log.fatal(msg)
|
2015-01-08 19:35:59 +01:00
|
|
|
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
import qubes.vm
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
class VMProperty(property):
|
|
|
|
'''Property that is referring to a VM
|
|
|
|
|
|
|
|
:param type vmclass: class that returned VM is supposed to be instance of
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
and all supported by :py:class:`property` with the exception of ``type`` \
|
|
|
|
and ``setter``
|
2014-12-05 14:58:05 +01:00
|
|
|
'''
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
def __init__(self, name, vmclass=qubes.vm.BaseVM, allow_none=False,
|
|
|
|
**kwargs):
|
2014-12-05 14:58:05 +01:00
|
|
|
if 'type' in kwargs:
|
2015-01-19 17:06:30 +01:00
|
|
|
raise TypeError(
|
|
|
|
"'type' keyword parameter is unsupported in {}".format(
|
|
|
|
self.__class__.__name__))
|
2014-12-05 14:58:05 +01:00
|
|
|
if 'setter' in kwargs:
|
2015-01-19 17:06:30 +01:00
|
|
|
raise TypeError(
|
|
|
|
"'setter' keyword parameter is unsupported in {}".format(
|
|
|
|
self.__class__.__name__))
|
2015-01-08 19:35:59 +01:00
|
|
|
if not issubclass(vmclass, qubes.vm.BaseVM):
|
2015-01-19 17:06:30 +01:00
|
|
|
raise TypeError(
|
|
|
|
"'vmclass' should specify a subclass of qubes.vm.BaseVM")
|
2015-01-08 19:35:59 +01:00
|
|
|
|
2015-09-17 12:08:03 +02:00
|
|
|
super(VMProperty, self).__init__(name,
|
|
|
|
saver=(lambda self, prop, value: value.name if value else 'None'),
|
|
|
|
**kwargs)
|
2014-12-05 14:58:05 +01:00
|
|
|
self.vmclass = vmclass
|
2015-01-08 19:35:59 +01:00
|
|
|
self.allow_none = allow_none
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-09-17 12:08:03 +02:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
def __set__(self, instance, value):
|
2015-01-08 19:35:59 +01:00
|
|
|
if value is None:
|
|
|
|
if self.allow_none:
|
2015-09-17 12:08:03 +02:00
|
|
|
super(VMProperty, self).__set__(instance, value)
|
2015-01-08 19:35:59 +01:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
raise ValueError(
|
|
|
|
'Property {!r} does not allow setting to {!r}'.format(
|
|
|
|
self.__name__, value))
|
|
|
|
|
2015-09-17 12:08:03 +02:00
|
|
|
app = instance if isinstance(instance, Qubes) else instance.app
|
|
|
|
|
2015-01-08 19:35:59 +01:00
|
|
|
# XXX this may throw LookupError; that's good until introduction
|
|
|
|
# of QubesNoSuchVMException or whatever
|
2015-09-17 12:08:03 +02:00
|
|
|
vm = app.domains[value]
|
2015-01-08 19:35:59 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
if not isinstance(vm, self.vmclass):
|
2015-01-19 17:06:30 +01:00
|
|
|
raise TypeError('wrong VM class: domains[{!r}] if of type {!s} '
|
|
|
|
'and not {!s}'.format(value,
|
|
|
|
vm.__class__.__name__,
|
|
|
|
self.vmclass.__name__))
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-09-17 12:08:03 +02:00
|
|
|
super(VMProperty, self).__set__(instance, vm)
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
import qubes.vm.qubesvm
|
|
|
|
import qubes.vm.templatevm
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
class Qubes(PropertyHolder):
|
|
|
|
'''Main Qubes application
|
|
|
|
|
|
|
|
:param str store: path to ``qubes.xml``
|
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
The store is loaded in stages:
|
|
|
|
|
|
|
|
1. In the first stage there are loaded some basic features from store
|
|
|
|
(currently labels).
|
|
|
|
|
|
|
|
2. In the second stage stubs for all VMs are loaded. They are filled
|
|
|
|
with their basic properties, like ``qid`` and ``name``.
|
|
|
|
|
|
|
|
3. In the third stage all global properties are loaded. They often
|
|
|
|
reference VMs, like default netvm, so they should be filled after
|
|
|
|
loading VMs.
|
|
|
|
|
|
|
|
4. In the fourth stage all remaining VM properties are loaded. They
|
|
|
|
also need all VMs loaded, because they represent dependencies
|
|
|
|
between VMs like aforementioned netvm.
|
|
|
|
|
|
|
|
5. In the fifth stage there are some fixups to ensure sane system
|
|
|
|
operation.
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
This class emits following events:
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
.. event:: domain-added (subject, event, vm)
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
When domain is added.
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
:param subject: Event emitter
|
|
|
|
:param event: Event name (``'domain-added'``)
|
|
|
|
:param vm: Domain object
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
.. event:: domain-deleted (subject, event, vm)
|
|
|
|
|
|
|
|
When domain is deleted. VM still has reference to ``app`` object,
|
|
|
|
but is not contained within VMCollection.
|
|
|
|
|
|
|
|
:param subject: Event emitter
|
|
|
|
:param event: Event name (``'domain-deleted'``)
|
|
|
|
:param vm: Domain object
|
|
|
|
|
|
|
|
Methods and attributes:
|
2014-12-05 14:58:05 +01:00
|
|
|
'''
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
default_netvm = VMProperty('default_netvm', load_stage=3,
|
2015-09-17 12:08:03 +02:00
|
|
|
default=None, allow_none=True,
|
2015-01-12 16:56:14 +01:00
|
|
|
doc='''Default NetVM for AppVMs. Initial state is `None`, which means
|
|
|
|
that AppVMs are not connected to the Internet.''')
|
2015-01-19 17:06:30 +01:00
|
|
|
default_fw_netvm = VMProperty('default_fw_netvm', load_stage=3,
|
2015-09-17 12:08:03 +02:00
|
|
|
default=None, allow_none=True,
|
2015-01-12 16:56:14 +01:00
|
|
|
doc='''Default NetVM for ProxyVMs. Initial state is `None`, which means
|
|
|
|
that ProxyVMs (including FirewallVM) are not connected to the
|
2015-01-08 19:35:59 +01:00
|
|
|
Internet.''')
|
2014-12-05 14:58:05 +01:00
|
|
|
default_template = VMProperty('default_template', load_stage=3,
|
|
|
|
vmclass=qubes.vm.templatevm.TemplateVM,
|
|
|
|
doc='Default template for new AppVMs')
|
|
|
|
updatevm = VMProperty('updatevm', load_stage=3,
|
2015-09-17 12:08:03 +02:00
|
|
|
allow_none=True,
|
2015-01-12 16:56:14 +01:00
|
|
|
doc='''Which VM to use as `yum` proxy for updating AdminVM and
|
|
|
|
TemplateVMs''')
|
2014-12-05 14:58:05 +01:00
|
|
|
clockvm = VMProperty('clockvm', load_stage=3,
|
2015-09-17 12:08:03 +02:00
|
|
|
allow_none=True,
|
2014-12-05 14:58:05 +01:00
|
|
|
doc='Which VM to use as NTP proxy for updating AdminVM')
|
|
|
|
default_kernel = property('default_kernel', load_stage=3,
|
|
|
|
doc='Which kernel to use when not overriden in VM')
|
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
# TODO #1637 #892
|
|
|
|
check_updates_vm = property('check_updates_vm',
|
|
|
|
type=bool, setter=property.bool,
|
2016-02-09 00:24:40 +01:00
|
|
|
default=True,
|
2016-03-03 01:05:23 +01:00
|
|
|
doc='check for updates inside qubes')
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-07-02 14:56:36 +02:00
|
|
|
def __init__(self, store=None, load=True, **kwargs):
|
2015-01-21 17:03:17 +01:00
|
|
|
#: logger instance for logging global messages
|
2015-01-15 12:57:44 +01:00
|
|
|
self.log = logging.getLogger('app')
|
|
|
|
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=no-member
|
|
|
|
self._extensions = set(
|
|
|
|
ext(self) for ext in qubes.ext.Extension.register.values())
|
2014-12-09 14:14:24 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
#: collection of all VMs managed by this Qubes instance
|
2015-01-15 12:57:44 +01:00
|
|
|
self.domains = VMCollection(self)
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
#: collection of all available labels for VMs
|
|
|
|
self.labels = {}
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
#: Connection to VMM
|
|
|
|
self.vmm = VMMConnection()
|
|
|
|
|
|
|
|
#: Information about host system
|
|
|
|
self.host = QubesHost(self)
|
|
|
|
|
2015-06-23 22:27:20 +02:00
|
|
|
self._store = store if store is not None else os.path.join(
|
|
|
|
qubes.config.system_path['qubes_base_dir'],
|
|
|
|
qubes.config.system_path['qubes_store_filename'])
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-07-02 14:56:36 +02:00
|
|
|
super(Qubes, self).__init__(xml=None, **kwargs)
|
|
|
|
|
2015-06-23 22:27:20 +02:00
|
|
|
if load:
|
|
|
|
self.load()
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-06-24 15:02:49 +02:00
|
|
|
self.events_enabled = True
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-06-23 22:27:20 +02:00
|
|
|
def load(self):
|
|
|
|
'''Open qubes.xml
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-06-23 22:27:20 +02:00
|
|
|
:throws EnvironmentError: failure on parsing store
|
|
|
|
:throws xml.parsers.expat.ExpatError: failure on parsing store
|
2015-01-15 12:57:44 +01:00
|
|
|
:raises lxml.etree.XMLSyntaxError: on syntax error in qubes.xml
|
|
|
|
'''
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-06-23 22:27:20 +02:00
|
|
|
fd = os.open(self._store, os.O_RDWR) # no O_CREAT
|
|
|
|
fh = os.fdopen(fd, 'rb')
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
if os.name == 'posix':
|
2015-06-23 22:27:20 +02:00
|
|
|
fcntl.lockf(fh, fcntl.LOCK_EX)
|
2014-12-05 14:58:05 +01:00
|
|
|
elif os.name == 'nt':
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=protected-access
|
2015-01-15 12:57:44 +01:00
|
|
|
win32file.LockFileEx(
|
2015-06-23 22:27:20 +02:00
|
|
|
win32file._get_osfhandle(fh.fileno()),
|
2015-01-15 12:57:44 +01:00
|
|
|
win32con.LOCKFILE_EXCLUSIVE_LOCK,
|
|
|
|
0, -0x10000,
|
|
|
|
pywintypes.OVERLAPPED())
|
|
|
|
|
2015-06-23 22:27:20 +02:00
|
|
|
self.xml = lxml.etree.parse(fh)
|
2015-01-15 12:57:44 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
# stage 1: load labels
|
2015-01-22 11:24:23 +01:00
|
|
|
for node in self.xml.xpath('./labels/label'):
|
2014-12-05 14:58:05 +01:00
|
|
|
label = Label.fromxml(node)
|
2015-01-22 11:24:23 +01:00
|
|
|
self.labels[label.index] = label
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
# stage 2: load VMs
|
2015-01-22 11:24:23 +01:00
|
|
|
for node in self.xml.xpath('./domains/domain'):
|
2015-01-21 15:24:29 +01:00
|
|
|
# pylint: disable=no-member
|
|
|
|
cls = qubes.vm.BaseVM.register[node.get('class')]
|
|
|
|
vm = cls(self, node)
|
|
|
|
vm.load_properties(load_stage=2)
|
2015-09-23 16:25:53 +02:00
|
|
|
vm.init_log()
|
2014-12-05 14:58:05 +01:00
|
|
|
self.domains.add(vm)
|
|
|
|
|
|
|
|
if not 0 in self.domains:
|
2015-01-15 12:57:44 +01:00
|
|
|
self.domains.add(qubes.vm.adminvm.AdminVM(
|
|
|
|
self, None, qid=0, name='dom0'))
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
# stage 3: load global properties
|
2015-01-20 16:32:25 +01:00
|
|
|
self.load_properties(load_stage=3)
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
# stage 4: fill all remaining VM properties
|
|
|
|
for vm in self.domains:
|
2015-01-20 16:32:25 +01:00
|
|
|
vm.load_properties(load_stage=4)
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
# stage 5: misc fixups
|
|
|
|
|
2015-01-21 12:50:00 +01:00
|
|
|
self.property_require('default_fw_netvm', allow_none=True)
|
|
|
|
self.property_require('default_netvm', allow_none=True)
|
|
|
|
self.property_require('default_template')
|
|
|
|
self.property_require('clockvm')
|
|
|
|
self.property_require('updatevm')
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
# Disable ntpd in ClockVM - to not conflict with ntpdate (both are
|
|
|
|
# using 123/udp port)
|
2015-09-17 12:08:03 +02:00
|
|
|
if hasattr(self, 'clockvm') and self.clockvm is not None:
|
2015-01-08 19:35:59 +01:00
|
|
|
if 'ntpd' in self.clockvm.services:
|
|
|
|
if self.clockvm.services['ntpd']:
|
|
|
|
self.log.warning("VM set as clockvm ({!r}) has enabled "
|
|
|
|
"'ntpd' service! Expect failure when syncing time in "
|
|
|
|
"dom0.".format(self.clockvm))
|
|
|
|
else:
|
|
|
|
self.clockvm.services['ntpd'] = False
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-21 15:24:29 +01:00
|
|
|
for vm in self.domains:
|
|
|
|
vm.events_enabled = True
|
|
|
|
vm.fire_event('domain-loaded')
|
|
|
|
|
2015-06-23 22:27:20 +02:00
|
|
|
# intentionally do not call explicit unlock
|
|
|
|
fh.close()
|
|
|
|
del fh
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
def __xml__(self):
|
|
|
|
element = lxml.etree.Element('qubes')
|
|
|
|
|
2015-01-13 15:56:10 +01:00
|
|
|
element.append(self.xml_labels())
|
|
|
|
element.append(self.xml_properties())
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
domains = lxml.etree.Element('domains')
|
|
|
|
for vm in self.domains:
|
|
|
|
domains.append(vm.__xml__())
|
|
|
|
element.append(domains)
|
|
|
|
|
|
|
|
return element
|
|
|
|
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
'''Save all data to qubes.xml
|
2015-06-23 22:27:20 +02:00
|
|
|
|
2015-10-27 13:38:33 +01:00
|
|
|
There are several problems with saving :file:`qubes.xml` which must be
|
|
|
|
mitigated:
|
|
|
|
|
|
|
|
- Running out of disk space. No space left should not result in empty
|
|
|
|
file. This is done by writing to temporary file and the renaming.
|
|
|
|
- Attempts to write two or more files concurrently. This is done by
|
|
|
|
sophisticated locking.
|
|
|
|
|
2015-06-23 22:27:20 +02:00
|
|
|
:throws EnvironmentError: failure on saving
|
2014-12-05 14:58:05 +01:00
|
|
|
'''
|
2015-06-23 22:27:20 +02:00
|
|
|
|
2015-10-27 13:38:33 +01:00
|
|
|
while True:
|
2015-12-22 16:26:09 +01:00
|
|
|
fd_old = os.open(self._store, os.O_RDWR | os.O_CREAT)
|
2015-10-27 13:38:33 +01:00
|
|
|
if os.name == 'posix':
|
|
|
|
fcntl.lockf(fd_old, fcntl.LOCK_EX)
|
|
|
|
elif os.name == 'nt':
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
overlapped = pywintypes.OVERLAPPED()
|
|
|
|
win32file.LockFileEx(
|
|
|
|
win32file._get_osfhandle(fd_old),
|
|
|
|
win32con.LOCKFILE_EXCLUSIVE_LOCK, 0, -0x10000, overlapped)
|
|
|
|
|
|
|
|
# While we were waiting for lock, someone could have unlink()ed (or
|
|
|
|
# rename()d) our file out of the filesystem. We have to ensure we
|
|
|
|
# got lock on something linked to filesystem. If not, try again.
|
|
|
|
if os.fstat(fd_old) == os.stat(self._store):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
os.close(fd_old)
|
|
|
|
|
|
|
|
fh_new = tempfile.NamedTemporaryFile(prefix=self._store, delete=False)
|
2014-12-05 14:58:05 +01:00
|
|
|
lxml.etree.ElementTree(self.__xml__()).write(
|
2015-10-27 13:38:33 +01:00
|
|
|
fh_new, encoding='utf-8', pretty_print=True)
|
|
|
|
fh_new.flush()
|
|
|
|
os.chmod(fh_new.name, 0660)
|
|
|
|
os.chown(fh_new.name, -1, grp.getgrnam('qubes').gr_gid)
|
|
|
|
os.rename(fh_new.name, self._store)
|
2015-06-23 22:27:20 +02:00
|
|
|
|
|
|
|
# intentionally do not call explicit unlock to not unlock the file
|
|
|
|
# before all buffers are flushed
|
2015-10-27 13:38:33 +01:00
|
|
|
fh_new.close()
|
|
|
|
os.close(fd_old)
|
2015-06-23 22:27:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_empty_store(cls, *args, **kwargs):
|
|
|
|
self = cls(*args, load=False, **kwargs)
|
|
|
|
self.labels = {
|
|
|
|
1: Label(1, '0xcc0000', 'red'),
|
|
|
|
2: Label(2, '0xf57900', 'orange'),
|
|
|
|
3: Label(3, '0xedd400', 'yellow'),
|
|
|
|
4: Label(4, '0x73d216', 'green'),
|
|
|
|
5: Label(5, '0x555753', 'gray'),
|
|
|
|
6: Label(6, '0x3465a4', 'blue'),
|
|
|
|
7: Label(7, '0x75507b', 'purple'),
|
|
|
|
8: Label(8, '0x000000', 'black'),
|
|
|
|
}
|
|
|
|
self.domains.add(
|
|
|
|
qubes.vm.adminvm.AdminVM(self, None, qid=0, name='dom0'))
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
return self
|
2014-12-05 14:58:05 +01:00
|
|
|
|
|
|
|
|
2015-01-13 15:56:10 +01:00
|
|
|
def xml_labels(self):
|
2014-12-05 14:58:05 +01:00
|
|
|
'''Serialise labels
|
|
|
|
|
|
|
|
:rtype: lxml.etree._Element
|
|
|
|
'''
|
|
|
|
|
|
|
|
labels = lxml.etree.Element('labels')
|
2015-06-23 22:46:56 +02:00
|
|
|
for label in sorted(self.labels.values(), key=lambda labl: labl.index):
|
2014-12-05 14:58:05 +01:00
|
|
|
labels.append(label.__xml__())
|
|
|
|
return labels
|
|
|
|
|
|
|
|
|
2015-06-26 11:08:26 +02:00
|
|
|
def add_new_vm(self, cls, qid=None, **kwargs):
|
2014-12-05 14:58:05 +01:00
|
|
|
'''Add new Virtual Machine to colletion
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
2015-06-26 11:08:26 +02:00
|
|
|
if qid is None:
|
2015-07-01 16:26:56 +02:00
|
|
|
qid = self.domains.get_new_unused_qid()
|
2015-06-26 11:08:26 +02:00
|
|
|
|
|
|
|
return self.domains.add(cls(self, None, qid=qid, **kwargs))
|
|
|
|
|
|
|
|
|
|
|
|
def get_label(self, label):
|
|
|
|
'''Get label as identified by index or name
|
|
|
|
|
|
|
|
:throws KeyError: when label is not found
|
|
|
|
'''
|
|
|
|
|
|
|
|
# first search for index, verbatim
|
|
|
|
try:
|
|
|
|
return self.labels[label]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# then search for name
|
2015-10-05 23:46:25 +02:00
|
|
|
for i in self.labels.values():
|
|
|
|
if i.name == label:
|
|
|
|
return i
|
2015-06-26 11:08:26 +02:00
|
|
|
|
|
|
|
# last call, if label is a number represented as str, search in indices
|
|
|
|
try:
|
|
|
|
return self.labels[int(label)]
|
|
|
|
except (KeyError, ValueError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
raise KeyError(label)
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-08 19:35:59 +01:00
|
|
|
|
|
|
|
@qubes.events.handler('domain-pre-deleted')
|
|
|
|
def on_domain_pre_deleted(self, event, vm):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=unused-argument
|
2015-01-08 19:35:59 +01:00
|
|
|
if isinstance(vm, qubes.vm.templatevm.TemplateVM):
|
2015-01-22 11:24:23 +01:00
|
|
|
appvms = self.domains.get_vms_based_on(vm)
|
2015-01-08 19:35:59 +01:00
|
|
|
if appvms:
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesException(
|
2015-01-08 19:35:59 +01:00
|
|
|
'Cannot remove template that has dependent AppVMs. '
|
|
|
|
'Affected are: {}'.format(', '.join(
|
|
|
|
vm.name for name in sorted(appvms))))
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
|
|
|
|
@qubes.events.handler('domain-deleted')
|
|
|
|
def on_domain_deleted(self, event, vm):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=unused-argument
|
2015-10-27 12:07:09 +01:00
|
|
|
for propname in (
|
|
|
|
'default_netvm',
|
|
|
|
'default_fw_netvm',
|
|
|
|
'clockvm',
|
|
|
|
'updatevm',
|
|
|
|
'default_template',
|
|
|
|
):
|
|
|
|
try:
|
|
|
|
if getattr(self, propname) == vm:
|
|
|
|
delattr(self, propname)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2014-12-09 18:34:00 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-08 19:35:59 +01:00
|
|
|
@qubes.events.handler('property-pre-set:clockvm')
|
|
|
|
def on_property_pre_set_clockvm(self, event, name, newvalue, oldvalue=None):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=unused-argument,no-self-use
|
2015-09-17 12:08:03 +02:00
|
|
|
if newvalue is None:
|
|
|
|
return
|
2015-01-08 19:35:59 +01:00
|
|
|
if 'ntpd' in newvalue.services:
|
|
|
|
if newvalue.services['ntpd']:
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesVMError(newvalue,
|
|
|
|
'Cannot set {!r} as {!r} since it has ntpd enabled.'.format(
|
|
|
|
newvalue.name, name))
|
2015-01-08 19:35:59 +01:00
|
|
|
else:
|
|
|
|
newvalue.services['ntpd'] = False
|
|
|
|
|
|
|
|
|
2015-01-20 16:32:25 +01:00
|
|
|
@qubes.events.handler(
|
|
|
|
'property-pre-set:default_netvm',
|
|
|
|
'property-pre-set:default_fw_netvm')
|
2015-01-19 17:06:30 +01:00
|
|
|
def on_property_pre_set_default_netvm(self, event, name, newvalue,
|
|
|
|
oldvalue=None):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=unused-argument,invalid-name
|
2015-01-08 19:35:59 +01:00
|
|
|
if newvalue is not None and oldvalue is not None \
|
|
|
|
and oldvalue.is_running() and not newvalue.is_running() \
|
|
|
|
and self.domains.get_vms_connected_to(oldvalue):
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesVMNotRunningError(newvalue,
|
|
|
|
'Cannot change {!r} to domain that '
|
|
|
|
'is not running ({!r}).'.format(name, newvalue.name))
|
2015-01-08 19:35:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
@qubes.events.handler('property-set:default_fw_netvm')
|
2015-01-20 14:41:19 +01:00
|
|
|
def on_property_set_default_fw_netvm(self, event, name, newvalue,
|
2015-01-19 17:06:30 +01:00
|
|
|
oldvalue=None):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=unused-argument,invalid-name
|
2015-01-08 19:35:59 +01:00
|
|
|
for vm in self.domains:
|
|
|
|
if not vm.provides_network and vm.property_is_default('netvm'):
|
|
|
|
# fire property-del:netvm as it is responsible for resetting
|
|
|
|
# netvm to it's default value
|
|
|
|
vm.fire_event('property-del:netvm', 'netvm', newvalue, oldvalue)
|
|
|
|
|
|
|
|
|
|
|
|
@qubes.events.handler('property-set:default_netvm')
|
2015-01-19 17:06:30 +01:00
|
|
|
def on_property_set_default_netvm(self, event, name, newvalue,
|
|
|
|
oldvalue=None):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=unused-argument
|
2015-01-08 19:35:59 +01:00
|
|
|
for vm in self.domains:
|
|
|
|
if vm.provides_network and vm.property_is_default('netvm'):
|
|
|
|
# fire property-del:netvm as it is responsible for resetting
|
|
|
|
# netvm to it's default value
|
|
|
|
vm.fire_event('property-del:netvm', 'netvm', newvalue, oldvalue)
|
|
|
|
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
# load plugins
|
|
|
|
import qubes._pluginloader
|