]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Add QemuCdrom= option
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Sat, 12 Aug 2023 12:41:52 +0000 (14:41 +0200)
committerJörg Behrmann <behrmann@physik.fu-berlin.de>
Sat, 12 Aug 2023 16:57:03 +0000 (18:57 +0200)
This attaches the image as a CD-ROM instead of a hard drive to the
virtual machine.

mkosi/config.py
mkosi/qemu.py

index 393affa675ee620c4cc878280e739c42cd5aeafb..b2bb4462a61413e581ad9348ea73abc45865d63a 100644 (file)
@@ -736,6 +736,7 @@ class MkosiConfig:
     qemu_kvm: ConfigFeature
     qemu_vsock: ConfigFeature
     qemu_swtpm: ConfigFeature
+    qemu_cdrom: bool
     qemu_args: Sequence[str]
 
     preset: Optional[str]
@@ -1458,6 +1459,14 @@ class MkosiConfigParser:
             parse=config_parse_feature,
             help="Configure whether to use qemu with swtpm or not",
         ),
+        MkosiConfigSetting(
+            dest="qemu_cdrom",
+            metavar="BOOLEAN",
+            nargs="?",
+            section="Host",
+            parse=config_parse_boolean,
+            help="Attach the image as a CD-ROM to the virtual machine",
+        ),
         MkosiConfigSetting(
             dest="qemu_args",
             metavar="ARGS",
@@ -1989,6 +1998,10 @@ def load_kernel_command_line_extra(args: argparse.Namespace) -> list[str]:
     if not any(s.startswith("SYSTEMD_SULOGIN_FORCE=") for s in args.kernel_command_line_extra):
         cmdline += ["SYSTEMD_SULOGIN_FORCE=1"]
 
+    if args.qemu_cdrom:
+        # CD-ROMs are read-only so tell systemd to boot in volatile mode.
+        cmdline += ["systemd.volatile=yes"]
+
     for s in args.kernel_command_line_extra:
         key, sep, value = s.partition("=")
         if " " in value:
@@ -2264,6 +2277,7 @@ Clean Package Manager Metadata: {yes_no_auto(config.clean_package_metadata)}
                   QEMU Use KVM: {config.qemu_kvm}
                 QEMU Use VSock: {config.qemu_vsock}
                 QEMU Use Swtpm: {config.qemu_swtpm}
+               QEMU Use CD-ROM: {yes_no(config.qemu_cdrom)}
           QEMU Extra Arguments: {line_join_list(config.qemu_args)}
 """
 
index 10b9d1b564c881c38a3602c9e1a75ae0192d4241..81cd8a3284316e8d9f91b60eebc9c6e115773604 100644 (file)
@@ -267,21 +267,33 @@ def run_qemu(args: MkosiArgs, config: MkosiConfig) -> None:
                 "-drive", f"file={ovmf_vars.name},if=pflash,format=raw",
             ]
 
-        if config.ephemeral:
+        if config.qemu_cdrom and config.output_format == OutputFormat.disk:
+            # CD-ROM devices have sector size 2048 so we transform the disk image into one with sector size 2048.
+            src = (config.output_dir / config.output).resolve()
+            fname = src.parent / f"{src.name}-{uuid.uuid4().hex}"
+            run(["systemd-repart",
+                 "--definitions", "",
+                 "--no-pager",
+                 "--pretty=no",
+                 "--offline=yes",
+                 "--empty=create",
+                 "--size=auto",
+                 "--sector-size=2048",
+                 "--copy-from", src,
+                 fname])
+        elif config.ephemeral:
             fname = stack.enter_context(copy_ephemeral(config, config.output_dir / config.output))
         else:
             fname = config.output_dir / config.output
 
-        if config.output_format == OutputFormat.disk:
-            run([
-                "systemd-repart",
-                "--definitions", "",
-                "--no-pager",
-                "--size", "8G",
-                "--pretty", "no",
-                "--offline", "yes",
-                fname,
-            ])
+        if config.output_format == OutputFormat.disk and not config.qemu_cdrom:
+            run(["systemd-repart",
+                 "--definitions", "",
+                 "--no-pager",
+                 "--size=8G",
+                 "--pretty=no",
+                 "--offline=yes",
+                 fname])
 
         # Debian images fail to boot with virtio-scsi, see: https://github.com/systemd/mkosi/issues/725
         if config.output_format == OutputFormat.cpio:
@@ -292,9 +304,9 @@ def run_qemu(args: MkosiArgs, config: MkosiConfig) -> None:
                         "-initrd", fname,
                         "-append", " ".join(config.kernel_command_line + config.kernel_command_line_extra)]
 
-        cmdline += ["-drive", f"if=none,id=hd,file={fname},format=raw",
+        cmdline += ["-drive", f"if=none,id=mkosi,file={fname},format=raw",
                     "-device", "virtio-scsi-pci,id=scsi",
-                    "-device", "scsi-hd,drive=hd,bootindex=1"]
+                    "-device", f"scsi-{'cd' if config.qemu_cdrom else 'hd'},drive=mkosi,bootindex=1"]
 
         if config.qemu_swtpm != ConfigFeature.disabled and shutil.which("swtpm") is not None:
             sock = stack.enter_context(start_swtpm())