app.py 46 KB

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