app.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2011-2015 Marek Marczykowski-Górecki
  8. # <marmarek@invisiblethingslab.com>
  9. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License along
  22. # with this program; if not, write to the Free Software Foundation, Inc.,
  23. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. #
  25. import collections
  26. import errno
  27. import functools
  28. import grp
  29. import logging
  30. import os
  31. import random
  32. import sys
  33. import tempfile
  34. import time
  35. import uuid
  36. import lxml.etree
  37. import jinja2
  38. import libvirt
  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. import qubes # pylint: disable=wrong-import-position
  55. import qubes.ext # pylint: disable=wrong-import-position
  56. import qubes.utils # pylint: disable=wrong-import-position
  57. import qubes.vm.adminvm # pylint: disable=wrong-import-position
  58. import qubes.vm.qubesvm # pylint: disable=wrong-import-position
  59. import qubes.vm.templatevm # pylint: disable=wrong-import-position
  60. class VirDomainWrapper(object):
  61. # pylint: disable=too-few-public-methods
  62. def __init__(self, connection, vm):
  63. self._connection = connection
  64. self._vm = vm
  65. def _reconnect_if_dead(self):
  66. is_dead = not self._vm.connect().isAlive()
  67. if is_dead:
  68. # pylint: disable=protected-access
  69. self._connection._reconnect_if_dead()
  70. self._vm = self._connection._conn.lookupByUUID(self._vm.UUID())
  71. return is_dead
  72. def __getattr__(self, attrname):
  73. attr = getattr(self._vm, attrname)
  74. if not isinstance(attr, collections.Callable):
  75. return attr
  76. @functools.wraps(attr)
  77. def wrapper(*args, **kwargs):
  78. try:
  79. return attr(*args, **kwargs)
  80. except libvirt.libvirtError:
  81. if self._reconnect_if_dead():
  82. return getattr(self._vm, attrname)(*args, **kwargs)
  83. raise
  84. return wrapper
  85. class VirConnectWrapper(object):
  86. # pylint: disable=too-few-public-methods
  87. def __init__(self, uri):
  88. self._conn = libvirt.open(uri)
  89. def _reconnect_if_dead(self):
  90. is_dead = not self._conn.isAlive()
  91. if is_dead:
  92. self._conn = libvirt.open(self._conn.getURI())
  93. return is_dead
  94. def _wrap_domain(self, ret):
  95. if isinstance(ret, libvirt.virDomain):
  96. ret = VirDomainWrapper(self, ret)
  97. return ret
  98. def __getattr__(self, attrname):
  99. attr = getattr(self._conn, attrname)
  100. if not isinstance(attr, collections.Callable):
  101. return attr
  102. @functools.wraps(attr)
  103. def wrapper(*args, **kwargs):
  104. try:
  105. return self._wrap_domain(attr(*args, **kwargs))
  106. except libvirt.libvirtError:
  107. if self._reconnect_if_dead():
  108. return self._wrap_domain(
  109. getattr(self._conn, attrname)(*args, **kwargs))
  110. raise
  111. return wrapper
  112. class VMMConnection(object):
  113. '''Connection to Virtual Machine Manager (libvirt)'''
  114. def __init__(self, offline_mode=False):
  115. self._libvirt_conn = None
  116. self._xs = None
  117. self._xc = None
  118. self._offline_mode = offline_mode
  119. @property
  120. def offline_mode(self):
  121. '''Check or enable offline mode (do not actually connect to vmm)'''
  122. return self._offline_mode
  123. def _libvirt_error_handler(self, ctx, error):
  124. pass
  125. def init_vmm_connection(self):
  126. '''Initialise connection
  127. This method is automatically called when getting'''
  128. if self._libvirt_conn is not None:
  129. # Already initialized
  130. return
  131. if self._offline_mode:
  132. # Do not initialize in offline mode
  133. raise qubes.exc.QubesException(
  134. 'VMM operations disabled in offline mode')
  135. if 'xen.lowlevel.xs' in sys.modules:
  136. self._xs = xen.lowlevel.xs.xs()
  137. if 'xen.lowlevel.cs' in sys.modules:
  138. self._xc = xen.lowlevel.xc.xc()
  139. self._libvirt_conn = VirConnectWrapper(
  140. qubes.config.defaults['libvirt_uri'])
  141. libvirt.registerErrorHandler(self._libvirt_error_handler, None)
  142. @property
  143. def libvirt_conn(self):
  144. '''Connection to libvirt'''
  145. self.init_vmm_connection()
  146. return self._libvirt_conn
  147. @property
  148. def xs(self):
  149. '''Connection to Xen Store
  150. This property in available only when running on Xen.
  151. '''
  152. # XXX what about the case when we run under KVM,
  153. # but xen modules are importable?
  154. if 'xen.lowlevel.xs' not in sys.modules:
  155. raise AttributeError(
  156. 'xs object is available under Xen hypervisor only')
  157. self.init_vmm_connection()
  158. return self._xs
  159. @property
  160. def xc(self):
  161. '''Connection to Xen
  162. This property in available only when running on Xen.
  163. '''
  164. # XXX what about the case when we run under KVM,
  165. # but xen modules are importable?
  166. if 'xen.lowlevel.xc' not in sys.modules:
  167. raise AttributeError(
  168. 'xc object is available under Xen hypervisor only')
  169. self.init_vmm_connection()
  170. return self._xs
  171. def __del__(self):
  172. if self._libvirt_conn:
  173. self._libvirt_conn.close()
  174. class QubesHost(object):
  175. '''Basic information about host machine
  176. :param qubes.Qubes app: Qubes application context (must have \
  177. :py:attr:`Qubes.vmm` attribute defined)
  178. '''
  179. def __init__(self, app):
  180. self.app = app
  181. self._no_cpus = None
  182. self._total_mem = None
  183. self._physinfo = None
  184. def _fetch(self):
  185. if self._no_cpus is not None:
  186. return
  187. # pylint: disable=unused-variable
  188. (model, memory, cpus, mhz, nodes, socket, cores, threads) = \
  189. self.app.vmm.libvirt_conn.getInfo()
  190. self._total_mem = long(memory) * 1024
  191. self._no_cpus = cpus
  192. self.app.log.debug('QubesHost: no_cpus={} memory_total={}'.format(
  193. self.no_cpus, self.memory_total))
  194. try:
  195. self.app.log.debug('QubesHost: xen_free_memory={}'.format(
  196. self.get_free_xen_memory()))
  197. except NotImplementedError:
  198. pass
  199. @property
  200. def memory_total(self):
  201. '''Total memory, in kbytes'''
  202. if self.app.vmm.offline_mode:
  203. return 2**64-1
  204. self._fetch()
  205. return self._total_mem
  206. @property
  207. def no_cpus(self):
  208. '''Number of CPUs'''
  209. if self.app.vmm.offline_mode:
  210. return 42
  211. self._fetch()
  212. return self._no_cpus
  213. def get_free_xen_memory(self):
  214. '''Get free memory from Xen's physinfo.
  215. :raises NotImplementedError: when not under Xen
  216. '''
  217. try:
  218. self._physinfo = self.app.xc.physinfo()
  219. except AttributeError:
  220. raise NotImplementedError('This function requires Xen hypervisor')
  221. return long(self._physinfo['free_memory'])
  222. def measure_cpu_usage(self, previous_time=None, previous=None,
  223. wait_time=1):
  224. '''Measure cpu usage for all domains at once.
  225. This function requires Xen hypervisor.
  226. .. versionchanged:: 3.0
  227. argument order to match return tuple
  228. :raises NotImplementedError: when not under Xen
  229. '''
  230. if previous is None:
  231. previous_time = time.time()
  232. previous = {}
  233. try:
  234. info = self.app.vmm.xc.domain_getinfo(0, qubes.config.max_qid)
  235. except AttributeError:
  236. raise NotImplementedError(
  237. 'This function requires Xen hypervisor')
  238. for vm in info:
  239. previous[vm['domid']] = {}
  240. previous[vm['domid']]['cpu_time'] = (
  241. vm['cpu_time'] / max(vm['online_vcpus'], 1))
  242. previous[vm['domid']]['cpu_usage'] = 0
  243. time.sleep(wait_time)
  244. current_time = time.time()
  245. current = {}
  246. try:
  247. info = self.app.vmm.xc.domain_getinfo(0, qubes.config.max_qid)
  248. except AttributeError:
  249. raise NotImplementedError(
  250. 'This function requires Xen hypervisor')
  251. for vm in info:
  252. current[vm['domid']] = {}
  253. current[vm['domid']]['cpu_time'] = (
  254. vm['cpu_time'] / max(vm['online_vcpus'], 1))
  255. if vm['domid'] in previous.keys():
  256. current[vm['domid']]['cpu_usage'] = (
  257. float(current[vm['domid']]['cpu_time'] -
  258. previous[vm['domid']]['cpu_time']) /
  259. long(1000 ** 3) / (current_time - previous_time) * 100)
  260. if current[vm['domid']]['cpu_usage'] < 0:
  261. # VM has been rebooted
  262. current[vm['domid']]['cpu_usage'] = 0
  263. else:
  264. current[vm['domid']]['cpu_usage'] = 0
  265. return (current_time, current)
  266. class VMCollection(object):
  267. '''A collection of Qubes VMs
  268. VMCollection supports ``in`` operator. You may test for ``qid``, ``name``
  269. and whole VM object's presence.
  270. Iterating over VMCollection will yield machine objects.
  271. '''
  272. def __init__(self, app):
  273. self.app = app
  274. self._dict = dict()
  275. def __repr__(self):
  276. return '<{} {!r}>'.format(
  277. self.__class__.__name__, list(sorted(self.keys())))
  278. def items(self):
  279. '''Iterate over ``(qid, vm)`` pairs'''
  280. for qid in self.qids():
  281. yield (qid, self[qid])
  282. def qids(self):
  283. '''Iterate over all qids
  284. qids are sorted by numerical order.
  285. '''
  286. return iter(sorted(self._dict.keys()))
  287. keys = qids
  288. def names(self):
  289. '''Iterate over all names
  290. names are sorted by lexical order.
  291. '''
  292. return iter(sorted(vm.name for vm in self._dict.values()))
  293. def vms(self):
  294. '''Iterate over all machines
  295. vms are sorted by qid.
  296. '''
  297. return iter(sorted(self._dict.values()))
  298. __iter__ = vms
  299. values = vms
  300. def add(self, value, _enable_events=True):
  301. '''Add VM to collection
  302. :param qubes.vm.BaseVM value: VM to add
  303. :raises TypeError: when value is of wrong type
  304. :raises ValueError: when there is already VM which has equal ``qid``
  305. '''
  306. # this violates duck typing, but is needed
  307. # for VMProperty to function correctly
  308. if not isinstance(value, qubes.vm.BaseVM):
  309. raise TypeError('{} holds only BaseVM instances'.format(
  310. self.__class__.__name__))
  311. if value.qid in self:
  312. raise ValueError('This collection already holds VM that has '
  313. 'qid={!r} ({!r})'.format(value.qid, self[value.qid]))
  314. if value.name in self:
  315. raise ValueError('A VM named {!s} already exists'
  316. .format(value.name))
  317. self._dict[value.qid] = value
  318. if _enable_events:
  319. value.events_enabled = True
  320. self.app.fire_event('domain-add', value)
  321. return value
  322. def __getitem__(self, key):
  323. if isinstance(key, int):
  324. return self._dict[key]
  325. if isinstance(key, basestring):
  326. for vm in self:
  327. if vm.name == key:
  328. return vm
  329. raise KeyError(key)
  330. if isinstance(key, qubes.vm.BaseVM):
  331. key = key.uuid
  332. if isinstance(key, uuid.UUID):
  333. for vm in self:
  334. if vm.uuid == key:
  335. return vm
  336. raise KeyError(key)
  337. raise KeyError(key)
  338. def __delitem__(self, key):
  339. vm = self[key]
  340. if not vm.is_halted():
  341. raise qubes.exc.QubesVMNotHaltedError(vm)
  342. self.app.fire_event_pre('domain-pre-delete', vm)
  343. try:
  344. vm.libvirt_domain.undefine()
  345. except libvirt.libvirtError as e:
  346. if e.get_error_code() == libvirt.VIR_ERR_NO_DOMAIN:
  347. # already undefined
  348. pass
  349. del self._dict[vm.qid]
  350. self.app.fire_event('domain-delete', vm)
  351. def __contains__(self, key):
  352. return any((key == vm or key == vm.qid or key == vm.name)
  353. for vm in self)
  354. def __len__(self):
  355. return len(self._dict)
  356. def get_vms_based_on(self, template):
  357. template = self[template]
  358. return set(vm for vm in self
  359. if hasattr(vm, 'template') and vm.template == template)
  360. def get_vms_connected_to(self, netvm):
  361. new_vms = set([self[netvm]])
  362. dependent_vms = set()
  363. # Dependency resolving only makes sense on NetVM (or derivative)
  364. # if not self[netvm_qid].is_netvm():
  365. # return set([])
  366. while len(new_vms) > 0:
  367. cur_vm = new_vms.pop()
  368. for vm in cur_vm.connected_vms:
  369. if vm in dependent_vms:
  370. continue
  371. dependent_vms.add(vm.qid)
  372. # if vm.is_netvm():
  373. new_vms.add(vm.qid)
  374. return dependent_vms
  375. # XXX with Qubes Admin Api this will probably lead to race condition
  376. # whole process of creating and adding should be synchronised
  377. def get_new_unused_qid(self):
  378. used_ids = set(self.qids())
  379. for i in range(1, qubes.config.max_qid):
  380. if i not in used_ids:
  381. return i
  382. raise LookupError("Cannot find unused qid!")
  383. def get_new_unused_netid(self):
  384. used_ids = set([vm.netid for vm in self]) # if vm.is_netvm()])
  385. for i in range(1, qubes.config.max_netid):
  386. if i not in used_ids:
  387. return i
  388. raise LookupError("Cannot find unused netid!")
  389. def get_new_unused_dispid(self):
  390. for _ in range(int(qubes.config.max_dispid ** 0.5)):
  391. dispid = random.SystemRandom().randrange(qubes.config.max_dispid)
  392. if not any(getattr(vm, 'dispid', None) == dispid for vm in self):
  393. return dispid
  394. raise LookupError((
  395. 'https://xkcd.com/221/',
  396. 'http://dilbert.com/strip/2001-10-25')[random.randint(0, 1)])
  397. class Qubes(qubes.PropertyHolder):
  398. '''Main Qubes application
  399. :param str store: path to ``qubes.xml``
  400. The store is loaded in stages:
  401. 1. In the first stage there are loaded some basic features from store
  402. (currently labels).
  403. 2. In the second stage stubs for all VMs are loaded. They are filled
  404. with their basic properties, like ``qid`` and ``name``.
  405. 3. In the third stage all global properties are loaded. They often
  406. reference VMs, like default netvm, so they should be filled after
  407. loading VMs.
  408. 4. In the fourth stage all remaining VM properties are loaded. They
  409. also need all VMs loaded, because they represent dependencies
  410. between VMs like aforementioned netvm.
  411. 5. In the fifth stage there are some fixups to ensure sane system
  412. operation.
  413. This class emits following events:
  414. .. event:: domain-add (subject, event, vm)
  415. When domain is added.
  416. :param subject: Event emitter
  417. :param event: Event name (``'domain-add'``)
  418. :param vm: Domain object
  419. .. event:: domain-delete (subject, event, vm)
  420. When domain is deleted. VM still has reference to ``app`` object,
  421. but is not contained within VMCollection.
  422. :param subject: Event emitter
  423. :param event: Event name (``'domain-delete'``)
  424. :param vm: Domain object
  425. Methods and attributes:
  426. '''
  427. default_netvm = qubes.VMProperty('default_netvm', load_stage=3,
  428. default=None, allow_none=True,
  429. doc='''Default NetVM for AppVMs. Initial state is `None`, which means
  430. that AppVMs are not connected to the Internet.''')
  431. default_fw_netvm = qubes.VMProperty('default_fw_netvm', load_stage=3,
  432. default=None, allow_none=True,
  433. doc='''Default NetVM for ProxyVMs. Initial state is `None`, which means
  434. that ProxyVMs (including FirewallVM) are not connected to the
  435. Internet.''')
  436. default_template = qubes.VMProperty('default_template', load_stage=3,
  437. vmclass=qubes.vm.templatevm.TemplateVM,
  438. doc='Default template for new AppVMs')
  439. updatevm = qubes.VMProperty('updatevm', load_stage=3,
  440. allow_none=True,
  441. doc='''Which VM to use as `yum` proxy for updating AdminVM and
  442. TemplateVMs''')
  443. clockvm = qubes.VMProperty('clockvm', load_stage=3,
  444. allow_none=True,
  445. doc='Which VM to use as NTP proxy for updating AdminVM')
  446. default_kernel = qubes.property('default_kernel', load_stage=3,
  447. doc='Which kernel to use when not overriden in VM')
  448. default_dispvm = qubes.VMProperty('default_dispvm', load_stage=3,
  449. doc='Default DispVM base for service calls')
  450. # TODO #1637 #892
  451. check_updates_vm = qubes.property('check_updates_vm',
  452. type=bool, setter=qubes.property.bool,
  453. default=True,
  454. doc='check for updates inside qubes')
  455. def __init__(self, store=None, load=True, offline_mode=False, **kwargs):
  456. #: logger instance for logging global messages
  457. self.log = logging.getLogger('app')
  458. self._extensions = qubes.ext.get_extensions()
  459. #: collection of all VMs managed by this Qubes instance
  460. self.domains = VMCollection(self)
  461. #: collection of all available labels for VMs
  462. self.labels = {}
  463. #: collection of all pools
  464. self.pools = {}
  465. #: Connection to VMM
  466. self.vmm = VMMConnection(offline_mode=offline_mode)
  467. #: Information about host system
  468. self.host = QubesHost(self)
  469. if store is not None:
  470. self._store = store
  471. else:
  472. self._store = os.environ.get('QUBES_XML_PATH',
  473. os.path.join(
  474. qubes.config.system_path['qubes_base_dir'],
  475. qubes.config.system_path['qubes_store_filename']))
  476. super(Qubes, self).__init__(xml=None, **kwargs)
  477. self.__load_timestamp = None
  478. #: jinja2 environment for libvirt XML templates
  479. self.env = jinja2.Environment(
  480. loader=jinja2.FileSystemLoader('/usr/share/qubes/templates'),
  481. undefined=jinja2.StrictUndefined)
  482. if load:
  483. self.load()
  484. self.events_enabled = True
  485. @property
  486. def store(self):
  487. return self._store
  488. def load(self):
  489. '''Open qubes.xml
  490. :throws EnvironmentError: failure on parsing store
  491. :throws xml.parsers.expat.ExpatError: failure on parsing store
  492. :raises lxml.etree.XMLSyntaxError: on syntax error in qubes.xml
  493. '''
  494. try:
  495. fd = os.open(self._store, os.O_RDWR) # no O_CREAT
  496. except OSError as e:
  497. if e.errno != errno.ENOENT:
  498. raise
  499. raise qubes.exc.QubesException(
  500. 'Qubes XML store {!r} is missing; use qubes-create tool'.format(
  501. self._store))
  502. fh = os.fdopen(fd, 'rb')
  503. if os.name == 'posix':
  504. fcntl.lockf(fh, fcntl.LOCK_EX)
  505. elif os.name == 'nt':
  506. # pylint: disable=protected-access
  507. win32file.LockFileEx(
  508. win32file._get_osfhandle(fh.fileno()),
  509. win32con.LOCKFILE_EXCLUSIVE_LOCK,
  510. 0, -0x10000,
  511. pywintypes.OVERLAPPED())
  512. self.xml = lxml.etree.parse(fh)
  513. # stage 1: load labels and pools
  514. for node in self.xml.xpath('./labels/label'):
  515. label = qubes.Label.fromxml(node)
  516. self.labels[label.index] = label
  517. for node in self.xml.xpath('./pools/pool'):
  518. name = node.get('name')
  519. assert name, "Pool name '%s' is invalid " % name
  520. try:
  521. self.pools[name] = self._get_pool(**node.attrib)
  522. except qubes.exc.QubesException as e:
  523. self.log.error(e.message)
  524. # stage 2: load VMs
  525. for node in self.xml.xpath('./domains/domain'):
  526. # pylint: disable=no-member
  527. cls = self.get_vm_class(node.get('class'))
  528. vm = cls(self, node)
  529. vm.load_properties(load_stage=2)
  530. vm.init_log()
  531. self.domains.add(vm, _enable_events=False)
  532. if 0 not in self.domains:
  533. self.domains.add(
  534. qubes.vm.adminvm.AdminVM(self, None, qid=0, name='dom0'),
  535. _enable_events=False)
  536. # stage 3: load global properties
  537. self.load_properties(load_stage=3)
  538. # stage 4: fill all remaining VM properties
  539. for vm in self.domains:
  540. vm.load_properties(load_stage=4)
  541. vm.load_extras()
  542. # stage 5: misc fixups
  543. self.property_require('default_fw_netvm', allow_none=True)
  544. self.property_require('default_netvm', allow_none=True)
  545. self.property_require('default_template')
  546. self.property_require('clockvm', allow_none=True)
  547. self.property_require('updatevm', allow_none=True)
  548. # Disable ntpd in ClockVM - to not conflict with ntpdate (both are
  549. # using 123/udp port)
  550. if hasattr(self, 'clockvm') and self.clockvm is not None:
  551. if self.clockvm.features.get('services/ntpd', False):
  552. self.log.warning(
  553. 'VM set as clockvm (%r) has enabled \'ntpd\' service! '
  554. 'Expect failure when syncing time in dom0.',
  555. self.clockvm)
  556. else:
  557. self.clockvm.features['services/ntpd'] = ''
  558. for vm in self.domains:
  559. vm.events_enabled = True
  560. vm.fire_event('domain-load')
  561. # get a file timestamp (before closing it - still holding the lock!),
  562. # to detect whether anyone else have modified it in the meantime
  563. self.__load_timestamp = os.path.getmtime(self._store)
  564. # intentionally do not call explicit unlock
  565. fh.close()
  566. del fh
  567. def __xml__(self):
  568. element = lxml.etree.Element('qubes')
  569. element.append(self.xml_labels())
  570. pools_xml = lxml.etree.Element('pools')
  571. for pool in self.pools.values():
  572. xml = pool.__xml__()
  573. if xml is not None:
  574. pools_xml.append(xml)
  575. element.append(pools_xml)
  576. element.append(self.xml_properties())
  577. domains = lxml.etree.Element('domains')
  578. for vm in self.domains:
  579. domains.append(vm.__xml__())
  580. element.append(domains)
  581. return element
  582. def save(self):
  583. '''Save all data to qubes.xml
  584. There are several problems with saving :file:`qubes.xml` which must be
  585. mitigated:
  586. - Running out of disk space. No space left should not result in empty
  587. file. This is done by writing to temporary file and then renaming.
  588. - Attempts to write two or more files concurrently. This is done by
  589. sophisticated locking.
  590. :throws EnvironmentError: failure on saving
  591. '''
  592. while True:
  593. fd_old = os.open(self._store, os.O_RDWR | os.O_CREAT)
  594. if os.name == 'posix':
  595. fcntl.lockf(fd_old, fcntl.LOCK_EX)
  596. elif os.name == 'nt':
  597. # pylint: disable=protected-access
  598. overlapped = pywintypes.OVERLAPPED()
  599. win32file.LockFileEx(
  600. win32file._get_osfhandle(fd_old),
  601. win32con.LOCKFILE_EXCLUSIVE_LOCK, 0, -0x10000, overlapped)
  602. # While we were waiting for lock, someone could have unlink()ed (or
  603. # rename()d) our file out of the filesystem. We have to ensure we
  604. # got lock on something linked to filesystem. If not, try again.
  605. if os.fstat(fd_old) == os.stat(self._store):
  606. break
  607. else:
  608. os.close(fd_old)
  609. if self.__load_timestamp:
  610. current_file_timestamp = os.path.getmtime(self._store)
  611. if current_file_timestamp != self.__load_timestamp:
  612. os.close(fd_old)
  613. raise qubes.exc.QubesException(
  614. "Someone else modified qubes.xml in the meantime")
  615. fh_new = tempfile.NamedTemporaryFile(prefix=self._store, delete=False)
  616. lxml.etree.ElementTree(self.__xml__()).write(
  617. fh_new, encoding='utf-8', pretty_print=True)
  618. fh_new.flush()
  619. os.chmod(fh_new.name, 0660)
  620. os.chown(fh_new.name, -1, grp.getgrnam('qubes').gr_gid)
  621. os.rename(fh_new.name, self._store)
  622. # intentionally do not call explicit unlock to not unlock the file
  623. # before all buffers are flushed
  624. fh_new.close()
  625. # update stored mtime, in case of multiple save() calls without
  626. # loading qubes.xml again
  627. self.__load_timestamp = os.path.getmtime(self._store)
  628. os.close(fd_old)
  629. def load_initial_values(self):
  630. self.labels = {
  631. 1: qubes.Label(1, '0xcc0000', 'red'),
  632. 2: qubes.Label(2, '0xf57900', 'orange'),
  633. 3: qubes.Label(3, '0xedd400', 'yellow'),
  634. 4: qubes.Label(4, '0x73d216', 'green'),
  635. 5: qubes.Label(5, '0x555753', 'gray'),
  636. 6: qubes.Label(6, '0x3465a4', 'blue'),
  637. 7: qubes.Label(7, '0x75507b', 'purple'),
  638. 8: qubes.Label(8, '0x000000', 'black'),
  639. }
  640. for name, config in qubes.config.defaults['pool_configs'].items():
  641. self.pools[name] = self._get_pool(**config)
  642. # check if the default LVM Thin pool qubes_dom0/pool00 exists
  643. if os.path.exists('/dev/mapper/qubes_dom0-pool00-tpool'):
  644. self.add_pool(volume_group='qubes_dom0', thin_pool='pool00',
  645. name='lvm', driver='lvm_thin')
  646. self.domains.add(
  647. qubes.vm.adminvm.AdminVM(self, None, qid=0, name='dom0'))
  648. @classmethod
  649. def create_empty_store(cls, *args, **kwargs):
  650. self = cls(*args, load=False, **kwargs)
  651. self.load_initial_values()
  652. self.save()
  653. return self
  654. def xml_labels(self):
  655. '''Serialise labels
  656. :rtype: lxml.etree._Element
  657. '''
  658. labels = lxml.etree.Element('labels')
  659. for label in sorted(self.labels.values(), key=lambda labl: labl.index):
  660. labels.append(label.__xml__())
  661. return labels
  662. @staticmethod
  663. def get_vm_class(clsname):
  664. '''Find the class for a domain.
  665. Classess are registered as setuptools' entry points in ``qubes.vm``
  666. group. Any package may supply their own classess.
  667. :param str clsname: name of the class
  668. :return type: class
  669. '''
  670. try:
  671. return qubes.utils.get_entry_point_one('qubes.vm', clsname)
  672. except KeyError:
  673. raise qubes.exc.QubesException(
  674. 'no such VM class: {!r}'.format(clsname))
  675. # don't catch TypeError
  676. def add_new_vm(self, cls, qid=None, **kwargs):
  677. '''Add new Virtual Machine to colletion
  678. '''
  679. if qid is None:
  680. qid = self.domains.get_new_unused_qid()
  681. # handle default template; specifically allow template=None (do not
  682. # override it with default template)
  683. if 'template' not in kwargs and hasattr(cls, 'template'):
  684. kwargs['template'] = self.default_template
  685. elif 'template' in kwargs and isinstance(kwargs['template'], str):
  686. kwargs['template'] = self.domains[kwargs['template']]
  687. return self.domains.add(cls(self, None, qid=qid, **kwargs))
  688. def get_label(self, label):
  689. '''Get label as identified by index or name
  690. :throws KeyError: when label is not found
  691. '''
  692. # first search for index, verbatim
  693. try:
  694. return self.labels[label]
  695. except KeyError:
  696. pass
  697. # then search for name
  698. for i in self.labels.values():
  699. if i.name == label:
  700. return i
  701. # last call, if label is a number represented as str, search in indices
  702. try:
  703. return self.labels[int(label)]
  704. except (KeyError, ValueError):
  705. pass
  706. raise KeyError(label)
  707. def add_pool(self, name, **kwargs):
  708. """ Add a storage pool to config."""
  709. if name in self.pools.keys():
  710. raise qubes.exc.QubesException('pool named %s already exists \n' %
  711. name)
  712. kwargs['name'] = name
  713. pool = self._get_pool(**kwargs)
  714. pool.setup()
  715. self.pools[name] = pool
  716. return pool
  717. def remove_pool(self, name):
  718. """ Remove a storage pool from config file. """
  719. try:
  720. pool = self.pools[name]
  721. del self.pools[name]
  722. pool.destroy()
  723. except KeyError:
  724. return
  725. def get_pool(self, name):
  726. ''' Returns a :py:class:`qubes.storage.Pool` instance '''
  727. try:
  728. return self.pools[name]
  729. except KeyError:
  730. raise qubes.exc.QubesException('Unknown storage pool ' + name)
  731. @staticmethod
  732. def _get_pool(**kwargs):
  733. try:
  734. name = kwargs['name']
  735. assert name, 'Name needs to be an non empty string'
  736. except KeyError:
  737. raise qubes.exc.QubesException('No pool name for pool')
  738. try:
  739. driver = kwargs['driver']
  740. except KeyError:
  741. raise qubes.exc.QubesException('No driver specified for pool ' +
  742. name)
  743. try:
  744. klass = qubes.utils.get_entry_point_one(
  745. qubes.storage.STORAGE_ENTRY_POINT, driver)
  746. del kwargs['driver']
  747. return klass(**kwargs)
  748. except KeyError:
  749. raise qubes.exc.QubesException('No driver %s for pool %s' %
  750. (driver, name))
  751. @qubes.events.handler('domain-pre-delete')
  752. def on_domain_pre_deleted(self, event, vm):
  753. # pylint: disable=unused-argument
  754. if isinstance(vm, qubes.vm.templatevm.TemplateVM):
  755. appvms = self.domains.get_vms_based_on(vm)
  756. if appvms:
  757. raise qubes.exc.QubesException(
  758. 'Cannot remove template that has dependent AppVMs. '
  759. 'Affected are: {}'.format(', '.join(
  760. vm.name for name in sorted(appvms))))
  761. @qubes.events.handler('domain-delete')
  762. def on_domain_deleted(self, event, vm):
  763. # pylint: disable=unused-argument
  764. for propname in (
  765. 'default_netvm',
  766. 'default_fw_netvm',
  767. 'clockvm',
  768. 'updatevm',
  769. 'default_template',
  770. ):
  771. try:
  772. if getattr(self, propname) == vm:
  773. delattr(self, propname)
  774. except AttributeError:
  775. pass
  776. @qubes.events.handler('property-pre-set:clockvm')
  777. def on_property_pre_set_clockvm(self, event, name, newvalue, oldvalue=None):
  778. # pylint: disable=unused-argument,no-self-use
  779. if newvalue is None:
  780. return
  781. if newvalue.features.get('services/ntpd', False):
  782. raise qubes.exc.QubesVMError(newvalue,
  783. 'Cannot set {!r} as {!r} since it has ntpd enabled.'.format(
  784. newvalue.name, name))
  785. else:
  786. newvalue.features['services/ntpd'] = ''
  787. @qubes.events.handler(
  788. 'property-pre-set:default_netvm',
  789. 'property-pre-set:default_fw_netvm')
  790. def on_property_pre_set_default_netvm(self, event, name, newvalue,
  791. oldvalue=None):
  792. # pylint: disable=unused-argument,invalid-name
  793. if newvalue is not None and oldvalue is not None \
  794. and oldvalue.is_running() and not newvalue.is_running() \
  795. and self.domains.get_vms_connected_to(oldvalue):
  796. raise qubes.exc.QubesVMNotRunningError(newvalue,
  797. 'Cannot change {!r} to domain that '
  798. 'is not running ({!r}).'.format(name, newvalue.name))
  799. @qubes.events.handler('property-set:default_fw_netvm')
  800. def on_property_set_default_fw_netvm(self, event, name, newvalue,
  801. oldvalue=None):
  802. # pylint: disable=unused-argument,invalid-name
  803. for vm in self.domains:
  804. if not vm.provides_network and vm.property_is_default('netvm'):
  805. # fire property-del:netvm as it is responsible for resetting
  806. # netvm to it's default value
  807. vm.fire_event('property-del:netvm', 'netvm', newvalue, oldvalue)
  808. @qubes.events.handler('property-set:default_netvm')
  809. def on_property_set_default_netvm(self, event, name, newvalue,
  810. oldvalue=None):
  811. # pylint: disable=unused-argument
  812. for vm in self.domains:
  813. if vm.provides_network and vm.property_is_default('netvm'):
  814. # fire property-del:netvm as it is responsible for resetting
  815. # netvm to it's default value
  816. vm.fire_event('property-del:netvm', 'netvm', oldvalue)