[__hostname] Improve systemd hostname setting

This means we write to /etc/hostname before running hostnamectl, so that
it can overwrite the file if it wants to.
This commit is contained in:
Dennis Camera 2019-10-12 18:49:31 +02:00
parent 30c7d153e8
commit cee553a6dd

View file

@ -61,23 +61,29 @@ echo 'changed' >> "$__messages_out"
# First try to set the hostname using hostnamectl, if available. # First try to set the hostname using hostnamectl, if available.
if [ "$has_hostnamectl" ]; then if [ "$has_hostnamectl" ]; then
# Allow hostnamectl to fail silently. # Allow hostnamectl to fail silently.
# Who the fuck invented a tool that needs dbus to set the hostname anyway ... # Who the fuck invented a tool that needs dbus to set the hostname anyway
cat <<EOF # NOTE: We write the static hostname to /etc/hostname first in case
if hostnamectl set-hostname '$name_should'; then # hostnamectl fails. Fallback-code below will then adjust the running
printf '%s\\n' '$name_should' > /etc/hostname # hostname below.
exit 0 echo "printf '%s\\n' '$name_should' > /etc/hostname"
fi
EOF echo "hostnamectl set-hostname '$name_should' && exit 0"
fi fi
# Use the good old way to set the hostname. Also if hostnamectl fails. # Use the good old way to set the hostname. Also if hostnamectl fails.
case $os case $os
in in
alpine|archlinux|coreos|debian|devuan|ubuntu) alpine|archlinux|debian|devuan|ubuntu)
echo "printf '%s\\n' '$name_should' > /etc/hostname" echo "printf '%s\\n' '$name_should' > /etc/hostname"
echo "hostname -F /etc/hostname" echo "hostname -F /etc/hostname"
;; ;;
coreos|openbmc-phosphor)
# NOTE: systemd-only distros have the hostname already written above.
# But since hostamectl failed, we update the running hostname
# manually.
echo "hostname -F /etc/hostname"
;;
gentoo) gentoo)
# NOTE: Writing the hostname to file is done in the manifest for OpenRC. # NOTE: Writing the hostname to file is done in the manifest for OpenRC.
# For systemd hostnamectl should take care of that. # For systemd hostnamectl should take care of that.