4b879bf78c42a83f327602d131e98c4027474b51
[snf-image] / snf-image-helper / tasks / 50AssignHostname.in
1 #! /bin/bash
2
3 # Copyright (C) 2011 GRNET S.A. 
4 #
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.
9 #
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.
14 #
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
18 # 02110-1301, USA.
19
20 ### BEGIN TASK INFO
21 # Provides:             AssignHostname
22 # RunBefore:            UmountImage
23 # RunAfter:             InstallUnattend
24 # Short-Description:    Assign Hostname/Computer Name to the instance
25 ### END TASK INFO
26
27 set -e
28 . "@commondir@/common.sh"
29
30 windows_hostname() {
31     local target="$1"
32     local password="$2"
33
34     local tmp_unattend=`mktemp` || exit 1
35     add_cleanup rm "$tmp_unattend"
36
37     echo -n "Assigning new computer name..."
38
39     local namespace="urn:schemas-microsoft-com:unattend"
40     
41     "$XMLSTARLET" ed -N x=$namespace -u "/x:unattend/x:settings/x:component/x:ComputerName" -v "$password" "$target/Unattend.xml" > "$tmp_unattend"
42
43     cat "$tmp_unattend" > "$target/Unattend.xml"
44     echo done
45 }
46
47 linux_hostname() {
48     local target="$1"
49     local hostname="$2"
50
51     local distro=$(get_base_distro "$target")
52
53     case "$distro" in
54         debian)
55             echo "$hostname" > "$target/etc/hostname";;
56         redhat)
57             sed -ie "s/HOSTNAME=.*$/HOSTNAME=$hostname/g" "$target/etc/sysconfig/network";;
58         slackware|suse)
59             #local domain=$(sed -e 's/^[^\.]*//g' < /etc/HOSTNAME)
60
61             # In slackware hostname and domain name are joined together. For now
62             # I will not retain the domain name.
63             echo "$hostname" > "${target}/etc/HOSTNAME";;
64         gentoo)
65             sed -ie "s/\(\(HOSTNAME\)\|\(hostname\)\)=.*$/\1=\"$hostname\"/" "$target/etc/conf.d/hostname";;
66     esac
67
68     # Some Linux distributions assign the hostname to 127.0.1.1 in order to be
69     # resolvable to an IP address. Lets replace this if found in /etc/hosts
70     sed -ie "s/^[[:blank:]]*127\.0\.1\.1[[:blank:]].\+$/127.0.1.1\t$hostname/" "$target/etc/hosts"
71 }
72
73 if [ ! -d "$SNF_IMAGE_TARGET" ]; then
74     log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"     
75 fi
76
77 if [ -z "$SNF_IMAGE_HOSTNAME" ]; then
78     log_error "Hostname is missing"
79 fi
80
81 if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
82     windows_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME"
83 elif [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "linux" ]; then
84     linux_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME"
85 fi
86
87 exit 0
88
89 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
90