Qemu tips for cdrom, ide and nvme hard drives

Posted on June 1, 2025
Tags: qemu, nix

Qemu’s documentation is a bit scattered around the web.

I managed to follow the nvme documentation but just couldn’t find anything in qemu.org about SATA drives apart from some small snippets in other sections.

Some other examples could be found on StackOverflow.

This blog post regroups snippets geared towards concrete examples to start a Qemu VM with EFI, SATA, NVMe and/or cdrom.

Nix

You can test with:

nix run nixpkgs#qemu -- <options go here>

For EFI though, using the following script will help you to get easily the needed firmware:

let
  nixos-qemu = pkgs.callPackage "${pkgs.path}/nixos/lib/qemu-common.nix" {};
  qemu = nixos-qemu.qemuBinary pkgs.qemu;
in
  pkgs.writeShellScriptBin "script" ''

  ${qemu} ... <options go here>
  '';

We purposely use the facilities provided by qemu-common.nix because all the wiring is done for us already for EFI and some options matching our architecture are set.

CDROM (sr0)

Assuming you have a ISO file available at ./iso. See next section if you want to create an ISO file with nix.

--drive media=cdrom,format=raw,readonly=on,file=./iso

It will show up as sr0.

Iso file in Nix

With nixos-generators:

let
  iso = nixos-generators.nixosGenerate {
    inherit system;
    format = "install-iso";

    modules = [
    ];
  };
in
  <use 'iso' variable here>

EFI

To boot using EFI, add:

--drive if=pflash,format=raw,unit=0,readonly=on,file=${pkgs.OVMF.firmware}

Here I’m using the Nix snippet to get the OVMF firmware.

SATA (sdX) drive

With the following variable:

diskSata1=./diskSata1.qcow2

Create a drive:

${qemu} create -f qcow2 $diskSata1 20G

Then use it as a SATA drive:

--drive format=qcow2,file=$diskSata1,if=none,id=diskSata1 \
--device ide-hd,drive=diskSata1

Pay attention that the values for id= and drive= can be arbitrary but must match.

It will show up as a sdX drive:

$ lsblk
sda

To add another drive as sdb, use the same method but create a new file and replace occurrences of diskSata1 in id= and drive= with another value.

To specify the device name yourself, add serial=<name> to the --device options.

NVMe (nvmeX) drive

The method for NVMe drives is exactly the same as for SATA drives but replace --device ide-hd,... with --device nvme,....

Epilogue

I’m using this in my project Skarabox whose goal is to be the fastest way to install NixOS on host with all bells and whistles included.

Specifically, I’m using a qemu VM to test the installation on a VM.