global_settings.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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, updates_vms_status
  29. from . import ui_globalsettingsdlg # pylint: disable=no-name-in-module
  30. from configparser import ConfigParser
  31. qmemman_config_path = '/etc/qubes/qmemman.conf'
  32. class GlobalSettingsWindow(ui_globalsettingsdlg.Ui_GlobalSettings,
  33. QtGui.QDialog):
  34. def __init__(self, app, qvm_collection, parent=None):
  35. super(GlobalSettingsWindow, self).__init__(parent)
  36. self.app = app
  37. self.qvm_collection = qvm_collection
  38. self.setupUi(self)
  39. self.connect(
  40. self.buttonBox,
  41. QtCore.SIGNAL("accepted()"),
  42. self.save_and_apply)
  43. self.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), self.reject)
  44. self.__init_system_defaults__()
  45. self.__init_kernel_defaults__()
  46. self.__init_mem_defaults__()
  47. self.__init_updates__()
  48. def __init_system_defaults__(self):
  49. # updatevm and clockvm
  50. all_vms = [vm for vm in self.qvm_collection.domains
  51. if (not vm.features.get('internal', False)) and vm.qid != 0]
  52. self.updatevm_idx = -1
  53. current_update_vm = self.qvm_collection.updatevm
  54. for (i, vm) in enumerate(all_vms):
  55. text = vm.name
  56. if vm is current_update_vm:
  57. self.updatevm_idx = i
  58. text += self.tr(" (current)")
  59. self.update_vm_combo.insertItem(i, text)
  60. self.update_vm_combo.insertItem(len(all_vms), "none")
  61. if current_update_vm is None:
  62. self.updatevm_idx = len(all_vms)
  63. self.update_vm_combo.setCurrentIndex(self.updatevm_idx)
  64. # clockvm
  65. self.clockvm_idx = -1
  66. current_clock_vm = self.qvm_collection.clockvm
  67. for (i, vm) in enumerate(all_vms):
  68. text = vm.name
  69. if vm is current_clock_vm:
  70. self.clockvm_idx = i
  71. text += self.tr(" (current)")
  72. self.clock_vm_combo.insertItem(i, text)
  73. self.clock_vm_combo.insertItem(len(all_vms), "none")
  74. if current_clock_vm is None:
  75. self.clockvm_idx = len(all_vms)
  76. self.clock_vm_combo.setCurrentIndex(self.clockvm_idx)
  77. # default netvm
  78. netvms = [vm for vm in all_vms
  79. if getattr(vm, 'provides_network', False)]
  80. self.netvm_idx = -1
  81. current_netvm = self.qvm_collection.default_netvm
  82. for (i, vm) in enumerate(netvms):
  83. text = vm.name
  84. if vm is current_netvm:
  85. self.netvm_idx = i
  86. text += self.tr(" (current)")
  87. self.default_netvm_combo.insertItem(i, text)
  88. if current_netvm is not None:
  89. self.default_netvm_combo.setCurrentIndex(self.netvm_idx)
  90. #default template
  91. templates = [vm for vm in all_vms if vm.klass == 'TemplateVM']
  92. self.template_idx = -1
  93. current_template = self.qvm_collection.default_template
  94. for (i, vm) in enumerate(templates):
  95. text = vm.name
  96. if vm is current_template:
  97. self.template_idx = i
  98. text += self.tr(" (current)")
  99. self.default_template_combo.insertItem(i, text)
  100. if current_template is not None:
  101. self.default_template_combo.setCurrentIndex(self.template_idx)
  102. def __apply_system_defaults__(self):
  103. #upatevm
  104. if self.update_vm_combo.currentIndex() != self.updatevm_idx:
  105. updatevm_name = str(self.update_vm_combo.currentText())
  106. updatevm_name = updatevm_name.split(' ')[0]
  107. updatevm = self.qvm_collection.domains[updatevm_name]
  108. self.qvm_collection.updatevm = updatevm
  109. #clockvm
  110. if self.clock_vm_combo.currentIndex() != self.clockvm_idx:
  111. clockvm_name = str(self.clock_vm_combo.currentText())
  112. clockvm_name = clockvm_name.split(' ')[0]
  113. clockvm = self.qvm_collection.domains[clockvm_name]
  114. self.qvm_collection.clockvm = clockvm
  115. #default netvm
  116. if self.default_netvm_combo.currentIndex() != self.netvm_idx:
  117. name = str(self.default_netvm_combo.currentText())
  118. name = name.split(' ')[0]
  119. vm = self.qvm_collection.domains[name]
  120. self.qvm_collection.default_netvm = vm
  121. #default template
  122. if self.default_template_combo.currentIndex() != self.template_idx:
  123. name = str(self.default_template_combo.currentText())
  124. name = name.split(' ')[0]
  125. vm = self.qvm_collection.domains[name]
  126. self.qvm_collection.default_template = vm
  127. def __init_kernel_defaults__(self):
  128. kernel_list = []
  129. # TODO system_path["qubes_kernels_base_dir"]
  130. # idea: qubes.pools['linux-kernel'].volumes
  131. for k in os.listdir('/var/lib/qubes/vm-kernels'):
  132. kernel_list.append(k)
  133. self.kernel_idx = 0
  134. for (i, k) in enumerate(kernel_list):
  135. text = k
  136. if k == self.qvm_collection.default_kernel:
  137. text += self.tr(" (current)")
  138. self.kernel_idx = i
  139. self.default_kernel_combo.insertItem(i, text)
  140. self.default_kernel_combo.setCurrentIndex(self.kernel_idx)
  141. def __apply_kernel_defaults__(self):
  142. if self.default_kernel_combo.currentIndex() != self.kernel_idx:
  143. kernel = str(self.default_kernel_combo.currentText())
  144. kernel = kernel.split(' ')[0]
  145. self.qvm_collection.default_kernel = kernel
  146. def __init_mem_defaults__(self):
  147. #qmemman settings
  148. self.qmemman_config = ConfigParser()
  149. self.vm_min_mem_val = '200MiB' #str(qmemman_algo.MIN_PREFMEM)
  150. self.dom0_mem_boost_val = '350MiB' #str(qmemman_algo.DOM0_MEM_BOOST)
  151. self.qmemman_config.read(qmemman_config_path)
  152. if self.qmemman_config.has_section('global'):
  153. self.vm_min_mem_val = \
  154. self.qmemman_config.get('global', 'vm-min-mem')
  155. self.dom0_mem_boost_val = \
  156. self.qmemman_config.get('global', 'dom0-mem-boost')
  157. self.vm_min_mem_val = parse_size(self.vm_min_mem_val)
  158. self.dom0_mem_boost_val = parse_size(self.dom0_mem_boost_val)
  159. self.min_vm_mem.setValue(self.vm_min_mem_val/1024/1024)
  160. self.dom0_mem_boost.setValue(self.dom0_mem_boost_val/1024/1024)
  161. def __apply_mem_defaults__(self):
  162. #qmemman settings
  163. current_min_vm_mem = self.min_vm_mem.value()
  164. current_dom0_mem_boost = self.dom0_mem_boost.value()
  165. if current_min_vm_mem*1024*1024 != self.vm_min_mem_val \
  166. or current_dom0_mem_boost*1024*1024 != self.dom0_mem_boost_val:
  167. current_min_vm_mem = str(current_min_vm_mem)+'M'
  168. current_dom0_mem_boost = str(current_dom0_mem_boost)+'M'
  169. if not self.qmemman_config.has_section('global'):
  170. #add the whole section
  171. self.qmemman_config.add_section('global')
  172. self.qmemman_config.set(
  173. 'global', 'vm-min-mem', current_min_vm_mem)
  174. self.qmemman_config.set(
  175. 'global', 'dom0-mem-boost', current_dom0_mem_boost)
  176. self.qmemman_config.set(
  177. 'global', 'cache-margin-factor', str(1.3))
  178. # removed qmemman_algo.CACHE_FACTOR
  179. qmemman_config_file = open(qmemman_config_path, 'a')
  180. self.qmemman_config.write(qmemman_config_file)
  181. qmemman_config_file.close()
  182. else:
  183. #If there already is a 'global' section, we don't use
  184. # SafeConfigParser.write() - it would get rid of
  185. # all the comments...
  186. lines_to_add = {}
  187. lines_to_add['vm-min-mem'] = \
  188. "vm-min-mem = " + current_min_vm_mem + "\n"
  189. lines_to_add['dom0-mem-boost'] = \
  190. "dom0-mem-boost = " + current_dom0_mem_boost +"\n"
  191. config_lines = []
  192. qmemman_config_file = open(qmemman_config_path, 'r')
  193. for line in qmemman_config_file:
  194. if line.strip().startswith('vm-min-mem'):
  195. config_lines.append(lines_to_add['vm-min-mem'])
  196. del lines_to_add['vm-min-mem']
  197. elif line.strip().startswith('dom0-mem-boost'):
  198. config_lines.append(lines_to_add['dom0-mem-boost'])
  199. del lines_to_add['dom0-mem-boost']
  200. else:
  201. config_lines.append(line)
  202. qmemman_config_file.close()
  203. for line in lines_to_add:
  204. config_lines.append(line)
  205. qmemman_config_file = open(qmemman_config_path, 'w')
  206. qmemman_config_file.writelines(config_lines)
  207. qmemman_config_file.close()
  208. def __init_updates__(self):
  209. self.updates_val = False
  210. # TODO updates_dom0_status(self.qvm_collection)
  211. self.updates_dom0_val = True
  212. self.updates_dom0.setChecked(self.updates_dom0_val)
  213. updates_vms = updates_vms_status(self.qvm_collection)
  214. if updates_vms is None:
  215. self.updates_vm.setCheckState(QtCore.Qt.PartiallyChecked)
  216. else:
  217. self.updates_vm.setCheckState(updates_vms)
  218. def __apply_updates__(self):
  219. if self.updates_dom0.isChecked() != self.updates_dom0_val:
  220. # TODO updates_dom0_toggle(
  221. # self.qvm_collection, self.updates_dom0.isChecked())
  222. raise NotImplementedError('Toggle dom0 updates not implemented')
  223. if self.updates_vm.checkState() != QtCore.Qt.PartiallyChecked:
  224. for vm in self.qvm_collection.domains:
  225. vm.features['check-updates'] = \
  226. bool(self.updates_vm.checkState())
  227. def reject(self):
  228. self.done(0)
  229. def save_and_apply(self):
  230. self.__apply_system_defaults__()
  231. self.__apply_kernel_defaults__()
  232. self.__apply_mem_defaults__()
  233. self.__apply_updates__()
  234. # Bases on the original code by:
  235. # Copyright (c) 2002-2007 Pascal Varet <p.varet@gmail.com>
  236. def handle_exception(exc_type, exc_value, exc_traceback):
  237. filename, line, dummy, dummy = traceback.extract_tb(exc_traceback).pop()
  238. filename = os.path.basename(filename)
  239. error = "%s: %s" % (exc_type.__name__, exc_value)
  240. QtGui.QMessageBox.critical(
  241. None,
  242. "Houston, we have a problem...",
  243. "Whoops. A critical error has occured. This is most likely a bug "
  244. "in Qubes Global Settings application.<br><br><b><i>%s</i></b>" %
  245. error + "at <b>line %d</b> of file <b>%s</b>.<br/><br/>"
  246. % (line, filename))
  247. def main():
  248. qtapp = QtGui.QApplication(sys.argv)
  249. qtapp.setOrganizationName("The Qubes Project")
  250. qtapp.setOrganizationDomain("http://qubes-os.org")
  251. qtapp.setApplicationName("Qubes Global Settings")
  252. sys.excepthook = handle_exception
  253. app = Qubes()
  254. global_window = GlobalSettingsWindow(qtapp, app)
  255. global_window.show()
  256. qtapp.exec_()
  257. qtapp.exit()
  258. if __name__ == "__main__":
  259. main()