From 5cb9f3aaa33d8ca3ff1a55a5ef0ac2b7a53293e8 Mon Sep 17 00:00:00 2001 From: Vlastimil Holer Date: Thu, 21 Oct 2021 10:57:07 +0200 Subject: [PATCH] F #248: Restart syslog on hostname change Closes #248 --- src/etc/init.d/one-context##apk.one | 2 +- src/etc/init.d/one-context-local##apk.one | 4 +- src/etc/one-context.d/net-15-hostname | 48 +++++++++++++++++------ 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/etc/init.d/one-context##apk.one b/src/etc/init.d/one-context##apk.one index 2ac7fba..13abc67 100755 --- a/src/etc/init.d/one-context##apk.one +++ b/src/etc/init.d/one-context##apk.one @@ -21,7 +21,7 @@ name="OpenNebula contextualization" depend() { use one-context-local net before sshd - use logger + after logger keyword -stop -shutdown } diff --git a/src/etc/init.d/one-context-local##apk.one b/src/etc/init.d/one-context-local##apk.one index f2b38f1..30a919e 100755 --- a/src/etc/init.d/one-context-local##apk.one +++ b/src/etc/init.d/one-context-local##apk.one @@ -15,13 +15,13 @@ # See the License for the specific language governing permissions and # # limitations under the License. # #--------------------------------------------------------------------------- # -# + name="OpenNebula pre-networking contextualization" depend() { need localmount udev udev-trigger udev-settle before net - use logger + after logger keyword -stop -shutdown } diff --git a/src/etc/one-context.d/net-15-hostname b/src/etc/one-context.d/net-15-hostname index e429b9e..88bcec3 100755 --- a/src/etc/one-context.d/net-15-hostname +++ b/src/etc/one-context.d/net-15-hostname @@ -23,29 +23,53 @@ else SED_I="sed -i''" fi +# Detects suitable running syslog service and restarts it +# to propagate the changed hostname into the logs +# (otherwise old or default value remains until reboot) +function restart_syslog() { + for _name in syslog syslogd rsyslog; do + if LANG=C systemctl is-active "${_name}" >/dev/null 2>&1; then + systemctl restart "${_name}" && return + elif service "${_name}" status >/dev/null 2>&1; then + service "${_name}" restart && return + fi + done +} + function set_hostname() { - local hostname=$1 + local _hostname=$1 + + # remember currently set hostname to detect a change + local _old_hostname + _old_hostname=$(hostname) if [ -d /run/systemd/system/ ] && hostnamectl status >/dev/null 2>/dev/null; then - hostnamectl set-hostname --static "${hostname}" + hostnamectl set-hostname --static "${_hostname}" else if [ -f /etc/sysconfig/network ]; then eval "${SED_I} '/^HOSTNAME=.*$/d' /etc/sysconfig/network" - echo "HOSTNAME=${hostname}" >>/etc/sysconfig/network + echo "HOSTNAME=${_hostname}" >>/etc/sysconfig/network elif [ "${_kernel}" = 'FreeBSD' ]; then - sysrc hostname="${hostname}" + sysrc hostname="${_hostname}" else - echo "${hostname}" >/etc/hostname + echo "${_hostname}" >/etc/hostname fi - hostname "${hostname}" + hostname "${_hostname}" + fi + + # restart syslog if actual hostname changed + if [ "${_old_hostname}" != "$(hostname)" ]; then + restart_syslog fi } function set_domainname() { - domain=$1 + local _domain=$1 + + touch /etc/resolv.conf eval "${SED_I} -e '/^domain .*/d' /etc/resolv.conf" - echo "domain ${domain}" >>/etc/resolv.conf + echo "domain ${_domain}" >>/etc/resolv.conf } function get_first_ip() { @@ -131,6 +155,10 @@ if [ -n "${name}" ]; then domain='' fi + if [ -n "${domain}" ]; then + set_domainname "${domain}" + fi + # FreeBSD if [ "${_kernel}" = 'FreeBSD' ]; then set_hostname "${name}" @@ -138,10 +166,6 @@ if [ -n "${name}" ]; then set_hostname "${hostname}" fi - if [ -n "${domain}" ]; then - set_domainname "${domain}" - fi - if [ -n "${DNS_HOSTNAME}" ]; then host_ip=$first_ip else