qvm-template-postprocess: resize volume only when needed

If needs to be extended - do it before import. If needs to be reduced -
after. This way, if data import fails for any reason, previous data
won't be destroyed (truncated).
Also, convert error on shrinking volume to a warning, as it doesn't break
the template (just leave it with bigger disk than needed). Currently all
storage pool implementations refuse to shrink a volume (but it may
change in the future).

QubesOS/qubes-issues#3169
This commit is contained in:
Marek Marczykowski-Górecki 2018-03-19 11:40:58 +01:00
parent b9b9eb1f3b
commit 562137c36d
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
2 changed files with 48 additions and 6 deletions

View File

@ -57,6 +57,18 @@ class TC_00_qvm_template_postprocess(qubesadmin.tests.QubesTestCase):
self.app.expected_calls[('test-vm', 'admin.vm.volume.List', None,
None)] = \
b'0\0root\nprivate\nvolatile\nkernel\n'
self.app.expected_calls[
('test-vm', 'admin.vm.volume.Info', 'root', None)] = \
b'0\x00pool=lvm\n' \
b'vid=qubes_dom0/vm-test-vm-root\n' \
b'size=10737418240\n' \
b'usage=0\n' \
b'rw=True\n' \
b'source=\n' \
b'save_on_stop=True\n' \
b'snap_on_start=False\n' \
b'revisions_to_keep=3\n' \
b'is_outdated=False\n'
self.app.expected_calls[('test-vm', 'admin.vm.volume.Resize', 'root',
str(len(volume_data)).encode())] = \
b'0\0'
@ -82,6 +94,18 @@ class TC_00_qvm_template_postprocess(qubesadmin.tests.QubesTestCase):
self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
b'0\0test-vm class=TemplateVM state=Halted\n'
self.app.expected_calls[
('test-vm', 'admin.vm.volume.Info', 'root', None)] = \
b'0\x00pool=lvm\n' \
b'vid=qubes_dom0/vm-test-vm-root\n' \
b'size=10737418240\n' \
b'usage=0\n' \
b'rw=True\n' \
b'source=\n' \
b'save_on_stop=True\n' \
b'snap_on_start=False\n' \
b'revisions_to_keep=3\n' \
b'is_outdated=False\n'
self.app.expected_calls[('test-vm', 'admin.vm.volume.List', None,
None)] = \
b'0\0root\nprivate\nvolatile\nkernel\n'
@ -114,17 +138,22 @@ class TC_00_qvm_template_postprocess(qubesadmin.tests.QubesTestCase):
b'0\0root\nprivate\nvolatile\nkernel\n'
self.app.expected_calls[
('test-vm', 'admin.vm.volume.Info', 'root', None)] = \
b'0\0pool=default\nvid=vm-templates/test-vm/root\n'
b'0\x00pool=default\n' \
b'vid=vm-templates/test-vm/root\n' \
b'size=10737418240\n' \
b'usage=0\n' \
b'rw=True\n' \
b'source=\n' \
b'save_on_stop=True\n' \
b'snap_on_start=False\n' \
b'revisions_to_keep=3\n' \
b'is_outdated=False\n'
self.app.expected_calls[
('dom0', 'admin.pool.List', None, None)] = \
b'0\0default\n'
self.app.expected_calls[
('dom0', 'admin.pool.Info', 'default', None)] = \
b'0\0driver=file\ndir_path=' + self.source_dir.name.encode() + b'\n'
self.app.expected_calls[
('test-vm', 'admin.vm.volume.Resize', 'root',
str(len(volume_data)).encode())] = \
b'0\0'
vm = self.app.domains['test-vm']
qubesadmin.tools.qvm_template_postprocess.import_root_img(

View File

@ -77,8 +77,12 @@ def get_root_img_size(source_dir):
def import_root_img(vm, source_dir):
'''Import root.img into VM object'''
# Try not break existing data in the volume in case of import failure. If
# volume needs to be extended, do it before import, if reduced - after.
root_size = get_root_img_size(source_dir)
vm.volumes['root'].resize(root_size)
if vm.volumes['root'].size < root_size:
vm.volumes['root'].resize(root_size)
root_path = os.path.join(source_dir, 'root.img')
if os.path.exists(root_path + '.part.00'):
@ -108,6 +112,15 @@ def import_root_img(vm, source_dir):
with open(root_path, 'rb') as root_file:
vm.volumes['root'].import_data(stream=root_file)
if vm.volumes['root'].size > root_size:
try:
vm.volumes['root'].resize(root_size)
except qubesadmin.exc.QubesException as err:
vm.log.warning(
'Failed to resize root volume of {} from {} to {} after '
'import: {}'.format(vm.name, vm.volumes['root'].size,
root_size, str(err)))
def import_appmenus(vm, source_dir):
'''Import appmenus settings into VM object (later: GUI VM)'''