app.py 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  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) 2011-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 collections.abc
  23. import copy
  24. import functools
  25. import grp
  26. import itertools
  27. import logging
  28. import os
  29. import random
  30. import sys
  31. import time
  32. import traceback
  33. import uuid
  34. from contextlib import suppress
  35. import asyncio
  36. import jinja2
  37. import libvirt
  38. import lxml.etree
  39. try:
  40. import xen.lowlevel.xs # pylint: disable=wrong-import-order
  41. import xen.lowlevel.xc # pylint: disable=wrong-import-order
  42. except ImportError:
  43. pass
  44. if os.name == 'posix':
  45. # pylint: disable=wrong-import-order
  46. import fcntl
  47. elif os.name == 'nt':
  48. # pylint: disable=import-error
  49. import win32con
  50. import win32file
  51. import pywintypes
  52. else:
  53. raise RuntimeError("Qubes works only on POSIX or WinNT systems")
  54. # pylint: disable=wrong-import-position
  55. import qubes
  56. import qubes.ext
  57. import qubes.utils
  58. import qubes.storage
  59. import qubes.storage.reflink
  60. import qubes.vm
  61. import qubes.vm.adminvm
  62. import qubes.vm.qubesvm
  63. import qubes.vm.templatevm
  64. # pylint: enable=wrong-import-position
  65. class VirDomainWrapper:
  66. # pylint: disable=too-few-public-methods
  67. def __init__(self, connection, vm):
  68. self._connection = connection
  69. self._vm = vm
  70. def _reconnect_if_dead(self):
  71. try:
  72. is_dead = not self._vm.connect().isAlive()
  73. except libvirt.libvirtError as ex:
  74. if ex.get_error_code() == libvirt.VIR_ERR_INVALID_CONN:
  75. # connection to libvirt was re-established in the meantime
  76. is_dead = True
  77. else:
  78. raise
  79. if is_dead:
  80. # pylint: disable=protected-access
  81. self._connection._reconnect_if_dead()
  82. self._vm = self._connection._conn.lookupByUUID(self._vm.UUID())
  83. return is_dead
  84. def __getattr__(self, attrname):
  85. attr = getattr(self._vm, attrname)
  86. if not isinstance(attr, collections.abc.Callable):
  87. return attr
  88. @functools.wraps(attr)
  89. def wrapper(*args, **kwargs):
  90. try:
  91. return attr(*args, **kwargs)
  92. except libvirt.libvirtError:
  93. if self._reconnect_if_dead():
  94. return getattr(self._vm, attrname)(*args, **kwargs)
  95. raise
  96. return wrapper
  97. class VirConnectWrapper:
  98. # pylint: disable=too-few-public-methods
  99. def __init__(self, uri, reconnect_cb=None):
  100. self._conn = libvirt.open(uri)
  101. self._reconnect_cb = reconnect_cb
  102. def _reconnect_if_dead(self):
  103. is_dead = not self._conn.isAlive()
  104. if is_dead:
  105. uri = self._conn.getURI()
  106. old_conn = self._conn
  107. self._conn = libvirt.open(uri)
  108. if callable(self._reconnect_cb):
  109. self._reconnect_cb(old_conn)
  110. old_conn.close()
  111. return is_dead
  112. def _wrap_domain(self, ret):
  113. if isinstance(ret, libvirt.virDomain):
  114. ret = VirDomainWrapper(self, ret)
  115. return ret
  116. def __getattr__(self, attrname):
  117. attr = getattr(self._conn, attrname)
  118. if not isinstance(attr, collections.abc.Callable):
  119. return attr
  120. if attrname == 'close':
  121. return attr
  122. @functools.wraps(attr)
  123. def wrapper(*args, **kwargs):
  124. try:
  125. return self._wrap_domain(attr(*args, **kwargs))
  126. except libvirt.libvirtError:
  127. if self._reconnect_if_dead():
  128. return self._wrap_domain(
  129. getattr(self._conn, attrname)(*args, **kwargs))
  130. raise
  131. return wrapper
  132. class VMMConnection:
  133. """Connection to Virtual Machine Manager (libvirt)"""
  134. def __init__(self, offline_mode=None, libvirt_reconnect_cb=None):
  135. """
  136. :param offline_mode: enable/disable offline mode; default is to
  137. enable when running in chroot as root, otherwise disable
  138. :param libvirt_reconnect_cb: callable to be called when connection to
  139. libvirt is re-established; the callback is called with old connection
  140. as argument
  141. """
  142. if offline_mode is None:
  143. offline_mode = bool(os.getuid() == 0 and
  144. os.stat('/') != os.stat('/proc/1/root/.'))
  145. self._offline_mode = offline_mode
  146. self._libvirt_reconnect_cb = libvirt_reconnect_cb
  147. self._libvirt_conn = None
  148. self._xs = None
  149. self._xc = None
  150. @property
  151. def offline_mode(self):
  152. """Check or enable offline mode (do not actually connect to vmm)"""
  153. return self._offline_mode
  154. def _libvirt_error_handler(self, ctx, error):
  155. pass
  156. def init_vmm_connection(self):
  157. """Initialise connection
  158. This method is automatically called when getting"""
  159. if self._libvirt_conn is not None:
  160. # Already initialized
  161. return
  162. if self._offline_mode:
  163. # Do not initialize in offline mode
  164. raise qubes.exc.QubesException(
  165. 'VMM operations disabled in offline mode')
  166. if 'xen.lowlevel.xs' in sys.modules:
  167. self._xs = xen.lowlevel.xs.xs()
  168. if 'xen.lowlevel.xc' in sys.modules:
  169. self._xc = xen.lowlevel.xc.xc()
  170. self._libvirt_conn = VirConnectWrapper(
  171. qubes.config.defaults['libvirt_uri'],
  172. reconnect_cb=self._libvirt_reconnect_cb)
  173. libvirt.registerErrorHandler(self._libvirt_error_handler, None)
  174. @property
  175. def libvirt_conn(self):
  176. """Connection to libvirt"""
  177. self.init_vmm_connection()
  178. return self._libvirt_conn
  179. @property
  180. def xs(self):
  181. """Connection to Xen Store
  182. This property in available only when running on Xen.
  183. """
  184. # XXX what about the case when we run under KVM,
  185. # but xen modules are importable?
  186. if 'xen.lowlevel.xs' not in sys.modules:
  187. raise AttributeError(
  188. 'xs object is available under Xen hypervisor only')
  189. self.init_vmm_connection()
  190. return self._xs
  191. @property
  192. def xc(self):
  193. """Connection to Xen
  194. This property in available only when running on Xen.
  195. """
  196. # XXX what about the case when we run under KVM,
  197. # but xen modules are importable?
  198. if 'xen.lowlevel.xc' not in sys.modules:
  199. raise AttributeError(
  200. 'xc object is available under Xen hypervisor only')
  201. self.init_vmm_connection()
  202. return self._xc
  203. def close(self):
  204. libvirt.registerErrorHandler(None, None)
  205. if self._xs:
  206. self._xs.close()
  207. self._xs = None
  208. if self._libvirt_conn:
  209. self._libvirt_conn.close()
  210. self._libvirt_conn = None
  211. self._xc = None # and pray it will get garbage-collected
  212. class QubesHost:
  213. """Basic information about host machine
  214. :param qubes.Qubes app: Qubes application context (must have \
  215. :py:attr:`Qubes.vmm` attribute defined)
  216. """
  217. def __init__(self, app):
  218. self.app = app
  219. self._no_cpus = None
  220. self._total_mem = None
  221. self._physinfo = None
  222. self._cpu_family = None
  223. self._cpu_model = None
  224. def _fetch(self):
  225. if self._no_cpus is not None:
  226. return
  227. # pylint: disable=unused-variable
  228. (model, memory, cpus, mhz, nodes, socket, cores, threads) = \
  229. self.app.vmm.libvirt_conn.getInfo()
  230. self._total_mem = int(memory) * 1024
  231. self._no_cpus = cpus
  232. self.app.log.debug('QubesHost: no_cpus={} memory_total={}'.format(
  233. self.no_cpus, self.memory_total))
  234. with suppress(NotImplementedError):
  235. self.app.log.debug('QubesHost: xen_free_memory={}'.format(
  236. self.get_free_xen_memory()))
  237. @property
  238. def memory_total(self):
  239. """Total memory, in kbytes"""
  240. if self.app.vmm.offline_mode:
  241. return 2 ** 64 - 1
  242. self._fetch()
  243. return self._total_mem
  244. @property
  245. def no_cpus(self):
  246. """Number of CPUs"""
  247. if self.app.vmm.offline_mode:
  248. return 42
  249. self._fetch()
  250. return self._no_cpus
  251. @property
  252. def cpu_family_model(self):
  253. """Get CPU family and model"""
  254. if self._cpu_family is None or self._cpu_model is None:
  255. family = None
  256. model = None
  257. with open('/proc/cpuinfo') as cpuinfo:
  258. for line in cpuinfo.readlines():
  259. line = line.strip()
  260. if not line:
  261. # take info from the first core
  262. break
  263. field, value = line.split(':', 1)
  264. if field.strip() == 'model':
  265. model = int(value.strip())
  266. elif field.strip() == 'cpu family':
  267. family = int(value.strip())
  268. self._cpu_family = family
  269. self._cpu_model = model
  270. return self._cpu_family, self._cpu_model
  271. def get_free_xen_memory(self):
  272. """Get free memory from Xen's physinfo.
  273. :raises NotImplementedError: when not under Xen
  274. """
  275. try:
  276. self._physinfo = self.app.vmm.xc.physinfo()
  277. except AttributeError:
  278. raise NotImplementedError('This function requires Xen hypervisor')
  279. return int(self._physinfo['free_memory'])
  280. def is_iommu_supported(self):
  281. """Check if IOMMU is supported on this platform"""
  282. if self._physinfo is None:
  283. try:
  284. self._physinfo = self.app.vmm.xc.physinfo()
  285. except AttributeError:
  286. raise NotImplementedError(
  287. 'This function requires Xen hypervisor')
  288. return 'hvm_directio' in self._physinfo['virt_caps']
  289. def get_vm_stats(self, previous_time=None, previous=None, only_vm=None):
  290. """Measure cpu usage for all domains at once.
  291. If previous measurements are given, CPU usage will be given in
  292. percents of time. Otherwise only absolute value (seconds).
  293. Return a tuple of (measurements_time, measurements),
  294. where measurements is a dictionary with key: domid, value: dict:
  295. - cpu_time - absolute CPU usage (seconds since its startup)
  296. - cpu_usage_raw - CPU usage in %
  297. - cpu_usage - CPU usage in % (normalized to number of vcpus)
  298. - memory_kb - current memory assigned, in kb
  299. This function requires Xen hypervisor.
  300. ..warning:
  301. This function may return info about implementation-specific VMs,
  302. like stubdomains for HVM
  303. :param previous: previous measurement
  304. :param previous_time: time of previous measurement
  305. :param only_vm: get measurements only for this VM
  306. :raises NotImplementedError: when not under Xen
  307. """
  308. if (previous_time is None) != (previous is None):
  309. raise ValueError(
  310. 'previous and previous_time must be given together (or none)')
  311. if previous is None:
  312. previous = {}
  313. current_time = time.time()
  314. current = {}
  315. try:
  316. if only_vm:
  317. xid = only_vm.xid
  318. if xid < 0:
  319. raise qubes.exc.QubesVMNotRunningError(only_vm)
  320. info = self.app.vmm.xc.domain_getinfo(xid, 1)
  321. if info[0]['domid'] != xid:
  322. raise qubes.exc.QubesVMNotRunningError(only_vm)
  323. else:
  324. info = self.app.vmm.xc.domain_getinfo(0, 1024)
  325. except AttributeError:
  326. raise NotImplementedError(
  327. 'This function requires Xen hypervisor')
  328. # TODO: add stubdomain stats to actual VMs
  329. for vm in info:
  330. domid = vm['domid']
  331. current[domid] = {}
  332. current[domid]['memory_kb'] = vm['mem_kb']
  333. current[domid]['cpu_time'] = int(vm['cpu_time'])
  334. vcpus = max(vm['online_vcpus'], 1)
  335. if domid in previous:
  336. current[domid]['cpu_usage_raw'] = int(
  337. (current[domid]['cpu_time'] - previous[domid]['cpu_time'])
  338. / 1000 ** 3 * 100 / (current_time - previous_time))
  339. if current[domid]['cpu_usage_raw'] < 0:
  340. # VM has been rebooted
  341. current[domid]['cpu_usage_raw'] = 0
  342. else:
  343. current[domid]['cpu_usage_raw'] = 0
  344. current[domid]['cpu_usage'] = \
  345. int(current[domid]['cpu_usage_raw'] / vcpus)
  346. return current_time, current
  347. class VMCollection:
  348. """A collection of Qubes VMs
  349. VMCollection supports ``in`` operator. You may test for ``qid``, ``name``
  350. and whole VM object's presence.
  351. Iterating over VMCollection will yield machine objects.
  352. """
  353. def __init__(self, app):
  354. self.app = app
  355. self._dict = dict()
  356. def close(self):
  357. del self.app
  358. self._dict.clear()
  359. del self._dict
  360. def __repr__(self):
  361. return '<{} {!r}>'.format(
  362. self.__class__.__name__, list(sorted(self.keys())))
  363. def items(self):
  364. """Iterate over ``(qid, vm)`` pairs"""
  365. for qid in self.qids():
  366. yield (qid, self[qid])
  367. def qids(self):
  368. """Iterate over all qids
  369. qids are sorted by numerical order.
  370. """
  371. return iter(sorted(self._dict.keys()))
  372. keys = qids
  373. def names(self):
  374. """Iterate over all names
  375. names are sorted by lexical order.
  376. """
  377. return iter(sorted(vm.name for vm in self._dict.values()))
  378. def vms(self):
  379. """Iterate over all machines
  380. vms are sorted by qid.
  381. """
  382. return iter(sorted(self._dict.values()))
  383. __iter__ = vms
  384. values = vms
  385. def add(self, value, _enable_events=True):
  386. """Add VM to collection
  387. :param qubes.vm.BaseVM value: VM to add
  388. :param _enable_events:
  389. :raises TypeError: when value is of wrong type
  390. :raises ValueError: when there is already VM which has equal ``qid``
  391. """
  392. # this violates duck typing, but is needed
  393. # for VMProperty to function correctly
  394. if not isinstance(value, qubes.vm.BaseVM):
  395. raise TypeError('{} holds only BaseVM instances'.format(
  396. self.__class__.__name__))
  397. if value.qid in self:
  398. raise ValueError('This collection already holds VM that has '
  399. 'qid={!r} ({!r})'.format(value.qid,
  400. self[value.qid]))
  401. if value.name in self:
  402. raise ValueError('A VM named {!s} already exists'
  403. .format(value.name))
  404. self._dict[value.qid] = value
  405. if _enable_events:
  406. value.events_enabled = True
  407. self.app.fire_event('domain-add', vm=value)
  408. return value
  409. def __getitem__(self, key):
  410. if isinstance(key, int):
  411. return self._dict[key]
  412. if isinstance(key, str):
  413. for vm in self:
  414. if vm.name == key:
  415. return vm
  416. raise KeyError(key)
  417. if isinstance(key, qubes.vm.BaseVM):
  418. key = key.uuid
  419. if isinstance(key, uuid.UUID):
  420. for vm in self:
  421. if vm.uuid == key:
  422. return vm
  423. raise KeyError(key)
  424. raise KeyError(key)
  425. def __delitem__(self, key):
  426. vm = self[key]
  427. if not vm.is_halted():
  428. raise qubes.exc.QubesVMNotHaltedError(vm)
  429. self.app.fire_event('domain-pre-delete', pre_event=True, vm=vm)
  430. try:
  431. if vm.libvirt_domain:
  432. vm.libvirt_domain.undefine()
  433. # pylint: disable=protected-access
  434. vm._libvirt_domain = None
  435. except libvirt.libvirtError as e:
  436. if e.get_error_code() == libvirt.VIR_ERR_NO_DOMAIN:
  437. # already undefined
  438. pass
  439. del self._dict[vm.qid]
  440. self.app.fire_event('domain-delete', vm=vm)
  441. def __contains__(self, key):
  442. return any((key in (vm, vm.qid, vm.name))
  443. for vm in self)
  444. def __len__(self):
  445. return len(self._dict)
  446. def get_vms_based_on(self, template):
  447. template = self[template]
  448. return set(vm for vm in self
  449. if hasattr(vm, 'template') and vm.template == template)
  450. def get_vms_connected_to(self, netvm):
  451. new_vms = {self[netvm]}
  452. dependent_vms = set()
  453. # Dependency resolving only makes sense on NetVM (or derivative)
  454. # if not self[netvm_qid].is_netvm():
  455. # return set([])
  456. while new_vms:
  457. cur_vm = new_vms.pop()
  458. for vm in cur_vm.connected_vms:
  459. if vm in dependent_vms:
  460. continue
  461. dependent_vms.add(vm)
  462. # if vm.is_netvm():
  463. new_vms.add(vm)
  464. return dependent_vms
  465. # XXX with Qubes Admin Api this will probably lead to race condition
  466. # whole process of creating and adding should be synchronised
  467. def get_new_unused_qid(self):
  468. used_ids = set(self.qids())
  469. for i in range(1, qubes.config.max_qid):
  470. if i not in used_ids:
  471. return i
  472. raise LookupError("Cannot find unused qid!")
  473. def get_new_unused_dispid(self):
  474. for _ in range(int(qubes.config.max_dispid ** 0.5)):
  475. dispid = random.SystemRandom().randrange(qubes.config.max_dispid)
  476. if not any(getattr(vm, 'dispid', None) == dispid for vm in self):
  477. return dispid
  478. raise LookupError((
  479. 'https://xkcd.com/221/',
  480. 'http://dilbert.com/strip/2001-10-25')[
  481. random.randint(0, 1)])
  482. def _default_pool(app):
  483. """ Default storage pool.
  484. 1. If there is one named 'default', use it.
  485. 2. Check if root fs is on LVM thin - use that
  486. 3. Look for file(-reflink)-based pool pointing to /var/lib/qubes
  487. 4. Fail
  488. """
  489. if 'default' in app.pools:
  490. return app.pools['default']
  491. if 'DEFAULT_LVM_POOL' in os.environ:
  492. thin_pool = os.environ['DEFAULT_LVM_POOL']
  493. for pool in app.pools.values():
  494. if pool.config.get('driver', None) != 'lvm_thin':
  495. continue
  496. if pool.config['thin_pool'] == thin_pool:
  497. return pool
  498. # no DEFAULT_LVM_POOL, or pool not defined
  499. root_volume_group, root_thin_pool = \
  500. qubes.storage.DirectoryThinPool.thin_pool('/')
  501. if root_thin_pool:
  502. for pool in app.pools.values():
  503. if pool.config.get('driver', None) != 'lvm_thin':
  504. continue
  505. if (pool.config['volume_group'] == root_volume_group and
  506. pool.config['thin_pool'] == root_thin_pool):
  507. return pool
  508. # not a thin volume? look for file pools
  509. for pool in app.pools.values():
  510. if pool.config.get('driver', None) not in ('file', 'file-reflink'):
  511. continue
  512. if pool.config['dir_path'] == qubes.config.qubes_base_dir:
  513. return pool
  514. raise AttributeError('Cannot determine default storage pool')
  515. def _setter_pool(app, prop, value):
  516. if isinstance(value, qubes.storage.Pool):
  517. return value
  518. try:
  519. return app.pools[value]
  520. except KeyError:
  521. raise qubes.exc.QubesPropertyValueError(app, prop, value,
  522. 'No such storage pool')
  523. def _setter_default_netvm(app, prop, value):
  524. # skip netvm loop check while loading qubes.xml, to avoid tricky loading
  525. # order
  526. if not app.events_enabled:
  527. return value
  528. if value is None:
  529. return value
  530. # forbid setting to a value that would result in netvm loop
  531. for vm in app.domains:
  532. if not hasattr(vm, 'netvm'):
  533. continue
  534. if not vm.property_is_default('netvm'):
  535. continue
  536. if value == vm \
  537. or value in app.domains.get_vms_connected_to(vm):
  538. raise qubes.exc.QubesPropertyValueError(
  539. app, prop, value, 'Network loop on \'{!s}\''.format(vm))
  540. return value
  541. class Qubes(qubes.PropertyHolder):
  542. """Main Qubes application
  543. :param str store: path to ``qubes.xml``
  544. The store is loaded in stages:
  545. 1. In the first stage there are loaded some basic features from store
  546. (currently labels).
  547. 2. In the second stage stubs for all VMs are loaded. They are filled
  548. with their basic properties, like ``qid`` and ``name``.
  549. 3. In the third stage all global properties are loaded. They often
  550. reference VMs, like default netvm, so they should be filled after
  551. loading VMs.
  552. 4. In the fourth stage all remaining VM properties are loaded. They
  553. also need all VMs loaded, because they represent dependencies
  554. between VMs like aforementioned netvm.
  555. 5. In the fifth stage there are some fixups to ensure sane system
  556. operation.
  557. This class emits following events:
  558. .. event:: domain-add (subject, event, vm)
  559. When domain is added.
  560. :param subject: Event emitter
  561. :param event: Event name (``'domain-add'``)
  562. :param vm: Domain object
  563. .. event:: domain-pre-delete (subject, event, vm)
  564. When domain is deleted. VM still has reference to ``app`` object,
  565. and is contained within VMCollection. You may prevent removal by
  566. raising an exception.
  567. :param subject: Event emitter
  568. :param event: Event name (``'domain-pre-delete'``)
  569. :param vm: Domain object
  570. .. event:: domain-delete (subject, event, vm)
  571. When domain is deleted. VM still has reference to ``app`` object,
  572. but is not contained within VMCollection.
  573. :param subject: Event emitter
  574. :param event: Event name (``'domain-delete'``)
  575. :param vm: Domain object
  576. .. event:: pool-add (subject, event, pool)
  577. When storage pool is added.
  578. Handler for this event can be asynchronous (a coroutine).
  579. :param subject: Event emitter
  580. :param event: Event name (``'pool-add'``)
  581. :param pool: Pool object
  582. .. event:: pool-pre-delete (subject, event, pool)
  583. When pool is deleted. Pool is still contained within app.pools
  584. dictionary. You may prevent removal by raising an exception.
  585. Handler for this event can be asynchronous (a coroutine).
  586. :param subject: Event emitter
  587. :param event: Event name (``'pool-pre-delete'``)
  588. :param pool: Pool object
  589. .. event:: pool-delete (subject, event, pool)
  590. When storage pool is deleted. The pool is already removed at this
  591. point.
  592. Handler for this event can be asynchronous (a coroutine).
  593. :param subject: Event emitter
  594. :param event: Event name (``'pool-delete'``)
  595. :param pool: Pool object
  596. .. event:: qubes-close (subject, event)
  597. Fired when this Qubes() object instance is going to be closed
  598. and destroyed. In practice it is called only during tests, to
  599. cleanup objects from one test, before another.
  600. It is _not_ called when qubesd daemon is stopped.
  601. :param subject: Event emitter
  602. :param event: Event name (``'qubes-close'``)
  603. Methods and attributes:
  604. """
  605. default_guivm = qubes.VMProperty(
  606. 'default_guivm',
  607. load_stage=3,
  608. default=lambda app: app.domains['dom0'], allow_none=True,
  609. doc='Default GuiVM for VMs.')
  610. default_audiovm = qubes.VMProperty(
  611. 'default_audiovm',
  612. load_stage=3,
  613. default=lambda app: app.domains['dom0'], allow_none=True,
  614. doc='Default AudioVM for VMs.')
  615. default_netvm = qubes.VMProperty(
  616. 'default_netvm',
  617. load_stage=3,
  618. default=None, allow_none=True,
  619. setter=_setter_default_netvm,
  620. doc="""Default NetVM for AppVMs. Initial state is `None`, which means
  621. that AppVMs are not connected to the Internet.""")
  622. default_template = qubes.VMProperty(
  623. 'default_template', load_stage=3,
  624. vmclass=qubes.vm.templatevm.TemplateVM,
  625. doc='Default template for new AppVMs',
  626. allow_none=True)
  627. updatevm = qubes.VMProperty(
  628. 'updatevm', load_stage=3,
  629. default=None, allow_none=True,
  630. doc="""Which VM to use as `yum` proxy for updating AdminVM and
  631. TemplateVMs""")
  632. clockvm = qubes.VMProperty(
  633. 'clockvm', load_stage=3,
  634. default=None, allow_none=True,
  635. doc='Which VM to use as NTP proxy for updating '
  636. 'AdminVM')
  637. default_kernel = qubes.property(
  638. 'default_kernel', load_stage=3,
  639. doc='Which kernel to use when not overriden in VM')
  640. default_dispvm = qubes.VMProperty(
  641. 'default_dispvm',
  642. load_stage=3,
  643. default=None,
  644. doc='Default DispVM base for service calls',
  645. allow_none=True)
  646. management_dispvm = qubes.VMProperty(
  647. 'management_dispvm',
  648. load_stage=3,
  649. default=None,
  650. doc='Default DispVM base for managing VMs',
  651. allow_none=True)
  652. default_pool = qubes.property(
  653. 'default_pool',
  654. load_stage=3,
  655. default=_default_pool,
  656. setter=_setter_pool,
  657. doc='Default storage pool')
  658. default_pool_private = qubes.property(
  659. 'default_pool_private',
  660. load_stage=3,
  661. default=lambda app: app.default_pool,
  662. setter=_setter_pool,
  663. doc='Default storage pool for private volumes')
  664. default_pool_root = qubes.property(
  665. 'default_pool_root',
  666. load_stage=3,
  667. default=lambda app: app.default_pool,
  668. setter=_setter_pool,
  669. doc='Default storage pool for root volumes')
  670. default_pool_volatile = qubes.property(
  671. 'default_pool_volatile',
  672. load_stage=3,
  673. default=lambda app: app.default_pool,
  674. setter=_setter_pool,
  675. doc='Default storage pool for volatile volumes')
  676. default_pool_kernel = qubes.property(
  677. 'default_pool_kernel',
  678. load_stage=3,
  679. default=lambda app: app.default_pool,
  680. setter=_setter_pool,
  681. doc='Default storage pool for kernel volumes')
  682. default_qrexec_timeout = qubes.property(
  683. 'default_qrexec_timeout',
  684. load_stage=3,
  685. default=60,
  686. type=int,
  687. doc="""Default time in seconds after which qrexec connection attempt
  688. is deemed failed""")
  689. default_shutdown_timeout = qubes.property(
  690. 'default_shutdown_timeout',
  691. load_stage=3,
  692. default=60,
  693. type=int,
  694. doc="""Default time in seconds for VM shutdown to complete""")
  695. stats_interval = qubes.property(
  696. 'stats_interval',
  697. load_stage=3,
  698. default=3,
  699. type=int,
  700. doc='Interval in seconds for VM stats reporting (memory, CPU usage)')
  701. # TODO #1637 #892
  702. check_updates_vm = qubes.property(
  703. 'check_updates_vm',
  704. type=bool,
  705. setter=qubes.property.bool,
  706. load_stage=3,
  707. default=True,
  708. doc='Check for updates inside qubes')
  709. def __init__(self, store=None, load=True, offline_mode=None, lock=False,
  710. **kwargs):
  711. #: logger instance for logging global messages
  712. self.log = logging.getLogger('app')
  713. self.log.debug('init() -> %#x', id(self))
  714. self.log.debug('stack:')
  715. for frame in traceback.extract_stack():
  716. self.log.debug('%s', frame)
  717. self._extensions = qubes.ext.get_extensions()
  718. #: collection of all VMs managed by this Qubes instance
  719. self.domains = VMCollection(self)
  720. #: collection of all available labels for VMs
  721. self.labels = {}
  722. #: collection of all pools
  723. self.pools = {}
  724. #: Connection to VMM
  725. self.vmm = VMMConnection(
  726. offline_mode=offline_mode,
  727. libvirt_reconnect_cb=self.register_event_handlers)
  728. #: Information about host system
  729. self.host = QubesHost(self)
  730. if store is not None:
  731. self._store = store
  732. else:
  733. self._store = os.environ.get('QUBES_XML_PATH',
  734. os.path.join(
  735. qubes.config.qubes_base_dir,
  736. qubes.config.system_path[
  737. 'qubes_store_filename']))
  738. super().__init__(xml=None, **kwargs)
  739. self.__load_timestamp = None
  740. self.__locked_fh = None
  741. self._domain_event_callback_id = None
  742. #: jinja2 environment for libvirt XML templates
  743. self.env = jinja2.Environment(
  744. loader=jinja2.FileSystemLoader([
  745. '/etc/qubes/templates',
  746. '/usr/share/qubes/templates',
  747. ]),
  748. undefined=jinja2.StrictUndefined)
  749. if load:
  750. self.load(lock=lock)
  751. self.events_enabled = True
  752. @property
  753. def store(self):
  754. return self._store
  755. def _migrate_global_properties(self):
  756. """Migrate renamed/dropped properties"""
  757. if self.xml is None:
  758. return
  759. # drop default_fw_netvm
  760. node_default_fw_netvm = self.xml.find(
  761. './properties/property[@name=\'default_fw_netvm\']')
  762. if node_default_fw_netvm is not None:
  763. node_default_netvm = self.xml.find(
  764. './properties/property[@name=\'default_netvm\']')
  765. try:
  766. default_fw_netvm = self.domains[node_default_fw_netvm.text]
  767. if node_default_netvm is None:
  768. default_netvm = None
  769. else:
  770. default_netvm = self.domains[node_default_netvm.text]
  771. if default_netvm != default_fw_netvm:
  772. for vm in self.domains:
  773. if not hasattr(vm, 'netvm'):
  774. continue
  775. if not getattr(vm, 'provides_network', False):
  776. continue
  777. node_netvm = vm.xml.find(
  778. './properties/property[@name=\'netvm\']')
  779. if node_netvm is not None:
  780. # non-default netvm
  781. continue
  782. # this will unfortunately break "being default"
  783. # property state, but the alternative (changing
  784. # value behind user's back) is worse
  785. properties = vm.xml.find('./properties')
  786. element = lxml.etree.Element('property',
  787. name='netvm')
  788. element.text = default_fw_netvm.name
  789. # manipulate xml directly, before loading netvm
  790. # property, to avoid hitting netvm loop detection
  791. properties.append(element)
  792. except KeyError:
  793. # if default_fw_netvm was set to invalid value, simply
  794. # drop it
  795. pass
  796. node_default_fw_netvm.getparent().remove(node_default_fw_netvm)
  797. def _migrate_labels(self):
  798. """Migrate changed labels"""
  799. if self.xml is None:
  800. return
  801. # fix grey being green
  802. grey_label = self.xml.find('./labels/label[@color=\'0x555753\']')
  803. if grey_label is not None:
  804. grey_label.set('color', '0x555555')
  805. def load(self, lock=False):
  806. """Open qubes.xml
  807. :throws EnvironmentError: failure on parsing store
  808. :throws xml.parsers.expat.ExpatError: failure on parsing store
  809. :raises lxml.etree.XMLSyntaxError: on syntax error in qubes.xml
  810. """
  811. fh = self._acquire_lock()
  812. self.xml = lxml.etree.parse(fh)
  813. self._migrate_labels()
  814. # stage 1: load labels and pools
  815. for node in self.xml.xpath('./labels/label'):
  816. label = qubes.Label.fromxml(node)
  817. self.labels[label.index] = label
  818. for node in self.xml.xpath('./pools/pool'):
  819. name = node.get('name')
  820. assert name, "Pool name '%s' is invalid " % name
  821. try:
  822. self.pools[name] = self._get_pool(**node.attrib)
  823. except qubes.exc.QubesException as e:
  824. self.log.error(str(e))
  825. # stage 2: load VMs
  826. for node in self.xml.xpath('./domains/domain'):
  827. # pylint: disable=no-member
  828. cls = self.get_vm_class(node.get('class'))
  829. vm = cls(self, node)
  830. vm.load_properties(load_stage=2)
  831. vm.init_log()
  832. self.domains.add(vm, _enable_events=False)
  833. if 0 not in self.domains:
  834. self.domains.add(
  835. qubes.vm.adminvm.AdminVM(self, None),
  836. _enable_events=False)
  837. self._migrate_global_properties()
  838. # stage 3: load global properties
  839. self.load_properties(load_stage=3)
  840. # stage 4: fill all remaining VM properties
  841. for vm in self.domains:
  842. vm.load_properties(load_stage=4)
  843. vm.load_extras()
  844. # stage 5: misc fixups
  845. self.property_require('default_guivm', allow_none=True)
  846. self.property_require('default_netvm', allow_none=True)
  847. self.property_require('default_template', allow_none=True)
  848. self.property_require('clockvm', allow_none=True)
  849. self.property_require('updatevm', allow_none=True)
  850. for vm in self.domains:
  851. vm.events_enabled = True
  852. vm.fire_event('domain-load')
  853. # get a file timestamp (before closing it - still holding the lock!),
  854. # to detect whether anyone else have modified it in the meantime
  855. self.__load_timestamp = os.path.getmtime(self._store)
  856. if not lock:
  857. self._release_lock()
  858. def __xml__(self):
  859. element = lxml.etree.Element('qubes')
  860. element.append(self.xml_labels())
  861. pools_xml = lxml.etree.Element('pools')
  862. for pool in self.pools.values():
  863. xml = pool.__xml__()
  864. if xml is not None:
  865. pools_xml.append(xml)
  866. element.append(pools_xml)
  867. element.append(self.xml_properties())
  868. domains = lxml.etree.Element('domains')
  869. for vm in self.domains:
  870. domains.append(vm.__xml__())
  871. element.append(domains)
  872. return element
  873. def __str__(self):
  874. return type(self).__name__
  875. def save(self, lock=True):
  876. """Save all data to qubes.xml
  877. There are several problems with saving :file:`qubes.xml` which must be
  878. mitigated:
  879. - Running out of disk space. No space left should not result in empty
  880. file. This is done by writing to temporary file and then renaming.
  881. - Attempts to write two or more files concurrently. This is done by
  882. sophisticated locking.
  883. :param bool lock: keep file locked after saving
  884. :throws EnvironmentError: failure on saving
  885. """
  886. if not self.__locked_fh:
  887. self._acquire_lock(for_save=True)
  888. with qubes.utils.replace_file(self._store, permissions=0o660,
  889. close_on_success=False) as fh_new:
  890. lxml.etree.ElementTree(self.__xml__()).write(
  891. fh_new, encoding='utf-8', pretty_print=True)
  892. with suppress(KeyError): # group not found
  893. os.fchown(fh_new.fileno(), -1, grp.getgrnam('qubes').gr_gid)
  894. # update stored mtime, in case of multiple save() calls without
  895. # loading qubes.xml again
  896. self.__load_timestamp = os.path.getmtime(self._store)
  897. # this releases lock for all other processes,
  898. # but they should instantly block on the new descriptor
  899. self.__locked_fh.close()
  900. self.__locked_fh = fh_new
  901. if not lock:
  902. self._release_lock()
  903. def close(self):
  904. """Deconstruct the object and break circular references
  905. After calling this the object is unusable, not even for saving."""
  906. self.log.debug('close() <- %#x', id(self))
  907. for frame in traceback.extract_stack():
  908. self.log.debug('%s', frame)
  909. # let all the extension cleanup things
  910. self.fire_event('qubes-close')
  911. super().close()
  912. if self._domain_event_callback_id is not None:
  913. self.vmm.libvirt_conn.domainEventDeregisterAny(
  914. self._domain_event_callback_id)
  915. self._domain_event_callback_id = None
  916. # Only our Lord, The God Almighty, knows what references
  917. # are kept in extensions.
  918. # NOTE: this doesn't really delete extension objects - Extension class
  919. # saves reference to instance, and also various registered (class level)
  920. # event handlers do that too
  921. del self._extensions
  922. for vm in self.domains:
  923. vm.close()
  924. self.domains.close()
  925. del self.domains
  926. self.vmm.close()
  927. del self.vmm
  928. del self.host
  929. if self.__locked_fh:
  930. self._release_lock()
  931. def _acquire_lock(self, for_save=False):
  932. assert self.__locked_fh is None, 'double lock'
  933. while True:
  934. try:
  935. fd = os.open(self._store,
  936. os.O_RDWR | (os.O_CREAT * int(for_save)))
  937. except FileNotFoundError:
  938. if not for_save:
  939. raise qubes.exc.QubesException(
  940. 'Qubes XML store {!r} is missing; '
  941. 'use qubes-create tool'.format(self._store))
  942. raise
  943. # While we were waiting for lock, someone could have unlink()ed
  944. # (or rename()d) our file out of the filesystem. We have to
  945. # ensure we got lock on something linked to filesystem.
  946. # If not, try again.
  947. if os.fstat(fd) != os.stat(self._store):
  948. os.close(fd)
  949. continue
  950. if self.__load_timestamp and \
  951. os.path.getmtime(self._store) != self.__load_timestamp:
  952. os.close(fd)
  953. raise qubes.exc.QubesException(
  954. 'Someone else modified qubes.xml in the meantime')
  955. break
  956. if os.name == 'posix':
  957. fcntl.lockf(fd, fcntl.LOCK_EX)
  958. elif os.name == 'nt':
  959. # pylint: disable=protected-access
  960. overlapped = pywintypes.OVERLAPPED()
  961. win32file.LockFileEx(
  962. win32file._get_osfhandle(fd),
  963. win32con.LOCKFILE_EXCLUSIVE_LOCK, 0, -0x10000, overlapped)
  964. self.__locked_fh = os.fdopen(fd, 'r+b')
  965. return self.__locked_fh
  966. def _release_lock(self):
  967. assert self.__locked_fh is not None, 'double release'
  968. # intentionally do not call explicit unlock to not unlock the file
  969. # before all buffers are flushed
  970. self.__locked_fh.close()
  971. self.__locked_fh = None
  972. def load_initial_values(self):
  973. self.labels = {
  974. 1: qubes.Label(1, '0xcc0000', 'red'),
  975. 2: qubes.Label(2, '0xf57900', 'orange'),
  976. 3: qubes.Label(3, '0xedd400', 'yellow'),
  977. 4: qubes.Label(4, '0x73d216', 'green'),
  978. 5: qubes.Label(5, '0x555555', 'gray'),
  979. 6: qubes.Label(6, '0x3465a4', 'blue'),
  980. 7: qubes.Label(7, '0x75507b', 'purple'),
  981. 8: qubes.Label(8, '0x000000', 'black'),
  982. }
  983. assert max(self.labels.keys()) == qubes.config.max_default_label
  984. pool_configs = copy.deepcopy(qubes.config.defaults['pool_configs'])
  985. for name, config in pool_configs.items():
  986. if 'driver' not in config and 'dir_path' in config:
  987. config['driver'] = 'file'
  988. try:
  989. os.makedirs(config['dir_path'], exist_ok=True)
  990. if qubes.storage.reflink.is_supported(config['dir_path']):
  991. config['driver'] = 'file-reflink'
  992. config['setup_check'] = False # don't check twice
  993. except PermissionError: # looks like a testing environment
  994. pass # stay with 'file'
  995. self.pools[name] = self._get_pool(**config)
  996. self.default_pool_kernel = 'linux-kernel'
  997. self.domains.add(
  998. qubes.vm.adminvm.AdminVM(self, None, label='black'))
  999. @classmethod
  1000. def create_empty_store(cls, *args, **kwargs):
  1001. self = cls(*args, load=False, **kwargs)
  1002. if os.path.exists(self.store):
  1003. raise qubes.exc.QubesException(
  1004. '{} already exists, aborting'.format(self.store))
  1005. self.load_initial_values()
  1006. # TODO py3 get lock= as keyword-only arg
  1007. self.save(kwargs.get('lock'))
  1008. return self
  1009. def xml_labels(self):
  1010. """Serialise labels
  1011. :rtype: lxml.etree._Element
  1012. """
  1013. labels = lxml.etree.Element('labels')
  1014. for label in sorted(self.labels.values(), key=lambda labl: labl.index):
  1015. labels.append(label.__xml__())
  1016. return labels
  1017. @staticmethod
  1018. def get_vm_class(clsname):
  1019. """Find the class for a domain.
  1020. Classes are registered as setuptools' entry points in ``qubes.vm``
  1021. group. Any package may supply their own classes.
  1022. :param str clsname: name of the class
  1023. :return type: class
  1024. """
  1025. try:
  1026. return qubes.utils.get_entry_point_one(
  1027. qubes.vm.VM_ENTRY_POINT, clsname)
  1028. except KeyError:
  1029. raise qubes.exc.QubesException(
  1030. 'no such VM class: {!r}'.format(clsname))
  1031. # don't catch TypeError
  1032. def add_new_vm(self, cls, qid=None, **kwargs):
  1033. """Add new Virtual Machine to collection
  1034. """
  1035. if qid is None:
  1036. qid = self.domains.get_new_unused_qid()
  1037. if isinstance(cls, str):
  1038. cls = self.get_vm_class(cls)
  1039. # handle default template; specifically allow template=None (do not
  1040. # override it with default template)
  1041. if 'template' not in kwargs and hasattr(cls, 'template'):
  1042. if cls == self.get_vm_class('DispVM'):
  1043. kwargs['template'] = self.default_dispvm
  1044. else:
  1045. kwargs['template'] = self.default_template
  1046. if kwargs['template'] is None:
  1047. raise qubes.exc.QubesValueError(
  1048. 'Template for the qube not specified, nor default '
  1049. 'template set.')
  1050. elif 'template' in kwargs and isinstance(kwargs['template'], str):
  1051. kwargs['template'] = self.domains[kwargs['template']]
  1052. return self.domains.add(cls(self, None, qid=qid, **kwargs))
  1053. def get_label(self, label):
  1054. """Get label as identified by index or name
  1055. :throws KeyError: when label is not found
  1056. """
  1057. # first search for index, verbatim
  1058. with suppress(KeyError):
  1059. return self.labels[label]
  1060. # then search for name
  1061. for i in self.labels.values():
  1062. if i.name == label:
  1063. return i
  1064. # last call, if label is a number represented as str, search in indices
  1065. with suppress(KeyError, ValueError):
  1066. return self.labels[int(label)]
  1067. raise qubes.exc.QubesLabelNotFoundError(label)
  1068. @asyncio.coroutine
  1069. def setup_pools(self):
  1070. """ Run implementation specific setup for each storage pool. """
  1071. yield from qubes.utils.void_coros_maybe(
  1072. pool.setup() for pool in self.pools.values())
  1073. @asyncio.coroutine
  1074. def add_pool(self, name, **kwargs):
  1075. """ Add a storage pool to config."""
  1076. if name in self.pools.keys():
  1077. raise qubes.exc.QubesException('pool named %s already exists \n' %
  1078. name)
  1079. kwargs['name'] = name
  1080. pool = self._get_pool(**kwargs)
  1081. yield from qubes.utils.coro_maybe(pool.setup())
  1082. self.pools[name] = pool
  1083. yield from self.fire_event_async('pool-add', pool=pool)
  1084. return pool
  1085. @asyncio.coroutine
  1086. def remove_pool(self, name):
  1087. """ Remove a storage pool from config file. """
  1088. try:
  1089. pool = self.pools[name]
  1090. volumes = [(vm, volume) for vm in self.domains
  1091. for volume in vm.volumes.values()
  1092. if volume.pool is pool]
  1093. if volumes:
  1094. raise qubes.exc.QubesPoolInUseError(pool)
  1095. prop_suffixes = ['', '_kernel', '_private', '_root', '_volatile']
  1096. for suffix in prop_suffixes:
  1097. if getattr(self, 'default_pool' + suffix, None) is pool:
  1098. raise qubes.exc.QubesPoolInUseError(
  1099. pool,
  1100. 'Storage pool is in use: '
  1101. 'set as {}'.format('default_pool' + suffix))
  1102. yield from self.fire_event_async('pool-pre-delete',
  1103. pre_event=True, pool=pool)
  1104. del self.pools[name]
  1105. yield from qubes.utils.coro_maybe(pool.destroy())
  1106. yield from self.fire_event_async('pool-delete', pool=pool)
  1107. except KeyError:
  1108. return
  1109. def get_pool(self, pool):
  1110. """ Returns a :py:class:`qubes.storage.Pool` instance """
  1111. if isinstance(pool, qubes.storage.Pool):
  1112. return pool
  1113. try:
  1114. return self.pools[pool]
  1115. except KeyError:
  1116. raise qubes.exc.QubesException('Unknown storage pool ' + pool)
  1117. @staticmethod
  1118. def _get_pool(**kwargs):
  1119. try:
  1120. name = kwargs['name']
  1121. assert name, 'Name needs to be an non empty string'
  1122. except KeyError:
  1123. raise qubes.exc.QubesException('No pool name for pool')
  1124. try:
  1125. driver = kwargs['driver']
  1126. except KeyError:
  1127. raise qubes.exc.QubesException('No driver specified for pool ' +
  1128. name)
  1129. try:
  1130. klass = qubes.utils.get_entry_point_one(
  1131. qubes.storage.STORAGE_ENTRY_POINT, driver)
  1132. del kwargs['driver']
  1133. return klass(**kwargs)
  1134. except KeyError:
  1135. raise qubes.exc.QubesException('No driver %s for pool %s' %
  1136. (driver, name))
  1137. def register_event_handlers(self, old_connection=None):
  1138. """Register libvirt event handlers, which will translate libvirt
  1139. events into qubes.events. This function should be called only in
  1140. 'qubesd' process and only when mainloop has been already set.
  1141. """
  1142. if old_connection:
  1143. try:
  1144. old_connection.domainEventDeregisterAny(
  1145. self._domain_event_callback_id)
  1146. except libvirt.libvirtError:
  1147. # the connection is probably in a bad state; but call the above
  1148. # anyway to cleanup the client structures
  1149. pass
  1150. self._domain_event_callback_id = (
  1151. self.vmm.libvirt_conn.domainEventRegisterAny(
  1152. None, # any domain
  1153. libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE,
  1154. self._domain_event_callback,
  1155. None))
  1156. def _domain_event_callback(self, _conn, domain, event, _detail, _opaque):
  1157. """Generic libvirt event handler (virConnectDomainEventCallback),
  1158. translate libvirt event into qubes.events.
  1159. """
  1160. if not self.events_enabled:
  1161. return
  1162. try:
  1163. vm = self.domains[domain.name()]
  1164. except KeyError:
  1165. # ignore events for unknown domains
  1166. return
  1167. if event == libvirt.VIR_DOMAIN_EVENT_STOPPED:
  1168. vm.on_libvirt_domain_stopped()
  1169. elif event == libvirt.VIR_DOMAIN_EVENT_SUSPENDED:
  1170. try:
  1171. vm.fire_event('domain-paused')
  1172. except Exception: # pylint: disable=broad-except
  1173. self.log.exception(
  1174. 'Uncaught exception from domain-paused handler '
  1175. 'for domain %s', vm.name)
  1176. elif event == libvirt.VIR_DOMAIN_EVENT_RESUMED:
  1177. try:
  1178. vm.fire_event('domain-unpaused')
  1179. except Exception: # pylint: disable=broad-except
  1180. self.log.exception(
  1181. 'Uncaught exception from domain-unpaused handler '
  1182. 'for domain %s', vm.name)
  1183. @qubes.events.handler('domain-pre-delete')
  1184. def on_domain_pre_deleted(self, event, vm):
  1185. # pylint: disable=unused-argument
  1186. for obj in itertools.chain(self.domains, (self,)):
  1187. if obj is vm:
  1188. # allow removed VM to reference itself
  1189. continue
  1190. for prop in obj.property_list():
  1191. with suppress(AttributeError):
  1192. if isinstance(prop, qubes.vm.VMProperty) and \
  1193. getattr(obj, prop.__name__) == vm:
  1194. self.log.error(
  1195. 'Cannot remove %s, used by %s.%s',
  1196. vm, obj, prop.__name__)
  1197. raise qubes.exc.QubesVMInUseError(
  1198. vm,
  1199. 'Domain is in use: {!r};'
  1200. "see 'journalctl -u qubesd -e' in dom0 for "
  1201. 'details'.format(
  1202. vm.name))
  1203. assignments = vm.get_provided_assignments()
  1204. if assignments:
  1205. desc = ', '.join(
  1206. assignment.ident for assignment in assignments)
  1207. raise qubes.exc.QubesVMInUseError(
  1208. vm,
  1209. 'VM has devices attached persistently to other VMs: ' +
  1210. desc)
  1211. @qubes.events.handler('domain-delete')
  1212. def on_domain_deleted(self, event, vm):
  1213. # pylint: disable=unused-argument
  1214. for propname in (
  1215. 'default_guivm'
  1216. 'default_netvm',
  1217. 'default_fw_netvm',
  1218. 'clockvm',
  1219. 'updatevm',
  1220. 'default_template',
  1221. ):
  1222. with suppress(AttributeError):
  1223. if getattr(self, propname) == vm:
  1224. delattr(self, propname)
  1225. @qubes.events.handler('property-pre-set:clockvm')
  1226. def on_property_pre_set_clockvm(self, event, name, newvalue, oldvalue=None):
  1227. # pylint: disable=unused-argument,no-self-use
  1228. if newvalue is None:
  1229. return
  1230. if 'service.clocksync' not in newvalue.features:
  1231. newvalue.features['service.clocksync'] = True
  1232. @qubes.events.handler('property-set:clockvm')
  1233. def on_property_set_clockvm(self, event, name, newvalue, oldvalue=None):
  1234. # pylint: disable=unused-argument,no-self-use
  1235. if oldvalue == newvalue:
  1236. return
  1237. if oldvalue and oldvalue.features.get('service.clocksync', False):
  1238. del oldvalue.features['service.clocksync']
  1239. @qubes.events.handler('property-pre-set:default_netvm')
  1240. def on_property_pre_set_default_netvm(self, event, name, newvalue,
  1241. oldvalue=None):
  1242. # pylint: disable=unused-argument,invalid-name
  1243. if newvalue is not None and oldvalue is not None \
  1244. and oldvalue.is_running() and not newvalue.is_running() \
  1245. and self.domains.get_vms_connected_to(oldvalue):
  1246. raise qubes.exc.QubesVMNotRunningError(
  1247. newvalue,
  1248. 'Cannot change {!r} to domain that '
  1249. 'is not running ({!r}).'.format(
  1250. name, newvalue.name))
  1251. @qubes.events.handler('property-set:default_netvm')
  1252. def on_property_set_default_netvm(self, event, name, newvalue,
  1253. oldvalue=None):
  1254. # pylint: disable=unused-argument
  1255. for vm in self.domains:
  1256. if hasattr(vm, 'provides_network') and not vm.provides_network and \
  1257. hasattr(vm, 'netvm') and vm.property_is_default('netvm'):
  1258. # fire property-reset:netvm as it is responsible for resetting
  1259. # netvm to it's default value
  1260. vm.fire_event('property-reset:netvm',
  1261. name='netvm', oldvalue=oldvalue)
  1262. @qubes.events.handler('property-set:default_dispvm')
  1263. def on_property_set_default_dispvm(self, event, name, newvalue,
  1264. oldvalue=None):
  1265. # pylint: disable=unused-argument
  1266. for vm in self.domains:
  1267. if hasattr(vm, 'default_dispvm') and \
  1268. vm.property_is_default('default_dispvm'):
  1269. # fire property-reset:default_dispvm as it is responsible for
  1270. # resetting dispvm to it's default value
  1271. vm.fire_event('property-reset:default_dispvm',
  1272. name='default_dispvm', oldvalue=oldvalue)