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

### 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
11
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Short-Description:	Assign the Hostname of Computer Name in the instance
### END TAST INFO

set -e
. /usr/share/snf-image/common.sh

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
}

if [ -z "$SNF_IMAGE_TARGET" -o ! -d "$SNF_IMAGE_TARGET" ]; then
    log_error "Missing target directory"	
fi

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

if [ "$SNF_IMAGE_TYPE" = "ntfsdump" ]; then
    windows_hostname $SNF_IMAGE_TARGET $SNF_IMAGE_PASSWORD
elif [ "$SNF_IMAGE_TYPE" = "extdump" ]; then
    linux_hostname $SNF_IMAGE_TARGET $SNF_IMAGE_PASSWORD
fi

echo "done"

cleanup
trap - EXIT

exit 0

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