From ec44a39a389cb2cb4f515060c307abdf29b9c4b0 Mon Sep 17 00:00:00 2001 From: Will Fancher Date: Sun, 1 Jun 2025 19:06:26 -0400 Subject: [PATCH 1/9] nixos/qemu-vm: Allow configuration of empty disk image qemu devices. --- nixos/modules/virtualisation/qemu-vm.nix | 56 +++++++++++++++++------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/nixos/modules/virtualisation/qemu-vm.nix b/nixos/modules/virtualisation/qemu-vm.nix index 0832ca937d32..0cc1b1146a84 100644 --- a/nixos/modules/virtualisation/qemu-vm.nix +++ b/nixos/modules/virtualisation/qemu-vm.nix @@ -20,25 +20,17 @@ let cfg = config.virtualisation; - opt = options.virtualisation; - qemu = cfg.qemu.package; hostPkgs = cfg.host.pkgs; consoles = lib.concatMapStringsSep " " (c: "console=${c}") cfg.qemu.consoles; - driveOpts = + driveOptions = { ... }: { options = { - - file = mkOption { - type = types.str; - description = "The file image used for this drive."; - }; - driveExtraOpts = mkOption { type = types.attrsOf types.str; default = { }; @@ -299,7 +291,9 @@ let ${lib.pipe cfg.emptyDiskImages [ (lib.imap0 ( - idx: size: '' + idx: + { size, ... }: + '' test -e "empty${builtins.toString idx}.qcow2" || ${qemu}/bin/qemu-img create -f qcow2 "empty${builtins.toString idx}.qcow2" "${builtins.toString size}M" '' )) @@ -477,7 +471,20 @@ in }; virtualisation.emptyDiskImages = mkOption { - type = types.listOf types.ints.positive; + type = types.listOf ( + types.coercedTo types.ints.positive (size: { inherit size; }) ( + types.submodule { + options.size = mkOption { + type = types.ints.positive; + description = "The size of the disk in MiB"; + }; + options.driveConfig = mkOption { + type = lib.types.submodule driveOptions; + description = "Drive configuration to pass to {option}`virtualisation.qemu.drives`"; + }; + } + ) + ); default = [ ]; description = '' Additional disk images to provide to the VM. The value is @@ -829,7 +836,18 @@ in }; drives = mkOption { - type = types.listOf (types.submodule driveOpts); + type = types.listOf ( + types.submodule { + imports = [ driveOptions ]; + + options = { + file = mkOption { + type = types.str; + description = "The file image used for this drive."; + }; + }; + } + ); description = "Drives passed to qemu."; }; @@ -1310,10 +1328,16 @@ in driveExtraOpts.format = "raw"; } ]) - (imap0 (idx: _: { - file = "$(pwd)/empty${toString idx}.qcow2"; - driveExtraOpts.werror = "report"; - }) cfg.emptyDiskImages) + (imap0 ( + idx: imgCfg: + lib.mkMerge [ + { + file = "$(pwd)/empty${toString idx}.qcow2"; + driveExtraOpts.werror = "report"; + } + imgCfg.driveConfig + ] + ) cfg.emptyDiskImages) ]; # By default, use mkVMOverride to enable building test VMs (e.g. via From 35bbf24122dd03522dc281fef72309f206429c48 Mon Sep 17 00:00:00 2001 From: Will Fancher Date: Sat, 16 Aug 2025 14:12:31 -0400 Subject: [PATCH 2/9] nixos/tests/bees: Use autoFormat --- nixos/tests/bees.nix | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/nixos/tests/bees.nix b/nixos/tests/bees.nix index b9e38b385d3c..e13e169bff14 100644 --- a/nixos/tests/bees.nix +++ b/nixos/tests/bees.nix @@ -5,29 +5,33 @@ nodes.machine = { config, pkgs, ... }: { - boot.initrd.postDeviceCommands = '' - ${pkgs.btrfs-progs}/bin/mkfs.btrfs -f -L aux1 /dev/vdb - ${pkgs.btrfs-progs}/bin/mkfs.btrfs -f -L aux2 /dev/vdc - ''; virtualisation.emptyDiskImages = [ - 4096 - 4096 + { + size = 4096; + driveConfig.deviceExtraOpts.serial = "aux1"; + } + { + size = 4096; + driveConfig.deviceExtraOpts.serial = "aux2"; + } ]; virtualisation.fileSystems = { "/aux1" = { # filesystem configured to be deduplicated - device = "/dev/disk/by-label/aux1"; + device = "/dev/disk/by-id/virtio-aux1"; fsType = "btrfs"; + autoFormat = true; }; "/aux2" = { # filesystem not configured to be deduplicated - device = "/dev/disk/by-label/aux2"; + device = "/dev/disk/by-id/virtio-aux2"; fsType = "btrfs"; + autoFormat = true; }; }; services.beesd.filesystems = { aux1 = { - spec = "LABEL=aux1"; + spec = "/dev/disk/by-id/virtio-aux1"; hashTableSizeMB = 16; verbosity = "debug"; }; From d22eef5cfa0cd941529ce3ad4d9b5d91ea791043 Mon Sep 17 00:00:00 2001 From: Will Fancher Date: Sat, 16 Aug 2025 14:12:31 -0400 Subject: [PATCH 3/9] nixos/tests/glusterfs: Use autoFormat --- nixos/tests/glusterfs.nix | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/nixos/tests/glusterfs.nix b/nixos/tests/glusterfs.nix index e8b0a442aedc..9224797704e2 100644 --- a/nixos/tests/glusterfs.nix +++ b/nixos/tests/glusterfs.nix @@ -19,17 +19,18 @@ let networking.firewall.enable = false; services.glusterfs.enable = true; - # create a mount point for the volume - boot.initrd.postDeviceCommands = '' - ${pkgs.e2fsprogs}/bin/mkfs.ext4 -L data /dev/vdb - ''; - - virtualisation.emptyDiskImages = [ 1024 ]; + virtualisation.emptyDiskImages = [ + { + size = 1024; + driveConfig.deviceExtraOpts.serial = "data"; + } + ]; virtualisation.fileSystems = { "/data" = { - device = "/dev/disk/by-label/data"; + device = "/dev/disk/by-id/virtio-data"; fsType = "ext4"; + autoFormat = true; }; }; }; From dc4a8f961ae3c9de85284d8d9a6850280d64f998 Mon Sep 17 00:00:00 2001 From: Will Fancher Date: Sat, 16 Aug 2025 14:12:31 -0400 Subject: [PATCH 4/9] nixos/tests/hardened: Use autoFormat --- nixos/tests/hardened.nix | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/nixos/tests/hardened.nix b/nixos/tests/hardened.nix index 5a11b0a90567..f025e85e3821 100644 --- a/nixos/tests/hardened.nix +++ b/nixos/tests/hardened.nix @@ -24,14 +24,17 @@ imports = [ ../modules/profiles/hardened.nix ]; environment.memoryAllocator.provider = "graphene-hardened"; nix.settings.sandbox = false; - virtualisation.emptyDiskImages = [ 4096 ]; - boot.initrd.postDeviceCommands = '' - ${pkgs.dosfstools}/bin/mkfs.vfat -n EFISYS /dev/vdb - ''; + virtualisation.emptyDiskImages = [ + { + size = 4096; + driveConfig.deviceExtraOpts.serial = "deferred"; + } + ]; virtualisation.fileSystems = { - "/efi" = { - device = "/dev/disk/by-label/EFISYS"; + "/deferred" = { + device = "/dev/disk/by-id/virtio-deferred"; fsType = "vfat"; + autoFormat = true; options = [ "noauto" ]; }; }; @@ -87,10 +90,9 @@ # Test deferred mount with subtest("Deferred mounts work"): - machine.fail("mountpoint -q /efi") # was deferred - machine.execute("mkdir -p /efi") - machine.succeed("mount /dev/disk/by-label/EFISYS /efi") - machine.succeed("mountpoint -q /efi") # now mounted + machine.fail("mountpoint -q /deferred") # was deferred + machine.systemctl("start deferred.mount") + machine.succeed("mountpoint -q /deferred") # now mounted # Test Nix dæmon usage From b9057606a5334caca626dc22277e2b85df25c04b Mon Sep 17 00:00:00 2001 From: Will Fancher Date: Sat, 16 Aug 2025 14:12:31 -0400 Subject: [PATCH 5/9] nixos/tests/moosefs: Use autoFormat --- nixos/tests/moosefs.nix | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/nixos/tests/moosefs.nix b/nixos/tests/moosefs.nix index 3166d34bf14a..9b4ccba08b8a 100644 --- a/nixos/tests/moosefs.nix +++ b/nixos/tests/moosefs.nix @@ -22,15 +22,18 @@ let chunkserver = { pkgs, ... }: { - virtualisation.emptyDiskImages = [ 4096 ]; - boot.initrd.postDeviceCommands = '' - ${pkgs.e2fsprogs}/bin/mkfs.ext4 -L data /dev/vdb - ''; + virtualisation.emptyDiskImages = [ + { + size = 4096; + driveConfig.deviceExtraOpts.serial = "data"; + } + ]; fileSystems = pkgs.lib.mkVMOverride { "/data" = { - device = "/dev/disk/by-label/data"; + device = "/dev/disk/by-id/virtio-data"; fsType = "ext4"; + autoFormat = true; }; }; From e31cb2587707ac6dfeebc048083e11903541c2c5 Mon Sep 17 00:00:00 2001 From: Will Fancher Date: Sat, 16 Aug 2025 14:12:31 -0400 Subject: [PATCH 6/9] nixos/tests/orangefs: Use autoFormat --- nixos/tests/orangefs.nix | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/nixos/tests/orangefs.nix b/nixos/tests/orangefs.nix index fe9335f74981..5c88a481b709 100644 --- a/nixos/tests/orangefs.nix +++ b/nixos/tests/orangefs.nix @@ -5,16 +5,19 @@ let { pkgs, ... }: { networking.firewall.allowedTCPPorts = [ 3334 ]; - boot.initrd.postDeviceCommands = '' - ${pkgs.e2fsprogs}/bin/mkfs.ext4 -L data /dev/vdb - ''; - virtualisation.emptyDiskImages = [ 4096 ]; + virtualisation.emptyDiskImages = [ + { + size = 4096; + driveConfig.deviceExtraOpts.serial = "data"; + } + ]; virtualisation.fileSystems = { "/data" = { - device = "/dev/disk/by-label/data"; + device = "/dev/disk/by-id/virtio-data"; fsType = "ext4"; + autoFormat = true; }; }; From e2e7ac25049773709e07f04003af7fdf8532f936 Mon Sep 17 00:00:00 2001 From: Will Fancher Date: Sat, 16 Aug 2025 14:12:31 -0400 Subject: [PATCH 7/9] nixos/tests/saunafs: Use autoFormat --- nixos/tests/saunafs.nix | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/nixos/tests/saunafs.nix b/nixos/tests/saunafs.nix index cc0c9e941372..0d89fb0c0b3b 100644 --- a/nixos/tests/saunafs.nix +++ b/nixos/tests/saunafs.nix @@ -20,15 +20,18 @@ let chunkserver = { pkgs, ... }: { - virtualisation.emptyDiskImages = [ 4096 ]; - boot.initrd.postDeviceCommands = '' - ${pkgs.e2fsprogs}/bin/mkfs.ext4 -L data /dev/vdb - ''; + virtualisation.emptyDiskImages = [ + { + size = 4096; + driveConfig.deviceExtraOpts.serial = "data"; + } + ]; fileSystems = pkgs.lib.mkVMOverride { "/data" = { - device = "/dev/disk/by-label/data"; + device = "/dev/disk/by-id/virtio-data"; fsType = "ext4"; + autoFormat = true; }; }; From bb51c2d8fe4ba70d5400932f625c1e828708a60c Mon Sep 17 00:00:00 2001 From: Will Fancher Date: Sat, 16 Aug 2025 14:12:31 -0400 Subject: [PATCH 8/9] nixos/tests/snapper: Use autoFormat --- nixos/tests/snapper.nix | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/nixos/tests/snapper.nix b/nixos/tests/snapper.nix index 4a03b85cc71d..b8a86c51d694 100644 --- a/nixos/tests/snapper.nix +++ b/nixos/tests/snapper.nix @@ -5,16 +5,18 @@ nodes.machine = { pkgs, lib, ... }: { - boot.initrd.postDeviceCommands = '' - ${pkgs.btrfs-progs}/bin/mkfs.btrfs -f -L aux /dev/vdb - ''; - - virtualisation.emptyDiskImages = [ 4096 ]; + virtualisation.emptyDiskImages = [ + { + size = 4096; + driveConfig.deviceExtraOpts.serial = "aux"; + } + ]; virtualisation.fileSystems = { "/home" = { - device = "/dev/disk/by-label/aux"; + device = "/dev/disk/by-id/virtio-aux"; fsType = "btrfs"; + autoFormat = true; }; }; services.snapper.configs.home.SUBVOLUME = "/home"; From 328b6d874bd3c7fb4b9a327e73b1af072a41de44 Mon Sep 17 00:00:00 2001 From: Will Fancher Date: Sat, 16 Aug 2025 14:12:31 -0400 Subject: [PATCH 9/9] nixos/tests/swap-file-btrfs: Use autoFormat --- nixos/tests/swap-file-btrfs.nix | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/nixos/tests/swap-file-btrfs.nix b/nixos/tests/swap-file-btrfs.nix index d074a781ce0a..620639ae893f 100644 --- a/nixos/tests/swap-file-btrfs.nix +++ b/nixos/tests/swap-file-btrfs.nix @@ -5,20 +5,18 @@ meta.maintainers = with lib.maintainers; [ oxalica ]; nodes.machine = - { pkgs, ... }: + { config, pkgs, ... }: { virtualisation.useDefaultFilesystems = false; virtualisation.rootDevice = "/dev/vda"; - boot.initrd.postDeviceCommands = '' - ${pkgs.btrfs-progs}/bin/mkfs.btrfs --label root /dev/vda - ''; - + boot.initrd.systemd.enable = true; virtualisation.fileSystems = { "/" = { - device = "/dev/disk/by-label/root"; + device = config.virtualisation.rootDevice; fsType = "btrfs"; + autoFormat = true; }; };