]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: enable KeepConfiguration= when running on network filesystem 22383/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 4 Feb 2022 06:33:38 +0000 (15:33 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 5 Feb 2022 00:42:32 +0000 (09:42 +0900)
Also, set KeepConfiguration=dhcp-on-stop by default when running in
initrd.

Fixes #21967.

man/systemd.network.xml
src/network/networkd-manager.c
src/network/networkd-manager.h
src/network/networkd-network.c
src/network/test-networkd-conf.c

index af431e4f2c93da2379eabc1e2c1706787067b64a..f90cff4cd6f56d126b8a5aad7c83009d872f7e43 100644 (file)
@@ -944,8 +944,10 @@ Table=1234</programlisting></para>
           lease expires. This is contrary to the DHCP specification, but may be the best choice if,
           e.g., the root filesystem relies on this connection. The setting <literal>dhcp</literal>
           implies <literal>dhcp-on-stop</literal>, and <literal>yes</literal> implies
-          <literal>dhcp</literal> and <literal>static</literal>. Defaults to <literal>no</literal>.
-          </para>
+          <literal>dhcp</literal> and <literal>static</literal>. Defaults to
+          <literal>dhcp-on-stop</literal> when <command>systemd-networkd</command> is running in
+          initrd, <literal>yes</literal> when the root filesystem is a network filesystem, and
+          <literal>no</literal> otherwise.</para>
         </listitem>
       </varlistentry>
     </variablelist>
index 7e89366ae8a65d8ef9d4434fe70cce89608686f4..3261f289e91486ae74aba012218b7afae36d4f45 100644 (file)
@@ -398,6 +398,30 @@ static int signal_restart_callback(sd_event_source *s, const struct signalfd_sig
         return sd_event_exit(sd_event_source_get_event(s), 0);
 }
 
+static int manager_set_keep_configuration(Manager *m) {
+        int r;
+
+        assert(m);
+
+        if (in_initrd()) {
+                log_debug("Running in initrd, keep DHCPv4 addresses on stopping networkd by default.");
+                m->keep_configuration = KEEP_CONFIGURATION_DHCP_ON_STOP;
+                return 0;
+        }
+
+        r = path_is_network_fs("/");
+        if (r < 0)
+                return log_error_errno(r, "Failed to detect if root is network filesystem: %m");
+        if (r == 0) {
+                m->keep_configuration = _KEEP_CONFIGURATION_INVALID;
+                return 0;
+        }
+
+        log_debug("Running on network filesystem, enabling KeepConfiguration= by default.");
+        m->keep_configuration = KEEP_CONFIGURATION_YES;
+        return 0;
+}
+
 int manager_setup(Manager *m) {
         int r;
 
@@ -453,6 +477,10 @@ int manager_setup(Manager *m) {
         if (r < 0)
                 return r;
 
+        r = manager_set_keep_configuration(m);
+        if (r < 0)
+                return r;
+
         m->state_file = strdup("/run/systemd/netif/state");
         if (!m->state_file)
                 return -ENOMEM;
@@ -468,6 +496,7 @@ int manager_new(Manager **ret, bool test_mode) {
                 return -ENOMEM;
 
         *m = (Manager) {
+                .keep_configuration = _KEEP_CONFIGURATION_INVALID,
                 .test_mode = test_mode,
                 .speed_meter_interval_usec = SPEED_METER_DEFAULT_TIME_INTERVAL,
                 .online_state = _LINK_ONLINE_STATE_INVALID,
index 36313589a3766afa689d846d4e05ce92c777c2b9..86de5291244a4ed77e01ad976914dfe526b8eb8a 100644 (file)
@@ -28,6 +28,8 @@ struct Manager {
         Hashmap *polkit_registry;
         int ethtool_fd;
 
+        KeepConfiguration keep_configuration;
+
         bool test_mode;
         bool enumerating;
         bool dirty;
index 3142be471f8b8f29608c8cfae98ff8bf50c00500..edcd68d61679b3e8a9be74b3c0e52e630f03e063 100644 (file)
@@ -124,6 +124,7 @@ int network_verify(Network *network) {
         int r;
 
         assert(network);
+        assert(network->manager);
         assert(network->filename);
 
         if (net_match_is_empty(&network->match) && !network->conditions)
@@ -248,10 +249,11 @@ int network_verify(Network *network) {
         }
 
         if (network->dhcp_critical >= 0) {
-                if (network->keep_configuration >= 0)
-                        log_warning("%s: Both KeepConfiguration= and deprecated CriticalConnection= are set. "
-                                    "Ignoring CriticalConnection=.", network->filename);
-                else if (network->dhcp_critical)
+                if (network->keep_configuration >= 0) {
+                        if (network->manager->keep_configuration < 0)
+                                log_warning("%s: Both KeepConfiguration= and deprecated CriticalConnection= are set. "
+                                            "Ignoring CriticalConnection=.", network->filename);
+                } else if (network->dhcp_critical)
                         /* CriticalConnection=yes also preserve foreign static configurations. */
                         network->keep_configuration = KEEP_CONFIGURATION_YES;
                 else
@@ -386,7 +388,7 @@ int network_load_one(Manager *manager, OrderedHashmap **networks, const char *fi
                 .allmulticast = -1,
                 .promiscuous = -1,
 
-                .keep_configuration = _KEEP_CONFIGURATION_INVALID,
+                .keep_configuration = manager->keep_configuration,
 
                 .dhcp_duid.type = _DUID_TYPE_INVALID,
                 .dhcp_critical = -1,
index 4b00a980863e95ccbfdaec0fc03a2a5e3ef9bcb7..5f1328e39c6d5f4e757e38205837d550994df498 100644 (file)
@@ -6,6 +6,7 @@
 #include "net-condition.h"
 #include "networkd-address.h"
 #include "networkd-conf.h"
+#include "networkd-manager.h"
 #include "networkd-network.h"
 #include "strv.h"
 
@@ -166,11 +167,15 @@ static void test_config_parse_ether_addr(void) {
 }
 
 static void test_config_parse_address_one(const char *rvalue, int family, unsigned n_addresses, const union in_addr_union *u, unsigned char prefixlen) {
+        _cleanup_(manager_freep) Manager *manager = NULL;
         _cleanup_(network_unrefp) Network *network = NULL;
 
+        assert_se(manager_new(&manager, /* test_mode = */ true) >= 0);
         assert_se(network = new0(Network, 1));
         network->n_ref = 1;
+        network->manager = manager;
         assert_se(network->filename = strdup("hogehoge.network"));
+
         assert_se(config_parse_match_ifnames("network", "filename", 1, "section", 1, "Name", 0, "*", &network->match.ifname, network) == 0);
         assert_se(config_parse_address("network", "filename", 1, "section", 1, "Address", 0, rvalue, network, network) == 0);
         assert_se(ordered_hashmap_size(network->addresses_by_section) == 1);