Skip to content

Instantly share code, notes, and snippets.

@cleverca22
Last active February 7, 2016 10:24
Show Gist options
  • Save cleverca22/9ea27422dcfff1bc859f to your computer and use it in GitHub Desktop.
Save cleverca22/9ea27422dcfff1bc859f to your computer and use it in GitHub Desktop.
network booting nixos
{ ... }:
{
imports = [ ./tgt_service.nix ];
services = {
atftpd.enable = true;
atftpd.root = "/tftproot";
tgtd = {
enable = true;
targets = {
# backing store can be a file made with truncate(1)
"iqn.2016-01.laptop-root" = { backingStore = "/dev/naspool/laptop-root"; index = 1; };
};
};
};
}
[root@amd-nixos:~]# nix-build '<nixpkgs>' -A ipxe
these paths will be fetched (0.60 MiB download, 1.20 MiB unpacked):
/nix/store/1xw30y28dfc1wz62wgcjifsp58c8wdkk-ipxe-20141124-5cbdc41
fetching path ‘/nix/store/1xw30y28dfc1wz62wgcjifsp58c8wdkk-ipxe-20141124-5cbdc41’...
*** Downloading ‘https://cache.nixos.org/nar/1sksixb1cc57ypjmm99k274wfmc3mqbrvnbvyilz0sd8h8bnyn0l.nar.xz’ (signed by ‘cache.nixos.org-1’) to ‘/nix/store/1xw30y28dfc1wz62wgcjifsp58c8wdkk-ipxe-20141124-5cbdc41’...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 614k 100 614k 0 0 1170k 0 --:--:-- --:--:-- --:--:-- 1172k
/nix/store/1xw30y28dfc1wz62wgcjifsp58c8wdkk-ipxe-20141124-5cbdc41
[root@amd-nixos:~]# mkdir /tftproot/
[root@amd-nixos:~]# cp -vi result/undionly.kpxe /tftproot/
setup a tgt daemon using the tgt_module and tftp server
configure and rebuild-switch the server with server-configuration.nix
setup dhcp to direct users to the tftp server with ipxe using the dhcp fragment
192.168.2.61 is the IP of the tftp and http server
subnet 192.168.2.0 netmask 255.255.255.0 {
option subnet-mask 255.255.255.0;
option broadcast-address 192.168.2.255;
option routers 192.168.2.1;
range 192.168.2.100 192.168.2.200;
next-server 192.168.2.61;
if exists user-class and option user-class = "iPXE" {
filename "http://192.168.2.61/boot.php?mac=${net0/mac}&asset=${asset:uristring}";
} else {
filename = "undionly.kpxe";
}
}
{ 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.optionSet;
options = 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; };
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment