#! /bin/bash # Copyright (C) 2011 GRNET S.A. # # This program 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 2 of the License, or # (at your option) any later version. # # This program 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 this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # 02110-1301, USA. ### BEGIN TASK INFO # Provides: AssignHostname # RunBefore: EnforcePersonality # RunAfter: InstallUnattend # Short-Description: Assign Hostname/Computer Name to the instance ### END TASK INFO set -e . "@commondir@/common.sh" trap task_cleanup EXIT report_task_start # Check if the task should be prevented from running. check_if_excluded windows_hostname() { local target password unattend tmp_unattend namespace target="$1" password="$2" tmp_unattend=$(mktemp) add_cleanup rm "$tmp_unattend" echo -n "Assigning new computer name..." namespace="urn:schemas-microsoft-com:unattend" unattend=$(get_unattend "$target") if [ -z "$unattend" ]; then log_error "Unattend.xml is missing." fi "$XMLSTARLET" ed -N x=$namespace -u "/x:unattend/x:settings/x:component/x:ComputerName" -v "$password" "$unattend" > "$tmp_unattend" cat "$tmp_unattend" > "$unattend" echo done } linux_hostname() { local target hostname distro target="$1" hostname="$2" if [ -f "$target/etc/hostname" ]; then echo "$hostname" > "$target/etc/hostname" else distro=$(get_base_distro "$target") case "$distro" in redhat) sed -ie "s/HOSTNAME=.*$/HOSTNAME=$hostname/g" "$target/etc/sysconfig/network";; slackware|suse) #local domain=$(sed -e 's/^[^\.]*//g' < /etc/HOSTNAME) # In slackware hostname and domain name are joined together. For now # I will not retain the domain name. echo "$hostname" > "${target}/etc/HOSTNAME";; gentoo) sed -ie "s/\(\(HOSTNAME\)\|\(hostname\)\)=.*$/\1=\"$hostname\"/" "$target/etc/conf.d/hostname";; arch) if [ -f "$target/etc/rc.conf" ]; then sed -ie "s/^HOSTNAME=.*$/HOSTNAME=\"$hostname\"/" "$target/etc/rc.conf" else # In new versions of arch, /etc/rc.conf is missing echo "$hostname" > "$target/etc/hostname" fi if grep "^127\.0\.0\.1[ \t]*" "$target/etc/hosts" > /dev/null; then sed -ie "s/127\.0\.0\.1[ \t]*.*$/127.0.0.1\t$hostname/" "$target/etc/hosts" else echo -e "127.0.0.1\t$hostname" >> "$target/etc/hosts" fi;; *) log_error "Don't know how to assign hostname. Unknown linux distribution.";; esac fi # Some Linux distributions assign the hostname to 127.0.1.1 in order to be # resolvable to an IP address. Lets replace this if found in /etc/hosts sed -ie "s/^[[:blank:]]*127\.0\.1\.1[[:blank:]].\+$/127.0.1.1\t$hostname/" "$target/etc/hosts" } if [ ! -d "$SNF_IMAGE_TARGET" ]; then log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing" fi if [ -z "$SNF_IMAGE_HOSTNAME" ]; then log_error "Hostname is missing" fi if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then windows_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME" elif [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "linux" ]; then linux_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME" fi exit 0 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :