Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module Request: nixos/targetclid #378196

Open
3 tasks done
Pablo1107 opened this issue Jan 31, 2025 · 0 comments
Open
3 tasks done

Module Request: nixos/targetclid #378196

Pablo1107 opened this issue Jan 31, 2025 · 0 comments
Labels
0.kind: enhancement Add something new 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 9.needs: module (new) This needs a module to be created

Comments

@Pablo1107
Copy link
Contributor

Nixpkgs version

  • Unstable (25.05)

Describe the proposed module

As a user that host several VMs on a NixOS host, it would be nice be able to declaratively create iSCSI Target from NixOS.

Something I build recently and can be used as a starter point would be something like this:

{ config, options, lib, myLib, pkgs, pkgs-stable, ... }:

with lib;
with myLib;

let
  cfg = config.personal.openiscsi;
in
{
  options.personal.openiscsi = {
    enable = mkEnableOption "openiscsi";
    backingStorePath = mkOption {
      type = types.str;
      default = "/var/lib/targetclid/mydisk.img";
      description = "Path to the file-based backing storage";
    };
    targetIQN = mkOption {
      type = types.str;
      default = "iqn.2025-01.com.example:mytarget";
      description = "iSCSI target IQN";
    };
    initiatorIQN = mkOption {
      type = types.str;
      default = "iqn.2004-10.com.ubuntu:01:dbd9d984ae5b"; # Updated to match Ubuntu's initiator name
      description = "iSCSI initiator IQN";
    };
    storageSize = mkOption {
      type = types.str;
      default = "10G";
      description = "Size of the file-based backing storage";
    };
  };

  config = mkIf cfg.enable {
    services.target = {
      enable = true;
    };

    environment.systemPackages = with pkgs; [ targetcli ];

    systemd.services.targetclid = {
      description = "iSCSI Target CLI Daemon";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        ExecStart = "${pkgs.targetcli}/bin/targetclid";
        Restart = "always";
      };
    };

    systemd.services.setup-iscsi-target = {
      description = "Setup iSCSI Target";
      after = [ "targetclid.service" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig.Type = "oneshot";
      script = ''
        set -e
        if ! ${pkgs.targetcli}/bin/targetcli ls /iscsi/${cfg.targetIQN} &>/dev/null; then
          echo "Creating iSCSI backing store"
          mkdir -p $(dirname ${cfg.backingStorePath})
          if [ ! -f ${cfg.backingStorePath} ]; then
            truncate -s ${cfg.storageSize} ${cfg.backingStorePath}
          fi

          echo "Configuring iSCSI target"
          ${pkgs.targetcli}/bin/targetcli <<EOF
/backstores/fileio create myfile ${cfg.backingStorePath}
/iscsi create ${cfg.targetIQN}
/iscsi/${cfg.targetIQN}/tpg1/luns create /backstores/fileio/myfile
/iscsi/${cfg.targetIQN}/tpg1/acls create ${cfg.initiatorIQN}
/iscsi/${cfg.targetIQN}/tpg1 set attribute authentication=0
/saveconfig
EOF
        else
          echo "iSCSI target already configured. Skipping."
        fi
      '';
    };
  };
}

I understand tho that the complexity would required a better interface and also analyze what to do on some edge cases, like removing or renaming resources, or trying to change the storage size. Probably some of these cases would have to be handled imperatively but maybe we could think of some solution.

Additional context

No response

Notify maintainers


Note for maintainers: Please tag this issue in your pull request description. (i.e. Resolves #ISSUE.)

cc @cleverca22 @zaninime @ajs124

I assert that this issue is relevant for Nixpkgs

Is this issue important to you?

Add a 👍 reaction to issues you find important.

@Pablo1107 Pablo1107 added 0.kind: enhancement Add something new 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 9.needs: module (new) This needs a module to be created labels Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.kind: enhancement Add something new 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 9.needs: module (new) This needs a module to be created
Projects
None yet
Development

No branches or pull requests

1 participant