QEMU: Difference between revisions
m link to qemu added |
Emulate different architectures |
||
| Line 1: | Line 1: | ||
[https://www.qemu.org/ QEMU] is a generic and open source machine emulator and virtualizer. | [https://www.qemu.org/ QEMU] is a generic and open source machine emulator and virtualizer. | ||
== | == Setup == | ||
<syntaxhighlight lang=nix> | <syntaxhighlight lang=nix> | ||
| Line 20: | Line 20: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Booting UEFI == | == Configuration == | ||
=== Booting UEFI === | |||
To boot UEFI systems using QEMU, the UEFI firmware replacing the BIOS implementation needs to be provided while starting QEMU. | To boot UEFI systems using QEMU, the UEFI firmware replacing the BIOS implementation needs to be provided while starting QEMU. | ||
| Line 39: | Line 41: | ||
qcow-efi images generated from [https://github.com/nix-community/nixos-generators nixos-generators] require more RAM than the default 128MB. Failing to provide enough RAM results in grub reporting "error: start_image() returned 0x800000000000009." or systemd-boot reporting "Failed to execute NixOS: Out of resources". | qcow-efi images generated from [https://github.com/nix-community/nixos-generators nixos-generators] require more RAM than the default 128MB. Failing to provide enough RAM results in grub reporting "error: start_image() returned 0x800000000000009." or systemd-boot reporting "Failed to execute NixOS: Out of resources". | ||
== 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> | |||
[[Category:Virtualization]] | [[Category:Virtualization]] | ||