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

### BEGIN TASK INFO
# Provides:		AssignHostname
Nikos Skalkotos's avatar
Nikos Skalkotos committed
5
6
# RunBefore:            UmountImage
# RunAfter:		InstallUnattend
Nikos Skalkotos's avatar
Nikos Skalkotos committed
7
8
9
10
# Short-Description:	Assign the Hostname of Computer Name in the instance
### END TAST INFO

set -e
11
. @commondir@/common.sh
Nikos Skalkotos's avatar
Nikos Skalkotos committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

windows_hostname() {
    local target=$1
    local password=$2

    local tmp_unattend=`mktemp` || exit 1
    CLEANUP+=("rm $tmp_unattend")

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

    local namespace="urn:schemas-microsoft-com:unattend"
    
    $XMLSTARLET ed -N x=$namespace -u "/x:unattend/x:settings/x:component/x:ComputerName" -v $password "$target/Unattend.xml" > $tmp_unattend

    cat $tmp_unattend > "$target/Unattend.xml"
    echo done
}

linux_hostname() {
    local target=$1
    local hostname=$2

    local distro=$(get_base_distro $target)

    case "$distro" in
        debian)
            echo "$hostname" > $target/etc/hostname;;
        redhat)
            sed -ie "s/HOSTNAME=.*$/HOSTNAME=$hostname/g" $target/etc/sysconfig/network;;
        slackware|suse)
        #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;;
    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
    sed -ie "s/^[[:blank:]]*127\.0\.1\.1[[:blank:]].\+$/127.0.1.1\t$hostname/" $target/etc/hosts
}

57
58
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
    log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"	
Nikos Skalkotos's avatar
Nikos Skalkotos committed
59
60
61
62
63
64
65
fi

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

if [ "$SNF_IMAGE_TYPE" = "ntfsdump" ]; then
66
    windows_hostname $SNF_IMAGE_TARGET $SNF_IMAGE_HOSTNAME
Nikos Skalkotos's avatar
Nikos Skalkotos committed
67
elif [ "$SNF_IMAGE_TYPE" = "extdump" ]; then
68
    linux_hostname $SNF_IMAGE_TARGET $SNF_IMAGE_HOSTNAME
Nikos Skalkotos's avatar
Nikos Skalkotos committed
69
70
71
72
73
74
75
76
77
fi

cleanup
trap - EXIT

exit 0

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