Skip to content

Commit 5a14e98

Browse files
braunerdaandemeyer
authored andcommitted
Add DriveType= setting for qemu root disk device type
Add a new DriveType= setting that allows configuring the device type used for the root disk when booting a virtual machine with qemu. The supported types are virtio-blk (default, preserving existing behavior), virtio-scsi, and nvme. Previously, the only way to use nvme was by manually adding qemu device arguments via QemuArgs=. This makes nvme a first-class option. When Removable= is enabled, the drive type is forced to virtio-scsi regardless of the DriveType= setting, preserving existing behavior. Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 1e439b6 commit 5a14e98

4 files changed

Lines changed: 51 additions & 8 deletions

File tree

mkosi/config.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,12 @@ class Vmm(StrEnum):
404404
vmspawn = enum.auto()
405405

406406

407+
class QemuDiskType(StrEnum):
408+
virtio_blk = enum.auto()
409+
virtio_scsi = enum.auto()
410+
nvme = enum.auto()
411+
412+
407413
class Ssh(StrEnum):
408414
always = enum.auto()
409415
auto = enum.auto()
@@ -2242,6 +2248,7 @@ class Config:
22422248
vsock_cid: int
22432249
tpm: ConfigFeature
22442250
removable: bool
2251+
disk_type: QemuDiskType
22452252
firmware: Firmware
22462253
firmware_variables: Optional[Path]
22472254
linux: Optional[str]
@@ -4346,6 +4353,16 @@ def parse_kernel_module_filter_regexp(p: str) -> str:
43464353
compat_names=("QemuRemovable",),
43474354
scope=SettingScope.main,
43484355
),
4356+
ConfigSetting(
4357+
dest="disk_type",
4358+
name="DiskType",
4359+
section="Runtime",
4360+
parse=config_make_enum_parser(QemuDiskType),
4361+
default=QemuDiskType.virtio_blk,
4362+
choices=QemuDiskType.choices(),
4363+
help="Set the disk type for the root disk in the virtual machine",
4364+
scope=SettingScope.main,
4365+
),
43494366
ConfigSetting(
43504367
dest="firmware",
43514368
section="Runtime",
@@ -6121,6 +6138,7 @@ def credential_transformer(
61216138
Network: enum_transformer,
61226139
KeySource: key_source_transformer,
61236140
Vmm: enum_transformer,
6141+
QemuDiskType: enum_transformer,
61246142
list[UKIProfile]: uki_profile_transformer,
61256143
UnifiedKernelImage: enum_transformer,
61266144
list[ArtifactOutput]: enum_list_transformer,

mkosi/qemu.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
Firmware,
3939
Network,
4040
OutputFormat,
41+
QemuDiskType,
4142
Ssh,
4243
Verb,
4344
VsockCID,
@@ -1201,9 +1202,6 @@ def add_virtiofs_mount(
12011202
sock = stack.enter_context(start_virtiofsd(config, tree.source))
12021203
add_virtiofs_mount(sock, Path("/root/src") / (tree.target or ""), cmdline, credentials)
12031204

1204-
if config.output_format in (OutputFormat.disk, OutputFormat.esp):
1205-
cmdline += ["-device", "virtio-scsi-pci,id=mkosi"]
1206-
12071205
if config.output_format == OutputFormat.cpio:
12081206
cmdline += ["-initrd", fname]
12091207
elif (
@@ -1216,6 +1214,13 @@ def add_virtiofs_mount(
12161214
cmdline += ["-initrd", initrd]
12171215

12181216
if config.output_format in (OutputFormat.disk, OutputFormat.esp):
1217+
disk_type = config.disk_type
1218+
if config.removable:
1219+
disk_type = QemuDiskType.virtio_scsi
1220+
1221+
if disk_type == QemuDiskType.virtio_scsi:
1222+
cmdline += ["-device", "virtio-scsi-pci,id=mkosi"]
1223+
12191224
blockdev = [
12201225
"driver=raw",
12211226
"node-name=mkosi",
@@ -1227,13 +1232,23 @@ def add_virtiofs_mount(
12271232
f"cache.no-flush={yes_no(config.ephemeral)}",
12281233
]
12291234

1230-
device_type = "virtio-blk-pci"
1231-
if config.removable:
1232-
device_type = "scsi-hd,device_id=mkosi,removable=on"
1235+
if disk_type == QemuDiskType.virtio_blk:
1236+
device = "virtio-blk-pci"
1237+
elif disk_type == QemuDiskType.virtio_scsi:
1238+
device = "scsi-hd"
1239+
if config.removable:
1240+
device += ",device_id=mkosi,removable=on"
1241+
elif disk_type == QemuDiskType.nvme:
1242+
device = "nvme,serial=mkosi"
1243+
else:
1244+
# pyright doesn't do exhaustiveness checking so we need this branch explicitly.
1245+
die(f"Unexpected disk type: {disk_type}")
1246+
1247+
device += ",drive=mkosi,bootindex=1"
12331248

12341249
cmdline += [
12351250
"-blockdev", ",".join(blockdev),
1236-
"-device", f"{device_type},drive=mkosi,bootindex=1",
1251+
"-device", device,
12371252
] # fmt: skip
12381253

12391254
if config.tpm == ConfigFeature.enabled or (

mkosi/resources/man/mkosi.1.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1897,7 +1897,14 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`,
18971897

18981898
`Removable=`, `--removable=`
18991899
: Configures whether to attach the image as a removable device when booting
1900-
a virtual machine. Takes a boolean value. Defaults to `no`.
1900+
a virtual machine. Takes a boolean value. Defaults to `no`. When enabled,
1901+
the root disk is always attached as a SCSI device regardless of the
1902+
`DiskType=` setting.
1903+
1904+
`DiskType=`, `--disk-type=`
1905+
: Configures the disk type to use for the root disk when booting a virtual
1906+
machine. Takes one of `virtio-blk`, `virtio-scsi`, or `nvme`. Defaults to
1907+
`virtio-blk`.
19011908

19021909
`Firmware=`, `--firmware=`
19031910
: Configures the virtual machine firmware to use. Takes one of `uefi`,

tests/test_json.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
ManifestFormat,
3636
Network,
3737
OutputFormat,
38+
QemuDiskType,
3839
SecureBootSignTool,
3940
ShimBootloader,
4041
Ssh,
@@ -166,6 +167,7 @@ def test_config() -> None:
166167
"Devicetrees": [
167168
"freescale/imx8mm-verdin-nonwifi-dev.dtb"
168169
],
170+
"DiskType": "virtio-blk",
169171
"Distribution": "fedora",
170172
"Drives": [
171173
{
@@ -482,6 +484,7 @@ def test_config() -> None:
482484
],
483485
dependencies=["dep1"],
484486
distribution=Distribution.fedora,
487+
disk_type=QemuDiskType.virtio_blk,
485488
drives=[
486489
Drive("abc", 200, Path("/foo/bar"), "abc,qed", "red", []),
487490
Drive("abc", 200, None, "", "wcd", []),

0 commit comments

Comments
 (0)