global_settings.py 12 KB

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