global_settings.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. allow_none=True
  79. )
  80. # default dispvm
  81. self.default_dispvm_vmlist, self.default_dispvm_idx = \
  82. utils.prepare_vm_choice(
  83. self.default_dispvm_combo,
  84. self.qvm_collection, 'default_dispvm',
  85. None,
  86. (lambda vm: getattr(vm, 'template_for_dispvms', False)),
  87. allow_none=True
  88. )
  89. def __apply_system_defaults__(self):
  90. # upatevm
  91. if self.qvm_collection.updatevm != \
  92. self.update_vm_vmlist[self.update_vm_combo.currentIndex()]:
  93. self.qvm_collection.updatevm = \
  94. self.update_vm_vmlist[self.update_vm_combo.currentIndex()]
  95. # clockvm
  96. if self.qvm_collection.clockvm !=\
  97. self.clock_vm_vmlist[self.clock_vm_combo.currentIndex()]:
  98. self.qvm_collection.clockvm = \
  99. self.clock_vm_vmlist[self.clock_vm_combo.currentIndex()]
  100. # default netvm
  101. if self.qvm_collection.default_netvm !=\
  102. self.default_netvm_vmlist[
  103. self.default_netvm_combo.currentIndex()]:
  104. self.qvm_collection.default_netvm = \
  105. self.default_netvm_vmlist[
  106. self.default_netvm_combo.currentIndex()]
  107. # default template
  108. if self.qvm_collection.default_template != \
  109. self.default_template_vmlist[
  110. self.default_template_combo.currentIndex()]:
  111. self.qvm_collection.default_template = \
  112. self.default_template_vmlist[
  113. self.default_template_combo.currentIndex()]
  114. # default_dispvm
  115. if self.qvm_collection.default_dispvm != \
  116. self.default_dispvm_vmlist[
  117. self.default_dispvm_combo.currentIndex()]:
  118. self.qvm_collection.default_dispvm = \
  119. self.default_dispvm_vmlist[
  120. self.default_dispvm_combo.currentIndex()]
  121. def __init_kernel_defaults__(self):
  122. self.kernels_list, self.kernels_idx = utils.prepare_kernel_choice(
  123. self.default_kernel_combo, self.qvm_collection, 'default_kernel',
  124. None,
  125. allow_none=True
  126. )
  127. def __apply_kernel_defaults__(self):
  128. if self.qvm_collection.default_kernel != \
  129. self.kernels_list[self.default_kernel_combo.currentIndex()]:
  130. self.qvm_collection.default_kernel = \
  131. self.kernels_list[self.default_kernel_combo.currentIndex()]
  132. def __init_mem_defaults__(self):
  133. #qmemman settings
  134. self.qmemman_config = ConfigParser()
  135. self.vm_min_mem_val = '200MiB' #str(qmemman_algo.MIN_PREFMEM)
  136. self.dom0_mem_boost_val = '350MiB' #str(qmemman_algo.DOM0_MEM_BOOST)
  137. self.qmemman_config.read(qmemman_config_path)
  138. if self.qmemman_config.has_section('global'):
  139. self.vm_min_mem_val = \
  140. self.qmemman_config.get('global', 'vm-min-mem')
  141. self.dom0_mem_boost_val = \
  142. self.qmemman_config.get('global', 'dom0-mem-boost')
  143. self.vm_min_mem_val = parse_size(self.vm_min_mem_val)
  144. self.dom0_mem_boost_val = parse_size(self.dom0_mem_boost_val)
  145. self.min_vm_mem.setValue(self.vm_min_mem_val/1024/1024)
  146. self.dom0_mem_boost.setValue(self.dom0_mem_boost_val/1024/1024)
  147. def __apply_mem_defaults__(self):
  148. #qmemman settings
  149. current_min_vm_mem = self.min_vm_mem.value()
  150. current_dom0_mem_boost = self.dom0_mem_boost.value()
  151. if current_min_vm_mem*1024*1024 != self.vm_min_mem_val \
  152. or current_dom0_mem_boost*1024*1024 != self.dom0_mem_boost_val:
  153. current_min_vm_mem = str(current_min_vm_mem)+'MiB'
  154. current_dom0_mem_boost = str(current_dom0_mem_boost)+'MiB'
  155. if not self.qmemman_config.has_section('global'):
  156. #add the whole section
  157. self.qmemman_config.add_section('global')
  158. self.qmemman_config.set(
  159. 'global', 'vm-min-mem', current_min_vm_mem)
  160. self.qmemman_config.set(
  161. 'global', 'dom0-mem-boost', current_dom0_mem_boost)
  162. self.qmemman_config.set(
  163. 'global', 'cache-margin-factor', str(1.3))
  164. # removed qmemman_algo.CACHE_FACTOR
  165. qmemman_config_file = open(qmemman_config_path, 'a')
  166. self.qmemman_config.write(qmemman_config_file)
  167. qmemman_config_file.close()
  168. else:
  169. #If there already is a 'global' section, we don't use
  170. # SafeConfigParser.write() - it would get rid of
  171. # all the comments...
  172. lines_to_add = {}
  173. lines_to_add['vm-min-mem'] = \
  174. "vm-min-mem = " + current_min_vm_mem + "\n"
  175. lines_to_add['dom0-mem-boost'] = \
  176. "dom0-mem-boost = " + current_dom0_mem_boost +"\n"
  177. config_lines = []
  178. qmemman_config_file = open(qmemman_config_path, 'r')
  179. for line in qmemman_config_file:
  180. if line.strip().startswith('vm-min-mem'):
  181. config_lines.append(lines_to_add['vm-min-mem'])
  182. del lines_to_add['vm-min-mem']
  183. elif line.strip().startswith('dom0-mem-boost'):
  184. config_lines.append(lines_to_add['dom0-mem-boost'])
  185. del lines_to_add['dom0-mem-boost']
  186. else:
  187. config_lines.append(line)
  188. qmemman_config_file.close()
  189. for line in lines_to_add:
  190. config_lines.append(line)
  191. qmemman_config_file = open(qmemman_config_path, 'w')
  192. qmemman_config_file.writelines(config_lines)
  193. qmemman_config_file.close()
  194. def __init_updates__(self):
  195. # TODO: remove workaround when it is no longer needed
  196. self.dom0_updates_file_path = '/var/lib/qubes/updates/disable-updates'
  197. try:
  198. self.updates_dom0_val = self.qvm_collection.check_updates_dom0
  199. except AttributeError:
  200. self.updates_dom0_val =\
  201. not os.path.isfile(self.dom0_updates_file_path)
  202. self.updates_dom0.setChecked(self.updates_dom0_val)
  203. self.updates_vm.setChecked(self.qvm_collection.check_updates_vm)
  204. self.enable_updates_all.clicked.connect(self.__enable_updates_all)
  205. self.disable_updates_all.clicked.connect(self.__disable_updates_all)
  206. def __enable_updates_all(self):
  207. reply = QtGui.QMessageBox.question(
  208. self, self.tr("Change state of all qubes"),
  209. self.tr("Are you sure you want to set all qubes to check "
  210. "for updates?"),
  211. QtGui.QMessageBox.Yes | QtGui.QMessageBox.Cancel)
  212. if reply == QtGui.QMessageBox.Cancel:
  213. return
  214. self.__set_updates_all(True)
  215. def __disable_updates_all(self):
  216. reply = QtGui.QMessageBox.question(
  217. self, self.tr("Change state of all qubes"),
  218. self.tr("Are you sure you want to set all qubes to not check "
  219. "for updates?"),
  220. QtGui.QMessageBox.Yes | QtGui.QMessageBox.Cancel)
  221. if reply == QtGui.QMessageBox.Cancel:
  222. return
  223. self.__set_updates_all(False)
  224. def __set_updates_all(self, state):
  225. for vm in self.qvm_collection.domains:
  226. vm.features['check-updates'] = state
  227. def __apply_updates__(self):
  228. if self.updates_dom0.isChecked() != self.updates_dom0_val:
  229. # TODO: remove workaround when it is no longer needed
  230. try:
  231. self.qvm_collection.check_updates_dom0 = \
  232. self.updates_dom0.isChecked()
  233. except AttributeError:
  234. if self.updates_dom0.isChecked():
  235. os.remove(self.dom0_updates_file_path)
  236. else:
  237. open(self.dom0_updates_file_path, 'a').close()
  238. self.qvm_collection.check_updates_vm = self.updates_vm.isChecked()
  239. def reject(self):
  240. self.done(0)
  241. def save_and_apply(self):
  242. self.__apply_system_defaults__()
  243. self.__apply_kernel_defaults__()
  244. self.__apply_mem_defaults__()
  245. self.__apply_updates__()
  246. # Bases on the original code by:
  247. # Copyright (c) 2002-2007 Pascal Varet <p.varet@gmail.com>
  248. def handle_exception(exc_type, exc_value, exc_traceback):
  249. filename, line, dummy, dummy = traceback.extract_tb(exc_traceback).pop()
  250. filename = os.path.basename(filename)
  251. error = "%s: %s" % (exc_type.__name__, exc_value)
  252. QtGui.QMessageBox.critical(
  253. None,
  254. "Houston, we have a problem...",
  255. "Whoops. A critical error has occured. This is most likely a bug "
  256. "in Qubes Global Settings application.<br><br><b><i>%s</i></b>" %
  257. error + "at <b>line %d</b> of file <b>%s</b>.<br/><br/>"
  258. % (line, filename))
  259. def main():
  260. qtapp = QtGui.QApplication(sys.argv)
  261. qtapp.setOrganizationName("The Qubes Project")
  262. qtapp.setOrganizationDomain("http://qubes-os.org")
  263. qtapp.setApplicationName("Qubes Global Settings")
  264. sys.excepthook = handle_exception
  265. app = Qubes()
  266. global_window = GlobalSettingsWindow(qtapp, app)
  267. global_window.show()
  268. qtapp.exec_()
  269. qtapp.exit()
  270. if __name__ == "__main__":
  271. main()