vuls: init at 0.27.0
[NixPkgs.git] / nixos / tests / zfs.nix
blob13db2c8e06be6ee3917088f0ed0eba8fa35478a5
1 { system ? builtins.currentSystem,
2   config ? {},
3   pkgs ? import ../.. { inherit system config; }
4 }:
6 with import ../lib/testing-python.nix { inherit system pkgs; };
8 let
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       };
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;
40         environment.systemPackages = [ pkgs.parted ];
42         # /dev/disk/by-id doesn't get populated in the NixOS test framework
43         boot.zfs.devNodes = "/dev/disk/by-uuid";
45         specialisation.samba.configuration = {
46           services.samba = {
47             enable = true;
48             settings.global = {
49               "registry shares" = true;
50               "usershare path" = "${usersharePath}";
51               "usershare allow guests" = true;
52               "usershare max shares" = "100";
53               "usershare owner only" = false;
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         };
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         };
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         };
99         services.nginx = {
100           enable = true;
101           virtualHosts = {
102             localhost = {
103               locations = {
104                 "/zfskey" = {
105                   return = ''200 "httpkeyabc"'';
106                 };
107               };
108             };
109           };
110         };
111       };
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         )
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             )
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             )
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;
189     };
192 in {
194   # maintainer: @raitobezarius
195   series_2_1 = makeZfsTest {
196     zfsPackage = pkgs.zfs_2_1;
197     kernelPackages = pkgs.linuxPackages;
198   };
200   series_2_2 = makeZfsTest {
201     zfsPackage = pkgs.zfs_2_2;
202     kernelPackages = pkgs.linuxPackages;
203   };
205   unstable = makeZfsTest rec {
206     zfsPackage = pkgs.zfs_unstable;
207     kernelPackages = pkgs.linuxPackages;
208   };
210   unstableWithSystemdStage1 = makeZfsTest rec {
211     zfsPackage = pkgs.zfs_unstable;
212     kernelPackages = pkgs.linuxPackages;
213     enableSystemdStage1 = true;
214   };
216   installerBoot = (import ./installer.nix { inherit system; }).separateBootZfs;
217   installer = (import ./installer.nix { inherit system; }).zfsroot;
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";
227         virtualisation = {
228           emptyDiskImages = [ 20480 20480 20480 20480 20480 20480 ];
229         };
231         specialisation.resize.configuration = {
232           services.zfs.expandOnBoot = [ "tank" ];
233         };
234       };
235     };
237     testScript = { nodes, ... }:
238       ''
239         start_all()
240         machine.wait_for_unit("default.target")
241         print(machine.succeed('mount'))
243         print(machine.succeed('parted --script /dev/vdb -- mklabel gpt'))
244         print(machine.succeed('parted --script /dev/vdb -- mkpart primary 1M 70M'))
246         print(machine.succeed('parted --script /dev/vdc -- mklabel gpt'))
247         print(machine.succeed('parted --script /dev/vdc -- mkpart primary 1M 70M'))
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())
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")
258         print(machine.succeed('zpool list -v'))
259         new_size = int(machine.succeed('df -k --output=size /tank | tail -n1').strip())
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   };