qubes.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. defaults["appvm_label"] = QubesVmLabels["red"]
  116. defaults["template_label"] = QubesVmLabels["black"]
  117. defaults["servicevm_label"] = QubesVmLabels["red"]
  118. QubesVmClasses = {}
  119. modules_dir = os.path.join(os.path.dirname(__file__), 'modules')
  120. for module_file in sorted(os.listdir(modules_dir)):
  121. if not module_file.endswith(".py") or module_file == "__init__.py":
  122. continue
  123. __import__('qubes.modules.%s' % module_file[:-3])
  124. try:
  125. import qubes.settings
  126. qubes.settings.apply(system_path, vm_files, defaults)
  127. except ImportError:
  128. pass
  129. for path_key in system_path.keys():
  130. if not os.path.isabs(system_path[path_key]):
  131. system_path[path_key] = os.path.join(
  132. system_path['qubes_base_dir'], system_path[path_key])
  133. # vim:sw=4:et: