Skip to content

Commit 3e2de41

Browse files
committed
qemu: Refactor drive handling
1 parent aad5b54 commit 3e2de41

1 file changed

Lines changed: 26 additions & 16 deletions

File tree

lib/qemu.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ def __init__(self, machine):
2727
self.host_command = 'run'
2828
self.gdb = None
2929
self.interactive = False
30-
self.drive = None
30+
self.drives = []
31+
self.next_drive = 0
3132
self.initrd = None
3233
self.compat_rootfs = False
3334
self.boot_func = None
@@ -150,20 +151,17 @@ def apply_defaults(self):
150151
self.user = 'root'
151152

152153
if 'ubuntu' in self.cloud_image:
153-
self.cmdline.append('root=/dev/vda1')
154154
self.prompt = 'root@ubuntu:~#'
155155
elif 'fedora' in self.cloud_image:
156-
self.cmdline.append('root=/dev/vda2')
157156
self.prompt = '\[root@fedora ~\]#'
158157
elif 'debian' in self.cloud_image:
159-
self.cmdline.append('root=/dev/vda2')
160158
self.prompt = 'root@debian:~#'
161159

162160
if self.prompt is None:
163161
# Default prompt for our root disks
164162
self.prompt = "/ #"
165163

166-
if self.initrd is None and self.drive is None and self.cloud_image is None:
164+
if self.initrd is None and len(self.drives) == 0 and self.cloud_image is None:
167165
if self.compat_rootfs or self.qemu_path.endswith('qemu-system-ppc'):
168166
subarch = 'ppc'
169167
elif get_endian(self.vmlinux) == 'little':
@@ -200,6 +198,21 @@ def boot(p, timeout, qconf):
200198

201199
self.boot_func = boot
202200

201+
def add_drive(self, args):
202+
drive_id = self.next_drive
203+
self.next_drive += 1
204+
205+
if self.machine_is('powernv'):
206+
interface = 'none'
207+
self.drives.append(f'-device virtio-blk-pci,drive=drive{drive_id},id=blk{drive_id},bus=pcie.{drive_id}')
208+
else:
209+
interface = 'virtio'
210+
211+
self.drives.append(f'-drive {args},if={interface},id=drive{drive_id}')
212+
213+
# Convert to drive letter
214+
return chr(ord('a') + drive_id)
215+
203216
def prepare_cloud_image(self):
204217
if self.cloud_image is None:
205218
return
@@ -219,16 +232,13 @@ def prepare_cloud_image(self):
219232
else:
220233
format = 'raw'
221234

222-
if self.machine_is('powernv'):
223-
interface = 'none'
224-
self.extra_args.append('-device virtio-blk-pci,drive=drive0,id=blk0,bus=pcie.0')
225-
self.extra_args.append('-device virtio-blk-pci,drive=drive1,id=blk1,bus=pcie.1')
226-
else:
227-
interface = 'virtio'
228-
229-
self.drive = f'-drive file={img_path},format={format},if={interface},id=drive0 '
230-
self.drive += f'-drive file={rdpath}/cloud-init-user-data.img,format=raw,if={interface},readonly=on,id=drive1'
235+
cloud_drive = self.add_drive(f'file={img_path},format={format}')
236+
self.add_drive(f'file={rdpath}/cloud-init-user-data.img,format=raw,readonly=on')
231237

238+
if 'ubuntu' in self.cloud_image:
239+
self.cmdline.insert(0, f'root=/dev/vd{cloud_drive}1')
240+
elif 'fedora' in self.cloud_image or 'debian' in self.cloud_image:
241+
self.cmdline.insert(0, f'root=/dev/vd{cloud_drive}2')
232242

233243
def cmd(self):
234244
logging.info('Using qemu version %s.%s "%s"' % get_qemu_version(self.qemu_path))
@@ -255,8 +265,8 @@ def cmd(self):
255265
l.append('-initrd')
256266
l.append(get_root_disk(self.initrd))
257267

258-
if self.drive:
259-
l.append(self.drive)
268+
if len(self.drives):
269+
l.extend(self.drives)
260270

261271
if self.cpu is not None:
262272
l.append('-cpu')

0 commit comments

Comments
 (0)