Skip to content

Commit b43e62a

Browse files
committed
qemu: Add support for non-qcow cloud images
1 parent 17fe910 commit b43e62a

1 file changed

Lines changed: 30 additions & 19 deletions

File tree

lib/qemu.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,35 @@ def boot(p, timeout, qconf):
198198

199199
self.boot_func = boot
200200

201+
def prepare_cloud_image(self):
202+
if self.cloud_image is None:
203+
return
204+
205+
rdpath = get_root_disk_path()
206+
img_path = f'{rdpath}/{self.cloud_image}'
207+
208+
if self.cloud_image.endswith('.qcow2'):
209+
# Create snapshot image
210+
pid = os.getpid()
211+
dst = f'{rdpath}/qemu-temp-{pid}.img'
212+
cmd = f'qemu-img create -f qcow2 -F qcow2 -b {img_path} {dst}'.split()
213+
subprocess.run(cmd, check=True)
214+
atexit.register(lambda: os.unlink(dst))
215+
img_path = dst
216+
format = 'qcow2'
217+
else:
218+
format = 'raw'
219+
220+
if self.machine_is('powernv'):
221+
interface = 'none'
222+
self.extra_args.append('-device virtio-blk-pci,drive=drive0,id=blk0,bus=pcie.0')
223+
self.extra_args.append('-device virtio-blk-pci,drive=drive1,id=blk1,bus=pcie.1')
224+
else:
225+
interface = 'virtio'
226+
227+
self.drive = f'-drive file={img_path},format={format},if={interface},id=drive0 '
228+
self.drive += f'-drive file={rdpath}/cloud-init-user-data.img,format=raw,if={interface},readonly=on,id=drive1'
229+
201230

202231
def cmd(self):
203232
logging.info('Using qemu version %s.%s "%s"' % get_qemu_version(self.qemu_path))
@@ -344,25 +373,7 @@ def qemu_main(qconf):
344373
logging.error(f"QEMU_HOST_MOUNTS must point to directories. Not found: '{path}'")
345374
return False
346375

347-
if qconf.cloud_image:
348-
# Create snapshot image
349-
rdpath = get_root_disk_path()
350-
src = f'{rdpath}/{qconf.cloud_image}'
351-
pid = os.getpid()
352-
dst = f'{rdpath}/qemu-temp-{pid}.img'
353-
cmd = f'qemu-img create -f qcow2 -F qcow2 -b {src} {dst}'.split()
354-
subprocess.run(cmd, check=True)
355-
atexit.register(lambda: os.unlink(dst))
356-
357-
if qconf.machine_is('powernv'):
358-
interface = 'none'
359-
qconf.extra_args.append('-device virtio-blk-pci,drive=drive0,id=blk0,bus=pcie.0')
360-
qconf.extra_args.append('-device virtio-blk-pci,drive=drive1,id=blk1,bus=pcie.1')
361-
else:
362-
interface = 'virtio'
363-
364-
qconf.drive = f'-drive file={dst},format=qcow2,if={interface},id=drive0 '
365-
qconf.drive += f'-drive file={rdpath}/cloud-init-user-data.img,format=raw,if={interface},readonly=on,id=drive1'
376+
qconf.prepare_cloud_image()
366377

367378
cmd = qconf.cmd()
368379

0 commit comments

Comments
 (0)