nixpkgs/nixos/modules/virtualisation/openvswitch.nix

146 lines
4.2 KiB
Nix
Raw Normal View History

# Systemd services for openvswitch
{ config, lib, pkgs, ... }:
with lib;
let
2014-06-12 16:08:49 +02:00
cfg = config.virtualisation.vswitch;
in {
options.virtualisation.vswitch = {
enable = mkOption {
2014-06-12 16:08:49 +02:00
type = types.bool;
default = false;
description = ''
Whether to enable Open vSwitch. A configuration daemon (ovs-server)
will be started.
2014-06-12 16:08:49 +02:00
'';
};
resetOnStart = mkOption {
type = types.bool;
default = false;
description = ''
Whether to reset the Open vSwitch configuration database to a default
configuration on every start of the systemd <literal>ovsdb.service</literal>.
'';
};
package = mkOption {
2014-06-12 16:08:49 +02:00
type = types.package;
default = pkgs.openvswitch;
defaultText = literalExpression "pkgs.openvswitch";
description = ''
2014-06-12 16:08:49 +02:00
Open vSwitch package to use.
'';
2014-06-12 16:08:49 +02:00
};
};
config = mkIf cfg.enable (let
2014-06-12 16:08:49 +02:00
# Where the communication sockets live
2018-12-19 22:45:15 +01:00
runDir = "/run/openvswitch";
2014-06-12 16:08:49 +02:00
# The path to the an initialized version of the database
2014-06-12 16:08:49 +02:00
db = pkgs.stdenv.mkDerivation {
name = "vswitch.db";
2019-06-19 17:45:34 +02:00
dontUnpack = true;
2014-06-12 16:08:49 +02:00
buildPhase = "true";
buildInputs = with pkgs; [
cfg.package
];
installPhase = "mkdir -p $out";
2014-06-12 16:08:49 +02:00
};
2021-12-15 19:00:30 +01:00
in {
2021-05-17 15:00:57 +02:00
environment.systemPackages = [ cfg.package ];
2014-06-12 16:08:49 +02:00
boot.kernelModules = [ "tun" "openvswitch" ];
boot.extraModulePackages = [ cfg.package ];
systemd.services.ovsdb = {
description = "Open_vSwitch Database Server";
wantedBy = [ "multi-user.target" ];
after = [ "systemd-udev-settle.service" ];
path = [ cfg.package ];
restartTriggers = [ db cfg.package ];
# Create the config database
preStart =
2014-06-12 16:08:49 +02:00
''
mkdir -p ${runDir}
mkdir -p /var/db/openvswitch
chmod +w /var/db/openvswitch
${optionalString cfg.resetOnStart "rm -f /var/db/openvswitch/conf.db"}
2014-06-12 16:08:49 +02:00
if [[ ! -e /var/db/openvswitch/conf.db ]]; then
${cfg.package}/bin/ovsdb-tool create \
"/var/db/openvswitch/conf.db" \
"${cfg.package}/share/openvswitch/vswitch.ovsschema"
fi
chmod -R +w /var/db/openvswitch
if ${cfg.package}/bin/ovsdb-tool needs-conversion /var/db/openvswitch/conf.db | grep -q "yes"
then
echo "Performing database upgrade"
${cfg.package}/bin/ovsdb-tool convert /var/db/openvswitch/conf.db
else
echo "Database already up to date"
fi
2014-06-12 16:08:49 +02:00
'';
serviceConfig = {
ExecStart =
''
${cfg.package}/bin/ovsdb-server \
--remote=punix:${runDir}/db.sock \
--private-key=db:Open_vSwitch,SSL,private_key \
--certificate=db:Open_vSwitch,SSL,certificate \
--bootstrap-ca-cert=db:Open_vSwitch,SSL,ca_cert \
--unixctl=ovsdb.ctl.sock \
2018-12-19 22:45:15 +01:00
--pidfile=/run/openvswitch/ovsdb.pid \
--detach \
/var/db/openvswitch/conf.db
'';
Restart = "always";
RestartSec = 3;
2018-12-19 22:45:15 +01:00
PIDFile = "/run/openvswitch/ovsdb.pid";
# Use service type 'forking' to correctly determine when ovsdb-server is ready.
Type = "forking";
};
postStart = ''
2014-06-12 16:08:49 +02:00
${cfg.package}/bin/ovs-vsctl --timeout 3 --retry --no-wait init
'';
2014-06-12 16:08:49 +02:00
};
systemd.services.ovs-vswitchd = {
2014-06-12 16:08:49 +02:00
description = "Open_vSwitch Daemon";
wantedBy = [ "multi-user.target" ];
2014-06-12 16:08:49 +02:00
bindsTo = [ "ovsdb.service" ];
after = [ "ovsdb.service" ];
path = [ cfg.package ];
serviceConfig = {
ExecStart = ''
${cfg.package}/bin/ovs-vswitchd \
2018-12-19 22:45:15 +01:00
--pidfile=/run/openvswitch/ovs-vswitchd.pid \
--detach
'';
2018-12-19 22:45:15 +01:00
PIDFile = "/run/openvswitch/ovs-vswitchd.pid";
# Use service type 'forking' to correctly determine when vswitchd is ready.
Type = "forking";
Restart = "always";
RestartSec = 3;
};
};
2021-12-15 19:00:30 +01:00
});
2021-12-15 19:00:30 +01:00
imports = [
(mkRemovedOptionModule [ "virtualisation" "vswitch" "ipsec" ] ''
OpenVSwitch IPSec functionality has been removed, because it depended on racoon,
which was removed from nixpkgs, because it was abanoded upstream.
'')
];
meta.maintainers = with maintainers; [ netixx ];
}