Skip to content

Commit

Permalink
Add support for cloud-init to preserve the hostname after reboots
Browse files Browse the repository at this point in the history
  • Loading branch information
iliajie committed Aug 20, 2023
1 parent 2bf9f3a commit cbaa616
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions slib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,17 @@ detect_ip () {
fi
}

# Set the hostname in cloud-init
set_hostname_cloud () {
# If cloud-init is installed, preserve the hostname Virtualmin sets
if [ -f "/etc/cloud/cloud.cfg" ]; then
if grep "^preserve_hostname: false" /etc/cloud/cloud.cfg >/dev/null; then
log_debug "Setting preserve_hostname to true in /etc/cloud/cloud.cfg"
sed -i "s/^preserve_hostname: false/preserve_hostname: true/" /etc/cloud/cloud.cfg
fi
fi
}

# Set the hostname
set_hostname () {
local i=0
Expand Down Expand Up @@ -501,6 +512,7 @@ set_hostname () {
hostname "$line"
echo "$line" > /etc/hostname
hostnamectl set-hostname "$line" 1>/dev/null 2>&1
set_hostname_cloud
detect_ip
shortname=$(echo "$line" | cut -d"." -f1)
if grep "^$address" /etc/hosts >/dev/null; then
Expand All @@ -526,6 +538,10 @@ is_fully_qualified () {
log_warning "Hostname cannot be *.localdomain."
return 1
;;
*.internal)
log_warning "Hostname cannot be *.internal."
return 1
;;
*.*)
log_debug "Hostname is fully qualified as $1"
return 0
Expand Down

0 comments on commit cbaa616

Please sign in to comment.