templatevm.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2014-2016 Wojtek Porczyk <woju@invisiblethingslab.com>
  5. # Copyright (C) 2016 Marek Marczykowski <marmarek@invisiblethingslab.com>)
  6. # Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  20. #
  21. ''' This module contains the TemplateVM implementation '''
  22. import qubes
  23. import qubes.config
  24. import qubes.vm.qubesvm
  25. import qubes.vm.mix.net
  26. from qubes.config import defaults
  27. from qubes.vm.qubesvm import QubesVM
  28. class TemplateVM(QubesVM):
  29. '''Template for AppVM'''
  30. dir_path_prefix = qubes.config.system_path['qubes_templates_dir']
  31. @property
  32. def appvms(self):
  33. ''' Returns a generator containing all domains based on the current
  34. TemplateVM.
  35. '''
  36. for vm in self.app.domains:
  37. if hasattr(vm, 'template') and vm.template is self:
  38. yield vm
  39. netvm = qubes.VMProperty('netvm', load_stage=4, allow_none=True,
  40. default=None,
  41. # pylint: disable=protected-access
  42. setter=qubes.vm.qubesvm.QubesVM.netvm._setter,
  43. doc='VM that provides network connection to this domain. When '
  44. '`None`, machine is disconnected.')
  45. def __init__(self, *args, **kwargs):
  46. assert 'template' not in kwargs, "A TemplateVM can not have a template"
  47. self.volume_config = {
  48. 'root': {
  49. 'name': 'root',
  50. 'snap_on_start': False,
  51. 'save_on_stop': True,
  52. 'rw': True,
  53. 'source': None,
  54. 'size': defaults['root_img_size'],
  55. },
  56. 'private': {
  57. 'name': 'private',
  58. 'snap_on_start': False,
  59. 'save_on_stop': True,
  60. 'rw': True,
  61. 'source': None,
  62. 'size': defaults['private_img_size'],
  63. 'revisions_to_keep': 0,
  64. },
  65. 'volatile': {
  66. 'name': 'volatile',
  67. 'size': defaults['root_img_size'],
  68. 'snap_on_start': False,
  69. 'save_on_stop': False,
  70. 'rw': True,
  71. },
  72. 'kernel': {
  73. 'name': 'kernel',
  74. 'snap_on_start': False,
  75. 'save_on_stop': False,
  76. 'rw': False
  77. }
  78. }
  79. super(TemplateVM, self).__init__(*args, **kwargs)
  80. @qubes.events.handler('property-set:default_user',
  81. 'property-set:kernel',
  82. 'property-set:kernelopts',
  83. 'property-set:vcpus',
  84. 'property-set:memory',
  85. 'property-set:maxmem',
  86. 'property-set:qrexec_timeout',
  87. 'property-set:shutdown_timeout',
  88. 'property-set:management_dispvm')
  89. def on_property_set_child(self, _event, name, newvalue, oldvalue=None):
  90. """Send event about default value change to child VMs
  91. (which use default inherited from the template).
  92. This handler is supposed to be set for properties using
  93. `_default_with_template()` function for the default value.
  94. """
  95. if newvalue == oldvalue:
  96. return
  97. for vm in self.appvms:
  98. if not vm.property_is_default(name):
  99. continue
  100. vm.fire_event('property-reset:' + name, name=name)