50AssignHostname.in 4.6 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
22
# RunBefore:            EnforcePersonality
Nikos Skalkotos's avatar
Nikos Skalkotos committed
23
# 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
trap task_cleanup EXIT
31
report_task_start
32

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() {
Nikos Skalkotos's avatar
Nikos Skalkotos committed
37
38
39
    local target password unattend tmp_unattend namespace
    target="$1"
    password="$2"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
40

Nikos Skalkotos's avatar
Nikos Skalkotos committed
41
    tmp_unattend=$(mktemp)
42
    add_cleanup rm "$tmp_unattend"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
43
44
45

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

Nikos Skalkotos's avatar
Nikos Skalkotos committed
46
    namespace="urn:schemas-microsoft-com:unattend"
47
48
49
50
51

    unattend=$(get_unattend "$target")
    if [ -z "$unattend" ]; then
        log_error "Unattend.xml is missing."
    fi
Nikos Skalkotos's avatar
Nikos Skalkotos committed
52
    
53
    "$XMLSTARLET" ed -N x=$namespace -u "/x:unattend/x:settings/x:component/x:ComputerName" -v "$password" "$unattend" > "$tmp_unattend"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
54

55
    cat "$tmp_unattend" > "$unattend"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
56
57
58
59
    echo done
}

linux_hostname() {
Nikos Skalkotos's avatar
Nikos Skalkotos committed
60
61
62
    local target hostname distro
    target="$1"
    hostname="$2"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
63

64
    if [ -f "$target/etc/hostname" ]; then
65
        echo "$hostname" > "$target/etc/hostname"
66
67
68
    else
        distro=$(get_base_distro "$target")
        case "$distro" in
69
70
            coreos)
                echo "$hostname" > "$target/etc/hostname";;
71
            redhat)
Nikos Skalkotos's avatar
Nikos Skalkotos committed
72
                sed -i -e "s/HOSTNAME=.*$/HOSTNAME=$hostname/g" "$target/etc/sysconfig/network";;
73
74
75
            slackware|suse)
                #local domain=$(sed -e 's/^[^\.]*//g' < /etc/HOSTNAME)

76
77
                # In Slackware hostname and domain name are joined together.
                # For now I will not retain the domain name.
78
79
                echo "$hostname" > "${target}/etc/HOSTNAME";;
            gentoo)
Nikos Skalkotos's avatar
Nikos Skalkotos committed
80
                sed -i -e "s/\(\(HOSTNAME\)\|\(hostname\)\)=.*$/\1=\"$hostname\"/" "$target/etc/conf.d/hostname";;
81
            arch)
82
                if [ -f "$target/etc/rc.conf" ]; then
Nikos Skalkotos's avatar
Nikos Skalkotos committed
83
                    sed -i -e "s/^HOSTNAME=.*$/HOSTNAME=\"$hostname\"/" "$target/etc/rc.conf"
84
85
86
87
88
                else
                    # In new versions of arch, /etc/rc.conf is missing
                    echo "$hostname" > "$target/etc/hostname"
                fi

89
                if grep "^127\.0\.0\.1[ \t]*" "$target/etc/hosts" > /dev/null; then
Nikos Skalkotos's avatar
Nikos Skalkotos committed
90
                   sed -i -e "s/127\.0\.0\.1[ \t]*.*$/127.0.0.1\t$hostname/" "$target/etc/hosts"
91
92
93
94
95
96
                else
                   echo -e "127.0.0.1\t$hostname" >> "$target/etc/hosts"
                fi;;
            *) log_error "Don't know how to assign hostname. Unknown linux distribution.";;
        esac
    fi
Nikos Skalkotos's avatar
Nikos Skalkotos committed
97
98
99

    # 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
100
101
102
    if [ -f "$target/etc/hosts" ]; then
        sed -i -e "s/^[[:blank:]]*127\.0\.1\.1[[:blank:]].\+$/127.0.1.1\t$hostname/" "$target/etc/hosts"
    fi
Nikos Skalkotos's avatar
Nikos Skalkotos committed
103
104
105
106
107
108
109
110
111
112
113
114
}

freebsd_hostname() {
    local target hostname
    target="$1"
    hostname="$2"

    if grep ^hostname= "$target/etc/rc.conf"; then
        sed -i -e "s/^hostname=.*$/hostname=\"$(printf "%q" "$hostname")\"/" "$target/etc/rc.conf"
    else
        echo "hostname=\"$(printf "%q" "$hostname")\"" >> "$target/etc/rc.conf"
    fi
Nikos Skalkotos's avatar
Nikos Skalkotos committed
115
116
}

117
118
119
120
121
122
123
124
openbsd_hostname() {
    local target hostname
    target="$1"
    hostname="$2"

    echo "$hostname" > "$target/etc/myname"
}

125
netbsd_hostname() {
126
127
128
129
130
131
132
133
134
135
136
    local target hostname
    target="$1"
    hostname="$2"

    openbsd_hostname "$@"

    if grep ^hostname= "$target/etc/rc.conf"; then
        sed -i -e "s/^hostname=.*$/hostname=\"$(printf "%q" "$hostname")\"/" "$target/etc/rc.conf"
    fi
}

137
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
Nikos Skalkotos's avatar
Nikos Skalkotos committed
138
    log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
139
140
141
142
143
144
fi

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

145
${SNF_IMAGE_PROPERTY_OSFAMILY}_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
146
147
148
149
150

exit 0

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