about summary refs log tree commit diff
path: root/nixos/tests/iscsi-root.nix
blob: eb0719edc37969bfcd17cd31b5bace1d71539972 (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
import ./make-test-python.nix (
  { pkgs, lib, ... }:
    let
      initiatorName = "iqn.2020-08.org.linux-iscsi.initiatorhost:example";
      targetName = "iqn.2003-01.org.linux-iscsi.target.x8664:sn.acf8fd9c23af";
    in
      {
        name = "iscsi";
        meta = {
          maintainers = pkgs.lib.teams.deshaw.members
          ++ (with pkgs.lib.maintainers; [ ajs124 ]);
        };

        nodes = {
          target = { config, pkgs, lib, ... }: {
            services.target = {
              enable = true;
              config = {
                fabric_modules = [];
                storage_objects = [
                  {
                    dev = "/dev/vdb";
                    name = "test";
                    plugin = "block";
                    write_back = true;
                    wwn = "92b17c3f-6b40-4168-b082-ceeb7b495522";
                  }
                ];
                targets = [
                  {
                    fabric = "iscsi";
                    tpgs = [
                      {
                        enable = true;
                        attributes = {
                          authentication = 0;
                          generate_node_acls = 1;
                        };
                        luns = [
                          {
                            alias = "94dfe06967";
                            alua_tg_pt_gp_name = "default_tg_pt_gp";
                            index = 0;
                            storage_object = "/backstores/block/test";
                          }
                        ];
                        node_acls = [
                          {
                            mapped_luns = [
                              {
                                alias = "d42f5bdf8a";
                                index = 0;
                                tpg_lun = 0;
                                write_protect = false;
                              }
                            ];
                            node_wwn = initiatorName;
                          }
                        ];
                        portals = [
                          {
                            ip_address = "0.0.0.0";
                            iser = false;
                            offload = false;
                            port = 3260;
                          }
                        ];
                        tag = 1;
                      }
                    ];
                    wwn = targetName;
                  }
                ];
              };
            };

            networking.firewall.allowedTCPPorts = [ 3260 ];
            networking.firewall.allowedUDPPorts = [ 3260 ];

            virtualisation.memorySize = 2048;
            virtualisation.emptyDiskImages = [ 2048 ];
          };

          initiatorAuto = { nodes, config, pkgs, ... }: {
            services.openiscsi = {
              enable = true;
              enableAutoLoginOut = true;
              discoverPortal = "target";
              name = initiatorName;
            };

            environment.systemPackages = with pkgs; [
              xfsprogs
            ];

            system.extraDependencies = [ nodes.initiatorRootDisk.config.system.build.toplevel ];

            nix.settings = {
              substituters = lib.mkForce [];
              hashed-mirrors = null;
              connect-timeout = 1;
            };
          };

          initiatorRootDisk = { config, pkgs, modulesPath, lib, ... }: {
            boot.loader.grub.enable = false;
            boot.kernelParams = lib.mkOverride 5 (
              [
                "boot.shell_on_fail"
                "console=tty1"
                "ip=${config.networking.primaryIPAddress}:::255.255.255.0::ens9:none"
              ]
            );

            # defaults to true, puts some code in the initrd that tries to mount an overlayfs on /nix/store
            virtualisation.writableStore = false;

            fileSystems = lib.mkOverride 5 {
              "/" = {
                fsType = "xfs";
                device = "/dev/sda";
                options = [ "_netdev" ];
              };
            };

            boot.iscsi-initiator = {
              discoverPortal = "target";
              name = initiatorName;
              target = targetName;
            };
          };
        };

        testScript = { nodes, ... }: ''
          target.start()
          target.wait_for_unit("iscsi-target.service")

          initiatorAuto.start()

          initiatorAuto.wait_for_unit("iscsid.service")
          initiatorAuto.wait_for_unit("iscsi.service")
          initiatorAuto.get_unit_info("iscsi")

          initiatorAuto.succeed("set -x; while ! test -e /dev/sda; do sleep 1; done")

          initiatorAuto.succeed("mkfs.xfs /dev/sda")
          initiatorAuto.succeed("mkdir /mnt && mount /dev/sda /mnt")
          initiatorAuto.succeed(
              "nixos-install --no-bootloader --no-root-passwd --system ${nodes.initiatorRootDisk.config.system.build.toplevel}"
          )
          initiatorAuto.succeed("umount /mnt && rmdir /mnt")
          initiatorAuto.shutdown()

          initiatorRootDisk.start()
          initiatorRootDisk.wait_for_unit("multi-user.target")
          initiatorRootDisk.wait_for_unit("iscsid")
          initiatorRootDisk.succeed("touch test")
          initiatorRootDisk.shutdown()
        '';
      }
)