QEMU: Difference between revisions

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..."
 
NobbZ (talk | contribs)
Emulate different architecture: Adjusted the build to not rely on reaily set up binfmt/remote builders anymore. Fixed some other problems I had during experiments. Still I was unable to actually see the started VM in virt-man or similar or connect to it, additional info needed!
 
(16 intermediate revisions by 9 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 ==
For running virtual machines as a services see [[Libvirt]].
environment = {
 
== Setup ==
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>
{{Evaluate}}
=== 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
<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
    # Put your actual system of your machine here. In VM terms it is the "HOST"
    buildPlatform = "x86_64-linux";
    # Put the system you want to emulate here. In VM terms it is the "GUEST"
    hostPlatform = "aarch64-linux";
    pkgs = import nixpkgs { system = buildPlatform; };
    vmConfig = (pkgs.nixos {
      imports = [ "${nixpkgs}/nixos/modules/installer/cd-dvd/installation-cd-base.nix" ];
      nixpkgs = { inherit buildPlatform hostPlatform; };
    });
    vmIso = vmConfig.config.system.build.isoImage;
    vmPkgs = vmConfig.pkgs;
    vmScript = pkgs.writeShellScriptBin "run-nixos-vm" ''
      ${pkgs.qemu}/bin/qemu-system-aarch64 \
        -machine virt,gic-version=max \
        -cpu max \
        -m 2G \
        -smp 4 \
        -drive file=$(echo ${vmIso}/iso/*.iso),format=raw,readonly=on \
        -nographic \
        -bios ${vmPkgs.OVMF.fd}/FV/OVMF.fd
    '';
  in {
    packages.x86_64-linux.default = vmScript;
  };
}
</syntaxhighlight>
Alternatively a different iso file can be specified in the drive-parameter, for example for Ubuntu Server ARM64.


== Booting UEFI ==
Make sure to adjust the 2 toplevel variables to set up the cross compilation correctly. If your target is not AARCH64, you have to use the correct qemu binay in the start script.
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.
[[Category:Virtualization]]
environment = {
  (pkgs.writeShellScriptBin "qemu-system-x86_64-uefi" ''
    qemu-system-x86_64 \
      -bios ${pkgs.OVMF.fd}/FV/OVMF.fd \
      "$@"
  '')
};