2017-07-28 15:08:33 +02:00
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2017 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@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.
|
2017-07-28 15:08:33 +02:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2017-07-28 15:08:33 +02: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.
|
2017-07-28 15:08:33 +02: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/>.
|
2017-07-28 15:08:33 +02:00
|
|
|
|
|
|
|
'''Extension responsible for qvm-service framework'''
|
|
|
|
|
|
|
|
import qubes.ext
|
|
|
|
|
|
|
|
class ServicesExtension(qubes.ext.Extension):
|
|
|
|
'''This extension export features with 'service.' prefix to QubesDB in
|
|
|
|
/qubes-service/ tree.
|
|
|
|
'''
|
|
|
|
# pylint: disable=no-self-use
|
|
|
|
@qubes.ext.handler('domain-qdb-create')
|
2017-08-14 02:30:52 +02:00
|
|
|
def on_domain_qdb_create(self, vm, event):
|
2017-07-28 15:08:33 +02:00
|
|
|
'''Actually export features'''
|
2017-08-14 02:30:52 +02:00
|
|
|
# pylint: disable=unused-argument
|
2017-07-28 15:08:33 +02:00
|
|
|
for feature, value in vm.features.items():
|
|
|
|
if not feature.startswith('service.'):
|
|
|
|
continue
|
|
|
|
service = feature[len('service.'):]
|
|
|
|
# forcefully convert to '0' or '1'
|
|
|
|
vm.untrusted_qdb.write('/qubes-service/{}'.format(service),
|
|
|
|
str(int(bool(value))))
|
|
|
|
|
Use maxmem=0 to disable qmemman, add more automation to it
Use maxmem=0 for disabling dynamic memory balance, instead of cryptic
service.meminfo-writer feature. Under the hood, meminfo-writer service
is also set based on maxmem property (directly in qubesdb, not
vm.features dict).
Having this as a property (not "feature"), allow to have sensible
handling of default value. Specifically, disable it automatically if
otherwise it would crash a VM. This is the case for:
- domain with PCI devices (PoD is not supported by Xen then)
- domain without balloon driver and/or meminfo-writer service
The check for the latter is heuristic (assume presence of 'qrexec' also
can indicate balloon driver support), but it is true for currently
supported systems.
This also allows more reliable control of libvirt config: do not set
memory != maxmem, unless qmemman is enabled.
memory != maxmem only makes sense if qmemman for given domain is
enabled. Besides wasting some domain resources for extra page tables
etc, for HVM domains this is harmful, because maxmem-memory difference
is made of Popupate-on-Demand pool, which - when depleted - will kill
the domain. This means domain without balloon driver will die as soon
as will try to use more than initial memory - but without balloon driver
it sees maxmem memory and doesn't know about the lower limit.
Fixes QubesOS/qubes-issues#4135
2018-11-03 05:13:23 +01:00
|
|
|
# always set meminfo-writer according to maxmem
|
|
|
|
vm.untrusted_qdb.write('/qubes-service/meminfo-writer',
|
|
|
|
'1' if vm.maxmem > 0 else '0')
|
|
|
|
|
2018-01-06 15:05:34 +01:00
|
|
|
@qubes.ext.handler('domain-feature-set:*')
|
2017-08-14 02:30:52 +02:00
|
|
|
def on_domain_feature_set(self, vm, event, feature, value, oldvalue=None):
|
2017-07-28 15:08:33 +02:00
|
|
|
'''Update /qubes-service/ QubesDB tree in runtime'''
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
if not vm.is_running():
|
|
|
|
return
|
|
|
|
if not feature.startswith('service.'):
|
|
|
|
return
|
|
|
|
service = feature[len('service.'):]
|
|
|
|
# forcefully convert to '0' or '1'
|
|
|
|
vm.untrusted_qdb.write('/qubes-service/{}'.format(service),
|
|
|
|
str(int(bool(value))))
|
|
|
|
|
2018-01-06 15:05:34 +01:00
|
|
|
@qubes.ext.handler('domain-feature-delete:*')
|
2017-08-14 02:30:52 +02:00
|
|
|
def on_domain_feature_delete(self, vm, event, feature):
|
2017-07-28 15:08:33 +02:00
|
|
|
'''Update /qubes-service/ QubesDB tree in runtime'''
|
2017-08-14 02:30:52 +02:00
|
|
|
# pylint: disable=unused-argument
|
2017-07-28 15:08:33 +02:00
|
|
|
if not vm.is_running():
|
|
|
|
return
|
|
|
|
if not feature.startswith('service.'):
|
|
|
|
return
|
|
|
|
service = feature[len('service.'):]
|
|
|
|
vm.untrusted_qdb.rm('/qubes-service/{}'.format(service))
|
2018-10-18 05:44:08 +02:00
|
|
|
|
|
|
|
@qubes.ext.handler('features-request')
|
|
|
|
def supported_services(self, vm, event, untrusted_features):
|
|
|
|
'''Handle advertisement of supported services'''
|
|
|
|
# pylint: disable=no-self-use,unused-argument
|
|
|
|
|
|
|
|
if getattr(vm, 'template', None):
|
|
|
|
vm.log.warning(
|
|
|
|
'Ignoring qubes.FeaturesRequest from template-based VM')
|
|
|
|
return
|
|
|
|
|
|
|
|
new_supported_services = set()
|
|
|
|
for requested_service in untrusted_features:
|
|
|
|
if not requested_service.startswith('supported-service.'):
|
|
|
|
continue
|
|
|
|
if untrusted_features[requested_service] == '1':
|
|
|
|
# only allow to advertise service as supported, lack of entry
|
|
|
|
# means service is not supported
|
|
|
|
new_supported_services.add(requested_service)
|
|
|
|
del untrusted_features
|
|
|
|
|
|
|
|
# if no service is supported, ignore the whole thing - do not clear
|
|
|
|
# all services in case of empty request (manual or such)
|
|
|
|
if not new_supported_services:
|
|
|
|
return
|
|
|
|
|
|
|
|
old_supported_services = set(
|
|
|
|
feat for feat in vm.features
|
|
|
|
if feat.startswith('supported-service.') and vm.features[feat])
|
|
|
|
|
|
|
|
for feature in new_supported_services.difference(
|
|
|
|
old_supported_services):
|
|
|
|
vm.features[feature] = True
|
|
|
|
|
|
|
|
for feature in old_supported_services.difference(
|
|
|
|
new_supported_services):
|
|
|
|
del vm.features[feature]
|