From 744a0d8e7aa2c16fe55098c96cf9e46984838a4f Mon Sep 17 00:00:00 2001 From: Kei Masumoto Date: Wed, 28 Sep 2011 15:05:54 -0400 Subject: [PATCH] Fixed bug lp850602. Adding backing file copy operation on kvm block migration. (cherry picked from commit b9aac1181581b9036c98f5aa493731fdc74be7e1) Change-Id: Ia2311ef0e65e97bad7e425ad6affdb5f864043ea --- nova/tests/test_libvirt.py | 12 +++++++++--- nova/virt/libvirt/connection.py | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/nova/tests/test_libvirt.py b/nova/tests/test_libvirt.py index 8d285901fcc..0f01de8627a 100644 --- a/nova/tests/test_libvirt.py +++ b/nova/tests/test_libvirt.py @@ -794,7 +794,8 @@ def test_pre_block_migration_works_correctly(self): # Test data instance_ref = db.instance_create(self.context, self.test_instance) - dummyjson = '[{"path": "%s/disk", "local_gb": "10G", "type": "raw"}]' + dummyjson = ('[{"path": "%s/disk", "local_gb": "10G",' + ' "type": "raw", "backing_file": ""}]') # Preparing mocks # qemu-img should be mockd since test environment might not have @@ -835,7 +836,10 @@ def test_get_instance_disk_info_works_correctly(self): "") ret = ("image: /test/disk\nfile format: raw\n" - "virtual size: 20G (21474836480 bytes)\ndisk size: 3.1G\n") + "virtual size: 20G (21474836480 bytes)\ndisk size: 3.1G\n" + "disk size: 102M\n" + "cluster_size: 2097152\n" + "backing file: /test/dummy (actual path: /backing/file)\n") # Preparing mocks vdmock = self.mox.CreateMock(libvirt.virDomain) @@ -865,7 +869,9 @@ def fake_lookup(instance_name): info[0]['path'] == '/test/disk' and info[1]['path'] == '/test/disk.local' and info[0]['local_gb'] == '10G' and - info[1]['local_gb'] == '20G') + info[1]['local_gb'] == '20G' and + info[0]['backing_file'] == "" and + info[1]['backing_file'] == "file") db.instance_destroy(self.context, instance_ref['id']) diff --git a/nova/virt/libvirt/connection.py b/nova/virt/libvirt/connection.py index 9290c5ec3ea..00be5458869 100644 --- a/nova/virt/libvirt/connection.py +++ b/nova/virt/libvirt/connection.py @@ -1722,9 +1722,31 @@ def pre_block_migration(self, ctxt, instance_ref, disk_info_json): for info in disk_info: base = os.path.basename(info['path']) - # Get image type and create empty disk image. + # Get image type and create empty disk image, and + # create backing file in case of qcow2. instance_disk = os.path.join(instance_dir, base) - utils.execute('qemu-img', 'create', '-f', info['type'], + if not info['backing_file']: + utils.execute('qemu-img', 'create', '-f', info['type'], + instance_disk, info['local_gb']) + + else: + # Creating backing file follows same way as spawning instances. + backing_file = os.path.join(FLAGS.instances_path, + '_base', info['backing_file']) + + if not os.path.exists(backing_file): + self._cache_image(fn=self._fetch_image, + context=ctxt, + target=info['path'], + fname=info['backing_file'], + cow=FLAGS.use_cow_images, + image_id=instance_ref['image_ref'], + user_id=instance_ref['user_id'], + project_id=instance_ref['project_id'], + size=instance_ref['local_gb']) + + utils.execute('qemu-img', 'create', '-f', info['type'], + '-o', 'backing_file=%s' % backing_file, instance_disk, info['local_gb']) # if image has kernel and ramdisk, just download @@ -1815,12 +1837,17 @@ def get_instance_disk_info(self, ctxt, instance_ref): driver_nodes[cnt].get_properties().get_next().getContent() if disk_type == 'raw': size = int(os.path.getsize(path)) + backing_file = "" else: out, err = utils.execute('qemu-img', 'info', path) size = [i.split('(')[1].split()[0] for i in out.split('\n') if i.strip().find('virtual size') >= 0] size = int(size[0]) + backing_file = [i.split('actual path:')[1].strip()[:-1] + for i in out.split('\n') if 0 <= i.find('backing file')] + backing_file = os.path.basename(backing_file[0]) + # block migration needs same/larger size of empty image on the # destination host. since qemu-img creates bit smaller size image # depending on original image size, fixed value is necessary. @@ -1836,7 +1863,7 @@ def get_instance_disk_info(self, ctxt, instance_ref): break disk_info.append({'type': disk_type, 'path': path, - 'local_gb': size}) + 'local_gb': size, 'backing_file': backing_file}) return utils.dumps(disk_info)