Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
filesystem: "root"
mode: 0755
path: "/etc/NetworkManager/dispatcher.d/90-long-hostname"
contents:
inline: |
#!/bin/bash
#
# On Google Compute Platform (GCP) the hostname may be too long (>63 chars).
# During firstboot the hostname is set in the initramfs before NetworkManager
# runs; on reboot affect nodes use 'localhost'. This hook is a simple work
# around: if the host name is longer than 63 characters, then the hostname
# is truncated at the _first_ dot.
#
# Additionally, this hook does not break DNS or cluster DNS resolution,
# since NetworkManager sets the appropriate /etc/resolv.conf settings.

IF=$1
STATUS=$2

log() { logger --tag "network-manager/$(basename $0)" "${@}"; }

# capture all eligible hostnames
if [[ ! "$(/bin/hostname)" =~ (localhost|localhost.local) ]]; then
log "hostname is already set"
exit 0
fi

if [[ ! "$STATUS" =~ (up|hostname|dhcp4-change|dhcp6-change) ]]; then
exit 0
fi

default_host="${DHCP4_HOST_NAME:-$DHCP6_HOST_NAME}"
# truncate the hostname to the first dot and than 64 characters.
host=$(echo ${default_host} | cut -f1 -d'.' | cut -c -63)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ echo "averylonghostnamewhichshouldprobablybelessbecausereallyitsprettycrazy.to.have.such.long.names" | cut -f1 -d'.' | cut -c -63
averylonghostnamewhichshouldprobablybelessbecausereallyitsprett
$ echo -n "averylonghostnamewhichshouldprobablybelessbecausereallyitsprettycrazy.to.have.such.long.names" | cut -f1 -d'.' | cut -c -63 | wc -c
64

Enforces 64 char limit. However, the next check on default host is >63. Should this check really be limiting to 63 instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than this, LGTM!

Copy link
Author

@darkmuggle darkmuggle May 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha! I did the same thing:

$ echo hi | cut -c -2 | wc -c
3

wc -c counts from 1

It warms my heart that we both thought of and ran the same test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh 👍


if [ "${#default_host}" -gt 63 ]; then
log "discovered hostname is longer than than 63 characters"
log "truncating ${default_host} => ${host}"
/bin/hostnamectl --transient set-hostname "${host}"
fi