3 # Copyright (C) 2011 GRNET S.A.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 # Provides: AssignHostname
22 # RunBefore: EnforcePersonality
23 # RunAfter: InstallUnattend
24 # Short-Description: Assign Hostname/Computer Name to the instance
28 . "@commondir@/common.sh"
30 trap task_cleanup EXIT
33 # Check if the task should be prevented from running.
40 local tmp_unattend=$(mktemp) || exit 1
41 add_cleanup rm "$tmp_unattend"
43 echo -n "Assigning new computer name..."
45 local namespace="urn:schemas-microsoft-com:unattend"
47 unattend=$(get_unattend "$target")
48 if [ -z "$unattend" ]; then
49 log_error "Unattend.xml is missing."
52 "$XMLSTARLET" ed -N x=$namespace -u "/x:unattend/x:settings/x:component/x:ComputerName" -v "$password" "$unattend" > "$tmp_unattend"
54 cat "$tmp_unattend" > "$unattend"
62 local distro=$(get_base_distro "$target")
66 echo "$hostname" > "$target/etc/hostname";;
68 sed -ie "s/HOSTNAME=.*$/HOSTNAME=$hostname/g" "$target/etc/sysconfig/network";;
70 #local domain=$(sed -e 's/^[^\.]*//g' < /etc/HOSTNAME)
72 # In slackware hostname and domain name are joined together. For now
73 # I will not retain the domain name.
74 echo "$hostname" > "${target}/etc/HOSTNAME";;
76 sed -ie "s/\(\(HOSTNAME\)\|\(hostname\)\)=.*$/\1=\"$hostname\"/" "$target/etc/conf.d/hostname";;
78 # In newer archlinux systems /etc/hostname is used to assign the hostname
79 if [[ -f "$target/etc/hostname" ]]; then
80 echo "$hostname" > "$target/etc/hostname"
82 # Older ones use the HOSTNAME variable in /etc/rc.conf
83 sed -ie "s/^HOSTNAME=.*$/HOSTNAME=\"$hostname\"/" "$target/etc/rc.conf"
84 if grep "^127\.0\.0\.1[ \t]*" "$target/etc/hosts" > /dev/null; then
85 sed -ie "s/127\.0\.0\.1[ \t]*.*$/127.0.0.1\t$hostname/" "$target/etc/hosts"
87 echo -e "127.0.0.1\t$hostname" >> "$target/etc/hosts"
89 *) log_error "Don't know how to assign hostname. Unknown linux distribution.";;
92 # Some Linux distributions assign the hostname to 127.0.1.1 in order to be
93 # resolvable to an IP address. Lets replace this if found in /etc/hosts
94 sed -ie "s/^[[:blank:]]*127\.0\.1\.1[[:blank:]].\+$/127.0.1.1\t$hostname/" "$target/etc/hosts"
97 if [ ! -d "$SNF_IMAGE_TARGET" ]; then
98 log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
101 if [ -z "$SNF_IMAGE_HOSTNAME" ]; then
102 log_error "Hostname is missing"
105 if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
106 windows_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME"
107 elif [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "linux" ]; then
108 linux_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME"
113 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :