1{ system ? builtins.currentSystem,
2 config ? {},
3 pkgs ? import ../.. { inherit system config; }
4}:
5
6with import ../lib/testing-python.nix { inherit system pkgs; };
7
8let
9
10 makeZfsTest =
11 { kernelPackages
12 , enableSystemdStage1 ? false
13 , zfsPackage
14 , extraTest ? ""
15 }:
16 makeTest {
17 name = zfsPackage.kernelModuleAttribute;
18 meta = with pkgs.lib.maintainers; {
19 maintainers = [ elvishjerricco ];
20 };
21
22 nodes.machine = { config, pkgs, lib, ... }:
23 let
24 usersharePath = "/var/lib/samba/usershares";
25 in {
26 virtualisation = {
27 emptyDiskImages = [ 4096 4096 ];
28 useBootLoader = true;
29 useEFIBoot = true;
30 };
31 boot.loader.systemd-boot.enable = true;
32 boot.loader.timeout = 0;
33 boot.loader.efi.canTouchEfiVariables = true;
34 networking.hostId = "deadbeef";
35 boot.kernelPackages = kernelPackages;
36 boot.zfs.package = zfsPackage;
37 boot.supportedFilesystems = [ "zfs" ];
38 boot.initrd.systemd.enable = enableSystemdStage1;
39
40 environment.systemPackages = [ pkgs.parted ];
41
42 # /dev/disk/by-id doesn't get populated in the NixOS test framework
43 boot.zfs.devNodes = "/dev/disk/by-uuid";
44
45 specialisation.samba.configuration = {
46 services.samba = {
47 enable = true;
48 extraConfig = ''
49 registry shares = yes
50 usershare path = ${usersharePath}
51 usershare allow guests = yes
52 usershare max shares = 100
53 usershare owner only = no
54 '';
55 };
56 systemd.services.samba-smbd.serviceConfig.ExecStartPre =
57 "${pkgs.coreutils}/bin/mkdir -m +t -p ${usersharePath}";
58 virtualisation.fileSystems = {
59 "/tmp/mnt" = {
60 device = "rpool/root";
61 fsType = "zfs";
62 };
63 };
64 };
65
66 specialisation.encryption.configuration = {
67 boot.zfs.requestEncryptionCredentials = [ "automatic" ];
68 virtualisation.fileSystems."/automatic" = {
69 device = "automatic";
70 fsType = "zfs";
71 };
72 virtualisation.fileSystems."/manual" = {
73 device = "manual";
74 fsType = "zfs";
75 };
76 virtualisation.fileSystems."/manual/encrypted" = {
77 device = "manual/encrypted";
78 fsType = "zfs";
79 options = [ "noauto" ];
80 };
81 virtualisation.fileSystems."/manual/httpkey" = {
82 device = "manual/httpkey";
83 fsType = "zfs";
84 options = [ "noauto" ];
85 };
86 };
87
88 specialisation.forcepool.configuration = {
89 systemd.services.zfs-import-forcepool.wantedBy = lib.mkVMOverride [ "forcepool.mount" ];
90 systemd.targets.zfs.wantedBy = lib.mkVMOverride [];
91 boot.zfs.forceImportAll = true;
92 virtualisation.fileSystems."/forcepool" = {
93 device = "forcepool";
94 fsType = "zfs";
95 options = [ "noauto" ];
96 };
97 };
98
99 services.nginx = {
100 enable = true;
101 virtualHosts = {
102 localhost = {
103 locations = {
104 "/zfskey" = {
105 return = ''200 "httpkeyabc"'';
106 };
107 };
108 };
109 };
110 };
111 };
112
113 testScript = ''
114 machine.wait_for_unit("multi-user.target")
115 machine.succeed(
116 "zpool status",
117 "parted --script /dev/vdb mklabel msdos",
118 "parted --script /dev/vdb -- mkpart primary 1024M -1s",
119 "parted --script /dev/vdc mklabel msdos",
120 "parted --script /dev/vdc -- mkpart primary 1024M -1s",
121 )
122
123 with subtest("sharesmb works"):
124 machine.succeed(
125 "zpool create rpool /dev/vdb1",
126 "zfs create -o mountpoint=legacy rpool/root",
127 # shared datasets cannot have legacy mountpoint
128 "zfs create rpool/shared_smb",
129 "bootctl set-default nixos-generation-1-specialisation-samba.conf",
130 "sync",
131 )
132 machine.crash()
133 machine.wait_for_unit("multi-user.target")
134 machine.succeed("zfs set sharesmb=on rpool/shared_smb")
135 machine.succeed(
136 "smbclient -gNL localhost | grep rpool_shared_smb",
137 "umount /tmp/mnt",
138 "zpool destroy rpool",
139 )
140
141 with subtest("encryption works"):
142 machine.succeed(
143 'echo password | zpool create -O mountpoint=legacy '
144 + "-O encryption=aes-256-gcm -O keyformat=passphrase automatic /dev/vdb1",
145 "zpool create -O mountpoint=legacy manual /dev/vdc1",
146 "echo otherpass | zfs create "
147 + "-o encryption=aes-256-gcm -o keyformat=passphrase manual/encrypted",
148 "zfs create -o encryption=aes-256-gcm -o keyformat=passphrase "
149 + "-o keylocation=http://localhost/zfskey manual/httpkey",
150 "bootctl set-default nixos-generation-1-specialisation-encryption.conf",
151 "sync",
152 "zpool export automatic",
153 "zpool export manual",
154 )
155 machine.crash()
156 machine.start()
157 machine.wait_for_console_text("Starting password query on")
158 machine.send_console("password\n")
159 machine.wait_for_unit("multi-user.target")
160 machine.succeed(
161 "zfs get -Ho value keystatus manual/encrypted | grep -Fx unavailable",
162 "echo otherpass | zfs load-key manual/encrypted",
163 "systemctl start manual-encrypted.mount",
164 "zfs load-key manual/httpkey",
165 "systemctl start manual-httpkey.mount",
166 "umount /automatic /manual/encrypted /manual/httpkey /manual",
167 "zpool destroy automatic",
168 "zpool destroy manual",
169 )
170
171 with subtest("boot.zfs.forceImportAll works"):
172 machine.succeed(
173 "rm /etc/hostid",
174 "zgenhostid deadcafe",
175 "zpool create forcepool /dev/vdb1 -O mountpoint=legacy",
176 "bootctl set-default nixos-generation-1-specialisation-forcepool.conf",
177 "rm /etc/hostid",
178 "sync",
179 )
180 machine.crash()
181 machine.wait_for_unit("multi-user.target")
182 machine.fail("zpool import forcepool")
183 machine.succeed(
184 "systemctl start forcepool.mount",
185 "mount | grep forcepool",
186 )
187 '' + extraTest;
188
189 };
190
191
192in {
193
194 # maintainer: @raitobezarius
195 series_2_1 = makeZfsTest {
196 zfsPackage = pkgs.zfs_2_1;
197 kernelPackages = pkgs.linuxPackages;
198 };
199
200 series_2_2 = makeZfsTest {
201 zfsPackage = pkgs.zfs_2_2;
202 kernelPackages = pkgs.linuxPackages;
203 };
204
205 unstable = makeZfsTest rec {
206 zfsPackage = pkgs.zfs_unstable;
207 kernelPackages = zfsPackage.latestCompatibleLinuxPackages;
208 };
209
210 unstableWithSystemdStage1 = makeZfsTest rec {
211 zfsPackage = pkgs.zfs_unstable;
212 kernelPackages = zfsPackage.latestCompatibleLinuxPackages;
213 enableSystemdStage1 = true;
214 };
215
216 installerBoot = (import ./installer.nix { }).separateBootZfs;
217 installer = (import ./installer.nix { }).zfsroot;
218
219 expand-partitions = makeTest {
220 name = "multi-disk-zfs";
221 nodes = {
222 machine = { pkgs, ... }: {
223 environment.systemPackages = [ pkgs.parted ];
224 boot.supportedFilesystems = [ "zfs" ];
225 networking.hostId = "00000000";
226
227 virtualisation = {
228 emptyDiskImages = [ 20480 20480 20480 20480 20480 20480 ];
229 };
230
231 specialisation.resize.configuration = {
232 services.zfs.expandOnBoot = [ "tank" ];
233 };
234 };
235 };
236
237 testScript = { nodes, ... }:
238 ''
239 start_all()
240 machine.wait_for_unit("default.target")
241 print(machine.succeed('mount'))
242
243 print(machine.succeed('parted --script /dev/vdb -- mklabel gpt'))
244 print(machine.succeed('parted --script /dev/vdb -- mkpart primary 1M 70M'))
245
246 print(machine.succeed('parted --script /dev/vdc -- mklabel gpt'))
247 print(machine.succeed('parted --script /dev/vdc -- mkpart primary 1M 70M'))
248
249 print(machine.succeed('zpool create tank mirror /dev/vdb1 /dev/vdc1 mirror /dev/vdd /dev/vde mirror /dev/vdf /dev/vdg'))
250 print(machine.succeed('zpool list -v'))
251 print(machine.succeed('mount'))
252 start_size = int(machine.succeed('df -k --output=size /tank | tail -n1').strip())
253
254 print(machine.succeed("/run/current-system/specialisation/resize/bin/switch-to-configuration test >&2"))
255 machine.wait_for_unit("zpool-expand-pools.service")
256 machine.wait_for_unit("zpool-expand@tank.service")
257
258 print(machine.succeed('zpool list -v'))
259 new_size = int(machine.succeed('df -k --output=size /tank | tail -n1').strip())
260
261 if (new_size - start_size) > 20000000:
262 print("Disk grew appropriately.")
263 else:
264 print(f"Disk went from {start_size} to {new_size}, which doesn't seem right.")
265 exit(1)
266 '';
267 };
268}