global_settings.py 12 KB

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