50AssignHostname.in 3.05 KB
Newer Older
1
#! /bin/bash
Nikos Skalkotos's avatar
Nikos Skalkotos committed
2

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Copyright (C) 2011 GRNET S.A. 
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

Nikos Skalkotos's avatar
Nikos Skalkotos committed
20
21
### BEGIN TASK INFO
# Provides:		AssignHostname
Nikos Skalkotos's avatar
Nikos Skalkotos committed
22
23
# RunBefore:            UmountImage
# RunAfter:		InstallUnattend
24
# Short-Description:	Assign Hostname/Computer Name to the instance
25
### END TASK INFO
Nikos Skalkotos's avatar
Nikos Skalkotos committed
26
27

set -e
28
. "@commondir@/common.sh"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
29

30
31
32
trap task_cleanup EXIT
report_start_task

33
34
35
# Check if the task should be prevented from running.
check_if_excluded

Nikos Skalkotos's avatar
Nikos Skalkotos committed
36
windows_hostname() {
37
38
    local target="$1"
    local password="$2"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
39
40

    local tmp_unattend=`mktemp` || exit 1
41
    add_cleanup rm "$tmp_unattend"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
42
43
44
45
46

    echo -n "Assigning new computer name..."

    local namespace="urn:schemas-microsoft-com:unattend"
    
47
    "$XMLSTARLET" ed -N x=$namespace -u "/x:unattend/x:settings/x:component/x:ComputerName" -v "$password" "$target/Unattend.xml" > "$tmp_unattend"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
48

49
    cat "$tmp_unattend" > "$target/Unattend.xml"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
50
51
52
53
    echo done
}

linux_hostname() {
54
55
    local target="$1"
    local hostname="$2"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
56

57
    local distro=$(get_base_distro "$target")
Nikos Skalkotos's avatar
Nikos Skalkotos committed
58
59
60

    case "$distro" in
        debian)
61
            echo "$hostname" > "$target/etc/hostname";;
Nikos Skalkotos's avatar
Nikos Skalkotos committed
62
        redhat)
63
            sed -ie "s/HOSTNAME=.*$/HOSTNAME=$hostname/g" "$target/etc/sysconfig/network";;
Nikos Skalkotos's avatar
Nikos Skalkotos committed
64
        slackware|suse)
65
66
67
68
69
70
71
            #local domain=$(sed -e 's/^[^\.]*//g' < /etc/HOSTNAME)

            # In slackware hostname and domain name are joined together. For now
            # I will not retain the domain name.
            echo "$hostname" > "${target}/etc/HOSTNAME";;
        gentoo)
            sed -ie "s/\(\(HOSTNAME\)\|\(hostname\)\)=.*$/\1=\"$hostname\"/" "$target/etc/conf.d/hostname";;
72
        *) log_error "Don't know how to assign hostname. Unknown linux distribution.";;
Nikos Skalkotos's avatar
Nikos Skalkotos committed
73
74
75
76
    esac

    # Some Linux distributions assign the hostname to 127.0.1.1 in order to be
    # resolvable to an IP address. Lets replace this if found in /etc/hosts
77
    sed -ie "s/^[[:blank:]]*127\.0\.1\.1[[:blank:]].\+$/127.0.1.1\t$hostname/" "$target/etc/hosts"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
78
79
}

80
81
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
    log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"	
Nikos Skalkotos's avatar
Nikos Skalkotos committed
82
83
84
85
86
87
fi

if [ -z "$SNF_IMAGE_HOSTNAME" ]; then
    log_error "Hostname is missing"
fi

88
if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
89
    windows_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME"
90
elif [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "linux" ]; then
91
    linux_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
92
93
94
95
96
97
fi

exit 0

# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :