2017-06-28 11:19:32 +00:00
|
|
|
#!/bin/sh -e
|
2014-01-20 12:47:45 +00:00
|
|
|
#
|
|
|
|
# 2012 Steven Armstrong (steven-cdist at armstrong.cc)
|
2014-01-20 12:50:42 +00:00
|
|
|
# 2014 Nico Schottelius (nico-cdist at schottelius.org)
|
2019-08-15 14:15:47 +00:00
|
|
|
# 2019 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
|
2014-01-20 12:47:45 +00:00
|
|
|
#
|
|
|
|
# This file is part of cdist.
|
|
|
|
#
|
|
|
|
# cdist is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# cdist is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
2019-10-26 15:11:35 +00:00
|
|
|
set_hostname_systemd() {
|
2019-10-26 15:25:44 +00:00
|
|
|
echo "$1" | __file /etc/hostname --source -
|
2019-10-26 15:11:35 +00:00
|
|
|
}
|
2019-10-24 17:44:51 +00:00
|
|
|
|
2020-11-11 13:09:26 +00:00
|
|
|
os=$(cat "${__global:?}/explorer/os")
|
2019-11-17 13:46:37 +00:00
|
|
|
|
2020-11-11 13:09:26 +00:00
|
|
|
max_len=$(cat "${__object:?}/explorer/max_len")
|
|
|
|
has_hostnamectl=$(cat "${__object:?}/explorer/has_hostnamectl")
|
2019-10-24 23:41:26 +00:00
|
|
|
|
2020-11-11 13:09:26 +00:00
|
|
|
if test -s "${__object:?}/parameter/name"
|
2019-10-26 15:11:35 +00:00
|
|
|
then
|
2020-11-11 13:09:26 +00:00
|
|
|
name_should=$(cat "${__object:?}/parameter/name")
|
2014-04-24 23:48:58 +00:00
|
|
|
else
|
2020-11-11 13:09:26 +00:00
|
|
|
case ${os}
|
2019-10-06 16:36:43 +00:00
|
|
|
in
|
2019-10-06 16:23:56 +00:00
|
|
|
# RedHat-derivatives and BSDs
|
2020-11-11 13:20:18 +00:00
|
|
|
(centos|fedora|redhat|scientific|freebsd|netbsd|openbsd|slackware|suse)
|
2019-10-06 16:23:56 +00:00
|
|
|
# Hostname is FQDN
|
2020-11-11 13:09:26 +00:00
|
|
|
name_should=${__target_host:?}
|
|
|
|
;;
|
2019-10-06 16:23:56 +00:00
|
|
|
*)
|
2019-11-17 13:46:37 +00:00
|
|
|
# Hostname is only first component of FQDN on all other systems.
|
2020-11-11 13:09:26 +00:00
|
|
|
name_should=${__target_host:?}
|
|
|
|
name_should=${name_should%%.*}
|
|
|
|
;;
|
2016-09-02 15:39:22 +00:00
|
|
|
esac
|
2014-04-24 23:48:58 +00:00
|
|
|
fi
|
|
|
|
|
2020-11-11 13:09:26 +00:00
|
|
|
if test -n "${max_len}" && test "$(printf '%s' "${name_should}" | wc -c)" -gt "${max_len}"
|
2019-10-24 17:44:51 +00:00
|
|
|
then
|
2019-10-26 15:11:35 +00:00
|
|
|
printf "Host name too long. Up to %u characters allowed.\n" "${max_len}" >&2
|
|
|
|
exit 1
|
2019-10-24 17:44:51 +00:00
|
|
|
fi
|
2014-01-20 12:47:45 +00:00
|
|
|
|
2020-11-11 13:09:26 +00:00
|
|
|
case ${os}
|
2019-10-06 16:36:43 +00:00
|
|
|
in
|
2020-11-11 13:09:26 +00:00
|
|
|
(alpine|debian|devuan|ubuntu|void)
|
|
|
|
echo "${name_should}" | __file /etc/hostname --source -
|
|
|
|
;;
|
|
|
|
(archlinux)
|
|
|
|
if test -n "${has_hostnamectl}"
|
2019-10-26 15:11:35 +00:00
|
|
|
then
|
2020-11-11 13:09:26 +00:00
|
|
|
set_hostname_systemd "${name_should}"
|
2019-10-26 15:11:35 +00:00
|
|
|
else
|
2019-11-16 22:31:58 +00:00
|
|
|
echo 'Ancient ArchLinux variants without hostnamectl are not supported.' >&2
|
|
|
|
exit 1
|
2019-10-26 15:11:35 +00:00
|
|
|
# Only for ancient ArchLinux, write to /etc/rc.conf on pre-systemd
|
2019-10-27 19:12:10 +00:00
|
|
|
# versions. There are some versions which use /etc/hostname but not
|
2019-11-16 22:31:58 +00:00
|
|
|
# systemd. It is unclear which ones these are.
|
|
|
|
|
|
|
|
# __key_value '/etc/rc.conf:HOSTNAME' \
|
|
|
|
# --file /etc/rc.conf \
|
|
|
|
# --delimiter '=' --exact_delimiter \
|
|
|
|
# --key 'HOSTNAME' \
|
|
|
|
# --value "\"$name_should\""
|
2019-10-26 15:11:35 +00:00
|
|
|
fi
|
|
|
|
;;
|
2020-11-11 13:09:26 +00:00
|
|
|
(centos|fedora|redhat|scientific)
|
|
|
|
if test -z "${has_hostnamectl}"
|
2019-10-12 16:47:27 +00:00
|
|
|
then
|
|
|
|
# Only write to /etc/sysconfig/network on non-systemd versions.
|
|
|
|
# On systemd-based versions this entry is ignored.
|
2019-10-27 12:16:05 +00:00
|
|
|
__key_value '/etc/sysconfig/network:HOSTNAME' \
|
2019-10-12 16:47:27 +00:00
|
|
|
--file /etc/sysconfig/network \
|
|
|
|
--delimiter '=' --exact_delimiter \
|
|
|
|
--key HOSTNAME \
|
2020-11-11 13:09:26 +00:00
|
|
|
--value "\"${name_should}\""
|
2019-10-26 15:11:35 +00:00
|
|
|
else
|
2020-11-11 13:09:26 +00:00
|
|
|
set_hostname_systemd "${name_should}"
|
2019-10-12 16:47:27 +00:00
|
|
|
fi
|
2020-11-11 13:09:26 +00:00
|
|
|
;;
|
|
|
|
(gentoo)
|
2019-11-16 13:58:30 +00:00
|
|
|
# Only write to /etc/conf.d/hostname on OpenRC-based installations.
|
|
|
|
# On systemd use hostnamectl(1) in gencode-remote.
|
2020-11-11 13:09:26 +00:00
|
|
|
if test -z "${has_hostnamectl}"
|
2019-10-12 16:47:27 +00:00
|
|
|
then
|
2019-10-27 12:16:05 +00:00
|
|
|
__key_value '/etc/conf.d/hostname:hostname' \
|
2019-10-12 16:47:27 +00:00
|
|
|
--file /etc/conf.d/hostname \
|
|
|
|
--delimiter '=' --exact_delimiter \
|
|
|
|
--key 'hostname' \
|
2020-11-11 13:09:26 +00:00
|
|
|
--value "\"${name_should}\""
|
2019-10-26 15:11:35 +00:00
|
|
|
else
|
|
|
|
set_hostname_systemd "$name_should"
|
2019-10-12 16:47:27 +00:00
|
|
|
fi
|
2020-11-11 13:09:26 +00:00
|
|
|
;;
|
|
|
|
(freebsd)
|
2019-10-27 12:16:05 +00:00
|
|
|
__key_value '/etc/rc.conf:hostname' \
|
2018-03-26 06:53:31 +00:00
|
|
|
--file /etc/rc.conf \
|
2019-10-06 16:01:58 +00:00
|
|
|
--delimiter '=' --exact_delimiter \
|
2018-03-26 06:53:31 +00:00
|
|
|
--key 'hostname' \
|
2020-11-11 13:09:26 +00:00
|
|
|
--value "\"${name_should}\""
|
|
|
|
;;
|
|
|
|
(macosx)
|
2019-10-27 12:16:05 +00:00
|
|
|
# handled in gencode-remote
|
2020-11-11 13:09:26 +00:00
|
|
|
;;
|
|
|
|
(netbsd)
|
2019-10-27 12:16:05 +00:00
|
|
|
__key_value '/etc/rc.conf:hostname' \
|
2019-10-05 18:10:52 +00:00
|
|
|
--file /etc/rc.conf \
|
2019-10-06 16:01:58 +00:00
|
|
|
--delimiter '=' --exact_delimiter \
|
2019-10-05 18:10:52 +00:00
|
|
|
--key 'hostname' \
|
2020-11-11 13:09:26 +00:00
|
|
|
--value "\"${name_should}\""
|
2019-10-26 15:11:35 +00:00
|
|
|
|
2019-11-17 13:46:37 +00:00
|
|
|
# To avoid confusion, ensure that the hostname is only stored once.
|
2019-10-05 18:10:52 +00:00
|
|
|
__file /etc/myname --state absent
|
2020-11-11 13:09:26 +00:00
|
|
|
;;
|
|
|
|
(openbsd)
|
|
|
|
echo "${name_should}" | __file /etc/myname --source -
|
|
|
|
;;
|
|
|
|
(openwrt)
|
|
|
|
__uci system.@system[0].hostname --value "${name_should}"
|
2020-06-11 19:27:53 +00:00
|
|
|
# --transaction hostname
|
2020-11-11 13:09:26 +00:00
|
|
|
;;
|
|
|
|
(slackware)
|
2019-11-17 13:46:37 +00:00
|
|
|
# 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.
|
2020-11-11 13:09:26 +00:00
|
|
|
echo "${name_should}" | __file /etc/HOSTNAME --source -
|
|
|
|
;;
|
|
|
|
(solaris)
|
|
|
|
echo "${name_should}" | __file /etc/nodename --source -
|
|
|
|
;;
|
2020-11-11 13:15:02 +00:00
|
|
|
(suse)
|
2020-11-11 13:20:18 +00:00
|
|
|
if test -s "${__global:?}/explorer/os_release"
|
|
|
|
then
|
|
|
|
# shellcheck source=/dev/null
|
|
|
|
os_version=$(. "${__global:?}/explorer/os_release" && echo "${VERSION}")
|
|
|
|
else
|
|
|
|
os_version=$(sed -n 's/^VERSION\ *=\ *//p' "${__global:?}/explorer/os_version")
|
|
|
|
fi
|
|
|
|
os_major=$(expr "${os_version}" : '\([0-9]\{1,\}\)')
|
|
|
|
|
|
|
|
# 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
|
|
|
|
# strip away everything but the first part from $name_should
|
|
|
|
name_should=${name_should%%.*}
|
|
|
|
fi
|
|
|
|
|
2019-11-17 13:46:37 +00:00
|
|
|
# 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.
|
|
|
|
|
2020-11-11 13:09:26 +00:00
|
|
|
if test -n "${has_hostnamectl}" -a "${os_major}" -gt 12
|
2019-11-17 13:46:37 +00:00
|
|
|
then
|
2020-11-11 13:09:26 +00:00
|
|
|
hostname_file=/etc/hostname
|
2019-11-17 13:46:37 +00:00
|
|
|
else
|
2020-11-11 13:09:26 +00:00
|
|
|
hostname_file=/etc/HOSTNAME
|
2019-11-17 13:46:37 +00:00
|
|
|
fi
|
|
|
|
|
2020-11-11 13:09:26 +00:00
|
|
|
echo "${name_should}" | __file "${hostname_file}" --source -
|
|
|
|
;;
|
|
|
|
(*)
|
2019-11-17 13:46:37 +00:00
|
|
|
# On other operating systems we fall back to systemd's
|
|
|
|
# hostnamectl if available…
|
2020-11-11 13:09:26 +00:00
|
|
|
if test -n "${has_hostnamectl}"
|
2019-10-26 15:11:35 +00:00
|
|
|
then
|
2020-11-11 13:09:26 +00:00
|
|
|
set_hostname_systemd "${name_should}"
|
2019-10-26 15:11:35 +00:00
|
|
|
else
|
2020-11-11 13:09:26 +00:00
|
|
|
echo "Your operating system (${os}) is currently not supported by this type (${__type##*/})." >&2
|
|
|
|
echo "Please contribute an implementation for it if you can." >&2
|
|
|
|
exit 1
|
2019-10-26 15:11:35 +00:00
|
|
|
fi
|
2020-11-11 13:09:26 +00:00
|
|
|
;;
|
2014-01-20 12:47:45 +00:00
|
|
|
esac
|