2013-01-20 21:44:13 +01:00
|
|
|
#!/usr/bin/python2
|
2014-05-18 21:01:21 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2010-04-05 20:58:57 +02:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010 Joanna Rutkowska <joanna@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.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import os.path
|
2012-10-17 21:26:13 +02:00
|
|
|
import lxml.etree
|
2010-04-05 20:58:57 +02:00
|
|
|
import xml.parsers.expat
|
|
|
|
import fcntl
|
2011-06-01 23:44:06 +02:00
|
|
|
import time
|
2013-01-17 01:29:32 +01:00
|
|
|
import warnings
|
2013-12-02 03:32:33 +01:00
|
|
|
import tempfile
|
|
|
|
import grp
|
2013-05-04 04:45:55 +02:00
|
|
|
import atexit
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
# Do not use XenAPI or create/read any VM files
|
|
|
|
# This is for testing only!
|
|
|
|
dry_run = False
|
|
|
|
#dry_run = True
|
|
|
|
|
|
|
|
if not dry_run:
|
2013-05-04 04:45:55 +02:00
|
|
|
import libvirt
|
2011-06-01 23:44:06 +02:00
|
|
|
import xen.lowlevel.xc
|
|
|
|
import xen.lowlevel.xs
|
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2010-06-26 15:02:58 +02:00
|
|
|
qubes_base_dir = "/var/lib/qubes"
|
2013-03-15 18:43:09 +01:00
|
|
|
system_path = {
|
|
|
|
'qubes_guid_path': '/usr/bin/qubes-guid',
|
|
|
|
'qrexec_daemon_path': '/usr/lib/qubes/qrexec-daemon',
|
|
|
|
'qrexec_client_path': '/usr/lib/qubes/qrexec-client',
|
2013-06-07 05:16:15 +02:00
|
|
|
'qubesdb_daemon_path': '/usr/sbin/qubesdb-daemon',
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2013-03-15 18:43:09 +01:00
|
|
|
'qubes_base_dir': qubes_base_dir,
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2013-03-15 18:43:09 +01:00
|
|
|
'qubes_appvms_dir': qubes_base_dir + '/appvms',
|
|
|
|
'qubes_templates_dir': qubes_base_dir + '/vm-templates',
|
|
|
|
'qubes_servicevms_dir': qubes_base_dir + '/servicevms',
|
|
|
|
'qubes_store_filename': qubes_base_dir + '/qubes.xml',
|
|
|
|
'qubes_kernels_base_dir': qubes_base_dir + '/vm-kernels',
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2014-07-10 16:17:58 +02:00
|
|
|
# qubes_icon_dir is obsolete
|
|
|
|
# use QIcon.fromTheme() where applicable
|
2014-07-16 02:55:42 +02:00
|
|
|
'qubes_icon_dir': '/usr/share/icons/hicolor/128x128/devices',
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2013-05-04 04:45:55 +02:00
|
|
|
'config_template_pv': '/usr/share/qubes/xen-vm-template.xml',
|
2012-02-24 04:23:27 +01:00
|
|
|
|
2013-03-15 18:43:09 +01:00
|
|
|
'qubes_pciback_cmd': '/usr/lib/qubes/unbind-pci-device.sh',
|
|
|
|
'prepare_volatile_img_cmd': '/usr/lib/qubes/prepare-volatile-img.sh',
|
2013-08-11 04:08:54 +02:00
|
|
|
'monitor_layout_notify_cmd': '/usr/bin/qubes-monitor-layout-notify',
|
2013-03-15 18:43:09 +01:00
|
|
|
}
|
2012-03-02 01:46:10 +01:00
|
|
|
|
2013-03-15 18:43:09 +01:00
|
|
|
vm_files = {
|
|
|
|
'root_img': 'root.img',
|
|
|
|
'rootcow_img': 'root-cow.img',
|
|
|
|
'volatile_img': 'volatile.img',
|
|
|
|
'clean_volatile_img': 'clean-volatile.img.tar',
|
|
|
|
'private_img': 'private.img',
|
|
|
|
'kernels_subdir': 'kernels',
|
|
|
|
'firewall_conf': 'firewall.xml',
|
|
|
|
'whitelisted_appmenus': 'whitelisted-appmenus.list',
|
|
|
|
'updates_stat_file': 'updates.stat',
|
|
|
|
}
|
2011-07-05 21:20:43 +02:00
|
|
|
|
2013-03-15 18:43:09 +01:00
|
|
|
defaults = {
|
2013-05-04 04:45:55 +02:00
|
|
|
'libvirt_uri': 'xen:///',
|
2013-03-15 18:43:09 +01:00
|
|
|
'memory': 400,
|
2014-04-16 16:07:59 +02:00
|
|
|
'kernelopts': "nopat",
|
|
|
|
'kernelopts_pcidevs': "nopat iommu=soft swiotlb=4096",
|
2011-06-22 00:44:48 +02:00
|
|
|
|
2013-03-15 18:43:09 +01:00
|
|
|
'dom0_update_check_interval': 6*3600,
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2013-03-15 18:43:09 +01:00
|
|
|
# how long (in sec) to wait for VMs to shutdown,
|
|
|
|
# before killing them (when used qvm-run with --wait option),
|
|
|
|
'shutdown_counter_max': 60,
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2013-03-15 18:43:09 +01:00
|
|
|
'vm_default_netmask': "255.255.255.0",
|
|
|
|
|
|
|
|
# Set later
|
|
|
|
'appvm_label': None,
|
|
|
|
'template_label': None,
|
|
|
|
'servicevm_label': None,
|
|
|
|
}
|
|
|
|
|
|
|
|
qubes_max_qid = 254
|
|
|
|
qubes_max_netid = 254
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2013-03-16 14:19:03 +01:00
|
|
|
class QubesException (Exception):
|
|
|
|
pass
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2013-05-17 04:06:29 +02:00
|
|
|
class QubesVMMConnection(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._libvirt_conn = None
|
|
|
|
self._xs = None
|
|
|
|
self._xc = None
|
|
|
|
self._offline_mode = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def offline_mode(self):
|
|
|
|
return self._offline_mode
|
|
|
|
|
|
|
|
@offline_mode.setter
|
|
|
|
def offline_mode(self, value):
|
|
|
|
if not value and self._libvirt_conn is not None:
|
|
|
|
raise QubesException("Cannot change offline mode while already connected")
|
|
|
|
|
|
|
|
self._offline_mode = value
|
|
|
|
|
|
|
|
def _libvirt_error_handler(self, ctx, error):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def init_vmm_connection(self):
|
|
|
|
if self._libvirt_conn is not None:
|
|
|
|
# Already initialized
|
|
|
|
return
|
|
|
|
if self._offline_mode:
|
|
|
|
# Do not initialize in offline mode
|
|
|
|
return
|
|
|
|
|
|
|
|
self._xc = xen.lowlevel.xc.xc()
|
|
|
|
self._xs = xen.lowlevel.xs.xs()
|
|
|
|
self._libvirt_conn = libvirt.open(defaults['libvirt_uri'])
|
|
|
|
if self._libvirt_conn == None:
|
|
|
|
raise QubesException("Failed connect to libvirt driver")
|
|
|
|
libvirt.registerErrorHandler(self._libvirt_error_handler, None)
|
|
|
|
atexit.register(self._libvirt_conn.close)
|
|
|
|
|
|
|
|
def _common_getter(self, name):
|
|
|
|
if self._offline_mode:
|
|
|
|
# Do not initialize in offline mode
|
|
|
|
raise QubesException("VMM operations disabled in offline mode")
|
|
|
|
|
|
|
|
if self._libvirt_conn is None:
|
|
|
|
self.init_vmm_connection()
|
|
|
|
return getattr(self, name)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def libvirt_conn(self):
|
|
|
|
return self._common_getter('_libvirt_conn')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def xs(self):
|
|
|
|
return self._common_getter('_xs')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def xc(self):
|
|
|
|
return self._common_getter('_xc')
|
|
|
|
|
|
|
|
|
|
|
|
##### VMM global variable definition #####
|
2013-05-04 04:45:55 +02:00
|
|
|
|
2011-06-01 23:44:06 +02:00
|
|
|
if not dry_run:
|
2013-05-17 04:06:29 +02:00
|
|
|
vmm = QubesVMMConnection()
|
|
|
|
|
|
|
|
##########################################
|
2010-09-16 17:52:52 +02:00
|
|
|
|
|
|
|
class QubesHost(object):
|
|
|
|
def __init__(self):
|
2013-05-17 04:06:29 +02:00
|
|
|
(model, memory, cpus, mhz, nodes, socket, cores, threads) = vmm.libvirt_conn.getInfo()
|
|
|
|
self.physinfo = vmm.xc.physinfo()
|
2010-09-16 17:52:52 +02:00
|
|
|
|
2013-05-04 04:45:55 +02:00
|
|
|
self._total_mem = long(memory)*1024
|
|
|
|
self._no_cpus = cpus
|
2010-09-16 17:52:52 +02:00
|
|
|
|
|
|
|
# print "QubesHost: total_mem = {0}B".format (self.xen_total_mem)
|
|
|
|
# print "QubesHost: free_mem = {0}".format (self.get_free_xen_memory())
|
|
|
|
# print "QubesHost: total_cpus = {0}".format (self.xen_no_cpus)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def memory_total(self):
|
2013-05-04 04:45:55 +02:00
|
|
|
return self._total_mem
|
2010-09-16 17:52:52 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def no_cpus(self):
|
2013-05-04 04:45:55 +02:00
|
|
|
return self._no_cpus
|
2010-09-16 17:52:52 +02:00
|
|
|
|
2013-05-04 04:45:55 +02:00
|
|
|
# TODO
|
2010-09-16 17:52:52 +02:00
|
|
|
def get_free_xen_memory(self):
|
2011-06-01 23:44:06 +02:00
|
|
|
ret = self.physinfo['free_memory']
|
2010-09-16 17:52:52 +02:00
|
|
|
return long(ret)
|
|
|
|
|
2013-05-04 04:45:55 +02:00
|
|
|
# TODO
|
2013-03-16 14:19:03 +01:00
|
|
|
def measure_cpu_usage(self, previous=None, previous_time = None,
|
|
|
|
wait_time=1):
|
|
|
|
"""measure cpu usage for all domains at once"""
|
2011-06-01 23:44:06 +02:00
|
|
|
if previous is None:
|
|
|
|
previous_time = time.time()
|
|
|
|
previous = {}
|
2013-05-17 04:06:29 +02:00
|
|
|
info = vmm.xc.domain_getinfo(0, qubes_max_qid)
|
2011-06-01 23:44:06 +02:00
|
|
|
for vm in info:
|
|
|
|
previous[vm['domid']] = {}
|
2013-03-16 14:19:03 +01:00
|
|
|
previous[vm['domid']]['cpu_time'] = (
|
|
|
|
vm['cpu_time'] / vm['online_vcpus'])
|
2011-06-01 23:44:06 +02:00
|
|
|
previous[vm['domid']]['cpu_usage'] = 0
|
|
|
|
time.sleep(wait_time)
|
|
|
|
|
|
|
|
current_time = time.time()
|
|
|
|
current = {}
|
2013-05-17 04:06:29 +02:00
|
|
|
info = vmm.xc.domain_getinfo(0, qubes_max_qid)
|
2011-06-01 23:44:06 +02:00
|
|
|
for vm in info:
|
|
|
|
current[vm['domid']] = {}
|
2013-03-16 14:19:03 +01:00
|
|
|
current[vm['domid']]['cpu_time'] = (
|
|
|
|
vm['cpu_time'] / max(vm['online_vcpus'], 1))
|
2011-06-01 23:44:06 +02:00
|
|
|
if vm['domid'] in previous.keys():
|
2013-03-16 14:19:03 +01:00
|
|
|
current[vm['domid']]['cpu_usage'] = (
|
|
|
|
float(current[vm['domid']]['cpu_time'] -
|
|
|
|
previous[vm['domid']]['cpu_time']) /
|
|
|
|
long(1000**3) / (current_time-previous_time) * 100)
|
2011-06-11 20:44:26 +02:00
|
|
|
if current[vm['domid']]['cpu_usage'] < 0:
|
|
|
|
# VM has been rebooted
|
|
|
|
current[vm['domid']]['cpu_usage'] = 0
|
2011-06-01 23:44:06 +02:00
|
|
|
else:
|
|
|
|
current[vm['domid']]['cpu_usage'] = 0
|
|
|
|
|
|
|
|
return (current_time, current)
|
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
class QubesVmLabel(object):
|
2014-07-10 16:17:58 +02:00
|
|
|
def __init__(self, index, color, name, dispvm=False):
|
2010-05-10 15:01:47 +02:00
|
|
|
self.index = index
|
2014-01-10 16:58:46 +01:00
|
|
|
self.color = color
|
2014-08-11 16:34:33 +02:00
|
|
|
self.name = name
|
|
|
|
self.dispvm = dispvm
|
|
|
|
|
2014-07-10 16:17:58 +02:00
|
|
|
self.icon = '{}-{}'.format(('dispvm' if dispvm else 'appvm'), name)
|
|
|
|
|
|
|
|
# self.icon_path is obsolete
|
|
|
|
# use QIcon.fromTheme(label.icon) where applicable
|
2013-03-16 14:19:03 +01:00
|
|
|
self.icon_path = os.path.join(
|
|
|
|
system_path['qubes_icon_dir'], self.icon) + ".png"
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2014-08-11 16:34:33 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return '{}({!r}, {!r}, {!r}, dispvm={!r})'.format(
|
|
|
|
self.__class__.__name__,
|
|
|
|
self.index,
|
|
|
|
self.color,
|
|
|
|
self.name,
|
|
|
|
self.dispvm)
|
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
# Globally defined lables
|
|
|
|
QubesVmLabels = {
|
2014-01-10 16:58:46 +01:00
|
|
|
"red": QubesVmLabel(1, "0xcc0000", "red" ),
|
|
|
|
"orange": QubesVmLabel(2, "0xf57900", "orange" ),
|
|
|
|
"yellow": QubesVmLabel(3, "0xedd400", "yellow" ),
|
|
|
|
"green": QubesVmLabel(4, "0x73d216", "green" ),
|
|
|
|
"gray": QubesVmLabel(5, "0x555753", "gray" ),
|
|
|
|
"blue": QubesVmLabel(6, "0x3465a4", "blue" ),
|
|
|
|
"purple": QubesVmLabel(7, "0x75507b", "purple" ),
|
|
|
|
"black": QubesVmLabel(8, "0x000000", "black" ),
|
2010-04-05 20:58:57 +02:00
|
|
|
}
|
|
|
|
|
2012-03-07 18:50:56 +01:00
|
|
|
QubesDispVmLabels = {
|
2014-07-10 16:17:58 +02:00
|
|
|
"red": QubesVmLabel(1, "0xcc0000", "red", dispvm=True),
|
|
|
|
"orange": QubesVmLabel(2, "0xf57900", "orange", dispvm=True),
|
|
|
|
"yellow": QubesVmLabel(3, "0xedd400", "yellow", dispvm=True),
|
|
|
|
"green": QubesVmLabel(4, "0x73d216", "green", dispvm=True),
|
|
|
|
"gray": QubesVmLabel(5, "0x555753", "gray", dispvm=True),
|
|
|
|
"blue": QubesVmLabel(6, "0x3465a4", "blue", dispvm=True),
|
|
|
|
"purple": QubesVmLabel(7, "0x75507b", "purple", dispvm=True),
|
|
|
|
"black": QubesVmLabel(8, "0x000000", "black", dispvm=True),
|
2012-03-07 18:50:56 +01:00
|
|
|
}
|
|
|
|
|
2013-03-15 18:43:09 +01:00
|
|
|
defaults["appvm_label"] = QubesVmLabels["red"]
|
|
|
|
defaults["template_label"] = QubesVmLabels["black"]
|
|
|
|
defaults["servicevm_label"] = QubesVmLabels["red"]
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2013-01-17 01:18:42 +01:00
|
|
|
QubesVmClasses = {}
|
2013-03-16 02:39:30 +01:00
|
|
|
def register_qubes_vm_class(vm_class):
|
|
|
|
QubesVmClasses[vm_class.__name__] = vm_class
|
|
|
|
# register class as local for this module - to make it easy to import from
|
|
|
|
# other modules
|
|
|
|
setattr(sys.modules[__name__], vm_class.__name__, vm_class)
|
2013-01-17 01:18:42 +01:00
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
class QubesVmCollection(dict):
|
|
|
|
"""
|
|
|
|
A collection of Qubes VMs indexed by Qubes id (qid)
|
|
|
|
"""
|
|
|
|
|
2013-03-15 18:43:09 +01:00
|
|
|
def __init__(self, store_filename=system_path["qubes_store_filename"]):
|
2010-04-05 20:58:57 +02:00
|
|
|
super(QubesVmCollection, self).__init__()
|
|
|
|
self.default_netvm_qid = None
|
2011-02-09 21:21:14 +01:00
|
|
|
self.default_fw_netvm_qid = None
|
2010-04-05 20:58:57 +02:00
|
|
|
self.default_template_qid = None
|
2011-07-15 12:24:27 +02:00
|
|
|
self.default_kernel = None
|
2011-06-22 00:44:48 +02:00
|
|
|
self.updatevm_qid = None
|
2010-06-26 15:00:19 +02:00
|
|
|
self.qubes_store_filename = store_filename
|
2011-10-01 02:52:56 +02:00
|
|
|
self.clockvm_qid = None
|
2013-03-16 14:19:03 +01:00
|
|
|
self.qubes_store_file = None
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2014-08-11 16:34:33 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return '<{} {!r}>'.format(self.__class__.__name__, list(sorted(self.keys())))
|
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
def values(self):
|
|
|
|
for qid in self.keys():
|
|
|
|
yield self[qid]
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
for qid in self.keys():
|
|
|
|
yield (qid, self[qid])
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for qid in sorted(super(QubesVmCollection, self).keys()):
|
|
|
|
yield qid
|
|
|
|
|
|
|
|
keys = __iter__
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
if key not in self:
|
|
|
|
return super(QubesVmCollection, self).__setitem__(key, value)
|
|
|
|
else:
|
|
|
|
assert False, "Attempt to add VM with qid that already exists in the collection!"
|
|
|
|
|
2013-01-17 01:29:32 +01:00
|
|
|
def add_new_vm(self, vm_type, **kwargs):
|
|
|
|
if vm_type not in QubesVmClasses.keys():
|
|
|
|
raise ValueError("Unknown VM type: %s" % vm_type)
|
|
|
|
|
|
|
|
qid = self.get_new_unused_qid()
|
2014-04-18 01:35:12 +02:00
|
|
|
vm_cls = QubesVmClasses[vm_type]
|
|
|
|
if 'template' in kwargs:
|
|
|
|
if not vm_cls.is_template_compatible(kwargs['template']):
|
|
|
|
raise QubesException("Template not compatible with selected "
|
|
|
|
"VM type")
|
|
|
|
|
|
|
|
vm = vm_cls(qid=qid, collection=self, **kwargs)
|
2013-01-17 01:29:32 +01:00
|
|
|
if not self.verify_new_vm(vm):
|
|
|
|
raise QubesException("Wrong VM description!")
|
2013-03-16 14:19:03 +01:00
|
|
|
self[vm.qid] = vm
|
2013-01-17 01:29:32 +01:00
|
|
|
|
|
|
|
# make first created NetVM the default one
|
|
|
|
if self.default_fw_netvm_qid is None and vm.is_netvm():
|
|
|
|
self.set_default_fw_netvm(vm)
|
|
|
|
|
|
|
|
if self.default_netvm_qid is None and vm.is_proxyvm():
|
|
|
|
self.set_default_netvm(vm)
|
|
|
|
|
|
|
|
# make first created TemplateVM the default one
|
|
|
|
if self.default_template_qid is None and vm.is_template():
|
|
|
|
self.set_default_template(vm)
|
|
|
|
|
|
|
|
# make first created ProxyVM the UpdateVM
|
|
|
|
if self.updatevm_qid is None and vm.is_proxyvm():
|
|
|
|
self.set_updatevm_vm(vm)
|
|
|
|
|
|
|
|
# by default ClockVM is the first NetVM
|
|
|
|
if self.clockvm_qid is None and vm.is_netvm():
|
|
|
|
self.set_clockvm_vm(vm)
|
|
|
|
|
|
|
|
return vm
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2012-03-09 11:01:20 +01:00
|
|
|
def add_new_appvm(self, name, template,
|
2010-04-05 20:58:57 +02:00
|
|
|
dir_path = None, conf_file = None,
|
|
|
|
private_img = None,
|
|
|
|
label = None):
|
|
|
|
|
2013-01-17 01:29:32 +01:00
|
|
|
warnings.warn("Call to deprecated function, use add_new_vm instead",
|
|
|
|
DeprecationWarning, stacklevel=2)
|
|
|
|
return self.add_new_vm("QubesAppVm", name=name, template=template,
|
2010-04-05 20:58:57 +02:00
|
|
|
dir_path=dir_path, conf_file=conf_file,
|
|
|
|
private_img=private_img,
|
2012-03-04 21:59:02 +01:00
|
|
|
netvm = self.get_default_netvm(),
|
2011-07-02 18:48:17 +02:00
|
|
|
kernel = self.get_default_kernel(),
|
|
|
|
uses_default_kernel = True,
|
2010-04-05 20:58:57 +02:00
|
|
|
label=label)
|
|
|
|
|
2012-02-24 04:23:27 +01:00
|
|
|
def add_new_hvm(self, name, label = None):
|
|
|
|
|
2013-01-17 01:29:32 +01:00
|
|
|
warnings.warn("Call to deprecated function, use add_new_vm instead",
|
|
|
|
DeprecationWarning, stacklevel=2)
|
|
|
|
return self.add_new_vm("QubesHVm", name=name, label=label)
|
2012-02-24 04:23:27 +01:00
|
|
|
|
2012-03-09 11:01:20 +01:00
|
|
|
def add_new_disposablevm(self, name, template, dispid,
|
2013-01-16 23:49:24 +01:00
|
|
|
label = None, netvm = None):
|
2010-09-21 15:59:22 +02:00
|
|
|
|
2013-01-17 01:29:32 +01:00
|
|
|
warnings.warn("Call to deprecated function, use add_new_vm instead",
|
|
|
|
DeprecationWarning, stacklevel=2)
|
|
|
|
return self.add_new_vm("QubesDisposableVm", name=name, template=template,
|
2013-01-16 23:49:24 +01:00
|
|
|
netvm = netvm,
|
2011-07-09 17:52:47 +02:00
|
|
|
label=label, dispid=dispid)
|
2010-09-21 15:59:22 +02:00
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
def add_new_templatevm(self, name,
|
|
|
|
dir_path = None, conf_file = None,
|
|
|
|
root_img = None, private_img = None,
|
|
|
|
installed_by_rpm = True):
|
|
|
|
|
2013-01-17 01:29:32 +01:00
|
|
|
warnings.warn("Call to deprecated function, use add_new_vm instead",
|
|
|
|
DeprecationWarning, stacklevel=2)
|
|
|
|
return self.add_new_vm("QubesTemplateVm", name=name,
|
2010-04-05 20:58:57 +02:00
|
|
|
dir_path=dir_path, conf_file=conf_file,
|
|
|
|
root_img=root_img, private_img=private_img,
|
|
|
|
installed_by_rpm=installed_by_rpm,
|
2012-03-04 21:59:02 +01:00
|
|
|
netvm = self.get_default_netvm(),
|
2011-07-02 18:48:17 +02:00
|
|
|
kernel = self.get_default_kernel(),
|
|
|
|
uses_default_kernel = True)
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2012-03-09 11:01:20 +01:00
|
|
|
def add_new_netvm(self, name, template,
|
2010-04-05 20:58:57 +02:00
|
|
|
dir_path = None, conf_file = None,
|
2011-03-16 11:28:16 +01:00
|
|
|
private_img = None, installed_by_rpm = False,
|
2012-03-09 11:28:06 +01:00
|
|
|
label = None):
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2013-01-17 01:29:32 +01:00
|
|
|
warnings.warn("Call to deprecated function, use add_new_vm instead",
|
|
|
|
DeprecationWarning, stacklevel=2)
|
|
|
|
return self.add_new_vm("QubesNetVm", name=name, template=template,
|
|
|
|
label=label,
|
2011-03-16 11:28:16 +01:00
|
|
|
private_img=private_img, installed_by_rpm=installed_by_rpm,
|
2011-07-02 18:48:17 +02:00
|
|
|
uses_default_kernel = True,
|
2011-03-11 01:59:56 +01:00
|
|
|
dir_path=dir_path, conf_file=conf_file)
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2012-03-09 11:01:20 +01:00
|
|
|
def add_new_proxyvm(self, name, template,
|
2011-02-09 21:21:14 +01:00
|
|
|
dir_path = None, conf_file = None,
|
2011-03-16 11:28:16 +01:00
|
|
|
private_img = None, installed_by_rpm = False,
|
2012-03-09 11:28:06 +01:00
|
|
|
label = None):
|
2011-02-09 21:21:14 +01:00
|
|
|
|
2013-01-17 01:29:32 +01:00
|
|
|
warnings.warn("Call to deprecated function, use add_new_vm instead",
|
|
|
|
DeprecationWarning, stacklevel=2)
|
|
|
|
return self.add_new_vm("QubesProxyVm", name=name, template=template,
|
|
|
|
label=label,
|
2011-03-16 11:28:16 +01:00
|
|
|
private_img=private_img, installed_by_rpm=installed_by_rpm,
|
2011-02-09 21:21:14 +01:00
|
|
|
dir_path=dir_path, conf_file=conf_file,
|
2011-07-02 18:48:17 +02:00
|
|
|
uses_default_kernel = True,
|
2012-03-04 21:59:02 +01:00
|
|
|
netvm = self.get_default_fw_netvm())
|
2011-02-09 21:21:14 +01:00
|
|
|
|
2012-03-09 11:01:20 +01:00
|
|
|
def set_default_template(self, vm):
|
2014-05-05 05:24:04 +02:00
|
|
|
if vm is None:
|
|
|
|
self.default_template_qid = None
|
|
|
|
else:
|
|
|
|
assert vm.is_template(), "VM {0} is not a TemplateVM!".format(vm.name)
|
|
|
|
self.default_template_qid = vm.qid
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2012-03-09 11:01:20 +01:00
|
|
|
def get_default_template(self):
|
2010-04-05 20:58:57 +02:00
|
|
|
if self.default_template_qid is None:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return self[self.default_template_qid]
|
|
|
|
|
2012-03-04 21:59:02 +01:00
|
|
|
def set_default_netvm(self, vm):
|
2014-05-05 05:24:04 +02:00
|
|
|
if vm is None:
|
|
|
|
self.default_netvm_qid = None
|
|
|
|
else:
|
|
|
|
assert vm.is_netvm(), "VM {0} does not provide network!".format(vm.name)
|
|
|
|
self.default_netvm_qid = vm.qid
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2012-03-04 21:59:02 +01:00
|
|
|
def get_default_netvm(self):
|
2010-04-05 20:58:57 +02:00
|
|
|
if self.default_netvm_qid is None:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return self[self.default_netvm_qid]
|
|
|
|
|
2011-06-30 01:07:47 +02:00
|
|
|
def set_default_kernel(self, kernel):
|
2013-03-16 14:19:03 +01:00
|
|
|
assert os.path.exists(
|
|
|
|
os.path.join(system_path["qubes_kernels_base_dir"], kernel)), \
|
|
|
|
"Kerel {0} not installed!".format(kernel)
|
2011-06-30 01:07:47 +02:00
|
|
|
self.default_kernel = kernel
|
|
|
|
|
|
|
|
def get_default_kernel(self):
|
|
|
|
return self.default_kernel
|
|
|
|
|
2012-03-04 21:59:02 +01:00
|
|
|
def set_default_fw_netvm(self, vm):
|
2014-05-05 05:24:04 +02:00
|
|
|
if vm is None:
|
|
|
|
self.default_fw_netvm_qid = None
|
|
|
|
else:
|
|
|
|
assert vm.is_netvm(), "VM {0} does not provide network!".format(vm.name)
|
|
|
|
self.default_fw_netvm_qid = vm.qid
|
2011-02-09 21:21:14 +01:00
|
|
|
|
2012-03-04 21:59:02 +01:00
|
|
|
def get_default_fw_netvm(self):
|
2011-02-09 21:21:14 +01:00
|
|
|
if self.default_fw_netvm_qid is None:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return self[self.default_fw_netvm_qid]
|
|
|
|
|
2011-06-22 00:44:48 +02:00
|
|
|
def set_updatevm_vm(self, vm):
|
2014-05-05 05:24:04 +02:00
|
|
|
if vm is None:
|
|
|
|
self.updatevm_qid = None
|
|
|
|
else:
|
|
|
|
self.updatevm_qid = vm.qid
|
2011-06-22 00:44:48 +02:00
|
|
|
|
|
|
|
def get_updatevm_vm(self):
|
|
|
|
if self.updatevm_qid is None:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return self[self.updatevm_qid]
|
|
|
|
|
2011-10-01 02:52:56 +02:00
|
|
|
def set_clockvm_vm(self, vm):
|
2014-05-05 05:24:04 +02:00
|
|
|
if vm is None:
|
|
|
|
self.clockvm_qid = None
|
|
|
|
else:
|
|
|
|
self.clockvm_qid = vm.qid
|
2011-10-01 02:52:56 +02:00
|
|
|
|
|
|
|
def get_clockvm_vm(self):
|
|
|
|
if self.clockvm_qid is None:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return self[self.clockvm_qid]
|
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
def get_vm_by_name(self, name):
|
|
|
|
for vm in self.values():
|
|
|
|
if (vm.name == name):
|
|
|
|
return vm
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_qid_by_name(self, name):
|
|
|
|
vm = self.get_vm_by_name(name)
|
|
|
|
return vm.qid if vm is not None else None
|
|
|
|
|
|
|
|
def get_vms_based_on(self, template_qid):
|
|
|
|
vms = set([vm for vm in self.values()
|
2012-03-09 11:01:20 +01:00
|
|
|
if (vm.template and vm.template.qid == template_qid)])
|
2010-04-05 20:58:57 +02:00
|
|
|
return vms
|
2011-03-02 15:00:19 +01:00
|
|
|
|
2011-03-31 01:58:45 +02:00
|
|
|
def get_vms_connected_to(self, netvm_qid):
|
|
|
|
new_vms = [ netvm_qid ]
|
|
|
|
dependend_vms_qid = []
|
|
|
|
|
|
|
|
# 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()
|
2011-04-07 10:42:24 +02:00
|
|
|
for vm in self[cur_vm].connected_vms.values():
|
2011-04-04 19:08:40 +02:00
|
|
|
if vm.qid not in dependend_vms_qid:
|
2011-03-31 01:58:45 +02:00
|
|
|
dependend_vms_qid.append(vm.qid)
|
|
|
|
if vm.is_netvm():
|
|
|
|
new_vms.append(vm.qid)
|
|
|
|
|
|
|
|
vms = [vm for vm in self.values() if vm.qid in dependend_vms_qid]
|
|
|
|
return vms
|
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
def verify_new_vm(self, new_vm):
|
|
|
|
|
|
|
|
# Verify that qid is unique
|
|
|
|
for vm in self.values():
|
|
|
|
if vm.qid == new_vm.qid:
|
2011-10-07 21:40:29 +02:00
|
|
|
print >> sys.stderr, "ERROR: The qid={0} is already used by VM '{1}'!".\
|
2010-04-05 20:58:57 +02:00
|
|
|
format(vm.qid, vm.name)
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Verify that name is unique
|
|
|
|
for vm in self.values():
|
|
|
|
if vm.name == new_vm.name:
|
2013-03-16 14:19:03 +01:00
|
|
|
print >> sys.stderr, \
|
|
|
|
"ERROR: The name={0} is already used by other VM with qid='{1}'!".\
|
2010-04-05 20:58:57 +02:00
|
|
|
format(vm.name, vm.qid)
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_new_unused_qid(self):
|
|
|
|
used_ids = set([vm.qid for vm in self.values()])
|
|
|
|
for id in range (1, qubes_max_qid):
|
|
|
|
if id not in used_ids:
|
|
|
|
return id
|
|
|
|
raise LookupError ("Cannot find unused qid!")
|
|
|
|
|
|
|
|
def get_new_unused_netid(self):
|
|
|
|
used_ids = set([vm.netid for vm in self.values() if vm.is_netvm()])
|
|
|
|
for id in range (1, qubes_max_netid):
|
|
|
|
if id not in used_ids:
|
|
|
|
return id
|
|
|
|
raise LookupError ("Cannot find unused netid!")
|
|
|
|
|
|
|
|
|
|
|
|
def check_if_storage_exists(self):
|
|
|
|
try:
|
2010-06-26 15:00:19 +02:00
|
|
|
f = open (self.qubes_store_filename, 'r')
|
2010-04-05 20:58:57 +02:00
|
|
|
except IOError:
|
|
|
|
return False
|
|
|
|
f.close()
|
|
|
|
return True
|
|
|
|
|
|
|
|
def create_empty_storage(self):
|
2010-06-26 15:00:19 +02:00
|
|
|
self.qubes_store_file = open (self.qubes_store_filename, 'w')
|
2010-04-05 20:58:57 +02:00
|
|
|
self.clear()
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
def lock_db_for_reading(self):
|
2014-10-22 03:53:30 +02:00
|
|
|
# save() would rename the file over qubes.xml, _then_ release lock,
|
|
|
|
# so we need to ensure that the file for which we've got the lock is
|
|
|
|
# still the right file
|
|
|
|
while True:
|
|
|
|
self.qubes_store_file = open (self.qubes_store_filename, 'r')
|
|
|
|
fcntl.lockf(self.qubes_store_file, fcntl.LOCK_SH)
|
|
|
|
if os.fstat(self.qubes_store_file.fileno()) == os.stat(
|
|
|
|
self.qubes_store_filename):
|
|
|
|
break
|
|
|
|
self.qubes_store_file.close()
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
def lock_db_for_writing(self):
|
2014-10-22 03:53:30 +02:00
|
|
|
# save() would rename the file over qubes.xml, _then_ release lock,
|
|
|
|
# so we need to ensure that the file for which we've got the lock is
|
|
|
|
# still the right file
|
|
|
|
while True:
|
|
|
|
self.qubes_store_file = open (self.qubes_store_filename, 'r+')
|
|
|
|
fcntl.lockf(self.qubes_store_file, fcntl.LOCK_EX)
|
|
|
|
if os.fstat(self.qubes_store_file.fileno()) == os.stat(
|
|
|
|
self.qubes_store_filename):
|
|
|
|
break
|
|
|
|
self.qubes_store_file.close()
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
def unlock_db(self):
|
2013-10-02 04:52:18 +02:00
|
|
|
# intentionally do not call explicit unlock to not unlock the file
|
|
|
|
# before all buffers are flushed
|
2010-04-05 20:58:57 +02:00
|
|
|
self.qubes_store_file.close()
|
|
|
|
|
|
|
|
def save(self):
|
2012-10-17 21:26:13 +02:00
|
|
|
root = lxml.etree.Element(
|
2010-04-05 20:58:57 +02:00
|
|
|
"QubesVmCollection",
|
|
|
|
|
|
|
|
default_template=str(self.default_template_qid) \
|
|
|
|
if self.default_template_qid is not None else "None",
|
|
|
|
|
|
|
|
default_netvm=str(self.default_netvm_qid) \
|
2011-03-11 02:12:23 +01:00
|
|
|
if self.default_netvm_qid is not None else "None",
|
2011-03-11 02:09:32 +01:00
|
|
|
|
|
|
|
default_fw_netvm=str(self.default_fw_netvm_qid) \
|
2011-06-22 00:44:48 +02:00
|
|
|
if self.default_fw_netvm_qid is not None else "None",
|
|
|
|
|
|
|
|
updatevm=str(self.updatevm_qid) \
|
2011-06-30 01:07:47 +02:00
|
|
|
if self.updatevm_qid is not None else "None",
|
|
|
|
|
2011-10-01 02:52:56 +02:00
|
|
|
clockvm=str(self.clockvm_qid) \
|
|
|
|
if self.clockvm_qid is not None else "None",
|
|
|
|
|
2011-06-30 01:07:47 +02:00
|
|
|
default_kernel=str(self.default_kernel) \
|
|
|
|
if self.default_kernel is not None else "None",
|
2010-04-05 20:58:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
for vm in self.values():
|
|
|
|
element = vm.create_xml_element()
|
|
|
|
if element is not None:
|
|
|
|
root.append(element)
|
2012-10-17 21:26:13 +02:00
|
|
|
tree = lxml.etree.ElementTree(root)
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
try:
|
2011-03-02 15:00:19 +01:00
|
|
|
|
2013-12-02 03:32:33 +01:00
|
|
|
new_store_file = tempfile.NamedTemporaryFile(prefix=self.qubes_store_filename, delete=False)
|
|
|
|
# XXX: do not get lock on the new file, as in all use cases
|
|
|
|
# unlock_db() is the next operation after save()
|
|
|
|
tree.write(new_store_file, encoding="UTF-8", pretty_print=True)
|
|
|
|
new_store_file.flush()
|
|
|
|
os.chmod(new_store_file.name, 0660)
|
|
|
|
os.chown(new_store_file.name, -1, grp.getgrnam('qubes').gr_gid)
|
|
|
|
os.rename(new_store_file.name, self.qubes_store_filename)
|
|
|
|
self.qubes_store_file.close()
|
|
|
|
self.qubes_store_file = new_store_file
|
2010-04-05 20:58:57 +02:00
|
|
|
except EnvironmentError as err:
|
2011-02-21 18:13:27 +01:00
|
|
|
print("{0}: export error: {1}".format(
|
2010-04-05 20:58:57 +02:00
|
|
|
os.path.basename(sys.argv[0]), err))
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2011-03-17 01:40:15 +01:00
|
|
|
def set_netvm_dependency(self, element):
|
|
|
|
kwargs = {}
|
|
|
|
attr_list = ("qid", "uses_default_netvm", "netvm_qid")
|
|
|
|
|
|
|
|
for attribute in attr_list:
|
|
|
|
kwargs[attribute] = element.get(attribute)
|
|
|
|
|
|
|
|
vm = self[int(kwargs["qid"])]
|
|
|
|
|
|
|
|
if "uses_default_netvm" not in kwargs:
|
|
|
|
vm.uses_default_netvm = True
|
|
|
|
else:
|
2013-03-16 14:19:03 +01:00
|
|
|
vm.uses_default_netvm = (
|
|
|
|
True if kwargs["uses_default_netvm"] == "True" else False)
|
2011-03-17 01:40:15 +01:00
|
|
|
if vm.uses_default_netvm is True:
|
2013-01-27 00:01:23 +01:00
|
|
|
if vm.is_proxyvm():
|
|
|
|
netvm = self.get_default_fw_netvm()
|
|
|
|
else:
|
|
|
|
netvm = self.get_default_netvm()
|
2011-03-17 01:40:15 +01:00
|
|
|
kwargs.pop("netvm_qid")
|
|
|
|
else:
|
|
|
|
if kwargs["netvm_qid"] == "none" or kwargs["netvm_qid"] is None:
|
2012-03-04 21:59:02 +01:00
|
|
|
netvm = None
|
2011-03-17 01:40:15 +01:00
|
|
|
kwargs.pop("netvm_qid")
|
|
|
|
else:
|
|
|
|
netvm_qid = int(kwargs.pop("netvm_qid"))
|
|
|
|
if netvm_qid not in self:
|
2012-03-04 21:59:02 +01:00
|
|
|
netvm = None
|
2011-03-17 01:40:15 +01:00
|
|
|
else:
|
2012-03-04 21:59:02 +01:00
|
|
|
netvm = self[netvm_qid]
|
2011-03-17 01:40:15 +01:00
|
|
|
|
2012-03-09 01:52:28 +01:00
|
|
|
# directly set internal attr to not call setters...
|
|
|
|
vm._netvm = netvm
|
2012-03-04 21:59:02 +01:00
|
|
|
if netvm:
|
|
|
|
netvm.connected_vms[vm.qid] = vm
|
2011-03-17 01:40:15 +01:00
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2013-01-17 01:18:42 +01:00
|
|
|
def load_globals(self, element):
|
2011-03-02 15:00:19 +01:00
|
|
|
default_template = element.get("default_template")
|
2010-04-05 20:58:57 +02:00
|
|
|
self.default_template_qid = int(default_template) \
|
2013-01-17 01:18:42 +01:00
|
|
|
if default_template.lower() != "none" else None
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2011-03-02 15:00:19 +01:00
|
|
|
default_netvm = element.get("default_netvm")
|
2010-04-05 20:58:57 +02:00
|
|
|
if default_netvm is not None:
|
|
|
|
self.default_netvm_qid = int(default_netvm) \
|
|
|
|
if default_netvm != "None" else None
|
2011-03-11 02:09:32 +01:00
|
|
|
#assert self.default_netvm_qid is not None
|
|
|
|
|
2011-10-01 02:55:22 +02:00
|
|
|
default_fw_netvm = element.get("default_fw_netvm")
|
2011-03-11 02:09:32 +01:00
|
|
|
if default_fw_netvm is not None:
|
|
|
|
self.default_fw_netvm_qid = int(default_fw_netvm) \
|
|
|
|
if default_fw_netvm != "None" else None
|
2010-04-05 20:58:57 +02:00
|
|
|
#assert self.default_netvm_qid is not None
|
|
|
|
|
2011-06-22 00:44:48 +02:00
|
|
|
updatevm = element.get("updatevm")
|
|
|
|
if updatevm is not None:
|
|
|
|
self.updatevm_qid = int(updatevm) \
|
|
|
|
if updatevm != "None" else None
|
|
|
|
#assert self.default_netvm_qid is not None
|
|
|
|
|
2011-10-01 02:52:56 +02:00
|
|
|
clockvm = element.get("clockvm")
|
|
|
|
if clockvm is not None:
|
|
|
|
self.clockvm_qid = int(clockvm) \
|
|
|
|
if clockvm != "None" else None
|
|
|
|
|
2011-06-30 01:07:47 +02:00
|
|
|
self.default_kernel = element.get("default_kernel")
|
2011-06-22 00:44:48 +02:00
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2013-01-17 01:18:42 +01:00
|
|
|
def load(self):
|
|
|
|
self.clear()
|
2010-09-21 15:59:22 +02:00
|
|
|
|
2013-01-17 01:18:42 +01:00
|
|
|
try:
|
|
|
|
tree = lxml.etree.parse(self.qubes_store_file)
|
|
|
|
except (EnvironmentError,
|
|
|
|
xml.parsers.expat.ExpatError) as err:
|
|
|
|
print("{0}: import error: {1}".format(
|
|
|
|
os.path.basename(sys.argv[0]), err))
|
|
|
|
return False
|
2010-09-21 15:59:22 +02:00
|
|
|
|
2013-01-17 01:18:42 +01:00
|
|
|
self.load_globals(tree.getroot())
|
|
|
|
|
|
|
|
for (vm_class_name, vm_class) in sorted(QubesVmClasses.items(),
|
|
|
|
key=lambda _x: _x[1].load_order):
|
2013-11-19 18:42:59 +01:00
|
|
|
vms_of_class = tree.findall(vm_class_name)
|
|
|
|
# first non-template based, then template based
|
|
|
|
sorted_vms_of_class = sorted(vms_of_class, key= \
|
|
|
|
lambda x: str(x.get('template_qid')).lower() != "none")
|
|
|
|
for element in sorted_vms_of_class:
|
2013-01-17 01:18:42 +01:00
|
|
|
try:
|
|
|
|
vm = vm_class(xml_element=element, collection=self)
|
|
|
|
self[vm.qid] = vm
|
|
|
|
except (ValueError, LookupError) as err:
|
|
|
|
print("{0}: import error ({1}): {2}".format(
|
|
|
|
os.path.basename(sys.argv[0]), vm_class_name, err))
|
|
|
|
raise
|
|
|
|
return False
|
|
|
|
|
|
|
|
# After importing all VMs, set netvm references, in the same order
|
|
|
|
for (vm_class_name, vm_class) in sorted(QubesVmClasses.items(),
|
|
|
|
key=lambda _x: _x[1].load_order):
|
|
|
|
for element in tree.findall(vm_class_name):
|
|
|
|
try:
|
|
|
|
self.set_netvm_dependency(element)
|
|
|
|
except (ValueError, LookupError) as err:
|
|
|
|
print("{0}: import error2 ({}): {}".format(
|
|
|
|
os.path.basename(sys.argv[0]), vm_class_name, err))
|
|
|
|
return False
|
2010-09-21 15:59:22 +02:00
|
|
|
|
2011-10-01 02:52:56 +02:00
|
|
|
# if there was no clockvm entry in qubes.xml, try to determine default:
|
|
|
|
# root of default NetVM chain
|
2013-01-23 02:04:34 +01:00
|
|
|
if tree.getroot().get("clockvm") is None:
|
2011-10-01 02:52:56 +02:00
|
|
|
if self.default_netvm_qid is not None:
|
|
|
|
clockvm = self[self.default_netvm_qid]
|
|
|
|
# Find root of netvm chain
|
2012-03-04 21:59:02 +01:00
|
|
|
while clockvm.netvm is not None:
|
|
|
|
clockvm = clockvm.netvm
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2011-10-01 02:52:56 +02:00
|
|
|
self.clockvm_qid = clockvm.qid
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2013-03-16 14:19:03 +01:00
|
|
|
# Disable ntpd in ClockVM - to not conflict with ntpdate (both are
|
|
|
|
# using 123/udp port)
|
2011-10-01 02:52:56 +02:00
|
|
|
if self.clockvm_qid is not None:
|
2012-02-01 17:51:04 +01:00
|
|
|
self[self.clockvm_qid].services['ntpd'] = False
|
2013-11-24 23:50:39 +01:00
|
|
|
|
|
|
|
# Add dom0 if wasn't present in qubes.xml
|
|
|
|
if not 0 in self.keys():
|
2013-11-29 03:42:56 +01:00
|
|
|
dom0vm = QubesAdminVm (collection=self)
|
2013-11-24 23:50:39 +01:00
|
|
|
self[dom0vm.qid] = dom0vm
|
|
|
|
|
2011-10-01 02:52:56 +02:00
|
|
|
return True
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2011-12-23 17:08:14 +01:00
|
|
|
def pop(self, qid):
|
|
|
|
if self.default_netvm_qid == qid:
|
|
|
|
self.default_netvm_qid = None
|
|
|
|
if self.default_fw_netvm_qid == qid:
|
|
|
|
self.default_fw_netvm_qid = None
|
|
|
|
if self.clockvm_qid == qid:
|
|
|
|
self.clockvm_qid = None
|
|
|
|
if self.updatevm_qid == qid:
|
|
|
|
self.updatevm_qid = None
|
|
|
|
if self.default_template_qid == qid:
|
|
|
|
self.default_template_qid = None
|
|
|
|
|
|
|
|
return super(QubesVmCollection, self).pop(qid)
|
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
class QubesDaemonPidfile(object):
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
self.path = "/var/run/qubes/" + name + ".pid"
|
|
|
|
|
|
|
|
def create_pidfile(self):
|
|
|
|
f = open (self.path, 'w')
|
|
|
|
f.write(str(os.getpid()))
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def pidfile_exists(self):
|
|
|
|
return os.path.exists(self.path)
|
|
|
|
|
|
|
|
def read_pid(self):
|
|
|
|
f = open (self.path)
|
|
|
|
pid = f.read ().strip()
|
|
|
|
f.close()
|
|
|
|
return int(pid)
|
|
|
|
|
|
|
|
def pidfile_is_stale(self):
|
|
|
|
if not self.pidfile_exists():
|
|
|
|
return False
|
|
|
|
|
|
|
|
# check if the pid file is valid...
|
|
|
|
proc_path = "/proc/" + str(self.read_pid()) + "/cmdline"
|
|
|
|
if not os.path.exists (proc_path):
|
2013-03-16 14:19:03 +01:00
|
|
|
print >> sys.stderr, \
|
|
|
|
"Path {0} doesn't exist, assuming stale pidfile.".\
|
|
|
|
format(proc_path)
|
2010-04-05 20:58:57 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
return False # It's a good pidfile
|
|
|
|
|
|
|
|
def remove_pidfile(self):
|
|
|
|
os.remove (self.path)
|
|
|
|
|
|
|
|
def __enter__ (self):
|
|
|
|
# assumes the pidfile doesn't exist -- you should ensure it before opening the context
|
|
|
|
self.create_pidfile()
|
2013-02-14 19:04:05 +01:00
|
|
|
|
|
|
|
def __exit__ (self, exc_type, exc_val, exc_tb):
|
2010-04-05 20:58:57 +02:00
|
|
|
self.remove_pidfile()
|
2013-02-14 19:04:05 +01:00
|
|
|
return False
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2013-03-16 02:39:30 +01:00
|
|
|
modules_dir = os.path.join(os.path.dirname(__file__), 'modules')
|
|
|
|
for module_file in sorted(os.listdir(modules_dir)):
|
|
|
|
if not module_file.endswith(".py") or module_file == "__init__.py":
|
|
|
|
continue
|
|
|
|
__import__('qubes.modules.%s' % module_file[:-3])
|
|
|
|
|
2011-02-27 00:06:46 +01:00
|
|
|
# vim:sw=4:et:
|