50AssignHostname.in 2.85 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

windows_hostname() {
31
32
    local target="$1"
    local password="$2"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
33
34

    local tmp_unattend=`mktemp` || exit 1
35
    add_cleanup rm "$tmp_unattend"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
36
37
38
39
40

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

    local namespace="urn:schemas-microsoft-com:unattend"
    
41
    "$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
42

43
    cat "$tmp_unattend" > "$target/Unattend.xml"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
44
45
46
47
    echo done
}

linux_hostname() {
48
49
    local target="$1"
    local hostname="$2"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
50

51
    local distro=$(get_base_distro "$target")
Nikos Skalkotos's avatar
Nikos Skalkotos committed
52
53
54

    case "$distro" in
        debian)
55
            echo "$hostname" > "$target/etc/hostname";;
Nikos Skalkotos's avatar
Nikos Skalkotos committed
56
        redhat)
57
            sed -ie "s/HOSTNAME=.*$/HOSTNAME=$hostname/g" "$target/etc/sysconfig/network";;
Nikos Skalkotos's avatar
Nikos Skalkotos committed
58
        slackware|suse)
59
60
61
62
63
64
65
            #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";;
Nikos Skalkotos's avatar
Nikos Skalkotos committed
66
67
68
69
    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
70
    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
71
72
}

73
74
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
    log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"	
Nikos Skalkotos's avatar
Nikos Skalkotos committed
75
76
77
78
79
80
fi

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

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"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
85
86
87
88
89
90
fi

exit 0

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