How setup iscsi

I couldn’t find satisfying documentation about how to get a iscsi drive running on nixos.

I tried to install open-iscsi and run iscsiadm but this does not work since nixos does not allow changes in /etc/iscsi/ it seems. I am new to nixos.

I have found some mentions about using the configuration.nix file but not how to do that exactly.

thanks in advance.

Ok I figured it out.

you need to enable iscsid in the conf and then you can also add the file system.

configuration.nix:

  services.openiscsi.enable = true;

# probably not needed 
  services.openiscsi.discoverPortal = "ip:3260";
# probably not needed 
  services.openiscsi.name = "iqn.2005-10.org.freenas.ctl:steam-library";

  environment.systemPackages = with pkgs; [
    openiscsi
  ];


  fileSystems."/run/media/user/SteamLibrary" = {
    device = "/dev/disk/by-uuid/uuidasdfasdfasdfasdf";
    fsType = "btrfs";
    options = ["nofail"
               "nofail"
               "auto"
               "users"
               "exec"
               "x-systemd.automount"
    ];
  };

you probably can setup the auto login in the configuration.nix too but I was happy to be able to use the iscsiadm after enableing openiscsi.

source:
https://nixos.org/manual/nixos/stable/options

I was trying to accomplish the same thing but I was able to auto login. I was able to accomplish it with this in my configuration.nix:

{
  services.openiscsi = {
    enable = true;
    discoverPortals = [ "ip:3260" ];
    targets = [ "iqn.2005-10.org.freenas.ctl:lingames" ];
    extraConfig = ''
      node.startup = automatic
      node.session.auth.authmethod = None
    '';
  };

Ok, turns out I was wrong. My previous one does not work. I had some other stuff going on in my configuration.nix that made that work until it didn’t. I was able to actually figure out how to get it to work. From what I have found the openiscsi nixpkg options are half baked.

According to the Arch Wiki, “To log in to a target during boot, [enable] iscsi.service and make sure the nodes have node.startup = automatic in their configuration (/var/lib/iscsi/nodes/iqn.node-name/node-ip-address,port).”

The issue with this is nix does not create a /var/lib/iscsi/nodes/. When you connect to target. There is the services.openiscsi.extraConfig but that appends lines to the /etc/iscsid.conf file which will not work.

To get around this I tried many things but the one that ended up working the best was created a systemd to connect to it. Here is everything you need to get it to work in your configuration.nix:

{
   ...
   
   boot.kernelModules = [ "iscsi_tcp" ];   # not sure if needed but added just incase
   
   services.openiscsi = {
    enable = true;  # Enable openiscsi daemon
    name = "iqn.2024-09.com.nixos:my-nixos-initiator";  # Set **YOUR** iSCSI initiator name not the one that you are trying to connect to. I am pretty sure this can be whatever you want.

    discoverPortal = "10.0.0.3";   # IP of your iscsi server
  };
 
 # Custom systemd service for logging in to a specific iSCSI target, you can name the service whatever youd like
  systemd.services.iscsi-login-lingames = {
    description = "Login to iSCSI target iqn.2005-10.org.freenas.ctl:lingames";
    after = [ "network.target" "iscsid.service" ];
    wants = [ "iscsid.service" ];
    serviceConfig = {
      ExecStartPre = "${pkgs.openiscsi}/bin/iscsiadm -m discovery -t sendtargets -p 10.0.0.3";
      ExecStart = "${pkgs.openiscsi}/bin/iscsiadm -m node -T iqn.2005-10.org.freenas.ctl:lingames -p 10.0.0.3 --login";
      ExecStop = "${pkgs.openiscsi}/bin/iscsiadm -m node -T iqn.2005-10.org.freenas.ctl:lingames -p 10.0.0.3 --logout";
      Restart = "on-failure";
      RemainAfterExit = true;
    };
    wantedBy = [ "multi-user.target" ];
  };

 fileSystems."<path-to-where-you-want-it-mounted>" = {
    device = "/dev/disk/by-path/ip-10.0.0.3:3260-iscsi-iqn.2005-10.org.freenas.ctl:lingames-lun-0";  # Replace with the correct device path after iSCSI login you cloud also do /dev/disk/by-uuid/<UUID-of-device>
    fsType = "ext4";  # Or the correct filesystem type
    options = [ "_netdev" "nofail" ];  # Ensures network is up before mounting and wont fail to boot if it cant connect
  };

  ...

}

This has been the only way I have found to get this to work if you want autologin at boot. There also is the services.openiscsi.enableAutoLoginOut which will log you into every available iscsi share it can find on the network. This could work if you don’t mind connecting to all of them and then you can just mount the one you want. I have many iscsi shares and that would have annoyed me. Hopefully we will get some better iscsi support in nixos. I would try but I am still learning nix.

Hope this helps someone else.

2 Likes

Hi there.

I have got myself into a self-inflicted issue, as my machine will no longer boot after changing something in the iscsi remote node.

I used btrfs as the filesystem without a nofail option (stupid I know) and now I cannot boot.

The iqn looks correct, but it will not mount the iscsi drive.

The boot does not show me an error, but it seems to fail on rpc bind.

Is there a way to disable iscsi via a rescue image?

Or maybe the mount point does not exist or the wrong permissions.

Well you are in the wrong place here, you should create a new question instead.

However when you are using nixos you should just select a different boot build, from before when you didn’t had that change.

If you don’t have that for some reason you probably need to figure out how to change the configuration without booting.

For example you could use a live usb and do it from there.

Wrong place, how, when it’s the thread I followed to get iscsi loading in the first place. Maybe others had the same issue with btrfs non booting.

Not looking for the blatantly obvious, more iscsi tweak that may stop it loading in nix.