Statistics
| Branch: | Revision:

root / example / hooks / linux / interfaces @ 8d77389f

History | View | Annotate | Download (4.4 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
debug set -x
28

    
29
FQDN="${INSTANCE_NAME}"
30
SHORT_NAME="$(echo ${INSTANCE_NAME} | cut -d . -f 1)"
31
STATIC=""
32

    
33
if [ -z "${TARGET}" -o ! -d "${TARGET}" ] ; then
34
    log_error "Missing target directory"
35
    exit 1
36
fi
37

    
38
if [ -z "${NIC_COUNT}" ] ; then
39
    log_error "Missing NIC_COUNT"
40
    exit 1
41
fi
42

    
43
if [ -f "${NETWORKS_DIR}/instances/${FQDN}" ] ; then
44
    STATIC="yes"
45
    source ${NETWORKS_DIR}/instances/${FQDN}
46
    if [ -f "${NETWORKS_DIR}/subnets/${SUBNET}" ] ; then
47
        source ${NETWORKS_DIR}/subnets/${SUBNET}
48
    else
49
        echo "No subnet file for subnet ${SUBNET}!"
50
        exit 1
51
    fi
52
fi
53

    
54
# Functions
55
debian_setup() {
56
    if [ ! -d "${TARGET}/etc/network" ] ; then
57
        log_error "Missing target network directory"
58
        exit 1
59
    fi
60

    
61
    if [ -z "${STATIC}" ] ; then
62
        cat > ${TARGET}/etc/network/interfaces << EOF
63
# This file describes the network interfaces available on your system
64
# and how to activate them. For more information, see interfaces(5).
65

    
66
auto lo
67
iface lo inet loopback
68

    
69
auto eth0
70
iface eth0 inet dhcp
71

    
72
EOF
73
    else
74
        cat > ${TARGET}/etc/network/interfaces << EOF
75
# This file describes the network interfaces available on your system
76
# and how to activate them. For more information, see interfaces(5).
77

    
78
auto lo
79
iface lo inet loopback
80

    
81
auto eth0
82
iface eth0 inet static
83
    address ${ADDRESS}
84
    netmask ${NETMASK}
85
    gateway ${GATEWAY}
86

    
87
EOF
88
    fi
89

    
90
    if [ -n "${FQDN}" ] ; then
91
        echo "${SHORT_NAME}" > ${TARGET}/etc/hostname
92
    fi
93
}
94

    
95
redhat_setup() {
96
    if [ ! -d "${TARGET}/etc/sysconfig/network-scripts" ] ; then
97
        log_error "Missing target network directory"
98
        exit 1
99
    fi
100
    if [ -z "${STATIC}" ] ; then
101
        cat > ${TARGET}/etc/sysconfig/network-scripts/ifcfg-eth0 << EOF
102
DEVICE=eth0
103
BOOTPROTO=dhcp
104
ONBOOT=yes
105
EOF
106
    else
107
        cat > ${TARGET}/etc/sysconfig/network-scripts/ifcfg-eth0 << EOF
108
DEVICE=eth0
109
BOOTPROTO=static
110
IPADDR=${ADDRESS}
111
NETMASK=${NETMASK}
112
ONBOOT=yes
113
EOF
114
    fi
115

    
116
    if [ -n "${FQDN}" ] ; then
117
        cat > ${TARGET}/etc/sysconfig/network << EOF
118
NETWORKING=yes
119
HOSTNAME=${FQDN}
120
GATEWAY=${GATEWAY}
121
EOF
122
    else
123
        cat > ${TARGET}/etc/sysconfig/network << EOF
124
NETWORKING=yes
125
GATEWAY=${GATEWAY}
126
EOF
127
    fi
128
}
129

    
130
gentoo_setup() {
131
    if [ ! -f "${TARGET}/etc/conf.d/net" ] ; then
132
        log_error "Missing target network file"
133
        exit 1
134
    fi
135
    if [ -z "${STATIC}" ] ; then
136
        cat > ${TARGET}/etc/conf.d/net << EOF
137
config_eth0=( "dhcp" )
138
EOF
139
    else
140
        cat > ${TARGET}/etc/conf.d/net << EOF
141
config_eth0=( "${ADDRESS} netmask ${NETMASK}" )
142
routes_eth0=( "default gw ${GATEWAY}" )
143
EOF
144
    fi
145

    
146
    chroot ${TARGET} ln -sf /etc/init.d/net.lo /etc/init.d/net.eth0
147
    chroot ${TARGET} rc-update add net.eth0 default
148

    
149
    if [ -n "${FQDN}" ] ; then
150
        # baselayout-2.x
151
        if [ -d "${TARGET}/usr/share/openrc/" ] ; then
152
            cat > ${TARGET}/etc/conf.d/hostname << EOF
153
hostname="${SHORT_NAME}"
154
EOF
155
        else
156
            cat > ${TARGET}/etc/conf.d/hostname << EOF
157
HOSTNAME="${SHORT_NAME}"
158
EOF
159
        fi
160
    fi
161
}
162

    
163
suse_setup() {
164
    if [ ! -d ${TARGET}/etc/sysconfig/network ] ; then
165
        log_error "Missing target network directory"
166
        exit 1
167
    fi
168
    cat > ${TARGET}/etc/sysconfig/network/ifcfg-eth0 << EOF
169
BOOTPROTO='dhcp4'
170
STARTMODE='auto'
171
NAME='Ethernet Card 0'
172
EOF
173
    if [ -n "${FQDN}" ] ; then
174
        echo "${FQDN}" > ${TARGET}/etc/HOSTNAME
175
    fi
176
}
177

    
178
# Main
179
get_os_type $TARGET
180

    
181
if [ "${NIC_COUNT}" -gt 0 -a -n "${OS_TYPE}" ] ; then
182
    ${OS_TYPE}_setup
183
else
184
    log_error "Unsupported OS_TYPE"
185
fi
186

    
187

    
188
exit 0