Skip to content

Commit

Permalink
coreos-teardown-initramfs-network: also propagate hostname
Browse files Browse the repository at this point in the history
This is a forward port of 1f8184e (coreos#156). If the admin specified
a hostname in the ip= karg static networking config and no hostname
was specified via Ignition then we'll make sure the hostname from
the ip= karg gets applied to the real root (persistently).

Ideally in the future there will be better support for this in
NetworkManager itself as the `network-legacy` dracut module did
at least provide more support for setting the hostname via ip=
kargs than `network-manager` currently does. The discussion
about this problem is in [1]. The fix for that will most likely
implicate changes to the propagation code introduced here.

Fixes: coreos/fedora-coreos-tracker#466

[1] https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/419
  • Loading branch information
dustymabe committed Apr 23, 2020
1 parent 68828c1 commit 657eacd
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion dracut/30ignition/coreos-teardown-initramfs-network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# ex: ts=8 sw=4 sts=4 et filetype=sh

# Load dracut library. Using getargbool() from dracut-lib.
# Load dracut libraries. Using getargbool() and getargs() from
# dracut-lib and ip_to_var() from net-lib
type getargbool &>/dev/null || . /lib/dracut-lib.sh
type ip_to_var &>/dev/null || . /lib/net-lib.sh

# Use tmpfiles.d to schedule SELinux relabel of files
schedule_selinux_relabel() {
Expand Down Expand Up @@ -39,6 +41,44 @@ propagate_initramfs_networking() {
fi
}

# Propagate the ip= karg hostname if desired. The policy here is:
#
# - IF a hostname is specified in static networking ip= kargs
# - AND no hostname was set via Ignition (realroot `/etc/hostname`)
# - THEN we make the last hostname specified in an ip= karg apply
# permanently by writing it into `/etc/hostname`
#
# This may no longer be needed when the following bug is fixed:
# https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/419
propagate_initramfs_hostname() {
if [ -e '/sysroot/etc/hostname' ]; then
echo "info: hostname is defined in the real root"
echo "info: will not attempt to propagate initramfs hostname"
return 0
fi
# Detect if any hostname was provided via static ip= kargs
# run in a subshell so we don't pollute our environment
hostnamefile=$(mktemp)
(
last_nonempty_hostname=''
# Inspired from ifup.sh from the 40network dracut module. Note that
# $hostname from ip_to_var will only be nonempty for static networking.
for iparg in $(getargs ip=); do
ip_to_var $iparg
[ -n "$hostname" ] && last_nonempty_hostname="$hostname"
done
echo -n "$last_nonempty_hostname" > $hostnamefile
)
hostname=$(<$hostnamefile); rm $hostnamefile
if [ -n "$hostname" ]; then
echo "info: propagating initramfs hostname (${hostname}) to the real root"
echo $hostname > /sysroot/etc/hostname
schedule_selinux_relabel /etc/hostname
else
echo "info: no initramfs hostname information to propagate"
fi
}

down_interface() {
echo "info: taking down network device: $1"
# On recommendation from the NM team let's try to delete the device
Expand Down Expand Up @@ -90,6 +130,7 @@ main() {
echo "info: coreos.no_persist_ip karg detected"
echo "info: skipping propagating initramfs settings"
else
propagate_initramfs_hostname
propagate_initramfs_networking
fi

Expand Down

0 comments on commit 657eacd

Please sign in to comment.