manager/qubesmanager/global_settings.py

308 lines
11 KiB
Python
Raw Normal View History

2017-06-25 16:39:30 +02:00
#!/usr/bin/python3
2012-01-31 17:29:00 +01:00
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.com>
# Copyright (C) 2012 Marek Marczykowski <marmarek@mimuw.edu.pl>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program; if not, see <http://www.gnu.org/licenses/>.
2012-01-31 17:29:00 +01:00
#
#
import sys
import os
import os.path
import traceback
from PyQt4 import QtCore, QtGui # pylint: disable=import-error
2012-01-31 17:29:00 +01:00
2017-06-25 16:39:30 +02:00
from qubesadmin import Qubes
from qubesadmin.utils import parse_size, updates_vms_status
2012-01-31 17:29:00 +01:00
from . import ui_globalsettingsdlg # pylint: disable=no-name-in-module
from . import utils
2012-01-31 17:29:00 +01:00
2017-06-25 16:39:30 +02:00
from configparser import ConfigParser
2012-01-31 17:29:00 +01:00
2012-04-14 17:58:29 +02:00
qmemman_config_path = '/etc/qubes/qmemman.conf'
2012-03-15 00:22:06 +01:00
class GlobalSettingsWindow(ui_globalsettingsdlg.Ui_GlobalSettings,
QtGui.QDialog):
2012-01-31 17:29:00 +01:00
2012-03-15 00:22:06 +01:00
def __init__(self, app, qvm_collection, parent=None):
2012-01-31 17:29:00 +01:00
super(GlobalSettingsWindow, self).__init__(parent)
2012-03-15 00:22:06 +01:00
self.app = app
self.qvm_collection = qvm_collection
2012-01-31 17:29:00 +01:00
self.setupUi(self)
self.connect(
self.buttonBox,
QtCore.SIGNAL("accepted()"),
self.save_and_apply)
self.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), self.reject)
2012-03-15 00:22:06 +01:00
self.__init_system_defaults__()
self.__init_kernel_defaults__()
self.__init_mem_defaults__()
self.__init_updates__()
2012-03-15 00:22:06 +01:00
def __init_system_defaults__(self):
2017-06-25 16:39:30 +02:00
all_vms = [vm for vm in self.qvm_collection.domains
if (not vm.features.get('internal', False)) and vm.qid != 0]
# set up updatevm choice
self.update_vm_vmlist, self.update_vm_vmidx = utils.prepare_vm_choice(
self.update_vm_combo, self.qvm_collection, 'updatevm',
None, allow_none=True
)
2012-03-15 00:22:06 +01:00
# set up clockvm choice
self.clock_vm_vmlist, self.clock_vm_vmidx = utils.prepare_vm_choice(
self.clock_vm_combo, self.qvm_collection, 'clockvm',
None, allow_none=True
)
2012-03-15 00:22:06 +01:00
2017-06-25 16:39:30 +02:00
# default netvm
netvms = [vm for vm in all_vms
if getattr(vm, 'provides_network', False)]
2012-03-15 00:22:06 +01:00
self.netvm_idx = -1
2017-06-25 16:39:30 +02:00
current_netvm = self.qvm_collection.default_netvm
2012-03-15 00:22:06 +01:00
for (i, vm) in enumerate(netvms):
text = vm.name
if vm is current_netvm:
self.netvm_idx = i
text += self.tr(" (current)")
2012-03-15 00:22:06 +01:00
self.default_netvm_combo.insertItem(i, text)
if current_netvm is not None:
self.default_netvm_combo.setCurrentIndex(self.netvm_idx)
#default template
templates = [vm for vm in all_vms if vm.klass == 'TemplateVM']
2012-03-15 00:22:06 +01:00
self.template_idx = -1
2017-06-25 16:39:30 +02:00
current_template = self.qvm_collection.default_template
2012-03-15 00:22:06 +01:00
for (i, vm) in enumerate(templates):
text = vm.name
if vm is current_template:
self.template_idx = i
text += self.tr(" (current)")
2012-03-15 00:22:06 +01:00
self.default_template_combo.insertItem(i, text)
if current_template is not None:
self.default_template_combo.setCurrentIndex(self.template_idx)
def __apply_system_defaults__(self):
# upatevm
self.qvm_collection.updatevm = \
self.update_vm_vmlist[self.update_vm_combo.currentIndex()]
2012-03-15 00:22:06 +01:00
# clockvm
self.qvm_collection.clockvm = \
self.clock_vm_vmlist[self.clock_vm_combo.currentIndex()]
2012-03-15 00:22:06 +01:00
#default netvm
if self.default_netvm_combo.currentIndex() != self.netvm_idx:
2014-02-16 21:56:55 +01:00
name = str(self.default_netvm_combo.currentText())
2012-03-15 00:22:06 +01:00
name = name.split(' ')[0]
2017-06-25 16:39:30 +02:00
vm = self.qvm_collection.domains[name]
2017-06-25 16:39:30 +02:00
self.qvm_collection.default_netvm = vm
2012-03-15 00:22:06 +01:00
#default template
if self.default_template_combo.currentIndex() != self.template_idx:
2014-02-16 21:56:55 +01:00
name = str(self.default_template_combo.currentText())
2012-03-15 00:22:06 +01:00
name = name.split(' ')[0]
2017-06-25 16:39:30 +02:00
vm = self.qvm_collection.domains[name]
2017-06-25 16:39:30 +02:00
self.qvm_collection.default_template = vm
2012-03-15 00:22:06 +01:00
def __init_kernel_defaults__(self):
kernel_list = []
# TODO system_path["qubes_kernels_base_dir"]
2017-11-17 17:34:38 +01:00
# idea: qubes.pools['linux-kernel'].volumes
2017-06-25 16:39:30 +02:00
for k in os.listdir('/var/lib/qubes/vm-kernels'):
2012-03-15 00:22:06 +01:00
kernel_list.append(k)
self.kernel_idx = 0
for (i, k) in enumerate(kernel_list):
text = k
2017-06-25 16:39:30 +02:00
if k == self.qvm_collection.default_kernel:
text += self.tr(" (current)")
2012-03-15 00:22:06 +01:00
self.kernel_idx = i
self.default_kernel_combo.insertItem(i, text)
2012-03-15 00:22:06 +01:00
self.default_kernel_combo.setCurrentIndex(self.kernel_idx)
def __apply_kernel_defaults__(self):
if self.default_kernel_combo.currentIndex() != self.kernel_idx:
2014-02-16 21:56:55 +01:00
kernel = str(self.default_kernel_combo.currentText())
2012-03-15 00:22:06 +01:00
kernel = kernel.split(' ')[0]
2017-06-25 16:39:30 +02:00
self.qvm_collection.default_kernel = kernel
2012-03-15 00:22:06 +01:00
2012-03-15 00:22:06 +01:00
def __init_mem_defaults__(self):
2012-04-14 17:58:29 +02:00
#qmemman settings
2017-06-25 16:39:30 +02:00
self.qmemman_config = ConfigParser()
self.vm_min_mem_val = '200MiB' #str(qmemman_algo.MIN_PREFMEM)
2017-06-25 16:39:30 +02:00
self.dom0_mem_boost_val = '350MiB' #str(qmemman_algo.DOM0_MEM_BOOST)
2012-04-14 17:58:29 +02:00
self.qmemman_config.read(qmemman_config_path)
if self.qmemman_config.has_section('global'):
self.vm_min_mem_val = \
self.qmemman_config.get('global', 'vm-min-mem')
self.dom0_mem_boost_val = \
self.qmemman_config.get('global', 'dom0-mem-boost')
2012-04-14 17:58:29 +02:00
self.vm_min_mem_val = parse_size(self.vm_min_mem_val)
self.dom0_mem_boost_val = parse_size(self.dom0_mem_boost_val)
self.min_vm_mem.setValue(self.vm_min_mem_val/1024/1024)
self.dom0_mem_boost.setValue(self.dom0_mem_boost_val/1024/1024)
2012-03-15 00:22:06 +01:00
def __apply_mem_defaults__(self):
2012-04-14 17:58:29 +02:00
#qmemman settings
current_min_vm_mem = self.min_vm_mem.value()
current_dom0_mem_boost = self.dom0_mem_boost.value()
if current_min_vm_mem*1024*1024 != self.vm_min_mem_val \
or current_dom0_mem_boost*1024*1024 != self.dom0_mem_boost_val:
2012-04-14 17:58:29 +02:00
current_min_vm_mem = str(current_min_vm_mem)+'M'
current_dom0_mem_boost = str(current_dom0_mem_boost)+'M'
if not self.qmemman_config.has_section('global'):
#add the whole section
self.qmemman_config.add_section('global')
self.qmemman_config.set(
'global', 'vm-min-mem', current_min_vm_mem)
self.qmemman_config.set(
'global', 'dom0-mem-boost', current_dom0_mem_boost)
self.qmemman_config.set(
'global', 'cache-margin-factor', str(1.3))
# removed qmemman_algo.CACHE_FACTOR
2012-04-14 17:58:29 +02:00
qmemman_config_file = open(qmemman_config_path, 'a')
self.qmemman_config.write(qmemman_config_file)
qmemman_config_file.close()
else:
#If there already is a 'global' section, we don't use
# SafeConfigParser.write() - it would get rid of
# all the comments...
2012-04-14 17:58:29 +02:00
lines_to_add = {}
lines_to_add['vm-min-mem'] = \
"vm-min-mem = " + current_min_vm_mem + "\n"
lines_to_add['dom0-mem-boost'] = \
"dom0-mem-boost = " + current_dom0_mem_boost +"\n"
2012-04-14 17:58:29 +02:00
config_lines = []
qmemman_config_file = open(qmemman_config_path, 'r')
for line in qmemman_config_file:
if line.strip().startswith('vm-min-mem'):
2012-04-14 17:58:29 +02:00
config_lines.append(lines_to_add['vm-min-mem'])
del lines_to_add['vm-min-mem']
elif line.strip().startswith('dom0-mem-boost'):
2012-04-14 17:58:29 +02:00
config_lines.append(lines_to_add['dom0-mem-boost'])
del lines_to_add['dom0-mem-boost']
else:
config_lines.append(line)
2012-04-14 17:58:29 +02:00
qmemman_config_file.close()
for line in lines_to_add:
config_lines.append(line)
2012-04-14 17:58:29 +02:00
qmemman_config_file = open(qmemman_config_path, 'w')
qmemman_config_file.writelines(config_lines)
qmemman_config_file.close()
2012-01-31 17:29:00 +01:00
def __init_updates__(self):
self.updates_val = False
2017-06-25 16:39:30 +02:00
# TODO updates_dom0_status(self.qvm_collection)
self.updates_dom0_val = True
self.updates_dom0.setChecked(self.updates_dom0_val)
updates_vms = updates_vms_status(self.qvm_collection)
if updates_vms is None:
self.updates_vm.setCheckState(QtCore.Qt.PartiallyChecked)
else:
self.updates_vm.setCheckState(updates_vms)
def __apply_updates__(self):
if self.updates_dom0.isChecked() != self.updates_dom0_val:
# TODO updates_dom0_toggle(
# self.qvm_collection, self.updates_dom0.isChecked())
2017-06-25 16:39:30 +02:00
raise NotImplementedError('Toggle dom0 updates not implemented')
if self.updates_vm.checkState() != QtCore.Qt.PartiallyChecked:
2017-06-25 16:39:30 +02:00
for vm in self.qvm_collection.domains:
vm.features['check-updates'] = \
bool(self.updates_vm.checkState())
2012-04-14 17:58:29 +02:00
2012-01-31 17:29:00 +01:00
def reject(self):
self.done(0)
def save_and_apply(self):
2017-06-25 16:39:30 +02:00
self.__apply_system_defaults__()
2012-03-15 00:22:06 +01:00
self.__apply_kernel_defaults__()
self.__apply_mem_defaults__()
self.__apply_updates__()
2012-03-15 00:22:06 +01:00
2017-06-25 16:39:30 +02:00
2012-01-31 17:29:00 +01:00
# Bases on the original code by:
# Copyright (c) 2002-2007 Pascal Varet <p.varet@gmail.com>
def handle_exception(exc_type, exc_value, exc_traceback):
filename, line, dummy, dummy = traceback.extract_tb(exc_traceback).pop()
filename = os.path.basename(filename)
error = "%s: %s" % (exc_type.__name__, exc_value)
2012-01-31 17:29:00 +01:00
QtGui.QMessageBox.critical(
None,
"Houston, we have a problem...",
"Whoops. A critical error has occured. This is most likely a bug "
"in Qubes Global Settings application.<br><br><b><i>%s</i></b>" %
error + "at <b>line %d</b> of file <b>%s</b>.<br/><br/>"
% (line, filename))
2012-01-31 17:29:00 +01:00
def main():
qtapp = QtGui.QApplication(sys.argv)
2017-06-25 16:39:30 +02:00
qtapp.setOrganizationName("The Qubes Project")
qtapp.setOrganizationDomain("http://qubes-os.org")
qtapp.setApplicationName("Qubes Global Settings")
2012-01-31 17:29:00 +01:00
sys.excepthook = handle_exception
2017-06-25 16:39:30 +02:00
app = Qubes()
2012-01-31 17:29:00 +01:00
2017-06-25 16:39:30 +02:00
global_window = GlobalSettingsWindow(qtapp, app)
2012-01-31 17:29:00 +01:00
global_window.show()
2017-06-25 16:39:30 +02:00
qtapp.exec_()
qtapp.exit()
2012-01-31 17:29:00 +01:00
if __name__ == "__main__":
main()