[__hostname] Better support different versions of SuSE

This commit is contained in:
Dennis Camera 2019-11-17 14:46:37 +01:00
parent f18bdd1fad
commit a45e306123
2 changed files with 50 additions and 16 deletions

View file

@ -66,7 +66,7 @@ in
"&& hostnamectl set-hostname '$name_should'" \
"|| hostname '$name_should'"
;;
centos|fedora|redhat|scientific|freebsd|netbsd|openbsd|gentoo|slackware|void)
centos|fedora|redhat|scientific|freebsd|netbsd|openbsd|gentoo|void)
echo "hostname '$name_should'"
;;
macosx)
@ -75,8 +75,11 @@ in
solaris)
echo "uname -S '$name_should'"
;;
suse)
echo 'hostname -F /etc/HOSTNAME'
slackware|suse|opensuse-leap)
# We do not read from /etc/HOSTNAME, because the running
# hostname is the first component only while the file contains
# the FQDN.
echo "hostname '$name_should'"
;;
*)
# Fall back to set the hostname using hostnamectl, if available.
@ -84,11 +87,12 @@ in
then
# Don't use hostnamectl as the primary means to set the hostname for
# systemd systems, because it cannot be trusted to work reliably and
# exit with non-zero when it fails.
# Who invented a tool that needs dbus to set the hostname anyway…
# exit with non-zero when it fails (e.g. hostname too long,
# D-Bus failure, etc.).
echo "hostnamectl set-hostname \"\$(cat /etc/hostname)\""
echo "test \"\$(hostname)\" = \"\$(cat /etc/hostname)\" || hostname -F /etc/hostname"
echo "test \"\$(hostname)\" = \"\$(cat /etc/hostname)\"" \
" || hostname -F /etc/hostname"
else
printf "echo 'Unsupported OS: %s' >&2\nexit 1\n" "$os"
fi

View file

@ -31,6 +31,9 @@ set_hostname_systemd() {
}
os=$(cat "$__global/explorer/os")
os_version=$(cat "$__global/explorer/os_version")
os_major=$(echo "$os_version" | grep -o '^[0-9][0-9]*')
max_len=$(cat "$__object/explorer/max_len")
has_hostnamectl=$(cat "$__object/explorer/has_hostnamectl")
@ -45,8 +48,21 @@ else
# Hostname is FQDN
name_should="${__target_host}"
;;
suse|opensuse-leap)
# Classic SuSE stores the FQDN in /etc/HOSTNAME, while
# systemd does not. The running hostname is the first
# component in both cases.
# In versions before 15.x, the FQDN is stored in /etc/hostname.
if test -n "$has_hostnamectl" && test "$os_major" -ge 15 \
&& test "$os_major" -ne 42
then
name_should="${__target_host%%.*}"
else
name_should="${__target_host}"
fi
;;
*)
# Hostname is only first component of FQDN
# Hostname is only first component of FQDN on all other systems.
name_should="${__target_host%%.*}"
;;
esac
@ -127,28 +143,42 @@ in
--key 'hostname' \
--value "\"$name_should\""
# To avoid confusion, ensure that the hostname is only stored once
# To avoid confusion, ensure that the hostname is only stored once.
__file /etc/myname --state absent
;;
openbsd)
echo "$name_should" | __file /etc/myname --source -
;;
slackware)
# We write the FQDN into /etc/HOSTNAME.
# But /etc/rc.d/rc.M will only read the first component from this file
# and set it as the running hostname on boot.
# We write the FQDN into /etc/HOSTNAME. But /etc/rc.d/rc.M will only
# read the first component from this file and set it as the running
# hostname on boot.
echo "$name_should" | __file /etc/HOSTNAME --source -
;;
solaris)
echo "$name_should" | __file /etc/nodename --source -
;;
suse)
# We write into /etc/HOSTNAME for backwards-compatibility. Modern SuSE
# has hostnamectl anyway and symlinks /etc/HOSTNAME to /etc/hostname.
echo "$name_should" | __file /etc/HOSTNAME --source -
suse|opensuse-leap)
# Modern SuSE provides /etc/HOSTNAME as a symlink for
# backwards-compatibility. Unfortunately it cannot be used
# here as __file does not follow the symlink.
# Therefore, we use the presence of the hostnamectl binary as
# an indication of which file to use. This unfortunately does
# not work correctly on openSUSE 12.x which provides
# hostnamectl but not /etc/hostname.
if test -n "$has_hostnamectl" -a "$os_major" -gt 12
then
hostname_file='/etc/hostname'
else
hostname_file='/etc/HOSTNAME'
fi
echo "$name_should" | __file "$hostname_file" --source -
;;
*)
# On other operating systems we fall back to systemd's hostnamectl if available…
# On other operating systems we fall back to systemd's
# hostnamectl if available…
if test -n "$has_hostnamectl"
then
set_hostname_systemd "$name_should"