qubesvm.py 59 KB

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