qubesvm.py 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2013-2015 Marek Marczykowski-Górecki
  8. # <marmarek@invisiblethingslab.com>
  9. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License along
  22. # with this program; if not, write to the Free Software Foundation, Inc.,
  23. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. #
  25. from __future__ import absolute_import
  26. import base64
  27. import datetime
  28. import itertools
  29. import os
  30. import os.path
  31. import re
  32. import shutil
  33. import subprocess
  34. import sys
  35. import time
  36. import uuid
  37. import warnings
  38. import lxml
  39. import libvirt
  40. import qubes
  41. import qubes.config
  42. import qubes.exc
  43. import qubes.storage
  44. import qubes.storage.domain
  45. import qubes.storage.file
  46. import qubes.tools.qvm_ls
  47. import qubes.utils
  48. import qubes.vm
  49. import qubes.vm.mix.net
  50. qmemman_present = False
  51. try:
  52. import qubes.qmemman.client # pylint: disable=wrong-import-position
  53. qmemman_present = True
  54. except ImportError:
  55. pass
  56. MEM_OVERHEAD_BASE = (3 + 1) * 1024 * 1024
  57. MEM_OVERHEAD_PER_VCPU = 3 * 1024 * 1024 / 2
  58. def _setter_qid(self, prop, value):
  59. # pylint: disable=unused-argument
  60. value = int(value)
  61. if not 0 <= value <= qubes.config.max_qid:
  62. raise ValueError(
  63. '{} value must be between 0 and qubes.config.max_qid'.format(
  64. prop.__name__))
  65. return value
  66. def _setter_name(self, prop, value):
  67. if not isinstance(value, basestring):
  68. raise TypeError('{} value must be string, {!r} found'.format(
  69. prop.__name__, type(value).__name__))
  70. if len(value) > 31:
  71. raise ValueError('{} value must be shorter than 32 characters'.format(
  72. prop.__name__))
  73. # this regexp does not contain '+'; if it had it, we should specifically
  74. # disallow 'lost+found' #1440
  75. if re.match(r"^[a-zA-Z][a-zA-Z0-9_-]*$", value) is None:
  76. raise ValueError('{} value contains illegal characters'.format(
  77. prop.__name__))
  78. if self.is_running():
  79. raise qubes.exc.QubesVMNotHaltedError(
  80. self, 'Cannot change name of running VM')
  81. try:
  82. if self.installed_by_rpm:
  83. raise qubes.exc.QubesException('Cannot rename VM installed by RPM '
  84. '-- first clone VM and then use yum to remove package.')
  85. except AttributeError:
  86. pass
  87. if value in self.app.domains:
  88. raise qubes.exc.QubesValueError(
  89. 'VM named {} alread exists'.format(value))
  90. return value
  91. def _setter_kernel(self, prop, value):
  92. # pylint: disable=unused-argument
  93. if value is None:
  94. return value
  95. value = str(value)
  96. dirname = os.path.join(
  97. qubes.config.system_path['qubes_base_dir'],
  98. qubes.config.system_path['qubes_kernels_base_dir'],
  99. value)
  100. if not os.path.exists(dirname):
  101. raise qubes.exc.QubesPropertyValueError(self, prop, value,
  102. 'Kernel {!r} not installed'.format(value))
  103. for filename in ('vmlinuz', 'initramfs'):
  104. if not os.path.exists(os.path.join(dirname, filename)):
  105. raise qubes.exc.QubesPropertyValueError(self, prop, value,
  106. 'Kernel {!r} not properly installed: missing {!r} file'.format(
  107. value, filename))
  108. return value
  109. def _setter_label(self, prop, value):
  110. # pylint: disable=unused-argument
  111. if isinstance(value, qubes.Label):
  112. return value
  113. if isinstance(value, basestring) and value.startswith('label-'):
  114. return self.app.labels[int(value.split('-', 1)[1])]
  115. return self.app.get_label(value)
  116. def _setter_positive_int(self, prop, value):
  117. # pylint: disable=unused-argument
  118. value = int(value)
  119. if value <= 0:
  120. raise ValueError('Value must be positive')
  121. return value
  122. class QubesVM(qubes.vm.mix.net.NetVMMixin, qubes.vm.BaseVM):
  123. '''Base functionality of Qubes VM shared between all VMs.'''
  124. #
  125. # per-class properties
  126. #
  127. #: directory in which domains of this class will reside
  128. dir_path_prefix = qubes.config.system_path['qubes_appvms_dir']
  129. #
  130. # properties loaded from XML
  131. #
  132. label = qubes.property('label',
  133. setter=_setter_label,
  134. saver=(lambda self, prop, value: 'label-{}'.format(value.index)),
  135. ls_width=14,
  136. doc='''Colourful label assigned to VM. This is where the colour of the
  137. padlock is set.''')
  138. # provides_network = qubes.property('provides_network',
  139. # type=bool, setter=qubes.property.bool,
  140. # doc='`True` if it is NetVM or ProxyVM, false otherwise.')
  141. qid = qubes.property('qid', type=int, write_once=True,
  142. setter=_setter_qid,
  143. clone=False,
  144. ls_width=3,
  145. doc='''Internal, persistent identificator of particular domain. Note
  146. this is different from Xen domid.''')
  147. name = qubes.property('name', type=str,
  148. clone=False,
  149. ls_width=31,
  150. doc='User-specified name of the domain.')
  151. uuid = qubes.property('uuid', type=uuid.UUID, write_once=True,
  152. clone=False,
  153. ls_width=36,
  154. doc='UUID from libvirt.')
  155. hvm = qubes.property('hvm',
  156. type=bool, setter=qubes.property.bool,
  157. default=False,
  158. doc='''Use full virtualisation (HVM) for this qube,
  159. instead of paravirtualisation (PV)''')
  160. # XXX this should be part of qubes.xml
  161. firewall_conf = qubes.property('firewall_conf', type=str,
  162. default='firewall.xml')
  163. installed_by_rpm = qubes.property('installed_by_rpm',
  164. type=bool, setter=qubes.property.bool,
  165. default=False,
  166. doc='''If this domain's image was installed from package tracked by
  167. package manager.''')
  168. memory = qubes.property('memory', type=int,
  169. setter=_setter_positive_int,
  170. default=(lambda self:
  171. qubes.config.defaults['hvm_memory' if self.hvm else 'memory']),
  172. doc='Memory currently available for this VM.')
  173. maxmem = qubes.property('maxmem', type=int,
  174. setter=_setter_positive_int,
  175. default=(lambda self: self.app.host.memory_total / 1024 / 2),
  176. doc='''Maximum amount of memory available for this VM (for the purpose
  177. of the memory balancer).''')
  178. internal = qubes.property('internal', default=False,
  179. type=bool, setter=qubes.property.bool,
  180. doc='''Internal VM (not shown in qubes-manager, don't create appmenus
  181. entries.''')
  182. # FIXME self.app.host could not exist - only self.app.vm required by API
  183. vcpus = qubes.property('vcpus',
  184. type=int,
  185. setter=_setter_positive_int,
  186. default=(lambda self: self.app.host.no_cpus),
  187. ls_width=2,
  188. doc='FIXME')
  189. pool_name = qubes.property('pool_name',
  190. default='default',
  191. doc='storage pool for this qube devices')
  192. dir_path = property('dir_path',
  193. (lambda self: os.path.join(qubes.config.system_path['qubes_base_dir'],
  194. self.dir_path_prefix, self.name)),
  195. doc='Root directory for files related to this domain')
  196. # XXX swallowed uses_default_kernel
  197. # XXX not applicable to HVM?
  198. kernel = qubes.property('kernel', type=str,
  199. setter=_setter_kernel,
  200. default=(lambda self: self.app.default_kernel),
  201. ls_width=12,
  202. doc='Kernel used by this domain.')
  203. # XXX swallowed uses_default_kernelopts
  204. # XXX not applicable to HVM?
  205. kernelopts = qubes.property('kernelopts', type=str, load_stage=4,
  206. default=(lambda self: qubes.config.defaults['kernelopts_pcidevs'] \
  207. if len(self.devices['pci']) > 0 \
  208. else self.template.kernelopts if hasattr(self, 'template') \
  209. else qubes.config.defaults['kernelopts']),
  210. ls_width=30,
  211. doc='Kernel command line passed to domain.')
  212. debug = qubes.property('debug', type=bool, default=False,
  213. setter=qubes.property.bool,
  214. doc='Turns on debugging features.')
  215. # XXX what this exactly does?
  216. # XXX shouldn't this go to standalone VM and TemplateVM, and leave here
  217. # only plain property?
  218. default_user = qubes.property('default_user', type=str,
  219. default=(lambda self: self.template.default_user
  220. if hasattr(self, 'template') else 'user'),
  221. ls_width=12,
  222. doc='FIXME')
  223. # @property
  224. # def default_user(self):
  225. # if self.template is not None:
  226. # return self.template.default_user
  227. # else:
  228. # return self._default_user
  229. qrexec_timeout = qubes.property('qrexec_timeout', type=int, default=60,
  230. setter=_setter_positive_int,
  231. ls_width=3,
  232. doc='''Time in seconds after which qrexec connection attempt is deemed
  233. failed. Operating system inside VM should be able to boot in this
  234. time.''')
  235. autostart = qubes.property('autostart', default=False,
  236. type=bool, setter=qubes.property.bool,
  237. doc='''Setting this to `True` means that VM should be autostarted on
  238. dom0 boot.''')
  239. include_in_backups = qubes.property('include_in_backups',
  240. default=(lambda self: not self.internal),
  241. type=bool, setter=qubes.property.bool,
  242. doc='If this domain is to be included in default backup.')
  243. # format got changed from %s to str(datetime.datetime)
  244. backup_timestamp = qubes.property('backup_timestamp', default=None,
  245. setter=(lambda self, prop, value:
  246. value if isinstance(value, datetime.datetime) else
  247. datetime.datetime.fromtimestamp(int(value))),
  248. saver=(lambda self, prop, value: value.strftime('%s')),
  249. doc='FIXME')
  250. #
  251. # static, class-wide properties
  252. #
  253. #
  254. # properties not loaded from XML, calculated at run-time
  255. #
  256. def __str__(self):
  257. return self.name
  258. # VMM-related
  259. @qubes.tools.qvm_ls.column(width=3)
  260. @property
  261. def xid(self):
  262. '''Xen ID.
  263. Or not Xen, but ID.
  264. '''
  265. if self.libvirt_domain is None:
  266. return -1
  267. try:
  268. return self.libvirt_domain.ID()
  269. except libvirt.libvirtError as e:
  270. if e.get_error_code() == libvirt.VIR_ERR_NO_DOMAIN:
  271. return -1
  272. else:
  273. self.log.exception('libvirt error code: {!r}'.format(
  274. e.get_error_code()))
  275. raise
  276. @property
  277. def libvirt_domain(self):
  278. '''Libvirt domain object from libvirt.
  279. May be :py:obj:`None`, if libvirt knows nothing about this domain.
  280. '''
  281. if self._libvirt_domain is not None:
  282. return self._libvirt_domain
  283. # XXX _update_libvirt_domain?
  284. try:
  285. self._libvirt_domain = self.app.vmm.libvirt_conn.lookupByUUID(
  286. self.uuid.bytes)
  287. except libvirt.libvirtError as e:
  288. if e.get_error_code() == libvirt.VIR_ERR_NO_DOMAIN:
  289. self._update_libvirt_domain()
  290. else:
  291. raise
  292. return self._libvirt_domain
  293. @property
  294. def block_devices(self):
  295. ''' Return all :py:class:`qubes.devices.BlockDevice`s for current domain
  296. for serialization in the libvirt XML template as <disk>.
  297. '''
  298. return [v.block_device() for v in self.volumes.values()]
  299. @property
  300. def qdb(self):
  301. '''QubesDB handle for this domain.'''
  302. if self._qdb_connection is None:
  303. if self.is_running():
  304. import qubesdb # pylint: disable=import-error
  305. self._qdb_connection = qubesdb.QubesDB(self.name)
  306. return self._qdb_connection
  307. # XXX this should go to to AppVM?
  308. @property
  309. def private_img(self):
  310. '''Location of private image of the VM (that contains :file:`/rw` \
  311. and :file:`/home`).'''
  312. warnings.warn("volatile_img is deprecated, use volumes['private'].vid",
  313. DeprecationWarning)
  314. return self.volumes['private'].vid
  315. # XXX this should go to to AppVM? or TemplateVM?
  316. @property
  317. def root_img(self):
  318. '''Location of root image.'''
  319. warnings.warn("root_img is deprecated, use volumes['root'].vid",
  320. DeprecationWarning)
  321. return self.volumes['root'].vid
  322. # XXX and this should go to exactly where? DispVM has it.
  323. @property
  324. def volatile_img(self):
  325. '''Volatile image that overlays :py:attr:`root_img`.'''
  326. warnings.warn("volatile_img is deprecated, use volumes['volatile'].vid",
  327. DeprecationWarning)
  328. return self.volumes['volatile'].vid
  329. # XXX shouldn't this go elsewhere?
  330. @property
  331. def updateable(self):
  332. '''True if this machine may be updated on its own.'''
  333. return not hasattr(self, 'template')
  334. @property
  335. def icon_path(self):
  336. return os.path.join(self.dir_path, 'icon.png')
  337. @property
  338. def conf_file(self):
  339. return os.path.join(self.dir_path, 'libvirt.xml')
  340. # XXX I don't know what to do with these; probably should be isinstance(...)
  341. def is_template(self):
  342. warnings.warn('vm.is_template() is deprecated, use isinstance()',
  343. DeprecationWarning)
  344. import qubes.vm.templatevm # pylint: disable=redefined-outer-name
  345. return isinstance(self, qubes.vm.templatevm.TemplateVM)
  346. def is_appvm(self):
  347. warnings.warn('vm.is_appvm() is deprecated, use isinstance()',
  348. DeprecationWarning)
  349. import qubes.vm.appvm # pylint: disable=redefined-outer-name
  350. return isinstance(self, qubes.vm.appvm.AppVM)
  351. def is_proxyvm(self):
  352. warnings.warn('vm.is_proxyvm() is deprecated',
  353. DeprecationWarning)
  354. return self.netvm is not None and self.provides_network
  355. def is_disposablevm(self):
  356. warnings.warn('vm.is_disposable() is deprecated, use isinstance()',
  357. DeprecationWarning)
  358. import qubes.vm.dispvm # pylint: disable=redefined-outer-name
  359. return isinstance(self, qubes.vm.dispvm.DispVM)
  360. def is_netvm(self):
  361. warnings.warn('vm.is_netvm() is deprecated, use isinstance()',
  362. DeprecationWarning)
  363. return isinstance(self, qubes.vm.mix.net.NetVMMixin) \
  364. and self.provides_network
  365. # network-related
  366. #
  367. # constructor
  368. #
  369. def __init__(self, app, xml, volume_config=None, **kwargs):
  370. super(QubesVM, self).__init__(app, xml, **kwargs)
  371. self.volumes = {}
  372. self.storage = None
  373. self.volume_config = {}
  374. if volume_config is None:
  375. volume_config = {}
  376. if hasattr(self, 'volume_config'):
  377. if xml is not None:
  378. for node in xml.xpath('volume-config/volume'):
  379. name = node.get('name')
  380. assert name
  381. for key, value in node.items():
  382. self.volume_config[name][key] = value
  383. for name, conf in volume_config.items():
  384. for key, value in conf.items():
  385. self.volume_config[name][key] = value
  386. elif volume_config:
  387. raise TypeError(
  388. 'volume_config specified, but {} did not expect that.'.format(
  389. self.__class__.__name__))
  390. import qubes.vm.adminvm # pylint: disable=redefined-outer-name
  391. # Init private attrs
  392. self._libvirt_domain = None
  393. self._qdb_connection = None
  394. if xml is None:
  395. # we are creating new VM and attributes came through kwargs
  396. assert hasattr(self, 'qid')
  397. assert hasattr(self, 'name')
  398. # Linux specific cap: max memory can't scale beyond 10.79*init_mem
  399. # see https://groups.google.com/forum/#!topic/qubes-devel/VRqkFj1IOtA
  400. if self.maxmem > self.memory * 10:
  401. self.maxmem = self.memory * 10
  402. # By default allow use all VCPUs
  403. # if not hasattr(self, 'vcpus') and not self.app.vmm.offline_mode:
  404. # self.vcpus = self.app.host.no_cpus
  405. if len(self.devices['pci']) > 0:
  406. # Force meminfo-writer disabled when VM have PCI devices
  407. self.features['meminfo-writer'] = None
  408. elif not isinstance(self, qubes.vm.adminvm.AdminVM) \
  409. and 'meminfo-writer' not in self.features:
  410. # Always set if meminfo-writer should be active or not
  411. self.features['meminfo-writer'] = '1'
  412. if xml is None:
  413. # new qube, disable updates check if requested for new qubes
  414. # TODO: when features (#1637) are done, migrate to plugin
  415. if not self.app.check_updates_vm:
  416. self.features['check-updates'] = None
  417. # will be initialized after loading all the properties
  418. # fire hooks
  419. if xml is None:
  420. self.events_enabled = True
  421. self.fire_event('domain-init')
  422. def __xml__(self):
  423. element = super(QubesVM, self).__xml__()
  424. if hasattr(self, 'volumes'):
  425. volume_config_node = lxml.etree.Element('volume-config')
  426. for volume in self.volumes.values():
  427. volume_config_node.append(volume.__xml__())
  428. element.append(volume_config_node)
  429. return element
  430. #
  431. # event handlers
  432. #
  433. @qubes.events.handler('domain-init', 'domain-load')
  434. def on_domain_init_loaded(self, event):
  435. # pylint: disable=unused-argument
  436. if not hasattr(self, 'uuid'):
  437. self.uuid = uuid.uuid4()
  438. # Initialize VM image storage class
  439. # XXX why not in constructor?
  440. self.storage = qubes.storage.Storage(self)
  441. vm_pool = qubes.storage.domain.DomainPool(self)
  442. self.app.pools[vm_pool.name] = vm_pool
  443. @qubes.events.handler('property-set:label')
  444. def on_property_set_label(self, event, name, new_label, old_label=None):
  445. # pylint: disable=unused-argument
  446. if self.icon_path:
  447. try:
  448. os.remove(self.icon_path)
  449. except OSError:
  450. pass
  451. if hasattr(os, "symlink"):
  452. os.symlink(new_label.icon_path, self.icon_path)
  453. # FIXME: some os-independent wrapper?
  454. subprocess.call(['sudo', 'xdg-icon-resource', 'forceupdate'])
  455. else:
  456. shutil.copy(new_label.icon_path, self.icon_path)
  457. @qubes.events.handler('property-pre-set:name')
  458. def on_property_pre_set_name(self, event, name, newvalue, oldvalue=None):
  459. # pylint: disable=unused-argument
  460. if newvalue in [d.name for d in self.app.domains.values()]:
  461. raise qubes.exc.QubesValueError(
  462. "A domain with name %s already exists", newvalue)
  463. # TODO not self.is_stopped() would be more appropriate
  464. if self.is_running():
  465. raise qubes.exc.QubesVMNotHaltedError(
  466. 'Cannot change name of running domain {!r}'.format(oldvalue))
  467. if self.autostart:
  468. subprocess.check_call(['sudo', 'systemctl', '-q', 'disable',
  469. 'qubes-vm@{}.service'.format(oldvalue)])
  470. try:
  471. self.app.domains[newvalue]
  472. except KeyError:
  473. pass
  474. else:
  475. raise qubes.exc.QubesValueError(
  476. 'VM named {!r} already exists'.format(newvalue))
  477. @qubes.events.handler('property-set:name')
  478. def on_property_set_name(self, event, name, new_name, old_name=None):
  479. # pylint: disable=unused-argument
  480. self.init_log()
  481. if self._libvirt_domain is not None:
  482. self.libvirt_domain.undefine()
  483. self._libvirt_domain = None
  484. if self._qdb_connection is not None:
  485. self._qdb_connection.close()
  486. self._qdb_connection = None
  487. self.storage.rename(old_name, new_name)
  488. self._update_libvirt_domain()
  489. if self.autostart:
  490. self.autostart = self.autostart
  491. @qubes.events.handler('property-pre-set:autostart')
  492. def on_property_pre_set_autostart(self, event, prop, value,
  493. oldvalue=None):
  494. # pylint: disable=unused-argument
  495. # workaround https://bugzilla.redhat.com/show_bug.cgi?id=1181922
  496. if value:
  497. retcode = subprocess.call(
  498. ["sudo", "ln", "-sf",
  499. "/usr/lib/systemd/system/qubes-vm@.service",
  500. "/etc/systemd/system/multi-user.target.wants/qubes-vm@{"
  501. "}".format(self.name)])
  502. else:
  503. retcode = subprocess.call(
  504. ['sudo', 'systemctl', 'disable',
  505. 'qubes-vm@{}.service'.format(self.name)])
  506. if retcode:
  507. raise qubes.exc.QubesException(
  508. 'Failed to set autostart for VM in systemd')
  509. @qubes.events.handler('device-pre-attach:pci')
  510. def on_device_pre_attached_pci(self, event, pci):
  511. # pylint: disable=unused-argument
  512. if not os.path.exists('/sys/bus/pci/devices/0000:{}'.format(pci)):
  513. raise qubes.exc.QubesException('Invalid PCI device: {}'.format(pci))
  514. if not self.is_running():
  515. return
  516. try:
  517. # TODO: libvirt-ise
  518. subprocess.check_call(
  519. ['sudo', qubes.config.system_path['qubes_pciback_cmd'], pci])
  520. subprocess.check_call(
  521. ['sudo', 'xl', 'pci-attach', str(self.xid), pci])
  522. except subprocess.CalledProcessError as e:
  523. self.log.exception('Failed to attach PCI device {!r} on the fly,'
  524. ' changes will be seen after VM restart.'.format(pci), e)
  525. @qubes.events.handler('device-pre-detach:pci')
  526. def on_device_pre_detached_pci(self, event, pci):
  527. # pylint: disable=unused-argument
  528. if not self.is_running():
  529. return
  530. # TODO: libvirt-ise
  531. p = subprocess.Popen(['xl', 'pci-list', str(self.xid)],
  532. stdout=subprocess.PIPE)
  533. result = p.communicate()
  534. m = re.search(r"^(\d+.\d+)\s+0000:%s$" % pci, result[0],
  535. flags=re.MULTILINE)
  536. if not m:
  537. print >>sys.stderr, "Device %s already detached" % pci
  538. return
  539. vmdev = m.group(1)
  540. try:
  541. self.run_service("qubes.DetachPciDevice",
  542. user="root", input="00:%s" % vmdev)
  543. subprocess.check_call(
  544. ['sudo', 'xl', 'pci-detach', str(self.xid), pci])
  545. except subprocess.CalledProcessError as e:
  546. self.log.exception('Failed to detach PCI device {!r} on the fly,'
  547. ' changes will be seen after VM restart.'.format(pci), e)
  548. #
  549. # methods for changing domain state
  550. #
  551. def start(self, preparing_dvm=False, start_guid=True,
  552. notify_function=None, mem_required=None):
  553. '''Start domain
  554. :param bool preparing_dvm: FIXME
  555. :param bool start_guid: FIXME
  556. :param collections.Callable notify_function: FIXME
  557. :param int mem_required: FIXME
  558. '''
  559. # Intentionally not used is_running(): eliminate also "Paused",
  560. # "Crashed", "Halting"
  561. if self.get_power_state() != 'Halted':
  562. raise qubes.exc.QubesVMNotHaltedError(self)
  563. self.log.info('Starting {}'.format(self.name))
  564. self.fire_event_pre('domain-pre-start', preparing_dvm=preparing_dvm,
  565. start_guid=start_guid, mem_required=mem_required)
  566. self.verify_files()
  567. if self.netvm is not None:
  568. if self.netvm.qid != 0:
  569. if not self.netvm.is_running():
  570. self.netvm.start(start_guid=start_guid,
  571. notify_function=notify_function)
  572. self.storage.start()
  573. self._update_libvirt_domain()
  574. qmemman_client = self.request_memory(mem_required)
  575. # Bind pci devices to pciback driver
  576. for pci in self.devices['pci']:
  577. try:
  578. node = self.app.vmm.libvirt_conn.nodeDeviceLookupByName(
  579. 'pci_0000_' + pci.replace(':', '_').replace('.', '_'))
  580. except libvirt.libvirtError as e:
  581. if e.get_error_code() == libvirt.VIR_ERR_NO_NODE_DEVICE:
  582. raise qubes.exc.QubesException(
  583. 'PCI device {!r} does not exist (domain {!r})'.format(
  584. pci, self.name))
  585. try:
  586. node.dettach()
  587. except libvirt.libvirtError as e:
  588. if e.get_error_code() == libvirt.VIR_ERR_INTERNAL_ERROR:
  589. # allreaddy dettached
  590. pass
  591. else:
  592. raise
  593. self.libvirt_domain.createWithFlags(libvirt.VIR_DOMAIN_START_PAUSED)
  594. try:
  595. self.fire_event('domain-spawn',
  596. preparing_dvm=preparing_dvm, start_guid=start_guid)
  597. self.log.info('Setting Qubes DB info for the VM')
  598. self.start_qubesdb()
  599. self.create_qdb_entries()
  600. if preparing_dvm:
  601. self.qdb.write('/dvm', '1')
  602. self.log.warning('Activating the {} VM'.format(self.name))
  603. self.libvirt_domain.resume()
  604. # close() is not really needed, because the descriptor is
  605. # close-on-exec anyway, the reason to postpone close() is that
  606. # possibly xl is not done constructing the domain after its main
  607. # process exits so we close() when we know the domain is up the
  608. # successful unpause is some indicator of it
  609. if qmemman_client:
  610. qmemman_client.close()
  611. # if self._start_guid_first and start_guid and not preparing_dvm \
  612. # and os.path.exists('/var/run/shm.id'):
  613. # self.start_guid()
  614. if not preparing_dvm:
  615. self.start_qrexec_daemon()
  616. self.fire_event('domain-start',
  617. preparing_dvm=preparing_dvm, start_guid=start_guid)
  618. except: # pylint: disable=bare-except
  619. if self.is_running() or self.is_paused():
  620. # This avoids losing the exception if an exception is raised in
  621. # self.force_shutdown(), because the vm is not running or paused
  622. self.force_shutdown()
  623. raise
  624. return self
  625. def shutdown(self, force=False):
  626. '''Shutdown domain.
  627. :raises qubes.exc.QubesVMNotStartedError: \
  628. when domain is already shut down.
  629. '''
  630. if not self.is_running(): # TODO not self.is_halted()
  631. raise qubes.exc.QubesVMNotStartedError(self)
  632. self.fire_event_pre('domain-pre-shutdown', force=force)
  633. # try to gracefully detach PCI devices before shutdown, to mitigate
  634. # timeouts on forcible detach at domain destroy; if that fails, too bad
  635. for pci in self.devices['pci']:
  636. try:
  637. self.libvirt_domain.detachDevice(
  638. lxml.etree.tostring(self.lvxml_pci_dev(pci)))
  639. except libvirt.libvirtError as e:
  640. self.log.warning(
  641. 'error while gracefully detaching PCI device ({!r}) during'
  642. ' shutdown of {!r}; error code: {!r}; continuing'
  643. ' anyway'.format(pci, self.name, e.get_error_code()),
  644. exc_info=1)
  645. self.libvirt_domain.shutdown()
  646. self.storage.stop()
  647. return self
  648. def kill(self):
  649. '''Forcefuly shutdown (destroy) domain.
  650. :raises qubes.exc.QubesVMNotStartedError: \
  651. when domain is already shut down.
  652. '''
  653. if not self.is_running() and not self.is_paused():
  654. raise qubes.exc.QubesVMNotStartedError(self)
  655. self.libvirt_domain.destroy()
  656. self.storage.stop()
  657. return self
  658. def force_shutdown(self, *args, **kwargs):
  659. '''Deprecated alias for :py:meth:`kill`'''
  660. warnings.warn(
  661. 'Call to deprecated function force_shutdown(), use kill() instead',
  662. DeprecationWarning, stacklevel=2)
  663. self.kill(*args, **kwargs)
  664. return self
  665. def suspend(self):
  666. '''Suspend (pause) domain.
  667. :raises qubes.exc.QubesVMNotRunnignError: \
  668. when domain is already shut down.
  669. :raises qubes.exc.QubesNotImplemetedError: \
  670. when domain has PCI devices attached.
  671. '''
  672. if not self.is_running() and not self.is_paused():
  673. raise qubes.exc.QubesVMNotRunningError(self)
  674. if len(self.devices['pci']) > 0:
  675. raise qubes.exc.QubesNotImplementedError(
  676. 'Cannot suspend domain {!r} which has PCI devices attached' \
  677. .format(self.name))
  678. else:
  679. if self.hvm:
  680. self.libvirt_domain.pause()
  681. else:
  682. self.libvirt_domain.suspend()
  683. return self
  684. def pause(self):
  685. '''Pause (suspend) domain. This currently delegates to \
  686. :py:meth:`suspend`.'''
  687. if not self.is_running():
  688. raise qubes.exc.QubesVMNotRunningError(self)
  689. self.suspend()
  690. return self
  691. def resume(self):
  692. '''Resume suspended domain.
  693. :raises qubes.exc.QubesVMNotSuspendedError: when machine is not paused
  694. :raises qubes.exc.QubesVMError: when machine is suspended
  695. '''
  696. if self.get_power_state() == "Suspended":
  697. raise qubes.exc.QubesVMError(self,
  698. 'Cannot resume suspended domain {!r}'.format(self.name))
  699. else:
  700. self.unpause()
  701. return self
  702. def unpause(self):
  703. '''Resume (unpause) a domain'''
  704. if not self.is_paused():
  705. raise qubes.exc.QubesVMNotPausedError(self)
  706. self.libvirt_domain.resume()
  707. return self
  708. def run(self, command, user=None, autostart=False, notify_function=None,
  709. passio=False, passio_popen=False, passio_stderr=False,
  710. ignore_stderr=False, localcmd=None, wait=False, gui=True,
  711. filter_esc=False):
  712. '''Run specified command inside domain
  713. :param str command: the command to be run
  714. :param str user: user to run the command as
  715. :param bool autostart: if :py:obj:`True`, machine will be started if \
  716. it is not running
  717. :param collections.Callable notify_function: FIXME, may go away
  718. :param bool passio: FIXME
  719. :param bool passio_popen: if :py:obj:`True`, \
  720. :py:class:`subprocess.Popen` object has connected ``stdin`` and \
  721. ``stdout``
  722. :param bool passio_stderr: if :py:obj:`True`, \
  723. :py:class:`subprocess.Popen` has additionaly ``stderr`` connected
  724. :param bool ignore_stderr: if :py:obj:`True`, ``stderr`` is connected \
  725. to :file:`/dev/null`
  726. :param str localcmd: local command to communicate with remote command
  727. :param bool wait: if :py:obj:`True`, wait for command completion
  728. :param bool gui: when autostarting, also start gui daemon
  729. :param bool filter_esc: filter escape sequences to protect terminal \
  730. emulator
  731. '''
  732. if user is None:
  733. user = self.default_user
  734. null = None
  735. if not self.is_running() and not self.is_paused():
  736. if not autostart:
  737. raise qubes.exc.QubesVMNotRunningError(self)
  738. if notify_function is not None:
  739. notify_function('info',
  740. 'Starting the {!r} VM...'.format(self.name))
  741. self.start(start_guid=gui, notify_function=notify_function)
  742. if self.is_paused():
  743. # XXX what about autostart?
  744. raise qubes.exc.QubesVMNotRunningError(
  745. self, 'Domain {!r} is paused'.format(self.name))
  746. if not self.is_qrexec_running():
  747. raise qubes.exc.QubesVMError(
  748. self, 'Domain {!r}: qrexec not connected'.format(self.name))
  749. self.fire_event_pre('domain-cmd-pre-run', start_guid=gui)
  750. args = [qubes.config.system_path['qrexec_client_path'],
  751. '-d', str(self.name),
  752. '{}:{}'.format(user, command)]
  753. if localcmd is not None:
  754. args += ['-l', localcmd]
  755. if filter_esc:
  756. args += ['-t']
  757. if os.isatty(sys.stderr.fileno()):
  758. args += ['-T']
  759. call_kwargs = {}
  760. if ignore_stderr or not passio:
  761. null = open("/dev/null", "r+")
  762. call_kwargs['stderr'] = null
  763. if not passio:
  764. call_kwargs['stdin'] = null
  765. call_kwargs['stdout'] = null
  766. if passio_popen:
  767. popen_kwargs = {
  768. 'stdout': subprocess.PIPE,
  769. 'stdin': subprocess.PIPE
  770. }
  771. if passio_stderr:
  772. popen_kwargs['stderr'] = subprocess.PIPE
  773. else:
  774. popen_kwargs['stderr'] = call_kwargs.get('stderr', None)
  775. p = subprocess.Popen(args, **popen_kwargs)
  776. if null:
  777. null.close()
  778. return p
  779. if not wait and not passio:
  780. args += ["-e"]
  781. retcode = subprocess.call(args, **call_kwargs)
  782. if null:
  783. null.close()
  784. return retcode
  785. def run_service(self, service, source=None, user=None,
  786. passio_popen=False, input=None, localcmd=None, gui=False,
  787. wait=True, passio_stderr=False):
  788. '''Run service on this VM
  789. **passio_popen** and **input** are mutually exclusive.
  790. :param str service: service name
  791. :param qubes.vm.qubesvm.QubesVM: source domain as presented to this VM
  792. :param str user: username to run service as
  793. :param bool passio_popen: passed verbatim to :py:meth:`run`
  794. :param str input: string passed as input to service
  795. ''' # pylint: disable=redefined-builtin
  796. if len([i for i in (input, passio_popen, localcmd) if i]) > 1:
  797. raise TypeError(
  798. 'input, passio_popen and localcmd cannot be used together')
  799. if not wait and (localcmd or input):
  800. raise ValueError("Cannot use wait=False with input or "
  801. "localcmd specified")
  802. if passio_stderr and not passio_popen:
  803. raise TypeError('passio_stderr can be used only with passio_popen')
  804. if input:
  805. # Internally use passio_popen, but do not return POpen object to
  806. # the user - use internally for p.communicate()
  807. passio_popen = True
  808. source = 'dom0' if source is None else self.app.domains[source].name
  809. p = self.run('QUBESRPC {} {}'.format(service, source),
  810. localcmd=localcmd, passio_popen=passio_popen, user=user, wait=wait,
  811. gui=gui, passio_stderr=passio_stderr)
  812. if input:
  813. p.communicate(input)
  814. return p.returncode
  815. else:
  816. return p
  817. def request_memory(self, mem_required=None):
  818. # overhead of per-qube/per-vcpu Xen structures,
  819. # taken from OpenStack nova/virt/xenapi/driver.py
  820. # see https://wiki.openstack.org/wiki/XenServer/Overhead
  821. # add an extra MB because Nova rounds up to MBs
  822. if not qmemman_present:
  823. return
  824. if mem_required is None:
  825. mem_required = int(self.memory) * 1024 * 1024
  826. qmemman_client = qubes.qmemman.client.QMemmanClient()
  827. try:
  828. mem_required_with_overhead = mem_required + MEM_OVERHEAD_BASE \
  829. + self.vcpus * MEM_OVERHEAD_PER_VCPU
  830. got_memory = qmemman_client.request_memory(
  831. mem_required_with_overhead)
  832. except IOError as e:
  833. raise IOError('Failed to connect to qmemman: {!s}'.format(e))
  834. if not got_memory:
  835. qmemman_client.close()
  836. raise qubes.exc.QubesMemoryError(self)
  837. return qmemman_client
  838. def start_qrexec_daemon(self):
  839. '''Start qrexec daemon.
  840. :raises OSError: when starting fails.
  841. '''
  842. self.log.debug('Starting the qrexec daemon')
  843. qrexec_args = [str(self.xid), self.name, self.default_user]
  844. if not self.debug:
  845. qrexec_args.insert(0, "-q")
  846. qrexec_env = os.environ.copy()
  847. if not self.features.check_with_template('qrexec', not self.hvm):
  848. self.log.debug(
  849. 'Starting the qrexec daemon in background, because of features')
  850. qrexec_env['QREXEC_STARTUP_NOWAIT'] = '1'
  851. else:
  852. qrexec_env['QREXEC_STARTUP_TIMEOUT'] = str(self.qrexec_timeout)
  853. try:
  854. subprocess.check_call(
  855. [qubes.config.system_path["qrexec_daemon_path"]] + qrexec_args,
  856. env=qrexec_env)
  857. except subprocess.CalledProcessError:
  858. raise qubes.exc.QubesVMError(self, 'Cannot execute qrexec-daemon!')
  859. def start_qubesdb(self):
  860. '''Start QubesDB daemon.
  861. :raises OSError: when starting fails.
  862. '''
  863. self.log.info('Starting Qubes DB')
  864. # FIXME #1694 #1241
  865. retcode = subprocess.call([
  866. qubes.config.system_path["qubesdb_daemon_path"],
  867. str(self.xid),
  868. self.name])
  869. if retcode != 0:
  870. raise qubes.exc.QubesException('Cannot execute qubesdb-daemon')
  871. def wait_for_session(self):
  872. '''Wait until machine finished boot sequence.
  873. This is done by executing qubes RPC call that checks if dummy system
  874. service (which is started late in standard runlevel) is active.
  875. '''
  876. self.log.info('Waiting for qubes-session')
  877. # Note : User root is redefined to SYSTEM in the Windows agent code
  878. p = self.run('QUBESRPC qubes.WaitForSession none',
  879. user="root", passio_popen=True, gui=False, wait=True)
  880. p.communicate(input=self.default_user)
  881. # TODO rename to create
  882. def create_on_disk(self, source_template=None):
  883. '''Create files needed for VM.
  884. :param qubes.vm.templatevm.TemplateVM source_template: Template to use
  885. (if :py:obj:`None`, use domain's own template
  886. '''
  887. if source_template is None and hasattr(self, 'template'):
  888. # pylint: disable=no-member
  889. source_template = self.template
  890. self.log.info('Creating directory: {0}'.format(self.dir_path))
  891. os.makedirs(self.dir_path, mode=0o775)
  892. self.storage.create_on_disk(source_template)
  893. self.log.info('Creating icon symlink: {} -> {}'.format(
  894. self.icon_path, self.label.icon_path))
  895. if hasattr(os, "symlink"):
  896. os.symlink(self.label.icon_path, self.icon_path)
  897. else:
  898. shutil.copy(self.label.icon_path, self.icon_path)
  899. # fire hooks
  900. self.fire_event('domain-create-on-disk', source_template)
  901. # TODO move to storage
  902. def resize_private_img(self, size):
  903. '''Resize private image.'''
  904. warnings.warn(
  905. "resize_private_img is deprecated, use volumes[name].resize()",
  906. DeprecationWarning)
  907. self.volumes['private'].resize(size)
  908. # and then the filesystem
  909. # FIXME move this to qubes.storage.xen.XenVMStorage
  910. retcode = 0
  911. if self.is_running():
  912. # pylint: disable=redefined-variable-type
  913. retcode = self.run('''
  914. while [ "`blockdev --getsize64 /dev/xvdb`" -lt {0} ]; do
  915. head /dev/xvdb >/dev/null;
  916. sleep 0.2;
  917. done;
  918. resize2fs /dev/xvdb'''.format(size), user="root", wait=True)
  919. if retcode != 0:
  920. raise qubes.exc.QubesException('resize2fs failed')
  921. def resize_root_img(self, size, allow_start=False):
  922. warnings.warn(
  923. "resize_root_img is deprecated, use volumes[name].resize()",
  924. DeprecationWarning)
  925. self.volumes['root'].resize(size)
  926. if not allow_start:
  927. raise qubes.exc.QubesException(
  928. 'The qube has to be started to complete the operation, but is'
  929. ' required not to start. Either run the operation again'
  930. ' allowing starting of the qube this time, or run resize2fs'
  931. ' in the qube manually.')
  932. self.start(start_guid=False)
  933. # TODO run_service #1695
  934. self.run('resize2fs /dev/mapper/dmroot', user='root',
  935. wait=True, gui=False)
  936. self.shutdown()
  937. while self.is_running(): #1696
  938. time.sleep(1)
  939. def remove_from_disk(self):
  940. '''Remove domain remnants from disk.'''
  941. self.fire_event('domain-remove-from-disk')
  942. self.storage.remove()
  943. shutil.rmtree(self.dir_path)
  944. def clone_disk_files(self, src):
  945. '''Clone files from other vm.
  946. :param qubes.vm.qubesvm.QubesVM src: source VM
  947. '''
  948. if src.is_running(): # XXX what about paused?
  949. raise qubes.exc.QubesVMNotHaltedError(
  950. self, 'Cannot clone a running domain {!r}'.format(self.name))
  951. if hasattr(src, 'volume_config'):
  952. self.volume_config = src.volume_config
  953. self.storage = qubes.storage.Storage(self)
  954. self.storage.clone(src)
  955. if src.icon_path is not None \
  956. and os.path.exists(src.dir_path) \
  957. and self.icon_path is not None:
  958. if os.path.islink(src.icon_path):
  959. icon_path = os.readlink(src.icon_path)
  960. self.log.info(
  961. 'Creating icon symlink {} -> {}'.format(
  962. self.icon_path, icon_path))
  963. os.symlink(icon_path, self.icon_path)
  964. else:
  965. self.log.info(
  966. 'Copying icon {} -> {}'.format(
  967. src.icon_path, self.icon_path))
  968. shutil.copy(src.icon_path, self.icon_path)
  969. # fire hooks
  970. self.fire_event('domain-clone-files', src)
  971. #
  972. # methods for querying domain state
  973. #
  974. # state of the machine
  975. def get_power_state(self):
  976. '''Return power state description string.
  977. Return value may be one of those:
  978. =============== ========================================================
  979. return value meaning
  980. =============== ========================================================
  981. ``'Halted'`` Machine is not active.
  982. ``'Transient'`` Machine is running, but does not have :program:`guid`
  983. or :program:`qrexec` available.
  984. ``'Running'`` Machine is ready and running.
  985. ``'Paused'`` Machine is paused (currently not available, see below).
  986. ``'Suspended'`` Machine is S3-suspended.
  987. ``'Halting'`` Machine is in process of shutting down.
  988. ``'Dying'`` Machine crashed and is unusable.
  989. ``'Crashed'`` Machine crashed and is unusable, probably because of
  990. bug in dom0.
  991. ``'NA'`` Machine is in unknown state (most likely libvirt domain
  992. is undefined).
  993. =============== ========================================================
  994. ``Paused`` state is currently unavailable because of missing code in
  995. libvirt/xen glue.
  996. FIXME: graph below may be incomplete and wrong. Click on method name to
  997. see its documentation.
  998. .. graphviz::
  999. digraph {
  1000. node [fontname="sans-serif"];
  1001. edge [fontname="mono"];
  1002. Halted;
  1003. NA;
  1004. Dying;
  1005. Crashed;
  1006. Transient;
  1007. Halting;
  1008. Running;
  1009. Paused [color=gray75 fontcolor=gray75];
  1010. Suspended;
  1011. NA -> Halted;
  1012. Halted -> NA [constraint=false];
  1013. Halted -> Transient
  1014. [xlabel="start()" URL="#qubes.vm.qubesvm.QubesVM.start"];
  1015. Transient -> Running;
  1016. Running -> Halting
  1017. [xlabel="shutdown()"
  1018. URL="#qubes.vm.qubesvm.QubesVM.shutdown"
  1019. constraint=false];
  1020. Halting -> Dying -> Halted [constraint=false];
  1021. /* cosmetic, invisible edges to put rank constraint */
  1022. Dying -> Halting [style="invis"];
  1023. Halting -> Transient [style="invis"];
  1024. Running -> Halted
  1025. [label="force_shutdown()"
  1026. URL="#qubes.vm.qubesvm.QubesVM.force_shutdown"
  1027. constraint=false];
  1028. Running -> Crashed [constraint=false];
  1029. Crashed -> Halted [constraint=false];
  1030. Running -> Paused
  1031. [label="pause()" URL="#qubes.vm.qubesvm.QubesVM.pause"
  1032. color=gray75 fontcolor=gray75];
  1033. Running -> Suspended
  1034. [label="pause()" URL="#qubes.vm.qubesvm.QubesVM.pause"
  1035. color=gray50 fontcolor=gray50];
  1036. Paused -> Running
  1037. [label="unpause()" URL="#qubes.vm.qubesvm.QubesVM.unpause"
  1038. color=gray75 fontcolor=gray75];
  1039. Suspended -> Running
  1040. [label="unpause()" URL="#qubes.vm.qubesvm.QubesVM.unpause"
  1041. color=gray50 fontcolor=gray50];
  1042. Running -> Suspended
  1043. [label="suspend()" URL="#qubes.vm.qubesvm.QubesVM.suspend"];
  1044. Suspended -> Running
  1045. [label="resume()" URL="#qubes.vm.qubesvm.QubesVM.resume"];
  1046. { rank=source; Halted NA };
  1047. { rank=same; Transient Halting };
  1048. { rank=same; Crashed Dying };
  1049. { rank=sink; Paused Suspended };
  1050. }
  1051. .. seealso::
  1052. http://wiki.libvirt.org/page/VM_lifecycle
  1053. Description of VM life cycle from the point of view of libvirt.
  1054. https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainState
  1055. Libvirt's enum describing precise state of a domain.
  1056. ''' # pylint: disable=too-many-return-statements
  1057. libvirt_domain = self.libvirt_domain
  1058. if libvirt_domain is None:
  1059. return 'Halted'
  1060. try:
  1061. if libvirt_domain.isActive():
  1062. # pylint: disable=line-too-long
  1063. if libvirt_domain.state()[0] == libvirt.VIR_DOMAIN_PAUSED:
  1064. return "Paused"
  1065. elif libvirt_domain.state()[0] == libvirt.VIR_DOMAIN_CRASHED:
  1066. return "Crashed"
  1067. elif libvirt_domain.state()[0] == libvirt.VIR_DOMAIN_SHUTDOWN:
  1068. return "Halting"
  1069. elif libvirt_domain.state()[0] == libvirt.VIR_DOMAIN_SHUTOFF:
  1070. return "Dying"
  1071. elif libvirt_domain.state()[0] == libvirt.VIR_DOMAIN_PMSUSPENDED:
  1072. return "Suspended"
  1073. else:
  1074. if not self.is_fully_usable():
  1075. return "Transient"
  1076. else:
  1077. return "Running"
  1078. else:
  1079. return 'Halted'
  1080. except libvirt.libvirtError as e:
  1081. if e.get_error_code() == libvirt.VIR_ERR_NO_DOMAIN:
  1082. return 'Halted'
  1083. else:
  1084. raise
  1085. assert False
  1086. def is_running(self):
  1087. '''Check whether this domain is running.
  1088. :returns: :py:obj:`True` if this domain is started, \
  1089. :py:obj:`False` otherwise.
  1090. :rtype: bool
  1091. '''
  1092. if self.app.vmm.offline_mode:
  1093. return False
  1094. # TODO context manager #1693
  1095. return self.libvirt_domain and self.libvirt_domain.isActive()
  1096. def is_paused(self):
  1097. '''Check whether this domain is paused.
  1098. :returns: :py:obj:`True` if this domain is paused, \
  1099. :py:obj:`False` otherwise.
  1100. :rtype: bool
  1101. '''
  1102. return self.libvirt_domain \
  1103. and self.libvirt_domain.state() == libvirt.VIR_DOMAIN_PAUSED
  1104. def is_qrexec_running(self):
  1105. '''Check whether qrexec for this domain is available.
  1106. :returns: :py:obj:`True` if qrexec is running, \
  1107. :py:obj:`False` otherwise.
  1108. :rtype: bool
  1109. '''
  1110. if self.xid < 0:
  1111. return False
  1112. return os.path.exists('/var/run/qubes/qrexec.%s' % self.name)
  1113. def is_fully_usable(self):
  1114. return all(self.fire_event('domain-is-fully-usable'))
  1115. @qubes.events.handler('domain-is-fully-usable')
  1116. def on_domain_is_fully_usable(self, event):
  1117. '''Check whether domain is running and sane.
  1118. Currently this checks for running qrexec.
  1119. ''' # pylint: disable=unused-argument
  1120. # Running gui-daemon implies also VM running
  1121. if not self.is_qrexec_running():
  1122. yield False
  1123. # memory and disk
  1124. def get_mem(self):
  1125. '''Get current memory usage from VM.
  1126. :returns: Memory usage [FIXME unit].
  1127. :rtype: FIXME
  1128. '''
  1129. if self.libvirt_domain is None:
  1130. return 0
  1131. try:
  1132. if not self.libvirt_domain.isActive():
  1133. return 0
  1134. return self.libvirt_domain.info()[1]
  1135. except libvirt.libvirtError as e:
  1136. if e.get_error_code() in (
  1137. # qube no longer exists
  1138. libvirt.VIR_ERR_NO_DOMAIN,
  1139. # libxl_domain_info failed (race condition from isActive)
  1140. libvirt.VIR_ERR_INTERNAL_ERROR,
  1141. ):
  1142. return 0
  1143. else:
  1144. self.log.exception(
  1145. 'libvirt error code: {!r}'.format(e.get_error_code()))
  1146. raise
  1147. def get_mem_static_max(self):
  1148. '''Get maximum memory available to VM.
  1149. :returns: Memory limit [FIXME unit].
  1150. :rtype: FIXME
  1151. '''
  1152. if self.libvirt_domain is None:
  1153. return 0
  1154. try:
  1155. return self.libvirt_domain.maxMemory()
  1156. except libvirt.libvirtError as e:
  1157. if e.get_error_code() in (
  1158. # qube no longer exists
  1159. libvirt.VIR_ERR_NO_DOMAIN,
  1160. # libxl_domain_info failed (race condition from isActive)
  1161. libvirt.VIR_ERR_INTERNAL_ERROR,
  1162. ):
  1163. return 0
  1164. else:
  1165. self.log.exception(
  1166. 'libvirt error code: {!r}'.format(e.get_error_code()))
  1167. raise
  1168. def get_cputime(self):
  1169. '''Get total CPU time burned by this domain since start.
  1170. :returns: CPU time usage [FIXME unit].
  1171. :rtype: FIXME
  1172. '''
  1173. if self.libvirt_domain is None:
  1174. return 0
  1175. if self.libvirt_domain is None:
  1176. return 0
  1177. if not self.libvirt_domain.isActive():
  1178. return 0
  1179. try:
  1180. if not self.libvirt_domain.isActive():
  1181. return 0
  1182. # this does not work, because libvirt
  1183. # return self.libvirt_domain.getCPUStats(
  1184. # libvirt.VIR_NODE_CPU_STATS_ALL_CPUS, 0)[0]['cpu_time']/10**9
  1185. return self.libvirt_domain.info()[4]
  1186. except libvirt.libvirtError as e:
  1187. if e.get_error_code() in (
  1188. # qube no longer exists
  1189. libvirt.VIR_ERR_NO_DOMAIN,
  1190. # libxl_domain_info failed (race condition from isActive)
  1191. libvirt.VIR_ERR_INTERNAL_ERROR,
  1192. ):
  1193. return 0
  1194. else:
  1195. self.log.exception(
  1196. 'libvirt error code: {!r}'.format(e.get_error_code()))
  1197. raise
  1198. # XXX shouldn't this go only to vms that have root image?
  1199. def get_disk_utilization_root_img(self):
  1200. '''Get space that is actually ocuppied by :py:attr:`volumes['root']`.
  1201. This is a temporary wrapper for backwards compatibility. You should
  1202. call directly :py:attr:`volumes[name].utilization`
  1203. :returns: domain's real disk image size [FIXME unit]
  1204. :rtype: FIXME
  1205. .. seealso:: :py:meth:`get_root_img_sz`
  1206. '''
  1207. warnings.warn("get_disk_utilization_root_img is deprecated,"
  1208. " use volumes['root'].utilization", DeprecationWarning)
  1209. return qubes.storage.file.get_disk_usage(
  1210. self.volumes['root'].utilization)
  1211. # XXX shouldn't this go only to vms that have root image?
  1212. def get_root_img_sz(self):
  1213. '''Get the size of the :py:attr:`volumes['root']`.
  1214. This is a temporary wrapper for backwards compatibility. You should
  1215. call directly :py:attr:`volumes[name].size`
  1216. :returns: domain's virtual disk size [FIXME unit]
  1217. :rtype: FIXME
  1218. .. seealso:: :py:meth:`get_disk_utilization_root_img`
  1219. '''
  1220. warnings.warn(
  1221. "get_disk_root_img_sz is deprecated, use volumes['root'].size",
  1222. DeprecationWarning)
  1223. return qubes.storage.file.get_disk_usage(self.volumes['root'].size)
  1224. def get_disk_utilization_private_img(self):
  1225. '''Get space that is actually ocuppied by :py:attr:`volumes['private']`.
  1226. This is a temporary wrapper for backwards compatibility. You should
  1227. call directly :py:attr:`volumes[name].utilization`
  1228. :returns: domain's real disk image size [FIXME unit]
  1229. :rtype: FIXME
  1230. ''' # pylint: disable=invalid-name
  1231. warnings.warn("get_disk_utilization_private_img is deprecated,"
  1232. " use volumes['private'].utilization", DeprecationWarning)
  1233. return qubes.storage.file.get_disk_usage(self.volumes[
  1234. 'private'].utilization)
  1235. def get_private_img_sz(self):
  1236. '''Get the size of the :py:attr:`volumes['private']`.
  1237. This is a temporary wrapper for backwards compatibility. You should
  1238. call directly :py:attr:`volumes[name].size`
  1239. :returns: domain's virtual disk size [FIXME unit]
  1240. :rtype: FIXME
  1241. .. seealso:: :py:meth:`get_disk_utilization_private_img`
  1242. '''
  1243. warnings.warn("get_disk_private_img_sz is deprecated,"
  1244. " use volumes['private'].size", DeprecationWarning)
  1245. return qubes.storage.file.get_disk_usage(self.volumes['private'].size)
  1246. def get_disk_utilization(self):
  1247. '''Return total space actually occuppied by all files belonging to \
  1248. this domain.
  1249. :returns: domain's total disk usage [FIXME unit]
  1250. :rtype: FIXME
  1251. '''
  1252. return qubes.storage.file.get_disk_usage(self.dir_path)
  1253. # TODO move to storage
  1254. def verify_files(self):
  1255. '''Verify that files accessed by this machine are sane.
  1256. On success, returns normally. On failure, raises exception.
  1257. '''
  1258. self.storage.verify_files()
  1259. self.fire_event('domain-verify-files')
  1260. return True
  1261. # miscellanous
  1262. def get_start_time(self):
  1263. '''Tell when machine was started.
  1264. :rtype: datetime.datetime
  1265. '''
  1266. if not self.is_running():
  1267. return None
  1268. # TODO shouldn't this be qubesdb?
  1269. start_time = self.app.vmm.xs.read('',
  1270. '/vm/{}/start_time'.format(self.uuid))
  1271. if start_time != '':
  1272. return datetime.datetime.fromtimestamp(float(start_time))
  1273. else:
  1274. return None
  1275. # XXX this probably should go to AppVM
  1276. def is_outdated(self):
  1277. '''Check whether domain needs restart to update root image from \
  1278. template.
  1279. :returns: :py:obj:`True` if is outdated, :py:obj:`False` otherwise.
  1280. :rtype: bool
  1281. '''
  1282. # pylint: disable=no-member
  1283. # Makes sense only on VM based on template
  1284. if self.template is None:
  1285. return False
  1286. if not self.is_running():
  1287. return False
  1288. if not hasattr(self.template, 'rootcow_img'):
  1289. return False
  1290. rootimg_inode = os.stat(self.template.root_img)
  1291. try:
  1292. rootcow_inode = os.stat(self.template.rootcow_img)
  1293. except OSError:
  1294. # The only case when rootcow_img doesn't exists is in the middle of
  1295. # commit_changes, so VM is outdated right now
  1296. return True
  1297. current_dmdev = "/dev/mapper/snapshot-{0:x}:{1}-{2:x}:{3}".format(
  1298. rootimg_inode[2], rootimg_inode[1],
  1299. rootcow_inode[2], rootcow_inode[1])
  1300. # FIXME
  1301. # 51712 (0xCA00) is xvda
  1302. # backend node name not available through xenapi :(
  1303. used_dmdev = self.app.vmm.xs.read('',
  1304. '/local/domain/0/backend/vbd/{}/51712/node'.format(self.xid))
  1305. return used_dmdev != current_dmdev
  1306. #
  1307. # helper methods
  1308. #
  1309. def relative_path(self, path):
  1310. '''Return path relative to py:attr:`dir_path`.
  1311. :param str path: Path in question.
  1312. :returns: Relative path.
  1313. '''
  1314. return os.path.relpath(path, self.dir_path)
  1315. def create_qdb_entries(self):
  1316. '''Create entries in Qubes DB.
  1317. '''
  1318. # pylint: disable=no-member
  1319. self.qdb.write('/name', self.name)
  1320. self.qdb.write('/type', self.__class__.__name__)
  1321. self.qdb.write('/updateable', str(self.updateable))
  1322. self.qdb.write('/persistence', 'full' if self.updateable else 'rw-only')
  1323. self.qdb.write('/debug', str(int(self.debug)))
  1324. try:
  1325. self.qdb.write('/template', self.template.name)
  1326. except AttributeError:
  1327. self.qdb.write('/template', '')
  1328. self.qdb.write('/random-seed',
  1329. base64.b64encode(qubes.utils.urandom(64)))
  1330. if self.provides_network:
  1331. self.qdb.write('/network-provider/gateway', self.gateway)
  1332. self.qdb.write('/network-provider/netmask', self.netmask)
  1333. for i, addr in zip(itertools.count(start=1), self.dns):
  1334. self.qdb.write('/network-provider/dns-{}'.format(i), addr)
  1335. if self.netvm is not None:
  1336. self.qdb.write('/network/ip', self.ip)
  1337. self.qdb.write('/network/netmask', self.netvm.netmask)
  1338. self.qdb.write('/network/gateway', self.netvm.gateway)
  1339. for i, addr in zip(itertools.count(start=1), self.dns):
  1340. self.qdb.write('/network/dns-{}'.format(i), addr)
  1341. tzname = qubes.utils.get_timezone()
  1342. if tzname:
  1343. self.qdb.write('/timezone', tzname)
  1344. for feature, value in self.features.items():
  1345. self.qdb.write('/features/{0}'.format(feature),
  1346. str(value) if value else '')
  1347. self.qdb.write('/devices/block', '')
  1348. self.qdb.write('/devices/usb', '')
  1349. # TODO: Currently the whole qmemman is quite Xen-specific, so stay with
  1350. # xenstore for it until decided otherwise
  1351. if qmemman_present:
  1352. self.app.vmm.xs.set_permissions('',
  1353. '/local/domain/{}/memory'.format(self.xid),
  1354. [{'dom': self.xid}])
  1355. self.fire_event('domain-qdb-create')
  1356. def _update_libvirt_domain(self):
  1357. '''Re-initialise :py:attr:`libvirt_domain`.'''
  1358. domain_config = self.create_config_file()
  1359. try:
  1360. self._libvirt_domain = self.app.vmm.libvirt_conn.defineXML(
  1361. domain_config)
  1362. except libvirt.libvirtError as e:
  1363. if e.get_error_code() == libvirt.VIR_ERR_OS_TYPE \
  1364. and e.get_str2() == 'hvm':
  1365. raise qubes.exc.QubesVMError(self,
  1366. 'HVM qubes are not supported on this machine. '
  1367. 'Check BIOS settings for VT-x/AMD-V extensions.')
  1368. else:
  1369. raise
  1370. #
  1371. # workshop -- those are to be reworked later
  1372. #
  1373. def get_prefmem(self):
  1374. # TODO: qmemman is still xen specific
  1375. untrusted_meminfo_key = self.app.vmm.xs.read('',
  1376. '/local/domain/{}/memory/meminfo'.format(self.xid))
  1377. if untrusted_meminfo_key is None or untrusted_meminfo_key == '':
  1378. return 0
  1379. domain = qubes.qmemman.DomainState(self.xid)
  1380. qubes.qmemman.algo.refresh_meminfo_for_domain(
  1381. domain, untrusted_meminfo_key)
  1382. domain.memory_maximum = self.get_mem_static_max() * 1024
  1383. return qubes.qmemman.algo.prefmem(domain) / 1024
  1384. #
  1385. # landfill -- those are unneeded
  1386. #
  1387. # attrs = {
  1388. # XXX probably will be obsoleted by .events_enabled
  1389. # "_do_not_reset_firewall": { "func": lambda x: False },
  1390. # "_start_guid_first": { "func": lambda x: False },
  1391. # }
  1392. # this function appears unused
  1393. # def _cleanup_zombie_domains(self):
  1394. # """
  1395. # This function is workaround broken libxl (which leaves not fully
  1396. # created domain on failure) and vchan on domain crash behaviour
  1397. # @return: None
  1398. # """
  1399. # xc = self.get_xc_dominfo()
  1400. # if xc and xc['dying'] == 1:
  1401. # # GUID still running?
  1402. # guid_pidfile = '/var/run/qubes/guid-running.%d' % xc['domid']
  1403. # if os.path.exists(guid_pidfile):
  1404. # guid_pid = open(guid_pidfile).read().strip()
  1405. # os.kill(int(guid_pid), 15)
  1406. # # qrexec still running?
  1407. # if self.is_qrexec_running():
  1408. # #TODO: kill qrexec daemon
  1409. # pass