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