qubes.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/python2
  2. # -*- coding: utf-8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
  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. qubes_base_dir = "/var/lib/qubes"
  24. system_path = {
  25. 'qubes_guid_path': '/usr/bin/qubes-guid',
  26. 'qrexec_daemon_path': '/usr/lib/qubes/qrexec-daemon',
  27. 'qrexec_client_path': '/usr/lib/qubes/qrexec-client',
  28. 'qubesdb_daemon_path': '/usr/sbin/qubesdb-daemon',
  29. 'qubes_base_dir': qubes_base_dir,
  30. # Relative to qubes_base_dir
  31. 'qubes_appvms_dir': 'appvms',
  32. 'qubes_templates_dir': 'vm-templates',
  33. 'qubes_servicevms_dir': 'servicevms',
  34. 'qubes_store_filename': 'qubes.xml',
  35. 'qubes_kernels_base_dir': 'vm-kernels',
  36. # qubes_icon_dir is obsolete
  37. # use QIcon.fromTheme() where applicable
  38. 'qubes_icon_dir': '/usr/share/icons/hicolor/128x128/devices',
  39. 'qrexec_policy_dir': '/etc/qubes-rpc/policy',
  40. 'config_template_pv': '/usr/share/qubes/vm-template.xml',
  41. 'qubes_pciback_cmd': '/usr/lib/qubes/unbind-pci-device.sh',
  42. 'prepare_volatile_img_cmd': '/usr/lib/qubes/prepare-volatile-img.sh',
  43. }
  44. vm_files = {
  45. 'root_img': 'root.img',
  46. 'rootcow_img': 'root-cow.img',
  47. 'volatile_img': 'volatile.img',
  48. 'private_img': 'private.img',
  49. 'kernels_subdir': 'kernels',
  50. 'firewall_conf': 'firewall.xml',
  51. 'whitelisted_appmenus': 'whitelisted-appmenus.list',
  52. 'updates_stat_file': 'updates.stat',
  53. }
  54. defaults = {
  55. 'libvirt_uri': 'xen:///',
  56. 'memory': 400,
  57. 'kernelopts': "nopat",
  58. 'kernelopts_pcidevs': "nopat iommu=soft swiotlb=8192",
  59. 'dom0_update_check_interval': 6*3600,
  60. 'private_img_size': 2*1024*1024*1024,
  61. 'root_img_size': 10*1024*1024*1024,
  62. 'storage_class': None,
  63. # how long (in sec) to wait for VMs to shutdown,
  64. # before killing them (when used qvm-run with --wait option),
  65. 'shutdown_counter_max': 60,
  66. 'vm_default_netmask': "255.255.255.0",
  67. # Set later
  68. 'appvm_label': None,
  69. 'template_label': None,
  70. 'servicevm_label': None,
  71. }
  72. qubes_max_qid = 254
  73. qubes_max_netid = 254
  74. ##########################################
  75. def register_qubes_vm_class(vm_class):
  76. QubesVmClasses[vm_class.__name__] = vm_class
  77. # register class as local for this module - to make it easy to import from
  78. # other modules
  79. setattr(sys.modules[__name__], vm_class.__name__, vm_class)
  80. class QubesDaemonPidfile(object):
  81. def __init__(self, name):
  82. self.name = name
  83. self.path = "/var/run/qubes/" + name + ".pid"
  84. def create_pidfile(self):
  85. f = open (self.path, 'w')
  86. f.write(str(os.getpid()))
  87. f.close()
  88. def pidfile_exists(self):
  89. return os.path.exists(self.path)
  90. def read_pid(self):
  91. f = open (self.path)
  92. pid = f.read ().strip()
  93. f.close()
  94. return int(pid)
  95. def pidfile_is_stale(self):
  96. if not self.pidfile_exists():
  97. return False
  98. # check if the pid file is valid...
  99. proc_path = "/proc/" + str(self.read_pid()) + "/cmdline"
  100. if not os.path.exists (proc_path):
  101. print >> sys.stderr, \
  102. "Path {0} doesn't exist, assuming stale pidfile.".\
  103. format(proc_path)
  104. return True
  105. return False # It's a good pidfile
  106. def remove_pidfile(self):
  107. os.remove (self.path)
  108. def __enter__ (self):
  109. # assumes the pidfile doesn't exist -- you should ensure it before opening the context
  110. self.create_pidfile()
  111. def __exit__ (self, exc_type, exc_val, exc_tb):
  112. self.remove_pidfile()
  113. return False
  114. ### Initialization code
  115. # Globally defined lables
  116. QubesVmLabels = {
  117. "red": QubesVmLabel(1, "0xcc0000", "red" ),
  118. "orange": QubesVmLabel(2, "0xf57900", "orange" ),
  119. "yellow": QubesVmLabel(3, "0xedd400", "yellow" ),
  120. "green": QubesVmLabel(4, "0x73d216", "green" ),
  121. "gray": QubesVmLabel(5, "0x555753", "gray" ),
  122. "blue": QubesVmLabel(6, "0x3465a4", "blue" ),
  123. "purple": QubesVmLabel(7, "0x75507b", "purple" ),
  124. "black": QubesVmLabel(8, "0x000000", "black" ),
  125. }
  126. QubesDispVmLabels = {
  127. k: QubesVmLabel(index=v.index, color=v.color, name=v.name, dispvm=True)
  128. for k, v in QubesVmLabels.iteritems()
  129. }
  130. defaults["appvm_label"] = QubesVmLabels["red"]
  131. defaults["template_label"] = QubesVmLabels["black"]
  132. defaults["servicevm_label"] = QubesVmLabels["red"]
  133. QubesVmClasses = {}
  134. modules_dir = os.path.join(os.path.dirname(__file__), 'modules')
  135. for module_file in sorted(os.listdir(modules_dir)):
  136. if not module_file.endswith(".py") or module_file == "__init__.py":
  137. continue
  138. __import__('qubes.modules.%s' % module_file[:-3])
  139. try:
  140. import qubes.settings
  141. qubes.settings.apply(system_path, vm_files, defaults)
  142. except ImportError:
  143. pass
  144. for path_key in system_path.keys():
  145. if not os.path.isabs(system_path[path_key]):
  146. system_path[path_key] = os.path.join(
  147. system_path['qubes_base_dir'], system_path[path_key])
  148. # vim:sw=4:et: