Statistics
| Branch: | Tag: | Revision:

root / snf-image-helper / tasks / 50AssignHostname.in @ 18b4d7d0

History | View | Annotate | Download (3.7 kB)

1 0a35a4ab Nikos Skalkotos
#! /bin/bash
2 54080484 Nikos Skalkotos
3 7e5d635b Nikos Skalkotos
# Copyright (C) 2011 GRNET S.A. 
4 7e5d635b Nikos Skalkotos
#
5 7e5d635b Nikos Skalkotos
# This program is free software; you can redistribute it and/or modify
6 7e5d635b Nikos Skalkotos
# it under the terms of the GNU General Public License as published by
7 7e5d635b Nikos Skalkotos
# the Free Software Foundation; either version 2 of the License, or
8 7e5d635b Nikos Skalkotos
# (at your option) any later version.
9 7e5d635b Nikos Skalkotos
#
10 7e5d635b Nikos Skalkotos
# This program is distributed in the hope that it will be useful, but
11 7e5d635b Nikos Skalkotos
# WITHOUT ANY WARRANTY; without even the implied warranty of
12 7e5d635b Nikos Skalkotos
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 7e5d635b Nikos Skalkotos
# General Public License for more details.
14 7e5d635b Nikos Skalkotos
#
15 7e5d635b Nikos Skalkotos
# You should have received a copy of the GNU General Public License
16 7e5d635b Nikos Skalkotos
# along with this program; if not, write to the Free Software
17 7e5d635b Nikos Skalkotos
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 7e5d635b Nikos Skalkotos
# 02110-1301, USA.
19 7e5d635b Nikos Skalkotos
20 54080484 Nikos Skalkotos
### BEGIN TASK INFO
21 54080484 Nikos Skalkotos
# Provides:		AssignHostname
22 c349d1b3 Nikos Skalkotos
# RunBefore:            UmountImage
23 c349d1b3 Nikos Skalkotos
# RunAfter:		InstallUnattend
24 0cf9835d Nikos Skalkotos
# Short-Description:	Assign Hostname/Computer Name to the instance
25 6a95db10 Vangelis Koukis
### END TASK INFO
26 54080484 Nikos Skalkotos
27 54080484 Nikos Skalkotos
set -e
28 bad5ca1f Nikos Skalkotos
. "@commondir@/common.sh"
29 54080484 Nikos Skalkotos
30 2a0ab295 Nikos Skalkotos
trap task_cleanup EXIT
31 a50a2bff Nikos Skalkotos
report_task_start
32 2a0ab295 Nikos Skalkotos
33 2a0c492d Nikos Skalkotos
# Check if the task should be prevented from running.
34 2a0c492d Nikos Skalkotos
check_if_excluded
35 2a0c492d Nikos Skalkotos
36 54080484 Nikos Skalkotos
windows_hostname() {
37 bad5ca1f Nikos Skalkotos
    local target="$1"
38 bad5ca1f Nikos Skalkotos
    local password="$2"
39 54080484 Nikos Skalkotos
40 54080484 Nikos Skalkotos
    local tmp_unattend=`mktemp` || exit 1
41 bad5ca1f Nikos Skalkotos
    add_cleanup rm "$tmp_unattend"
42 54080484 Nikos Skalkotos
43 54080484 Nikos Skalkotos
    echo -n "Assigning new computer name..."
44 54080484 Nikos Skalkotos
45 54080484 Nikos Skalkotos
    local namespace="urn:schemas-microsoft-com:unattend"
46 54080484 Nikos Skalkotos
    
47 bad5ca1f Nikos Skalkotos
    "$XMLSTARLET" ed -N x=$namespace -u "/x:unattend/x:settings/x:component/x:ComputerName" -v "$password" "$target/Unattend.xml" > "$tmp_unattend"
48 54080484 Nikos Skalkotos
49 bad5ca1f Nikos Skalkotos
    cat "$tmp_unattend" > "$target/Unattend.xml"
50 54080484 Nikos Skalkotos
    echo done
51 54080484 Nikos Skalkotos
}
52 54080484 Nikos Skalkotos
53 54080484 Nikos Skalkotos
linux_hostname() {
54 bad5ca1f Nikos Skalkotos
    local target="$1"
55 bad5ca1f Nikos Skalkotos
    local hostname="$2"
56 54080484 Nikos Skalkotos
57 bad5ca1f Nikos Skalkotos
    local distro=$(get_base_distro "$target")
58 54080484 Nikos Skalkotos
59 54080484 Nikos Skalkotos
    case "$distro" in
60 54080484 Nikos Skalkotos
        debian)
61 bad5ca1f Nikos Skalkotos
            echo "$hostname" > "$target/etc/hostname";;
62 54080484 Nikos Skalkotos
        redhat)
63 bad5ca1f Nikos Skalkotos
            sed -ie "s/HOSTNAME=.*$/HOSTNAME=$hostname/g" "$target/etc/sysconfig/network";;
64 54080484 Nikos Skalkotos
        slackware|suse)
65 c334f836 Nikos Skalkotos
            #local domain=$(sed -e 's/^[^\.]*//g' < /etc/HOSTNAME)
66 c334f836 Nikos Skalkotos
67 c334f836 Nikos Skalkotos
            # In slackware hostname and domain name are joined together. For now
68 c334f836 Nikos Skalkotos
            # I will not retain the domain name.
69 c334f836 Nikos Skalkotos
            echo "$hostname" > "${target}/etc/HOSTNAME";;
70 c334f836 Nikos Skalkotos
        gentoo)
71 c334f836 Nikos Skalkotos
            sed -ie "s/\(\(HOSTNAME\)\|\(hostname\)\)=.*$/\1=\"$hostname\"/" "$target/etc/conf.d/hostname";;
72 9eaa21c5 Nikos Skalkotos
        arch)
73 18b4d7d0 Nikos Skalkotos
            # In newer archlinux systems /etc/hostname is used to assign the hostname
74 18b4d7d0 Nikos Skalkotos
            if [[ -f "$target/etc/hostname" ]]; then
75 18b4d7d0 Nikos Skalkotos
                echo "$hostname" > "$target/etc/hostname"
76 18b4d7d0 Nikos Skalkotos
            fi
77 18b4d7d0 Nikos Skalkotos
            # Older ones use the HOSTNAME variable in /etc/rc.conf
78 9eaa21c5 Nikos Skalkotos
            sed -ie "s/^HOSTNAME=.*$/HOSTNAME=\"$hostname\"/" "$target/etc/rc.conf"
79 9eaa21c5 Nikos Skalkotos
            if grep "^127\.0\.0\.1[ \t]*" "$target/etc/hosts" > /dev/null; then
80 9eaa21c5 Nikos Skalkotos
               sed -ie "s/127\.0\.0\.1[ \t]*.*$/127.0.0.1\t$hostname/" "$target/etc/hosts"
81 9eaa21c5 Nikos Skalkotos
            else
82 9eaa21c5 Nikos Skalkotos
               echo -e "127.0.0.1\t$hostname" >> "$target/etc/hosts"
83 9eaa21c5 Nikos Skalkotos
            fi;;
84 2755bb06 Nikos Skalkotos
        *) log_error "Don't know how to assign hostname. Unknown linux distribution.";;
85 54080484 Nikos Skalkotos
    esac
86 54080484 Nikos Skalkotos
87 54080484 Nikos Skalkotos
    # Some Linux distributions assign the hostname to 127.0.1.1 in order to be
88 54080484 Nikos Skalkotos
    # resolvable to an IP address. Lets replace this if found in /etc/hosts
89 bad5ca1f Nikos Skalkotos
    sed -ie "s/^[[:blank:]]*127\.0\.1\.1[[:blank:]].\+$/127.0.1.1\t$hostname/" "$target/etc/hosts"
90 54080484 Nikos Skalkotos
}
91 54080484 Nikos Skalkotos
92 8704ee47 Nikos Skalkotos
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
93 8704ee47 Nikos Skalkotos
    log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"	
94 54080484 Nikos Skalkotos
fi
95 54080484 Nikos Skalkotos
96 54080484 Nikos Skalkotos
if [ -z "$SNF_IMAGE_HOSTNAME" ]; then
97 54080484 Nikos Skalkotos
    log_error "Hostname is missing"
98 54080484 Nikos Skalkotos
fi
99 54080484 Nikos Skalkotos
100 473f4fa5 Nikos Skalkotos
if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
101 bad5ca1f Nikos Skalkotos
    windows_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME"
102 473f4fa5 Nikos Skalkotos
elif [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "linux" ]; then
103 bad5ca1f Nikos Skalkotos
    linux_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME"
104 54080484 Nikos Skalkotos
fi
105 54080484 Nikos Skalkotos
106 54080484 Nikos Skalkotos
exit 0
107 54080484 Nikos Skalkotos
108 54080484 Nikos Skalkotos
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :