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-29 12:46:16 +01:00
|
|
|
|
|
|
|
#
|
2015-01-19 18:03:23 +01:00
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
2014-12-29 12:46:16 +01:00
|
|
|
#
|
2015-01-19 18:03:23 +01:00
|
|
|
# Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2013-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.
|
2014-12-29 12:46:16 +01:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2015-01-19 18:03:23 +01:00
|
|
|
# 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-12-29 12:46:16 +01:00
|
|
|
#
|
|
|
|
|
2015-09-17 12:08:03 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2016-01-29 17:56:33 +01:00
|
|
|
import base64
|
2014-12-29 12:46:16 +01:00
|
|
|
import datetime
|
2016-01-21 13:08:56 +01:00
|
|
|
import itertools
|
2014-12-29 12:46:16 +01:00
|
|
|
import os
|
|
|
|
import os.path
|
2016-01-29 17:56:33 +01:00
|
|
|
import pipes
|
2014-12-29 12:46:16 +01:00
|
|
|
import re
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import uuid
|
2015-09-17 12:08:03 +02:00
|
|
|
import warnings
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
import libvirt
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
import qubes
|
2014-12-29 12:46:16 +01:00
|
|
|
import qubes.config
|
2015-10-14 22:02:11 +02:00
|
|
|
import qubes.exc
|
2015-01-16 15:52:01 +01:00
|
|
|
import qubes.storage
|
2014-12-29 12:46:16 +01:00
|
|
|
import qubes.utils
|
2014-11-13 14:38:41 +01:00
|
|
|
import qubes.vm
|
2016-01-21 13:08:56 +01:00
|
|
|
import qubes.vm.mix.net
|
2015-01-23 18:37:40 +01:00
|
|
|
import qubes.tools.qvm_ls
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
qmemman_present = False
|
|
|
|
try:
|
2015-10-05 23:46:25 +02:00
|
|
|
import qubes.qmemman.client
|
2014-12-29 12:46:16 +01:00
|
|
|
qmemman_present = True
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def _setter_qid(self, prop, value):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=unused-argument
|
2015-06-24 15:39:29 +02:00
|
|
|
value = int(value)
|
2015-01-15 12:57:44 +01:00
|
|
|
if not 0 <= value <= qubes.config.max_qid:
|
2014-12-29 12:46:16 +01:00
|
|
|
raise ValueError(
|
2015-01-15 12:57:44 +01:00
|
|
|
'{} value must be between 0 and qubes.config.max_qid'.format(
|
2014-12-29 12:46:16 +01:00
|
|
|
prop.__name__))
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
def _setter_name(self, prop, value):
|
|
|
|
if not isinstance(value, basestring):
|
|
|
|
raise TypeError('{} value must be string, {!r} found'.format(
|
|
|
|
prop.__name__, type(value).__name__))
|
|
|
|
if len(value) > 31:
|
|
|
|
raise ValueError('{} value must be shorter than 32 characters'.format(
|
|
|
|
prop.__name__))
|
2016-03-03 01:05:23 +01:00
|
|
|
|
|
|
|
# this regexp does not contain '+'; if it had it, we should specifically
|
|
|
|
# disallow 'lost+found' #1440
|
2014-12-29 12:46:16 +01:00
|
|
|
if re.match(r"^[a-zA-Z][a-zA-Z0-9_-]*$", value) is None:
|
|
|
|
raise ValueError('{} value contains illegal characters'.format(
|
|
|
|
prop.__name__))
|
|
|
|
if self.is_running():
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesVMNotHaltedError(
|
|
|
|
self, 'Cannot change name of running VM')
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
if self.installed_by_rpm:
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesException('Cannot rename VM installed by RPM '
|
|
|
|
'-- first clone VM and then use yum to remove package.')
|
2014-12-29 12:46:16 +01:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
def _setter_kernel(self, prop, value):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=unused-argument
|
2016-02-10 17:49:22 +01:00
|
|
|
value = str(value)
|
2015-09-17 12:08:03 +02:00
|
|
|
dirname = os.path.join(
|
|
|
|
qubes.config.system_path['qubes_base_dir'],
|
|
|
|
qubes.config.system_path['qubes_kernels_base_dir'],
|
|
|
|
value)
|
|
|
|
if not os.path.exists(dirname):
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesPropertyValueError(self, prop, value,
|
|
|
|
'Kernel {!r} not installed'.format(value))
|
2016-03-03 01:05:23 +01:00
|
|
|
for filename in ('vmlinuz', 'initramfs'):
|
2015-09-17 12:08:03 +02:00
|
|
|
if not os.path.exists(os.path.join(dirname, filename)):
|
2016-02-10 17:49:22 +01:00
|
|
|
raise qubes.exc.QubesPropertyValueError(self, prop, value,
|
2014-12-29 12:46:16 +01:00
|
|
|
'Kernel {!r} not properly installed: missing {!r} file'.format(
|
2015-01-20 14:41:19 +01:00
|
|
|
value, filename))
|
2014-12-29 12:46:16 +01:00
|
|
|
return value
|
|
|
|
|
|
|
|
|
2015-09-17 12:08:03 +02:00
|
|
|
def _setter_label(self, prop, value):
|
2015-10-05 23:46:25 +02:00
|
|
|
# pylint: disable=unused-argument
|
2015-09-17 12:08:03 +02:00
|
|
|
if isinstance(value, qubes.Label):
|
|
|
|
return value
|
2016-02-10 19:26:44 +01:00
|
|
|
if isinstance(value, basestring) and value.startswith('label-'):
|
2015-09-17 12:08:03 +02:00
|
|
|
return self.app.labels[int(value.split('-', 1)[1])]
|
|
|
|
|
|
|
|
return self.app.get_label(value)
|
|
|
|
|
|
|
|
|
2016-01-21 13:08:56 +01:00
|
|
|
class QubesVM(qubes.vm.mix.net.NetVMMixin, qubes.vm.BaseVM):
|
2014-11-13 18:10:27 +01:00
|
|
|
'''Base functionality of Qubes VM shared between all VMs.'''
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-09-25 16:50:09 +02:00
|
|
|
#
|
|
|
|
# per-class properties
|
|
|
|
#
|
|
|
|
|
|
|
|
#: directory in which domains of this class will reside
|
|
|
|
dir_path_prefix = qubes.config.system_path['qubes_appvms_dir']
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
#
|
|
|
|
# properties loaded from XML
|
|
|
|
#
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
label = qubes.property('label',
|
2015-09-17 12:08:03 +02:00
|
|
|
setter=_setter_label,
|
|
|
|
saver=(lambda self, prop, value: 'label-{}'.format(value.index)),
|
2015-09-26 00:13:30 +02:00
|
|
|
ls_width=14,
|
2015-01-23 18:37:40 +01:00
|
|
|
doc='''Colourful label assigned to VM. This is where the colour of the
|
|
|
|
padlock is set.''')
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-09-17 12:08:03 +02:00
|
|
|
# provides_network = qubes.property('provides_network',
|
|
|
|
# type=bool, setter=qubes.property.bool,
|
|
|
|
# doc='`True` if it is NetVM or ProxyVM, false otherwise.')
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2015-09-25 21:36:35 +02:00
|
|
|
qid = qubes.property('qid', type=int, write_once=True,
|
2014-12-29 12:46:16 +01:00
|
|
|
setter=_setter_qid,
|
2016-02-24 01:08:32 +01:00
|
|
|
clone=False,
|
2015-01-23 18:37:40 +01:00
|
|
|
ls_width=3,
|
2015-01-12 16:56:14 +01:00
|
|
|
doc='''Internal, persistent identificator of particular domain. Note
|
|
|
|
this is different from Xen domid.''')
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
name = qubes.property('name', type=str,
|
2016-02-24 01:08:32 +01:00
|
|
|
clone=False,
|
2015-01-23 18:37:40 +01:00
|
|
|
ls_width=31,
|
2014-12-29 12:46:16 +01:00
|
|
|
doc='User-specified name of the domain.')
|
|
|
|
|
2015-09-25 21:36:35 +02:00
|
|
|
uuid = qubes.property('uuid', type=uuid.UUID, write_once=True,
|
2016-02-24 01:08:32 +01:00
|
|
|
clone=False,
|
2015-01-23 18:37:40 +01:00
|
|
|
ls_width=36,
|
2014-12-29 12:46:16 +01:00
|
|
|
doc='UUID from libvirt.')
|
|
|
|
|
|
|
|
# XXX this should be part of qubes.xml
|
|
|
|
firewall_conf = qubes.property('firewall_conf', type=str,
|
|
|
|
default='firewall.xml')
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
installed_by_rpm = qubes.property('installed_by_rpm',
|
|
|
|
type=bool, setter=qubes.property.bool,
|
|
|
|
default=False,
|
2015-01-12 16:56:14 +01:00
|
|
|
doc='''If this domain's image was installed from package tracked by
|
|
|
|
package manager.''')
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
memory = qubes.property('memory', type=int,
|
|
|
|
default=qubes.config.defaults['memory'],
|
2014-12-29 12:46:16 +01:00
|
|
|
doc='Memory currently available for this VM.')
|
|
|
|
|
2016-02-10 17:51:16 +01:00
|
|
|
maxmem = qubes.property('maxmem', type=int,
|
2016-02-10 18:15:16 +01:00
|
|
|
default=(lambda self: self.app.host.memory_total / 1024 / 2),
|
2015-01-12 16:56:14 +01:00
|
|
|
doc='''Maximum amount of memory available for this VM (for the purpose
|
|
|
|
of the memory balancer).''')
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
internal = qubes.property('internal', default=False,
|
|
|
|
type=bool, setter=qubes.property.bool,
|
2015-01-12 16:56:14 +01:00
|
|
|
doc='''Internal VM (not shown in qubes-manager, don't create appmenus
|
|
|
|
entries.''')
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2015-09-17 12:08:03 +02:00
|
|
|
# FIXME self.app.host could not exist - only self.app.vm required by API
|
|
|
|
vcpus = qubes.property('vcpus',
|
2016-02-10 17:49:22 +01:00
|
|
|
type=int,
|
2015-09-17 12:08:03 +02:00
|
|
|
default=(lambda self: self.app.host.no_cpus),
|
2015-01-23 18:37:40 +01:00
|
|
|
ls_width=2,
|
2014-12-29 12:46:16 +01:00
|
|
|
doc='FIXME')
|
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
pool_name = qubes.property('pool_name',
|
|
|
|
default='default',
|
|
|
|
doc='storage pool for this qube devices')
|
|
|
|
|
|
|
|
dir_path = property((lambda self: self.storage.vmdir),
|
|
|
|
doc='Root directory for files related to this domain')
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
# XXX swallowed uses_default_kernel
|
|
|
|
# XXX not applicable to HVM?
|
|
|
|
kernel = qubes.property('kernel', type=str,
|
|
|
|
setter=_setter_kernel,
|
|
|
|
default=(lambda self: self.app.default_kernel),
|
2015-01-23 18:37:40 +01:00
|
|
|
ls_width=12,
|
2014-12-29 12:46:16 +01:00
|
|
|
doc='Kernel used by this domain.')
|
|
|
|
|
|
|
|
# XXX swallowed uses_default_kernelopts
|
|
|
|
# XXX not applicable to HVM?
|
|
|
|
kernelopts = qubes.property('kernelopts', type=str, load_stage=4,
|
2015-01-20 14:09:47 +01:00
|
|
|
default=(lambda self: qubes.config.defaults['kernelopts_pcidevs'] \
|
2015-01-15 12:57:44 +01:00
|
|
|
if len(self.devices['pci']) > 0 \
|
2016-03-03 01:05:23 +01:00
|
|
|
else self.template.kernelopts if hasattr(self, 'template') \
|
2015-01-15 12:57:44 +01:00
|
|
|
else qubes.config.defaults['kernelopts']),
|
2015-01-23 18:37:40 +01:00
|
|
|
ls_width=30,
|
2014-12-29 12:46:16 +01:00
|
|
|
doc='Kernel command line passed to domain.')
|
|
|
|
|
|
|
|
debug = qubes.property('debug', type=bool, default=False,
|
|
|
|
setter=qubes.property.bool,
|
|
|
|
doc='Turns on debugging features.')
|
|
|
|
|
|
|
|
# XXX what this exactly does?
|
|
|
|
# XXX shouldn't this go to standalone VM and TemplateVM, and leave here
|
|
|
|
# only plain property?
|
|
|
|
default_user = qubes.property('default_user', type=str,
|
2015-09-17 12:08:03 +02:00
|
|
|
default=(lambda self: self.template.default_user
|
|
|
|
if hasattr(self, 'template') else 'user'),
|
2015-01-23 18:37:40 +01:00
|
|
|
ls_width=12,
|
2014-12-29 12:46:16 +01:00
|
|
|
doc='FIXME')
|
|
|
|
|
|
|
|
# @property
|
|
|
|
# def default_user(self):
|
|
|
|
# if self.template is not None:
|
|
|
|
# return self.template.default_user
|
|
|
|
# else:
|
|
|
|
# return self._default_user
|
|
|
|
|
|
|
|
qrexec_timeout = qubes.property('qrexec_timeout', type=int, default=60,
|
2015-01-23 18:37:40 +01:00
|
|
|
ls_width=3,
|
2015-01-12 16:56:14 +01:00
|
|
|
doc='''Time in seconds after which qrexec connection attempt is deemed
|
|
|
|
failed. Operating system inside VM should be able to boot in this
|
|
|
|
time.''')
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
autostart = qubes.property('autostart', default=False,
|
|
|
|
type=bool, setter=qubes.property.bool,
|
|
|
|
doc='''Setting this to `True` means that VM should be autostarted on
|
|
|
|
dom0 boot.''')
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
# XXX I don't understand backups
|
2015-01-19 17:06:30 +01:00
|
|
|
include_in_backups = qubes.property('include_in_backups', default=True,
|
|
|
|
type=bool, setter=qubes.property.bool,
|
2014-12-29 12:46:16 +01:00
|
|
|
doc='If this domain is to be included in default backup.')
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
backup_content = qubes.property('backup_content', default=False,
|
|
|
|
type=bool, setter=qubes.property.bool,
|
2014-12-29 12:46:16 +01:00
|
|
|
doc='FIXME')
|
|
|
|
|
|
|
|
backup_size = qubes.property('backup_size', type=int, default=0,
|
|
|
|
doc='FIXME')
|
|
|
|
|
2016-02-10 17:49:22 +01:00
|
|
|
# TODO default=None?
|
2014-12-29 12:46:16 +01:00
|
|
|
backup_path = qubes.property('backup_path', type=str, default='',
|
|
|
|
doc='FIXME')
|
|
|
|
|
|
|
|
# format got changed from %s to str(datetime.datetime)
|
|
|
|
backup_timestamp = qubes.property('backup_timestamp', default=None,
|
2015-01-19 17:06:30 +01:00
|
|
|
setter=(lambda self, prop, value:
|
2016-02-10 17:49:22 +01:00
|
|
|
value if isinstance(value, datetime.datetime) else
|
|
|
|
datetime.datetime.fromtimestamp(int(value))),
|
2014-12-29 12:46:16 +01:00
|
|
|
saver=(lambda self, prop, value: value.strftime('%s')),
|
|
|
|
doc='FIXME')
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# static, class-wide properties
|
|
|
|
#
|
|
|
|
|
|
|
|
# config file should go away to storage/backend class
|
|
|
|
#: template for libvirt config file (XML)
|
|
|
|
config_file_template = qubes.config.system_path["config_template_pv"]
|
|
|
|
|
|
|
|
#
|
|
|
|
# properties not loaded from XML, calculated at run-time
|
|
|
|
#
|
|
|
|
|
2016-02-11 02:52:06 +01:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
# VMM-related
|
|
|
|
|
2015-01-23 18:37:40 +01:00
|
|
|
@qubes.tools.qvm_ls.column(width=3)
|
2014-12-29 12:46:16 +01:00
|
|
|
@property
|
|
|
|
def xid(self):
|
|
|
|
'''Xen ID.
|
|
|
|
|
|
|
|
Or not Xen, but ID.
|
|
|
|
'''
|
|
|
|
|
|
|
|
if self.libvirt_domain is None:
|
|
|
|
return -1
|
2016-03-03 01:05:23 +01:00
|
|
|
try:
|
|
|
|
return self.libvirt_domain.ID()
|
|
|
|
except libvirt.libvirtError as e:
|
|
|
|
if e.get_error_code() == libvirt.VIR_ERR_NO_DOMAIN:
|
|
|
|
return -1
|
|
|
|
else:
|
|
|
|
self.log.exception('libvirt error code: {!r}'.format(
|
|
|
|
e.get_error_code()))
|
|
|
|
raise
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
def libvirt_domain(self):
|
|
|
|
'''Libvirt domain object from libvirt.
|
|
|
|
|
|
|
|
May be :py:obj:`None`, if libvirt knows nothing about this domain.
|
|
|
|
'''
|
|
|
|
|
|
|
|
if self._libvirt_domain is not None:
|
|
|
|
return self._libvirt_domain
|
|
|
|
|
2015-01-16 15:33:03 +01:00
|
|
|
# XXX _update_libvirt_domain?
|
2014-12-29 12:46:16 +01:00
|
|
|
try:
|
2015-09-28 23:34:29 +02:00
|
|
|
self._libvirt_domain = self.app.vmm.libvirt_conn.lookupByUUID(
|
|
|
|
self.uuid.bytes)
|
2014-12-29 12:46:16 +01:00
|
|
|
except libvirt.libvirtError:
|
2015-01-20 14:09:47 +01:00
|
|
|
if self.app.vmm.libvirt_conn.virConnGetLastError()[0] == \
|
2015-01-19 17:06:30 +01:00
|
|
|
libvirt.VIR_ERR_NO_DOMAIN:
|
2014-12-29 12:46:16 +01:00
|
|
|
self._update_libvirt_domain()
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
return self._libvirt_domain
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
def qdb(self):
|
|
|
|
'''QubesDB handle for this domain.'''
|
|
|
|
if self._qdb_connection is None:
|
|
|
|
if self.is_running():
|
2016-03-02 15:35:43 +01:00
|
|
|
import qubesdb
|
|
|
|
self._qdb_connection = qubesdb.QubesDB(self.name)
|
2014-12-29 12:46:16 +01:00
|
|
|
return self._qdb_connection
|
|
|
|
|
|
|
|
|
|
|
|
# XXX this should go to to AppVM?
|
|
|
|
@property
|
|
|
|
def private_img(self):
|
2015-01-19 17:06:30 +01:00
|
|
|
'''Location of private image of the VM (that contains :file:`/rw` \
|
|
|
|
and :file:`/home`).'''
|
2014-12-29 12:46:16 +01:00
|
|
|
return self.storage.private_img
|
|
|
|
|
|
|
|
|
|
|
|
# XXX this should go to to AppVM? or TemplateVM?
|
|
|
|
@property
|
|
|
|
def root_img(self):
|
|
|
|
'''Location of root image.'''
|
|
|
|
return self.storage.root_img
|
|
|
|
|
|
|
|
|
|
|
|
# XXX and this should go to exactly where? DispVM has it.
|
|
|
|
@property
|
|
|
|
def volatile_img(self):
|
|
|
|
'''Volatile image that overlays :py:attr:`root_img`.'''
|
|
|
|
return self.storage.volatile_img
|
|
|
|
|
|
|
|
|
|
|
|
# XXX shouldn't this go elsewhere?
|
|
|
|
@property
|
|
|
|
def updateable(self):
|
|
|
|
'''True if this machine may be updated on its own.'''
|
2015-01-22 11:24:23 +01:00
|
|
|
return not hasattr(self, 'template')
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
@property
|
2016-03-03 01:05:23 +01:00
|
|
|
def icon_path(self):
|
|
|
|
return os.path.join(self.dir_path, 'icon.png')
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
@property
|
2016-03-03 01:05:23 +01:00
|
|
|
def conf_file(self):
|
|
|
|
return os.path.join(self.dir_path, self.name + '.conf')
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
# XXX I don't know what to do with these; probably should be isinstance(...)
|
2015-09-17 12:08:03 +02:00
|
|
|
def is_template(self):
|
|
|
|
warnings.warn('vm.is_template() is deprecated, use isinstance()',
|
|
|
|
DeprecationWarning)
|
2016-03-04 13:03:43 +01:00
|
|
|
import qubes.vm.templatevm
|
2015-09-17 12:08:03 +02:00
|
|
|
return isinstance(self, qubes.vm.templatevm.TemplateVM)
|
|
|
|
|
|
|
|
def is_appvm(self):
|
|
|
|
warnings.warn('vm.is_appvm() is deprecated, use isinstance()',
|
|
|
|
DeprecationWarning)
|
2016-03-04 13:03:43 +01:00
|
|
|
import qubes.vm.appvm
|
2015-09-17 12:08:03 +02:00
|
|
|
return isinstance(self, qubes.vm.appvm.AppVM)
|
|
|
|
|
|
|
|
def is_proxyvm(self):
|
2016-01-29 17:56:33 +01:00
|
|
|
warnings.warn('vm.is_proxyvm() is deprecated',
|
2015-09-17 12:08:03 +02:00
|
|
|
DeprecationWarning)
|
2016-01-29 17:56:33 +01:00
|
|
|
return self.netvm is not None and self.provides_network
|
2015-09-17 12:08:03 +02:00
|
|
|
|
|
|
|
def is_disposablevm(self):
|
|
|
|
warnings.warn('vm.is_disposable() is deprecated, use isinstance()',
|
|
|
|
DeprecationWarning)
|
2016-03-04 13:03:43 +01:00
|
|
|
import qubes.vm.dispvm
|
2015-09-17 12:08:03 +02:00
|
|
|
return isinstance(self, qubes.vm.dispvm.DispVM)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2016-02-08 20:23:51 +01:00
|
|
|
def is_netvm(self):
|
|
|
|
warnings.warn('vm.is_netvm() is deprecated, use isinstance()',
|
|
|
|
DeprecationWarning)
|
|
|
|
return isinstance(self, qubes.vm.mix.net.NetVMMixin) \
|
|
|
|
and self.provides_network
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
# network-related
|
|
|
|
|
2015-09-17 12:08:03 +02:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
#
|
|
|
|
# constructor
|
|
|
|
#
|
|
|
|
|
2015-01-15 12:57:44 +01:00
|
|
|
def __init__(self, app, xml, **kwargs):
|
|
|
|
super(QubesVM, self).__init__(app, xml, **kwargs)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2015-10-05 23:46:25 +02:00
|
|
|
import qubes.vm.adminvm # pylint: disable=redefined-outer-name
|
2015-06-24 12:10:12 +02:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
#Init private attrs
|
|
|
|
|
|
|
|
self._libvirt_domain = None
|
|
|
|
self._qdb_connection = None
|
|
|
|
|
2015-07-08 11:32:41 +02:00
|
|
|
if xml is None:
|
|
|
|
# we are creating new VM and attributes came through kwargs
|
2015-12-29 20:35:04 +01:00
|
|
|
assert hasattr(self, 'qid')
|
|
|
|
assert hasattr(self, 'name')
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
# Linux specific cap: max memory can't scale beyond 10.79*init_mem
|
2015-01-13 15:40:43 +01:00
|
|
|
# see https://groups.google.com/forum/#!topic/qubes-devel/VRqkFj1IOtA
|
2014-12-29 12:46:16 +01:00
|
|
|
if self.maxmem > self.memory * 10:
|
|
|
|
self.maxmem = self.memory * 10
|
|
|
|
|
|
|
|
# By default allow use all VCPUs
|
2015-09-17 12:08:03 +02:00
|
|
|
# if not hasattr(self, 'vcpus') and not self.app.vmm.offline_mode:
|
|
|
|
# self.vcpus = self.app.host.no_cpus
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
if len(self.devices['pci']) > 0:
|
2015-06-24 12:10:12 +02:00
|
|
|
# Force meminfo-writer disabled when VM have PCI devices
|
2016-03-03 13:03:27 +01:00
|
|
|
self.features['meminfo-writer'] = None
|
2015-06-24 12:10:12 +02:00
|
|
|
elif not isinstance(self, qubes.vm.adminvm.AdminVM) \
|
2016-03-03 13:03:27 +01:00
|
|
|
and 'meminfo-writer' not in self.features:
|
2015-06-24 12:10:12 +02:00
|
|
|
# Always set if meminfo-writer should be active or not
|
2016-03-03 13:03:27 +01:00
|
|
|
self.features['meminfo-writer'] = '1'
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
if xml is None:
|
|
|
|
# new qube, disable updates check if requested for new qubes
|
|
|
|
# TODO: when features (#1637) are done, migrate to plugin
|
|
|
|
if not self.app.check_updates_vm:
|
2016-03-03 13:03:27 +01:00
|
|
|
self.features['check-updates'] = None
|
2016-03-03 01:05:23 +01:00
|
|
|
|
2016-02-08 20:27:44 +01:00
|
|
|
# will be initialized after loading all the properties
|
|
|
|
self.storage = None
|
2015-01-16 15:52:01 +01:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
# fire hooks
|
2015-12-29 02:27:46 +01:00
|
|
|
if xml is None:
|
|
|
|
self.events_enabled = True
|
2014-12-29 12:46:16 +01:00
|
|
|
self.fire_event('domain-init')
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# event handlers
|
|
|
|
#
|
|
|
|
|
2015-09-28 23:34:29 +02:00
|
|
|
@qubes.events.handler('domain-init', 'domain-loaded')
|
|
|
|
def on_domain_init_loaded(self, event):
|
2015-10-05 23:46:25 +02:00
|
|
|
# pylint: disable=unused-argument
|
2015-09-28 23:34:29 +02:00
|
|
|
if not hasattr(self, 'uuid'):
|
|
|
|
self.uuid = uuid.uuid4()
|
|
|
|
|
2016-02-08 20:27:44 +01:00
|
|
|
# Initialize VM image storage class
|
|
|
|
self.storage = qubes.storage.get_pool(
|
|
|
|
self.pool_name, self).get_storage()
|
|
|
|
|
2015-09-28 23:34:29 +02:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
@qubes.events.handler('property-set:label')
|
|
|
|
def on_property_set_label(self, event, name, new_label, old_label=None):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=unused-argument
|
2014-12-29 12:46:16 +01:00
|
|
|
if self.icon_path:
|
|
|
|
try:
|
|
|
|
os.remove(self.icon_path)
|
2015-10-05 23:46:25 +02:00
|
|
|
except OSError:
|
2014-12-29 12:46:16 +01:00
|
|
|
pass
|
|
|
|
if hasattr(os, "symlink"):
|
|
|
|
os.symlink(new_label.icon_path, self.icon_path)
|
|
|
|
# FIXME: some os-independent wrapper?
|
|
|
|
subprocess.call(['sudo', 'xdg-icon-resource', 'forceupdate'])
|
|
|
|
else:
|
|
|
|
shutil.copy(new_label.icon_path, self.icon_path)
|
|
|
|
|
|
|
|
|
2015-01-16 15:33:03 +01:00
|
|
|
@qubes.events.handler('property-pre-set:name')
|
|
|
|
def on_property_pre_set_name(self, event, name, newvalue, oldvalue=None):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=unused-argument
|
2016-03-03 01:05:23 +01:00
|
|
|
|
2015-01-08 19:44:14 +01:00
|
|
|
# TODO not self.is_stopped() would be more appropriate
|
|
|
|
if self.is_running():
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesVMNotHaltedError(
|
|
|
|
'Cannot change name of running domain {!r}'.format(oldvalue))
|
2015-01-16 15:33:03 +01:00
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
if self.autostart:
|
|
|
|
subprocess.check_call(['sudo', 'systemctl', '-q', 'disable',
|
|
|
|
'qubes-vm@{}.service'.format(oldvalue)])
|
|
|
|
|
2015-01-16 15:33:03 +01:00
|
|
|
|
|
|
|
@qubes.events.handler('property-set:name')
|
|
|
|
def on_property_set_name(self, event, name, new_name, old_name=None):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=unused-argument
|
2015-09-23 16:25:53 +02:00
|
|
|
self.init_log()
|
|
|
|
|
2015-01-16 15:33:03 +01:00
|
|
|
if self._libvirt_domain is not None:
|
2014-12-29 12:46:16 +01:00
|
|
|
self.libvirt_domain.undefine()
|
2015-01-16 15:33:03 +01:00
|
|
|
self._libvirt_domain = None
|
|
|
|
if self._qdb_connection is not None:
|
2014-12-29 12:46:16 +01:00
|
|
|
self._qdb_connection.close()
|
|
|
|
self._qdb_connection = None
|
|
|
|
|
2015-09-25 16:50:09 +02:00
|
|
|
self.storage.rename(
|
2016-01-29 17:56:33 +01:00
|
|
|
os.path.join(qubes.config.system_path['qubes_base_dir'],
|
|
|
|
self.dir_path_prefix, new_name),
|
|
|
|
os.path.join(qubes.config.system_path['qubes_base_dir'],
|
|
|
|
self.dir_path_prefix, old_name))
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2016-01-29 17:56:33 +01:00
|
|
|
self.storage.rename(
|
|
|
|
os.path.join(self.dir_path, new_name + '.conf'),
|
|
|
|
os.path.join(self.dir_path, old_name + '.conf'))
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
self._update_libvirt_domain()
|
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
if self.autostart:
|
|
|
|
self.autostart = self.autostart
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
@qubes.events.handler('property-pre-set:autostart')
|
2015-01-19 17:06:30 +01:00
|
|
|
def on_property_pre_set_autostart(self, event, prop, name, value,
|
|
|
|
oldvalue=None):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=unused-argument
|
2014-12-29 12:46:16 +01:00
|
|
|
if subprocess.call(['sudo', 'systemctl',
|
|
|
|
('enable' if value else 'disable'),
|
|
|
|
'qubes-vm@{}.service'.format(self.name)]):
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesException(
|
2015-01-20 14:09:47 +01:00
|
|
|
'Failed to set autostart for VM via systemctl')
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
@qubes.events.handler('device-pre-attached:pci')
|
|
|
|
def on_device_pre_attached_pci(self, event, pci):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=unused-argument
|
2015-01-20 14:09:47 +01:00
|
|
|
if not os.path.exists('/sys/bus/pci/devices/0000:{}'.format(pci)):
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesException('Invalid PCI device: {}'.format(pci))
|
2015-01-20 14:09:47 +01:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
if not self.is_running():
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2015-01-13 15:40:43 +01:00
|
|
|
# TODO: libvirt-ise
|
2015-01-19 17:06:30 +01:00
|
|
|
subprocess.check_call(
|
2015-01-20 14:09:47 +01:00
|
|
|
['sudo', qubes.config.system_path['qubes_pciback_cmd'], pci])
|
2015-01-19 17:06:30 +01:00
|
|
|
subprocess.check_call(
|
|
|
|
['sudo', 'xl', 'pci-attach', str(self.xid), pci])
|
2015-12-29 22:04:00 +01:00
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
self.log.exception('Failed to attach PCI device {!r} on the fly,'
|
|
|
|
' changes will be seen after VM restart.'.format(pci), e)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
@qubes.events.handler('device-pre-detached:pci')
|
|
|
|
def on_device_pre_detached_pci(self, event, pci):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=unused-argument
|
2014-12-29 12:46:16 +01:00
|
|
|
if not self.is_running():
|
|
|
|
return
|
|
|
|
|
2015-01-13 15:40:43 +01:00
|
|
|
# TODO: libvirt-ise
|
2014-12-29 12:46:16 +01:00
|
|
|
p = subprocess.Popen(['xl', 'pci-list', str(self.xid)],
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
result = p.communicate()
|
2015-01-19 17:06:30 +01:00
|
|
|
m = re.search(r"^(\d+.\d+)\s+0000:%s$" % pci, result[0],
|
|
|
|
flags=re.MULTILINE)
|
2014-12-29 12:46:16 +01:00
|
|
|
if not m:
|
|
|
|
print >>sys.stderr, "Device %s already detached" % pci
|
|
|
|
return
|
|
|
|
vmdev = m.group(1)
|
|
|
|
try:
|
|
|
|
self.run_service("qubes.DetachPciDevice",
|
2015-01-19 17:06:30 +01:00
|
|
|
user="root", input="00:%s" % vmdev)
|
|
|
|
subprocess.check_call(
|
|
|
|
['sudo', 'xl', 'pci-detach', str(self.xid), pci])
|
2015-12-29 22:04:00 +01:00
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
self.log.exception('Failed to detach PCI device {!r} on the fly,'
|
|
|
|
' changes will be seen after VM restart.'.format(pci), e)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# methods for changing domain state
|
|
|
|
#
|
|
|
|
|
|
|
|
def start(self, preparing_dvm=False, start_guid=True,
|
|
|
|
notify_function=None, mem_required=None):
|
|
|
|
'''Start domain
|
|
|
|
|
|
|
|
:param bool preparing_dvm: FIXME
|
|
|
|
:param bool start_guid: FIXME
|
|
|
|
:param collections.Callable notify_function: FIXME
|
|
|
|
:param int mem_required: FIXME
|
|
|
|
'''
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
# Intentionally not used is_running(): eliminate also "Paused",
|
|
|
|
# "Crashed", "Halting"
|
2015-01-20 14:09:47 +01:00
|
|
|
if self.get_power_state() != 'Halted':
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesVMNotHaltedError(self)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
self.log.info('Starting {}'.format(self.name))
|
|
|
|
|
|
|
|
self.verify_files()
|
|
|
|
|
|
|
|
if self.netvm is not None:
|
|
|
|
if self.netvm.qid != 0:
|
|
|
|
if not self.netvm.is_running():
|
2015-01-19 17:06:30 +01:00
|
|
|
self.netvm.start(start_guid=start_guid,
|
|
|
|
notify_function=notify_function)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
self.storage.prepare_for_vm_startup()
|
|
|
|
self._update_libvirt_domain()
|
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
qmemman_client = self.request_memory(mem_required)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
# Bind pci devices to pciback driver
|
|
|
|
for pci in self.devices['pci']:
|
2016-03-03 01:05:23 +01:00
|
|
|
try:
|
|
|
|
node = self.app.vmm.libvirt_conn.nodeDeviceLookupByName(
|
|
|
|
'pci_0000_' + pci.replace(':', '_').replace('.', '_'))
|
|
|
|
except libvirt.libvirtError as e:
|
|
|
|
if e.get_error_code() == libvirt.VIR_ERR_NO_NODE_DEVICE:
|
|
|
|
raise qubes.exc.QubesException(
|
|
|
|
'PCI device {!r} does not exist (domain {!r})'.format(
|
|
|
|
pci, self.name))
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
try:
|
2015-01-20 14:41:19 +01:00
|
|
|
node.dettach()
|
2016-03-03 01:05:23 +01:00
|
|
|
except libvirt.libvirtError as e:
|
|
|
|
if e.get_error_code() == libvirt.VIR_ERR_INTERNAL_ERROR:
|
|
|
|
# allreaddy dettached
|
2014-12-29 12:46:16 +01:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
|
|
|
self.libvirt_domain.createWithFlags(libvirt.VIR_DOMAIN_START_PAUSED)
|
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
try:
|
|
|
|
if preparing_dvm:
|
2016-03-03 13:03:27 +01:00
|
|
|
self.features['dvm'] = True
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
self.log.info('Setting Qubes DB info for the VM')
|
|
|
|
self.start_qubesdb()
|
|
|
|
self.create_qdb_entries()
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
self.log.info('Updating firewall rules')
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
for vm in self.app.domains:
|
|
|
|
if vm.is_proxyvm() and vm.is_running():
|
|
|
|
vm.write_iptables_xenstore_entry()
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
self.log.warning('Activating the {} VM'.format(self.name))
|
|
|
|
self.libvirt_domain.resume()
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2016-01-29 17:56:33 +01:00
|
|
|
# close() is not really needed, because the descriptor is
|
|
|
|
# close-on-exec anyway, the reason to postpone close() is that
|
|
|
|
# possibly xl is not done constructing the domain after its main
|
|
|
|
# process exits so we close() when we know the domain is up the
|
|
|
|
# successful unpause is some indicator of it
|
2016-03-03 01:05:23 +01:00
|
|
|
if qmemman_client:
|
|
|
|
qmemman_client.close()
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
# if self._start_guid_first and start_guid and not preparing_dvm \
|
|
|
|
# and os.path.exists('/var/run/shm.id'):
|
|
|
|
# self.start_guid()
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
if not preparing_dvm:
|
|
|
|
self.start_qrexec_daemon()
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
if start_guid and not preparing_dvm \
|
|
|
|
and os.path.exists('/var/run/shm.id'):
|
|
|
|
self.start_guid()
|
|
|
|
|
|
|
|
self.fire_event('domain-started',
|
|
|
|
preparing_dvm=preparing_dvm, start_guid=start_guid)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
except: # pylint: disable=bare-except
|
|
|
|
self.force_shutdown()
|
|
|
|
raise
|
2016-01-21 13:08:56 +01:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2016-01-21 13:08:56 +01:00
|
|
|
def shutdown(self, force=False):
|
2014-12-29 12:46:16 +01:00
|
|
|
'''Shutdown domain.
|
|
|
|
|
2015-10-14 22:02:11 +02:00
|
|
|
:raises qubes.exc.QubesVMNotStartedError: \
|
|
|
|
when domain is already shut down.
|
2014-12-29 12:46:16 +01:00
|
|
|
'''
|
|
|
|
|
2015-10-14 22:02:11 +02:00
|
|
|
if not self.is_running(): # TODO not self.is_halted()
|
|
|
|
raise qubes.exc.QubesVMNotStartedError(self)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2016-01-21 13:08:56 +01:00
|
|
|
self.fire_event_pre('pre-domain-shutdown', force=force)
|
2016-03-03 01:05:23 +01:00
|
|
|
|
|
|
|
# try to gracefully detach PCI devices before shutdown, to mitigate
|
|
|
|
# timeouts on forcible detach at domain destroy; if that fails, too bad
|
|
|
|
for pci in self.devices['pci']:
|
|
|
|
try:
|
|
|
|
self.libvirt_domain.detachDevice(self.lvxml_pci_dev(pci))
|
|
|
|
except libvirt.libvirtError as e:
|
|
|
|
self.log.warning(
|
|
|
|
'error while gracefully detaching PCI device ({!r}) during'
|
|
|
|
' shutdown of {!r}; error code: {!r}; continuing'
|
|
|
|
' anyway'.format(pci, self.name, e.get_error_code()),
|
|
|
|
exc_info=1)
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
self.libvirt_domain.shutdown()
|
|
|
|
|
|
|
|
|
2016-01-21 13:08:56 +01:00
|
|
|
def kill(self):
|
2014-12-29 12:46:16 +01:00
|
|
|
'''Forcefuly shutdown (destroy) domain.
|
|
|
|
|
2015-10-14 22:02:11 +02:00
|
|
|
:raises qubes.exc.QubesVMNotStartedError: \
|
|
|
|
when domain is already shut down.
|
2014-12-29 12:46:16 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
if not self.is_running() and not self.is_paused():
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesVMNotStartedError(self)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
self.libvirt_domain.destroy()
|
|
|
|
|
|
|
|
|
2016-01-21 13:08:56 +01:00
|
|
|
def force_shutdown(self, *args, **kwargs):
|
|
|
|
'''Deprecated alias for :py:meth:`kill`'''
|
|
|
|
warnings.warn(
|
|
|
|
'Call to deprecated function force_shutdown(), use kill() instead',
|
|
|
|
DeprecationWarning, stacklevel=2)
|
|
|
|
self.kill(*args, **kwargs)
|
|
|
|
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
def suspend(self):
|
|
|
|
'''Suspend (pause) domain.
|
|
|
|
|
2015-10-14 22:02:11 +02:00
|
|
|
:raises qubes.exc.QubesVMNotRunnignError: \
|
|
|
|
when domain is already shut down.
|
|
|
|
:raises qubes.exc.QubesNotImplemetedError: \
|
|
|
|
when domain has PCI devices attached.
|
2014-12-29 12:46:16 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
if not self.is_running() and not self.is_paused():
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesVMNotRunningError(self)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
if len(self.devices['pci']) > 0:
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesNotImplementedError(
|
|
|
|
'Cannot suspend domain {!r} which has PCI devices attached' \
|
|
|
|
.format(self.name))
|
2014-12-29 12:46:16 +01:00
|
|
|
else:
|
|
|
|
self.libvirt_domain.suspend()
|
|
|
|
|
|
|
|
|
|
|
|
def pause(self):
|
2015-01-19 17:06:30 +01:00
|
|
|
'''Pause (suspend) domain. This currently delegates to \
|
|
|
|
:py:meth:`suspend`.'''
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
if not self.is_running():
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesVMNotRunningError(self)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
self.suspend()
|
|
|
|
|
|
|
|
|
|
|
|
def resume(self):
|
|
|
|
'''Resume suspended domain.
|
|
|
|
|
2015-10-14 22:02:11 +02:00
|
|
|
:raises qubes.exc.QubesVMNotSuspendedError: when machine is not paused
|
|
|
|
:raises qubes.exc.QubesVMError: when machine is suspended
|
2014-12-29 12:46:16 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
if self.get_power_state() == "Suspended":
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesVMError(self,
|
|
|
|
'Cannot resume suspended domain {!r}'.format(self.name))
|
2014-12-29 12:46:16 +01:00
|
|
|
else:
|
|
|
|
self.unpause()
|
|
|
|
|
2015-10-14 22:02:11 +02:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
def unpause(self):
|
|
|
|
'''Resume (unpause) a domain'''
|
|
|
|
if not self.is_paused():
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesVMNotPausedError(self)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
self.libvirt_domain.resume()
|
|
|
|
|
|
|
|
|
|
|
|
def run(self, command, user=None, autostart=False, notify_function=None,
|
|
|
|
passio=False, passio_popen=False, passio_stderr=False,
|
|
|
|
ignore_stderr=False, localcmd=None, wait=False, gui=True,
|
|
|
|
filter_esc=False):
|
|
|
|
'''Run specified command inside domain
|
|
|
|
|
|
|
|
:param str command: the command to be run
|
|
|
|
:param str user: user to run the command as
|
2015-01-19 17:06:30 +01:00
|
|
|
:param bool autostart: if :py:obj:`True`, machine will be started if \
|
|
|
|
it is not running
|
2014-12-29 12:46:16 +01:00
|
|
|
:param collections.Callable notify_function: FIXME, may go away
|
|
|
|
:param bool passio: FIXME
|
2015-01-19 17:06:30 +01:00
|
|
|
:param bool passio_popen: if :py:obj:`True`, \
|
|
|
|
:py:class:`subprocess.Popen` object has connected ``stdin`` and \
|
|
|
|
``stdout``
|
|
|
|
:param bool passio_stderr: if :py:obj:`True`, \
|
|
|
|
:py:class:`subprocess.Popen` has additionaly ``stderr`` connected
|
|
|
|
:param bool ignore_stderr: if :py:obj:`True`, ``stderr`` is connected \
|
|
|
|
to :file:`/dev/null`
|
2014-12-29 12:46:16 +01:00
|
|
|
:param str localcmd: local command to communicate with remote command
|
|
|
|
:param bool wait: if :py:obj:`True`, wait for command completion
|
|
|
|
:param bool gui: when autostarting, also start gui daemon
|
2015-01-19 17:06:30 +01:00
|
|
|
:param bool filter_esc: filter escape sequences to protect terminal \
|
|
|
|
emulator
|
2014-12-29 12:46:16 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
if user is None:
|
|
|
|
user = self.default_user
|
|
|
|
null = None
|
|
|
|
if not self.is_running() and not self.is_paused():
|
|
|
|
if not autostart:
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesVMNotRunningError(self)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2015-10-14 22:02:11 +02:00
|
|
|
if notify_function is not None:
|
|
|
|
notify_function('info',
|
|
|
|
'Starting the {!r} VM...'.format(self.name))
|
|
|
|
self.start(start_guid=gui, notify_function=notify_function)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
if self.is_paused():
|
2015-10-14 22:02:11 +02:00
|
|
|
# XXX what about autostart?
|
|
|
|
raise qubes.exc.QubesVMNotRunningError(
|
|
|
|
self, 'Domain {!r} is paused'.format(self.name))
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
if not self.is_qrexec_running():
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesVMError(
|
|
|
|
self, 'Domain {!r}: qrexec not connected'.format(self.name))
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
if gui and os.getenv("DISPLAY") is not None \
|
|
|
|
and not self.is_guid_running():
|
2015-01-20 16:32:25 +01:00
|
|
|
self.start_guid()
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2015-01-20 14:09:47 +01:00
|
|
|
args = [qubes.config.system_path['qrexec_client_path'],
|
|
|
|
'-d', str(self.name),
|
2015-01-19 17:06:30 +01:00
|
|
|
'{}:{}'.format(user, command)]
|
2014-12-29 12:46:16 +01:00
|
|
|
if localcmd is not None:
|
2015-01-19 17:06:30 +01:00
|
|
|
args += ['-l', localcmd]
|
2014-12-29 12:46:16 +01:00
|
|
|
if filter_esc:
|
2015-01-19 17:06:30 +01:00
|
|
|
args += ['-t']
|
2014-12-29 12:46:16 +01:00
|
|
|
if os.isatty(sys.stderr.fileno()):
|
2015-01-19 17:06:30 +01:00
|
|
|
args += ['-T']
|
2015-01-13 15:40:43 +01:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
call_kwargs = {}
|
2015-12-28 18:14:37 +01:00
|
|
|
if ignore_stderr or not passio:
|
2015-12-29 22:04:00 +01:00
|
|
|
null = open("/dev/null", "r+")
|
2014-12-29 12:46:16 +01:00
|
|
|
call_kwargs['stderr'] = null
|
2015-12-28 18:14:37 +01:00
|
|
|
if not passio:
|
|
|
|
call_kwargs['stdin'] = null
|
|
|
|
call_kwargs['stdout'] = null
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
if passio_popen:
|
2015-01-19 17:06:30 +01:00
|
|
|
popen_kwargs = {'stdout': subprocess.PIPE}
|
2014-12-29 12:46:16 +01:00
|
|
|
popen_kwargs['stdin'] = subprocess.PIPE
|
|
|
|
if passio_stderr:
|
|
|
|
popen_kwargs['stderr'] = subprocess.PIPE
|
|
|
|
else:
|
|
|
|
popen_kwargs['stderr'] = call_kwargs.get('stderr', None)
|
|
|
|
p = subprocess.Popen(args, **popen_kwargs)
|
|
|
|
if null:
|
|
|
|
null.close()
|
|
|
|
return p
|
2015-12-28 18:14:37 +01:00
|
|
|
if not wait and not passio:
|
2014-12-29 12:46:16 +01:00
|
|
|
args += ["-e"]
|
|
|
|
retcode = subprocess.call(args, **call_kwargs)
|
|
|
|
if null:
|
|
|
|
null.close()
|
|
|
|
return retcode
|
|
|
|
|
|
|
|
|
|
|
|
def run_service(self, service, source=None, user=None,
|
2016-03-03 01:05:23 +01:00
|
|
|
passio_popen=False, input=None, localcmd=None, gui=False,
|
|
|
|
wait=True):
|
2014-12-29 12:46:16 +01:00
|
|
|
'''Run service on this VM
|
|
|
|
|
|
|
|
**passio_popen** and **input** are mutually exclusive.
|
|
|
|
|
|
|
|
:param str service: service name
|
2015-01-22 19:25:00 +01:00
|
|
|
:param qubes.vm.qubesvm.QubesVM: source domain as presented to this VM
|
2014-12-29 12:46:16 +01:00
|
|
|
:param str user: username to run service as
|
|
|
|
:param bool passio_popen: passed verbatim to :py:meth:`run`
|
|
|
|
:param str input: string passed as input to service
|
2015-01-19 19:02:28 +01:00
|
|
|
''' # pylint: disable=redefined-builtin
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2016-02-10 17:49:22 +01:00
|
|
|
if len([i for i in (input, passio_popen, localcmd) if i]) > 1:
|
2016-03-03 01:05:23 +01:00
|
|
|
raise ValueError(
|
|
|
|
'input, passio_popen and localcmd cannot be used together')
|
|
|
|
|
|
|
|
if input:
|
|
|
|
localcmd = 'printf %s {}'.format(pipes.quote(input))
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
source = 'dom0' if source is None else self.app.domains[source].name
|
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
return self.run('QUBESRPC {} {}'.format(service, source),
|
|
|
|
localcmd=localcmd, passio_popen=passio_popen, user=user, wait=wait,
|
|
|
|
gui=gui)
|
|
|
|
|
|
|
|
|
|
|
|
def request_memory(self, mem_required=None):
|
|
|
|
# overhead of per-qube/per-vcpu Xen structures,
|
|
|
|
# taken from OpenStack nova/virt/xenapi/driver.py
|
|
|
|
# see https://wiki.openstack.org/wiki/XenServer/Overhead
|
|
|
|
# add an extra MB because Nova rounds up to MBs
|
|
|
|
|
|
|
|
if not qmemman_present:
|
|
|
|
return
|
|
|
|
|
|
|
|
MEM_OVERHEAD_BASE = (3 + 1) * 1024 * 1024
|
2016-01-29 17:56:33 +01:00
|
|
|
MEM_OVERHEAD_PER_VCPU = 3 * 1024 * 1024 / 2
|
2016-03-03 01:05:23 +01:00
|
|
|
|
|
|
|
if mem_required is None:
|
|
|
|
mem_required = int(self.memory) * 1024 * 1024
|
|
|
|
|
|
|
|
qmemman_client = qubes.qmemman.client.QMemmanClient()
|
|
|
|
try:
|
|
|
|
mem_required_with_overhead = mem_required + MEM_OVERHEAD_BASE \
|
|
|
|
+ self.vcpus * MEM_OVERHEAD_PER_VCPU
|
|
|
|
got_memory = qmemman_client.request_memory(
|
|
|
|
mem_required_with_overhead)
|
|
|
|
|
|
|
|
except IOError as e:
|
|
|
|
raise IOError('Failed to connect to qmemman: {!s}'.format(e))
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
if not got_memory:
|
|
|
|
qmemman_client.close()
|
|
|
|
raise qubes.exc.QubesMemoryError(self)
|
|
|
|
|
|
|
|
return qmemman_client
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
2015-01-20 16:32:25 +01:00
|
|
|
def start_guid(self, extra_guid_args=None):
|
2014-12-29 12:46:16 +01:00
|
|
|
'''Launch gui daemon.
|
|
|
|
|
|
|
|
GUI daemon securely displays windows from domain.
|
|
|
|
|
|
|
|
:param list extra_guid_args: Extra argv to pass to :program:`guid`.
|
|
|
|
'''
|
|
|
|
|
|
|
|
self.log.info('Starting gui daemon')
|
|
|
|
|
2015-01-20 14:09:47 +01:00
|
|
|
guid_cmd = [qubes.config.system_path['qubes_guid_path'],
|
|
|
|
'-d', str(self.xid), "-N", self.name,
|
|
|
|
'-c', self.label.color,
|
|
|
|
'-i', self.label.icon_path,
|
|
|
|
'-l', str(self.label.index)]
|
2015-01-20 16:32:25 +01:00
|
|
|
if extra_guid_args is not None:
|
|
|
|
guid_cmd += extra_guid_args
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
if self.debug:
|
|
|
|
guid_cmd += ['-v', '-v']
|
|
|
|
|
|
|
|
# elif not verbose:
|
2016-03-03 01:05:23 +01:00
|
|
|
else:
|
|
|
|
guid_cmd += ['-q']
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
retcode = subprocess.call(guid_cmd)
|
2015-01-19 17:06:30 +01:00
|
|
|
if retcode != 0:
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesVMError(self,
|
|
|
|
'Cannot start qubes-guid for domain {!r}'.format(self.name))
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
self.notify_monitor_layout()
|
2014-12-29 12:46:16 +01:00
|
|
|
self.wait_for_session()
|
|
|
|
|
|
|
|
|
|
|
|
def start_qrexec_daemon(self):
|
|
|
|
'''Start qrexec daemon.
|
|
|
|
|
|
|
|
:raises OSError: when starting fails.
|
|
|
|
'''
|
|
|
|
|
|
|
|
self.log.debug('Starting the qrexec daemon')
|
|
|
|
qrexec_args = [str(self.xid), self.name, self.default_user]
|
|
|
|
if not self.debug:
|
|
|
|
qrexec_args.insert(0, "-q")
|
2015-01-13 17:55:13 +01:00
|
|
|
qrexec_env = os.environ.copy()
|
2014-12-29 12:46:16 +01:00
|
|
|
qrexec_env['QREXEC_STARTUP_TIMEOUT'] = str(self.qrexec_timeout)
|
2015-01-20 14:09:47 +01:00
|
|
|
retcode = subprocess.call(
|
|
|
|
[qubes.config.system_path["qrexec_daemon_path"]] + qrexec_args,
|
|
|
|
env=qrexec_env)
|
2015-01-19 17:06:30 +01:00
|
|
|
if retcode != 0:
|
|
|
|
raise OSError('Cannot execute qrexec-daemon!')
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
def start_qubesdb(self):
|
|
|
|
'''Start QubesDB daemon.
|
|
|
|
|
|
|
|
:raises OSError: when starting fails.
|
|
|
|
'''
|
|
|
|
|
|
|
|
self.log.info('Starting Qubes DB')
|
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
# FIXME #1694 #1241
|
2014-12-29 12:46:16 +01:00
|
|
|
retcode = subprocess.call([
|
2015-01-20 14:09:47 +01:00
|
|
|
qubes.config.system_path["qubesdb_daemon_path"],
|
2014-12-29 12:46:16 +01:00
|
|
|
str(self.xid),
|
|
|
|
self.name])
|
|
|
|
if retcode != 0:
|
2016-03-03 01:05:23 +01:00
|
|
|
raise qubes.exc.QubesException('Cannot execute qubesdb-daemon')
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
def wait_for_session(self):
|
|
|
|
'''Wait until machine finished boot sequence.
|
|
|
|
|
|
|
|
This is done by executing qubes RPC call that checks if dummy system
|
|
|
|
service (which is started late in standard runlevel) is active.
|
|
|
|
'''
|
|
|
|
|
|
|
|
self.log.info('Waiting for qubes-session')
|
|
|
|
|
|
|
|
# Note : User root is redefined to SYSTEM in the Windows agent code
|
|
|
|
p = self.run('QUBESRPC qubes.WaitForSession none',
|
|
|
|
user="root", passio_popen=True, gui=False, wait=True)
|
|
|
|
p.communicate(input=self.default_user)
|
|
|
|
|
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
# TODO event, extension
|
|
|
|
def notify_monitor_layout(self):
|
2016-02-09 00:26:03 +01:00
|
|
|
try:
|
|
|
|
import qubes.monitorlayoutnotify
|
|
|
|
monitor_layout = qubes.monitorlayoutnotify.get_monitor_layout()
|
|
|
|
|
|
|
|
# notify qube only if we've got a non-empty monitor_layout or else we
|
|
|
|
# break proper qube resolution set by gui-agent
|
|
|
|
if not monitor_layout:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.log.info('Sending monitor layout')
|
|
|
|
qubes.monitorlayoutnotify.notify_vm(self, monitor_layout)
|
|
|
|
except ImportError:
|
|
|
|
self.log.warning('Monitor layout notify module not installed')
|
2016-03-03 01:05:23 +01:00
|
|
|
|
|
|
|
|
2015-10-02 16:02:51 +02:00
|
|
|
# TODO move to storage
|
2014-12-29 12:46:16 +01:00
|
|
|
def create_on_disk(self, source_template=None):
|
|
|
|
'''Create files needed for VM.
|
|
|
|
|
|
|
|
:param qubes.vm.templatevm.TemplateVM source_template: Template to use
|
|
|
|
(if :py:obj:`None`, use domain's own template
|
|
|
|
'''
|
|
|
|
|
2016-02-10 17:52:45 +01:00
|
|
|
if source_template is None and hasattr(self, 'template'):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=no-member
|
2014-12-29 12:46:16 +01:00
|
|
|
source_template = self.template
|
|
|
|
|
2015-01-20 16:32:25 +01:00
|
|
|
self.storage.create_on_disk(source_template)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
self.log.info('Creating icon symlink: {} -> {}'.format(
|
|
|
|
self.icon_path, self.label.icon_path))
|
2014-12-29 12:46:16 +01:00
|
|
|
if hasattr(os, "symlink"):
|
|
|
|
os.symlink(self.label.icon_path, self.icon_path)
|
|
|
|
else:
|
|
|
|
shutil.copy(self.label.icon_path, self.icon_path)
|
|
|
|
|
|
|
|
# fire hooks
|
|
|
|
self.fire_event('domain-created-on-disk', source_template)
|
|
|
|
|
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
# TODO move to storage
|
2014-12-29 12:46:16 +01:00
|
|
|
def resize_private_img(self, size):
|
|
|
|
'''Resize private image.'''
|
|
|
|
|
2015-10-14 22:02:11 +02:00
|
|
|
if size >= self.get_private_img_sz():
|
|
|
|
raise qubes.exc.QubesValueError('Cannot shrink private.img')
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
# resize the image
|
|
|
|
self.storage.resize_private_img(size)
|
|
|
|
|
|
|
|
# and then the filesystem
|
2015-01-16 15:33:03 +01:00
|
|
|
# FIXME move this to qubes.storage.xen.XenVMStorage
|
2014-12-29 12:46:16 +01:00
|
|
|
retcode = 0
|
|
|
|
if self.is_running():
|
2015-01-19 17:06:30 +01:00
|
|
|
retcode = self.run('''
|
|
|
|
while [ "`blockdev --getsize64 /dev/xvdb`" -lt {0} ]; do
|
|
|
|
head /dev/xvdb >/dev/null;
|
|
|
|
sleep 0.2;
|
|
|
|
done;
|
|
|
|
resize2fs /dev/xvdb'''.format(size), user="root", wait=True)
|
2015-10-14 22:02:11 +02:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
if retcode != 0:
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesException('resize2fs failed')
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
# TODO move to storage
|
|
|
|
def resize_root_img(self, size, allow_start=False):
|
|
|
|
if hasattr(self, 'template'):
|
|
|
|
raise qubes.exc.QubesVMError(self,
|
|
|
|
'Cannot resize root.img of template based qube. Resize the'
|
|
|
|
' root.img of the template instead.')
|
|
|
|
|
|
|
|
# TODO self.is_halted
|
|
|
|
if self.is_running():
|
|
|
|
raise qubes.exc.QubesVMNotHaltedError(self,
|
|
|
|
'Cannot resize root.img of a running qube')
|
|
|
|
|
|
|
|
if size < self.get_root_img_sz():
|
|
|
|
raise qubes.exc.QubesValueError(
|
|
|
|
'For your own safety, shrinking of root.img is disabled. If you'
|
|
|
|
' really know what you are doing, use `truncate` manually.')
|
|
|
|
|
|
|
|
with open(self.root_img, 'a+b') as fd:
|
|
|
|
fd.truncate(size)
|
|
|
|
|
|
|
|
if False: #self.hvm:
|
|
|
|
return
|
|
|
|
|
|
|
|
if not allow_start:
|
|
|
|
raise qubes.exc.QubesException(
|
|
|
|
'The qube has to be started to complete the operation, but is'
|
2016-01-29 17:56:33 +01:00
|
|
|
' required not to start. Either run the operation again'
|
|
|
|
' allowing starting of the qube this time, or run resize2fs'
|
|
|
|
' in the qube manually.')
|
2016-03-03 01:05:23 +01:00
|
|
|
|
|
|
|
self.start(start_guid=False)
|
|
|
|
|
|
|
|
# TODO run_service #1695
|
|
|
|
self.run('resize2fs /dev/mapper/dmroot', user='root',
|
|
|
|
wait=True, gui=False)
|
|
|
|
|
|
|
|
self.shutdown()
|
|
|
|
while self.is_running(): #1696
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
def remove_from_disk(self):
|
|
|
|
'''Remove domain remnants from disk.'''
|
|
|
|
self.fire_event('domain-removed-from-disk')
|
|
|
|
self.storage.remove_from_disk()
|
|
|
|
|
|
|
|
|
|
|
|
def clone_disk_files(self, src):
|
|
|
|
'''Clone files from other vm.
|
|
|
|
|
2015-01-22 19:25:00 +01:00
|
|
|
:param qubes.vm.qubesvm.QubesVM src: source VM
|
2014-12-29 12:46:16 +01:00
|
|
|
'''
|
|
|
|
|
2015-10-14 22:02:11 +02:00
|
|
|
if src.is_running(): # XXX what about paused?
|
|
|
|
raise qubes.exc.QubesVMNotHaltedError(
|
|
|
|
self, 'Cannot clone a running domain {!r}'.format(self.name))
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2016-02-10 17:52:45 +01:00
|
|
|
self.storage.clone_disk_files(src)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2015-01-20 14:41:19 +01:00
|
|
|
if src.icon_path is not None \
|
2014-12-29 12:46:16 +01:00
|
|
|
and os.path.exists(src.dir_path) \
|
|
|
|
and self.icon_path is not None:
|
|
|
|
if os.path.islink(src.icon_path):
|
|
|
|
icon_path = os.readlink(src.icon_path)
|
|
|
|
self.log.info(
|
|
|
|
'Creating icon symlink {} -> {}'.format(
|
|
|
|
self.icon_path, icon_path))
|
|
|
|
os.symlink(icon_path, self.icon_path)
|
|
|
|
else:
|
|
|
|
self.log.info(
|
|
|
|
'Copying icon {} -> {}'.format(
|
|
|
|
src.icon_path, self.icon_path))
|
|
|
|
shutil.copy(src.icon_path, self.icon_path)
|
|
|
|
|
|
|
|
# fire hooks
|
|
|
|
self.fire_event('cloned-files', src)
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# methods for querying domain state
|
|
|
|
#
|
|
|
|
|
|
|
|
# state of the machine
|
|
|
|
|
|
|
|
def get_power_state(self):
|
|
|
|
'''Return power state description string.
|
|
|
|
|
|
|
|
Return value may be one of those:
|
|
|
|
|
|
|
|
=============== ========================================================
|
|
|
|
return value meaning
|
|
|
|
=============== ========================================================
|
|
|
|
``'Halted'`` Machine is not active.
|
|
|
|
``'Transient'`` Machine is running, but does not have :program:`guid`
|
|
|
|
or :program:`qrexec` available.
|
|
|
|
``'Running'`` Machine is ready and running.
|
|
|
|
``'Paused'`` Machine is paused (currently not available, see below).
|
|
|
|
``'Suspended'`` Machine is S3-suspended.
|
|
|
|
``'Halting'`` Machine is in process of shutting down.
|
|
|
|
``'Dying'`` Machine crashed and is unusable.
|
|
|
|
``'Crashed'`` Machine crashed and is unusable, probably because of
|
|
|
|
bug in dom0.
|
|
|
|
``'NA'`` Machine is in unknown state (most likely libvirt domain
|
|
|
|
is undefined).
|
|
|
|
=============== ========================================================
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
``Paused`` state is currently unavailable because of missing code in
|
|
|
|
libvirt/xen glue.
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
FIXME: graph below may be incomplete and wrong. Click on method name to
|
|
|
|
see its documentation.
|
|
|
|
|
|
|
|
.. graphviz::
|
|
|
|
|
|
|
|
digraph {
|
|
|
|
node [fontname="sans-serif"];
|
|
|
|
edge [fontname="mono"];
|
|
|
|
|
|
|
|
|
|
|
|
Halted;
|
|
|
|
NA;
|
|
|
|
Dying;
|
|
|
|
Crashed;
|
|
|
|
Transient;
|
|
|
|
Halting;
|
|
|
|
Running;
|
|
|
|
Paused [color=gray75 fontcolor=gray75];
|
|
|
|
Suspended;
|
|
|
|
|
|
|
|
NA -> Halted;
|
|
|
|
Halted -> NA [constraint=false];
|
|
|
|
|
|
|
|
Halted -> Transient
|
|
|
|
[xlabel="start()" URL="#qubes.vm.qubesvm.QubesVM.start"];
|
|
|
|
Transient -> Running;
|
|
|
|
|
|
|
|
Running -> Halting
|
|
|
|
[xlabel="shutdown()"
|
|
|
|
URL="#qubes.vm.qubesvm.QubesVM.shutdown"
|
|
|
|
constraint=false];
|
|
|
|
Halting -> Dying -> Halted [constraint=false];
|
|
|
|
|
|
|
|
/* cosmetic, invisible edges to put rank constraint */
|
|
|
|
Dying -> Halting [style="invis"];
|
|
|
|
Halting -> Transient [style="invis"];
|
|
|
|
|
|
|
|
Running -> Halted
|
|
|
|
[label="force_shutdown()"
|
|
|
|
URL="#qubes.vm.qubesvm.QubesVM.force_shutdown"
|
|
|
|
constraint=false];
|
|
|
|
|
|
|
|
Running -> Crashed [constraint=false];
|
|
|
|
Crashed -> Halted [constraint=false];
|
|
|
|
|
|
|
|
Running -> Paused
|
|
|
|
[label="pause()" URL="#qubes.vm.qubesvm.QubesVM.pause"
|
|
|
|
color=gray75 fontcolor=gray75];
|
|
|
|
Running -> Suspended
|
|
|
|
[label="pause()" URL="#qubes.vm.qubesvm.QubesVM.pause"
|
|
|
|
color=gray50 fontcolor=gray50];
|
|
|
|
Paused -> Running
|
|
|
|
[label="unpause()" URL="#qubes.vm.qubesvm.QubesVM.unpause"
|
|
|
|
color=gray75 fontcolor=gray75];
|
|
|
|
Suspended -> Running
|
|
|
|
[label="unpause()" URL="#qubes.vm.qubesvm.QubesVM.unpause"
|
|
|
|
color=gray50 fontcolor=gray50];
|
|
|
|
|
|
|
|
Running -> Suspended
|
|
|
|
[label="suspend()" URL="#qubes.vm.qubesvm.QubesVM.suspend"];
|
|
|
|
Suspended -> Running
|
|
|
|
[label="resume()" URL="#qubes.vm.qubesvm.QubesVM.resume"];
|
|
|
|
|
|
|
|
|
|
|
|
{ rank=source; Halted NA };
|
|
|
|
{ rank=same; Transient Halting };
|
|
|
|
{ rank=same; Crashed Dying };
|
|
|
|
{ rank=sink; Paused Suspended };
|
|
|
|
}
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
.. seealso::
|
2015-01-22 19:25:00 +01:00
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
http://wiki.libvirt.org/page/VM_lifecycle
|
|
|
|
Description of VM life cycle from the point of view of libvirt.
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainState
|
2015-01-22 19:25:00 +01:00
|
|
|
Libvirt's enum describing precise state of a domain.
|
2015-01-19 19:02:28 +01:00
|
|
|
''' # pylint: disable=too-many-return-statements
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
libvirt_domain = self.libvirt_domain
|
|
|
|
if libvirt_domain is None:
|
|
|
|
return 'Halted'
|
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
try:
|
|
|
|
if libvirt_domain.isActive():
|
2016-01-29 17:56:33 +01:00
|
|
|
# pylint: disable=line-too-long
|
2016-03-03 01:05:23 +01:00
|
|
|
if libvirt_domain.state()[0] == libvirt.VIR_DOMAIN_PAUSED:
|
|
|
|
return "Paused"
|
|
|
|
elif libvirt_domain.state()[0] == libvirt.VIR_DOMAIN_CRASHED:
|
|
|
|
return "Crashed"
|
|
|
|
elif libvirt_domain.state()[0] == libvirt.VIR_DOMAIN_SHUTDOWN:
|
|
|
|
return "Halting"
|
|
|
|
elif libvirt_domain.state()[0] == libvirt.VIR_DOMAIN_SHUTOFF:
|
|
|
|
return "Dying"
|
|
|
|
elif libvirt_domain.state()[0] == libvirt.VIR_DOMAIN_PMSUSPENDED:
|
|
|
|
return "Suspended"
|
|
|
|
else:
|
|
|
|
if not self.is_fully_usable():
|
|
|
|
return "Transient"
|
|
|
|
else:
|
|
|
|
return "Running"
|
|
|
|
else:
|
|
|
|
return 'Halted'
|
|
|
|
except libvirt.libvirtError as e:
|
|
|
|
if e.get_error_code() == libvirt.VIR_ERR_NO_DOMAIN:
|
|
|
|
return 'Halted'
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
|
|
|
assert False
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
def is_running(self):
|
|
|
|
'''Check whether this domain is running.
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
:returns: :py:obj:`True` if this domain is started, \
|
|
|
|
:py:obj:`False` otherwise.
|
2014-12-29 12:46:16 +01:00
|
|
|
:rtype: bool
|
|
|
|
'''
|
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
# TODO context manager #1693
|
2014-12-29 12:46:16 +01:00
|
|
|
return self.libvirt_domain and self.libvirt_domain.isActive()
|
|
|
|
|
|
|
|
|
|
|
|
def is_paused(self):
|
|
|
|
'''Check whether this domain is paused.
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
:returns: :py:obj:`True` if this domain is paused, \
|
|
|
|
:py:obj:`False` otherwise.
|
2014-12-29 12:46:16 +01:00
|
|
|
:rtype: bool
|
|
|
|
'''
|
|
|
|
|
|
|
|
return self.libvirt_domain \
|
|
|
|
and self.libvirt_domain.state() == libvirt.VIR_DOMAIN_PAUSED
|
|
|
|
|
|
|
|
|
|
|
|
def is_guid_running(self):
|
|
|
|
'''Check whether gui daemon for this domain is available.
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
:returns: :py:obj:`True` if guid is running, \
|
|
|
|
:py:obj:`False` otherwise.
|
2014-12-29 12:46:16 +01:00
|
|
|
:rtype: bool
|
|
|
|
'''
|
|
|
|
xid = self.xid
|
|
|
|
if xid < 0:
|
|
|
|
return False
|
|
|
|
if not os.path.exists('/var/run/qubes/guid-running.%d' % xid):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def is_qrexec_running(self):
|
|
|
|
'''Check whether qrexec for this domain is available.
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
:returns: :py:obj:`True` if qrexec is running, \
|
|
|
|
:py:obj:`False` otherwise.
|
2014-12-29 12:46:16 +01:00
|
|
|
:rtype: bool
|
|
|
|
'''
|
|
|
|
if self.xid < 0:
|
|
|
|
return False
|
|
|
|
return os.path.exists('/var/run/qubes/qrexec.%s' % self.name)
|
|
|
|
|
|
|
|
|
|
|
|
def is_fully_usable(self):
|
|
|
|
'''Check whether domain is running and sane.
|
|
|
|
|
|
|
|
Currently this checks for running guid and qrexec.
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
:returns: :py:obj:`True` if qrexec is running, \
|
|
|
|
:py:obj:`False` otherwise.
|
2014-12-29 12:46:16 +01:00
|
|
|
:rtype: bool
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Running gui-daemon implies also VM running
|
|
|
|
if not self.is_guid_running():
|
|
|
|
return False
|
|
|
|
if not self.is_qrexec_running():
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
# memory and disk
|
|
|
|
|
|
|
|
def get_mem(self):
|
|
|
|
'''Get current memory usage from VM.
|
|
|
|
|
|
|
|
:returns: Memory usage [FIXME unit].
|
|
|
|
:rtype: FIXME
|
|
|
|
'''
|
|
|
|
|
|
|
|
if self.libvirt_domain is None:
|
|
|
|
return 0
|
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
try:
|
|
|
|
if not self.libvirt_domain.isActive():
|
|
|
|
return 0
|
|
|
|
return self.libvirt_domain.info()[1]
|
|
|
|
|
|
|
|
except libvirt.libvirtError as e:
|
|
|
|
if e.get_error_code() in (
|
|
|
|
# qube no longer exists
|
|
|
|
libvirt.VIR_ERR_NO_DOMAIN,
|
|
|
|
|
|
|
|
# libxl_domain_info failed (race condition from isActive)
|
|
|
|
libvirt.VIR_ERR_INTERNAL_ERROR,
|
|
|
|
):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.log.exception(
|
|
|
|
'libvirt error code: {!r}'.format(e.get_error_code()))
|
|
|
|
raise
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_mem_static_max(self):
|
|
|
|
'''Get maximum memory available to VM.
|
|
|
|
|
|
|
|
:returns: Memory limit [FIXME unit].
|
|
|
|
:rtype: FIXME
|
|
|
|
'''
|
|
|
|
|
|
|
|
if self.libvirt_domain is None:
|
|
|
|
return 0
|
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
try:
|
|
|
|
return self.libvirt_domain.maxMemory()
|
|
|
|
|
|
|
|
except libvirt.libvirtError as e:
|
|
|
|
if e.get_error_code() in (
|
|
|
|
# qube no longer exists
|
|
|
|
libvirt.VIR_ERR_NO_DOMAIN,
|
|
|
|
|
|
|
|
# libxl_domain_info failed (race condition from isActive)
|
|
|
|
libvirt.VIR_ERR_INTERNAL_ERROR,
|
|
|
|
):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.log.exception(
|
|
|
|
'libvirt error code: {!r}'.format(e.get_error_code()))
|
|
|
|
raise
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
def get_cputime(self):
|
2014-12-29 12:46:16 +01:00
|
|
|
'''Get total CPU time burned by this domain since start.
|
|
|
|
|
|
|
|
:returns: CPU time usage [FIXME unit].
|
|
|
|
:rtype: FIXME
|
|
|
|
'''
|
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
if self.libvirt_domain is None:
|
|
|
|
return 0
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
if self.libvirt_domain is None:
|
|
|
|
return 0
|
|
|
|
if not self.libvirt_domain.isActive():
|
|
|
|
return 0
|
|
|
|
|
2016-03-03 01:05:23 +01:00
|
|
|
try:
|
|
|
|
if not self.libvirt_domain.isActive():
|
|
|
|
return 0
|
|
|
|
|
|
|
|
# this does not work, because libvirt
|
|
|
|
# return self.libvirt_domain.getCPUStats(
|
|
|
|
# libvirt.VIR_NODE_CPU_STATS_ALL_CPUS, 0)[0]['cpu_time']/10**9
|
|
|
|
|
|
|
|
return self.libvirt_domain.info()[4]
|
|
|
|
|
|
|
|
except libvirt.libvirtError as e:
|
|
|
|
if e.get_error_code() in (
|
|
|
|
# qube no longer exists
|
|
|
|
libvirt.VIR_ERR_NO_DOMAIN,
|
|
|
|
|
|
|
|
# libxl_domain_info failed (race condition from isActive)
|
|
|
|
libvirt.VIR_ERR_INTERNAL_ERROR,
|
|
|
|
):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.log.exception(
|
|
|
|
'libvirt error code: {!r}'.format(e.get_error_code()))
|
|
|
|
raise
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
# XXX shouldn't this go only to vms that have root image?
|
|
|
|
def get_disk_utilization_root_img(self):
|
|
|
|
'''Get space that is actually ocuppied by :py:attr:`root_img`.
|
|
|
|
|
|
|
|
Root image is a sparse file, so it is probably much less than logical
|
|
|
|
available space.
|
|
|
|
|
|
|
|
:returns: domain's real disk image size [FIXME unit]
|
|
|
|
:rtype: FIXME
|
|
|
|
|
|
|
|
.. seealso:: :py:meth:`get_root_img_sz`
|
|
|
|
'''
|
|
|
|
|
2015-10-05 23:46:25 +02:00
|
|
|
return qubes.storage.get_disk_usage(self.root_img)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
# XXX shouldn't this go only to vms that have root image?
|
|
|
|
def get_root_img_sz(self):
|
|
|
|
'''Get image size of :py:attr:`root_img`.
|
|
|
|
|
|
|
|
Root image is a sparse file, so it is probably much more than ocuppied
|
|
|
|
physical space.
|
|
|
|
|
|
|
|
:returns: domain's virtual disk size [FIXME unit]
|
|
|
|
:rtype: FIXME
|
|
|
|
|
|
|
|
.. seealso:: :py:meth:`get_disk_utilization_root_img`
|
|
|
|
'''
|
|
|
|
|
|
|
|
if not os.path.exists(self.root_img):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
return os.path.getsize(self.root_img)
|
|
|
|
|
|
|
|
|
|
|
|
def get_disk_utilization_private_img(self):
|
|
|
|
'''Get space that is actually ocuppied by :py:attr:`private_img`.
|
|
|
|
|
|
|
|
Private image is a sparse file, so it is probably much less than
|
|
|
|
logical available space.
|
|
|
|
|
|
|
|
:returns: domain's real disk image size [FIXME unit]
|
|
|
|
:rtype: FIXME
|
|
|
|
|
|
|
|
.. seealso:: :py:meth:`get_private_img_sz`
|
2015-01-19 19:02:28 +01:00
|
|
|
''' # pylint: disable=invalid-name
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2015-10-05 23:46:25 +02:00
|
|
|
return qubes.storage.get_disk_usage(self.private_img)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_private_img_sz(self):
|
|
|
|
'''Get image size of :py:attr:`private_img`.
|
|
|
|
|
|
|
|
Private image is a sparse file, so it is probably much more than
|
|
|
|
ocuppied physical space.
|
|
|
|
|
|
|
|
:returns: domain's virtual disk size [FIXME unit]
|
|
|
|
:rtype: FIXME
|
|
|
|
|
|
|
|
.. seealso:: :py:meth:`get_disk_utilization_private_img`
|
|
|
|
'''
|
|
|
|
|
|
|
|
return self.storage.get_private_img_sz()
|
|
|
|
|
|
|
|
|
|
|
|
def get_disk_utilization(self):
|
2015-01-19 17:06:30 +01:00
|
|
|
'''Return total space actually occuppied by all files belonging to \
|
|
|
|
this domain.
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
:returns: domain's total disk usage [FIXME unit]
|
|
|
|
:rtype: FIXME
|
|
|
|
'''
|
|
|
|
|
2015-10-05 23:46:25 +02:00
|
|
|
return qubes.storage.get_disk_usage(self.dir_path)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
2015-10-02 16:02:51 +02:00
|
|
|
# TODO move to storage
|
2014-12-29 12:46:16 +01:00
|
|
|
def verify_files(self):
|
|
|
|
'''Verify that files accessed by this machine are sane.
|
|
|
|
|
|
|
|
On success, returns normally. On failure, raises exception.
|
|
|
|
'''
|
|
|
|
|
|
|
|
self.storage.verify_files()
|
|
|
|
|
2015-10-02 16:02:51 +02:00
|
|
|
if not os.path.exists(
|
|
|
|
os.path.join(self.storage.kernels_dir, 'vmlinuz')):
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesException(
|
|
|
|
'VM kernel does not exist: {0}'.format(
|
|
|
|
os.path.join(self.storage.kernels_dir, 'vmlinuz')))
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2015-10-02 16:02:51 +02:00
|
|
|
if not os.path.exists(
|
|
|
|
os.path.join(self.storage.kernels_dir, 'initramfs')):
|
2015-10-14 22:02:11 +02:00
|
|
|
raise qubes.exc.QubesException(
|
2015-01-20 14:09:47 +01:00
|
|
|
'VM initramfs does not exist: {0}'.format(
|
2015-10-02 16:02:51 +02:00
|
|
|
os.path.join(self.storage.kernels_dir, 'initramfs')))
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
self.fire_event('verify-files')
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
# miscellanous
|
|
|
|
|
|
|
|
def get_start_time(self):
|
|
|
|
'''Tell when machine was started.
|
|
|
|
|
|
|
|
:rtype: datetime.datetime
|
|
|
|
'''
|
|
|
|
if not self.is_running():
|
|
|
|
return None
|
|
|
|
|
|
|
|
# TODO shouldn't this be qubesdb?
|
2015-01-19 17:06:30 +01:00
|
|
|
start_time = self.app.vmm.xs.read('',
|
|
|
|
'/vm/{}/start_time'.format(self.uuid))
|
2014-12-29 12:46:16 +01:00
|
|
|
if start_time != '':
|
|
|
|
return datetime.datetime.fromtimestamp(float(start_time))
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
# XXX this probably should go to AppVM
|
|
|
|
def is_outdated(self):
|
2015-01-19 17:06:30 +01:00
|
|
|
'''Check whether domain needs restart to update root image from \
|
|
|
|
template.
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
:returns: :py:obj:`True` if is outdated, :py:obj:`False` otherwise.
|
|
|
|
:rtype: bool
|
|
|
|
'''
|
2015-10-05 23:46:25 +02:00
|
|
|
# pylint: disable=no-member
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
# Makes sense only on VM based on template
|
|
|
|
if self.template is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not self.is_running():
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not hasattr(self.template, 'rootcow_img'):
|
|
|
|
return False
|
|
|
|
|
|
|
|
rootimg_inode = os.stat(self.template.root_img)
|
|
|
|
try:
|
|
|
|
rootcow_inode = os.stat(self.template.rootcow_img)
|
|
|
|
except OSError:
|
|
|
|
# The only case when rootcow_img doesn't exists is in the middle of
|
|
|
|
# commit_changes, so VM is outdated right now
|
|
|
|
return True
|
|
|
|
|
|
|
|
current_dmdev = "/dev/mapper/snapshot-{0:x}:{1}-{2:x}:{3}".format(
|
|
|
|
rootimg_inode[2], rootimg_inode[1],
|
|
|
|
rootcow_inode[2], rootcow_inode[1])
|
|
|
|
|
|
|
|
# FIXME
|
|
|
|
# 51712 (0xCA00) is xvda
|
|
|
|
# backend node name not available through xenapi :(
|
2015-01-20 14:09:47 +01:00
|
|
|
used_dmdev = self.app.vmm.xs.read('',
|
2015-01-19 17:06:30 +01:00
|
|
|
'/local/domain/0/backend/vbd/{}/51712/node'.format(self.xid))
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
return used_dmdev != current_dmdev
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# helper methods
|
|
|
|
#
|
|
|
|
|
|
|
|
def relative_path(self, path):
|
|
|
|
'''Return path relative to py:attr:`dir_path`.
|
|
|
|
|
|
|
|
:param str path: Path in question.
|
|
|
|
:returns: Relative path.
|
|
|
|
'''
|
|
|
|
|
|
|
|
return os.path.relpath(path, self.dir_path)
|
|
|
|
|
|
|
|
|
|
|
|
def create_qdb_entries(self):
|
|
|
|
'''Create entries in Qubes DB.
|
|
|
|
'''
|
2015-10-05 23:46:25 +02:00
|
|
|
# pylint: disable=no-member
|
|
|
|
|
2016-01-21 13:08:56 +01:00
|
|
|
self.qdb.write('/name', self.name)
|
2016-01-24 02:09:27 +01:00
|
|
|
self.qdb.write('/type', self.__class__.__name__)
|
|
|
|
self.qdb.write('/updateable', str(self.updateable))
|
2016-03-03 01:05:23 +01:00
|
|
|
self.qdb.write('/persistence', 'full' if self.updateable else 'rw-only')
|
2016-01-24 02:09:27 +01:00
|
|
|
self.qdb.write('/debug', str(int(self.debug)))
|
2016-03-03 01:05:23 +01:00
|
|
|
try:
|
|
|
|
self.qdb.write('/template', self.template.name)
|
|
|
|
except AttributeError:
|
|
|
|
self.qdb.write('/template', '')
|
|
|
|
|
|
|
|
self.qdb.write('/random-seed',
|
|
|
|
base64.b64encode(qubes.utils.urandom(64)))
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
if self.provides_network:
|
2016-01-21 13:08:56 +01:00
|
|
|
self.qdb.write('/network-provider/gateway', self.gateway)
|
|
|
|
self.qdb.write('/network-provider/netmask', self.netmask)
|
|
|
|
|
|
|
|
for i, addr in zip(itertools.count(start=1), self.dns):
|
|
|
|
self.qdb.write('/network-provider/dns-{}'.format(i), addr)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
if self.netvm is not None:
|
2016-01-21 13:08:56 +01:00
|
|
|
self.qdb.write('/network/ip', self.ip)
|
|
|
|
self.qdb.write('/network/netmask', self.netvm.netmask)
|
|
|
|
self.qdb.write('/network/gateway', self.netvm.gateway)
|
|
|
|
|
|
|
|
for i, addr in zip(itertools.count(start=1), self.dns):
|
|
|
|
self.qdb.write('/network/dns-{}'.format(i), addr)
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
tzname = qubes.utils.get_timezone()
|
|
|
|
if tzname:
|
2016-01-24 02:09:27 +01:00
|
|
|
self.qdb.write('/timezone', tzname)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2016-03-03 13:03:27 +01:00
|
|
|
for feature, value in self.features.items():
|
|
|
|
self.qdb.write('/features/{0}'.format(feature),
|
|
|
|
str(value) if value else '')
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2016-01-24 02:09:27 +01:00
|
|
|
self.qdb.write('/devices/block', '')
|
|
|
|
self.qdb.write('/devices/usb', '')
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
# TODO: Currently the whole qmemman is quite Xen-specific, so stay with
|
|
|
|
# xenstore for it until decided otherwise
|
|
|
|
if qmemman_present:
|
2015-01-20 14:09:47 +01:00
|
|
|
self.app.vmm.xs.set_permissions('',
|
2015-01-19 17:06:30 +01:00
|
|
|
'/local/domain/{}/memory'.format(self.xid),
|
|
|
|
[{'dom': self.xid}])
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
self.fire_event('qdb-created')
|
|
|
|
|
|
|
|
|
|
|
|
def _update_libvirt_domain(self):
|
|
|
|
'''Re-initialise :py:attr:`libvirt_domain`.'''
|
|
|
|
domain_config = self.create_config_file()
|
|
|
|
try:
|
2015-01-20 14:09:47 +01:00
|
|
|
self._libvirt_domain = self.app.vmm.libvirt_conn.defineXML(
|
|
|
|
domain_config)
|
2016-03-03 01:05:23 +01:00
|
|
|
except libvirt.libvirtError as e:
|
|
|
|
if e.get_error_code() == libvirt.VIR_ERR_OS_TYPE \
|
|
|
|
and e.get_str2() == 'hvm':
|
|
|
|
raise qubes.exc.QubesVMError(self,
|
|
|
|
'HVM qubes are not supported on this machine. '
|
|
|
|
'Check BIOS settings for VT-x/AMD-V extensions.')
|
2014-12-29 12:46:16 +01:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# workshop -- those are to be reworked later
|
|
|
|
#
|
|
|
|
|
|
|
|
def get_prefmem(self):
|
|
|
|
# TODO: qmemman is still xen specific
|
2015-01-20 14:09:47 +01:00
|
|
|
untrusted_meminfo_key = self.app.vmm.xs.read('',
|
|
|
|
'/local/domain/{}/memory/meminfo'.format(self.xid))
|
2015-10-05 23:46:25 +02:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
if untrusted_meminfo_key is None or untrusted_meminfo_key == '':
|
|
|
|
return 0
|
2015-10-05 23:46:25 +02:00
|
|
|
|
|
|
|
domain = qubes.qmemman.DomainState(self.xid)
|
|
|
|
qubes.qmemman.algo.refresh_meminfo_for_domain(
|
|
|
|
domain, untrusted_meminfo_key)
|
|
|
|
domain.memory_maximum = self.get_mem_static_max() * 1024
|
|
|
|
|
|
|
|
return qubes.qmemman.algo.prefmem(domain) / 1024
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# landfill -- those are unneeded
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# attrs = {
|
|
|
|
# XXX probably will be obsoleted by .events_enabled
|
|
|
|
# "_do_not_reset_firewall": { "func": lambda x: False },
|
|
|
|
|
|
|
|
# "_start_guid_first": { "func": lambda x: False },
|
|
|
|
# }
|
|
|
|
|
|
|
|
# this function appears unused
|
|
|
|
# def _cleanup_zombie_domains(self):
|
|
|
|
# """
|
|
|
|
# This function is workaround broken libxl (which leaves not fully
|
|
|
|
# created domain on failure) and vchan on domain crash behaviour
|
|
|
|
# @return: None
|
|
|
|
# """
|
|
|
|
# xc = self.get_xc_dominfo()
|
|
|
|
# if xc and xc['dying'] == 1:
|
|
|
|
# # GUID still running?
|
|
|
|
# guid_pidfile = '/var/run/qubes/guid-running.%d' % xc['domid']
|
|
|
|
# if os.path.exists(guid_pidfile):
|
|
|
|
# guid_pid = open(guid_pidfile).read().strip()
|
|
|
|
# os.kill(int(guid_pid), 15)
|
|
|
|
# # qrexec still running?
|
|
|
|
# if self.is_qrexec_running():
|
|
|
|
# #TODO: kill qrexec daemon
|
|
|
|
# pass
|