merge: branch 'initrd-services-dbus-fix'

systemd: install initrd services using a generator

Closes #1814

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/2312
This commit is contained in:
Beniamino Galvani 2025-11-20 17:41:45 +00:00
commit b41a5ec2d4
9 changed files with 73 additions and 17 deletions

View file

@ -675,6 +675,7 @@ Preferably use nmcli instead.
-Dsession_tracking=systemd \ -Dsession_tracking=systemd \
-Dsuspend_resume=systemd \ -Dsuspend_resume=systemd \
-Dsystemdsystemunitdir=%{_unitdir} \ -Dsystemdsystemunitdir=%{_unitdir} \
-Dsystemdsystemgeneratordir=%{_systemdgeneratordir} \
-Dsystem_ca_path=/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem \ -Dsystem_ca_path=/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem \
-Ddbus_conf_dir=%{dbus_sys_dir} \ -Ddbus_conf_dir=%{dbus_sys_dir} \
-Dtests=yes \ -Dtests=yes \
@ -747,6 +748,7 @@ rm -f %{buildroot}%{_libdir}/pppd/%{ppp_version}/*.la
rm -f %{buildroot}%{nmplugindir}/*.la rm -f %{buildroot}%{nmplugindir}/*.la
# Don't use the *-initrd.service files yet, wait dracut to support them # Don't use the *-initrd.service files yet, wait dracut to support them
rm -f %{buildroot}%{_systemdgeneratordir}/nm-initrd-generator.sh
rm -f %{buildroot}%{_unitdir}/NetworkManager-config-initrd.service rm -f %{buildroot}%{_unitdir}/NetworkManager-config-initrd.service
rm -f %{buildroot}%{_unitdir}/NetworkManager-initrd.service rm -f %{buildroot}%{_unitdir}/NetworkManager-initrd.service
rm -f %{buildroot}%{_unitdir}/NetworkManager-wait-online-initrd.service rm -f %{buildroot}%{_unitdir}/NetworkManager-wait-online-initrd.service

View file

@ -398,6 +398,7 @@ meson setup\
-Dsession_tracking=systemd \ -Dsession_tracking=systemd \
-Dsuspend_resume=systemd \ -Dsuspend_resume=systemd \
-Dsystemdsystemunitdir=/usr/lib/systemd/system \ -Dsystemdsystemunitdir=/usr/lib/systemd/system \
-Dsystemdsystemgeneratordir=/usr/lib/systemd/system-generators \
-Dsystem_ca_path=/etc/pki/tls/cert.pem \ -Dsystem_ca_path=/etc/pki/tls/cert.pem \
-Ddbus_conf_dir="$P_DBUS_SYS_DIR" \ -Ddbus_conf_dir="$P_DBUS_SYS_DIR" \
-Dtests=yes \ -Dtests=yes \

View file

@ -1,10 +1,10 @@
[Unit] [Unit]
Description=NetworkManager Configuration (initrd) Description=NetworkManager Configuration (initrd)
AssertPathExists=/etc/initrd-release
DefaultDependencies=no DefaultDependencies=no
Wants=systemd-journald.socket Wants=systemd-journald.socket
After=systemd-journald.socket After=systemd-journald.socket
Before=systemd-udevd.service systemd-udev-trigger.service Before=systemd-udevd.service systemd-udev-trigger.service
ConditionPathExists=/etc/initrd-release
[Service] [Service]
Type=oneshot Type=oneshot
@ -22,6 +22,3 @@ ExecStartPost=/bin/sh -c ' \
fi \ fi \
' '
RemainAfterExit=yes RemainAfterExit=yes
[Install]
WantedBy=initrd.target

View file

@ -1,11 +1,11 @@
[Unit] [Unit]
Description=NetworkManager (initrd) Description=NetworkManager (initrd)
AssertPathExists=/etc/initrd-release
DefaultDependencies=no DefaultDependencies=no
Wants=systemd-udev-trigger.service network.target Wants=systemd-udev-trigger.service network.target
After=systemd-udev-trigger.service network-pre.target dbus.service NetworkManager-config-initrd.service After=systemd-udev-trigger.service network-pre.target dbus.service NetworkManager-config-initrd.service
Before=network.target Before=network.target
BindsTo=dbus.service BindsTo=dbus.service
ConditionPathExists=/etc/initrd-release
ConditionPathExists=/run/NetworkManager/initrd/neednet ConditionPathExists=/run/NetworkManager/initrd/neednet
ConditionPathExistsGlob=|/usr/lib/NetworkManager/system-connections/* ConditionPathExistsGlob=|/usr/lib/NetworkManager/system-connections/*
ConditionPathExistsGlob=|/run/NetworkManager/system-connections/* ConditionPathExistsGlob=|/run/NetworkManager/system-connections/*
@ -22,11 +22,3 @@ Environment=NM_CONFIG_ENABLE_TAG=initrd
Restart=on-failure Restart=on-failure
ProtectSystem=true ProtectSystem=true
ProtectHome=read-only ProtectHome=read-only
[Install]
WantedBy=initrd.target
# We want to enable NetworkManager-wait-online-initrd.service whenever this
# service is enabled. NetworkManager-wait-online-initrd.service has
# WantedBy=network-online.target, so enabling it only has an effect if
# network-online.target itself is enabled or pulled in by some other unit.
Also=NetworkManager-config-initrd.service NetworkManager-wait-online-initrd.service

View file

@ -1,10 +1,10 @@
[Unit] [Unit]
Description=NetworkManager Wait Online (initrd) Description=NetworkManager Wait Online (initrd)
AssertPathExists=/etc/initrd-release
DefaultDependencies=no DefaultDependencies=no
Requires=NetworkManager-initrd.service Requires=NetworkManager-initrd.service
After=NetworkManager-initrd.service After=NetworkManager-initrd.service
Before=network-online.target Before=network-online.target
ConditionPathExists=/etc/initrd-release
ConditionPathExists=/run/NetworkManager/initrd/neednet ConditionPathExists=/run/NetworkManager/initrd/neednet
[Service] [Service]
@ -21,6 +21,3 @@ Type=oneshot
ExecStart=@bindir@/nm-online -s -q ExecStart=@bindir@/nm-online -s -q
RemainAfterExit=yes RemainAfterExit=yes
Environment=NM_ONLINE_TIMEOUT=3600 Environment=NM_ONLINE_TIMEOUT=3600
[Install]
WantedBy=initrd.target network-online.target

View file

@ -383,6 +383,14 @@ if install_systemdunitdir and systemd_systemdsystemunitdir == ''
systemd_systemdsystemunitdir = systemd_dep.get_variable(pkgconfig: 'systemdsystemunitdir', pkgconfig_define: ['rootprefix', nm_prefix]) systemd_systemdsystemunitdir = systemd_dep.get_variable(pkgconfig: 'systemdsystemunitdir', pkgconfig_define: ['rootprefix', nm_prefix])
endif endif
systemd_systemdsystemgeneratordir = get_option('systemdsystemgeneratordir')
install_systemdgeneratordir = (systemd_systemdsystemgeneratordir != 'no')
if install_systemdgeneratordir and systemd_systemdsystemgeneratordir == ''
assert(systemd_dep.found(), 'systemd required but not found, please provide a valid systemd user generator dir or disable it')
systemd_systemdsystemgeneratordir = systemd_dep.get_variable(pkgconfig: 'systemdsystemgeneratordir', pkgconfig_define: ['rootprefix', nm_prefix])
endif
enable_systemd_journal = get_option('systemd_journal') enable_systemd_journal = get_option('systemd_journal')
if enable_systemd_journal if enable_systemd_journal
assert(libsystemd_dep.found(), 'Missing systemd-journald support') assert(libsystemd_dep.found(), 'Missing systemd-journald support')
@ -1059,6 +1067,7 @@ output = '\nSystem paths:\n'
output += ' prefix: ' + nm_prefix + '\n' output += ' prefix: ' + nm_prefix + '\n'
output += ' exec_prefix: ' + nm_prefix + '\n' output += ' exec_prefix: ' + nm_prefix + '\n'
output += ' systemdunitdir: ' + systemd_systemdsystemunitdir + '\n' output += ' systemdunitdir: ' + systemd_systemdsystemunitdir + '\n'
output += ' systemdgeneratordir: ' + systemd_systemdsystemgeneratordir + '\n'
output += ' udev_dir: ' + udev_udevdir + '\n' output += ' udev_dir: ' + udev_udevdir + '\n'
output += ' nmbinary: ' + nm_pkgsbindir + '\n' output += ' nmbinary: ' + nm_pkgsbindir + '\n'
output += ' nmconfdir: ' + nm_pkgconfdir + '\n' output += ' nmconfdir: ' + nm_pkgconfdir + '\n'

View file

@ -1,5 +1,6 @@
# system paths # system paths
option('systemdsystemunitdir', type: 'string', value: '', description: 'Directory for systemd service files') option('systemdsystemunitdir', type: 'string', value: '', description: 'Directory for systemd service files')
option('systemdsystemgeneratordir', type: 'string', value: '', description: 'Directory for systemd generator files')
option('system_ca_path', type: 'string', value: '/etc/ssl/certs', description: 'path to system CA certificates') option('system_ca_path', type: 'string', value: '/etc/ssl/certs', description: 'path to system CA certificates')
option('udev_dir', type: 'string', value: '', description: 'Absolute path of the udev base directory. Set to \'no\' not to install the udev rule') option('udev_dir', type: 'string', value: '', description: 'Absolute path of the udev base directory. Set to \'no\' not to install the udev rule')
option('dbus_conf_dir', type: 'string', value: '', description: 'where D-Bus system.d directory is') option('dbus_conf_dir', type: 'string', value: '', description: 'where D-Bus system.d directory is')

View file

@ -46,3 +46,10 @@ executable(
install: true, install: true,
install_dir: nm_libexecdir, install_dir: nm_libexecdir,
) )
if install_systemdgeneratordir
install_data(
'nm-initrd-generator.sh',
install_dir: systemd_systemdsystemgeneratordir,
)
endif

View file

@ -0,0 +1,50 @@
#!/bin/bash
# SPDX-License-Identifier: LGPL-2.1-or-later
# initrd-specific units
initrd_units=(
NetworkManager-config-initrd.service
NetworkManager-initrd.service
NetworkManager-wait-online-initrd.service
)
# host-specific units
host_units=(
NetworkManager.service
NetworkManager-dispatcher.service
NetworkManager-wait-online.service
)
# Get generator normal directory
normal_dir=$1
# Since NetworkManager-initrd.service and NetworkManager.service are allocated
# for the same bus name org.freedesktop.NetworkManager, we should mask one of
# them depending on if we are in the initrd or on the host.
if [ "$SYSTEMD_IN_INITRD" != 1 ]; then
# Mask initrd units in the host
for unit in "${initrd_units[@]}"; do
ln -s /dev/null "$normal_dir"/"$unit" 2> /dev/null
done
# Nothing else to do
exit 0
fi
# Mask host units in the initrd
for unit in "${host_units[@]}"; do
ln -s /dev/null "$normal_dir"/"$unit" 2> /dev/null
done
# Install initrd units in the unit file hierarchy
mkdir -p "$normal_dir"/initrd.target.wants
mkdir -p "$normal_dir"/network-online.target.wants
for unit in "${initrd_units[@]}"; do
ln -s /usr/lib/systemd/system/"$unit" \
"$normal_dir"/initrd.target.wants/"$unit"
if [ "$unit" = "NetworkManager-wait-online-initrd.service" ]; then
ln -s /usr/lib/systemd/system/"$unit" \
"$normal_dir"/network-online.target.wants/"$unit"
fi
done
exit 0