Statistics
| Branch: | Revision:

root / example / instance-image.d / interfaces @ e84e87cd

History | View | Annotate | Download (2.9 kB)

1
#!/bin/bash
2

    
3
# This is an example script that configures your network settings after
4
# installation. By default it sets it up to use dhcp.
5

    
6
. common.sh
7

    
8
if [ "${DEBUG_LEVEL} " = 1 ] ; then
9
    set -x
10
fi
11

    
12
DIG="$(which dig)"
13
OS_TYPE=""
14
FQDN=""
15

    
16
# discover fqdn name using dig
17
if [ -n "$NIC_0_IP" -a -n "${DIG}" ] ; then
18
    FQDN="$(${DIG} -x ${NIC_0_IP} +short | sed -e 's/\.$//')"
19
fi
20

    
21
if [ -z "${TARGET}" -o ! -d "${TARGET}" ] ; then
22
    echo "Missing target directory"
23
    exit 1
24
fi
25

    
26
if [ -z "${NIC_COUNT}" ] ; then
27
    echo "Missing NIC_COUNT"
28
    exit 1
29
fi
30

    
31
if [ -e ${TARGET}/etc/redhat-release ] ; then
32
    OS_TYPE="redhat"
33
elif [ -e ${TARGET}/etc/debian_version ] ; then
34
    OS_TYPE="debian"
35
elif [ -e ${TARGET}/etc/gentoo-release ] ; then
36
    OS_TYPE="gentoo"
37
elif [ -e ${TARGET}/etc/SuSE-release ] ; then
38
    OS_TYPE="suse"
39
fi
40

    
41
debian_setup() {
42
    if [ ! -d "${TARGET}/etc/network" ] ; then
43
        echo "Missing target network directory"
44
        exit 1
45
    fi
46
    cat > ${TARGET}/etc/network/interfaces << EOF
47
# This file describes the network interfaces available on your system
48
# and how to activate them. For more information, see interfaces(5).
49

    
50
auto lo
51
iface lo inet loopback
52

    
53
auto eth0
54
iface eth0 inet dhcp
55

    
56
EOF
57

    
58
    if [ -n "${FQDN}" ] ; then
59
        echo "${FQDN}" > ${TARGET}/etc/hostname
60
    fi
61
}
62

    
63
redhat_setup() {
64
    if [ ! -d "${TARGET}/etc/sysconfig/network-scripts" ] ; then
65
        echo "Missing target network directory"
66
        exit 1
67
    fi
68
    cat > ${TARGET}/etc/sysconfig/network-scripts/ifcfg-eth0 << EOF
69
DEVICE=eth0
70
BOOTPROTO=dhcp
71
HWADDR=${NIC_0_MAC}
72
ONBOOT=yes
73
EOF
74

    
75
    if [ -n "${FQDN}" ] ; then
76
        cat > ${TARGET}/etc/sysconfig/network << EOF
77
NETWORKING=yes
78
HOSTNAME=${FQDN}
79
EOF
80
    else
81
        cat > ${TARGET}/etc/sysconfig/network << EOF
82
NETWORKING=yes
83
EOF
84
    fi
85
}
86

    
87
gentoo_setup() {
88
    if [ ! -f "${TARGET}/etc/conf.d/net" ] ; then
89
        echo "Missing target network file"
90
        exit 1
91
    fi
92
    cat > ${TARGET}/etc/conf.d/net << EOF
93
config_eth0=( "dhcp" )
94
EOF
95
    chroot ${TARGET} rc-update add net.eth0 default
96

    
97
    if [ -n "${FQDN}" ] ; then
98
        cat > ${TARGET}/etc/conf.d/hostname << EOF
99
HOSTNAME=\"${FQDN}\"
100
EOF
101
    fi
102
}
103

    
104
suse_setup() {
105
    if [ ! -d ${TARGET}/etc/sysconfig/network ] ; then
106
        echo "Missing target network directory"
107
        exit 1
108
    fi
109
    cat > ${TARGET}/etc/sysconfig/network/ifcfg-eth0 << EOF
110
BOOTPROTO=\'dhcp4\'
111
LLADDR=\'${NIC_0_MAC}\'
112
STARTMODE=\'auto\'
113
NAME=\'Ethernet Card 0\'
114
EOF
115
    if [ -n "${FQDN}" ] ; then
116
        echo "${FQDN}" > ${TARGET}/etc/HOSTNAME
117
    fi
118
}
119

    
120
if [ "${NIC_COUNT}" -gt 0 ] ; then
121
    case "${OS_TYPE}" in
122
        debian)
123
            debian_setup
124
            ;;
125
        redhat)
126
            redhat_setup
127
            ;;
128
        gentoo)
129
            gentoo_setup
130
            ;;
131
        suse)
132
            suse_setup
133
            ;;
134
        *)
135
            echo "Unsupported OS_TYPE"
136
            exit 1
137
            ;;
138
    esac
139
fi
140

    
141
exit 0