-
Notifications
You must be signed in to change notification settings - Fork 13
/
tgt_service.nix
72 lines (70 loc) · 2.23 KB
/
tgt_service.nix
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
{ config, lib, pkgs, ... }:
with lib;
let
targetOpts = { name, config, ... }: {
options = {
name = mkOption {
type = types.str;
};
backingStore = mkOption {
type = types.str;
};
index = mkOption {
type = types.int;
description = "the index of the target, must be unique within the server";
};
};
config = {
name = mkDefault name;
};
};
makeService = target: {
name = target.name;
value = {
description = target.name+" auto-starter";
wantedBy = [ "basic.target" ];
partOf = [ "tgtd.service" ];
script = ''
${pkgs.tgt}/bin/tgtadm --lld iscsi --op new --mode target --tid ${builtins.toString target.index} -T ${target.name}
${pkgs.tgt}/bin/tgtadm --lld iscsi --op new --mode logicalunit --tid ${builtins.toString target.index} --lun 1 -b ${target.backingStore}
${pkgs.tgt}/bin/tgtadm --lld iscsi --op bind --mode target --tid ${builtins.toString target.index} -I ALL # gives everybody access
'';
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStop = "${pkgs.tgt}/bin/tgtadm --lld iscsi --op delete --mode target --tid ${builtins.toString target.index}";
};
};
};
in
{
options = {
services.tgtd = {
enable = mkOption {
type = types.bool;
default = false;
description = "enable tgtd running on startup";
};
targets = mkOption {
default = [];
type = types.loaOf (types.submodule targetOpts);
};
};
};
config = let
LUNs = builtins.listToAttrs (map makeService (attrValues config.services.tgtd.targets));
tgtd = {
description = "tgtd daemon";
wantedBy = [ "basic.target" ];
serviceConfig = {
ExecStart = "${pkgs.tgt}/bin/tgtd -f --iscsi nop_interval=30 --iscsi nop_count=10";
ExecStop = "${pkgs.coreutils}/bin/sleep 30 ; ${pkgs.tgt}/bin/tgtadm --op delete --mode system";
KillMode = "process";
Restart = "on-success";
};
};
in
mkIf config.services.tgtd.enable {
systemd.services = LUNs // { tgtd = tgtd; };
};
}