nixos/*qemu*: Add systemd initrd support

This commit is contained in:
Janne Heß 2022-04-30 19:17:57 +02:00
parent 5b47056b7d
commit 007108f04d
No known key found for this signature in database
GPG key ID: 69165158F05265DF
3 changed files with 29 additions and 31 deletions

View file

@ -1,13 +1,13 @@
# Common configuration for virtual machines running under QEMU (using # Common configuration for virtual machines running under QEMU (using
# virtio). # virtio).
{ ... }: { config, lib, ... }:
{ {
boot.initrd.availableKernelModules = [ "virtio_net" "virtio_pci" "virtio_mmio" "virtio_blk" "virtio_scsi" "9p" "9pnet_virtio" ]; boot.initrd.availableKernelModules = [ "virtio_net" "virtio_pci" "virtio_mmio" "virtio_blk" "virtio_scsi" "9p" "9pnet_virtio" ];
boot.initrd.kernelModules = [ "virtio_balloon" "virtio_console" "virtio_rng" ]; boot.initrd.kernelModules = [ "virtio_balloon" "virtio_console" "virtio_rng" ];
boot.initrd.postDeviceCommands = boot.initrd.postDeviceCommands = lib.mkIf (!config.boot.initrd.systemd.enable)
'' ''
# Set the system time from the hardware clock to work around a # Set the system time from the hardware clock to work around a
# bug in qemu-kvm > 1.5.2 (where the VM clock is initialised # bug in qemu-kvm > 1.5.2 (where the VM clock is initialised

View file

@ -65,33 +65,26 @@ in
}; };
}; };
boot.initrd.preDeviceCommands = boot.kernel.sysctl = {
'' "kernel.hung_task_timeout_secs" = 600;
echo 600 > /proc/sys/kernel/hung_task_timeout_secs # Panic on out-of-memory conditions rather than letting the
''; # OOM killer randomly get rid of processes, since this leads
# to failures that are hard to diagnose.
"vm.panic_on_oom" = lib.mkDefault 2;
};
boot.initrd.postDeviceCommands = boot.kernelParams = [
'' "console=${qemu-common.qemuSerialDevice}"
# Using acpi_pm as a clock source causes the guest clock to # Panic if an error occurs in stage 1 (rather than waiting for
# slow down under high host load. This is usually a bad # user intervention).
# thing, but for VM tests it should provide a bit more "panic=1" "boot.panic_on_fail"
# determinism (e.g. if the VM runs at lower speed, then # Using acpi_pm as a clock source causes the guest clock to
# timeouts in the VM should also be delayed). # slow down under high host load. This is usually a bad
echo acpi_pm > /sys/devices/system/clocksource/clocksource0/current_clocksource # thing, but for VM tests it should provide a bit more
''; # determinism (e.g. if the VM runs at lower speed, then
# timeouts in the VM should also be delayed).
boot.postBootCommands = "clock=acpi_pm"
'' ];
# Panic on out-of-memory conditions rather than letting the
# OOM killer randomly get rid of processes, since this leads
# to failures that are hard to diagnose.
echo 2 > /proc/sys/vm/panic_on_oom
'';
# Panic if an error occurs in stage 1 (rather than waiting for
# user intervention).
boot.kernelParams =
[ "console=${qemu-common.qemuSerialDevice}" "panic=1" "boot.panic_on_fail" ];
# `xwininfo' is used by the test driver to query open windows. # `xwininfo' is used by the test driver to query open windows.
environment.systemPackages = [ pkgs.xorg.xwininfo ]; environment.systemPackages = [ pkgs.xorg.xwininfo ];

View file

@ -754,13 +754,13 @@ in
); );
boot.loader.grub.gfxmodeBios = with cfg.resolution; "${toString x}x${toString y}"; boot.loader.grub.gfxmodeBios = with cfg.resolution; "${toString x}x${toString y}";
boot.initrd.extraUtilsCommands = boot.initrd.extraUtilsCommands = lib.mkIf (!config.boot.initrd.systemd.enable)
'' ''
# We need mke2fs in the initrd. # We need mke2fs in the initrd.
copy_bin_and_libs ${pkgs.e2fsprogs}/bin/mke2fs copy_bin_and_libs ${pkgs.e2fsprogs}/bin/mke2fs
''; '';
boot.initrd.postDeviceCommands = boot.initrd.postDeviceCommands = lib.mkIf (!config.boot.initrd.systemd.enable)
'' ''
# If the disk image appears to be empty, run mke2fs to # If the disk image appears to be empty, run mke2fs to
# initialise. # initialise.
@ -770,7 +770,7 @@ in
fi fi
''; '';
boot.initrd.postMountCommands = boot.initrd.postMountCommands = lib.mkIf (!config.boot.initrd.systemd.enable)
'' ''
# Mark this as a NixOS machine. # Mark this as a NixOS machine.
mkdir -p $targetRoot/etc mkdir -p $targetRoot/etc
@ -789,6 +789,11 @@ in
''} ''}
''; '';
systemd.tmpfiles.rules = lib.mkIf config.boot.initrd.systemd.enable [
"f /etc/NIXOS 0644 root root -"
"d /boot 0644 root root -"
];
# After booting, register the closure of the paths in # After booting, register the closure of the paths in
# `virtualisation.additionalPaths' in the Nix database in the VM. This # `virtualisation.additionalPaths' in the Nix database in the VM. This
# allows Nix operations to work in the VM. The path to the # allows Nix operations to work in the VM. The path to the