qubesvm.py 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. #!/usr/bin/python2 -O
  2. # -*- coding: utf-8 -*-
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. # Copyright (C) 2013 Marek Marczykowski <marmarek@invisiblethingslab.com>
  7. # Copyright (C) 2014 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 2
  12. # of the License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. #
  23. import datetime
  24. import lxml.etree
  25. import os
  26. import os.path
  27. import re
  28. import shutil
  29. import subprocess
  30. import sys
  31. import time
  32. import uuid
  33. import xml.parsers.expat
  34. import libvirt
  35. import warnings
  36. import qubes
  37. import qubes.config
  38. #import qubes.qdb
  39. #import qubes.qmemman
  40. #import qubes.qmemman_algo
  41. import qubes.utils
  42. import qubes.vm
  43. qmemman_present = False
  44. try:
  45. import qubes.qmemman_client
  46. qmemman_present = True
  47. except ImportError:
  48. pass
  49. def _setter_qid(self, prop, value):
  50. if not 0 <= value <= qubes.MAX_QID:
  51. raise ValueError(
  52. '{} value must be between 0 and qubes.MAX_QID'.format(
  53. prop.__name__))
  54. return value
  55. def _setter_name(self, prop, value):
  56. if not isinstance(value, basestring):
  57. raise TypeError('{} value must be string, {!r} found'.format(
  58. prop.__name__, type(value).__name__))
  59. if len(value) > 31:
  60. raise ValueError('{} value must be shorter than 32 characters'.format(
  61. prop.__name__))
  62. if re.match(r"^[a-zA-Z][a-zA-Z0-9_-]*$", value) is None:
  63. raise ValueError('{} value contains illegal characters'.format(
  64. prop.__name__))
  65. if self.is_running():
  66. raise qubes.QubesException('Cannot change name of running VM')
  67. try:
  68. if self.installed_by_rpm:
  69. raise qubes.QubesException('Cannot rename VM installed by RPM -- '
  70. 'first clone VM and then use yum to remove package.')
  71. except AttributeError:
  72. pass
  73. return value
  74. def _setter_kernel(self, prop, value):
  75. if not os.path.exists(os.path.join(system_path[
  76. 'qubes_kernels_base_dir'], value)):
  77. raise qubes.QubesException('Kernel {!r} not installed'.format(value))
  78. for f in ('vmlinuz', 'modules.img'):
  79. if not os.path.exists(os.path.join(
  80. system_path['qubes_kernels_base_dir'], value, f)):
  81. raise qubes.QubesException(
  82. 'Kernel {!r} not properly installed: missing {!r} file'.format(
  83. value, f))
  84. return value
  85. class QubesVM(qubes.vm.BaseVM):
  86. '''Base functionality of Qubes VM shared between all VMs.'''
  87. #
  88. # properties loaded from XML
  89. #
  90. label = qubes.property('label',
  91. setter=(lambda self, prop, value: self.app.labels[
  92. int(value.rsplit('-', 1)[1])]),
  93. doc='Colourful label assigned to VM. This is where you set the colour '
  94. 'of the padlock.')
  95. # XXX swallowed uses_default_netvm
  96. netvm = qubes.VMProperty('netvm', load_stage=4, allow_none=True,
  97. default=(lambda self: self.app.default_fw_netvm if self.provides_network
  98. else self.app.default_netvm),
  99. doc='''VM that provides network connection to this domain. When
  100. `None`, machine is disconnected. When absent, domain uses default
  101. NetVM.''')
  102. provides_network = qubes.property('provides_network', type=bool,
  103. doc='`True` if it is NetVM or ProxyVM, false otherwise.')
  104. qid = qubes.property('qid', type=int,
  105. setter=_setter_qid,
  106. doc='''Internal, persistent identificator of particular domain. Note
  107. this is different from Xen domid.''')
  108. name = qubes.property('name', type=str,
  109. doc='User-specified name of the domain.')
  110. uuid = qubes.property('uuid', type=uuid.UUID, default=None,
  111. doc='UUID from libvirt.')
  112. dir_path = qubes.property('dir_path', type=str, default=None,
  113. doc='FIXME')
  114. conf_file = qubes.property('conf_file', type=str,
  115. default=(lambda self: self.name + '.conf'),
  116. saver=(lambda self, prop, value: self.relative_path(value)),
  117. doc='libvirt config file?')
  118. # XXX this should be part of qubes.xml
  119. firewall_conf = qubes.property('firewall_conf', type=str,
  120. default='firewall.xml')
  121. installed_by_rpm = qubes.property('installed_by_rpm', type=bool, default=False,
  122. setter=qubes.property.bool,
  123. doc='''If this domain's image was installed from package tracked by
  124. package manager.''')
  125. memory = qubes.property('memory', type=int, default=qubes.config.defaults['memory'],
  126. doc='Memory currently available for this VM.')
  127. maxmem = qubes.property('maxmem', type=int, default=None,
  128. doc='''Maximum amount of memory available for this VM (for the purpose
  129. of the memory balancer).''')
  130. internal = qubes.property('internal', type=bool, default=False,
  131. setter=qubes.property.bool,
  132. doc='''Internal VM (not shown in qubes-manager, don't create appmenus
  133. entries.''')
  134. # XXX what is that
  135. vcpus = qubes.property('vcpus', default=None,
  136. doc='FIXME')
  137. # XXX swallowed uses_default_kernel
  138. # XXX not applicable to HVM?
  139. kernel = qubes.property('kernel', type=str,
  140. setter=_setter_kernel,
  141. default=(lambda self: self.app.default_kernel),
  142. doc='Kernel used by this domain.')
  143. # XXX swallowed uses_default_kernelopts
  144. # XXX not applicable to HVM?
  145. kernelopts = qubes.property('kernelopts', type=str, load_stage=4,
  146. default=(lambda self: defaults['kernelopts_pcidevs'] \
  147. if len(self.devices['pci']) > 0 else defaults['kernelopts']),
  148. doc='Kernel command line passed to domain.')
  149. mac = qubes.property('mac', type=str,
  150. default=(lambda self: '00:16:3E:5E:6C:{:02X}'.format(self.qid)),
  151. doc='MAC address of the NIC emulated inside VM')
  152. debug = qubes.property('debug', type=bool, default=False,
  153. setter=qubes.property.bool,
  154. doc='Turns on debugging features.')
  155. # XXX what this exactly does?
  156. # XXX shouldn't this go to standalone VM and TemplateVM, and leave here
  157. # only plain property?
  158. default_user = qubes.property('default_user', type=str,
  159. default=(lambda self: self.template.default_user),
  160. doc='FIXME')
  161. # @property
  162. # def default_user(self):
  163. # if self.template is not None:
  164. # return self.template.default_user
  165. # else:
  166. # return self._default_user
  167. qrexec_timeout = qubes.property('qrexec_timeout', type=int, default=60,
  168. doc='''Time in seconds after which qrexec connection attempt is deemed
  169. failed. Operating system inside VM should be able to boot in this
  170. time.''')
  171. autostart = qubes.property('autostart', type=bool, default=False,
  172. setter=qubes.property.bool,
  173. doc='''Setting this to `True` means that VM should be autostarted on dom0
  174. boot.''')
  175. # XXX I don't understand backups
  176. include_in_backups = qubes.property('include_in_backups', type=bool, default=True,
  177. setter=qubes.property.bool,
  178. doc='If this domain is to be included in default backup.')
  179. backup_content = qubes.property('backup_content', type=bool, default=False,
  180. setter=qubes.property.bool,
  181. doc='FIXME')
  182. backup_size = qubes.property('backup_size', type=int, default=0,
  183. doc='FIXME')
  184. backup_path = qubes.property('backup_path', type=str, default='',
  185. doc='FIXME')
  186. # format got changed from %s to str(datetime.datetime)
  187. backup_timestamp = qubes.property('backup_timestamp', default=None,
  188. setter=(lambda self, prop, value: datetime.datetime.fromtimestamp(value)),
  189. saver=(lambda self, prop, value: value.strftime('%s')),
  190. doc='FIXME')
  191. #
  192. # static, class-wide properties
  193. #
  194. # config file should go away to storage/backend class
  195. #: template for libvirt config file (XML)
  196. config_file_template = qubes.config.system_path["config_template_pv"]
  197. #
  198. # properties not loaded from XML, calculated at run-time
  199. #
  200. # VMM-related
  201. @property
  202. def xid(self):
  203. '''Xen ID.
  204. Or not Xen, but ID.
  205. '''
  206. if self.libvirt_domain is None:
  207. return -1
  208. return self.libvirt_domain.ID()
  209. @property
  210. def libvirt_domain(self):
  211. '''Libvirt domain object from libvirt.
  212. May be :py:obj:`None`, if libvirt knows nothing about this domain.
  213. '''
  214. if self._libvirt_domain is not None:
  215. return self._libvirt_domain
  216. try:
  217. if self.uuid is not None:
  218. self._libvirt_domain = vmm.libvirt_conn.lookupByUUID(self.uuid.bytes)
  219. else:
  220. self._libvirt_domain = vmm.libvirt_conn.lookupByName(self.name)
  221. self.uuid = uuid.UUID(bytes=self._libvirt_domain.UUID())
  222. except libvirt.libvirtError:
  223. if vmm.libvirt_conn.virConnGetLastError()[0] == libvirt.VIR_ERR_NO_DOMAIN:
  224. self._update_libvirt_domain()
  225. else:
  226. raise
  227. return self._libvirt_domain
  228. @property
  229. def qdb(self):
  230. '''QubesDB handle for this domain.'''
  231. if self._qdb_connection is None:
  232. if self.is_running():
  233. self._qdb_connection = qubes.qdb.QubesDB(self.name)
  234. return self._qdb_connection
  235. # XXX this should go to to AppVM?
  236. @property
  237. def private_img(self):
  238. '''Location of private image of the VM (that contains :file:`/rw` and :file:`/home`).'''
  239. return self.storage.private_img
  240. # XXX this should go to to AppVM? or TemplateVM?
  241. @property
  242. def root_img(self):
  243. '''Location of root image.'''
  244. return self.storage.root_img
  245. # XXX and this should go to exactly where? DispVM has it.
  246. @property
  247. def volatile_img(self):
  248. '''Volatile image that overlays :py:attr:`root_img`.'''
  249. return self.storage.volatile_img
  250. # XXX shouldn't this go elsewhere?
  251. @property
  252. def updateable(self):
  253. '''True if this machine may be updated on its own.'''
  254. return hasattr(self, 'template')
  255. @property
  256. def uses_custom_config(self):
  257. '''True if this machine has config in non-standard place.'''
  258. return self.conf_file != self.absolute_path(self.name + ".conf", None)
  259. @property
  260. def icon_path(self):
  261. return self.dir_path and os.path.join(self.dir_path, "icon.png")
  262. # XXX I don't know what to do with these; probably should be isinstance(...)
  263. # def is_template(self):
  264. # return False
  265. #
  266. # def is_appvm(self):
  267. # return False
  268. #
  269. # def is_proxyvm(self):
  270. # return False
  271. #
  272. # def is_disposablevm(self):
  273. # return False
  274. # network-related
  275. @property
  276. def ip(self):
  277. '''IP address of this domain.'''
  278. if self.netvm is not None:
  279. return self.netvm.get_ip_for_vm(self.qid)
  280. else:
  281. return None
  282. @property
  283. def netmask(self):
  284. '''Netmask for this domain's IP address.'''
  285. if self.netvm is not None:
  286. return self.netvm.netmask
  287. else:
  288. return None
  289. @property
  290. def gateway(self):
  291. '''Gateway for other domains that use this domain as netvm.'''
  292. # This is gateway IP for _other_ VMs, so make sense only in NetVMs
  293. return None
  294. @property
  295. def secondary_dns(self):
  296. '''Secondary DNS server set up for this domain.'''
  297. if self.netvm is not None:
  298. return self.netvm.secondary_dns
  299. else:
  300. return None
  301. @property
  302. def vif(self):
  303. '''Name of the network interface backend in netvm that is connected to
  304. NIC inside this domain.'''
  305. if self.xid < 0:
  306. return None
  307. if self.netvm is None:
  308. return None
  309. return "vif{0}.+".format(self.xid)
  310. #
  311. # constructor
  312. #
  313. def __init__(self, app, xml):
  314. super(QubesVM, self).__init__(app, xml)
  315. #Init private attrs
  316. self._libvirt_domain = None
  317. self._qdb_connection = None
  318. assert self.__qid < qubes_max_qid, "VM id out of bounds!"
  319. assert self.name is not None
  320. if not self.verify_name(self.name):
  321. msg = ("'%s' is invalid VM name (invalid characters, over 31 chars long, "
  322. "or one of 'none', 'true', 'false')") % self.name
  323. if xml is not None:
  324. print >>sys.stderr, "WARNING: %s" % msg
  325. else:
  326. raise QubesException(msg)
  327. # Not in generic way to not create QubesHost() to frequently
  328. # XXX this doesn't apply, host is instantiated once
  329. if self.maxmem is None and not self.app.vmm.offline_mode:
  330. total_mem_mb = self.app.host.memory_total/1024
  331. self.maxmem = total_mem_mb/2
  332. # Linux specific cap: max memory can't scale beyond 10.79*init_mem
  333. # XXX what?! -woju
  334. if self.maxmem > self.memory * 10:
  335. self.maxmem = self.memory * 10
  336. # By default allow use all VCPUs
  337. if not hasattr(self, 'vcpus') and not self.app.vmm.offline_mode:
  338. self.vcpus = self.app.host.no_cpus
  339. # Always set if meminfo-writer should be active or not
  340. if 'meminfo-writer' not in self.services:
  341. self.services['meminfo-writer'] = not (len(self.devices['pci']) > 0)
  342. # Additionally force meminfo-writer disabled when VM have PCI devices
  343. if len(self.devices['pci']) > 0:
  344. self.services['meminfo-writer'] = False
  345. # Initialize VM image storage class
  346. self.storage = qubes.config.defaults["storage_class"](self)
  347. if hasattr(self, 'kernels_dir'):
  348. self.storage.modules_img = os.path.join(self.kernels_dir,
  349. "modules.img")
  350. self.storage.modules_img_rw = self.kernel is None
  351. # fire hooks
  352. self.fire_event('domain-init')
  353. #
  354. # event handlers
  355. #
  356. @qubes.events.handler('property-set:label')
  357. def on_property_set_label(self, event, name, new_label, old_label=None):
  358. if self.icon_path:
  359. try:
  360. os.remove(self.icon_path)
  361. except:
  362. pass
  363. if hasattr(os, "symlink"):
  364. os.symlink(new_label.icon_path, self.icon_path)
  365. # FIXME: some os-independent wrapper?
  366. subprocess.call(['sudo', 'xdg-icon-resource', 'forceupdate'])
  367. else:
  368. shutil.copy(new_label.icon_path, self.icon_path)
  369. @qubes.events.handler('property-del:netvm')
  370. def on_property_del_netvm(self, event, name, old_netvm):
  371. # we are changing to default netvm
  372. new_netvm = self.netvm
  373. if new_netvm == old_netvm: return
  374. self.fire_event('property-set:netvm', 'netvm', new_netvm, old_netvm)
  375. @qubes.events.handler('property-set:netvm')
  376. def on_property_set_netvm(self, event, name, new_netvm, old_netvm=None):
  377. if self.is_running() and new_netvm is not None and not new_netvm.is_running():
  378. raise QubesException("Cannot dynamically attach to stopped NetVM")
  379. if self.netvm is not None:
  380. del self.netvm.connected_vms[self]
  381. if self.is_running():
  382. self.detach_network()
  383. # TODO change to domain-removed event handler in netvm
  384. # if hasattr(self.netvm, 'post_vm_net_detach'):
  385. # self.netvm.post_vm_net_detach(self)
  386. if new_netvm is None:
  387. if not self._do_not_reset_firewall:
  388. # Set also firewall to block all traffic as discussed in #370
  389. if os.path.exists(self.firewall_conf):
  390. shutil.copy(self.firewall_conf, os.path.join(system_path["qubes_base_dir"],
  391. "backup", "%s-firewall-%s.xml" % (self.name,
  392. time.strftime('%Y-%m-%d-%H:%M:%S'))))
  393. self.write_firewall_conf({'allow': False, 'allowDns': False,
  394. 'allowIcmp': False, 'allowYumProxy': False, 'rules': []})
  395. else:
  396. new_netvm.connected_vms.add(self)
  397. if new_netvm is None:
  398. return
  399. if self.is_running():
  400. # refresh IP, DNS etc
  401. self.create_qdb_entries()
  402. self.attach_network()
  403. # TODO domain-added event handler in netvm
  404. # if hasattr(self.netvm, 'post_vm_net_attach'):
  405. # self.netvm.post_vm_net_attach(self)
  406. @qubes.events.handler('property-set:name')
  407. def on_property_set_name(self, event, name, new_name, old_name=None):
  408. # TODO not self.is_stopped() would be more appropriate
  409. if self.is_running():
  410. raise QubesException('Cannot change name of running domain')
  411. if self.libvirt_domain:
  412. self.libvirt_domain.undefine()
  413. self._libvirt_domain = None
  414. if self._qdb_connection:
  415. self._qdb_connection.close()
  416. self._qdb_connection = None
  417. new_conf = os.path.join(self.dir_path, new_name + '.conf')
  418. if os.path.exists(self.conf_file):
  419. os.rename(self.conf_file, new_conf)
  420. old_dirpath = self.dir_path
  421. self.storage.rename(self.name, name)
  422. new_dirpath = self.storage.vmdir
  423. self.dir_path = new_dirpath
  424. if self.conf_file is not None:
  425. self.conf_file = new_conf.replace(old_dirpath, new_dirpath)
  426. if hasattr(self, 'kernels_dir') and self.kernels_dir is not None:
  427. self.kernels_dir = self.kernels_dir.replace(old_dirpath, new_dirpath)
  428. self._update_libvirt_domain()
  429. self.post_rename(old_name)
  430. @qubes.events.handler('property-pre-set:autostart')
  431. def on_property_pre_set_autostart(self, event, prop, name, value, oldvalue=None):
  432. if subprocess.call(['sudo', 'systemctl',
  433. ('enable' if value else 'disable'),
  434. 'qubes-vm@{}.service'.format(self.name)]):
  435. raise QubesException("Failed to set autostart for VM via systemctl")
  436. @qubes.events.handler('device-pre-attached:pci')
  437. def on_device_pre_attached_pci(self, event, pci):
  438. if not os.path.exists('/sys/bus/pci/devices/0000:%s' % pci):
  439. raise QubesException("Invalid PCI device: %s" % pci)
  440. if not self.is_running():
  441. return
  442. try:
  443. subprocess.check_call(['sudo', system_path["qubes_pciback_cmd"], pci])
  444. subprocess.check_call(['sudo', 'xl', 'pci-attach', str(self.xid), pci])
  445. except Exception as e:
  446. print >>sys.stderr, "Failed to attach PCI device on the fly " \
  447. "(%s), changes will be seen after VM restart" % str(e)
  448. @qubes.events.handler('device-pre-detached:pci')
  449. def on_device_pre_detached_pci(self, event, pci):
  450. if not self.is_running():
  451. return
  452. p = subprocess.Popen(['xl', 'pci-list', str(self.xid)],
  453. stdout=subprocess.PIPE)
  454. result = p.communicate()
  455. m = re.search(r"^(\d+.\d+)\s+0000:%s$" % pci, result[0], flags=re.MULTILINE)
  456. if not m:
  457. print >>sys.stderr, "Device %s already detached" % pci
  458. return
  459. vmdev = m.group(1)
  460. try:
  461. self.run_service("qubes.DetachPciDevice",
  462. user="root", input="00:%s" % vmdev)
  463. subprocess.check_call(['sudo', 'xl', 'pci-detach', str(self.xid), pci])
  464. except Exception as e:
  465. print >>sys.stderr, "Failed to detach PCI device on the fly " \
  466. "(%s), changes will be seen after VM restart" % str(e)
  467. #
  468. # methods for changing domain state
  469. #
  470. def start(self, preparing_dvm=False, start_guid=True,
  471. notify_function=None, mem_required=None):
  472. '''Start domain
  473. :param bool preparing_dvm: FIXME
  474. :param bool start_guid: FIXME
  475. :param collections.Callable notify_function: FIXME
  476. :param int mem_required: FIXME
  477. '''
  478. # Intentionally not used is_running(): eliminate also "Paused", "Crashed", "Halting"
  479. if self.get_power_state() != "Halted":
  480. raise QubesException("VM is already running!")
  481. self.log.info('Starting {}'.format(self.name))
  482. self.verify_files()
  483. if self.netvm is not None:
  484. if self.netvm.qid != 0:
  485. if not self.netvm.is_running():
  486. self.netvm.start(start_guid=start_guid, notify_function=notify_function)
  487. self.storage.prepare_for_vm_startup()
  488. self._update_libvirt_domain()
  489. if mem_required is None:
  490. mem_required = int(self.memory) * 1024 * 1024
  491. if qmemman_present:
  492. qmemman_client = qubes.qmemman_client.QMemmanClient()
  493. try:
  494. got_memory = qmemman_client.request_memory(mem_required)
  495. except IOError as e:
  496. raise IOError("ERROR: Failed to connect to qmemman: %s" % str(e))
  497. if not got_memory:
  498. qmemman_client.close()
  499. raise MemoryError("ERROR: insufficient memory to start VM '%s'" % self.name)
  500. # Bind pci devices to pciback driver
  501. for pci in self.devices['pci']:
  502. nd = vmm.libvirt_conn.nodeDeviceLookupByName('pci_0000_' + pci.replace(':','_').replace('.','_'))
  503. try:
  504. nd.dettach()
  505. except libvirt.libvirtError:
  506. if vmm.libvirt_conn.virConnGetLastError()[0] == libvirt.VIR_ERR_INTERNAL_ERROR:
  507. # allready detached
  508. pass
  509. else:
  510. raise
  511. self.libvirt_domain.createWithFlags(libvirt.VIR_DOMAIN_START_PAUSED)
  512. if preparing_dvm:
  513. self.services['qubes-dvm'] = True
  514. self.log.info('Setting Qubes DB info for the VM')
  515. self.start_qubesdb()
  516. self.create_qdb_entries()
  517. self.log.info('Updating firewall rules')
  518. for vm in self.app.domains:
  519. if vm.is_proxyvm() and vm.is_running():
  520. vm.write_iptables_xenstore_entry()
  521. self.fire_event('domain-started', preparing_dvm=preparing_dvm, start_guid=start_guid)
  522. self.log.warning('Activating the {} VM'.format(self.name))
  523. self.libvirt_domain.resume()
  524. # close() is not really needed, because the descriptor is close-on-exec
  525. # anyway, the reason to postpone close() is that possibly xl is not done
  526. # constructing the domain after its main process exits
  527. # so we close() when we know the domain is up
  528. # the successful unpause is some indicator of it
  529. if qmemman_present:
  530. qmemman_client.close()
  531. if self._start_guid_first and start_guid and not preparing_dvm and os.path.exists('/var/run/shm.id'):
  532. self.start_guid(notify_function=notify_function)
  533. if not preparing_dvm:
  534. self.start_qrexec_daemon(notify_function=notify_function)
  535. if start_guid and not preparing_dvm and os.path.exists('/var/run/shm.id'):
  536. self.start_guid(notify_function=notify_function)
  537. def shutdown(self):
  538. '''Shutdown domain.
  539. :raises QubesException: when domain is already shut down.
  540. '''
  541. if not self.is_running():
  542. raise QubesException("VM already stopped!")
  543. self.libvirt_domain.shutdown()
  544. def force_shutdown(self):
  545. '''Forcefuly shutdown (destroy) domain.
  546. :raises QubesException: when domain is already shut down.
  547. '''
  548. if not self.is_running() and not self.is_paused():
  549. raise QubesException("VM already stopped!")
  550. self.libvirt_domain.destroy()
  551. def suspend(self):
  552. '''Suspend (pause) domain.
  553. :raises QubesException: when domain is already shut down.
  554. :raises NotImplemetedError: when domain has PCI devices attached.
  555. '''
  556. if not self.is_running() and not self.is_paused():
  557. raise QubesException("VM already stopped!")
  558. if len(self.devices['pci']) > 0:
  559. raise NotImplementedError()
  560. else:
  561. self.libvirt_domain.suspend()
  562. def pause(self):
  563. '''Pause (suspend) domain. This currently delegates to :py:meth:`suspend`.'''
  564. if not self.is_running():
  565. raise QubesException("VM not running!")
  566. self.suspend()
  567. def resume(self):
  568. '''Resume suspended domain.
  569. :raises NotImplemetedError: when machine is alread suspended.
  570. '''
  571. if self.get_power_state() == "Suspended":
  572. raise NotImplementedError()
  573. else:
  574. self.unpause()
  575. def unpause(self):
  576. '''Resume (unpause) a domain'''
  577. if not self.is_paused():
  578. raise QubesException("VM not paused!")
  579. self.libvirt_domain.resume()
  580. def run(self, command, user=None, autostart=False, notify_function=None,
  581. passio=False, passio_popen=False, passio_stderr=False,
  582. ignore_stderr=False, localcmd=None, wait=False, gui=True,
  583. filter_esc=False):
  584. '''Run specified command inside domain
  585. :param str command: the command to be run
  586. :param str user: user to run the command as
  587. :param bool autostart: if :py:obj:`True`, machine will be started if it is not running
  588. :param collections.Callable notify_function: FIXME, may go away
  589. :param bool passio: FIXME
  590. :param bool passio_popen: if :py:obj:`True`, :py:class:`subprocess.Popen` object has connected ``stdin`` and ``stdout``
  591. :param bool passio_stderr: if :py:obj:`True`, :py:class:`subprocess.Popen` has additionaly ``stderr`` connected
  592. :param bool ignore_stderr: if :py:obj:`True`, ``stderr`` is connected to :file:`/dev/null`
  593. :param str localcmd: local command to communicate with remote command
  594. :param bool wait: if :py:obj:`True`, wait for command completion
  595. :param bool gui: when autostarting, also start gui daemon
  596. :param bool filter_esc: filter escape sequences to protect terminal emulator
  597. '''
  598. if user is None:
  599. user = self.default_user
  600. null = None
  601. if not self.is_running() and not self.is_paused():
  602. if not autostart:
  603. raise QubesException("VM not running")
  604. try:
  605. if notify_function is not None:
  606. notify_function("info", "Starting the '{0}' VM...".format(self.name))
  607. self.start(start_guid=gui, notify_function=notify_function)
  608. except (IOError, OSError, QubesException) as err:
  609. raise QubesException("Error while starting the '{0}' VM: {1}".format(self.name, err))
  610. except (MemoryError) as err:
  611. raise QubesException("Not enough memory to start '{0}' VM! "
  612. "Close one or more running VMs and try "
  613. "again.".format(self.name))
  614. if self.is_paused():
  615. raise QubesException("VM is paused")
  616. if not self.is_qrexec_running():
  617. raise QubesException(
  618. "Domain '{}': qrexec not connected.".format(self.name))
  619. if gui and os.getenv("DISPLAY") is not None and not self.is_guid_running():
  620. self.start_guid(verbose = verbose, notify_function = notify_function)
  621. args = [system_path["qrexec_client_path"], "-d", str(self.name), "%s:%s" % (user, command)]
  622. if localcmd is not None:
  623. args += [ "-l", localcmd]
  624. if filter_esc:
  625. args += ["-t"]
  626. if os.isatty(sys.stderr.fileno()):
  627. args += ["-T"]
  628. if passio:
  629. if os.name == 'nt':
  630. # wait for qrexec-client to exit, otherwise client is not properly attached to console
  631. # if qvm-run is executed from cmd.exe
  632. ret = subprocess.call(args)
  633. exit(ret)
  634. os.execv(system_path["qrexec_client_path"], args)
  635. exit(1)
  636. call_kwargs = {}
  637. if ignore_stderr:
  638. null = open("/dev/null", "w")
  639. call_kwargs['stderr'] = null
  640. if passio_popen:
  641. popen_kwargs={'stdout': subprocess.PIPE}
  642. popen_kwargs['stdin'] = subprocess.PIPE
  643. if passio_stderr:
  644. popen_kwargs['stderr'] = subprocess.PIPE
  645. else:
  646. popen_kwargs['stderr'] = call_kwargs.get('stderr', None)
  647. p = subprocess.Popen(args, **popen_kwargs)
  648. if null:
  649. null.close()
  650. return p
  651. if not wait:
  652. args += ["-e"]
  653. retcode = subprocess.call(args, **call_kwargs)
  654. if null:
  655. null.close()
  656. return retcode
  657. def run_service(self, service, source=None, user=None,
  658. passio_popen=False, input=None):
  659. '''Run service on this VM
  660. **passio_popen** and **input** are mutually exclusive.
  661. :param str service: service name
  662. :param qubes.vm.QubesVM: source domain as presented to this VM
  663. :param str user: username to run service as
  664. :param bool passio_popen: passed verbatim to :py:meth:`run`
  665. :param str input: string passed as input to service
  666. '''
  667. if input is not None and passio_popen is not None:
  668. raise ValueError("'input' and 'passio_popen' cannot be used "
  669. "together")
  670. source = 'dom0' if source is None else self.app.domains[source].name
  671. if input:
  672. return self.run("QUBESRPC %s %s" % (service, source),
  673. localcmd="echo %s" % input, user=user, wait=True)
  674. else:
  675. return self.run("QUBESRPC %s %s" % (service, source),
  676. passio_popen=passio_popen, user=user, wait=True)
  677. def start_guid(self, extra_guid_args=[]):
  678. '''Launch gui daemon.
  679. GUI daemon securely displays windows from domain.
  680. :param list extra_guid_args: Extra argv to pass to :program:`guid`.
  681. '''
  682. self.log.info('Starting gui daemon')
  683. guid_cmd = [system_path["qubes_guid_path"],
  684. "-d", str(self.xid), "-N", self.name,
  685. "-c", self.label.color,
  686. "-i", self.label.icon_path,
  687. "-l", str(self.label.index)]
  688. guid_cmd += extra_guid_args
  689. if self.debug:
  690. guid_cmd += ['-v', '-v']
  691. # elif not verbose:
  692. guid_cmd += ['-q']
  693. retcode = subprocess.call(guid_cmd)
  694. if (retcode != 0) :
  695. raise QubesException("Cannot start qubes-guid!")
  696. self.log.info('Sending monitor layout')
  697. try:
  698. subprocess.call([system_path["monitor_layout_notify_cmd"], self.name])
  699. except Exception as e:
  700. self.log.error('ERROR: {!s}'.format(e))
  701. self.wait_for_session()
  702. def start_qrexec_daemon(self):
  703. '''Start qrexec daemon.
  704. :raises OSError: when starting fails.
  705. '''
  706. self.log.debug('Starting the qrexec daemon')
  707. qrexec_args = [str(self.xid), self.name, self.default_user]
  708. if not self.debug:
  709. qrexec_args.insert(0, "-q")
  710. qrexec_env = os.environ
  711. qrexec_env['QREXEC_STARTUP_TIMEOUT'] = str(self.qrexec_timeout)
  712. retcode = subprocess.call([system_path["qrexec_daemon_path"]] +
  713. qrexec_args, env=qrexec_env)
  714. if (retcode != 0) :
  715. raise OSError("Cannot execute qrexec-daemon!")
  716. def start_qubesdb(self):
  717. '''Start QubesDB daemon.
  718. :raises OSError: when starting fails.
  719. '''
  720. self.log.info('Starting Qubes DB')
  721. retcode = subprocess.call([
  722. system_path["qubesdb_daemon_path"],
  723. str(self.xid),
  724. self.name])
  725. if retcode != 0:
  726. self.force_shutdown()
  727. raise OSError("ERROR: Cannot execute qubesdb-daemon!")
  728. def wait_for_session(self):
  729. '''Wait until machine finished boot sequence.
  730. This is done by executing qubes RPC call that checks if dummy system
  731. service (which is started late in standard runlevel) is active.
  732. '''
  733. self.log.info('Waiting for qubes-session')
  734. # Note : User root is redefined to SYSTEM in the Windows agent code
  735. p = self.run('QUBESRPC qubes.WaitForSession none',
  736. user="root", passio_popen=True, gui=False, wait=True)
  737. p.communicate(input=self.default_user)
  738. def create_on_disk(self, source_template=None):
  739. '''Create files needed for VM.
  740. :param qubes.vm.templatevm.TemplateVM source_template: Template to use
  741. (if :py:obj:`None`, use domain's own template
  742. '''
  743. if source_template is None:
  744. source_template = self.template
  745. assert source_template is not None
  746. self.storage.create_on_disk(verbose, source_template)
  747. if self.updateable:
  748. kernels_dir = source_template.kernels_dir
  749. self.log.info(
  750. 'Copying the kernel (unset kernel to use it): {0}'.format(
  751. kernels_dir))
  752. os.mkdir(self.dir_path + '/kernels')
  753. for f in ("vmlinuz", "initramfs", "modules.img"):
  754. shutil.copy(os.path.join(kernels_dir, f),
  755. os.path.join(self.dir_path,
  756. qubes.config.vm_files["kernels_subdir"], f))
  757. self.log.info('Creating icon symlink: {0} -> {1}'.format(self.icon_path, self.label.icon_path))
  758. if hasattr(os, "symlink"):
  759. os.symlink(self.label.icon_path, self.icon_path)
  760. else:
  761. shutil.copy(self.label.icon_path, self.icon_path)
  762. # fire hooks
  763. self.fire_event('domain-created-on-disk', source_template)
  764. def resize_private_img(self, size):
  765. '''Resize private image.'''
  766. # TODO QubesValueError, not assert
  767. assert size >= self.get_private_img_sz(), "Cannot shrink private.img"
  768. # resize the image
  769. self.storage.resize_private_img(size)
  770. # and then the filesystem
  771. retcode = 0
  772. if self.is_running():
  773. retcode = self.run("while [ \"`blockdev --getsize64 /dev/xvdb`\" -lt {0} ]; do ".format(size) +
  774. "head /dev/xvdb > /dev/null; sleep 0.2; done; resize2fs /dev/xvdb", user="root", wait=True)
  775. if retcode != 0:
  776. raise QubesException("resize2fs failed")
  777. def remove_from_disk(self):
  778. '''Remove domain remnants from disk.'''
  779. self.fire_event('domain-removed-from-disk')
  780. self.storage.remove_from_disk()
  781. def clone_disk_files(self, src):
  782. '''Clone files from other vm.
  783. :param qubes.vm.QubesVM src: source VM
  784. '''
  785. if src_vm.is_running():
  786. raise QubesException("Attempt to clone a running VM!")
  787. self.storage.clone_disk_files(src, verbose=False)
  788. if srv.icon_path is not None \
  789. and os.path.exists(src.dir_path) \
  790. and self.icon_path is not None:
  791. if os.path.islink(src.icon_path):
  792. icon_path = os.readlink(src.icon_path)
  793. self.log.info(
  794. 'Creating icon symlink {} -> {}'.format(
  795. self.icon_path, icon_path))
  796. os.symlink(icon_path, self.icon_path)
  797. else:
  798. self.log.info(
  799. 'Copying icon {} -> {}'.format(
  800. src.icon_path, self.icon_path))
  801. shutil.copy(src.icon_path, self.icon_path)
  802. # fire hooks
  803. self.fire_event('cloned-files', src)
  804. # TODO maybe this should be other way: backend.devices['net'].attach(self)
  805. def attach_network(self):
  806. '''Attach network in this machine to it's netvm.'''
  807. if not self.is_running():
  808. raise QubesException("VM not running!")
  809. if self.netvm is None:
  810. raise QubesException("NetVM not set!")
  811. if not netvm.is_running():
  812. self.log.info('Starting NetVM ({0})'.format(netvm.name))
  813. netvm.start()
  814. self.libvirt_domain.attachDevice(lxml.etree.ElementTree(
  815. self.xml_net_dev(self.ip, self.mac, self.netvm)).tostring())
  816. def detach_network(self):
  817. '''Detach machine from it's netvm'''
  818. if not self.is_running():
  819. raise QubesException("VM not running!")
  820. if self.netvm is None:
  821. raise QubesException("NetVM not set!")
  822. self.libvirt_domain.detachDevice(lxml.etree.ElementTree(
  823. self.xml_net_dev(self.ip, self.mac, self.netvm)).tostring())
  824. #
  825. # methods for querying domain state
  826. #
  827. # state of the machine
  828. def get_power_state(self):
  829. '''Return power state description string.
  830. Return value may be one of those:
  831. =============== ========================================================
  832. return value meaning
  833. =============== ========================================================
  834. ``'Halted'`` Machine is not active.
  835. ``'Transient'`` Machine is running, but does not have :program:`guid`
  836. or :program:`qrexec` available.
  837. ``'Running'`` Machine is ready and running.
  838. ``'Paused'`` Machine is paused (currently not available, see below).
  839. ``'Suspended'`` Machine is S3-suspended.
  840. ``'Halting'`` Machine is in process of shutting down.
  841. ``'Dying'`` Machine crashed and is unusable.
  842. ``'Crashed'`` Machine crashed and is unusable, probably because of
  843. bug in dom0.
  844. ``'NA'`` Machine is in unknown state (most likely libvirt domain
  845. is undefined).
  846. =============== ========================================================
  847. ``Paused`` state is currently unavailable because of missing code in libvirt/xen glue.
  848. FIXME: graph below may be incomplete and wrong. Click on method name to
  849. see its documentation.
  850. .. graphviz::
  851. digraph {
  852. node [fontname="sans-serif"];
  853. edge [fontname="mono"];
  854. Halted;
  855. NA;
  856. Dying;
  857. Crashed;
  858. Transient;
  859. Halting;
  860. Running;
  861. Paused [color=gray75 fontcolor=gray75];
  862. Suspended;
  863. NA -> Halted;
  864. Halted -> NA [constraint=false];
  865. Halted -> Transient
  866. [xlabel="start()" URL="#qubes.vm.qubesvm.QubesVM.start"];
  867. Transient -> Running;
  868. Running -> Halting
  869. [xlabel="shutdown()"
  870. URL="#qubes.vm.qubesvm.QubesVM.shutdown"
  871. constraint=false];
  872. Halting -> Dying -> Halted [constraint=false];
  873. /* cosmetic, invisible edges to put rank constraint */
  874. Dying -> Halting [style="invis"];
  875. Halting -> Transient [style="invis"];
  876. Running -> Halted
  877. [label="force_shutdown()"
  878. URL="#qubes.vm.qubesvm.QubesVM.force_shutdown"
  879. constraint=false];
  880. Running -> Crashed [constraint=false];
  881. Crashed -> Halted [constraint=false];
  882. Running -> Paused
  883. [label="pause()" URL="#qubes.vm.qubesvm.QubesVM.pause"
  884. color=gray75 fontcolor=gray75];
  885. Running -> Suspended
  886. [label="pause()" URL="#qubes.vm.qubesvm.QubesVM.pause"
  887. color=gray50 fontcolor=gray50];
  888. Paused -> Running
  889. [label="unpause()" URL="#qubes.vm.qubesvm.QubesVM.unpause"
  890. color=gray75 fontcolor=gray75];
  891. Suspended -> Running
  892. [label="unpause()" URL="#qubes.vm.qubesvm.QubesVM.unpause"
  893. color=gray50 fontcolor=gray50];
  894. Running -> Suspended
  895. [label="suspend()" URL="#qubes.vm.qubesvm.QubesVM.suspend"];
  896. Suspended -> Running
  897. [label="resume()" URL="#qubes.vm.qubesvm.QubesVM.resume"];
  898. { rank=source; Halted NA };
  899. { rank=same; Transient Halting };
  900. { rank=same; Crashed Dying };
  901. { rank=sink; Paused Suspended };
  902. }
  903. .. seealso:: http://wiki.libvirt.org/page/VM_lifecycle
  904. .. seealso:: https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainState
  905. '''
  906. libvirt_domain = self.libvirt_domain
  907. if libvirt_domain is None:
  908. return "NA"
  909. if libvirt_domain.isActive():
  910. if libvirt_domain.state()[0] == libvirt.VIR_DOMAIN_PAUSED:
  911. return "Paused"
  912. elif libvirt_domain.state()[0] == libvirt.VIR_DOMAIN_CRASHED:
  913. return "Crashed"
  914. elif libvirt_domain.state()[0] == libvirt.VIR_DOMAIN_SHUTDOWN:
  915. return "Halting"
  916. elif libvirt_domain.state()[0] == libvirt.VIR_DOMAIN_SHUTOFF:
  917. return "Dying"
  918. elif libvirt_domain.state()[0] == libvirt.VIR_DOMAIN_PMSUSPENDED:
  919. return "Suspended"
  920. else:
  921. if not self.is_fully_usable():
  922. return "Transient"
  923. else:
  924. return "Running"
  925. else:
  926. return 'Halted'
  927. return "NA"
  928. def is_running(self):
  929. '''Check whether this domain is running.
  930. :returns: :py:obj:`True` if this domain is started, :py:obj:`False` otherwise.
  931. :rtype: bool
  932. '''
  933. return self.libvirt_domain and self.libvirt_domain.isActive()
  934. def is_paused(self):
  935. '''Check whether this domain is paused.
  936. :returns: :py:obj:`True` if this domain is paused, :py:obj:`False` otherwise.
  937. :rtype: bool
  938. '''
  939. return self.libvirt_domain \
  940. and self.libvirt_domain.state() == libvirt.VIR_DOMAIN_PAUSED
  941. def is_guid_running(self):
  942. '''Check whether gui daemon for this domain is available.
  943. :returns: :py:obj:`True` if guid is running, :py:obj:`False` otherwise.
  944. :rtype: bool
  945. '''
  946. xid = self.xid
  947. if xid < 0:
  948. return False
  949. if not os.path.exists('/var/run/qubes/guid-running.%d' % xid):
  950. return False
  951. return True
  952. def is_qrexec_running(self):
  953. '''Check whether qrexec for this domain is available.
  954. :returns: :py:obj:`True` if qrexec is running, :py:obj:`False` otherwise.
  955. :rtype: bool
  956. '''
  957. if self.xid < 0:
  958. return False
  959. return os.path.exists('/var/run/qubes/qrexec.%s' % self.name)
  960. def is_fully_usable(self):
  961. '''Check whether domain is running and sane.
  962. Currently this checks for running guid and qrexec.
  963. :returns: :py:obj:`True` if qrexec is running, :py:obj:`False` otherwise.
  964. :rtype: bool
  965. '''
  966. # Running gui-daemon implies also VM running
  967. if not self.is_guid_running():
  968. return False
  969. if not self.is_qrexec_running():
  970. return False
  971. return True
  972. # memory and disk
  973. def get_mem(self):
  974. '''Get current memory usage from VM.
  975. :returns: Memory usage [FIXME unit].
  976. :rtype: FIXME
  977. '''
  978. if self.libvirt_domain is None:
  979. return 0
  980. if not self.libvirt_domain.isActive():
  981. return 0
  982. return self.libvirt_domain.info()[1]
  983. def get_mem_static_max(self):
  984. '''Get maximum memory available to VM.
  985. :returns: Memory limit [FIXME unit].
  986. :rtype: FIXME
  987. '''
  988. if self.libvirt_domain is None:
  989. return 0
  990. return self.libvirt_domain.maxMemory()
  991. def get_per_cpu_time(self):
  992. '''Get total CPU time burned by this domain since start.
  993. :returns: CPU time usage [FIXME unit].
  994. :rtype: FIXME
  995. '''
  996. if self.libvirt_domain is None:
  997. return 0
  998. if not self.libvirt_domain.isActive():
  999. return 0
  1000. return libvirt_domain.getCPUStats(
  1001. libvirt.VIR_NODE_CPU_STATS_ALL_CPUS, 0)[0]['cpu_time']/10**9
  1002. # XXX shouldn't this go only to vms that have root image?
  1003. def get_disk_utilization_root_img(self):
  1004. '''Get space that is actually ocuppied by :py:attr:`root_img`.
  1005. Root image is a sparse file, so it is probably much less than logical
  1006. available space.
  1007. :returns: domain's real disk image size [FIXME unit]
  1008. :rtype: FIXME
  1009. .. seealso:: :py:meth:`get_root_img_sz`
  1010. '''
  1011. return qubes.utils.get_disk_usage(self.root_img)
  1012. # XXX shouldn't this go only to vms that have root image?
  1013. def get_root_img_sz(self):
  1014. '''Get image size of :py:attr:`root_img`.
  1015. Root image is a sparse file, so it is probably much more than ocuppied
  1016. physical space.
  1017. :returns: domain's virtual disk size [FIXME unit]
  1018. :rtype: FIXME
  1019. .. seealso:: :py:meth:`get_disk_utilization_root_img`
  1020. '''
  1021. if not os.path.exists(self.root_img):
  1022. return 0
  1023. return os.path.getsize(self.root_img)
  1024. def get_disk_utilization_private_img(self):
  1025. '''Get space that is actually ocuppied by :py:attr:`private_img`.
  1026. Private image is a sparse file, so it is probably much less than
  1027. logical available space.
  1028. :returns: domain's real disk image size [FIXME unit]
  1029. :rtype: FIXME
  1030. .. seealso:: :py:meth:`get_private_img_sz`
  1031. '''
  1032. return qubes.utils.get_disk_usage(self.private_img)
  1033. def get_private_img_sz(self):
  1034. '''Get image size of :py:attr:`private_img`.
  1035. Private image is a sparse file, so it is probably much more than
  1036. ocuppied physical space.
  1037. :returns: domain's virtual disk size [FIXME unit]
  1038. :rtype: FIXME
  1039. .. seealso:: :py:meth:`get_disk_utilization_private_img`
  1040. '''
  1041. return self.storage.get_private_img_sz()
  1042. def get_disk_utilization(self):
  1043. '''Return total space actually occuppied by all files belonging to this domain.
  1044. :returns: domain's total disk usage [FIXME unit]
  1045. :rtype: FIXME
  1046. '''
  1047. return qubes.utils.get_disk_usage(self.dir_path)
  1048. def verify_files(self):
  1049. '''Verify that files accessed by this machine are sane.
  1050. On success, returns normally. On failure, raises exception.
  1051. '''
  1052. self.storage.verify_files()
  1053. if not os.path.exists(os.path.join(self.kernels_dir, 'vmlinuz')):
  1054. raise QubesException(
  1055. "VM kernel does not exist: {0}".\
  1056. format(os.path.join(self.kernels_dir, 'vmlinuz')))
  1057. if not os.path.exists(os.path.join(self.kernels_dir, 'initramfs')):
  1058. raise QubesException(
  1059. "VM initramfs does not exist: {0}".\
  1060. format(os.path.join(self.kernels_dir, 'initramfs')))
  1061. self.fire_event('verify-files')
  1062. return True
  1063. # miscellanous
  1064. def get_start_time(self):
  1065. '''Tell when machine was started.
  1066. :rtype: datetime.datetime
  1067. '''
  1068. if not self.is_running():
  1069. return None
  1070. # TODO shouldn't this be qubesdb?
  1071. start_time = self.app.vmm.xs.read('', "/vm/%s/start_time" % str(self.uuid))
  1072. if start_time != '':
  1073. return datetime.datetime.fromtimestamp(float(start_time))
  1074. else:
  1075. return None
  1076. # XXX this probably should go to AppVM
  1077. def is_outdated(self):
  1078. '''Check whether domain needs restart to update root image from template.
  1079. :returns: :py:obj:`True` if is outdated, :py:obj:`False` otherwise.
  1080. :rtype: bool
  1081. '''
  1082. # Makes sense only on VM based on template
  1083. if self.template is None:
  1084. return False
  1085. if not self.is_running():
  1086. return False
  1087. if not hasattr(self.template, 'rootcow_img'):
  1088. return False
  1089. rootimg_inode = os.stat(self.template.root_img)
  1090. try:
  1091. rootcow_inode = os.stat(self.template.rootcow_img)
  1092. except OSError:
  1093. # The only case when rootcow_img doesn't exists is in the middle of
  1094. # commit_changes, so VM is outdated right now
  1095. return True
  1096. current_dmdev = "/dev/mapper/snapshot-{0:x}:{1}-{2:x}:{3}".format(
  1097. rootimg_inode[2], rootimg_inode[1],
  1098. rootcow_inode[2], rootcow_inode[1])
  1099. # FIXME
  1100. # 51712 (0xCA00) is xvda
  1101. # backend node name not available through xenapi :(
  1102. used_dmdev = vmm.xs.read('', "/local/domain/0/backend/vbd/{0}/51712/node".format(self.xid))
  1103. return used_dmdev != current_dmdev
  1104. def is_networked(self):
  1105. '''Check whether this VM can reach network (firewall notwithstanding).
  1106. :returns: :py:obj:`True` if is machine can reach network, :py:obj:`False` otherwise.
  1107. :rtype: bool
  1108. '''
  1109. if self.provides_network:
  1110. return True
  1111. return self.netvm is not None
  1112. #
  1113. # helper methods
  1114. #
  1115. # TODO deprecate `default`
  1116. def absolute_path(self, path, default):
  1117. '''Return specified path as absolute path.
  1118. Relative paths are relative to :py:attr:`dir_path`. Absolute path are left unchanged.
  1119. :param str path: Path in question (possibly relatve).
  1120. :param default: What to return if ``arg`` is :py:obj:`None`.
  1121. :returns: Absolute path.
  1122. '''
  1123. if arg is not None and os.path.isabs(arg):
  1124. return arg
  1125. else:
  1126. return os.path.join(self.dir_path, (arg if arg is not None else default))
  1127. def relative_path(self, path):
  1128. '''Return path relative to py:attr:`dir_path`.
  1129. :param str path: Path in question.
  1130. :returns: Relative path.
  1131. '''
  1132. return os.path.relpath(path, self.dir_path)
  1133. # return arg.replace(self.dir_path + '/', '')
  1134. def create_qdb_entries(self):
  1135. '''Create entries in Qubes DB.
  1136. '''
  1137. self.qdb.write("/name", self.name)
  1138. self.qdb.write("/qubes-vm-type", self.type)
  1139. self.qdb.write("/qubes-vm-updateable", str(self.updateable))
  1140. if self.provides_network:
  1141. self.qdb.write("/qubes-netvm-gateway", self.gateway)
  1142. self.qdb.write("/qubes-netvm-secondary-dns", self.secondary_dns)
  1143. self.qdb.write("/qubes-netvm-netmask", self.netmask)
  1144. self.qdb.write("/qubes-netvm-network", self.network)
  1145. if self.netvm is not None:
  1146. self.qdb.write("/qubes-ip", self.ip)
  1147. self.qdb.write("/qubes-netmask", self.netvm.netmask)
  1148. self.qdb.write("/qubes-gateway", self.netvm.gateway)
  1149. self.qdb.write("/qubes-secondary-dns", self.netvm.secondary_dns)
  1150. tzname = qubes.utils.get_timezone()
  1151. if tzname:
  1152. self.qdb.write("/qubes-timezone", tzname)
  1153. for srv in self.services.keys():
  1154. # convert True/False to "1"/"0"
  1155. self.qdb.write("/qubes-service/{0}".format(srv),
  1156. str(int(self.services[srv])))
  1157. self.qdb.write("/qubes-block-devices", '')
  1158. self.qdb.write("/qubes-usb-devices", '')
  1159. self.qdb.write("/qubes-debug-mode", str(int(self.debug)))
  1160. # TODO: Currently the whole qmemman is quite Xen-specific, so stay with
  1161. # xenstore for it until decided otherwise
  1162. if qmemman_present:
  1163. vmm.xs.set_permissions('', '/local/domain/{0}/memory'.format(self.xid),
  1164. [{ 'dom': self.xid }])
  1165. self.fire_event('qdb-created')
  1166. def _update_libvirt_domain(self):
  1167. '''Re-initialise :py:attr:`libvirt_domain`.'''
  1168. domain_config = self.create_config_file()
  1169. if self._libvirt_domain:
  1170. self._libvirt_domain.undefine()
  1171. try:
  1172. self._libvirt_domain = vmm.libvirt_conn.defineXML(domain_config)
  1173. self.uuid = uuid.UUID(bytes=self._libvirt_domain.UUID())
  1174. except libvirt.libvirtError:
  1175. if vmm.libvirt_conn.virConnGetLastError()[0] == libvirt.VIR_ERR_NO_DOMAIN:
  1176. # accept the fact that libvirt doesn't know anything about this
  1177. # domain...
  1178. pass
  1179. else:
  1180. raise
  1181. def cleanup_vifs(self):
  1182. '''Remove stale network device backends.
  1183. Xend does not remove vif when backend domain is down, so we must do it
  1184. manually.
  1185. '''
  1186. # FIXME: remove this?
  1187. if not self.is_running():
  1188. return
  1189. dev_basepath = '/local/domain/%d/device/vif' % self.xid
  1190. for dev in self.app.vmm.xs.ls('', dev_basepath):
  1191. # check if backend domain is alive
  1192. backend_xid = int(self.app.vmm.xs.read('', '%s/%s/backend-id' % (dev_basepath, dev)))
  1193. if backend_xid in self.app.vmm.libvirt_conn.listDomainsID():
  1194. # check if device is still active
  1195. if self.app.vmm.xs.read('', '%s/%s/state' % (dev_basepath, dev)) == '4':
  1196. continue
  1197. # remove dead device
  1198. self.app.vmm.xs.rm('', '%s/%s' % (dev_basepath, dev))
  1199. #
  1200. # workshop -- those are to be reworked later
  1201. #
  1202. def get_prefmem(self):
  1203. # TODO: qmemman is still xen specific
  1204. untrusted_meminfo_key = xs.read('', '/local/domain/%s/memory/meminfo'
  1205. % self.xid)
  1206. if untrusted_meminfo_key is None or untrusted_meminfo_key == '':
  1207. return 0
  1208. domain = qmemman.DomainState(self.xid)
  1209. qmemman_algo.refresh_meminfo_for_domain(domain, untrusted_meminfo_key)
  1210. domain.memory_maximum = self.get_mem_static_max()*1024
  1211. return qmemman_algo.prefmem(domain)/1024
  1212. #
  1213. # landfill -- those are unneeded
  1214. #
  1215. # attrs = {
  1216. #
  1217. ##### Internal attributes - will be overriden in __init__ regardless of args
  1218. # those should be __builtin__.property of something
  1219. # used to suppress side effects of clone_attrs
  1220. # XXX probably will be obsoleted by .events_enabled
  1221. # "_do_not_reset_firewall": { "func": lambda x: False },
  1222. # XXX WTF?
  1223. # "kernels_dir": {
  1224. # # for backward compatibility (or another rare case): kernel=None -> kernel in VM dir
  1225. # "func": lambda x: \
  1226. # os.path.join(system_path["qubes_kernels_base_dir"],
  1227. # self.kernel) if self.kernel is not None \
  1228. # else os.path.join(self.dir_path,
  1229. # vm_files["kernels_subdir"]) },
  1230. # "_start_guid_first": { "func": lambda x: False },
  1231. # }
  1232. # this function appears unused
  1233. # def _cleanup_zombie_domains(self):
  1234. # """
  1235. # This function is workaround broken libxl (which leaves not fully
  1236. # created domain on failure) and vchan on domain crash behaviour
  1237. # @return: None
  1238. # """
  1239. # xc = self.get_xc_dominfo()
  1240. # if xc and xc['dying'] == 1:
  1241. # # GUID still running?
  1242. # guid_pidfile = '/var/run/qubes/guid-running.%d' % xc['domid']
  1243. # if os.path.exists(guid_pidfile):
  1244. # guid_pid = open(guid_pidfile).read().strip()
  1245. # os.kill(int(guid_pid), 15)
  1246. # # qrexec still running?
  1247. # if self.is_qrexec_running():
  1248. # #TODO: kill qrexec daemon
  1249. # pass