root / example / hooks / linux / interfaces @ 7be1e51e
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 |
. common_linux.sh |
27 |
|
28 |
debug set -x |
29 |
|
30 |
FQDN="${INSTANCE_NAME}" |
31 |
SHORT_NAME="$(echo ${INSTANCE_NAME} | cut -d . -f 1)" |
32 |
STATIC="" |
33 |
|
34 |
if [ -z "${TARGET}" -o ! -d "${TARGET}" ] ; then |
35 |
log_error "Missing target directory" |
36 |
exit 1 |
37 |
fi |
38 |
|
39 |
if [ -z "${NIC_COUNT}" ] ; then |
40 |
log_error "Missing NIC_COUNT" |
41 |
exit 1 |
42 |
fi |
43 |
|
44 |
if [ -f "${NETWORKS_DIR}/instances/${FQDN}" ] ; then |
45 |
STATIC="yes" |
46 |
source ${NETWORKS_DIR}/instances/${FQDN} |
47 |
if [ -f "${NETWORKS_DIR}/subnets/${SUBNET}" ] ; then |
48 |
source ${NETWORKS_DIR}/subnets/${SUBNET} |
49 |
else |
50 |
echo "No subnet file for subnet ${SUBNET}!" |
51 |
exit 1 |
52 |
fi |
53 |
fi |
54 |
|
55 |
# Functions |
56 |
debian_setup() { |
57 |
if [ ! -d "${TARGET}/etc/network" ] ; then |
58 |
log_error "Missing target network directory" |
59 |
exit 1 |
60 |
fi |
61 |
|
62 |
if [ -z "${STATIC}" ] ; then |
63 |
cat > ${TARGET}/etc/network/interfaces << EOF |
64 |
# This file describes the network interfaces available on your system |
65 |
# and how to activate them. For more information, see interfaces(5). |
66 |
|
67 |
auto lo |
68 |
iface lo inet loopback |
69 |
|
70 |
auto eth0 |
71 |
iface eth0 inet dhcp |
72 |
|
73 |
EOF |
74 |
else |
75 |
cat > ${TARGET}/etc/network/interfaces << EOF |
76 |
# This file describes the network interfaces available on your system |
77 |
# and how to activate them. For more information, see interfaces(5). |
78 |
|
79 |
auto lo |
80 |
iface lo inet loopback |
81 |
|
82 |
auto eth0 |
83 |
iface eth0 inet static |
84 |
address ${ADDRESS} |
85 |
netmask ${NETMASK} |
86 |
gateway ${GATEWAY} |
87 |
|
88 |
EOF |
89 |
fi |
90 |
|
91 |
if [ -n "${FQDN}" ] ; then |
92 |
echo "${SHORT_NAME}" > ${TARGET}/etc/hostname |
93 |
fi |
94 |
} |
95 |
|
96 |
redhat_setup() { |
97 |
if [ ! -d "${TARGET}/etc/sysconfig/network-scripts" ] ; then |
98 |
log_error "Missing target network directory" |
99 |
exit 1 |
100 |
fi |
101 |
if [ -z "${STATIC}" ] ; then |
102 |
cat > ${TARGET}/etc/sysconfig/network-scripts/ifcfg-eth0 << EOF |
103 |
DEVICE=eth0 |
104 |
BOOTPROTO=dhcp |
105 |
ONBOOT=yes |
106 |
EOF |
107 |
else |
108 |
cat > ${TARGET}/etc/sysconfig/network-scripts/ifcfg-eth0 << EOF |
109 |
DEVICE=eth0 |
110 |
BOOTPROTO=static |
111 |
IPADDR=${ADDRESS} |
112 |
NETMASK=${NETMASK} |
113 |
ONBOOT=yes |
114 |
EOF |
115 |
fi |
116 |
|
117 |
if [ -n "${FQDN}" ] ; then |
118 |
cat > ${TARGET}/etc/sysconfig/network << EOF |
119 |
NETWORKING=yes |
120 |
HOSTNAME=${FQDN} |
121 |
GATEWAY=${GATEWAY} |
122 |
EOF |
123 |
else |
124 |
cat > ${TARGET}/etc/sysconfig/network << EOF |
125 |
NETWORKING=yes |
126 |
GATEWAY=${GATEWAY} |
127 |
EOF |
128 |
fi |
129 |
} |
130 |
|
131 |
gentoo_setup() { |
132 |
if [ ! -f "${TARGET}/etc/conf.d/net" ] ; then |
133 |
log_error "Missing target network file" |
134 |
exit 1 |
135 |
fi |
136 |
if [ -z "${STATIC}" ] ; then |
137 |
cat > ${TARGET}/etc/conf.d/net << EOF |
138 |
config_eth0=( "dhcp" ) |
139 |
EOF |
140 |
else |
141 |
cat > ${TARGET}/etc/conf.d/net << EOF |
142 |
config_eth0=( "${ADDRESS} netmask ${NETMASK}" ) |
143 |
routes_eth0=( "default gw ${GATEWAY}" ) |
144 |
EOF |
145 |
fi |
146 |
|
147 |
chroot ${TARGET} ln -sf /etc/init.d/net.lo /etc/init.d/net.eth0 |
148 |
chroot ${TARGET} rc-update add net.eth0 default |
149 |
|
150 |
if [ -n "${FQDN}" ] ; then |
151 |
# baselayout-2.x |
152 |
if [ -d "${TARGET}/usr/share/openrc/" ] ; then |
153 |
cat > ${TARGET}/etc/conf.d/hostname << EOF |
154 |
hostname="${SHORT_NAME}" |
155 |
EOF |
156 |
else |
157 |
cat > ${TARGET}/etc/conf.d/hostname << EOF |
158 |
HOSTNAME="${SHORT_NAME}" |
159 |
EOF |
160 |
fi |
161 |
fi |
162 |
} |
163 |
|
164 |
suse_setup() { |
165 |
if [ ! -d ${TARGET}/etc/sysconfig/network ] ; then |
166 |
log_error "Missing target network directory" |
167 |
exit 1 |
168 |
fi |
169 |
cat > ${TARGET}/etc/sysconfig/network/ifcfg-eth0 << EOF |
170 |
BOOTPROTO='dhcp4' |
171 |
STARTMODE='auto' |
172 |
NAME='Ethernet Card 0' |
173 |
EOF |
174 |
if [ -n "${FQDN}" ] ; then |
175 |
echo "${FQDN}" > ${TARGET}/etc/HOSTNAME |
176 |
fi |
177 |
} |
178 |
|
179 |
# Main |
180 |
get_os_type $TARGET |
181 |
|
182 |
if [ "${NIC_COUNT}" -gt 0 -a -n "${OS_TYPE}" ] ; then |
183 |
${OS_TYPE}_setup |
184 |
else |
185 |
log_error "Unsupported OS_TYPE" |
186 |
fi |
187 |
|
188 |
|
189 |
exit 0 |