forked from mirrors/nixpkgs
Merge pull request #178531 from T0astBread/nixostest-custom-partitions
nixos/qemu-vm: allow custom partition- and filesystem layouts
This commit is contained in:
commit
d3e7ff2a70
|
@ -684,6 +684,21 @@ in
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
virtualisation.useDefaultFilesystems =
|
||||||
|
mkOption {
|
||||||
|
type = types.bool;
|
||||||
|
default = true;
|
||||||
|
description =
|
||||||
|
''
|
||||||
|
If enabled, the boot disk of the virtual machine will be
|
||||||
|
formatted and mounted with the default filesystems for
|
||||||
|
testing. Swap devices and LUKS will be disabled.
|
||||||
|
|
||||||
|
If disabled, a root filesystem has to be specified and
|
||||||
|
formatted (for example in the initial ramdisk).
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
virtualisation.efiVars =
|
virtualisation.efiVars =
|
||||||
mkOption {
|
mkOption {
|
||||||
type = types.str;
|
type = types.str;
|
||||||
|
@ -754,13 +769,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 = lib.mkIf (!config.boot.initrd.systemd.enable)
|
boot.initrd.extraUtilsCommands = lib.mkIf (cfg.useDefaultFilesystems && !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 = lib.mkIf (!config.boot.initrd.systemd.enable)
|
boot.initrd.postDeviceCommands = lib.mkIf (cfg.useDefaultFilesystems && !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.
|
||||||
|
@ -930,35 +945,38 @@ in
|
||||||
};
|
};
|
||||||
in
|
in
|
||||||
mkVMOverride (cfg.fileSystems //
|
mkVMOverride (cfg.fileSystems //
|
||||||
{
|
optionalAttrs cfg.useDefaultFilesystems {
|
||||||
"/".device = cfg.bootDevice;
|
"/".device = cfg.bootDevice;
|
||||||
"/".fsType = "ext4";
|
"/".fsType = "ext4";
|
||||||
"/".autoFormat = true;
|
"/".autoFormat = true;
|
||||||
|
} //
|
||||||
"/tmp" = mkIf config.boot.tmpOnTmpfs
|
optionalAttrs config.boot.tmpOnTmpfs {
|
||||||
{ device = "tmpfs";
|
"/tmp" = {
|
||||||
|
device = "tmpfs";
|
||||||
fsType = "tmpfs";
|
fsType = "tmpfs";
|
||||||
neededForBoot = true;
|
neededForBoot = true;
|
||||||
# Sync with systemd's tmp.mount;
|
# Sync with systemd's tmp.mount;
|
||||||
options = [ "mode=1777" "strictatime" "nosuid" "nodev" "size=${toString config.boot.tmpOnTmpfsSize}" ];
|
options = [ "mode=1777" "strictatime" "nosuid" "nodev" "size=${toString config.boot.tmpOnTmpfsSize}" ];
|
||||||
};
|
};
|
||||||
|
} //
|
||||||
"/nix/${if cfg.writableStore then ".ro-store" else "store"}" =
|
optionalAttrs cfg.useNixStoreImage {
|
||||||
mkIf cfg.useNixStoreImage
|
"/nix/${if cfg.writableStore then ".ro-store" else "store"}" = {
|
||||||
{ device = "${lookupDriveDeviceName "nix-store" cfg.qemu.drives}";
|
device = "${lookupDriveDeviceName "nix-store" cfg.qemu.drives}";
|
||||||
neededForBoot = true;
|
neededForBoot = true;
|
||||||
options = [ "ro" ];
|
options = [ "ro" ];
|
||||||
};
|
};
|
||||||
|
} //
|
||||||
"/nix/.rw-store" = mkIf (cfg.writableStore && cfg.writableStoreUseTmpfs)
|
optionalAttrs (cfg.writableStore && cfg.writableStoreUseTmpfs) {
|
||||||
{ fsType = "tmpfs";
|
"/nix/.rw-store" = {
|
||||||
|
fsType = "tmpfs";
|
||||||
options = [ "mode=0755" ];
|
options = [ "mode=0755" ];
|
||||||
neededForBoot = true;
|
neededForBoot = true;
|
||||||
};
|
};
|
||||||
|
} //
|
||||||
"/boot" = mkIf cfg.useBootLoader
|
optionalAttrs cfg.useBootLoader {
|
||||||
# see note [Disk layout with `useBootLoader`]
|
# see note [Disk layout with `useBootLoader`]
|
||||||
{ device = "${lookupDriveDeviceName "boot" cfg.qemu.drives}2"; # 2 for e.g. `vdb2`, as created in `bootDisk`
|
"/boot" = {
|
||||||
|
device = "${lookupDriveDeviceName "boot" cfg.qemu.drives}2"; # 2 for e.g. `vdb2`, as created in `bootDisk`
|
||||||
fsType = "vfat";
|
fsType = "vfat";
|
||||||
noCheck = true; # fsck fails on a r/o filesystem
|
noCheck = true; # fsck fails on a r/o filesystem
|
||||||
};
|
};
|
||||||
|
@ -986,8 +1004,8 @@ in
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
swapDevices = mkVMOverride [ ];
|
swapDevices = (if cfg.useDefaultFilesystems then mkVMOverride else mkDefault) [ ];
|
||||||
boot.initrd.luks.devices = mkVMOverride {};
|
boot.initrd.luks.devices = (if cfg.useDefaultFilesystems then mkVMOverride else mkDefault) {};
|
||||||
|
|
||||||
# Don't run ntpd in the guest. It should get the correct time from KVM.
|
# Don't run ntpd in the guest. It should get the correct time from KVM.
|
||||||
services.timesyncd.enable = false;
|
services.timesyncd.enable = false;
|
||||||
|
|
|
@ -384,6 +384,7 @@ in {
|
||||||
nixpkgs = pkgs.callPackage ../modules/misc/nixpkgs/test.nix { inherit evalMinimalConfig; };
|
nixpkgs = pkgs.callPackage ../modules/misc/nixpkgs/test.nix { inherit evalMinimalConfig; };
|
||||||
node-red = handleTest ./node-red.nix {};
|
node-red = handleTest ./node-red.nix {};
|
||||||
nomad = handleTest ./nomad.nix {};
|
nomad = handleTest ./nomad.nix {};
|
||||||
|
non-default-filesystems = handleTest ./non-default-filesystems.nix {};
|
||||||
noto-fonts = handleTest ./noto-fonts.nix {};
|
noto-fonts = handleTest ./noto-fonts.nix {};
|
||||||
novacomd = handleTestOn ["x86_64-linux"] ./novacomd.nix {};
|
novacomd = handleTestOn ["x86_64-linux"] ./novacomd.nix {};
|
||||||
nsd = handleTest ./nsd.nix {};
|
nsd = handleTest ./nsd.nix {};
|
||||||
|
@ -519,6 +520,7 @@ in {
|
||||||
step-ca = handleTestOn ["x86_64-linux"] ./step-ca.nix {};
|
step-ca = handleTestOn ["x86_64-linux"] ./step-ca.nix {};
|
||||||
strongswan-swanctl = handleTest ./strongswan-swanctl.nix {};
|
strongswan-swanctl = handleTest ./strongswan-swanctl.nix {};
|
||||||
sudo = handleTest ./sudo.nix {};
|
sudo = handleTest ./sudo.nix {};
|
||||||
|
swap-partition = handleTest ./swap-partition.nix {};
|
||||||
sway = handleTest ./sway.nix {};
|
sway = handleTest ./sway.nix {};
|
||||||
switchTest = handleTest ./switch-test.nix {};
|
switchTest = handleTest ./switch-test.nix {};
|
||||||
sympa = handleTest ./sympa.nix {};
|
sympa = handleTest ./sympa.nix {};
|
||||||
|
|
54
nixos/tests/non-default-filesystems.nix
Normal file
54
nixos/tests/non-default-filesystems.nix
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
import ./make-test-python.nix ({ lib, pkgs, ... }:
|
||||||
|
{
|
||||||
|
name = "non-default-filesystems";
|
||||||
|
|
||||||
|
nodes.machine =
|
||||||
|
{ config, pkgs, lib, ... }:
|
||||||
|
let
|
||||||
|
disk = config.virtualisation.bootDevice;
|
||||||
|
in
|
||||||
|
{
|
||||||
|
virtualisation.useDefaultFilesystems = false;
|
||||||
|
|
||||||
|
boot.initrd.availableKernelModules = [ "btrfs" ];
|
||||||
|
boot.supportedFilesystems = [ "btrfs" ];
|
||||||
|
|
||||||
|
boot.initrd.postDeviceCommands = ''
|
||||||
|
FSTYPE=$(blkid -o value -s TYPE ${disk} || true)
|
||||||
|
if test -z "$FSTYPE"; then
|
||||||
|
modprobe btrfs
|
||||||
|
${pkgs.btrfs-progs}/bin/mkfs.btrfs ${disk}
|
||||||
|
|
||||||
|
mkdir /nixos
|
||||||
|
mount -t btrfs ${disk} /nixos
|
||||||
|
|
||||||
|
${pkgs.btrfs-progs}/bin/btrfs subvolume create /nixos/root
|
||||||
|
${pkgs.btrfs-progs}/bin/btrfs subvolume create /nixos/home
|
||||||
|
|
||||||
|
umount /nixos
|
||||||
|
fi
|
||||||
|
'';
|
||||||
|
|
||||||
|
virtualisation.fileSystems = {
|
||||||
|
"/" = {
|
||||||
|
device = disk;
|
||||||
|
fsType = "btrfs";
|
||||||
|
options = [ "subvol=/root" ];
|
||||||
|
};
|
||||||
|
|
||||||
|
"/home" = {
|
||||||
|
device = disk;
|
||||||
|
fsType = "btrfs";
|
||||||
|
options = [ "subvol=/home" ];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
testScript = ''
|
||||||
|
machine.wait_for_unit("multi-user.target")
|
||||||
|
|
||||||
|
with subtest("BTRFS filesystems are mounted correctly"):
|
||||||
|
machine.succeed("grep -E '/dev/vda / btrfs rw,relatime,space_cache=v2,subvolid=[0-9]+,subvol=/root 0 0' /proc/mounts")
|
||||||
|
machine.succeed("grep -E '/dev/vda /home btrfs rw,relatime,space_cache=v2,subvolid=[0-9]+,subvol=/home 0 0' /proc/mounts")
|
||||||
|
'';
|
||||||
|
})
|
48
nixos/tests/swap-partition.nix
Normal file
48
nixos/tests/swap-partition.nix
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
import ./make-test-python.nix ({ lib, pkgs, ... }:
|
||||||
|
{
|
||||||
|
name = "swap-partition";
|
||||||
|
|
||||||
|
nodes.machine =
|
||||||
|
{ config, pkgs, lib, ... }:
|
||||||
|
{
|
||||||
|
virtualisation.useDefaultFilesystems = false;
|
||||||
|
|
||||||
|
virtualisation.bootDevice = "/dev/vda1";
|
||||||
|
|
||||||
|
boot.initrd.postDeviceCommands = ''
|
||||||
|
if ! test -b /dev/vda1; then
|
||||||
|
${pkgs.parted}/bin/parted --script /dev/vda -- mklabel msdos
|
||||||
|
${pkgs.parted}/bin/parted --script /dev/vda -- mkpart primary 1MiB -250MiB
|
||||||
|
${pkgs.parted}/bin/parted --script /dev/vda -- mkpart primary -250MiB 100%
|
||||||
|
sync
|
||||||
|
fi
|
||||||
|
|
||||||
|
FSTYPE=$(blkid -o value -s TYPE /dev/vda1 || true)
|
||||||
|
if test -z "$FSTYPE"; then
|
||||||
|
${pkgs.e2fsprogs}/bin/mke2fs -t ext4 -L root /dev/vda1
|
||||||
|
${pkgs.util-linux}/bin/mkswap --label swap /dev/vda2
|
||||||
|
fi
|
||||||
|
'';
|
||||||
|
|
||||||
|
virtualisation.fileSystems = {
|
||||||
|
"/" = {
|
||||||
|
device = "/dev/disk/by-label/root";
|
||||||
|
fsType = "ext4";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
swapDevices = [
|
||||||
|
{
|
||||||
|
device = "/dev/disk/by-label/swap";
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
|
||||||
|
testScript = ''
|
||||||
|
machine.wait_for_unit("multi-user.target")
|
||||||
|
|
||||||
|
with subtest("Swap is active"):
|
||||||
|
# Doesn't matter if the numbers reported by `free` are slightly off due to unit conversions.
|
||||||
|
machine.succeed("free -h | grep -E 'Swap:\s+2[45][0-9]Mi'")
|
||||||
|
'';
|
||||||
|
})
|
Loading…
Reference in a new issue