Skip to content

Commit

Permalink
Fixed bug lp850602.
Browse files Browse the repository at this point in the history
Adding backing file copy operation on kvm block migration.

(cherry picked from commit b9aac11)

Change-Id: Ia2311ef0e65e97bad7e425ad6affdb5f864043ea
  • Loading branch information
Kei Masumoto authored and markmc committed Oct 25, 2011
1 parent 852b930 commit 744a0d8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
12 changes: 9 additions & 3 deletions nova/tests/test_libvirt.py
Expand Up @@ -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
Expand Down Expand Up @@ -835,7 +836,10 @@ def test_get_instance_disk_info_works_correctly(self):
"</devices></domain>")

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)
Expand Down Expand Up @@ -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'])

Expand Down
33 changes: 30 additions & 3 deletions nova/virt/libvirt/connection.py
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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)

Expand Down

0 comments on commit 744a0d8

Please sign in to comment.