global_settings.py 13 KB

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