Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.4 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:            EnforcePersonality
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 password unattend tmp_unattend namespace
38
    target="$1"
39
    password="$2"
40

    
41
    tmp_unattend=$(mktemp)
42
    add_cleanup rm "$tmp_unattend"
43

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

    
46
    namespace="urn:schemas-microsoft-com:unattend"
47

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

    
55
    cat "$tmp_unattend" > "$unattend"
56
    echo done
57
}
58

    
59
linux_hostname() {
60
    local target hostname distro
61
    target="$1"
62
    hostname="$2"
63

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

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

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

    
96
    # Some Linux distributions assign the hostname to 127.0.1.1 in order to be
97
    # resolvable to an IP address. Lets replace this if found in /etc/hosts
98
    sed -i -e "s/^[[:blank:]]*127\.0\.1\.1[[:blank:]].\+$/127.0.1.1\t$hostname/" "$target/etc/hosts"
99
}
100

    
101
freebsd_hostname() {
102
    local target hostname
103
    target="$1"
104
    hostname="$2"
105

    
106
    if grep ^hostname= "$target/etc/rc.conf"; then
107
        sed -i -e "s/^hostname=.*$/hostname=\"$(printf "%q" "$hostname")\"/" "$target/etc/rc.conf"
108
    else
109
        echo "hostname=\"$(printf "%q" "$hostname")\"" >> "$target/etc/rc.conf"
110
    fi
111
}
112

    
113
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
114
    log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
115
fi
116

    
117
if [ -z "$SNF_IMAGE_HOSTNAME" ]; then
118
    log_error "Hostname is missing"
119
fi
120

    
121
if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
122
    windows_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME"
123
elif [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "linux" ]; then
124
    linux_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME"
125
elif [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "freebsd" ]; then
126
    freebsd_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME"
127
fi
128

    
129
exit 0
130

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