Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (3.7 kB)

1
#!/bin/bash
2

    
3
# Copyright (C) 2010 Oregon State University
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
# General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18
# 02110-1301, USA.
19
#
20
# This is an example script that configures your network settings after
21
# installation. By default it sets it up to use dhcp.
22

    
23
set -e
24

    
25
. common.sh
26

    
27
if [ "${IMAGE_DEBUG} " = 1 ] ; then
28
    set -x
29
fi
30

    
31
DIG="$(which dig)"
32
OS_TYPE=""
33
FQDN=""
34

    
35
# discover fqdn name using dig
36
if [ -n "$NIC_0_IP" -a -n "${DIG}" ] ; then
37
    FQDN="$(${DIG} -x ${NIC_0_IP} +short | sed -e 's/\.$//')"
38
fi
39

    
40
if [ -z "${TARGET}" -o ! -d "${TARGET}" ] ; then
41
    log_error "Missing target directory"
42
    exit 1
43
fi
44

    
45
if [ -z "${NIC_COUNT}" ] ; then
46
    log_error "Missing NIC_COUNT"
47
    exit 1
48
fi
49

    
50
if [ -e ${TARGET}/etc/redhat-release ] ; then
51
    OS_TYPE="redhat"
52
elif [ -e ${TARGET}/etc/debian_version ] ; then
53
    OS_TYPE="debian"
54
elif [ -e ${TARGET}/etc/gentoo-release ] ; then
55
    OS_TYPE="gentoo"
56
elif [ -e ${TARGET}/etc/SuSE-release ] ; then
57
    OS_TYPE="suse"
58
fi
59

    
60
debian_setup() {
61
    if [ ! -d "${TARGET}/etc/network" ] ; then
62
        log_error "Missing target network directory"
63
        exit 1
64
    fi
65
    cat > ${TARGET}/etc/network/interfaces << EOF
66
# This file describes the network interfaces available on your system
67
# and how to activate them. For more information, see interfaces(5).
68

    
69
auto lo
70
iface lo inet loopback
71

    
72
auto eth0
73
iface eth0 inet dhcp
74

    
75
EOF
76

    
77
    if [ -n "${FQDN}" ] ; then
78
        echo "${FQDN}" > ${TARGET}/etc/hostname
79
    fi
80
}
81

    
82
redhat_setup() {
83
    if [ ! -d "${TARGET}/etc/sysconfig/network-scripts" ] ; then
84
        log_error "Missing target network directory"
85
        exit 1
86
    fi
87
    cat > ${TARGET}/etc/sysconfig/network-scripts/ifcfg-eth0 << EOF
88
DEVICE=eth0
89
BOOTPROTO=dhcp
90
HWADDR=${NIC_0_MAC^^[a-z]}
91
ONBOOT=yes
92
EOF
93

    
94
    if [ -n "${FQDN}" ] ; then
95
        cat > ${TARGET}/etc/sysconfig/network << EOF
96
NETWORKING=yes
97
HOSTNAME=${FQDN}
98
EOF
99
    else
100
        cat > ${TARGET}/etc/sysconfig/network << EOF
101
NETWORKING=yes
102
EOF
103
    fi
104
}
105

    
106
gentoo_setup() {
107
    if [ ! -f "${TARGET}/etc/conf.d/net" ] ; then
108
        log_error "Missing target network file"
109
        exit 1
110
    fi
111
    cat > ${TARGET}/etc/conf.d/net << EOF
112
config_eth0=( "dhcp" )
113
EOF
114
    chroot ${TARGET} rc-update add net.eth0 default
115

    
116
    if [ -n "${FQDN}" ] ; then
117
        cat > ${TARGET}/etc/conf.d/hostname << EOF
118
HOSTNAME="${FQDN}"
119
EOF
120
    fi
121
}
122

    
123
suse_setup() {
124
    if [ ! -d ${TARGET}/etc/sysconfig/network ] ; then
125
        log_error "Missing target network directory"
126
        exit 1
127
    fi
128
    cat > ${TARGET}/etc/sysconfig/network/ifcfg-eth0 << EOF
129
BOOTPROTO='dhcp4'
130
LLADDR='${NIC_0_MAC^^[a-z]}'
131
STARTMODE='auto'
132
NAME='Ethernet Card 0'
133
EOF
134
    if [ -n "${FQDN}" ] ; then
135
        echo "${FQDN}" > ${TARGET}/etc/HOSTNAME
136
    fi
137
}
138

    
139
if [ "${NIC_COUNT}" -gt 0 ] ; then
140
    case "${OS_TYPE}" in
141
        debian)
142
            debian_setup
143
            ;;
144
        redhat)
145
            redhat_setup
146
            ;;
147
        gentoo)
148
            gentoo_setup
149
            ;;
150
        suse)
151
            suse_setup
152
            ;;
153
        *)
154
            log_error "Unsupported OS_TYPE"
155
            exit 1
156
            ;;
157
    esac
158
fi
159

    
160
exit 0