root / snf-image-helper / tasks / 50AssignHostname.in @ 2a0c492d
History | View | Annotate | Download (2.9 kB)
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 |
# Check if the task should be prevented from running. |
31 |
check_if_excluded |
32 |
|
33 |
windows_hostname() { |
34 |
local target="$1" |
35 |
local password="$2" |
36 |
|
37 |
local tmp_unattend=`mktemp` || exit 1 |
38 |
add_cleanup rm "$tmp_unattend" |
39 |
|
40 |
echo -n "Assigning new computer name..." |
41 |
|
42 |
local namespace="urn:schemas-microsoft-com:unattend" |
43 |
|
44 |
"$XMLSTARLET" ed -N x=$namespace -u "/x:unattend/x:settings/x:component/x:ComputerName" -v "$password" "$target/Unattend.xml" > "$tmp_unattend" |
45 |
|
46 |
cat "$tmp_unattend" > "$target/Unattend.xml" |
47 |
echo done |
48 |
} |
49 |
|
50 |
linux_hostname() { |
51 |
local target="$1" |
52 |
local hostname="$2" |
53 |
|
54 |
local distro=$(get_base_distro "$target") |
55 |
|
56 |
case "$distro" in |
57 |
debian) |
58 |
echo "$hostname" > "$target/etc/hostname";; |
59 |
redhat) |
60 |
sed -ie "s/HOSTNAME=.*$/HOSTNAME=$hostname/g" "$target/etc/sysconfig/network";; |
61 |
slackware|suse) |
62 |
#local domain=$(sed -e 's/^[^\.]*//g' < /etc/HOSTNAME) |
63 |
|
64 |
# In slackware hostname and domain name are joined together. For now |
65 |
# I will not retain the domain name. |
66 |
echo "$hostname" > "${target}/etc/HOSTNAME";; |
67 |
gentoo) |
68 |
sed -ie "s/\(\(HOSTNAME\)\|\(hostname\)\)=.*$/\1=\"$hostname\"/" "$target/etc/conf.d/hostname";; |
69 |
esac |
70 |
|
71 |
# Some Linux distributions assign the hostname to 127.0.1.1 in order to be |
72 |
# resolvable to an IP address. Lets replace this if found in /etc/hosts |
73 |
sed -ie "s/^[[:blank:]]*127\.0\.1\.1[[:blank:]].\+$/127.0.1.1\t$hostname/" "$target/etc/hosts" |
74 |
} |
75 |
|
76 |
if [ ! -d "$SNF_IMAGE_TARGET" ]; then |
77 |
log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing" |
78 |
fi |
79 |
|
80 |
if [ -z "$SNF_IMAGE_HOSTNAME" ]; then |
81 |
log_error "Hostname is missing" |
82 |
fi |
83 |
|
84 |
if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then |
85 |
windows_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME" |
86 |
elif [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "linux" ]; then |
87 |
linux_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME" |
88 |
fi |
89 |
|
90 |
exit 0 |
91 |
|
92 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |
93 |
|