01QubesHVm.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #!/usr/bin/python2
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. # Copyright (C) 2013 Marek Marczykowski <marmarek@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. import os
  24. from qubes.qubes import QubesVm,register_qubes_vm_class,xs,dry_run
  25. from qubes.qubes import system_path,defaults
  26. system_path["config_template_hvm"] = '/usr/share/qubes/vm-template-hvm.conf'
  27. defaults["hvm_disk_size"] = 20*1024*1024*1024
  28. defaults["hvm_private_img_size"] = 2*1024*1024*1024
  29. defaults["hvm_memory"] = 512
  30. class QubesHVm(QubesVm):
  31. """
  32. A class that represents an HVM. A child of QubesVm.
  33. """
  34. # FIXME: logically should inherit after QubesAppVm, but none of its methods
  35. # are useful for HVM
  36. def get_attrs_config(self):
  37. attrs = super(QubesHVm, self).get_attrs_config()
  38. attrs.pop('kernel')
  39. attrs.pop('kernels_dir')
  40. attrs.pop('kernelopts')
  41. attrs.pop('uses_default_kernel')
  42. attrs.pop('uses_default_kernelopts')
  43. attrs['dir_path']['eval'] = 'value if value is not None else os.path.join(system_path["qubes_appvms_dir"], self.name)'
  44. attrs['volatile_img']['eval'] = 'None'
  45. attrs['config_file_template']['eval'] = 'system_path["config_template_hvm"]'
  46. attrs['drive'] = { 'save': 'str(self.drive)' }
  47. attrs['maxmem'].pop('save')
  48. attrs['timezone'] = { 'default': 'localtime', 'save': 'str(self.timezone)' }
  49. attrs['qrexec_installed'] = { 'default': False, 'save': 'str(self.qrexec_installed)' }
  50. attrs['guiagent_installed'] = { 'default' : False, 'save': 'str(self.guiagent_installed)' }
  51. attrs['_start_guid_first']['eval'] = 'True'
  52. attrs['services']['default'] = "{'meminfo-writer': False}"
  53. # only standalone HVM supported for now
  54. attrs['template']['eval'] = 'None'
  55. attrs['memory']['default'] = defaults["hvm_memory"]
  56. return attrs
  57. def __init__(self, **kwargs):
  58. super(QubesHVm, self).__init__(**kwargs)
  59. # Default for meminfo-writer have changed to (correct) False in the
  60. # same version as introduction of guiagent_installed, so for older VMs
  61. # with wrong setting, change is based on 'guiagent_installed' presence
  62. if "guiagent_installed" not in kwargs and \
  63. (not 'xml_element' in kwargs or kwargs['xml_element'].get('guiagent_installed') is None):
  64. self.services['meminfo-writer'] = False
  65. # HVM normally doesn't support dynamic memory management
  66. if not ('meminfo-writer' in self.services and self.services['meminfo-writer']):
  67. self.maxmem = self.memory
  68. # Disable qemu GUID if the user installed qubes gui agent
  69. if self.guiagent_installed:
  70. self._start_guid_first = False
  71. @property
  72. def type(self):
  73. return "HVM"
  74. def is_appvm(self):
  75. return True
  76. def get_clone_attrs(self):
  77. attrs = super(QubesHVm, self).get_clone_attrs()
  78. attrs.remove('kernel')
  79. attrs.remove('uses_default_kernel')
  80. attrs.remove('kernelopts')
  81. attrs.remove('uses_default_kernelopts')
  82. attrs += [ 'timezone' ]
  83. attrs += [ 'qrexec_installed' ]
  84. attrs += [ 'guiagent_installed' ]
  85. return attrs
  86. def create_on_disk(self, verbose, source_template = None):
  87. if dry_run:
  88. return
  89. if verbose:
  90. print >> sys.stderr, "--> Creating directory: {0}".format(self.dir_path)
  91. os.mkdir (self.dir_path)
  92. if verbose:
  93. print >> sys.stderr, "--> Creating icon symlink: {0} -> {1}".format(self.icon_path, self.label.icon_path)
  94. os.symlink (self.label.icon_path, self.icon_path)
  95. self.create_config_file()
  96. # create empty disk
  97. f_root = open(self.root_img, "w")
  98. f_root.truncate(defaults["hvm_disk_size"])
  99. f_root.close()
  100. # create empty private.img
  101. f_private = open(self.private_img, "w")
  102. f_private.truncate(defaults["hvm_private_img_size"])
  103. f_root.close()
  104. # fire hooks
  105. for hook in self.hooks_create_on_disk:
  106. hook(self, verbose, source_template=source_template)
  107. def get_disk_utilization_private_img(self):
  108. return 0
  109. def get_private_img_sz(self):
  110. return 0
  111. def resize_private_img(self, size):
  112. raise NotImplementedError("HVM has no private.img")
  113. def get_config_params(self, source_template=None):
  114. params = super(QubesHVm, self).get_config_params(source_template=source_template)
  115. params['volatiledev'] = ''
  116. if self.drive:
  117. type_mode = ":cdrom,r"
  118. drive_path = self.drive
  119. # leave empty to use standard syntax in case of dom0
  120. backend_domain = ""
  121. if drive_path.startswith("hd:"):
  122. type_mode = ",w"
  123. drive_path = drive_path[3:]
  124. elif drive_path.startswith("cdrom:"):
  125. type_mode = ":cdrom,r"
  126. drive_path = drive_path[6:]
  127. backend_split = re.match(r"^([a-zA-Z0-9-]*):(.*)", drive_path)
  128. if backend_split:
  129. backend_domain = "," + backend_split.group(1)
  130. drive_path = backend_split.group(2)
  131. # FIXME: os.stat will work only when backend in dom0...
  132. stat_res = None
  133. if backend_domain == "":
  134. stat_res = os.stat(drive_path)
  135. if stat_res and stat.S_ISBLK(stat_res.st_mode):
  136. params['otherdevs'] = "'phy:%s,xvdc%s%s'," % (drive_path, type_mode, backend_domain)
  137. else:
  138. params['otherdevs'] = "'script:file:%s,xvdc%s%s'," % (drive_path, type_mode, backend_domain)
  139. else:
  140. params['otherdevs'] = ''
  141. # Disable currently unused private.img - to be enabled when TemplateHVm done
  142. params['privatedev'] = ''
  143. if self.timezone.lower() == 'localtime':
  144. params['localtime'] = '1'
  145. params['timeoffset'] = '0'
  146. elif self.timezone.isdigit():
  147. params['localtime'] = '0'
  148. params['timeoffset'] = self.timezone
  149. else:
  150. print >>sys.stderr, "WARNING: invalid 'timezone' value: %s" % self.timezone
  151. params['localtime'] = '0'
  152. params['timeoffset'] = '0'
  153. return params
  154. def verify_files(self):
  155. if dry_run:
  156. return
  157. if not os.path.exists (self.dir_path):
  158. raise QubesException (
  159. "VM directory doesn't exist: {0}".\
  160. format(self.dir_path))
  161. if self.is_updateable() and not os.path.exists (self.root_img):
  162. raise QubesException (
  163. "VM root image file doesn't exist: {0}".\
  164. format(self.root_img))
  165. if not os.path.exists (self.private_img):
  166. print >>sys.stderr, "WARNING: Creating empty VM private image file: {0}".\
  167. format(self.private_img)
  168. f_private = open(self.private_img, "w")
  169. f_private.truncate(defaults["hvm_private_img_size"])
  170. f_private.close()
  171. # fire hooks
  172. for hook in self.hooks_verify_files:
  173. hook(self)
  174. return True
  175. def reset_volatile_storage(self, **kwargs):
  176. pass
  177. @property
  178. def vif(self):
  179. if self.xid < 0:
  180. return None
  181. if self.netvm is None:
  182. return None
  183. return "vif{0}.+".format(self.stubdom_xid)
  184. def run(self, command, **kwargs):
  185. if self.qrexec_installed:
  186. if 'gui' in kwargs and kwargs['gui']==False:
  187. command = "nogui:" + command
  188. return super(QubesHVm, self).run(command, **kwargs)
  189. else:
  190. raise QubesException("Needs qrexec agent installed in VM to use this function. See also qvm-prefs.")
  191. @property
  192. def stubdom_xid(self):
  193. if self.xid < 0:
  194. return -1
  195. stubdom_xid_str = xs.read('', '/local/domain/%d/image/device-model-domid' % self.xid)
  196. if stubdom_xid_str is not None:
  197. return int(stubdom_xid_str)
  198. else:
  199. return -1
  200. def start_guid(self, verbose = True, notify_function = None):
  201. # If user force the guiagent, start_guid will mimic a standard QubesVM
  202. if self.guiagent_installed:
  203. super(QubesHVm, self).start_guid(verbose, notify_function)
  204. else:
  205. if verbose:
  206. print >> sys.stderr, "--> Starting Qubes GUId..."
  207. retcode = subprocess.call ([system_path["qubes_guid_path"], "-d", str(self.stubdom_xid), "-c", self.label.color, "-i", self.label.icon_path, "-l", str(self.label.index)])
  208. if (retcode != 0) :
  209. raise QubesException("Cannot start qubes-guid!")
  210. def start_qrexec_daemon(self, **kwargs):
  211. if self.qrexec_installed:
  212. super(QubesHVm, self).start_qrexec_daemon(**kwargs)
  213. if self._start_guid_first:
  214. if kwargs.get('verbose'):
  215. print >> sys.stderr, "--> Waiting for user '%s' login..." % self.default_user
  216. self.wait_for_session(notify_function=kwargs.get('notify_function', None))
  217. def pause(self):
  218. if dry_run:
  219. return
  220. xc.domain_pause(self.stubdom_xid)
  221. super(QubesHVm, self).pause()
  222. def unpause(self):
  223. if dry_run:
  224. return
  225. xc.domain_unpause(self.stubdom_xid)
  226. super(QubesHVm, self).unpause()
  227. def is_guid_running(self):
  228. # If user force the guiagent, is_guid_running will mimic a standard QubesVM
  229. if self.guiagent_installed:
  230. return super(QubesHVm, self).is_guid_running()
  231. else:
  232. xid = self.stubdom_xid
  233. if xid < 0:
  234. return False
  235. if not os.path.exists('/var/run/qubes/guid-running.%d' % xid):
  236. return False
  237. return True
  238. register_qubes_vm_class(QubesHVm)