2869
Comment: Add link to french translation + tags
|
8177
|
Deletions are marked like this. | Additions are marked like this. |
Line 7: | Line 7: |
This updated version of the script now also supports RPM based systems... sorry about that! |
|
Line 8: | Line 11: |
By default the script assumes eth0 is the primary interface and if bonding then eth1 is the secondary interface but these can be overridden on the command line. |
|
Line 10: | Line 15: |
== Usage: == mk-net-static [--debug] [--bond] [primary_interface [secondary_interface]] eg: To make bond0 out of em1 em2 p1p1 and p1p2 using the IP address assigned by dhcp to em1 run: mk-net-static --bond em1 em2 p1p1 and p1p2 |
|
Line 16: | Line 30: |
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin #variables SYSCONFIG=/etc/sysconfig SCRIPTS=/etc/sysconfig/network-scripts BACKUPD=$SCRIPTS/backup-files INTERFACES=/etc/network/interfaces |
|
Line 39: | Line 61: |
gen-interfaces(){ cat <<EOF | $FILTER |
get-current(){ #$1 = optional ethernet device (defaults to eth0) #Use sed magic and function cidr2mask to convert ip command output to bash variables DEVICE=${1:-eth0} IPADDR=$(ip addr show $DEVICE|\ sed -n -e's%.*inet \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)/\([0-9]*\) brd.*scope global.*%\1%p') BROADCAST=$(ip addr show $DEVICE|\ sed -n -e's%.*inet.*brd \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\) scope global.*%\1%p') NETMASK=$(ip addr show $DEVICE |\ sed -n -e's%.*inet [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/\([0-9]*\) brd.*scope global.*%\1\n%p' |\ (read mask; if [ -n "$mask" ] ; then printf "%s\n" `cidr2mask $mask` ; fi)) GATEWAY=$(ip route show |sed -n -e's%default via \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\) dev \(\w*\)\ .*%\1%p') #DOMAIN=$(cat /etc/resolv.conf | sed -e '/^#.*$/d' -e's/#.*//' | sed -n -e's%domain \(\w*\)%\1%p') DOMAIN=$(dnsdomainname) I present this here in the hope it helps someone else. NAMESERVERS=$(cat /etc/resolv.conf | sed -e '/^#.*$/d' -e's/#.*//' | sed -n -e's%nameserver \(\w*\)%\1%p') set -- $NAMESERVERS DNS1=$1 DNS2=$2 } get-hwaddr(){ #$1 = optional ethernet device (defaults to eth0) DEVICE=${1:-eth0} ip addr show $DEVICE | sed -n -e's%.*link/ether \([0-9A-F:].*\) brd.*%\1%p' | tr 'a-z' 'A-Z' } backup-files(){ if [ -d $SCRIPTS ]; then #redhat cd $SYSCONFIG [ -f "$SYSCONFIG/network.orig" ] || cp -p "$SYSCONFIG/network" "$SYSCONFIG/network.orig" cp -p "$SYSCONFIG/network" "$SYSCONFIG/network-`date +%F-%T`" [ -d $BACKUPD ] || mkdir $BACKUPD cd $SCRIPTS/ for name in ifcfg-* ; do [ -f "$BACKUPD/${name}.orig" ] || cp -p "$name" "$BACKUPD/${name}.orig" cp -p "$name" "$BACKUPD/`date +%F-%T`-${name}" done fi if [ -f "${INTERFACES}" ] ; then #debian [ -f "${INTERFACES}.orig" ] || cp -p "${INTERFACES}" "${INTERFACES}.orig" cp -p "${INTERFACES}" "${INTERFACES}-`date +%F-%T`" fi } fix-zeroconf(){ if [ -d $SCRIPTS ]; then #redhat if ! grep -q "NOZEROCONF=yes" $SYSCONFIG/network ; then echo "NOZEROCONF=yes" >>$SYSCONFIG/network fi fi } fix-networkmanager(){ local RUN RUN="" if $DEBUG ; then RUN="echo DEBUG: " fi if [ -d $SCRIPTS ]; then #redhat $RUN chkconfig NetworkManager off $RUN service NetworkManager stop $RUN chkconfig network on fi } gen-ifcfg-dev-static(){ #$1 = optional ethernet device (defaults to eth0) DEVICE=${1:-eth0} HWADDR=$(get-hwaddr $DEVICE) cat <<EOF >$SCRIPTS/ifcfg-$DEVICE DEVICE=$DEVICE NM_CONTROLLED=no #TYPE="Ethernet" #HWADDR=$HWADDR IPV6INIT=no ONBOOT=yes USERCTL=no BOOTPROTO=none IPADDR=$IPADDR NETMASK=$NETMASK BROADCAST=$BROADCAST GATEWAY=$GATEWAY DOMAIN=$DOMAIN DNS1=$DNS1 DNS2=$DNS2 EOF } gen-ifcfg-bond-static(){ #$1 = optional bond device (default bond0) DEVICE=${1:-bond0} cat <<EOF >$SCRIPTS/ifcfg-$DEVICE DEVICE=$DEVICE BONDING_OPTS="mode=4 miimon=100 xmit_hash_policy=layer3+4" NM_CONTROLLED=no #TYPE=Bond #BONDING_MASTER=yes IPV6INIT=no ONBOOT=yes USERCTL=no BOOTPROTO=none IPADDR=$IPADDR NETMASK=$NETMASK BROADCAST=$BROADCAST GATEWAY=$GATEWAY DOMAIN=$DOMAIN DNS1=$DNS1 DNS2=$DNS2 EOF } gen-ifcfg-bond-slave(){ #$1 = optional ethernet device (defaults to eth0) DEVICE=${1:-eth0} HWADDR=$(get-hwaddr $DEVICE) if $DEBUG ; then echo DEBUG: gen-ifcfg-bond-slave dev $DEVICE mac $HWADDR fi cat <<EOF >$SCRIPTS/ifcfg-$DEVICE DEVICE=$DEVICE MASTER=bond0 SLAVE=yes NM_CONTROLLED=no ONBOOT=yes USERCTL=no BOOTPROTO=none #HWADDR=$HWADDR #TYPE=Ethernet EOF } gen-interfaces-static(){ DEVICE=${1:-eth0} cat <<EOF >"${INTERFACES}" # This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). #source /etc/network/interfaces.d/* |
Line 47: | Line 224: |
auto eth0 EOF #Use sed magic and function cidr2mask to convert ip command output into output of this form # eth0-1 iface eth0 inet static # eth0-2 address nnn.nnn.50.203 # eth0-4 broadcast nnn.nnn.51.255 # eth0-3 netmask nnn.nnn.254.0 # eth0-5 gateway 138.37.51.254 #In preparation for further filtering ( ip addr show |\ sed -n -e's%.*inet \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)/\([0-9]*\) brd \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\) scope global \(.*\)%'\ '\4-1 iface \4 inet static\n\4-2 \taddress \1\n\4-4 \tbroadcast \3%p' ip addr show |\ sed -n -e's%.*inet \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)/\([0-9]*\) brd \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\) scope global \(.*\)%'\ '\4-3 \2\n%p' | (read key mask; if [ -n "$mask" ] ; then printf "%s \t%s %s\n" $key netmask `cidr2mask $mask` ; fi) ip route show |sed -n -e's%default via \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\) dev \(.*\) %\2-5 \t\gateway \1%p' )| sort | cut -f2- -d" " | $FILTER if [ $BOND = 1 ] ; then cat <<EOF slaves eth0 eth1 bond_mode 802.3ad bond_miimon 100 bond_updelay 200 bond_downdelay 200 bond_xmit_hash_policy layer2+3 EOF fi } |
auto $DEVICE iface $DEVICE inet static address $IPADDR broadcast $BROADCAST netmask $NETMASK gateway $GATEWAY #dns-nameservers $DNS1 $DNS2 EOF } gen-interfaces-bond-static(){ DEVICE=${1:-bond0} cat <<EOF >"${INTERFACES}" # This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). #source /etc/network/interfaces.d/* # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto $DEVICE iface $DEVICE inet static address $IPADDR broadcast $BROADCAST netmask $NETMASK gateway $GATEWAY #dns-nameservers $DNS1 $DNS2 slaves $slaves bond_mode 802.3ad bond_miimon 100 bond_updelay 200 bond_downdelay 200 bond_xmit_hash_policy layer3+4 EOF } |
Line 82: | Line 266: |
primary=eth0 slaves="eth0 eth1" DEBUG=false RUN="" if [ "$1" = "--debug" ] ; then DEBUG=true shift RUN="echo DEUBG: " SYSCONFIG=/tmp/sysconfig SCRIPTS=/tmp/sysconfig/network-scripts BACKUPD=$SCRIPTS/backup-files INTERFACES=/tmp/network/interfaces echo "DEBUG MODE: altering files under /tmp not /etc" fi |
|
Line 85: | Line 286: |
BOND=1 fi FILTER=cat if [ $BOND = 1 ] ; then FILTER='sed -e s/eth0/bond0/ ' fi [ -f /etc/network/interfaces.orig ] || cp -p /etc/network/interfaces /etc/network/interfaces.orig gen-interfaces >/etc/network/interfaces.new ifdown eth0 cp -p /etc/network/interfaces /etc/network/interfaces-"`date +%F-%T`" mv /etc/network/interfaces.new /etc/network/interfaces if [ $BOND = 1 ] ; then ifconfig eth0 0.0.0.0 ifup bond0 else ifup eth0 |
BOND=1 shift fi #Any further command line args are primary and secondary interfaces if [ $# -gt 0 ] ; then primary=$1 shift fi if [ $# -gt 0 ] ; then secondaries="$*" slaves="$primary $secondaries" fi if $DEBUG ; then echo DEBUG: bond $BOND, primary $primary, secondaries $secondaries. fi backup-files get-current $primary fix-zeroconf fix-networkmanager $RUN ifdown $primary if [ -d $SCRIPTS ]; then #redhat if [ "$BOND" = "0" ] ; then #single static device gen-ifcfg-dev-static $primary else #bonded static device gen-ifcfg-bond-static for dev in $slaves ; do gen-ifcfg-bond-slave $dev done $RUN ifconfig $primary 0.0.0.0 fi #Start new network config $RUN service network restart fi if [ -f "${INTERFACES}" ] ; then #debian if [ "$BOND" = "0" ] ; then #single static device gen-interfaces-static $RUN ifup $primary else #bonded static device gen-interfaces-bond-static $RUN ifconfig $primary 0.0.0.0 $RUN ifup bond0 fi |
Translation(s): English - Français - Italiano
There is a long standing bug (267838, marked wontfix) against the debian-installer, to allow a machine to boot via DHCP during install, but create the installed system with a static network config.
Here is a script I have written to run on first boot to make that change for me.
This updated version of the script now also supports RPM based systems... sorry about that!
It also accepts a "--bond" argument to convert to a static bonded interface. By default the script assumes eth0 is the primary interface and if bonding then eth1 is the secondary interface but these can be overridden on the command line.
I present this here in the hope it helps someone else.
Usage:
mk-net-static [--debug] [--bond] [primary_interface [secondary_interface]]
eg: To make bond0 out of em1 em2 p1p1 and p1p2 using the IP address assigned by dhcp to em1 run:
mk-net-static --bond em1 em2 p1p1 and p1p2
1 #!/bin/bash
2 set -e
3
4 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
5
6 #variables
7 SYSCONFIG=/etc/sysconfig
8 SCRIPTS=/etc/sysconfig/network-scripts
9 BACKUPD=$SCRIPTS/backup-files
10 INTERFACES=/etc/network/interfaces
11
12 #functions
13
14 cidr2mask() {
15 local i mask=""
16 local full_octets=$(($1/8))
17 local partial_octet=$(($1%8))
18
19 for ((i=0;i<4;i+=1)); do
20 if [ $i -lt $full_octets ]; then
21 mask+=255
22 elif [ $i -eq $full_octets ]; then
23 mask+=$((256 - 2**(8-$partial_octet)))
24 else
25 mask+=0
26 fi
27 test $i -lt 3 && mask+=.
28 done
29
30 echo $mask
31 }
32
33
34 get-current(){ #$1 = optional ethernet device (defaults to eth0)
35 #Use sed magic and function cidr2mask to convert ip command output to bash variables
36
37 DEVICE=${1:-eth0}
38
39 IPADDR=$(ip addr show $DEVICE|\
40 sed -n -e's%.*inet \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)/\([0-9]*\) brd.*scope global.*%\1%p')
41 BROADCAST=$(ip addr show $DEVICE|\
42 sed -n -e's%.*inet.*brd \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\) scope global.*%\1%p')
43 NETMASK=$(ip addr show $DEVICE |\
44 sed -n -e's%.*inet [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/\([0-9]*\) brd.*scope global.*%\1\n%p' |\
45 (read mask; if [ -n "$mask" ] ; then printf "%s\n" `cidr2mask $mask` ; fi))
46 GATEWAY=$(ip route show |sed -n -e's%default via \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\) dev \(\w*\)\ .*%\1%p')
47 #DOMAIN=$(cat /etc/resolv.conf | sed -e '/^#.*$/d' -e's/#.*//' | sed -n -e's%domain \(\w*\)%\1%p')
48 DOMAIN=$(dnsdomainname)
49 I present this here in the hope it helps someone else.
50 NAMESERVERS=$(cat /etc/resolv.conf | sed -e '/^#.*$/d' -e's/#.*//' | sed -n -e's%nameserver \(\w*\)%\1%p')
51 set -- $NAMESERVERS
52 DNS1=$1
53 DNS2=$2
54 }
55
56 get-hwaddr(){ #$1 = optional ethernet device (defaults to eth0)
57 DEVICE=${1:-eth0}
58 ip addr show $DEVICE | sed -n -e's%.*link/ether \([0-9A-F:].*\) brd.*%\1%p' | tr 'a-z' 'A-Z'
59 }
60
61
62 backup-files(){
63 if [ -d $SCRIPTS ]; then #redhat
64 cd $SYSCONFIG
65 [ -f "$SYSCONFIG/network.orig" ] || cp -p "$SYSCONFIG/network" "$SYSCONFIG/network.orig"
66 cp -p "$SYSCONFIG/network" "$SYSCONFIG/network-`date +%F-%T`"
67
68 [ -d $BACKUPD ] || mkdir $BACKUPD
69 cd $SCRIPTS/
70 for name in ifcfg-* ; do
71 [ -f "$BACKUPD/${name}.orig" ] || cp -p "$name" "$BACKUPD/${name}.orig"
72 cp -p "$name" "$BACKUPD/`date +%F-%T`-${name}"
73 done
74 fi
75 if [ -f "${INTERFACES}" ] ; then #debian
76 [ -f "${INTERFACES}.orig" ] || cp -p "${INTERFACES}" "${INTERFACES}.orig"
77 cp -p "${INTERFACES}" "${INTERFACES}-`date +%F-%T`"
78 fi
79 }
80
81 fix-zeroconf(){
82 if [ -d $SCRIPTS ]; then #redhat
83 if ! grep -q "NOZEROCONF=yes" $SYSCONFIG/network ; then
84 echo "NOZEROCONF=yes" >>$SYSCONFIG/network
85 fi
86 fi
87 }
88
89 fix-networkmanager(){
90 local RUN
91 RUN=""
92 if $DEBUG ; then
93 RUN="echo DEBUG: "
94 fi
95 if [ -d $SCRIPTS ]; then #redhat
96 $RUN chkconfig NetworkManager off
97 $RUN service NetworkManager stop
98 $RUN chkconfig network on
99 fi
100 }
101
102 gen-ifcfg-dev-static(){ #$1 = optional ethernet device (defaults to eth0)
103 DEVICE=${1:-eth0}
104 HWADDR=$(get-hwaddr $DEVICE)
105
106 cat <<EOF >$SCRIPTS/ifcfg-$DEVICE
107 DEVICE=$DEVICE
108
109 NM_CONTROLLED=no
110 #TYPE="Ethernet"
111 #HWADDR=$HWADDR
112
113 IPV6INIT=no
114
115 ONBOOT=yes
116 USERCTL=no
117 BOOTPROTO=none
118
119 IPADDR=$IPADDR
120 NETMASK=$NETMASK
121 BROADCAST=$BROADCAST
122 GATEWAY=$GATEWAY
123 DOMAIN=$DOMAIN
124 DNS1=$DNS1
125 DNS2=$DNS2
126 EOF
127 }
128
129
130 gen-ifcfg-bond-static(){ #$1 = optional bond device (default bond0)
131 DEVICE=${1:-bond0}
132
133 cat <<EOF >$SCRIPTS/ifcfg-$DEVICE
134 DEVICE=$DEVICE
135 BONDING_OPTS="mode=4 miimon=100 xmit_hash_policy=layer3+4"
136
137 NM_CONTROLLED=no
138 #TYPE=Bond
139 #BONDING_MASTER=yes
140
141 IPV6INIT=no
142
143 ONBOOT=yes
144 USERCTL=no
145 BOOTPROTO=none
146
147 IPADDR=$IPADDR
148 NETMASK=$NETMASK
149 BROADCAST=$BROADCAST
150 GATEWAY=$GATEWAY
151 DOMAIN=$DOMAIN
152 DNS1=$DNS1
153 DNS2=$DNS2
154 EOF
155
156 }
157
158 gen-ifcfg-bond-slave(){ #$1 = optional ethernet device (defaults to eth0)
159 DEVICE=${1:-eth0}
160 HWADDR=$(get-hwaddr $DEVICE)
161
162 if $DEBUG ; then
163 echo DEBUG: gen-ifcfg-bond-slave dev $DEVICE mac $HWADDR
164 fi
165
166 cat <<EOF >$SCRIPTS/ifcfg-$DEVICE
167 DEVICE=$DEVICE
168 MASTER=bond0
169 SLAVE=yes
170
171 NM_CONTROLLED=no
172
173 ONBOOT=yes
174 USERCTL=no
175 BOOTPROTO=none
176 #HWADDR=$HWADDR
177 #TYPE=Ethernet
178 EOF
179 }
180
181
182
183
184 gen-interfaces-static(){
185 DEVICE=${1:-eth0}
186 cat <<EOF >"${INTERFACES}"
187 # This file describes the network interfaces available on your system
188 # and how to activate them. For more information, see interfaces(5).
189
190 #source /etc/network/interfaces.d/*
191
192 # The loopback network interface
193 auto lo
194 iface lo inet loopback
195
196 # The primary network interface
197 auto $DEVICE
198 iface $DEVICE inet static
199 address $IPADDR
200 broadcast $BROADCAST
201 netmask $NETMASK
202 gateway $GATEWAY
203 #dns-nameservers $DNS1 $DNS2
204
205 EOF
206
207 }
208
209 gen-interfaces-bond-static(){
210 DEVICE=${1:-bond0}
211 cat <<EOF >"${INTERFACES}"
212 # This file describes the network interfaces available on your system
213 # and how to activate them. For more information, see interfaces(5).
214
215 #source /etc/network/interfaces.d/*
216
217 # The loopback network interface
218 auto lo
219 iface lo inet loopback
220
221 # The primary network interface
222 auto $DEVICE
223 iface $DEVICE inet static
224 address $IPADDR
225 broadcast $BROADCAST
226 netmask $NETMASK
227 gateway $GATEWAY
228 #dns-nameservers $DNS1 $DNS2
229 slaves $slaves
230 bond_mode 802.3ad
231 bond_miimon 100
232 bond_updelay 200
233 bond_downdelay 200
234 bond_xmit_hash_policy layer3+4
235 EOF
236 }
237
238 #Main
239 primary=eth0
240 slaves="eth0 eth1"
241
242 DEBUG=false
243 RUN=""
244 if [ "$1" = "--debug" ] ; then
245 DEBUG=true
246 shift
247
248 RUN="echo DEUBG: "
249 SYSCONFIG=/tmp/sysconfig
250 SCRIPTS=/tmp/sysconfig/network-scripts
251 BACKUPD=$SCRIPTS/backup-files
252 INTERFACES=/tmp/network/interfaces
253
254 echo "DEBUG MODE: altering files under /tmp not /etc"
255 fi
256
257 BOND=0
258 if [ "$1" = "--bond" ] ; then
259 BOND=1
260 shift
261 fi
262
263 #Any further command line args are primary and secondary interfaces
264 if [ $# -gt 0 ] ; then
265 primary=$1
266 shift
267 fi
268
269 if [ $# -gt 0 ] ; then
270 secondaries="$*"
271 slaves="$primary $secondaries"
272 fi
273
274 if $DEBUG ; then
275 echo DEBUG: bond $BOND, primary $primary, secondaries $secondaries.
276 fi
277
278
279
280 backup-files
281 get-current $primary
282 fix-zeroconf
283 fix-networkmanager
284
285 $RUN ifdown $primary
286
287 if [ -d $SCRIPTS ]; then #redhat
288 if [ "$BOND" = "0" ] ; then
289 #single static device
290 gen-ifcfg-dev-static $primary
291 else
292 #bonded static device
293 gen-ifcfg-bond-static
294 for dev in $slaves ; do
295 gen-ifcfg-bond-slave $dev
296 done
297 $RUN ifconfig $primary 0.0.0.0
298 fi
299 #Start new network config
300 $RUN service network restart
301 fi
302
303 if [ -f "${INTERFACES}" ] ; then #debian
304 if [ "$BOND" = "0" ] ; then
305 #single static device
306 gen-interfaces-static
307 $RUN ifup $primary
308 else
309 #bonded static device
310 gen-interfaces-bond-static
311 $RUN ifconfig $primary 0.0.0.0
312 $RUN ifup bond0
313 fi
314 fi