summaryrefslogtreecommitdiffstats
path: root/snippets/post_install_network_config
blob: e6839f776387964fbfe468823f767ad5d9349098 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# Start post_install_network_config generated code
#if $getVar("system_name","") != ""
    ## this is being provisioned by system records, not profile records
    ## so we can do the more complex stuff
    ## get the list of interface names
    #set ikeys = $interfaces.keys()
    #import re
    #set $vlanpattern = $re.compile("[a-zA-Z0-9]+\.[0-9]+")
    ## Determine if we should use the MAC address to configure the interfaces first
    ## Only physical interfaces are required to have a MAC address
    ## Also determine the number of bonding devices we have, so we can set the
    ## max-bonds option in modprobe.conf accordingly. -- jcapel
    #
    #set $configbymac = True
    #set $numbondingdevs = 0
    ## =============================================================================
    #for $iname in $ikeys
        ## look at the interface hash data for the specific interface
    	#set $idata = $interfaces[$iname]
        ## do not configure by mac address if we don't have one AND it's not for bonding/vlans
        ## as opposed to a "real" physical interface
    	#if $idata["mac_address"] == "" and not $vlanpattern.match($iname) and not $idata["bonding"].lower() == "master":
    		## we have to globally turn off the config by mac feature as we can't
    		## use it now
    		#set $configbymac = False
    	#end if
    	## count the number of bonding devices we have.
    	#if $idata["bonding"].lower() == "master"
    	    #set $numbondingdevs += 1
    	#end if
    #end for
    ## end looping through the interfaces to see which ones we need to configure.
    ## =============================================================================
    #set $i = 0
    ## setup bonding if we have to
    #if $numbondingdevs > 0
if [ -f "/etc/modprobe.conf" ]; then
    echo "options bonding max_bonds=$numbondingdevs" >> /etc/modprobe.conf
fi 
    #end if
    ## =============================================================================
    ## create a staging directory to build out our network scripts into
    ## make sure we preserve the loopback device
mkdir /etc/sysconfig/network-scripts/cobbler
cp /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/cobbler/
    ## =============================================================================
    ## configure the gateway if set up (this is global, not a per-interface setting)
    #if $gateway != ""
grep -v GATEWAY /etc/sysconfig/network > /etc/sysconfig/network.cobbler
echo "GATEWAY=$gateway" >> /etc/sysconfig/network.cobbler
rm -f /etc/sysconfig/network
mv /etc/sysconfig/network.cobbler /etc/sysconfig/network
    #end if
    ## =============================================================================
    ## Configure the system's primary hostname. This is also passed to anaconda, but
    ## anaconda doesn't seem to honour it in DHCP-setups.
    #if $hostname != ""
grep -v HOSTNAME /etc/sysconfig/network > /etc/sysconfig/network.cobbler
echo "HOSTNAME=$hostname" >> /etc/sysconfig/network.cobbler
rm -f /etc/sysconfig/network
mv /etc/sysconfig/network.cobbler /etc/sysconfig/network
    # Also set the hostname now, some applications require it (e.g.: if we're
    # connecting to Puppet before a reboot).
/bin/hostname $hostname
    #end if
    ## =============================================================================
    ## now create the config file for each interface
    #for $iname in $ikeys
# Start configuration for $iname
        ## create lots of variables to use later
        #set $idata          = $interfaces[$iname]
        #set $mac            = $idata["mac_address"].upper()
        #set $static         = $idata["static"]
        #set $ip             = $idata["ip_address"]
        #set $netmask        = $idata["subnet"]
        #set $static_routes  = $idata["static_routes"]
        #set $bonding        = $idata["bonding"]
        #set $bonding_master = $idata["bonding_master"]
        #set $bonding_opts   = $idata["bonding_opts"]
        #set $devfile        = "/etc/sysconfig/network-scripts/cobbler/ifcfg-" + $iname
        #set $routesfile     = "/etc/sysconfig/network-scripts/cobbler/route-" + $iname
        ## determine if this interface is for a VLAN
        #if $vlanpattern.match($iname)
            ## If this is a VLAN interface, skip it, anaconda doesn't know
            ## about VLANs.
            #set $is_vlan = "true"
        #else
            #set $is_vlan = "false"
        #end if
        ## if this is a bonded interface, configure it in modprobe.conf
        #if $bonding.lower() == "master"
            ## Add required entry to modprobe.conf
if [ -f "/etc/modprobe.conf" ]; then
    echo "alias $iname bonding" >> /etc/modprobe.conf.cobbler
fi
        #end if
        #if $configbymac and $is_vlan == "false" and $bonding.lower() != "master"
            ## This is the code path physical interfaces will follow.
            ## Get the current interface name
IFNAME=\$(ifconfig -a | grep -i '$mac' | cut -d ' ' -f 1)
            ## Rename this interface in modprobe.conf
            ## FIXME: if both interfaces startwith eth this is wrong
if [ -f "/etc/modprobe.conf" ]; then
    grep \$IFNAME /etc/modprobe.conf | sed "s/\$IFNAME/$iname/" >> /etc/modprobe.conf.cobbler
    grep -v \$IFNAME /etc/modprobe.conf >> /etc/modprobe.conf.new
    rm -f /etc/modprobe.conf
    mv /etc/modprobe.conf.new /etc/modprobe.conf
fi
echo "DEVICE=$iname" > $devfile
echo "HWADDR=$mac" >> $devfile
echo "ONBOOT=yes" >> $devfile
            #if $bonding.lower() == "slave" and $bonding_master != ""
                ## if needed setup bonding
echo "SLAVE=yes" >> $devfile
echo "MASTER=$bonding_master" >> $devfile
                ## see Red Hat bugzilla 442339
echo "HOTPLUG=no" >> $devfile
            #end if
            #if $static or $bonding.lower() == "slave"
                ## for static or slave interfaces
                #if $ip != "" and $bonding.lower() != "slave"
                    ## Only configure static networking if an IP-address is
                    ## configured
echo "BOOTPROTO=static" >> $devfile
echo "IPADDR=$ip" >> $devfile
                    #if $netmask == ""
                        ## Default to 255.255.255.0?
                        #set $netmask = "255.255.255.0"
                    #end if
echo "NETMASK=$netmask" >> $devfile
                #else
                    ## Leave the interface unconfigured
                    ## we don't have enough info for static configuration
echo "BOOTPROTO=none" >> $devfile
                #end if
            #else
                ## this is a DHCP interface, much less work to do
echo "BOOTPROTO=dhcp" >> $devfile
            #end if
        #else if $is_vlan == "true" or $bonding.lower() == "master"
            ## Handle non-physical interfaces with special care. :)
echo "# Cobbler generated non-physical interface" > $devfile
echo "DEVICE=$iname" >> $devfile
            #if $is_vlan == "true"
                ## configure vlan if required
echo "VLAN=yes" >> $devfile
            #end if
            #if $bonding.lower() == "master" and $bonding_opts != ""
                ## configure bonding if required
cat >> $devfile << EOF
BONDING_OPTS="$bonding_opts"
EOF
            #end if
echo "ONPARENT=yes" >> $devfile
            #if $static
                ## for static non-physical interfaces...
                #if $ip != ""
                    ## Only configure static networking if an IP-address is
                    ## configured
echo "BOOTPROTO=static" >> $devfile
echo "IPADDR=$ip" >> $devfile
                    #if $netmask == ""
                        ## Default to 255.255.255.0?
                        #set $netmask = "255.255.255.0"
                    #end if
echo "NETMASK=$netmask" >> $devfile
                #else
                    ## Leave the interface unconfigured
echo "BOOTPROTO=none" >> $devfile
                #end if
            #else
echo "BOOTPROTO=dhcp" >> $devfile
            #end if
        #else if $configbymac == False
            ## We'll end up here when not all physical interfaces present for
            ## this system have MAC-addresses configured for them. We don't
            ## support interface renaming here.
MAC=\$(ifconfig -a | grep $iname | awk '{ print \$5 }')
echo "DEVICE=$iname" > $devfile
echo "HWADDR=\$MAC" >> $devfile
echo "ONBOOT=yes" >> $devfile
            #if $bonding.lower() == "slave" and $bonding_master != ""
                ## if needed setup bonding
echo "SLAVE=yes" >> $devfile
echo "MASTER=$bonding_master" >> $devfile
                ## see Red Hat bugzilla 442339
echo "HOTPLUG=no" >> $devfile
            #end if
            #if $static or $bonding.lower() == "slave"
                ## for static or slave interfaces
                #if $ip != "" and $bonding.lower() != "slave"
                    ## Only configure static networking if an IP-address is
                    ## configured
echo "BOOTPROTO=static" >> $devfile
echo "IPADDR=$ip" >> $devfile
                    #if $netmask == ""
                        ## Default to 255.255.255.0?
                        #set $netmask = "255.255.255.0"
                    #end if
echo "NETMASK=$netmask" >> $devfile
                #else
                    ## Leave the interface unconfigured
                    ## we don't have enough info for static configuration
echo "BOOTPROTO=none" >> $devfile
                #end if
            #else
                ## this is a DHCP interface, much less work to do
echo "BOOTPROTO=dhcp" >> $devfile
            #end if
        #else
            # If you end up here, please mail the list... This shouldn't
            # happen. ;-) -- jcapel
	#end if
        ## If the interface is anything but a slave then add DNSn entry
        #if $bonding.lower() != "slave"
            #set $nct = 0
            #for $nameserver in $name_servers
                #set $nct = $nct + 1
echo "DNS$nct=$nameserver" >> $devfile
            #end for
	#end if
	#for $route in $static_routes
	    #set routepattern = $re.compile("[0-9/.]+:[0-9.]+")
	    #if $routepattern.match($route)
                #set $routebits = $route.split(":")
                #set [$network, $router] = $route.split(":")
echo "$network via $router" >> $routesfile
	    #else
                # Warning: invalid route "$route"
            #end if
	#end for
	#set $i = $i + 1
# End configuration for $iname
    #end for	
    ## =============================================================================
    ## Configure name server search path in /etc/resolv.conf
    #if $name_servers_search != ""
sed -i -e "/^search /d" /etc/resolv.conf
echo -n "search " >>/etc/resolv.conf
        #for $nameserversearch in $name_servers_search
echo -n "$nameserversearch " >>/etc/resolv.conf
        #end for
echo "" >>/etc/resolv.conf
    #end if
    ## =============================================================================
    ## Configure name server search path in /etc/resolv.conf
    #if $name_servers_search != ""
sed -i -e "/^nameserver /d" /etc/resolv.conf
        #for $nameserver in $name_servers
echo "nameserver $nameserver" >>/etc/resolv.conf
        #end for
    #end if
## Move all staged files to their final location
rm -f /etc/sysconfig/network-scripts/ifcfg-*
mv /etc/sysconfig/network-scripts/cobbler/* /etc/sysconfig/network-scripts/
rm -r /etc/sysconfig/network-scripts/cobbler
if [ -f "/etc/modprobe.conf" ]; then
cat /etc/modprobe.conf.cobbler >> /etc/modprobe.conf
rm -f /etc/modprobe.conf.cobbler
fi
#end if
# End post_install_network_config generated code