network: enable KeepConfiguration= when running on network filesystem

Also, set KeepConfiguration=dhcp-on-stop by default when running in
initrd.

Fixes #21967.
This commit is contained in:
Yu Watanabe 2022-02-04 15:33:38 +09:00
parent 4e247216e5
commit ea853de57d
5 changed files with 47 additions and 7 deletions

View 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>

View 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,

View file

@ -28,6 +28,8 @@ struct Manager {
Hashmap *polkit_registry;
int ethtool_fd;
KeepConfiguration keep_configuration;
bool test_mode;
bool enumerating;
bool dirty;

View 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,

View 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);