[__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'" \ "&& hostnamectl set-hostname '$name_should'" \
"|| 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'" echo "hostname '$name_should'"
;; ;;
macosx) macosx)
@ -75,8 +75,11 @@ in
solaris) solaris)
echo "uname -S '$name_should'" echo "uname -S '$name_should'"
;; ;;
suse) slackware|suse|opensuse-leap)
echo 'hostname -F /etc/HOSTNAME' # 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. # Fall back to set the hostname using hostnamectl, if available.
@ -84,11 +87,12 @@ in
then then
# Don't use hostnamectl as the primary means to set the hostname for # Don't use hostnamectl as the primary means to set the hostname for
# systemd systems, because it cannot be trusted to work reliably and # systemd systems, because it cannot be trusted to work reliably and
# exit with non-zero when it fails. # exit with non-zero when it fails (e.g. hostname too long,
# Who invented a tool that needs dbus to set the hostname anyway… # D-Bus failure, etc.).
echo "hostnamectl set-hostname \"\$(cat /etc/hostname)\"" 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 else
printf "echo 'Unsupported OS: %s' >&2\nexit 1\n" "$os" printf "echo 'Unsupported OS: %s' >&2\nexit 1\n" "$os"
fi fi

View file

@ -31,6 +31,9 @@ set_hostname_systemd() {
} }
os=$(cat "$__global/explorer/os") 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") max_len=$(cat "$__object/explorer/max_len")
has_hostnamectl=$(cat "$__object/explorer/has_hostnamectl") has_hostnamectl=$(cat "$__object/explorer/has_hostnamectl")
@ -45,8 +48,21 @@ else
# Hostname is FQDN # Hostname is FQDN
name_should="${__target_host}" 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%%.*}" name_should="${__target_host%%.*}"
;; ;;
esac esac
@ -127,28 +143,42 @@ in
--key 'hostname' \ --key 'hostname' \
--value "\"$name_should\"" --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 __file /etc/myname --state absent
;; ;;
openbsd) openbsd)
echo "$name_should" | __file /etc/myname --source - echo "$name_should" | __file /etc/myname --source -
;; ;;
slackware) slackware)
# We write the FQDN into /etc/HOSTNAME. # We write the FQDN into /etc/HOSTNAME. But /etc/rc.d/rc.M will only
# But /etc/rc.d/rc.M will only read the first component from this file # read the first component from this file and set it as the running
# and set it as the running hostname on boot. # hostname on boot.
echo "$name_should" | __file /etc/HOSTNAME --source - echo "$name_should" | __file /etc/HOSTNAME --source -
;; ;;
solaris) solaris)
echo "$name_should" | __file /etc/nodename --source - echo "$name_should" | __file /etc/nodename --source -
;; ;;
suse) suse|opensuse-leap)
# We write into /etc/HOSTNAME for backwards-compatibility. Modern SuSE # Modern SuSE provides /etc/HOSTNAME as a symlink for
# has hostnamectl anyway and symlinks /etc/HOSTNAME to /etc/hostname. # backwards-compatibility. Unfortunately it cannot be used
echo "$name_should" | __file /etc/HOSTNAME --source - # 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" if test -n "$has_hostnamectl"
then then
set_hostname_systemd "$name_should" set_hostname_systemd "$name_should"