summary refs log tree commit diff
path: root/nixos/tests/zfs.nix
blob: 3454fbaf78fe564142d7aef90d6c08a831d6f378 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
{ system ? builtins.currentSystem,
  config ? {},
  pkgs ? import ../.. { inherit system config; }
}:

with import ../lib/testing-python.nix { inherit system pkgs; };

let

  makeZfsTest = name:
    { kernelPackage ? if enableUnstable
                      then pkgs.zfsUnstable.latestCompatibleLinuxPackages
                      else pkgs.linuxPackages
    , enableUnstable ? false
    , enableSystemdStage1 ? false
    , extraTest ? ""
    }:
    makeTest {
      name = "zfs-" + name;
      meta = with pkgs.lib.maintainers; {
        maintainers = [ adisbladis elvishjerricco ];
      };

      nodes.machine = { pkgs, lib, ... }:
        let
          usersharePath = "/var/lib/samba/usershares";
        in {
        virtualisation = {
          emptyDiskImages = [ 4096 4096 ];
          useBootLoader = true;
          useEFIBoot = true;
        };
        boot.loader.systemd-boot.enable = true;
        boot.loader.timeout = 0;
        boot.loader.efi.canTouchEfiVariables = true;
        networking.hostId = "deadbeef";
        boot.kernelPackages = kernelPackage;
        boot.supportedFilesystems = [ "zfs" ];
        boot.zfs.enableUnstable = enableUnstable;
        boot.initrd.systemd.enable = enableSystemdStage1;

        environment.systemPackages = [ pkgs.parted ];

        # /dev/disk/by-id doesn't get populated in the NixOS test framework
        boot.zfs.devNodes = "/dev/disk/by-uuid";

        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 = {
          boot.zfs.requestEncryptionCredentials = [ "automatic" ];
          virtualisation.fileSystems."/automatic" = {
            device = "automatic";
            fsType = "zfs";
          };
          virtualisation.fileSystems."/manual" = {
            device = "manual";
            fsType = "zfs";
          };
          virtualisation.fileSystems."/manual/encrypted" = {
            device = "manual/encrypted";
            fsType = "zfs";
            options = [ "noauto" ];
          };
          virtualisation.fileSystems."/manual/httpkey" = {
            device = "manual/httpkey";
            fsType = "zfs";
            options = [ "noauto" ];
          };
        };

        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";
            fsType = "zfs";
            options = [ "noauto" ];
          };
        };

        services.nginx = {
          enable = true;
          virtualHosts = {
            localhost = {
              locations = {
                "/zfskey" = {
                  return = ''200 "httpkeyabc"'';
                };
              };
            };
          };
        };
      };

      testScript = ''
        machine.wait_for_unit("multi-user.target")
        machine.succeed(
            "zpool status",
            "parted --script /dev/vdb mklabel msdos",
            "parted --script /dev/vdb -- mkpart primary 1024M -1s",
            "parted --script /dev/vdc mklabel msdos",
            "parted --script /dev/vdc -- mkpart primary 1024M -1s",
        )

        with subtest("sharesmb works"):
            machine.succeed(
                "zpool create rpool /dev/vdb1",
                "zfs create -o mountpoint=legacy rpool/root",
                # shared datasets cannot have legacy mountpoint
                "zfs create rpool/shared_smb",
                "bootctl set-default nixos-generation-1-specialisation-samba.conf",
                "sync",
            )
            machine.crash()
            machine.wait_for_unit("multi-user.target")
            machine.succeed("zfs set sharesmb=on rpool/shared_smb")
            machine.succeed(
                "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/vdb1",
                "zpool create -O mountpoint=legacy manual /dev/vdc1",
                "echo otherpass | zfs create "
                + "-o encryption=aes-256-gcm -o keyformat=passphrase manual/encrypted",
                "zfs create -o encryption=aes-256-gcm -o keyformat=passphrase "
                + "-o keylocation=http://localhost/zfskey manual/httpkey",
                "bootctl set-default nixos-generation-1-specialisation-encryption.conf",
                "sync",
                "zpool export automatic",
                "zpool export manual",
            )
            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(
                "zfs get -Ho value keystatus manual/encrypted | grep -Fx unavailable",
                "echo otherpass | zfs load-key manual/encrypted",
                "systemctl start manual-encrypted.mount",
                "zfs load-key manual/httpkey",
                "systemctl start manual-httpkey.mount",
                "umount /automatic /manual/encrypted /manual/httpkey /manual",
                "zpool destroy automatic",
                "zpool destroy manual",
            )

        with subtest("boot.zfs.forceImportAll works"):
            machine.succeed(
                "rm /etc/hostid",
                "zgenhostid deadcafe",
                "zpool create forcepool /dev/vdb1 -O mountpoint=legacy",
                "bootctl set-default nixos-generation-1-specialisation-forcepool.conf",
                "rm /etc/hostid",
                "sync",
            )
            machine.crash()
            machine.wait_for_unit("multi-user.target")
            machine.fail("zpool import forcepool")
            machine.succeed(
                "systemctl start forcepool.mount",
                "mount | grep forcepool",
            )
      '' + extraTest;

    };


in {

  stable = makeZfsTest "stable" { };

  unstable = makeZfsTest "unstable" {
    enableUnstable = true;
  };

  unstableWithSystemdStage1 = makeZfsTest "unstable" {
    enableUnstable = true;
    enableSystemdStage1 = true;
  };

  installer = (import ./installer.nix { }).zfsroot;

  expand-partitions = makeTest {
    name = "multi-disk-zfs";
    nodes = {
      machine = { pkgs, ... }: {
        environment.systemPackages = [ pkgs.parted ];
        boot.supportedFilesystems = [ "zfs" ];
        networking.hostId = "00000000";

        virtualisation = {
          emptyDiskImages = [ 20480 20480 20480 20480 20480 20480 ];
        };

        specialisation.resize.configuration = {
          services.zfs.expandOnBoot = [ "tank" ];
        };
      };
    };

    testScript = { nodes, ... }:
      ''
        start_all()
        machine.wait_for_unit("default.target")
        print(machine.succeed('mount'))

        print(machine.succeed('parted --script /dev/vdb -- mklabel gpt'))
        print(machine.succeed('parted --script /dev/vdb -- mkpart primary 1M 70M'))

        print(machine.succeed('parted --script /dev/vdc -- mklabel gpt'))
        print(machine.succeed('parted --script /dev/vdc -- mkpart primary 1M 70M'))

        print(machine.succeed('zpool create tank mirror /dev/vdb1 /dev/vdc1 mirror /dev/vdd /dev/vde mirror /dev/vdf /dev/vdg'))
        print(machine.succeed('zpool list -v'))
        print(machine.succeed('mount'))
        start_size = int(machine.succeed('df -k --output=size /tank | tail -n1').strip())

        print(machine.succeed("/run/current-system/specialisation/resize/bin/switch-to-configuration test >&2"))
        machine.wait_for_unit("zpool-expand-pools.service")
        machine.wait_for_unit("zpool-expand@tank.service")

        print(machine.succeed('zpool list -v'))
        new_size = int(machine.succeed('df -k --output=size /tank | tail -n1').strip())

        if (new_size - start_size) > 20000000:
          print("Disk grew appropriately.")
        else:
          print(f"Disk went from {start_size} to {new_size}, which doesn't seem right.")
          exit(1)
      '';
  };
}