qubesvm.py 64 KB

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