Jump to content

QEMU: Difference between revisions

From NixOS Wiki
imported>Rti
Created page with "A generic and open source machine emulator and virtualizer == Install == environment = { systemPackages = [ pkgs.qemu ]; }; == Booting UEFI == To boot UEFI systems us..."
 
Onny (talk | contribs)
 
(12 intermediate revisions by 8 users not shown)
Line 1: Line 1:
A generic and open source machine emulator and virtualizer
[https://www.qemu.org/ QEMU] is a generic and open source machine emulator and virtualizer.


== Install ==
== Setup ==
environment = {
To install the main QEMU program system-wide, add the following to your <code>configuration.nix</code>:<syntaxhighlight lang=nix>
  environment = {
     systemPackages = [ pkgs.qemu ];
     systemPackages = [ pkgs.qemu ];
   };
   };
</syntaxhighlight>


== Booting UEFI ==
{{Evaluate}}
To boot UEFI systems using qemu, the UEFI firmware replacing the BIOS implementation needs to be provided while starting QEMU.


The following installs a script, that always starts QEMU with OVMF firmware implementing UEFI support.
=== Quick EMU ===
environment = {
 
  (pkgs.writeShellScriptBin "qemu-system-x86_64-uefi" ''
Quickly create and run highly optimised desktop virtual machines for Linux, macOS and Windows; with just two commands.
    qemu-system-x86_64 \
 
      -bios ${pkgs.OVMF.fd}/FV/OVMF.fd \
https://github.com/quickemu-project/quickemu
      "$@"
 
  '')
<syntaxhighlight lang=bash>
};
quickget windows 11
quickemu --vm windows-11.conf
</syntaxhighlight>
 
== Configuration ==
 
=== UEFI firmware support ===
To enable UEFI firmware support in Virt-Manager, Libvirt, Gnome-Boxes etc. add following snippet to your system configuration and apply it<syntaxhighlight lang="nix">
systemd.tmpfiles.rules = [ "L+ /var/lib/qemu/firmware - - - - ${pkgs.qemu}/share/qemu/firmware" ];
</syntaxhighlight>
 
=== Run binaries of different architecture ===
Following configuration will enable the emulation of different architectures. For example to run aarch64 and riscv64 binaries on an native x86_64 host, add following part to your system configuration, apply it and reboot your system.<syntaxhighlight lang="nix">
boot.binfmt.emulatedSystems = [
  "aarch64-linux"
  "riscv64-linux"
];
</syntaxhighlight>
 
== Tips and tricks ==
 
=== Emulate different architecture ===
The following [[Flake]] file constructs and executes a NixOS virtual machine with an architecture distinct from that of the host system; in this example, it utilizes aarch64.
 
Save the snippet as <code>flake.nix</code> and run <code>nix run</code> in the same directory to bootup the VM.<syntaxhighlight lang="nix">
{
  description = "Nix flake to build and run a NixOS VM for aarch64";
 
  inputs = {
    nixpkgs.url = "nixpkgs/nixos-24.05";
  };
 
  outputs = { self, nixpkgs }:
  let
    pkgs = import nixpkgs { system = "x86_64-linux"; };
    pkgsAarch64 = import nixpkgs { system = "aarch64-linux"; };
 
    iso = (pkgsAarch64.nixos {
      imports = [ "${nixpkgs}/nixos/modules/installer/cd-dvd/installation-cd-base.nix" ];
    }).config.system.build.isoImage;
 
    vmScript = pkgs.writeScriptBin "run-nixos-vm" ''
      #!${pkgs.runtimeShell}
      ${pkgs.qemu}/bin/qemu-system-aarch64 \
        -machine virt,gic-version=max \
        -cpu max \
        -m 2G \
        -smp 4 \
        -drive file=$(echo ${iso}/iso/*.iso),format=raw,readonly=on \
        -nographic \
        -bios ${pkgsAarch64.OVMF.fd}/FV/QEMU_EFI.fd
    '';
 
  in {
    defaultPackage.x86_64-linux = vmScript;
  };
}
 
</syntaxhighlight>Alternatively a different iso file can be specified in the drive-parameter, for example for Ubuntu Server ARM64.
[[Category:Virtualization]]

Latest revision as of 20:41, 15 December 2024

QEMU is a generic and open source machine emulator and virtualizer.

Setup

To install the main QEMU program system-wide, add the following to your configuration.nix:

  environment = {
    systemPackages = [ pkgs.qemu ];
  };
🟆︎
Tip: In order to affect your NixOS system by your nix-language-specific changes you must first evaluate it:
$ nixos-rebuild switch --use-remote-sudo

Quick EMU

Quickly create and run highly optimised desktop virtual machines for Linux, macOS and Windows; with just two commands.

https://github.com/quickemu-project/quickemu

quickget windows 11
quickemu --vm windows-11.conf

Configuration

UEFI firmware support

To enable UEFI firmware support in Virt-Manager, Libvirt, Gnome-Boxes etc. add following snippet to your system configuration and apply it

systemd.tmpfiles.rules = [ "L+ /var/lib/qemu/firmware - - - - ${pkgs.qemu}/share/qemu/firmware" ];

Run binaries of different architecture

Following configuration will enable the emulation of different architectures. For example to run aarch64 and riscv64 binaries on an native x86_64 host, add following part to your system configuration, apply it and reboot your system.

boot.binfmt.emulatedSystems = [
  "aarch64-linux"
  "riscv64-linux"
];

Tips and tricks

Emulate different architecture

The following Flake file constructs and executes a NixOS virtual machine with an architecture distinct from that of the host system; in this example, it utilizes aarch64.

Save the snippet as flake.nix and run nix run in the same directory to bootup the VM.

{
  description = "Nix flake to build and run a NixOS VM for aarch64";

  inputs = {
    nixpkgs.url = "nixpkgs/nixos-24.05";
  };

  outputs = { self, nixpkgs }:
  let
    pkgs = import nixpkgs { system = "x86_64-linux"; };
    pkgsAarch64 = import nixpkgs { system = "aarch64-linux"; };

    iso = (pkgsAarch64.nixos {
      imports = [ "${nixpkgs}/nixos/modules/installer/cd-dvd/installation-cd-base.nix" ];
    }).config.system.build.isoImage;

    vmScript = pkgs.writeScriptBin "run-nixos-vm" ''
      #!${pkgs.runtimeShell}
      ${pkgs.qemu}/bin/qemu-system-aarch64 \
        -machine virt,gic-version=max \
        -cpu max \
        -m 2G \
        -smp 4 \
        -drive file=$(echo ${iso}/iso/*.iso),format=raw,readonly=on \
        -nographic \
        -bios ${pkgsAarch64.OVMF.fd}/FV/QEMU_EFI.fd
    '';

  in {
    defaultPackage.x86_64-linux = vmScript;
  };
}

Alternatively a different iso file can be specified in the drive-parameter, for example for Ubuntu Server ARM64.