Statistics
| Branch: | Tag: | Revision:

root / helper / tasks / 50AssignHostname.sh @ 0a35a4ab

History | View | Annotate | Download (2.1 kB)

1
#! /bin/bash
2

    
3
### BEGIN TASK INFO
4
# Provides:		AssignHostname
5
# RunBefore:            UmountImage
6
# RunAfter:		InstallUnattend
7
# Short-Description:	Assign the Hostname of Computer Name in the instance
8
### END TAST INFO
9

    
10
set -e
11
. /usr/share/snf-image/common.sh
12

    
13
windows_hostname() {
14
    local target=$1
15
    local password=$2
16

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

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

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

    
26
    cat $tmp_unattend > "$target/Unattend.xml"
27
    echo done
28
}
29

    
30
linux_hostname() {
31
    local target=$1
32
    local hostname=$2
33

    
34
    local distro=$(get_base_distro $target)
35

    
36
    case "$distro" in
37
        debian)
38
            echo "$hostname" > $target/etc/hostname;;
39
        redhat)
40
            sed -ie "s/HOSTNAME=.*$/HOSTNAME=$hostname/g" $target/etc/sysconfig/network;;
41
        slackware|suse)
42
        #local domain=$(sed -e 's/^[^\.]*//g' < /etc/HOSTNAME)
43
        
44
        # In slackware hostname and domain name are joined together. For now I
45
        # will not retain the domain name.
46
        
47
        echo $hostname > ${target}/etc/HOSTNAME;;
48
    gentoo)
49
        sed -ie "s/\(\(HOSTNAME\)\|\(hostname\)\)=.*$/\1=\"$hostname\"/" $target/etc/conf.d/hostname;;
50
    esac
51

    
52
    # Some Linux distributions assign the hostname to 127.0.1.1 in order to be
53
    # 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
55
}
56

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

    
61
if [ -z "$SNF_IMAGE_HOSTNAME" ]; then
62
    log_error "Hostname is missing"
63
fi
64

    
65
if [ "$SNF_IMAGE_TYPE" = "ntfsdump" ]; then
66
    windows_hostname $SNF_IMAGE_TARGET $SNF_IMAGE_PASSWORD
67
elif [ "$SNF_IMAGE_TYPE" = "extdump" ]; then
68
    linux_hostname $SNF_IMAGE_TARGET $SNF_IMAGE_PASSWORD
69
fi
70

    
71
echo "done"
72

    
73
cleanup
74
trap - EXIT
75

    
76
exit 0
77

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