global_settings.py 12 KB

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