nixos/tests/zfs: Represent real world usage better

It's better to utilize the boot process and systemd mechanisms to test
these zfs features, rather than manually simulating the same behavior
with testScript.
This commit is contained in:
Will Fancher
2022-12-28 22:42:37 -05:00
parent a45968c1e4
commit 22b6f785a7
+92 -64
View File
@@ -17,103 +17,131 @@ let
makeTest { makeTest {
name = "zfs-" + name; name = "zfs-" + name;
meta = with pkgs.lib.maintainers; { meta = with pkgs.lib.maintainers; {
maintainers = [ adisbladis ]; maintainers = [ adisbladis elvishjerricco ];
}; };
nodes.machine = { pkgs, lib, ... }: nodes.machine = { pkgs, lib, ... }:
let let
usersharePath = "/var/lib/samba/usershares"; usersharePath = "/var/lib/samba/usershares";
in { in {
virtualisation.emptyDiskImages = [ 4096 ]; virtualisation = {
emptyDiskImages = [ 4096 ];
useBootLoader = true;
useEFIBoot = true;
};
boot.loader.systemd-boot.enable = true;
boot.loader.timeout = 0;
boot.loader.efi.canTouchEfiVariables = true;
networking.hostId = "deadbeef"; networking.hostId = "deadbeef";
boot.kernelPackages = kernelPackage; boot.kernelPackages = kernelPackage;
boot.supportedFilesystems = [ "zfs" ]; boot.supportedFilesystems = [ "zfs" ];
boot.zfs.enableUnstable = enableUnstable; boot.zfs.enableUnstable = enableUnstable;
services.samba = {
enable = true;
extraConfig = ''
registry shares = yes
usershare path = ${usersharePath}
usershare allow guests = yes
usershare max shares = 100
usershare owner only = no
'';
};
systemd.services.samba-smbd.serviceConfig.ExecStartPre =
"${pkgs.coreutils}/bin/mkdir -m +t -p ${usersharePath}";
environment.systemPackages = [ pkgs.parted ]; environment.systemPackages = [ pkgs.parted ];
# Setup regular fileSystems machinery to ensure forceImportAll can be # /dev/disk/by-id doesn't get populated in the NixOS test framework
# tested via the regular service units. boot.zfs.devNodes = "/dev/disk/by-uuid";
virtualisation.fileSystems = {
"/forcepool" = { specialisation.samba.configuration = {
services.samba = {
enable = true;
extraConfig = ''
registry shares = yes
usershare path = ${usersharePath}
usershare allow guests = yes
usershare max shares = 100
usershare owner only = no
'';
};
systemd.services.samba-smbd.serviceConfig.ExecStartPre =
"${pkgs.coreutils}/bin/mkdir -m +t -p ${usersharePath}";
virtualisation.fileSystems = {
"/tmp/mnt" = {
device = "rpool/root";
fsType = "zfs";
};
};
};
specialisation.encryption.configuration = {
virtualisation.fileSystems."/automatic" = {
device = "automatic";
fsType = "zfs";
};
};
specialisation.forcepool.configuration = {
systemd.services.zfs-import-forcepool.wantedBy = lib.mkVMOverride [ "forcepool.mount" ];
systemd.targets.zfs.wantedBy = lib.mkVMOverride [];
boot.zfs.forceImportAll = true;
virtualisation.fileSystems."/forcepool" = {
device = "forcepool"; device = "forcepool";
fsType = "zfs"; fsType = "zfs";
options = [ "noauto" ]; options = [ "noauto" ];
}; };
}; };
# forcepool doesn't exist at first boot, and we need to manually test
# the import after tweaking the hostId.
systemd.services.zfs-import-forcepool.wantedBy = lib.mkVMOverride [];
systemd.targets.zfs.wantedBy = lib.mkVMOverride [];
boot.zfs.forceImportAll = true;
# /dev/disk/by-id doesn't get populated in the NixOS test framework
boot.zfs.devNodes = "/dev/disk/by-uuid";
}; };
testScript = '' testScript = ''
machine.wait_for_unit("multi-user.target")
machine.succeed( machine.succeed(
"modprobe zfs",
"zpool status", "zpool status",
"ls /dev", "parted --script /dev/vdc mklabel msdos",
"mkdir /tmp/mnt", "parted --script /dev/vdc -- mkpart primary 1024M -1s",
"udevadm settle",
"parted --script /dev/vdb mklabel msdos",
"parted --script /dev/vdb -- mkpart primary 1024M -1s",
"udevadm settle",
"zpool create rpool /dev/vdb1",
"zfs create -o mountpoint=legacy rpool/root",
# shared datasets cannot have legacy mountpoint
"zfs create rpool/shared_smb",
"mount -t zfs rpool/root /tmp/mnt",
"udevadm settle",
# wait for samba services
"systemctl is-system-running --wait",
"zfs set sharesmb=on rpool/shared_smb",
"zfs share rpool/shared_smb",
"smbclient -gNL localhost | grep rpool_shared_smb",
"umount /tmp/mnt",
"zpool destroy rpool",
"udevadm settle",
) )
machine.succeed( with subtest("sharesmb works"):
'echo password | zpool create -o altroot="/tmp/mnt" ' machine.succeed(
+ "-O encryption=aes-256-gcm -O keyformat=passphrase rpool /dev/vdb1", "zpool create rpool /dev/vdc1",
"zfs create -o mountpoint=legacy rpool/root", "zfs create -o mountpoint=legacy rpool/root",
"mount -t zfs rpool/root /tmp/mnt", # shared datasets cannot have legacy mountpoint
"udevadm settle", "zfs create rpool/shared_smb",
"umount /tmp/mnt", "bootctl set-default nixos-generation-1-specialisation-samba.conf",
"zpool destroy rpool", "sync",
"udevadm settle", )
) machine.crash()
machine.wait_for_unit("multi-user.target")
machine.succeed(
"zfs set sharesmb=on rpool/shared_smb",
"zfs share rpool/shared_smb",
"smbclient -gNL localhost | grep rpool_shared_smb",
"umount /tmp/mnt",
"zpool destroy rpool",
)
with subtest("encryption works"):
machine.succeed(
'echo password | zpool create -O mountpoint=legacy '
+ "-O encryption=aes-256-gcm -O keyformat=passphrase automatic /dev/vdc1",
"bootctl set-default nixos-generation-1-specialisation-encryption.conf",
"sync",
"zpool export automatic",
)
machine.crash()
machine.start()
machine.wait_for_console_text("Starting password query on")
machine.send_console("password\n")
machine.wait_for_unit("multi-user.target")
machine.succeed(
"umount /automatic",
"zpool destroy automatic",
)
with subtest("boot.zfs.forceImportAll works"): with subtest("boot.zfs.forceImportAll works"):
machine.succeed( machine.succeed(
"rm /etc/hostid", "rm /etc/hostid",
"zgenhostid deadcafe", "zgenhostid deadcafe",
"zpool create forcepool /dev/vdb1 -O mountpoint=legacy", "zpool create forcepool /dev/vdc1 -O mountpoint=legacy",
"bootctl set-default nixos-generation-1-specialisation-forcepool.conf",
"rm /etc/hostid",
"sync",
) )
machine.shutdown() machine.crash()
machine.start() machine.wait_for_unit("multi-user.target")
machine.succeed("udevadm settle")
machine.fail("zpool import forcepool") machine.fail("zpool import forcepool")
machine.succeed( machine.succeed(
"systemctl start zfs-import-forcepool.service", "systemctl start forcepool.mount",
"mount -t zfs forcepool /tmp/mnt", "mount | grep forcepool",
) )
'' + extraTest; '' + extraTest;