qubesvm.py 58 KB

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