From 3718498cdba505f892440794a10d0dbcf0cbe936 Mon Sep 17 00:00:00 2001 From: Abdul Karim Date: Tue, 10 Sep 2024 23:51:16 +0100 Subject: [PATCH] ncm-network: add support for device configuration dropin file provide support to create dropin file for device config and add option to add keep-configuration option --- .../components/network/core-schema-legacy.pan | 8 +++++ ncm-network/src/main/perl/nmstate.pm | 29 +++++++++++++++++++ ncm-network/src/test/perl/nmstate_simple.t | 6 ++++ .../src/test/resources/nmstate_simple.pan | 3 ++ 4 files changed, 46 insertions(+) diff --git a/ncm-network/src/main/pan/components/network/core-schema-legacy.pan b/ncm-network/src/main/pan/components/network/core-schema-legacy.pan index b1745ee243..b19fa6df31 100644 --- a/ncm-network/src/main/pan/components/network/core-schema-legacy.pan +++ b/ncm-network/src/main/pan/components/network/core-schema-legacy.pan @@ -410,6 +410,13 @@ type structure_ipv6 = { "gatewaydev" ? valid_interface # sets IPV6_DEFAULTDEV }; +@documentation{ + NetworkManager device configuration for drop in config file. +} +type structure_nm_device_config = { + "keep-configuration" ? choice("yes", "no") +}; + @documentation{ Host network configuration @@ -448,6 +455,7 @@ type structure_network = { "ipv6" ? structure_ipv6 "manage_vips" : boolean = false "vips" ? structure_vip{} + "nm_device_config" ? structure_nm_device_config @{Manage custom routing table entries; key is the name; value is the id} "routing_table" ? long(1..252){} with { if (exists(SELF['main']) || exists(SELF['local']) || exists(SELF['default']) || exists(SELF['unspec'])) { diff --git a/ncm-network/src/main/perl/nmstate.pm b/ncm-network/src/main/perl/nmstate.pm index 3be7ef2488..64f527799c 100644 --- a/ncm-network/src/main/perl/nmstate.pm +++ b/ncm-network/src/main/perl/nmstate.pm @@ -35,6 +35,7 @@ Readonly my $NMCLI_CMD => '/usr/bin/nmcli'; # pick a config name for nmstate yml to configure dns-resolver: settings. if nm_manage_dns=true Readonly my $NM_RESOLV_YML => "/etc/nmstate/resolv.yml"; Readonly my $NM_DROPIN_CFG_FILE => "/etc/NetworkManager/conf.d/90-quattor.conf"; +Readonly my $NM_DEVICE_DROPIN_CFG_FILE => "/etc/NetworkManager/conf.d/89-device-quattor.conf"; # generate the correct fake yaml boolean value so TextRender can convert it in a yaml boolean Readonly my $YTRUE => $EDG::WP4::CCM::TextRender::ELEMENT_CONVERT{yaml_boolean}->(1); @@ -103,6 +104,26 @@ sub disable_nm_manage_dns }; } +sub nm_create_device_config_dropin +{ + my ($self, $nm_device_config, $nwsrv) = @_; + my @data = ('[device]'); + + if ( $nm_device_config ) { + foreach my $key (sort keys %$nm_device_config){ + push @data, $key."=".$nm_device_config->{$key}; + }; + + $self->verbose("setting device configuration dropin"); + } + my $fh = CAF::FileWriter->new($NM_DEVICE_DROPIN_CFG_FILE, mode => oct(444), log => $self, keeps_state => 1); + print $fh join("\n", @data, ''); + if ($fh->close()) { + $self->info("File $NM_DEVICE_DROPIN_CFG_FILE changed, reload network"); + $nwsrv->reload(); + }; +} + # return hashref of ipv4 policy rule sub make_nm_ip_rule { @@ -852,6 +873,14 @@ sub Configure # 3. (re)start things my $nwsrv = CAF::Service->new(['NetworkManager'], log => $self); + # NetworkManager device configuration, if defined. + my $nm_device_cfg = $nwtree->{nm_device_config}; + if ($nm_device_cfg){ + $self->nm_create_device_config_dropin($nm_device_cfg, $nwsrv); + } else { + $self->cleanup($NM_DEVICE_DROPIN_CFG_FILE); + } + # NetworkManager manages dns by default, but we manage dns with e.g. ncm-resolver, new option to enable/disable it. $self->disable_nm_manage_dns($manage_dns, $nwsrv); diff --git a/ncm-network/src/test/perl/nmstate_simple.t b/ncm-network/src/test/perl/nmstate_simple.t index 5ca004519c..a86bcc7ac4 100644 --- a/ncm-network/src/test/perl/nmstate_simple.t +++ b/ncm-network/src/test/perl/nmstate_simple.t @@ -42,6 +42,11 @@ Readonly my $NODNS => < < <file_exists("/etc/nmstate/toremove0.yml"), "unconfigured yml nmstate is removed"); diff --git a/ncm-network/src/test/resources/nmstate_simple.pan b/ncm-network/src/test/resources/nmstate_simple.pan index 0109c9a2ba..e8dc6bbfbd 100644 --- a/ncm-network/src/test/resources/nmstate_simple.pan +++ b/ncm-network/src/test/resources/nmstate_simple.pan @@ -2,6 +2,9 @@ object template nmstate_simple; include 'simple_base_profile'; "/hardware/cards/nic/eth0/hwaddr" = "6e:a5:1b:55:77:0a"; + +"/system/network/nm_device_config/keep-configuration" = "no"; + # the next include is mainly to the profile, it is not used in the tests # (unless the component gets specific schema things) include 'components/network/config-nmstate';