global_settings.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #!/usr/bin/python3
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.com>
  6. # Copyright (C) 2012 Marek Marczykowski <marmarek@mimuw.edu.pl>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. #
  21. #
  22. import sys
  23. import os
  24. import os.path
  25. import traceback
  26. from PyQt4 import QtCore, QtGui # pylint: disable=import-error
  27. from qubesadmin import Qubes
  28. from qubesadmin.utils import parse_size
  29. from . import ui_globalsettingsdlg # pylint: disable=no-name-in-module
  30. from . import utils
  31. from configparser import ConfigParser
  32. qmemman_config_path = '/etc/qubes/qmemman.conf'
  33. # pylint: disable=too-many-instance-attributes
  34. class GlobalSettingsWindow(ui_globalsettingsdlg.Ui_GlobalSettings,
  35. QtGui.QDialog):
  36. def __init__(self, app, qvm_collection, parent=None):
  37. super(GlobalSettingsWindow, self).__init__(parent)
  38. self.app = app
  39. self.qvm_collection = qvm_collection
  40. self.setupUi(self)
  41. self.connect(
  42. self.buttonBox,
  43. QtCore.SIGNAL("accepted()"),
  44. self.save_and_apply)
  45. self.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), self.reject)
  46. self.__init_system_defaults__()
  47. self.__init_kernel_defaults__()
  48. self.__init_mem_defaults__()
  49. self.__init_updates__()
  50. def __init_system_defaults__(self):
  51. # set up updatevm choice
  52. self.update_vm_vmlist, self.update_vm_idx = utils.prepare_vm_choice(
  53. self.update_vm_combo, self.qvm_collection, 'updatevm',
  54. None, allow_none=True,
  55. filter_function=(lambda vm: vm.klass != 'TemplateVM')
  56. )
  57. # set up clockvm choice
  58. self.clock_vm_vmlist, self.clock_vm_idx = utils.prepare_vm_choice(
  59. self.clock_vm_combo, self.qvm_collection, 'clockvm',
  60. None, allow_none=True,
  61. filter_function=(lambda vm: vm.klass != 'TemplateVM')
  62. )
  63. # set up default netvm
  64. self.default_netvm_vmlist, self.default_netvm_idx = \
  65. utils.prepare_vm_choice(
  66. self.default_netvm_combo,
  67. self.qvm_collection, 'default_netvm',
  68. None,
  69. filter_function=(lambda vm: vm.provides_network),
  70. allow_none=True)
  71. # default template
  72. self.default_template_vmlist, self.default_template_idx = \
  73. utils.prepare_vm_choice(
  74. self.default_template_combo,
  75. self.qvm_collection, 'default_template',
  76. None,
  77. filter_function=(lambda vm: vm.klass == 'TemplateVM')
  78. )
  79. # default dispvm
  80. self.default_dispvm_vmlist, self.default_dispvm_idx = \
  81. utils.prepare_vm_choice(
  82. self.default_dispvm_combo,
  83. self.qvm_collection, 'default_dispvm',
  84. None,
  85. (lambda vm: getattr(vm, 'template_for_dispvms', False))
  86. )
  87. def __apply_system_defaults__(self):
  88. # upatevm
  89. if self.qvm_collection.updatevm != \
  90. self.update_vm_vmlist[self.update_vm_combo.currentIndex()]:
  91. self.qvm_collection.updatevm = \
  92. self.update_vm_vmlist[self.update_vm_combo.currentIndex()]
  93. # clockvm
  94. if self.qvm_collection.clockvm !=\
  95. self.clock_vm_vmlist[self.clock_vm_combo.currentIndex()]:
  96. self.qvm_collection.clockvm = \
  97. self.clock_vm_vmlist[self.clock_vm_combo.currentIndex()]
  98. # default netvm
  99. if self.qvm_collection.default_netvm !=\
  100. self.default_netvm_vmlist[
  101. self.default_netvm_combo.currentIndex()]:
  102. self.qvm_collection.default_netvm = \
  103. self.default_netvm_vmlist[
  104. self.default_netvm_combo.currentIndex()]
  105. # default template
  106. if self.qvm_collection.default_template != \
  107. self.default_template_vmlist[
  108. self.default_template_combo.currentIndex()]:
  109. self.qvm_collection.default_template = \
  110. self.default_template_vmlist[
  111. self.default_template_combo.currentIndex()]
  112. # default_dispvm
  113. if self.qvm_collection.default_dispvm != \
  114. self.default_dispvm_vmlist[
  115. self.default_dispvm_combo.currentIndex()]:
  116. self.qvm_collection.default_dispvm = \
  117. self.default_dispvm_vmlist[
  118. self.default_dispvm_combo.currentIndex()]
  119. def __init_kernel_defaults__(self):
  120. self.kernels_list, self.kernels_idx = utils.prepare_kernel_choice(
  121. self.default_kernel_combo, self.qvm_collection, 'default_kernel',
  122. None,
  123. allow_none=True
  124. )
  125. def __apply_kernel_defaults__(self):
  126. if self.qvm_collection.default_kernel != \
  127. self.kernels_list[self.default_kernel_combo.currentIndex()]:
  128. self.qvm_collection.default_kernel = \
  129. self.kernels_list[self.default_kernel_combo.currentIndex()]
  130. def __init_mem_defaults__(self):
  131. #qmemman settings
  132. self.qmemman_config = ConfigParser()
  133. self.vm_min_mem_val = '200MiB' #str(qmemman_algo.MIN_PREFMEM)
  134. self.dom0_mem_boost_val = '350MiB' #str(qmemman_algo.DOM0_MEM_BOOST)
  135. self.qmemman_config.read(qmemman_config_path)
  136. if self.qmemman_config.has_section('global'):
  137. self.vm_min_mem_val = \
  138. self.qmemman_config.get('global', 'vm-min-mem')
  139. self.dom0_mem_boost_val = \
  140. self.qmemman_config.get('global', 'dom0-mem-boost')
  141. self.vm_min_mem_val = parse_size(self.vm_min_mem_val)
  142. self.dom0_mem_boost_val = parse_size(self.dom0_mem_boost_val)
  143. self.min_vm_mem.setValue(self.vm_min_mem_val/1024/1024)
  144. self.dom0_mem_boost.setValue(self.dom0_mem_boost_val/1024/1024)
  145. def __apply_mem_defaults__(self):
  146. #qmemman settings
  147. current_min_vm_mem = self.min_vm_mem.value()
  148. current_dom0_mem_boost = self.dom0_mem_boost.value()
  149. if current_min_vm_mem*1024*1024 != self.vm_min_mem_val \
  150. or current_dom0_mem_boost*1024*1024 != self.dom0_mem_boost_val:
  151. current_min_vm_mem = str(current_min_vm_mem)+'M'
  152. current_dom0_mem_boost = str(current_dom0_mem_boost)+'M'
  153. if not self.qmemman_config.has_section('global'):
  154. #add the whole section
  155. self.qmemman_config.add_section('global')
  156. self.qmemman_config.set(
  157. 'global', 'vm-min-mem', current_min_vm_mem)
  158. self.qmemman_config.set(
  159. 'global', 'dom0-mem-boost', current_dom0_mem_boost)
  160. self.qmemman_config.set(
  161. 'global', 'cache-margin-factor', str(1.3))
  162. # removed qmemman_algo.CACHE_FACTOR
  163. qmemman_config_file = open(qmemman_config_path, 'a')
  164. self.qmemman_config.write(qmemman_config_file)
  165. qmemman_config_file.close()
  166. else:
  167. #If there already is a 'global' section, we don't use
  168. # SafeConfigParser.write() - it would get rid of
  169. # all the comments...
  170. lines_to_add = {}
  171. lines_to_add['vm-min-mem'] = \
  172. "vm-min-mem = " + current_min_vm_mem + "\n"
  173. lines_to_add['dom0-mem-boost'] = \
  174. "dom0-mem-boost = " + current_dom0_mem_boost +"\n"
  175. config_lines = []
  176. qmemman_config_file = open(qmemman_config_path, 'r')
  177. for line in qmemman_config_file:
  178. if line.strip().startswith('vm-min-mem'):
  179. config_lines.append(lines_to_add['vm-min-mem'])
  180. del lines_to_add['vm-min-mem']
  181. elif line.strip().startswith('dom0-mem-boost'):
  182. config_lines.append(lines_to_add['dom0-mem-boost'])
  183. del lines_to_add['dom0-mem-boost']
  184. else:
  185. config_lines.append(line)
  186. qmemman_config_file.close()
  187. for line in lines_to_add:
  188. config_lines.append(line)
  189. qmemman_config_file = open(qmemman_config_path, 'w')
  190. qmemman_config_file.writelines(config_lines)
  191. qmemman_config_file.close()
  192. def __init_updates__(self):
  193. # TODO: remove workaround when it is no longer needed
  194. self.dom0_updates_file_path = '/var/lib/qubes/updates/disable-updates'
  195. try:
  196. self.updates_dom0_val = self.qvm_collection.check_updates_dom0
  197. except AttributeError:
  198. self.updates_dom0_val =\
  199. not os.path.isfile(self.dom0_updates_file_path)
  200. self.updates_dom0.setChecked(self.updates_dom0_val)
  201. self.updates_vm.setChecked(self.qvm_collection.check_updates_vm)
  202. self.enable_updates_all.clicked.connect(self.__enable_updates_all)
  203. self.disable_updates_all.clicked.connect(self.__disable_updates_all)
  204. def __enable_updates_all(self):
  205. reply = QtGui.QMessageBox.question(
  206. self, self.tr("Change state of all qubes"),
  207. self.tr("Are you sure you want to set all qubes to check "
  208. "for updates?"),
  209. QtGui.QMessageBox.Yes | QtGui.QMessageBox.Cancel)
  210. if reply == QtGui.QMessageBox.Cancel:
  211. return
  212. self.__set_updates_all(True)
  213. def __disable_updates_all(self):
  214. reply = QtGui.QMessageBox.question(
  215. self, self.tr("Change state of all qubes"),
  216. self.tr("Are you sure you want to set all qubes to not check "
  217. "for updates?"),
  218. QtGui.QMessageBox.Yes | QtGui.QMessageBox.Cancel)
  219. if reply == QtGui.QMessageBox.Cancel:
  220. return
  221. self.__set_updates_all(False)
  222. def __set_updates_all(self, state):
  223. for vm in self.qvm_collection.domains:
  224. vm.features['check-updates'] = state
  225. def __apply_updates__(self):
  226. if self.updates_dom0.isChecked() != self.updates_dom0_val:
  227. # TODO: remove workaround when it is no longer needed
  228. try:
  229. self.qvm_collection.check_updates_dom0 = \
  230. self.updates_dom0.isChecked()
  231. except AttributeError:
  232. if self.updates_dom0.isChecked():
  233. os.remove(self.dom0_updates_file_path)
  234. else:
  235. open(self.dom0_updates_file_path, 'a').close()
  236. self.qvm_collection.check_updates_vm = self.updates_vm.isChecked()
  237. def reject(self):
  238. self.done(0)
  239. def save_and_apply(self):
  240. self.__apply_system_defaults__()
  241. self.__apply_kernel_defaults__()
  242. self.__apply_mem_defaults__()
  243. self.__apply_updates__()
  244. # Bases on the original code by:
  245. # Copyright (c) 2002-2007 Pascal Varet <p.varet@gmail.com>
  246. def handle_exception(exc_type, exc_value, exc_traceback):
  247. filename, line, dummy, dummy = traceback.extract_tb(exc_traceback).pop()
  248. filename = os.path.basename(filename)
  249. error = "%s: %s" % (exc_type.__name__, exc_value)
  250. QtGui.QMessageBox.critical(
  251. None,
  252. "Houston, we have a problem...",
  253. "Whoops. A critical error has occured. This is most likely a bug "
  254. "in Qubes Global Settings application.<br><br><b><i>%s</i></b>" %
  255. error + "at <b>line %d</b> of file <b>%s</b>.<br/><br/>"
  256. % (line, filename))
  257. def main():
  258. qtapp = QtGui.QApplication(sys.argv)
  259. qtapp.setOrganizationName("The Qubes Project")
  260. qtapp.setOrganizationDomain("http://qubes-os.org")
  261. qtapp.setApplicationName("Qubes Global Settings")
  262. sys.excepthook = handle_exception
  263. app = Qubes()
  264. global_window = GlobalSettingsWindow(qtapp, app)
  265. global_window.show()
  266. qtapp.exec_()
  267. qtapp.exit()
  268. if __name__ == "__main__":
  269. main()