vm: reduce code duplication

Move inheriting volume from template to a helper function.

No functional change.

QubesOS/qubes-issues#2256
This commit is contained in:
Marek Marczykowski-Górecki 2017-06-03 12:20:57 +02:00
parent 28737d16ce
commit 63c23c835b
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
3 changed files with 20 additions and 21 deletions

View File

@ -86,16 +86,7 @@ class AppVM(qubes.vm.qubesvm.QubesVM):
for name, conf in self.volume_config.items():
tpl_volume = template.volumes[name]
conf['size'] = tpl_volume.size
conf['pool'] = tpl_volume.pool
has_source = ('source' in conf and conf['source'] is not None)
is_snapshot = 'snap_on_start' in conf and conf['snap_on_start']
if is_snapshot and not has_source:
if tpl_volume.source is not None:
conf['source'] = tpl_volume.source
else:
conf['source'] = tpl_volume.vid
self.config_volume_from_source(conf, tpl_volume)
for name, config in template.volume_config.items():
# in case the template vm has more volumes add them to own

View File

@ -84,17 +84,7 @@ class DispVM(qubes.vm.qubesvm.QubesVM):
for name, conf in self.volume_config.items():
tpl_volume = template.volumes[name]
conf['size'] = tpl_volume.size
conf['pool'] = tpl_volume.pool
has_source = ('source' in conf and conf['source'] is not None)
is_snapshot = 'snap_on_start' in conf and conf['snap_on_start']
if is_snapshot and not has_source:
if tpl_volume.source is not None:
conf['source'] = tpl_volume.source
else:
conf['source'] = tpl_volume.vid
self.config_volume_from_source(conf, tpl_volume)
for name, config in template.volume_config.items():
# in case the template vm has more volumes add them to own

View File

@ -1762,6 +1762,24 @@ class QubesVM(qubes.vm.mix.net.NetVMMixin, qubes.vm.BaseVM):
# helper methods
#
@staticmethod
def config_volume_from_source(volume_config, source):
'''Adjust storage volume config to use given volume as a source'''
volume_config['size'] = source.size
volume_config['pool'] = source.pool
has_source = (
'source' in volume_config and volume_config['source'] is not None)
is_snapshot = 'snap_on_start' in volume_config and volume_config[
'snap_on_start']
if is_snapshot and not has_source:
if source.source is not None:
volume_config['source'] = source.source
else:
volume_config['source'] = source.vid
return volume_config
def relative_path(self, path):
'''Return path relative to py:attr:`dir_path`.