2015-01-19 18:03:23 +01:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2013-2015 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
|
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
2015-01-19 18:03:23 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2015-01-19 18:03:23 +01:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2017-10-12 00:11:50 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2015-01-19 18:03:23 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
|
2015-01-19 18:03:23 +01:00
|
|
|
#
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2016-06-16 14:00:53 +02:00
|
|
|
''' This module contains the AdminVM implementation '''
|
2019-04-04 05:16:28 +02:00
|
|
|
import asyncio
|
|
|
|
import subprocess
|
2016-11-02 17:30:27 +01:00
|
|
|
import libvirt
|
2017-09-26 13:24:22 +02:00
|
|
|
|
2015-01-15 16:05:42 +01:00
|
|
|
import qubes
|
2015-10-14 22:02:11 +02:00
|
|
|
import qubes.exc
|
2017-04-01 01:25:57 +02:00
|
|
|
import qubes.vm
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2017-09-26 13:24:22 +02:00
|
|
|
|
2017-04-01 01:25:57 +02:00
|
|
|
class AdminVM(qubes.vm.BaseVM):
|
2014-11-13 18:10:27 +01:00
|
|
|
'''Dom0'''
|
2015-01-15 16:05:42 +01:00
|
|
|
|
2015-09-25 16:50:09 +02:00
|
|
|
dir_path = None
|
|
|
|
|
2017-04-01 01:25:57 +02:00
|
|
|
name = qubes.property('name',
|
|
|
|
default='dom0', setter=qubes.property.forbidden)
|
2015-01-15 16:05:42 +01:00
|
|
|
|
2017-04-01 01:25:57 +02:00
|
|
|
qid = qubes.property('qid',
|
2017-12-21 18:18:12 +01:00
|
|
|
default=0, type=int, setter=qubes.property.forbidden)
|
2016-11-02 17:30:27 +01:00
|
|
|
|
2017-04-01 01:25:57 +02:00
|
|
|
uuid = qubes.property('uuid',
|
|
|
|
default='00000000-0000-0000-0000-000000000000',
|
|
|
|
setter=qubes.property.forbidden)
|
2016-11-02 17:30:27 +01:00
|
|
|
|
2017-08-06 12:44:08 +02:00
|
|
|
default_dispvm = qubes.VMProperty('default_dispvm',
|
|
|
|
load_stage=4,
|
|
|
|
allow_none=True,
|
|
|
|
default=(lambda self: self.app.default_dispvm),
|
|
|
|
doc='Default VM to be used as Disposable VM for service calls.')
|
|
|
|
|
2018-02-22 21:26:41 +01:00
|
|
|
include_in_backups = qubes.property('include_in_backups',
|
|
|
|
default=True, type=bool,
|
|
|
|
doc='If this domain is to be included in default backup.')
|
|
|
|
|
2019-03-26 01:19:34 +01:00
|
|
|
updateable = qubes.property('updateable',
|
|
|
|
default=True,
|
|
|
|
type=bool,
|
|
|
|
setter=qubes.property.forbidden,
|
|
|
|
doc='True if this machine may be updated on its own.')
|
|
|
|
|
2017-06-23 11:09:40 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
self._qdb_connection = None
|
2017-07-03 23:13:23 +02:00
|
|
|
self._libvirt_domain = None
|
2017-06-23 11:09:40 +02:00
|
|
|
|
2017-07-25 01:00:04 +02:00
|
|
|
if not self.app.vmm.offline_mode:
|
2017-09-28 16:12:05 +02:00
|
|
|
self.start_qdb_watch()
|
2017-07-25 01:00:04 +02:00
|
|
|
|
2017-06-12 10:15:13 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2017-06-14 04:01:07 +02:00
|
|
|
def __lt__(self, other):
|
|
|
|
# order dom0 before anything
|
|
|
|
return self.name != other.name
|
|
|
|
|
2016-06-21 17:37:58 +02:00
|
|
|
@property
|
|
|
|
def attached_volumes(self):
|
|
|
|
return []
|
|
|
|
|
2015-01-15 16:05:42 +01:00
|
|
|
@property
|
|
|
|
def xid(self):
|
|
|
|
'''Always ``0``.
|
|
|
|
|
|
|
|
.. seealso:
|
|
|
|
:py:attr:`qubes.vm.qubesvm.QubesVM.xid`
|
|
|
|
'''
|
|
|
|
return 0
|
|
|
|
|
|
|
|
@property
|
|
|
|
def libvirt_domain(self):
|
2017-07-03 23:13:23 +02:00
|
|
|
'''Libvirt object for dom0.
|
2015-01-15 16:05:42 +01:00
|
|
|
|
|
|
|
.. seealso:
|
|
|
|
:py:attr:`qubes.vm.qubesvm.QubesVM.libvirt_domain`
|
|
|
|
'''
|
2017-07-03 23:13:23 +02:00
|
|
|
if self._libvirt_domain is None:
|
|
|
|
self._libvirt_domain = self.app.vmm.libvirt_conn.lookupByID(0)
|
|
|
|
return self._libvirt_domain
|
2015-01-15 16:05:42 +01:00
|
|
|
|
2017-04-01 01:25:57 +02:00
|
|
|
@staticmethod
|
|
|
|
def is_running():
|
2015-01-15 16:05:42 +01:00
|
|
|
'''Always :py:obj:`True`.
|
|
|
|
|
|
|
|
.. seealso:
|
|
|
|
:py:meth:`qubes.vm.qubesvm.QubesVM.is_running`
|
|
|
|
'''
|
|
|
|
return True
|
|
|
|
|
2017-10-20 02:39:44 +02:00
|
|
|
@staticmethod
|
|
|
|
def is_halted():
|
|
|
|
'''Always :py:obj:`False`.
|
|
|
|
|
|
|
|
.. seealso:
|
|
|
|
:py:meth:`qubes.vm.qubesvm.QubesVM.is_halted`
|
|
|
|
'''
|
|
|
|
return False
|
|
|
|
|
2017-04-01 01:25:57 +02:00
|
|
|
@staticmethod
|
|
|
|
def get_power_state():
|
2015-01-15 16:05:42 +01:00
|
|
|
'''Always ``'Running'``.
|
|
|
|
|
|
|
|
.. seealso:
|
|
|
|
:py:meth:`qubes.vm.qubesvm.QubesVM.get_power_state`
|
|
|
|
'''
|
|
|
|
return 'Running'
|
|
|
|
|
2017-04-01 01:25:57 +02:00
|
|
|
@staticmethod
|
|
|
|
def get_mem():
|
2015-01-15 16:05:42 +01:00
|
|
|
'''Get current memory usage of Dom0.
|
|
|
|
|
|
|
|
Unit is KiB.
|
|
|
|
|
|
|
|
.. seealso:
|
|
|
|
:py:meth:`qubes.vm.qubesvm.QubesVM.get_mem`
|
|
|
|
'''
|
|
|
|
|
2016-06-16 11:15:38 +02:00
|
|
|
# return psutil.virtual_memory().total/1024
|
2017-05-29 16:42:40 +02:00
|
|
|
with open('/proc/meminfo') as file:
|
|
|
|
for line in file:
|
|
|
|
if line.startswith('MemTotal:'):
|
|
|
|
return int(line.split(':')[1].strip().split()[0])
|
2015-01-15 16:05:42 +01:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def get_mem_static_max(self):
|
|
|
|
'''Get maximum memory available to Dom0.
|
|
|
|
|
|
|
|
.. seealso:
|
|
|
|
:py:meth:`qubes.vm.qubesvm.QubesVM.get_mem_static_max`
|
|
|
|
'''
|
2016-11-02 17:30:27 +01:00
|
|
|
if self.app.vmm.offline_mode:
|
|
|
|
# default value passed on xen cmdline
|
|
|
|
return 4096
|
2018-07-15 23:08:23 +02:00
|
|
|
try:
|
|
|
|
return self.app.vmm.libvirt_conn.getInfo()[1]
|
|
|
|
except libvirt.libvirtError as e:
|
|
|
|
self.log.warning('Failed to get memory limit for dom0: %s', e)
|
|
|
|
return 4096
|
2015-01-15 16:05:42 +01:00
|
|
|
|
|
|
|
def verify_files(self):
|
|
|
|
'''Always :py:obj:`True`
|
|
|
|
|
|
|
|
.. seealso:
|
|
|
|
:py:meth:`qubes.vm.qubesvm.QubesVM.verify_files`
|
2016-06-21 14:21:31 +02:00
|
|
|
''' # pylint: disable=no-self-use
|
2015-01-15 16:05:42 +01:00
|
|
|
return True
|
|
|
|
|
2017-06-01 03:49:57 +02:00
|
|
|
def start(self, start_guid=True, notify_function=None,
|
2017-04-15 23:48:02 +02:00
|
|
|
mem_required=None):
|
2015-01-15 16:05:42 +01:00
|
|
|
'''Always raises an exception.
|
|
|
|
|
|
|
|
.. seealso:
|
|
|
|
:py:meth:`qubes.vm.qubesvm.QubesVM.start`
|
2017-04-21 15:43:46 +02:00
|
|
|
''' # pylint: disable=unused-argument,arguments-differ
|
2015-10-14 22:27:36 +02:00
|
|
|
raise qubes.exc.QubesVMError(self, 'Cannot start Dom0 fake domain!')
|
2015-01-15 16:05:42 +01:00
|
|
|
|
|
|
|
def suspend(self):
|
|
|
|
'''Does nothing.
|
|
|
|
|
|
|
|
.. seealso:
|
|
|
|
:py:meth:`qubes.vm.qubesvm.QubesVM.suspend`
|
|
|
|
'''
|
2016-06-16 14:00:53 +02:00
|
|
|
raise qubes.exc.QubesVMError(self, 'Cannot suspend Dom0 fake domain!')
|
2015-01-15 16:05:42 +01:00
|
|
|
|
2019-02-19 15:25:05 +01:00
|
|
|
def shutdown(self):
|
|
|
|
'''Does nothing.
|
|
|
|
|
|
|
|
.. seealso:
|
|
|
|
:py:meth:`qubes.vm.qubesvm.QubesVM.shutdown`
|
|
|
|
'''
|
|
|
|
raise qubes.exc.QubesVMError(self, 'Cannot shutdown Dom0 fake domain!')
|
|
|
|
|
|
|
|
def kill(self):
|
|
|
|
'''Does nothing.
|
|
|
|
|
|
|
|
.. seealso:
|
|
|
|
:py:meth:`qubes.vm.qubesvm.QubesVM.kill`
|
|
|
|
'''
|
|
|
|
raise qubes.exc.QubesVMError(self, 'Cannot kill Dom0 fake domain!')
|
|
|
|
|
2017-03-10 23:54:57 +01:00
|
|
|
@property
|
|
|
|
def icon_path(self):
|
2018-07-15 23:08:23 +02:00
|
|
|
pass
|
2015-01-15 16:05:42 +01:00
|
|
|
|
2017-06-23 11:09:40 +02:00
|
|
|
@property
|
2017-07-21 23:11:24 +02:00
|
|
|
def untrusted_qdb(self):
|
2017-06-23 11:09:40 +02:00
|
|
|
'''QubesDB handle for this domain.'''
|
|
|
|
if self._qdb_connection is None:
|
|
|
|
import qubesdb # pylint: disable=import-error
|
|
|
|
self._qdb_connection = qubesdb.QubesDB(self.name)
|
|
|
|
return self._qdb_connection
|
|
|
|
|
2019-04-04 05:16:28 +02:00
|
|
|
@asyncio.coroutine
|
|
|
|
def run_service(self, service, source=None, user=None,
|
|
|
|
filter_esc=False, autostart=False, gui=False, **kwargs):
|
|
|
|
'''Run service on this VM
|
|
|
|
|
|
|
|
:param str service: service name
|
|
|
|
:param qubes.vm.qubesvm.QubesVM source: source domain as presented to
|
|
|
|
this VM
|
|
|
|
:param str user: username to run service as
|
|
|
|
:param bool filter_esc: filter escape sequences to protect terminal \
|
|
|
|
emulator
|
|
|
|
:param bool autostart: if :py:obj:`True`, machine will be started if \
|
|
|
|
it is not running
|
|
|
|
:param bool gui: when autostarting, also start gui daemon
|
|
|
|
:rtype: asyncio.subprocess.Process
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
User ``root`` is redefined to ``SYSTEM`` in the Windows agent code
|
|
|
|
'''
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
|
|
|
|
source = 'dom0' if source is None else self.app.domains[source].name
|
|
|
|
|
|
|
|
if filter_esc:
|
|
|
|
raise NotImplementedError(
|
|
|
|
'filter_esc=True not supported on calls to dom0')
|
|
|
|
|
|
|
|
if user is None:
|
|
|
|
user = 'root'
|
|
|
|
|
|
|
|
yield from self.fire_event_async('domain-cmd-pre-run', pre_event=True,
|
|
|
|
start_guid=gui)
|
|
|
|
|
|
|
|
if user != 'root':
|
|
|
|
cmd = ['runuser', '-u', user, '--']
|
|
|
|
else:
|
|
|
|
cmd = []
|
|
|
|
cmd.extend([
|
|
|
|
qubes.config.system_path['qrexec_rpc_multiplexer'],
|
|
|
|
service,
|
|
|
|
source,
|
|
|
|
'name',
|
|
|
|
self.name,
|
|
|
|
])
|
|
|
|
return (yield from asyncio.create_subprocess_exec(
|
|
|
|
*cmd,
|
|
|
|
**kwargs))
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def run_service_for_stdio(self, *args, input=None, **kwargs):
|
|
|
|
'''Run a service, pass an optional input and return (stdout, stderr).
|
|
|
|
|
|
|
|
Raises an exception if return code != 0.
|
|
|
|
|
|
|
|
*args* and *kwargs* are passed verbatim to :py:meth:`run_service`.
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
There are some combinations if stdio-related *kwargs*, which are
|
|
|
|
not filtered for problems originating between the keyboard and the
|
|
|
|
chair.
|
|
|
|
''' # pylint: disable=redefined-builtin
|
|
|
|
|
|
|
|
kwargs.setdefault('stdin', subprocess.PIPE)
|
|
|
|
kwargs.setdefault('stdout', subprocess.PIPE)
|
|
|
|
kwargs.setdefault('stderr', subprocess.PIPE)
|
|
|
|
p = yield from self.run_service(*args, **kwargs)
|
|
|
|
|
|
|
|
# this one is actually a tuple, but there is no need to unpack it
|
|
|
|
stdouterr = yield from p.communicate(input=input)
|
|
|
|
|
|
|
|
if p.returncode:
|
|
|
|
raise subprocess.CalledProcessError(p.returncode,
|
|
|
|
args[0], *stdouterr)
|
|
|
|
|
|
|
|
return stdouterr
|
2017-06-23 11:09:40 +02:00
|
|
|
|
2015-01-15 16:05:42 +01:00
|
|
|
# def __init__(self, **kwargs):
|
2017-11-11 02:36:17 +01:00
|
|
|
# super(QubesAdminVm, self).__init__(qid=0, name="dom0",
|
2015-01-15 16:05:42 +01:00
|
|
|
# dir_path=None,
|
|
|
|
# private_img = None,
|
|
|
|
# template = None,
|
|
|
|
# maxmem = 0,
|
|
|
|
# vcpus = 0,
|
|
|
|
# label = defaults["template_label"],
|
|
|
|
# **kwargs)
|