From 57394dfebf4c1dc3d18b614f43135f8806985b0b Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Tue, 5 Apr 2016 12:41:26 +0200 Subject: [PATCH] Rebase the qcow2 when starting the VM if needed Ref #466 --- gns3server/modules/qemu/qcow2.py | 2 +- gns3server/modules/qemu/qemu_vm.py | 9 +++++++++ tests/modules/qemu/test_qcow2.py | 3 +-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/gns3server/modules/qemu/qcow2.py b/gns3server/modules/qemu/qcow2.py index 9f0fc462..d9bc438a 100644 --- a/gns3server/modules/qemu/qcow2.py +++ b/gns3server/modules/qemu/qcow2.py @@ -64,7 +64,7 @@ class Qcow2: self.magic, self.version, self.backing_file_offset, self.backing_file_size = struct.unpack_from(struct_format, content) - if self.magic != 1363560955: # The first 4 bytes contain the characters 'Q', 'F', 'I' followed by 0xfb. + if self.magic != 1363560955: # The first 4 bytes contain the characters 'Q', 'F', 'I' followed by 0xfb. raise Qcow2Error("Invalid magic for {}".format(self.path)) @property diff --git a/gns3server/modules/qemu/qemu_vm.py b/gns3server/modules/qemu/qemu_vm.py index 8335cfe9..849c5e1b 100644 --- a/gns3server/modules/qemu/qemu_vm.py +++ b/gns3server/modules/qemu/qemu_vm.py @@ -40,6 +40,7 @@ from ..base_vm import BaseVM from ...schemas.qemu import QEMU_OBJECT_SCHEMA, QEMU_PLATFORMS from ...utils.asyncio import monitor_process from ...utils.images import md5sum +from .qcow2 import Qcow2, Qcow2Error import logging log = logging.getLogger(__name__) @@ -1261,6 +1262,14 @@ class QemuVM(BaseVM): log.info("{} returned with {}".format(qemu_img_path, retcode)) except (OSError, subprocess.SubprocessError) as e: raise QemuError("Could not create {} disk image {}".format(disk_name, e)) + else: + # The disk exists we check if the clone work + try: + qcow2 = Qcow2(disk) + yield from qcow2.rebase(qemu_img_path, disk_image) + except (Qcow2Error, OSError) as e: + raise QemuError("Could not use qcow2 disk image {} for {} {}".format(disk_image, disk_name, e)) + else: disk = disk_image options.extend(["-drive", 'file={},if={},index={},media=disk'.format(disk, interface, disk_index)]) diff --git a/tests/modules/qemu/test_qcow2.py b/tests/modules/qemu/test_qcow2.py index d193c625..a86ecf67 100644 --- a/tests/modules/qemu/test_qcow2.py +++ b/tests/modules/qemu/test_qcow2.py @@ -20,7 +20,7 @@ import pytest import shutil import asyncio -from gns3server.modules.qemu.qcow2 import Qcow2,Qcow2Error +from gns3server.modules.qemu.qcow2 import Qcow2, Qcow2Error def qemu_img(): @@ -65,4 +65,3 @@ def test_rebase(tmpdir, loop): assert qcow2.backing_file == "empty8G.qcow2" loop.run_until_complete(asyncio.async(qcow2.rebase(qemu_img(), str(tmpdir / "empty16G.qcow2")))) assert qcow2.backing_file == str(tmpdir / "empty16G.qcow2") -