2021-10-06 19:50:13 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
|
|
|
cfg = config.services.moosefs;
|
|
|
|
|
|
|
|
mfsUser = if cfg.runAsUser then "moosefs" else "root";
|
|
|
|
|
|
|
|
settingsFormat = let
|
|
|
|
listSep = " ";
|
2024-08-28 19:18:58 +00:00
|
|
|
allowedTypes = with lib.types; [ bool int float str ];
|
2021-10-06 19:50:13 +00:00
|
|
|
valueToString = val:
|
2024-08-28 19:18:58 +00:00
|
|
|
if lib.isList val then lib.concatStringsSep listSep (map (x: valueToString x) val)
|
|
|
|
else if lib.isBool val then (if val then "1" else "0")
|
2021-10-06 19:50:13 +00:00
|
|
|
else toString val;
|
|
|
|
|
|
|
|
in {
|
2024-08-28 19:18:58 +00:00
|
|
|
type = with lib.types; let
|
2021-10-06 19:50:13 +00:00
|
|
|
valueType = oneOf ([
|
|
|
|
(listOf valueType)
|
|
|
|
] ++ allowedTypes) // {
|
|
|
|
description = "Flat key-value file";
|
|
|
|
};
|
|
|
|
in attrsOf valueType;
|
|
|
|
|
|
|
|
generate = name: value:
|
|
|
|
pkgs.writeText name ( lib.concatStringsSep "\n" (
|
|
|
|
lib.mapAttrsToList (key: val: "${key} = ${valueToString val}") value ));
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
initTool = pkgs.writeShellScriptBin "mfsmaster-init" ''
|
|
|
|
if [ ! -e ${cfg.master.settings.DATA_PATH}/metadata.mfs ]; then
|
|
|
|
cp ${pkgs.moosefs}/var/mfs/metadata.mfs.empty ${cfg.master.settings.DATA_PATH}
|
|
|
|
chmod +w ${cfg.master.settings.DATA_PATH}/metadata.mfs.empty
|
|
|
|
${pkgs.moosefs}/bin/mfsmaster -a -c ${masterCfg} start
|
|
|
|
${pkgs.moosefs}/bin/mfsmaster -c ${masterCfg} stop
|
|
|
|
rm ${cfg.master.settings.DATA_PATH}/metadata.mfs.empty
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
|
|
|
# master config file
|
|
|
|
masterCfg = settingsFormat.generate
|
|
|
|
"mfsmaster.cfg" cfg.master.settings;
|
|
|
|
|
|
|
|
# metalogger config file
|
|
|
|
metaloggerCfg = settingsFormat.generate
|
|
|
|
"mfsmetalogger.cfg" cfg.metalogger.settings;
|
|
|
|
|
|
|
|
# chunkserver config file
|
|
|
|
chunkserverCfg = settingsFormat.generate
|
|
|
|
"mfschunkserver.cfg" cfg.chunkserver.settings;
|
|
|
|
|
2022-12-18 00:31:14 +00:00
|
|
|
# generic template for all daemons
|
2021-10-06 19:50:13 +00:00
|
|
|
systemdService = name: extraConfig: configFile: {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
wants = [ "network-online.target" ];
|
|
|
|
after = [ "network.target" "network-online.target" ];
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "forking";
|
|
|
|
ExecStart = "${pkgs.moosefs}/bin/mfs${name} -c ${configFile} start";
|
|
|
|
ExecStop = "${pkgs.moosefs}/bin/mfs${name} -c ${configFile} stop";
|
|
|
|
ExecReload = "${pkgs.moosefs}/bin/mfs${name} -c ${configFile} reload";
|
|
|
|
PIDFile = "${cfg."${name}".settings.DATA_PATH}/.mfs${name}.lock";
|
|
|
|
} // extraConfig;
|
|
|
|
};
|
|
|
|
|
|
|
|
in {
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
services.moosefs = {
|
2024-08-28 19:18:58 +00:00
|
|
|
masterHost = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2021-10-06 19:50:13 +00:00
|
|
|
default = null;
|
|
|
|
description = "IP or DNS name of master host.";
|
|
|
|
};
|
|
|
|
|
2024-08-28 19:18:58 +00:00
|
|
|
runAsUser = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2021-10-06 19:50:13 +00:00
|
|
|
default = true;
|
|
|
|
example = true;
|
|
|
|
description = "Run daemons as user moosefs instead of root.";
|
|
|
|
};
|
|
|
|
|
2024-08-28 19:18:58 +00:00
|
|
|
client.enable = lib.mkEnableOption "Moosefs client";
|
2021-10-06 19:50:13 +00:00
|
|
|
|
|
|
|
master = {
|
2024-08-28 19:18:58 +00:00
|
|
|
enable = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2021-10-06 19:50:13 +00:00
|
|
|
description = ''
|
|
|
|
Enable Moosefs master daemon.
|
|
|
|
|
|
|
|
You need to run `mfsmaster-init` on a freshly installed master server to
|
2022-12-18 00:31:14 +00:00
|
|
|
initialize the `DATA_PATH` directory.
|
2021-10-06 19:50:13 +00:00
|
|
|
'';
|
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
|
2024-08-28 19:18:58 +00:00
|
|
|
exports = lib.mkOption {
|
|
|
|
type = with lib.types; listOf str;
|
2021-10-06 19:50:13 +00:00
|
|
|
default = null;
|
|
|
|
description = "Paths to export (see mfsexports.cfg).";
|
|
|
|
example = [
|
|
|
|
"* / rw,alldirs,admin,maproot=0:0"
|
|
|
|
"* . rw"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2024-08-28 19:18:58 +00:00
|
|
|
openFirewall = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2021-10-06 19:50:13 +00:00
|
|
|
description = "Whether to automatically open the necessary ports in the firewall.";
|
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
|
2024-08-28 19:18:58 +00:00
|
|
|
settings = lib.mkOption {
|
|
|
|
type = lib.types.submodule {
|
2021-10-06 19:50:13 +00:00
|
|
|
freeformType = settingsFormat.type;
|
|
|
|
|
2024-08-28 19:18:58 +00:00
|
|
|
options.DATA_PATH = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2021-10-06 19:50:13 +00:00
|
|
|
default = "/var/lib/mfs";
|
|
|
|
description = "Data storage directory.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
description = "Contents of config file (mfsmaster.cfg).";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
metalogger = {
|
2024-08-28 19:18:58 +00:00
|
|
|
enable = lib.mkEnableOption "Moosefs metalogger daemon";
|
2021-10-06 19:50:13 +00:00
|
|
|
|
2024-08-28 19:18:58 +00:00
|
|
|
settings = lib.mkOption {
|
|
|
|
type = lib.types.submodule {
|
2021-10-06 19:50:13 +00:00
|
|
|
freeformType = settingsFormat.type;
|
|
|
|
|
2024-08-28 19:18:58 +00:00
|
|
|
options.DATA_PATH = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2021-10-06 19:50:13 +00:00
|
|
|
default = "/var/lib/mfs";
|
|
|
|
description = "Data storage directory";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
description = "Contents of metalogger config file (mfsmetalogger.cfg).";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
chunkserver = {
|
2024-08-28 19:18:58 +00:00
|
|
|
enable = lib.mkEnableOption "Moosefs chunkserver daemon";
|
2021-10-06 19:50:13 +00:00
|
|
|
|
2024-08-28 19:18:58 +00:00
|
|
|
openFirewall = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2021-10-06 19:50:13 +00:00
|
|
|
description = "Whether to automatically open the necessary ports in the firewall.";
|
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
|
2024-08-28 19:18:58 +00:00
|
|
|
hdds = lib.mkOption {
|
|
|
|
type = with lib.types; listOf str;
|
2021-10-06 19:50:13 +00:00
|
|
|
default = null;
|
|
|
|
description = "Mount points to be used by chunkserver for storage (see mfshdd.cfg).";
|
|
|
|
example = [ "/mnt/hdd1" ];
|
|
|
|
};
|
|
|
|
|
2024-08-28 19:18:58 +00:00
|
|
|
settings = lib.mkOption {
|
|
|
|
type = lib.types.submodule {
|
2021-10-06 19:50:13 +00:00
|
|
|
freeformType = settingsFormat.type;
|
|
|
|
|
2024-08-28 19:18:58 +00:00
|
|
|
options.DATA_PATH = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2021-10-06 19:50:13 +00:00
|
|
|
default = "/var/lib/mfs";
|
|
|
|
description = "Directory for lock file.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
description = "Contents of chunkserver config file (mfschunkserver.cfg).";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
2024-08-28 19:18:58 +00:00
|
|
|
config = lib.mkIf ( cfg.client.enable || cfg.master.enable || cfg.metalogger.enable || cfg.chunkserver.enable ) {
|
2021-10-06 19:50:13 +00:00
|
|
|
|
2024-08-28 19:18:58 +00:00
|
|
|
warnings = [ ( lib.mkIf (!cfg.runAsUser) "Running moosefs services as root is not recommended.") ];
|
2021-10-06 19:50:13 +00:00
|
|
|
|
|
|
|
# Service settings
|
|
|
|
services.moosefs = {
|
2024-08-28 19:18:58 +00:00
|
|
|
master.settings = lib.mkIf cfg.master.enable {
|
2021-10-06 19:50:13 +00:00
|
|
|
WORKING_USER = mfsUser;
|
|
|
|
EXPORTS_FILENAME = toString ( pkgs.writeText "mfsexports.cfg"
|
2024-08-28 19:18:58 +00:00
|
|
|
(lib.concatStringsSep "\n" cfg.master.exports));
|
2021-10-06 19:50:13 +00:00
|
|
|
};
|
|
|
|
|
2024-08-28 19:18:58 +00:00
|
|
|
metalogger.settings = lib.mkIf cfg.metalogger.enable {
|
2021-10-06 19:50:13 +00:00
|
|
|
WORKING_USER = mfsUser;
|
|
|
|
MASTER_HOST = cfg.masterHost;
|
|
|
|
};
|
|
|
|
|
2024-08-28 19:18:58 +00:00
|
|
|
chunkserver.settings = lib.mkIf cfg.chunkserver.enable {
|
2021-10-06 19:50:13 +00:00
|
|
|
WORKING_USER = mfsUser;
|
|
|
|
MASTER_HOST = cfg.masterHost;
|
|
|
|
HDD_CONF_FILENAME = toString ( pkgs.writeText "mfshdd.cfg"
|
2024-08-28 19:18:58 +00:00
|
|
|
(lib.concatStringsSep "\n" cfg.chunkserver.hdds));
|
2021-10-06 19:50:13 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
# Create system user account for daemons
|
2024-08-28 19:18:58 +00:00
|
|
|
users = lib.mkIf ( cfg.runAsUser && ( cfg.master.enable || cfg.metalogger.enable || cfg.chunkserver.enable ) ) {
|
2021-10-06 19:50:13 +00:00
|
|
|
users.moosefs = {
|
|
|
|
isSystemUser = true;
|
|
|
|
description = "moosefs daemon user";
|
|
|
|
group = "moosefs";
|
|
|
|
};
|
|
|
|
groups.moosefs = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
environment.systemPackages =
|
|
|
|
(lib.optional cfg.client.enable pkgs.moosefs) ++
|
|
|
|
(lib.optional cfg.master.enable initTool);
|
|
|
|
|
|
|
|
networking.firewall.allowedTCPPorts =
|
|
|
|
(lib.optionals cfg.master.openFirewall [ 9419 9420 9421 ]) ++
|
|
|
|
(lib.optional cfg.chunkserver.openFirewall 9422);
|
|
|
|
|
|
|
|
# Ensure storage directories exist
|
|
|
|
systemd.tmpfiles.rules =
|
2024-08-28 19:18:58 +00:00
|
|
|
lib.optional cfg.master.enable "d ${cfg.master.settings.DATA_PATH} 0700 ${mfsUser} ${mfsUser}"
|
|
|
|
++ lib.optional cfg.metalogger.enable "d ${cfg.metalogger.settings.DATA_PATH} 0700 ${mfsUser} ${mfsUser}"
|
|
|
|
++ lib.optional cfg.chunkserver.enable "d ${cfg.chunkserver.settings.DATA_PATH} 0700 ${mfsUser} ${mfsUser}";
|
2021-10-06 19:50:13 +00:00
|
|
|
|
|
|
|
# Service definitions
|
2024-08-28 19:18:58 +00:00
|
|
|
systemd.services.mfs-master = lib.mkIf cfg.master.enable
|
2021-10-06 19:50:13 +00:00
|
|
|
( systemdService "master" {
|
|
|
|
TimeoutStartSec = 1800;
|
|
|
|
TimeoutStopSec = 1800;
|
|
|
|
Restart = "no";
|
|
|
|
} masterCfg );
|
|
|
|
|
2024-08-28 19:18:58 +00:00
|
|
|
systemd.services.mfs-metalogger = lib.mkIf cfg.metalogger.enable
|
2021-10-06 19:50:13 +00:00
|
|
|
( systemdService "metalogger" { Restart = "on-abnormal"; } metaloggerCfg );
|
|
|
|
|
2024-08-28 19:18:58 +00:00
|
|
|
systemd.services.mfs-chunkserver = lib.mkIf cfg.chunkserver.enable
|
2021-10-06 19:50:13 +00:00
|
|
|
( systemdService "chunkserver" { Restart = "on-abnormal"; } chunkserverCfg );
|
|
|
|
};
|
|
|
|
}
|