qubesvm.py 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308
  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 library is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU Lesser General Public
  11. # License as published by the Free Software Foundation; either
  12. # version 2.1 of the License, or (at your option) any later version.
  13. #
  14. # This library 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 GNU
  17. # Lesser General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Lesser General Public
  20. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  21. #
  22. import asyncio
  23. import base64
  24. import grp
  25. import re
  26. import os
  27. import os.path
  28. import shutil
  29. import string
  30. import subprocess
  31. import uuid
  32. import libvirt # pylint: disable=import-error
  33. import lxml
  34. import qubes
  35. import qubes.config
  36. import qubes.exc
  37. import qubes.storage
  38. import qubes.storage.file
  39. import qubes.utils
  40. import qubes.vm
  41. import qubes.vm.mix.net
  42. qmemman_present = False
  43. try:
  44. import qubes.qmemman.client # pylint: disable=wrong-import-position
  45. qmemman_present = True
  46. except ImportError:
  47. pass
  48. # overhead of per-qube/per-vcpu Xen structures,
  49. # taken from OpenStack nova/virt/xenapi/driver.py
  50. # see https://wiki.openstack.org/wiki/XenServer/Overhead
  51. # add an extra MB because Nova rounds up to MBs
  52. MEM_OVERHEAD_BASE = (3 + 1) * 1024 * 1024
  53. MEM_OVERHEAD_PER_VCPU = 3 * 1024 * 1024 / 2
  54. def _setter_kernel(self, prop, value):
  55. """ Helper for setting the domain kernel and running sanity checks on it.
  56. """ # pylint: disable=unused-argument
  57. if not value:
  58. return ''
  59. value = str(value)
  60. if '/' in value:
  61. raise qubes.exc.QubesPropertyValueError(
  62. self, prop, value,
  63. 'Kernel name cannot contain \'/\'')
  64. return value
  65. def _setter_positive_int(self, prop, value):
  66. """ Helper for setting a positive int. Checks that the int is > 0 """
  67. # pylint: disable=unused-argument
  68. value = int(value)
  69. if value <= 0:
  70. raise ValueError('Value must be positive')
  71. return value
  72. def _setter_non_negative_int(self, prop, value):
  73. """ Helper for setting a positive int. Checks that the int is >= 0 """
  74. # pylint: disable=unused-argument
  75. value = int(value)
  76. if value < 0:
  77. raise ValueError('Value must be positive or zero')
  78. return value
  79. def _setter_default_user(self, prop, value):
  80. """ Helper for setting default user """
  81. value = str(value)
  82. # specifically forbid: ':', ' ', """, '"'
  83. allowed_chars = string.ascii_letters + string.digits + '_-+,.'
  84. if not all(c in allowed_chars for c in value):
  85. raise qubes.exc.QubesPropertyValueError(
  86. self, prop, value,
  87. 'Username can contain only those characters: ' + allowed_chars)
  88. return value
  89. def _setter_virt_mode(self, prop, value):
  90. value = str(value)
  91. value = value.lower()
  92. if value not in ('hvm', 'pv', 'pvh'):
  93. raise qubes.exc.QubesPropertyValueError(
  94. self, prop, value,
  95. 'Invalid virtualization mode, supported values: hvm, pv, pvh')
  96. if value == 'pvh' and list(self.devices['pci'].persistent()):
  97. raise qubes.exc.QubesPropertyValueError(
  98. self, prop, value,
  99. "pvh mode can't be set if pci devices are attached")
  100. return value
  101. def _setter_kbd_layout(self, prop, value):
  102. untrusted_xkb_layout = value.split('+')
  103. if len(untrusted_xkb_layout) != 3:
  104. raise qubes.exc.QubesPropertyValueError(
  105. self, prop, value, "invalid number of keyboard layout parameters")
  106. untrusted_layout = untrusted_xkb_layout[0]
  107. untrusted_variant = untrusted_xkb_layout[1]
  108. untrusted_options = untrusted_xkb_layout[2]
  109. re_variant = r'^[a-zA-Z0-9-_]*$'
  110. re_options = r'^[a-zA-Z0-9-_:,]*$'
  111. if not untrusted_layout.isalpha():
  112. raise qubes.exc.QubesPropertyValueError(
  113. self, prop, value, "Invalid keyboard layout provided")
  114. if not re.match(re_variant, untrusted_variant):
  115. raise qubes.exc.QubesPropertyValueError(
  116. self, prop, value, "Invalid layout variant provided")
  117. if not re.match(re_options, untrusted_options):
  118. raise qubes.exc.QubesPropertyValueError(
  119. self, prop, value, "Invalid layout options provided")
  120. return value
  121. def _default_virt_mode(self):
  122. if self.devices['pci'].persistent():
  123. return 'hvm'
  124. try:
  125. return self.template.virt_mode
  126. except AttributeError:
  127. return 'pvh'
  128. def _default_with_template(prop, default):
  129. """Return a callable for 'default' argument of a property. Use a value
  130. from a template (if any), otherwise *default*
  131. """
  132. def _func(self):
  133. try:
  134. return getattr(self.template, prop)
  135. except AttributeError:
  136. if callable(default):
  137. return default(self)
  138. return default
  139. return _func
  140. def _default_maxmem(self):
  141. # first check for any reason to _not_ enable qmemman
  142. if not self.is_memory_balancing_possible():
  143. return 0
  144. # Linux specific cap: max memory can't scale beyond 10.79*init_mem
  145. # see https://groups.google.com/forum/#!topic/qubes-devel/VRqkFj1IOtA
  146. if self.features.get('os', None) == 'Linux':
  147. default_maxmem = self.memory * 10
  148. else:
  149. default_maxmem = 4000
  150. # don't use default larger than half of physical ram
  151. default_maxmem = min(default_maxmem,
  152. int(self.app.host.memory_total / 1024 / 2))
  153. return _default_with_template('maxmem', default_maxmem)(self)
  154. def _default_kernelopts(self):
  155. """
  156. Return default kernel options for the given kernel. If kernel directory
  157. contains 'default-kernelopts-{pci,nopci}.txt' file, use that. Otherwise
  158. use built-in defaults.
  159. For qubes without PCI devices, kernelopts of qube's template are
  160. considered (for template-based qubes).
  161. """
  162. if not self.kernel:
  163. return ''
  164. if 'kernel' in self.volumes:
  165. kernels_dir = self.storage.kernels_dir
  166. else:
  167. kernels_dir = os.path.join(
  168. qubes.config.system_path['qubes_kernels_base_dir'],
  169. self.kernel)
  170. pci = bool(list(self.devices['pci'].persistent()))
  171. if pci:
  172. path = os.path.join(kernels_dir, 'default-kernelopts-pci.txt')
  173. else:
  174. try:
  175. return self.template.kernelopts
  176. except AttributeError:
  177. pass
  178. path = os.path.join(kernels_dir, 'default-kernelopts-nopci.txt')
  179. if os.path.exists(path):
  180. with open(path) as f_kernelopts:
  181. return f_kernelopts.read().strip()
  182. else:
  183. return (qubes.config.defaults['kernelopts_pcidevs'] if pci else
  184. qubes.config.defaults['kernelopts'])
  185. class QubesVM(qubes.vm.mix.net.NetVMMixin, qubes.vm.BaseVM):
  186. """Base functionality of Qubes VM shared between all VMs.
  187. The following events are raised on this class or its subclasses:
  188. .. event:: domain-init (subject, event)
  189. Fired at the end of class' constructor.
  190. :param subject: Event emitter (the qube object)
  191. :param event: Event name (``'domain-init'``)
  192. .. event:: domain-load (subject, event)
  193. Fired after the qube was loaded from :file:`qubes.xml`
  194. :param subject: Event emitter (the qube object)
  195. :param event: Event name (``'domain-loaded'``)
  196. .. event:: domain-pre-start \
  197. (subject, event, start_guid, mem_required)
  198. Fired at the beginning of :py:meth:`start` method.
  199. Handler for this event can be asynchronous (a coroutine).
  200. :param subject: Event emitter (the qube object)
  201. :param event: Event name (``'domain-pre-start'``)
  202. *other arguments are as in :py:meth:`start`*
  203. .. event:: domain-spawn (subject, event, start_guid)
  204. Fired after creating libvirt domain.
  205. :param subject: Event emitter (the qube object)
  206. :param event: Event name (``'domain-spawn'``)
  207. Handler for this event can be asynchronous (a coroutine).
  208. *other arguments are as in :py:meth:`start`*
  209. .. event:: domain-start (subject, event, start_guid)
  210. Fired at the end of :py:meth:`start` method.
  211. Handler for this event can be asynchronous (a coroutine).
  212. :param subject: Event emitter (the qube object)
  213. :param event: Event name (``'domain-start'``)
  214. *other arguments are as in :py:meth:`start`*
  215. .. event:: domain-start-failed (subject, event, reason)
  216. Fired when :py:meth:`start` method fails.
  217. *reason* argument is a textual error message.
  218. Handler for this event can be asynchronous (a coroutine).
  219. :param subject: Event emitter (the qube object)
  220. :param event: Event name (``'domain-start-failed'``)
  221. .. event:: domain-paused (subject, event)
  222. Fired when the domain has been paused.
  223. :param subject: Event emitter (the qube object)
  224. :param event: Event name (``'domain-paused'``)
  225. .. event:: domain-unpaused (subject, event)
  226. Fired when the domain has been unpaused.
  227. :param subject: Event emitter (the qube object)
  228. :param event: Event name (``'domain-unpaused'``)
  229. .. event:: domain-stopped (subject, event)
  230. Fired when domain has been stopped.
  231. This event is emitted before ``'domain-shutdown'`` and will trigger
  232. the cleanup in QubesVM. So if you require that the cleanup has
  233. already run use ``'domain-shutdown'``.
  234. Note that you can receive this event as soon as you received
  235. ``'domain-pre-start'``. This also can be emitted in case of a
  236. startup failure, before or after ``'domain-start-failed'``.
  237. Handler for this event can be asynchronous (a coroutine).
  238. :param subject: Event emitter (the qube object)
  239. :param event: Event name (``'domain-stopped'``)
  240. .. event:: domain-shutdown (subject, event)
  241. Fired when domain has been shut down. It is generated after
  242. ``'domain-stopped'``.
  243. Note that you can receive this event as soon as you received
  244. ``'domain-pre-start'``. This also can be emitted in case of a
  245. startup failure, before or after ``'domain-start-failed'``.
  246. Handler for this event can be asynchronous (a coroutine).
  247. :param subject: Event emitter (the qube object)
  248. :param event: Event name (``'domain-shutdown'``)
  249. .. event:: domain-pre-shutdown (subject, event, force)
  250. Fired at the beginning of :py:meth:`shutdown` method.
  251. Handler for this event can be asynchronous (a coroutine).
  252. :param subject: Event emitter (the qube object)
  253. :param event: Event name (``'domain-pre-shutdown'``)
  254. :param force: If the shutdown is to be forceful
  255. .. event:: domain-shutdown-failed (subject, event, reason)
  256. Fired when ``domain-pre-shutdown`` event was sent, but the actual
  257. shutdown operation failed. It can be caused by other
  258. ``domain-pre-shutdown`` handler blocking the operation with an
  259. exception, or a shutdown timeout.
  260. Handler for this event can be asynchronous (a coroutine).
  261. :param subject: Event emitter (the qube object)
  262. :param event: Event name (``'domain-shutdown-failed'``)
  263. :param reason: Error message
  264. .. event:: domain-cmd-pre-run (subject, event, start_guid)
  265. Fired at the beginning of :py:meth:`run_service` method.
  266. Handler for this event can be asynchronous (a coroutine).
  267. :param subject: Event emitter (the qube object)
  268. :param event: Event name (``'domain-cmd-pre-run'``)
  269. :param start_guid: If the gui daemon can be started
  270. .. event:: domain-create-on-disk (subject, event)
  271. Fired at the end of :py:meth:`create_on_disk` method.
  272. Handler for this event can be asynchronous (a coroutine).
  273. :param subject: Event emitter (the qube object)
  274. :param event: Event name (``'domain-create-on-disk'``)
  275. .. event:: domain-remove-from-disk (subject, event)
  276. Fired at the beginning of :py:meth:`remove_from_disk` method, before
  277. the qube directory is removed.
  278. Handler for this event can be asynchronous (a coroutine).
  279. :param subject: Event emitter (the qube object)
  280. :param event: Event name (``'domain-remove-from-disk'``)
  281. .. event:: domain-clone-files (subject, event, src)
  282. Fired at the end of :py:meth:`clone_disk_files` method.
  283. Handler for this event can be asynchronous (a coroutine).
  284. :param subject: Event emitter (the qube object)
  285. :param event: Event name (``'domain-clone-files'``)
  286. :param src: source qube
  287. .. event:: domain-verify-files (subject, event)
  288. Fired at the end of :py:meth:`clone_disk_files` method.
  289. :param subject: Event emitter (the qube object)
  290. :param event: Event name (``'domain-verify-files'``)
  291. If you think some files are missing or damaged, raise an exception.
  292. .. event:: domain-is-fully-usable (subject, event)
  293. Fired at the end of :py:meth:`clone_disk_files` method.
  294. :param subject: Event emitter (the qube object)
  295. :param event: Event name (``'domain-is-fully-usable'``)
  296. You may ``yield False`` from the handler if you think the qube is
  297. not fully usable. This will cause the domain to be in "transient"
  298. state in the domain lifecycle.
  299. .. event:: domain-qdb-create (subject, event)
  300. Fired at the end of :py:meth:`create_qdb_entries` method.
  301. :param subject: Event emitter (the qube object)
  302. :param event: Event name (``'domain-qdb-create'``)
  303. This event is a good place to add your custom entries to the qdb.
  304. .. event:: domain-qdb-change:watched-path (subject, event, path)
  305. Fired when watched QubesDB entry is changed. See
  306. :py:meth:`watch_qdb_path`. *watched-path* part of event name is
  307. what path was registered for watching, *path* in event argument
  308. is what actually have changed (which may be different if watching a
  309. directory, i.e. a path with `/` at the end).
  310. :param subject: Event emitter (the qube object)
  311. :param event: Event name (``'domain-qdb-change'``)
  312. :param path: changed QubesDB path
  313. .. event:: backup-get-files (subject, event)
  314. Collects additional file to be included in a backup.
  315. :param subject: Event emitter (the qube object)
  316. :param event: Event name (``'backup-get-files'``)
  317. Handlers should yield paths of the files.
  318. .. event:: domain-restore (subject, event)
  319. Domain was just restored from backup, although the storage was not
  320. yet verified and the app object was not yet saved.
  321. :param subject: Event emitter (the qube object)
  322. :param event: Event name (``'domain-restore'``)
  323. .. event:: domain-feature-pre-set:feature (subject, event, feature,
  324. value [, oldvalue])
  325. A feature will be changed. This event is fired before value is set.
  326. If any handler raises an exception, value will not be set.
  327. *oldvalue* is present only when there was any.
  328. :param subject: Event emitter (the qube object)
  329. :param event: Event name (``'domain-feature-pre-set:' feature``)
  330. :param feature: feature name
  331. :param value: new value
  332. :param oldvalue: old value, if any
  333. .. event:: domain-feature-set:feature (subject, event, feature, value
  334. [, oldvalue])
  335. A feature was changed. This event is fired before bare
  336. `domain-feature-set` event.
  337. *oldvalue* is present only when there was any.
  338. :param subject: Event emitter (the qube object)
  339. :param event: Event name (``'domain-feature-set:' feature``)
  340. :param feature: feature name
  341. :param value: new value
  342. :param oldvalue: old value, if any
  343. .. event:: domain-feature-delete:feature (subject, event, feature)
  344. A feature was removed. This event is fired before bare
  345. `domain-feature-delete` event.
  346. :param subject: Event emitter (the qube object)
  347. :param event: Event name (``'domain-feature-delete:' feature``)
  348. :param feature: feature name
  349. .. event:: domain-feature-pre-delete:feature (subject, event, feature)
  350. A feature will be removed. This event is fired before feature is
  351. removed. If any handler raises an exception,feature will not be
  352. removed.
  353. :param subject: Event emitter (the qube object)
  354. :param event: Event name (``'domain-feature-pre-delete:' feature``)
  355. :param feature: feature name
  356. .. event:: domain-tag-add:tag (subject, event, tag)
  357. A tag was added.
  358. :param subject: Event emitter (the qube object)
  359. :param event: Event name (``'domain-tag-add:' tag``)
  360. :param tag: tag name
  361. .. event:: domain-tag-delete:tag (subject, event, tag)
  362. A feature was removed.
  363. :param subject: Event emitter (the qube object)
  364. :param event: Event name (``'domain-tag-delete:' tag``)
  365. :param tag: tag name
  366. .. event:: features-request (subject, event, *, untrusted_features)
  367. The domain is performing a features request.
  368. :param subject: Event emitter (the qube object)
  369. :param event: Event name (``'features-request'``)
  370. :param untrusted_features: :py:class:`dict` containing the feature \
  371. request
  372. The content of the `untrusted_features` variable is, as the name
  373. implies, **UNTRUSTED**. The remind this to programmer, the variable
  374. name has to be exactly as provided.
  375. It is up to the extensions to decide, what to do with request,
  376. ranging from plainly ignoring the request to verbatim copy into
  377. :py:attr:`features` with only minimal sanitisation.
  378. Handler for this event can be asynchronous (a coroutine).
  379. .. event:: firewall-changed (subject, event)
  380. Firewall was changed.
  381. :param subject: Event emitter (the qube object)
  382. :param event: Event name (``'firewall-changed'``)
  383. .. event:: net-domain-connect (subject, event, vm)
  384. Fired after connecting a domiain to this vm.
  385. :param subject: Event emitter (the qube object)
  386. :param event: Event name (``'net-domain-connect'``)
  387. :param vm: The domain that was just connected.
  388. On the `vm` object there was probably ``property-set:netvm`` fired
  389. earlier.
  390. .. event:: template-postinstall (subject, event)
  391. Fired on non-template-based domain (TemplateVM, StandaloneVM) when
  392. it first reports qrexec presence. This happens at the first
  393. domain startup just after its installation and is suitable for
  394. performing various post-installation setup.
  395. Handler for this event can be asynchronous (a coroutine).
  396. """
  397. #
  398. # per-class properties
  399. #
  400. #: directory in which domains of this class will reside
  401. dir_path_prefix = qubes.config.system_path['qubes_appvms_dir']
  402. #
  403. # properties loaded from XML
  404. #
  405. guivm = qubes.VMProperty('guivm', load_stage=4, allow_none=True,
  406. default=(lambda self: self.app.default_guivm),
  407. doc='VM used for Gui')
  408. audiovm = qubes.VMProperty('audiovm', load_stage=4, allow_none=True,
  409. default=(lambda self: self.app.default_audiovm),
  410. doc='VM used for Audio')
  411. virt_mode = qubes.property(
  412. 'virt_mode',
  413. type=str, setter=_setter_virt_mode,
  414. default=_default_virt_mode,
  415. doc="""Virtualisation mode: full virtualisation ("HVM"),
  416. or paravirtualisation ("PV"), or hybrid ("PVH").
  417. TemplateBasedVMs use its template\'s value by default.""")
  418. installed_by_rpm = qubes.property(
  419. 'installed_by_rpm',
  420. type=bool, setter=qubes.property.bool,
  421. default=False,
  422. doc="""If this domain's image was installed from package tracked by
  423. package manager.""")
  424. memory = qubes.property(
  425. 'memory', type=int,
  426. setter=_setter_positive_int,
  427. default=_default_with_template(
  428. 'memory',
  429. lambda self:
  430. qubes.config.defaults[
  431. 'hvm_memory' if self.virt_mode == 'hvm' else 'memory']),
  432. doc='Memory currently available for this VM. TemplateBasedVMs use its '
  433. 'template\'s value by default.')
  434. maxmem = qubes.property(
  435. 'maxmem', type=int,
  436. setter=_setter_non_negative_int,
  437. default=_default_maxmem,
  438. doc="""Maximum amount of memory available for this VM (for the purpose
  439. of the memory balancer). Set to 0 to disable memory balancing for
  440. this qube. TemplateBasedVMs use its template\'s value by default
  441. (unless memory balancing not supported for this qube).""")
  442. stubdom_mem = qubes.property(
  443. 'stubdom_mem', type=int,
  444. setter=_setter_positive_int,
  445. default=None,
  446. doc='Memory amount allocated for the stubdom')
  447. vcpus = qubes.property(
  448. 'vcpus',
  449. type=int,
  450. setter=_setter_positive_int,
  451. default=_default_with_template('vcpus', 2),
  452. doc='Number of virtual CPUs for a qube. TemplateBasedVMs use its '
  453. 'template\'s value by default.')
  454. # CORE2: swallowed uses_default_kernel
  455. kernel = qubes.property(
  456. 'kernel', type=str,
  457. setter=_setter_kernel,
  458. default=_default_with_template('kernel',
  459. lambda self: self.app.default_kernel),
  460. doc='Kernel used by this domain. TemplateBasedVMs use its '
  461. 'template\'s value by default.')
  462. # CORE2: swallowed uses_default_kernelopts
  463. # pylint: disable=no-member
  464. kernelopts = qubes.property(
  465. 'kernelopts', type=str, load_stage=4,
  466. default=_default_kernelopts,
  467. doc='Kernel command line passed to domain. TemplateBasedVMs use its '
  468. 'template\'s value by default.')
  469. debug = qubes.property(
  470. 'debug', type=bool, default=False,
  471. setter=qubes.property.bool,
  472. doc='Turns on debugging features.')
  473. # XXX what this exactly does?
  474. # XXX shouldn't this go to standalone VM and TemplateVM, and leave here
  475. # only plain property?
  476. default_user = qubes.property(
  477. 'default_user', type=str,
  478. # pylint: disable=no-member
  479. default=_default_with_template('default_user',
  480. 'user'),
  481. setter=_setter_default_user,
  482. doc='Default user to start applications as. TemplateBasedVMs use its '
  483. 'template\'s value by default.')
  484. qrexec_timeout = qubes.property(
  485. 'qrexec_timeout', type=int,
  486. default=_default_with_template(
  487. 'qrexec_timeout',
  488. lambda self: self.app.default_qrexec_timeout),
  489. setter=_setter_positive_int,
  490. doc="""Time in seconds after which qrexec connection attempt is deemed
  491. failed. Operating system inside VM should be able to boot in this
  492. time.""")
  493. shutdown_timeout = qubes.property(
  494. 'shutdown_timeout', type=int,
  495. default=_default_with_template(
  496. 'shutdown_timeout',
  497. lambda self: self.app.default_shutdown_timeout),
  498. setter=_setter_positive_int,
  499. doc="""Time in seconds for shutdown of the VM, after which VM may be
  500. forcefully powered off. Operating system inside VM should be
  501. able to fully shutdown in this time.""")
  502. autostart = qubes.property(
  503. 'autostart', default=False,
  504. type=bool, setter=qubes.property.bool,
  505. doc="""Setting this to `True` means that VM should be autostarted on
  506. dom0 boot.""")
  507. include_in_backups = qubes.property(
  508. 'include_in_backups',
  509. default=True,
  510. type=bool, setter=qubes.property.bool,
  511. doc='If this domain is to be included in default backup.')
  512. backup_timestamp = qubes.property(
  513. 'backup_timestamp', default=None,
  514. type=int,
  515. doc='Time of last backup of the qube, in seconds since unix epoch')
  516. default_dispvm = qubes.VMProperty(
  517. 'default_dispvm',
  518. load_stage=4,
  519. allow_none=True,
  520. default=(
  521. lambda self: self.app.default_dispvm),
  522. doc='Default VM to be used as Disposable VM for service calls.')
  523. management_dispvm = qubes.VMProperty(
  524. 'management_dispvm',
  525. load_stage=4,
  526. allow_none=True,
  527. default=_default_with_template(
  528. 'management_dispvm',
  529. (lambda self: self.app.management_dispvm)),
  530. doc='Default DVM template for Disposable VM for managing this VM.')
  531. updateable = qubes.property(
  532. 'updateable',
  533. default=(lambda self: not hasattr(self, 'template')),
  534. type=bool,
  535. setter=qubes.property.forbidden,
  536. doc='True if this machine may be updated on its own.')
  537. # for changes in keyboard_layout, see also the same property in AdminVM
  538. keyboard_layout = qubes.property(
  539. 'keyboard_layout',
  540. default=(lambda self: getattr(self.guivm, 'keyboard_layout', 'us++')),
  541. type=str,
  542. setter=_setter_kbd_layout,
  543. doc='Keyboard layout for this VM')
  544. #
  545. # static, class-wide properties
  546. #
  547. #
  548. # properties not loaded from XML, calculated at run-time
  549. #
  550. def __str__(self):
  551. return self.name
  552. # VMM-related
  553. @qubes.stateless_property
  554. def xid(self):
  555. """Xen ID.
  556. Or not Xen, but ID.
  557. """
  558. if self.libvirt_domain is None:
  559. return -1
  560. try:
  561. if self.is_running():
  562. return self.libvirt_domain.ID()
  563. return -1
  564. except libvirt.libvirtError as e:
  565. if e.get_error_code() == libvirt.VIR_ERR_NO_DOMAIN:
  566. return -1
  567. self.log.exception('libvirt error code: {!r}'.format(
  568. e.get_error_code()))
  569. raise
  570. @qubes.stateless_property
  571. def stubdom_xid(self):
  572. if not self.is_running():
  573. return -1
  574. if self.app.vmm.xs is None:
  575. return -1
  576. stubdom_xid_str = self.app.vmm.xs.read(
  577. '', '/local/domain/{}/image/device-model-domid'.format(
  578. self.xid))
  579. if stubdom_xid_str is None or not stubdom_xid_str.isdigit():
  580. return -1
  581. return int(stubdom_xid_str)
  582. @property
  583. def attached_volumes(self):
  584. result = []
  585. xml_desc = self.libvirt_domain.XMLDesc()
  586. xml = lxml.etree.fromstring(xml_desc)
  587. for disk in xml.xpath("//domain/devices/disk"):
  588. if disk.find('backenddomain') is not None:
  589. pool_name = 'p_%s' % disk.find('backenddomain').get('name')
  590. pool = self.app.pools[pool_name]
  591. vid = disk.find('source').get('dev').split('/dev/')[1]
  592. for volume in pool.volumes:
  593. if volume.vid == vid:
  594. result += [volume]
  595. break
  596. return result + list(self.volumes.values())
  597. @property
  598. def libvirt_domain(self):
  599. """Libvirt domain object from libvirt.
  600. May be :py:obj:`None`, if libvirt knows nothing about this domain.
  601. """
  602. if self._libvirt_domain is not None:
  603. return self._libvirt_domain
  604. if self.app.vmm.offline_mode:
  605. return None
  606. # XXX _update_libvirt_domain?
  607. try:
  608. self._libvirt_domain = self.app.vmm.libvirt_conn.lookupByUUID(
  609. self.uuid.bytes)
  610. except libvirt.libvirtError as e:
  611. if e.get_error_code() == libvirt.VIR_ERR_NO_DOMAIN:
  612. self._update_libvirt_domain()
  613. else:
  614. raise
  615. return self._libvirt_domain
  616. @property
  617. def block_devices(self):
  618. """ Return all :py:class:`qubes.storage.BlockDevice` for current domain
  619. for serialization in the libvirt XML template as <disk>.
  620. """
  621. for v in self.volumes.values():
  622. block_dev = v.block_device()
  623. if block_dev is not None:
  624. yield block_dev
  625. @property
  626. def untrusted_qdb(self):
  627. """QubesDB handle for this domain."""
  628. if self._qdb_connection is None:
  629. if self.is_running():
  630. import qubesdb # pylint: disable=import-error
  631. self._qdb_connection = qubesdb.QubesDB(self.name)
  632. return self._qdb_connection
  633. @property
  634. def dir_path(self):
  635. """Root directory for files related to this domain"""
  636. return os.path.join(
  637. qubes.config.qubes_base_dir,
  638. self.dir_path_prefix,
  639. self.name)
  640. @property
  641. def conf_file(self):
  642. return os.path.join(self.dir_path, 'libvirt.xml')
  643. # network-related
  644. #
  645. # constructor
  646. #
  647. def __init__(self, app, xml, volume_config=None, **kwargs):
  648. # migrate renamed properties
  649. if xml is not None:
  650. node_hvm = xml.find('./properties/property[@name=\'hvm\']')
  651. if node_hvm is not None:
  652. if qubes.property.bool(None, None, node_hvm.text):
  653. kwargs['virt_mode'] = 'hvm'
  654. else:
  655. kwargs['virt_mode'] = 'pv'
  656. node_hvm.getparent().remove(node_hvm)
  657. super(QubesVM, self).__init__(app, xml, **kwargs)
  658. if volume_config is None:
  659. volume_config = {}
  660. if hasattr(self, 'volume_config'):
  661. if xml is not None:
  662. for node in xml.xpath('volume-config/volume'):
  663. name = node.get('name')
  664. assert name
  665. for key, value in node.items():
  666. # pylint: disable=no-member
  667. if value == 'True':
  668. value = True
  669. try:
  670. self.volume_config[name][key] = value
  671. except KeyError:
  672. self.volume_config[name] = {key: value}
  673. for name, conf in volume_config.items():
  674. for key, value in conf.items():
  675. # pylint: disable=no-member
  676. try:
  677. self.volume_config[name][key] = value
  678. except KeyError:
  679. self.volume_config[name] = {key: value}
  680. elif volume_config:
  681. raise TypeError(
  682. 'volume_config specified, but {} did not expect that.'.format(
  683. self.__class__.__name__))
  684. # Init private attrs
  685. self._libvirt_domain = None
  686. self._qdb_connection = None
  687. # We assume a fully halted VM here. The 'domain-init' handler will
  688. # check if the VM is already running.
  689. self._domain_stopped_event_received = True
  690. self._domain_stopped_event_handled = True
  691. self._domain_stopped_future = None
  692. # Internal lock to ensure ordering between _domain_stopped_coro() and
  693. # start(). This should not be accessed anywhere else.
  694. self._domain_stopped_lock = asyncio.Lock()
  695. if xml is None:
  696. # we are creating new VM and attributes came through kwargs
  697. assert hasattr(self, 'qid')
  698. assert hasattr(self, 'name')
  699. if xml is None:
  700. # new qube, disable updates check if requested for new qubes
  701. # SEE: 1637 when features are done, migrate to plugin
  702. if not self.app.check_updates_vm:
  703. self.features['check-updates'] = False
  704. # will be initialized after loading all the properties
  705. #: operations which shouldn't happen simultaneously with qube startup
  706. # (including another startup of the same qube)
  707. self.startup_lock = asyncio.Lock()
  708. # fire hooks
  709. if xml is None:
  710. self.events_enabled = True
  711. self.fire_event('domain-init')
  712. def close(self):
  713. if self._qdb_connection is not None:
  714. self._qdb_connection.close()
  715. self._qdb_connection = None
  716. if self._libvirt_domain is not None:
  717. self._libvirt_domain = None
  718. super().close()
  719. def __hash__(self):
  720. return self.qid
  721. def __lt__(self, other):
  722. return self.name < other.name
  723. def __xml__(self):
  724. # pylint: disable=no-member
  725. element = super(QubesVM, self).__xml__()
  726. # pylint: enable=no-member
  727. if hasattr(self, 'volumes'):
  728. volume_config_node = lxml.etree.Element('volume-config')
  729. for volume in self.volumes.values():
  730. volume_config_node.append(volume.__xml__())
  731. element.append(volume_config_node)
  732. return element
  733. #
  734. # event handlers
  735. #
  736. @qubes.events.handler('domain-init', 'domain-load')
  737. def on_domain_init_loaded(self, event):
  738. # pylint: disable=unused-argument
  739. if not hasattr(self, 'uuid'):
  740. # pylint: disable=attribute-defined-outside-init
  741. self.uuid = uuid.uuid4()
  742. # Initialize VM image storage class;
  743. # it might be already initialized by a recursive call from a child VM
  744. if self.storage is None:
  745. self.storage = qubes.storage.Storage(self)
  746. if not self.app.vmm.offline_mode and self.is_running():
  747. self.start_qdb_watch()
  748. self._domain_stopped_event_received = False
  749. self._domain_stopped_event_handled = False
  750. @qubes.events.handler('property-set:label')
  751. def on_property_set_label(self, event, name, newvalue, oldvalue=None):
  752. # pylint: disable=unused-argument
  753. # icon is calculated based on label
  754. self.fire_event('property-reset:icon', name='icon')
  755. @qubes.events.handler('property-pre-set:kernel')
  756. def on_property_pre_set_kernel(self, event, name, newvalue, oldvalue=None):
  757. # pylint: disable=unused-argument
  758. if not newvalue:
  759. return
  760. dirname = os.path.join(
  761. qubes.config.qubes_base_dir,
  762. qubes.config.system_path['qubes_kernels_base_dir'],
  763. newvalue)
  764. if not os.path.exists(dirname):
  765. raise qubes.exc.QubesPropertyValueError(
  766. self, self.property_get_def(name), newvalue,
  767. 'Kernel {!r} not installed'.format(
  768. newvalue))
  769. for filename in ('vmlinuz', 'initramfs'):
  770. if not os.path.exists(os.path.join(dirname, filename)):
  771. raise qubes.exc.QubesPropertyValueError(
  772. self, self.property_get_def(name), newvalue,
  773. 'Kernel {!r} not properly installed: '
  774. 'missing {!r} file'.format(
  775. newvalue, filename))
  776. @qubes.events.handler('property-pre-set:autostart')
  777. def on_property_pre_set_autostart(self, event, name, newvalue,
  778. oldvalue=None):
  779. # pylint: disable=unused-argument
  780. # workaround https://bugzilla.redhat.com/show_bug.cgi?id=1181922
  781. if newvalue:
  782. retcode = subprocess.call(
  783. ["sudo", "ln", "-sf",
  784. "/usr/lib/systemd/system/qubes-vm@.service",
  785. "/etc/systemd/system/multi-user.target.wants/qubes-vm@"
  786. "{}.service".format(self.name)])
  787. else:
  788. retcode = subprocess.call(
  789. ['sudo', 'systemctl', 'disable',
  790. 'qubes-vm@{}.service'.format(self.name)])
  791. if retcode:
  792. raise qubes.exc.QubesException(
  793. 'Failed to set autostart for VM in systemd')
  794. @qubes.events.handler('property-pre-reset:autostart')
  795. def on_property_pre_reset_autostart(self, event, name, oldvalue=None):
  796. # pylint: disable=unused-argument
  797. if oldvalue:
  798. retcode = subprocess.call(
  799. ['sudo', 'systemctl', 'disable',
  800. 'qubes-vm@{}.service'.format(self.name)])
  801. if retcode:
  802. raise qubes.exc.QubesException(
  803. 'Failed to reset autostart for VM in systemd')
  804. @qubes.events.handler('domain-remove-from-disk')
  805. def on_remove_from_disk(self, event, **kwargs):
  806. # pylint: disable=unused-argument
  807. if self.autostart:
  808. subprocess.call(
  809. ['sudo', 'systemctl', 'disable',
  810. 'qubes-vm@{}.service'.format(self.name)])
  811. @qubes.events.handler('domain-create-on-disk')
  812. def on_create_on_disk(self, event, **kwargs):
  813. # pylint: disable=unused-argument
  814. if self.autostart:
  815. subprocess.call(
  816. ['sudo', 'systemctl', 'enable',
  817. 'qubes-vm@{}.service'.format(self.name)])
  818. #
  819. # methods for changing domain state
  820. #
  821. @asyncio.coroutine
  822. def _ensure_shutdown_handled(self):
  823. """Make sure previous shutdown is fully handled.
  824. MUST NOT be called when domain is running.
  825. """
  826. with (yield from self._domain_stopped_lock):
  827. # Don't accept any new stopped event's till a new VM has been
  828. # created. If we didn't received any stopped event or it wasn't
  829. # handled yet we will handle this in the next lines.
  830. self._domain_stopped_event_received = True
  831. if self._domain_stopped_future is not None:
  832. # Libvirt stopped event was already received, so cancel the
  833. # future. If it didn't generate the Qubes events yet we
  834. # will do it below.
  835. self._domain_stopped_future.cancel()
  836. self._domain_stopped_future = None
  837. if not self._domain_stopped_event_handled:
  838. # No Qubes domain-stopped events have been generated yet.
  839. # So do this now.
  840. # Set this immediately such that we don't generate events
  841. # twice if an exception gets thrown.
  842. self._domain_stopped_event_handled = True
  843. yield from self.fire_event_async('domain-stopped')
  844. yield from self.fire_event_async('domain-shutdown')
  845. @asyncio.coroutine
  846. def start(self, start_guid=True, notify_function=None,
  847. mem_required=None):
  848. """Start domain
  849. :param bool start_guid: FIXME
  850. :param collections.abc.Callable notify_function: FIXME
  851. :param int mem_required: FIXME
  852. """
  853. with (yield from self.startup_lock):
  854. # check if domain wasn't removed in the meantime
  855. if self not in self.app.domains:
  856. raise qubes.exc.QubesVMNotFoundError(self.name)
  857. # Intentionally not used is_running(): eliminate also "Paused",
  858. # "Crashed", "Halting"
  859. if self.get_power_state() != 'Halted':
  860. return self
  861. yield from self._ensure_shutdown_handled()
  862. self.log.info('Starting {}'.format(self.name))
  863. try:
  864. yield from self.fire_event_async('domain-pre-start',
  865. pre_event=True,
  866. start_guid=start_guid,
  867. mem_required=mem_required)
  868. except Exception as exc:
  869. self.log.error('Start failed: %s', str(exc))
  870. yield from self.fire_event_async('domain-start-failed',
  871. reason=str(exc))
  872. raise
  873. qmemman_client = None
  874. try:
  875. for devclass in self.devices:
  876. for dev in self.devices[devclass].persistent():
  877. if isinstance(dev, qubes.devices.UnknownDevice):
  878. raise qubes.exc.QubesException(
  879. '{} device {} not available'.format(
  880. devclass, dev))
  881. if self.virt_mode == 'pvh' and not self.kernel:
  882. raise qubes.exc.QubesException(
  883. 'virt_mode PVH require kernel to be set')
  884. yield from self.storage.verify()
  885. if self.netvm is not None:
  886. # pylint: disable = no-member
  887. if self.netvm.qid != 0:
  888. if not self.netvm.is_running():
  889. yield from self.netvm.start(
  890. start_guid=start_guid,
  891. notify_function=notify_function)
  892. qmemman_client = yield from asyncio.get_event_loop(). \
  893. run_in_executor(None, self.request_memory, mem_required)
  894. yield from self.storage.start()
  895. except Exception as exc:
  896. self.log.error('Start failed: %s', str(exc))
  897. # let anyone receiving domain-pre-start know that startup failed
  898. yield from self.fire_event_async('domain-start-failed',
  899. reason=str(exc))
  900. if qmemman_client:
  901. qmemman_client.close()
  902. raise
  903. try:
  904. self._update_libvirt_domain()
  905. self.libvirt_domain.createWithFlags(
  906. libvirt.VIR_DOMAIN_START_PAUSED)
  907. # the above allocates xid, lets announce that
  908. self.fire_event('property-reset:xid', name='xid')
  909. self.fire_event('property-reset:stubdom_xid',
  910. name='stubdom_xid')
  911. self.fire_event('property-reset:start_time', name='start_time')
  912. except libvirt.libvirtError as exc:
  913. # missing IOMMU?
  914. if self.virt_mode == 'hvm' and \
  915. list(self.devices['pci'].persistent()) and \
  916. not self.app.host.is_iommu_supported():
  917. exc = qubes.exc.QubesException(
  918. 'Failed to start an HVM qube with PCI devices assigned '
  919. '- hardware does not support IOMMU/VT-d/AMD-Vi')
  920. self.log.error('Start failed: %s', str(exc))
  921. yield from self.fire_event_async('domain-start-failed',
  922. reason=str(exc))
  923. yield from self.storage.stop()
  924. raise exc
  925. except Exception as exc:
  926. self.log.error('Start failed: %s', str(exc))
  927. # let anyone receiving domain-pre-start know that startup failed
  928. yield from self.fire_event_async('domain-start-failed',
  929. reason=str(exc))
  930. yield from self.storage.stop()
  931. raise
  932. finally:
  933. if qmemman_client:
  934. qmemman_client.close()
  935. self._domain_stopped_event_received = False
  936. self._domain_stopped_event_handled = False
  937. try:
  938. yield from self.fire_event_async('domain-spawn',
  939. start_guid=start_guid)
  940. self.log.info('Setting Qubes DB info for the VM')
  941. yield from self.start_qubesdb()
  942. self.create_qdb_entries()
  943. self.start_qdb_watch()
  944. self.log.warning('Activating the {} VM'.format(self.name))
  945. self.libvirt_domain.resume()
  946. yield from self.start_qrexec_daemon()
  947. yield from self.fire_event_async('domain-start',
  948. start_guid=start_guid)
  949. except Exception as exc: # pylint: disable=bare-except
  950. self.log.error('Start failed: %s', str(exc))
  951. # This avoids losing the exception if an exception is
  952. # raised in self._kill_locked(), because the vm is not
  953. # running or paused
  954. try:
  955. yield from self._kill_locked()
  956. except qubes.exc.QubesVMNotStartedError:
  957. pass
  958. # let anyone receiving domain-pre-start know that startup failed
  959. yield from self.fire_event_async('domain-start-failed',
  960. reason=str(exc))
  961. raise
  962. return self
  963. def on_libvirt_domain_stopped(self):
  964. """ Handle VIR_DOMAIN_EVENT_STOPPED events from libvirt.
  965. This is not a Qubes event handler. Instead we do some sanity checks
  966. and synchronization with start() and then emits Qubes events.
  967. """
  968. state = self.get_power_state()
  969. if state not in ['Halted', 'Crashed', 'Dying']:
  970. self.log.warning('Stopped event from libvirt received,'
  971. ' but domain is in state {}!'.format(state))
  972. # ignore this unexpected event
  973. return
  974. if self._domain_stopped_event_received:
  975. # ignore this event - already triggered by shutdown(), kill(),
  976. # or subsequent start()
  977. return
  978. self._domain_stopped_event_received = True
  979. self._domain_stopped_future = \
  980. asyncio.ensure_future(self._domain_stopped_coro())
  981. @asyncio.coroutine
  982. def _domain_stopped_coro(self):
  983. with (yield from self._domain_stopped_lock):
  984. assert not self._domain_stopped_event_handled
  985. # Set this immediately such that we don't generate events twice if
  986. # an exception gets thrown.
  987. self._domain_stopped_event_handled = True
  988. while self.get_power_state() == 'Dying':
  989. yield from asyncio.sleep(0.25)
  990. yield from self.fire_event_async('domain-stopped')
  991. yield from self.fire_event_async('domain-shutdown')
  992. @qubes.events.handler('domain-stopped')
  993. @asyncio.coroutine
  994. def on_domain_stopped(self, _event, **_kwargs):
  995. """Cleanup after domain was stopped"""
  996. try:
  997. yield from self.storage.stop()
  998. except qubes.storage.StoragePoolException:
  999. self.log.exception('Failed to stop storage for domain %s',
  1000. self.name)
  1001. self.fire_event('property-reset:xid', name='xid')
  1002. self.fire_event('property-reset:stubdom_xid', name='stubdom_xid')
  1003. self.fire_event('property-reset:start_time', name='start_time')
  1004. @asyncio.coroutine
  1005. def shutdown(self, force=False, wait=False, timeout=None):
  1006. """Shutdown domain.
  1007. :param force: ignored
  1008. :param wait: wait for shutdown to complete
  1009. :param timeout: shutdown wait timeout (for *wait*=True), defaults to
  1010. :py:attr:`shutdown_timeout`
  1011. :raises qubes.exc.QubesVMNotStartedError: \
  1012. when domain is already shut down.
  1013. """
  1014. if self.is_halted():
  1015. raise qubes.exc.QubesVMNotStartedError(self)
  1016. try:
  1017. yield from self.fire_event_async('domain-pre-shutdown',
  1018. pre_event=True, force=force)
  1019. self.libvirt_domain.shutdown()
  1020. if wait:
  1021. if timeout is None:
  1022. timeout = self.shutdown_timeout
  1023. while timeout > 0 and not self.is_halted():
  1024. yield from asyncio.sleep(0.25)
  1025. timeout -= 0.25
  1026. with (yield from self.startup_lock):
  1027. if self.is_halted():
  1028. # make sure all shutdown tasks are completed
  1029. yield from self._ensure_shutdown_handled()
  1030. else:
  1031. raise qubes.exc.QubesVMShutdownTimeoutError(self)
  1032. except Exception as ex:
  1033. yield from self.fire_event_async('domain-shutdown-failed',
  1034. reason=str(ex))
  1035. raise
  1036. return self
  1037. @asyncio.coroutine
  1038. def kill(self):
  1039. """Forcefully shutdown (destroy) domain.
  1040. :raises qubes.exc.QubesVMNotStartedError: \
  1041. when domain is already shut down.
  1042. """
  1043. if not self.is_running() and not self.is_paused():
  1044. raise qubes.exc.QubesVMNotStartedError(self)
  1045. with (yield from self.startup_lock):
  1046. yield from self._kill_locked()
  1047. return self
  1048. @asyncio.coroutine
  1049. def _kill_locked(self):
  1050. """Forcefully shutdown (destroy) domain.
  1051. This function needs to be called with self.startup_lock held."""
  1052. try:
  1053. self.libvirt_domain.destroy()
  1054. except libvirt.libvirtError as e:
  1055. if e.get_error_code() == libvirt.VIR_ERR_OPERATION_INVALID:
  1056. raise qubes.exc.QubesVMNotStartedError(self)
  1057. raise
  1058. # make sure all shutdown tasks are completed
  1059. yield from self._ensure_shutdown_handled()
  1060. @asyncio.coroutine
  1061. def suspend(self):
  1062. """Suspend (pause) domain.
  1063. :raises qubes.exc.QubesVMNotRunnignError: \
  1064. when domain is already shut down.
  1065. """
  1066. if not self.is_running() and not self.is_paused():
  1067. raise qubes.exc.QubesVMNotRunningError(self)
  1068. if list(self.devices['pci'].attached()):
  1069. if self.features.check_with_template('qrexec', False):
  1070. yield from self.run_service_for_stdio('qubes.SuspendPre',
  1071. user='root')
  1072. self.libvirt_domain.pMSuspendForDuration(
  1073. libvirt.VIR_NODE_SUSPEND_TARGET_MEM, 0, 0)
  1074. else:
  1075. self.libvirt_domain.suspend()
  1076. return self
  1077. @asyncio.coroutine
  1078. def pause(self):
  1079. """Pause (suspend) domain."""
  1080. if not self.is_running():
  1081. raise qubes.exc.QubesVMNotRunningError(self)
  1082. self.libvirt_domain.suspend()
  1083. return self
  1084. @asyncio.coroutine
  1085. def resume(self):
  1086. """Resume suspended domain.
  1087. :raises qubes.exc.QubesVMNotSuspendedError: when machine is not paused
  1088. :raises qubes.exc.QubesVMError: when machine is suspended
  1089. """
  1090. # pylint: disable=not-an-iterable
  1091. if self.get_power_state() == "Suspended":
  1092. self.libvirt_domain.pMWakeup()
  1093. if self.features.check_with_template('qrexec', False):
  1094. yield from self.run_service_for_stdio('qubes.SuspendPost',
  1095. user='root')
  1096. else:
  1097. yield from self.unpause()
  1098. return self
  1099. @asyncio.coroutine
  1100. def unpause(self):
  1101. """Resume (unpause) a domain"""
  1102. if not self.is_paused():
  1103. raise qubes.exc.QubesVMNotPausedError(self)
  1104. self.libvirt_domain.resume()
  1105. return self
  1106. @asyncio.coroutine
  1107. def run_service(self, service, source=None, user=None,
  1108. filter_esc=False, autostart=False, gui=False, **kwargs):
  1109. """Run service on this VM
  1110. :param str service: service name
  1111. :param qubes.vm.qubesvm.QubesVM source: source domain as presented to
  1112. this VM
  1113. :param str user: username to run service as
  1114. :param bool filter_esc: filter escape sequences to protect terminal \
  1115. emulator
  1116. :param bool autostart: if :py:obj:`True`, machine will be started if \
  1117. it is not running
  1118. :param bool gui: when autostarting, also start gui daemon
  1119. :rtype: asyncio.subprocess.Process
  1120. .. note::
  1121. User ``root`` is redefined to ``SYSTEM`` in the Windows agent code
  1122. """
  1123. # UNSUPPORTED from previous incarnation:
  1124. # localcmd, wait, passio*, notify_function, `-e` switch
  1125. #
  1126. # - passio* and friends depend on params to command (like in stdlib)
  1127. # - the filter_esc is orthogonal to passio*
  1128. # - input: see run_service_for_stdio
  1129. # - wait has no purpose since this is asynchronous
  1130. # - notify_function is gone
  1131. source = 'dom0' if source is None else self.app.domains[source].name
  1132. if user is None:
  1133. user = self.default_user
  1134. if self.is_paused():
  1135. # XXX what about autostart?
  1136. raise qubes.exc.QubesVMNotRunningError(
  1137. self, 'Domain {!r} is paused'.format(self.name))
  1138. if not self.is_running():
  1139. if not autostart:
  1140. raise qubes.exc.QubesVMNotRunningError(self)
  1141. yield from self.start(start_guid=gui)
  1142. if not self.is_qrexec_running():
  1143. raise qubes.exc.QubesVMError(
  1144. self, 'Domain {!r}: qrexec not connected'.format(self.name))
  1145. yield from self.fire_event_async('domain-cmd-pre-run', pre_event=True,
  1146. start_guid=gui)
  1147. return (yield from asyncio.create_subprocess_exec(
  1148. qubes.config.system_path['qrexec_client_path'],
  1149. '-d', str(self.name),
  1150. *(('-t', '-T') if filter_esc else ()),
  1151. '{}:QUBESRPC {} {}'.format(user, service, source),
  1152. **kwargs))
  1153. @asyncio.coroutine
  1154. def run_service_for_stdio(self, *args, input=None, **kwargs):
  1155. """Run a service, pass an optional input and return (stdout, stderr).
  1156. Raises an exception if return code != 0.
  1157. *args* and *kwargs* are passed verbatim to :py:meth:`run_service`.
  1158. .. warning::
  1159. There are some combinations if stdio-related *kwargs*, which are
  1160. not filtered for problems originating between the keyboard and the
  1161. chair.
  1162. """ # pylint: disable=redefined-builtin
  1163. kwargs.setdefault('stdin', subprocess.PIPE)
  1164. kwargs.setdefault('stdout', subprocess.PIPE)
  1165. kwargs.setdefault('stderr', subprocess.PIPE)
  1166. if kwargs['stdin'] == subprocess.PIPE and input is None:
  1167. # workaround for https://bugs.python.org/issue39744
  1168. input = b''
  1169. p = yield from self.run_service(*args, **kwargs)
  1170. # this one is actually a tuple, but there is no need to unpack it
  1171. stdouterr = yield from p.communicate(input=input)
  1172. if p.returncode:
  1173. raise subprocess.CalledProcessError(p.returncode,
  1174. args[0], *stdouterr)
  1175. return stdouterr
  1176. def run(self, command, user=None, **kwargs):
  1177. """Run a shell command inside the domain using qrexec.
  1178. This method is a coroutine.
  1179. """ # pylint: disable=redefined-builtin
  1180. if user is None:
  1181. user = self.default_user
  1182. return asyncio.create_subprocess_exec(
  1183. qubes.config.system_path['qrexec_client_path'],
  1184. '-d', str(self.name),
  1185. '{}:{}'.format(user, command),
  1186. **kwargs)
  1187. @asyncio.coroutine
  1188. def run_for_stdio(self, *args, input=None, **kwargs):
  1189. """Run a shell command inside the domain using qrexec.
  1190. This method is a coroutine.
  1191. """ # pylint: disable=redefined-builtin
  1192. kwargs.setdefault('stdin', subprocess.PIPE)
  1193. kwargs.setdefault('stdout', subprocess.PIPE)
  1194. kwargs.setdefault('stderr', subprocess.PIPE)
  1195. if kwargs['stdin'] == subprocess.PIPE and input is None:
  1196. # workaround for https://bugs.python.org/issue39744
  1197. input = b''
  1198. p = yield from self.run(*args, **kwargs)
  1199. stdouterr = yield from p.communicate(input=input)
  1200. if p.returncode:
  1201. raise subprocess.CalledProcessError(p.returncode,
  1202. args[0], *stdouterr)
  1203. return stdouterr
  1204. def is_memory_balancing_possible(self):
  1205. """Check if memory balancing can be enabled.
  1206. Reasons to not enable it:
  1207. - have PCI devices
  1208. - balloon driver not present
  1209. We don't have reliable way to detect the second point, but good
  1210. heuristic is HVM virt_mode (PV and PVH require OS support and it does
  1211. include balloon driver) and lack of qrexec/meminfo-writer service
  1212. support (no qubes tools installed).
  1213. """
  1214. if list(self.devices['pci'].persistent()):
  1215. return False
  1216. if self.virt_mode == 'hvm':
  1217. # if VM announce any supported service
  1218. features_set = set(self.features)
  1219. template = getattr(self, 'template', None)
  1220. while template is not None:
  1221. features_set.update(template.features)
  1222. template = getattr(template, 'template', None)
  1223. supported_services = any(f.startswith('supported-service.')
  1224. for f in features_set)
  1225. if (not self.features.check_with_template('qrexec', False) or
  1226. (supported_services and
  1227. not self.features.check_with_template(
  1228. 'supported-service.meminfo-writer', False))):
  1229. return False
  1230. return True
  1231. def request_memory(self, mem_required=None):
  1232. if not qmemman_present:
  1233. return None
  1234. if mem_required is None:
  1235. if self.virt_mode == 'hvm':
  1236. if self.stubdom_mem:
  1237. stubdom_mem = self.stubdom_mem
  1238. else:
  1239. if self.features.check_with_template('linux-stubdom', True):
  1240. stubdom_mem = 128 # from libxl_create.c
  1241. else:
  1242. stubdom_mem = 28 # from libxl_create.c
  1243. stubdom_mem += 16 # video ram
  1244. else:
  1245. stubdom_mem = 0
  1246. initial_memory = self.memory
  1247. mem_required = int(initial_memory + stubdom_mem) * 1024 * 1024
  1248. qmemman_client = qubes.qmemman.client.QMemmanClient()
  1249. try:
  1250. mem_required_with_overhead = mem_required + MEM_OVERHEAD_BASE \
  1251. + self.vcpus * MEM_OVERHEAD_PER_VCPU
  1252. got_memory = qmemman_client.request_memory(
  1253. mem_required_with_overhead)
  1254. except IOError as e:
  1255. raise IOError('Failed to connect to qmemman: {!s}'.format(e))
  1256. if not got_memory:
  1257. qmemman_client.close()
  1258. raise qubes.exc.QubesMemoryError(self)
  1259. return qmemman_client
  1260. @staticmethod
  1261. @asyncio.coroutine
  1262. def start_daemon(*command, input=None, **kwargs):
  1263. """Start a daemon for the VM
  1264. This function take care to run it as appropriate user.
  1265. :param command: command to run (array for
  1266. :py:meth:`subprocess.check_call`)
  1267. :param kwargs: args for :py:meth:`subprocess.check_call`
  1268. :return: None
  1269. """ # pylint: disable=redefined-builtin
  1270. if os.getuid() == 0:
  1271. # try to always have VM daemons running as normal user, otherwise
  1272. # some files (like clipboard) may be created as root and cause
  1273. # permission problems
  1274. qubes_group = grp.getgrnam('qubes')
  1275. command = ['runuser', '-u', qubes_group.gr_mem[0], '--'] + \
  1276. list(command)
  1277. p = yield from asyncio.create_subprocess_exec(*command, **kwargs)
  1278. stdout, stderr = yield from p.communicate(input=input)
  1279. if p.returncode:
  1280. raise subprocess.CalledProcessError(p.returncode, command,
  1281. output=stdout, stderr=stderr)
  1282. @asyncio.coroutine
  1283. def start_qrexec_daemon(self):
  1284. """Start qrexec daemon.
  1285. :raises OSError: when starting fails.
  1286. """
  1287. self.log.debug('Starting the qrexec daemon')
  1288. qrexec_args = [str(self.xid), self.name, self.default_user]
  1289. if not self.debug:
  1290. qrexec_args.insert(0, "-q")
  1291. qrexec_env = os.environ.copy()
  1292. if not self.features.check_with_template('qrexec', False):
  1293. self.log.debug(
  1294. 'Starting the qrexec daemon in background, because of features')
  1295. qrexec_env['QREXEC_STARTUP_NOWAIT'] = '1'
  1296. else:
  1297. qrexec_env['QREXEC_STARTUP_TIMEOUT'] = str(self.qrexec_timeout)
  1298. try:
  1299. yield from self.start_daemon(
  1300. qubes.config.system_path['qrexec_daemon_path'], *qrexec_args,
  1301. env=qrexec_env, stderr=subprocess.PIPE)
  1302. except subprocess.CalledProcessError as err:
  1303. if err.returncode == 3:
  1304. raise qubes.exc.QubesVMError(
  1305. self,
  1306. 'Cannot connect to qrexec agent for {} seconds, '
  1307. 'see /var/log/xen/console/guest-{}.log for details'.format(
  1308. self.qrexec_timeout, self.name
  1309. ))
  1310. raise qubes.exc.QubesVMError(
  1311. self, 'qrexec-daemon startup failed: ' + err.stderr.decode())
  1312. @asyncio.coroutine
  1313. def start_qubesdb(self):
  1314. """Start QubesDB daemon.
  1315. :raises OSError: when starting fails.
  1316. """
  1317. # drop old connection to QubesDB, if any
  1318. self._qdb_connection = None
  1319. self.log.info('Starting Qubes DB')
  1320. try:
  1321. yield from self.start_daemon(
  1322. qubes.config.system_path['qubesdb_daemon_path'],
  1323. str(self.xid),
  1324. self.name)
  1325. except subprocess.CalledProcessError:
  1326. raise qubes.exc.QubesException('Cannot execute qubesdb-daemon')
  1327. @asyncio.coroutine
  1328. def create_on_disk(self, pool=None, pools=None):
  1329. """Create files needed for VM.
  1330. """
  1331. self.log.info('Creating directory: {0}'.format(self.dir_path))
  1332. os.makedirs(self.dir_path, mode=0o775, exist_ok=True)
  1333. if pool or pools:
  1334. # pylint: disable=attribute-defined-outside-init
  1335. self.volume_config = _patch_volume_config(self.volume_config, pool,
  1336. pools)
  1337. self.storage = qubes.storage.Storage(self)
  1338. try:
  1339. yield from self.storage.create()
  1340. except:
  1341. try:
  1342. yield from self.storage.remove()
  1343. os.rmdir(self.dir_path)
  1344. except: # pylint: disable=bare-except
  1345. self.log.exception('failed to cleanup {} after failed VM '
  1346. 'creation'.format(self.dir_path))
  1347. raise
  1348. # fire hooks
  1349. yield from self.fire_event_async('domain-create-on-disk')
  1350. @asyncio.coroutine
  1351. def remove_from_disk(self):
  1352. """Remove domain remnants from disk."""
  1353. if not self.is_halted():
  1354. raise qubes.exc.QubesVMNotHaltedError(
  1355. "Can't remove VM {!s}, because it's in state {!r}.".format(
  1356. self, self.get_power_state()))
  1357. # make sure shutdown is handled before removing anything, but only if
  1358. # handling is pending; if not, we may be called from within
  1359. # domain-shutdown event (DispVM._auto_cleanup), which would deadlock
  1360. if not self._domain_stopped_event_handled:
  1361. yield from self._ensure_shutdown_handled()
  1362. yield from self.fire_event_async('domain-remove-from-disk')
  1363. try:
  1364. yield from self.storage.remove()
  1365. finally:
  1366. try:
  1367. # TODO: make it async?
  1368. shutil.rmtree(self.dir_path)
  1369. except FileNotFoundError:
  1370. pass
  1371. @asyncio.coroutine
  1372. def clone_disk_files(self, src, pool=None, pools=None, ):
  1373. """Clone files from other vm.
  1374. :param qubes.vm.qubesvm.QubesVM src: source VM
  1375. """
  1376. # If the current vm name is not a part of `self.app.domains.keys()`,
  1377. # then the current vm is in creation process. Calling
  1378. # `self.is_halted()` at this point, would instantiate libvirt, we want
  1379. # avoid that.
  1380. if self.name in self.app.domains.keys() and not self.is_halted():
  1381. raise qubes.exc.QubesVMNotHaltedError(
  1382. self, 'Cannot clone a running domain {!r}'.format(self.name))
  1383. msg = "Destination {!s} already exists".format(self.dir_path)
  1384. assert not os.path.exists(self.dir_path), msg
  1385. self.log.info('Creating directory: {0}'.format(self.dir_path))
  1386. os.makedirs(self.dir_path, mode=0o775, exist_ok=True)
  1387. if pool or pools:
  1388. # pylint: disable=attribute-defined-outside-init
  1389. self.volume_config = _patch_volume_config(self.volume_config, pool,
  1390. pools)
  1391. self.storage = qubes.storage.Storage(self)
  1392. yield from self.storage.clone(src)
  1393. self.storage.verify()
  1394. assert self.volumes != {}
  1395. # fire hooks
  1396. yield from self.fire_event_async('domain-clone-files', src=src)
  1397. #
  1398. # methods for querying domain state
  1399. #
  1400. # state of the machine
  1401. def get_power_state(self):
  1402. """Return power state description string.
  1403. Return value may be one of those:
  1404. =============== ========================================================
  1405. return value meaning
  1406. =============== ========================================================
  1407. ``'Halted'`` Machine is not active.
  1408. ``'Transient'`` Machine is running, but does not have :program:`guid`
  1409. or :program:`qrexec` available.
  1410. ``'Running'`` Machine is ready and running.
  1411. ``'Paused'`` Machine is paused.
  1412. ``'Suspended'`` Machine is S3-suspended.
  1413. ``'Halting'`` Machine is in process of shutting down.
  1414. ``'Dying'`` Machine is still in process of shutting down.
  1415. ``'Crashed'`` Machine crashed and is unusable, probably because of
  1416. bug in dom0.
  1417. ``'NA'`` Machine is in unknown state (most likely libvirt domain
  1418. is undefined).
  1419. =============== ========================================================
  1420. FIXME: graph below may be incomplete and wrong. Click on method name to
  1421. see its documentation.
  1422. .. graphviz::
  1423. digraph {
  1424. node [fontname="sans-serif"];
  1425. edge [fontname="mono"];
  1426. Halted;
  1427. NA;
  1428. Dying;
  1429. Crashed;
  1430. Transient;
  1431. Halting;
  1432. Running;
  1433. Paused [color=gray75 fontcolor=gray75];
  1434. Suspended;
  1435. NA -> Halted;
  1436. Halted -> NA [constraint=false];
  1437. Halted -> Transient
  1438. [xlabel="start()" URL="#qubes.vm.qubesvm.QubesVM.start"];
  1439. Transient -> Running;
  1440. Running -> Halting
  1441. [xlabel="shutdown()"
  1442. URL="#qubes.vm.qubesvm.QubesVM.shutdown"
  1443. constraint=false];
  1444. Halting -> Dying -> Halted [constraint=false];
  1445. /* cosmetic, invisible edges to put rank constraint */
  1446. Dying -> Halting [style="invis"];
  1447. Halting -> Transient [style="invis"];
  1448. Running -> Halted
  1449. [label="kill()"
  1450. URL="#qubes.vm.qubesvm.QubesVM.kill"
  1451. constraint=false];
  1452. Running -> Crashed [constraint=false];
  1453. Crashed -> Halted [constraint=false];
  1454. Running -> Paused
  1455. [label="pause()" URL="#qubes.vm.qubesvm.QubesVM.pause"
  1456. color=gray75 fontcolor=gray75];
  1457. Running -> Suspended
  1458. [label="suspend()" URL="#qubes.vm.qubesvm.QubesVM.suspend"
  1459. color=gray50 fontcolor=gray50];
  1460. Paused -> Running
  1461. [label="unpause()" URL="#qubes.vm.qubesvm.QubesVM.unpause"
  1462. color=gray75 fontcolor=gray75];
  1463. Suspended -> Running
  1464. [label="resume()" URL="#qubes.vm.qubesvm.QubesVM.resume"
  1465. color=gray50 fontcolor=gray50];
  1466. Running -> Suspended
  1467. [label="suspend()" URL="#qubes.vm.qubesvm.QubesVM.suspend"];
  1468. Suspended -> Running
  1469. [label="resume()" URL="#qubes.vm.qubesvm.QubesVM.resume"];
  1470. { rank=source; Halted NA };
  1471. { rank=same; Transient Halting };
  1472. { rank=same; Crashed Dying };
  1473. { rank=sink; Paused Suspended };
  1474. }
  1475. .. seealso::
  1476. http://wiki.libvirt.org/page/VM_lifecycle
  1477. Description of VM life cycle from the point of view of libvirt.
  1478. https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainState
  1479. Libvirt's enum describing precise state of a domain.
  1480. """ # pylint: disable=too-many-return-statements
  1481. # don't try to define libvirt domain, if it isn't there, VM surely
  1482. # isn't running
  1483. # reason for this "if": allow vm.is_running() in PCI (or other
  1484. # device) extension while constructing libvirt XML
  1485. if self.app.vmm.offline_mode:
  1486. return 'Halted'
  1487. if self._libvirt_domain is None:
  1488. try:
  1489. self._libvirt_domain = self.app.vmm.libvirt_conn.lookupByUUID(
  1490. self.uuid.bytes)
  1491. except libvirt.libvirtError as e:
  1492. if e.get_error_code() == libvirt.VIR_ERR_NO_DOMAIN:
  1493. return 'Halted'
  1494. raise
  1495. libvirt_domain = self.libvirt_domain
  1496. if libvirt_domain is None:
  1497. return 'Halted'
  1498. try:
  1499. if libvirt_domain.isActive():
  1500. # pylint: disable=line-too-long
  1501. if libvirt_domain.state()[0] == libvirt.VIR_DOMAIN_PAUSED:
  1502. return "Paused"
  1503. if libvirt_domain.state()[0] == libvirt.VIR_DOMAIN_CRASHED:
  1504. return "Crashed"
  1505. if libvirt_domain.state()[0] == libvirt.VIR_DOMAIN_SHUTDOWN:
  1506. return "Halting"
  1507. if libvirt_domain.state()[0] == libvirt.VIR_DOMAIN_SHUTOFF:
  1508. return "Dying"
  1509. if libvirt_domain.state()[
  1510. 0] == libvirt.VIR_DOMAIN_PMSUSPENDED: # nopep8
  1511. return "Suspended"
  1512. if not self.is_fully_usable():
  1513. return "Transient"
  1514. return "Running"
  1515. return 'Halted'
  1516. except libvirt.libvirtError as e:
  1517. if e.get_error_code() == libvirt.VIR_ERR_NO_DOMAIN:
  1518. return 'Halted'
  1519. raise
  1520. assert False
  1521. def is_halted(self):
  1522. """ Check whether this domain's state is 'Halted'
  1523. :returns: :py:obj:`True` if this domain is halted, \
  1524. :py:obj:`False` otherwise.
  1525. :rtype: bool
  1526. """
  1527. return self.get_power_state() == 'Halted'
  1528. def is_running(self):
  1529. """Check whether this domain is running.
  1530. :returns: :py:obj:`True` if this domain is started, \
  1531. :py:obj:`False` otherwise.
  1532. :rtype: bool
  1533. """
  1534. if self.app.vmm.offline_mode:
  1535. return False
  1536. # don't try to define libvirt domain, if it isn't there, VM surely
  1537. # isn't running
  1538. # reason for this "if": allow vm.is_running() in PCI (or other
  1539. # device) extension while constructing libvirt XML
  1540. if self._libvirt_domain is None:
  1541. try:
  1542. self._libvirt_domain = self.app.vmm.libvirt_conn.lookupByUUID(
  1543. self.uuid.bytes)
  1544. except libvirt.libvirtError as e:
  1545. if e.get_error_code() == libvirt.VIR_ERR_NO_DOMAIN:
  1546. return False
  1547. raise
  1548. return bool(self.libvirt_domain.isActive())
  1549. def is_paused(self):
  1550. """Check whether this domain is paused.
  1551. :returns: :py:obj:`True` if this domain is paused, \
  1552. :py:obj:`False` otherwise.
  1553. :rtype: bool
  1554. """
  1555. return self.libvirt_domain \
  1556. and self.libvirt_domain.state()[0] == libvirt.VIR_DOMAIN_PAUSED
  1557. def is_qrexec_running(self):
  1558. """Check whether qrexec for this domain is available.
  1559. :returns: :py:obj:`True` if qrexec is running, \
  1560. :py:obj:`False` otherwise.
  1561. :rtype: bool
  1562. """
  1563. if self.xid < 0: # pylint: disable=comparison-with-callable
  1564. return False
  1565. return os.path.exists('/var/run/qubes/qrexec.%s' % self.name)
  1566. def is_fully_usable(self):
  1567. return all(self.fire_event('domain-is-fully-usable'))
  1568. @qubes.events.handler('domain-is-fully-usable')
  1569. def on_domain_is_fully_usable(self, event):
  1570. """Check whether domain is running and sane.
  1571. Currently this checks for running qrexec.
  1572. """ # pylint: disable=unused-argument
  1573. if self.features.check_with_template('qrexec', False):
  1574. # Running gui-daemon implies also VM running
  1575. yield self.is_qrexec_running()
  1576. else:
  1577. yield True
  1578. # memory and disk
  1579. def get_mem(self):
  1580. """Get current memory usage from VM.
  1581. :returns: Memory usage [FIXME unit].
  1582. :rtype: FIXME
  1583. """
  1584. if self.libvirt_domain is None:
  1585. return 0
  1586. try:
  1587. if not self.libvirt_domain.isActive():
  1588. return 0
  1589. return self.libvirt_domain.info()[1]
  1590. except libvirt.libvirtError as e:
  1591. if e.get_error_code() in (
  1592. # qube no longer exists
  1593. libvirt.VIR_ERR_NO_DOMAIN,
  1594. # libxl_domain_info failed (race condition from isActive)
  1595. libvirt.VIR_ERR_INTERNAL_ERROR):
  1596. return 0
  1597. self.log.exception(
  1598. 'libvirt error code: {!r}'.format(e.get_error_code()))
  1599. raise
  1600. def get_mem_static_max(self):
  1601. """Get maximum memory available to VM.
  1602. :returns: Memory limit [FIXME unit].
  1603. :rtype: FIXME
  1604. """
  1605. if self.libvirt_domain is None:
  1606. return 0
  1607. try:
  1608. return self.libvirt_domain.maxMemory()
  1609. except libvirt.libvirtError as e:
  1610. if e.get_error_code() in (
  1611. # qube no longer exists
  1612. libvirt.VIR_ERR_NO_DOMAIN,
  1613. # libxl_domain_info failed (race condition from isActive)
  1614. libvirt.VIR_ERR_INTERNAL_ERROR):
  1615. return 0
  1616. self.log.exception(
  1617. 'libvirt error code: {!r}'.format(e.get_error_code()))
  1618. raise
  1619. def get_cputime(self):
  1620. """Get total CPU time burned by this domain since start.
  1621. :returns: CPU time usage [FIXME unit].
  1622. :rtype: FIXME
  1623. """
  1624. if self.libvirt_domain is None:
  1625. return 0
  1626. if self.libvirt_domain is None:
  1627. return 0
  1628. if not self.libvirt_domain.isActive():
  1629. return 0
  1630. try:
  1631. if not self.libvirt_domain.isActive():
  1632. return 0
  1633. # this does not work, because libvirt
  1634. # return self.libvirt_domain.getCPUStats(
  1635. # libvirt.VIR_NODE_CPU_STATS_ALL_CPUS, 0)[0]['cpu_time']/10**9
  1636. return self.libvirt_domain.info()[4]
  1637. except libvirt.libvirtError as e:
  1638. if e.get_error_code() in (
  1639. # qube no longer exists
  1640. libvirt.VIR_ERR_NO_DOMAIN,
  1641. # libxl_domain_info failed (race condition from isActive)
  1642. libvirt.VIR_ERR_INTERNAL_ERROR):
  1643. return 0
  1644. self.log.exception(
  1645. 'libvirt error code: {!r}'.format(e.get_error_code()))
  1646. raise
  1647. # miscellanous
  1648. @qubes.stateless_property
  1649. def start_time(self):
  1650. """Tell when machine was started.
  1651. :rtype: float or None
  1652. """
  1653. if not self.is_running():
  1654. return None
  1655. # TODO shouldn't this be qubesdb?
  1656. start_time = self.app.vmm.xs.read('',
  1657. '/vm/{}/start_time'.format(self.uuid))
  1658. if start_time != '':
  1659. return float(start_time)
  1660. return None
  1661. @qubes.stateless_property
  1662. def icon(self):
  1663. """freedesktop icon name, suitable for use in
  1664. :py:meth:`PyQt4.QtGui.QIcon.fromTheme`"""
  1665. # pylint: disable=comparison-with-callable
  1666. raw_icon_name = self.label.name
  1667. if self.klass == 'TemplateVM':
  1668. return 'templatevm-' + raw_icon_name
  1669. if self.klass == 'DispVM':
  1670. return 'dispvm-' + raw_icon_name
  1671. if self.features.get('servicevm', False):
  1672. return 'servicevm-' + raw_icon_name
  1673. return 'appvm-' + raw_icon_name
  1674. @property
  1675. def kernelopts_common(self):
  1676. """Kernel options which should be used in addition to *kernelopts*
  1677. property.
  1678. This is specific to kernel (and initrd if any)
  1679. """
  1680. if not self.kernel:
  1681. return ''
  1682. kernels_dir = self.storage.kernels_dir
  1683. kernelopts_path = os.path.join(kernels_dir,
  1684. 'default-kernelopts-common.txt')
  1685. if os.path.exists(kernelopts_path):
  1686. with open(kernelopts_path) as f_kernelopts:
  1687. return f_kernelopts.read().rstrip('\n\r')
  1688. else:
  1689. return qubes.config.defaults['kernelopts_common']
  1690. #
  1691. # helper methods
  1692. #
  1693. def relative_path(self, path):
  1694. """Return path relative to py:attr:`dir_path`.
  1695. :param str path: Path in question.
  1696. :returns: Relative path.
  1697. """
  1698. return os.path.relpath(path, self.dir_path)
  1699. def create_qdb_entries(self):
  1700. """Create entries in Qubes DB.
  1701. """
  1702. # pylint: disable=no-member
  1703. self.untrusted_qdb.write('/name', self.name)
  1704. self.untrusted_qdb.write('/type', self.__class__.__name__)
  1705. self.untrusted_qdb.write('/default-user', self.default_user)
  1706. self.untrusted_qdb.write('/qubes-vm-updateable', str(self.updateable))
  1707. self.untrusted_qdb.write('/qubes-vm-persistence',
  1708. 'full' if self.updateable else 'rw-only')
  1709. self.untrusted_qdb.write('/qubes-debug-mode', str(int(self.debug)))
  1710. try:
  1711. self.untrusted_qdb.write('/qubes-base-template', self.template.name)
  1712. except AttributeError:
  1713. self.untrusted_qdb.write('/qubes-base-template', '')
  1714. self.untrusted_qdb.write('/qubes-random-seed',
  1715. base64.b64encode(qubes.utils.urandom(64)))
  1716. if self.provides_network:
  1717. # '/qubes-netvm-network' value is only checked for being non empty
  1718. self.untrusted_qdb.write('/qubes-netvm-network', str(self.gateway))
  1719. self.untrusted_qdb.write('/qubes-netvm-gateway', str(self.gateway))
  1720. if self.gateway6: # pylint: disable=using-constant-test
  1721. self.untrusted_qdb.write('/qubes-netvm-gateway6',
  1722. str(self.gateway6))
  1723. self.untrusted_qdb.write('/qubes-netvm-netmask', str(self.netmask))
  1724. for i, addr in zip(('primary', 'secondary'), self.dns):
  1725. self.untrusted_qdb.write('/qubes-netvm-{}-dns'.format(i), addr)
  1726. if self.netvm is not None:
  1727. self.untrusted_qdb.write('/qubes-mac', str(self.mac))
  1728. self.untrusted_qdb.write('/qubes-ip', str(self.visible_ip))
  1729. self.untrusted_qdb.write('/qubes-netmask',
  1730. str(self.visible_netmask))
  1731. self.untrusted_qdb.write('/qubes-gateway',
  1732. str(self.visible_gateway))
  1733. for i, addr in zip(('primary', 'secondary'), self.dns):
  1734. self.untrusted_qdb.write('/qubes-{}-dns'.format(i), str(addr))
  1735. if self.visible_ip6: # pylint: disable=using-constant-test
  1736. self.untrusted_qdb.write('/qubes-ip6', str(self.visible_ip6))
  1737. if self.visible_gateway6: # pylint: disable=using-constant-test
  1738. self.untrusted_qdb.write('/qubes-gateway6',
  1739. str(self.visible_gateway6))
  1740. tzname = qubes.utils.get_timezone()
  1741. if tzname:
  1742. self.untrusted_qdb.write('/qubes-timezone', tzname)
  1743. self.untrusted_qdb.write('/qubes-block-devices', '')
  1744. self.untrusted_qdb.write('/qubes-usb-devices', '')
  1745. # TODO: Currently the whole qmemman is quite Xen-specific, so stay with
  1746. # xenstore for it until decided otherwise
  1747. if qmemman_present:
  1748. self.app.vmm.xs.set_permissions('',
  1749. '/local/domain/{}/memory'.format(
  1750. self.xid),
  1751. [{'dom': self.xid}])
  1752. self.fire_event('domain-qdb-create')
  1753. # TODO async; update this in constructor
  1754. def _update_libvirt_domain(self):
  1755. """Re-initialise :py:attr:`libvirt_domain`."""
  1756. domain_config = self.create_config_file()
  1757. try:
  1758. self._libvirt_domain = self.app.vmm.libvirt_conn.defineXML(
  1759. domain_config)
  1760. except libvirt.libvirtError as e:
  1761. if e.get_error_code() == libvirt.VIR_ERR_OS_TYPE \
  1762. and e.get_str2() == 'hvm':
  1763. raise qubes.exc.QubesVMError(
  1764. self,
  1765. 'HVM qubes are not supported on this machine. '
  1766. 'Check BIOS settings for VT-x/AMD-V extensions.')
  1767. raise
  1768. #
  1769. # workshop -- those are to be reworked later
  1770. #
  1771. def get_prefmem(self):
  1772. # TODO: qmemman is still xen specific
  1773. untrusted_meminfo_key = self.app.vmm.xs.read(
  1774. '', '/local/domain/{}/memory/meminfo'.format(self.xid))
  1775. if untrusted_meminfo_key is None or untrusted_meminfo_key == '':
  1776. return 0
  1777. domain = qubes.qmemman.DomainState(self.xid)
  1778. qubes.qmemman.algo.refresh_meminfo_for_domain(
  1779. domain, untrusted_meminfo_key)
  1780. if domain.mem_used is None:
  1781. # apparently invalid xenstore content
  1782. return 0
  1783. domain.memory_maximum = self.get_mem_static_max() * 1024
  1784. return qubes.qmemman.algo.prefmem(domain) / 1024
  1785. def _clean_volume_config(config):
  1786. common_attributes = ['name', 'pool', 'size',
  1787. 'rw', 'snap_on_start',
  1788. 'save_on_stop', 'source']
  1789. return {k: v for k, v in config.items() if k in common_attributes}
  1790. def _patch_pool_config(config, pool=None, pools=None):
  1791. assert pool is not None or pools is not None
  1792. is_snapshot = config['snap_on_start']
  1793. is_rw = config['rw']
  1794. name = config['name']
  1795. if pool and not is_snapshot and is_rw:
  1796. config['pool'] = str(pool)
  1797. elif pool:
  1798. pass
  1799. elif pools and name in pools.keys():
  1800. if not is_snapshot:
  1801. config['pool'] = str(pools[name])
  1802. else:
  1803. msg = "Snapshot volume {0!s} must be in the same pool as its " \
  1804. "origin ({0!s} volume of template)," \
  1805. "cannot move to pool {1!s} " \
  1806. .format(name, pools[name])
  1807. raise qubes.exc.QubesException(msg)
  1808. return config
  1809. def _patch_volume_config(volume_config, pool=None, pools=None):
  1810. assert not (pool and pools), \
  1811. 'You can not pass pool & pools parameter at same time'
  1812. assert pool or pools
  1813. result = {}
  1814. for name, config in volume_config.items():
  1815. # copy only the subset of volume_config key/values
  1816. dst_config = _clean_volume_config(config)
  1817. if pool is not None or pools is not None:
  1818. dst_config = _patch_pool_config(dst_config, pool, pools)
  1819. result[name] = dst_config
  1820. return result