Skip to content

Commit

Permalink
Merge pull request coreos#156 from darkmuggle/pr/spec2x-hostname
Browse files Browse the repository at this point in the history
Persist the first-boot hostname if not otherwise set
  • Loading branch information
Ben Howard authored Mar 10, 2020
2 parents 7715ed3 + 25a9511 commit 1f8184e
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions dracut/30ignition/persist-ifcfg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# ex: ts=8 sw=4 sts=4 et filetype=sh

type info >/dev/null 2>&1 || . /lib/net-lib.sh

cmdline=( $(</proc/cmdline) )

cmdline_arg() {
Expand Down Expand Up @@ -33,6 +35,34 @@ persist_ifcfg() {
return 0
fi

# Persist the hostname if the admin has elected to use the dracut method of
# defining the IP on the commandline, but only if Ignition hasn't set the hostname.
hpath="/sysroot/etc/hostname"
hname=$(< /proc/sys/kernel/hostname)
if [ ! -f "${hpath}" ]; then
for iface in $(ls /sys/class/net)
do
# Find the totems indicating that dracut set up the interface
iface_totems="/run/initramfs/net.${iface}"
[ -f "${iface_totems}.did-setup" ] || continue;
[ -f "${iface_totems}.hostname" ] || continue;

# The format of the file is _the command used to set the hostname_
# echo hostname > /proc/sys/kernel/hostname.
read _ iface_hostname _ < "${iface_totems}.hostname"

# Ensure that the hostname used by the kernel is the same one
# used by this interface. This guard is intended to protect against
# mixed `ip=` or where one is set via dhcp.
if [ "${iface_hostname}" == "${hname}" ]; then
echo "${hname}" > "${hpath}"
echo -ne '/etc/hostname/\0' >> /sysroot/etc/selinux/ignition.relabel
info "persisting hostname set by kargs for ${iface}"
break
fi
done
fi

# Unless overridden, propagate the kernel commandline networking into
# ifcfg files, so that users don't have to write the config in both kernel
# commandline *and* Ignition.
Expand Down

0 comments on commit 1f8184e

Please sign in to comment.