2022-02-10 20:34:41 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
|
|
|
cfg = config.services.moosefs;
|
|
|
|
|
|
|
|
mfsUser = if cfg.runAsUser then "moosefs" else "root";
|
|
|
|
|
|
|
|
settingsFormat = let
|
|
|
|
listSep = " ";
|
2024-09-19 14:19:46 +00:00
|
|
|
allowedTypes = with lib.types; [ bool int float str ];
|
2022-02-10 20:34:41 +00:00
|
|
|
valueToString = val:
|
2024-09-19 14:19:46 +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")
|
2022-02-10 20:34:41 +00:00
|
|
|
else toString val;
|
|
|
|
|
|
|
|
in {
|
2024-09-19 14:19:46 +00:00
|
|
|
type = with lib.types; let
|
2022-02-10 20:34:41 +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-28 21:21:41 +00:00
|
|
|
# generic template for all daemons
|
2022-02-10 20:34:41 +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-09-19 14:19:46 +00:00
|
|
|
masterHost = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2022-02-10 20:34:41 +00:00
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "IP or DNS name of master host.";
|
2022-02-10 20:34:41 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
runAsUser = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2022-02-10 20:34:41 +00:00
|
|
|
default = true;
|
|
|
|
example = true;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Run daemons as user moosefs instead of root.";
|
2022-02-10 20:34:41 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
client.enable = lib.mkEnableOption "Moosefs client";
|
2022-02-10 20:34:41 +00:00
|
|
|
|
|
|
|
master = {
|
2024-09-19 14:19:46 +00:00
|
|
|
enable = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-02-10 20:34:41 +00:00
|
|
|
Enable Moosefs master daemon.
|
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
You need to run `mfsmaster-init` on a freshly installed master server to
|
2022-12-28 21:21:41 +00:00
|
|
|
initialize the `DATA_PATH` directory.
|
2022-02-10 20:34:41 +00:00
|
|
|
'';
|
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
exports = lib.mkOption {
|
|
|
|
type = with lib.types; listOf str;
|
2022-02-10 20:34:41 +00:00
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Paths to export (see mfsexports.cfg).";
|
2022-02-10 20:34:41 +00:00
|
|
|
example = [
|
|
|
|
"* / rw,alldirs,admin,maproot=0:0"
|
|
|
|
"* . rw"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
openFirewall = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Whether to automatically open the necessary ports in the firewall.";
|
2022-02-10 20:34:41 +00:00
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
settings = lib.mkOption {
|
|
|
|
type = lib.types.submodule {
|
2022-02-10 20:34:41 +00:00
|
|
|
freeformType = settingsFormat.type;
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
options.DATA_PATH = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2022-02-10 20:34:41 +00:00
|
|
|
default = "/var/lib/mfs";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Data storage directory.";
|
2022-02-10 20:34:41 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Contents of config file (mfsmaster.cfg).";
|
2022-02-10 20:34:41 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
metalogger = {
|
2024-09-19 14:19:46 +00:00
|
|
|
enable = lib.mkEnableOption "Moosefs metalogger daemon";
|
2022-02-10 20:34:41 +00:00
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
settings = lib.mkOption {
|
|
|
|
type = lib.types.submodule {
|
2022-02-10 20:34:41 +00:00
|
|
|
freeformType = settingsFormat.type;
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
options.DATA_PATH = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2022-02-10 20:34:41 +00:00
|
|
|
default = "/var/lib/mfs";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Data storage directory";
|
2022-02-10 20:34:41 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Contents of metalogger config file (mfsmetalogger.cfg).";
|
2022-02-10 20:34:41 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
chunkserver = {
|
2024-09-19 14:19:46 +00:00
|
|
|
enable = lib.mkEnableOption "Moosefs chunkserver daemon";
|
2022-02-10 20:34:41 +00:00
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
openFirewall = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Whether to automatically open the necessary ports in the firewall.";
|
2022-02-10 20:34:41 +00:00
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
hdds = lib.mkOption {
|
|
|
|
type = with lib.types; listOf str;
|
2022-02-10 20:34:41 +00:00
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Mount points to be used by chunkserver for storage (see mfshdd.cfg).";
|
2022-02-10 20:34:41 +00:00
|
|
|
example = [ "/mnt/hdd1" ];
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
settings = lib.mkOption {
|
|
|
|
type = lib.types.submodule {
|
2022-02-10 20:34:41 +00:00
|
|
|
freeformType = settingsFormat.type;
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
options.DATA_PATH = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2022-02-10 20:34:41 +00:00
|
|
|
default = "/var/lib/mfs";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Directory for lock file.";
|
2022-02-10 20:34:41 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Contents of chunkserver config file (mfschunkserver.cfg).";
|
2022-02-10 20:34:41 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
config = lib.mkIf ( cfg.client.enable || cfg.master.enable || cfg.metalogger.enable || cfg.chunkserver.enable ) {
|
2022-02-10 20:34:41 +00:00
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
warnings = [ ( lib.mkIf (!cfg.runAsUser) "Running moosefs services as root is not recommended.") ];
|
2022-02-10 20:34:41 +00:00
|
|
|
|
|
|
|
# Service settings
|
|
|
|
services.moosefs = {
|
2024-09-19 14:19:46 +00:00
|
|
|
master.settings = lib.mkIf cfg.master.enable {
|
2022-02-10 20:34:41 +00:00
|
|
|
WORKING_USER = mfsUser;
|
|
|
|
EXPORTS_FILENAME = toString ( pkgs.writeText "mfsexports.cfg"
|
2024-09-19 14:19:46 +00:00
|
|
|
(lib.concatStringsSep "\n" cfg.master.exports));
|
2022-02-10 20:34:41 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
metalogger.settings = lib.mkIf cfg.metalogger.enable {
|
2022-02-10 20:34:41 +00:00
|
|
|
WORKING_USER = mfsUser;
|
|
|
|
MASTER_HOST = cfg.masterHost;
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
chunkserver.settings = lib.mkIf cfg.chunkserver.enable {
|
2022-02-10 20:34:41 +00:00
|
|
|
WORKING_USER = mfsUser;
|
|
|
|
MASTER_HOST = cfg.masterHost;
|
|
|
|
HDD_CONF_FILENAME = toString ( pkgs.writeText "mfshdd.cfg"
|
2024-09-19 14:19:46 +00:00
|
|
|
(lib.concatStringsSep "\n" cfg.chunkserver.hdds));
|
2022-02-10 20:34:41 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
# Create system user account for daemons
|
2024-09-19 14:19:46 +00:00
|
|
|
users = lib.mkIf ( cfg.runAsUser && ( cfg.master.enable || cfg.metalogger.enable || cfg.chunkserver.enable ) ) {
|
2022-02-10 20:34:41 +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-09-19 14:19:46 +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}";
|
2022-02-10 20:34:41 +00:00
|
|
|
|
|
|
|
# Service definitions
|
2024-09-19 14:19:46 +00:00
|
|
|
systemd.services.mfs-master = lib.mkIf cfg.master.enable
|
2022-02-10 20:34:41 +00:00
|
|
|
( systemdService "master" {
|
|
|
|
TimeoutStartSec = 1800;
|
|
|
|
TimeoutStopSec = 1800;
|
|
|
|
Restart = "no";
|
|
|
|
} masterCfg );
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
systemd.services.mfs-metalogger = lib.mkIf cfg.metalogger.enable
|
2022-02-10 20:34:41 +00:00
|
|
|
( systemdService "metalogger" { Restart = "on-abnormal"; } metaloggerCfg );
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
systemd.services.mfs-chunkserver = lib.mkIf cfg.chunkserver.enable
|
2022-02-10 20:34:41 +00:00
|
|
|
( systemdService "chunkserver" { Restart = "on-abnormal"; } chunkserverCfg );
|
|
|
|
};
|
|
|
|
}
|