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

windows_hostname() {
14
15
    local target="$1"
    local password="$2"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
16
17

    local tmp_unattend=`mktemp` || exit 1
18
    add_cleanup rm "$tmp_unattend"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
19
20
21
22
23

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

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

26
    cat "$tmp_unattend" > "$target/Unattend.xml"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
27
28
29
30
    echo done
}

linux_hostname() {
31
32
    local target="$1"
    local hostname="$2"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
33

34
    local distro=$(get_base_distro "$target")
Nikos Skalkotos's avatar
Nikos Skalkotos committed
35
36
37

    case "$distro" in
        debian)
38
            echo "$hostname" > "$target/etc/hostname";;
Nikos Skalkotos's avatar
Nikos Skalkotos committed
39
        redhat)
40
            sed -ie "s/HOSTNAME=.*$/HOSTNAME=$hostname/g" "$target/etc/sysconfig/network";;
Nikos Skalkotos's avatar
Nikos Skalkotos committed
41
42
43
44
45
46
        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.
        
47
        echo "$hostname" > "${target}/etc/HOSTNAME";;
Nikos Skalkotos's avatar
Nikos Skalkotos committed
48
    gentoo)
49
        sed -ie "s/\(\(HOSTNAME\)\|\(hostname\)\)=.*$/\1=\"$hostname\"/" "$target/etc/conf.d/hostname";;
Nikos Skalkotos's avatar
Nikos Skalkotos committed
50
51
52
53
    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
54
    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
55
56
}

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 :