compass-adapters/cobbler/snippets/preseed_post_install_networ...

365 lines
14 KiB
Plaintext

## Start post_install_network_config generated code
#if $getVar('promisc_nics', '') != ""
#set promisc_interfaces = [promisc.strip() for promisc in $promisc_nics.split(',') if promisc.strip()]
#else
#set promisc_interfaces = []
#end if
#if $hostname != ""
# set the hostname
echo "$hostname" > /etc/hostname
/bin/hostname $hostname
#end if
$SNIPPET('preseed_hosts')
#set $num_ns_search = $len($name_servers_search)
#if $num_ns_search > 0
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
#set $num_ns = $len($name_servers)
#if $num_ns > 0
sed -i -e "/^nameserver /d" /etc/resolv.conf
#for $nameserver in $name_servers
echo "nameserver $nameserver" >>/etc/resolv.conf
#end for
#end if
echo '' > /tmp/network_log
physical_interfaces=""
for physical_interface in \$(ip -o link | cut -d: -f2 | grep -v lo); do
echo "processing physical interface \${physical_interface}" >> /tmp/network_log
if [ -z "\${physical_interfaces}" ]; then
physical_interfaces="\${physical_interface}"
else
physical_interfaces="\${physical_interfaces} \${physical_interface}"
fi
done
echo "physical interfaces \${physical_interfaces}" >> /tmp/network_log
for physical_interface in \${physical_interfaces}; do
mac=\$(cat /sys/class/net/\${physical_interface}/address)
mac=\$(echo \${mac} |tr 'a-z' 'A-Z' | tr ':' '_')
eval "physical_interface_mac_\${physical_interface}=\$mac"
eval "physical_mac_interface_\${mac}=\${physical_interface}"
done
for physcial_interface in \${physical_interfaces}; do
eval "mac=\\${physical_interface_mac_\${physical_interface}}"
echo "physical interface to mac: \${physcial_interface} => \${mac}" >> /tmp/network_log
done
logical_interfaces=""
#set ikeys = $interfaces.keys()
#set osversion = $getVar("os_version","")
#import re
#set $vlanpattern = $re.compile("([a-zA-Z0-9]+)[\.][0-9]+")
#set $subinterfacepattern = $re.compile("([a-zA-Z0-9]+)[:][0-9]+")
#set $numbondingdevs = 0
#for $iname in $ikeys
#set $idata = $interfaces[$iname]
#set $mac = $idata.get("mac_address", "").upper()
#set $interface_type = $idata.get("interface_type", "").lower()
#if $interface_type in ("master","bond","bonded_bridge_slave")
#set $numbondingdevs += 1
#end if
if [ -z "\${logical_interfaces}" ]; then
logical_interfaces="$iname"
else
logical_interfaces="\${logical_interfaces} $iname"
fi
#if $mac != ""
#set mac = $mac.replace(':', '_')
physical_interface=\${physical_mac_interface_$mac}
if [ ! -z "\${physical_interface}" ]; then
logical_interface_mapping_$iname=\${physical_interface}
eval "physical_interface_mapping_\${physical_interface}=$iname"
echo "set logical interface $iname map to physical interface \${physical_interface}" >> /tmp/network_log
else
logical_interface_mapping_$iname=$iname
physical_interface_mapping_$iname=$iname
echo "force set logical interface $iname to the same physical interface" >> /tmp/network_log
fi
#end if
#end for
echo "logical interfaces \${logical_interfaces}" >> /tmp/network_log
for logical_interface in \${logical_interfaces}; do
eval "physical_interface=\\${logical_interface_mapping_\${logical_interface}}"
if [ ! -z "\${physical_interface}" ]; then
continue
fi
eval "physical_interface_mac=\\${physical_interface_mac_\${logical_interface}}"
if [ -z "\${physical_interface_mac}" ]; then
continue
fi
eval "logical_interface_mapping_\${logical_interface}=\${logical_interface}"
eval "physical_interface_mapping_\${logical_interface}=\${logical_interface}"
echo "set logical interface \${logical_interface} to the same physical interface" >> /tmp/network_log
done
unset_physical_interfaces=""
for physical_interface in \${physical_interfaces}; do
eval "logical_interface=\\${physical_interface_mapping_\${physical_interface}}"
if [ ! -z "\${logical_interface}" ]; then
continue
fi
if [ -z "\${unset_physical_interfaces}" ]; then
unset_physical_interfaces="\${physical_interface}"
else
unset_physical_interfaces="\${unset_physical_interfaces} \${physical_interface}"
fi
done
echo "unset phsycial interfaces \${unset_physiccal_interfaces}" >> /tmp/network_log
unset_logical_interfaces=""
for logical_interface in \${logical_interfaces}; do
eval "physical_interface=\\${logical_interface_mapping_\${logical_interface}}"
if [ ! -z "\${physical_interface}" ]; then
continue
fi
if [ -z "\${unset_logical_interfaces}" ]; then
unset_logical_interfaces="\${logical_interface}"
else
unset_logical_interfaces="\${unset_logical_interfaces} \${logical_interface}"
fi
done
echo "unset logical interfaces \${unset_logical_interfaces}" >> /tmp/network_log
sorted_physical_interfaces=\$(printf '%s\n' \${unset_physical_interfaces} | sort)
echo "sorted unset physical interfaces \${sorted_physical_interfaces}" >> /tmp/network_log
sorted_logical_interfaces=\$(printf '%s\n' \${unset_logical_interfaces} | sort)
echo "sorted unset logical interfaces \${sorted_logical_interfaces}" >> /tmp/network_log
for logical_interface in \${sorted_logical_interfaces}; do
set \${sorted_physical_interfaces}
physical_interface_num=\$#;
if [ \${phsycial_interface_num} -gt 0 ]; then
physical_interface=\$1
shift 1
sorted_physical_interfaces="\$@"
else
physical_interface=\${logical_interface}
fi
eval "physical_interface_mapping_\${physical_interface}=\${logical_interface}"
eval "logical_interface_mapping_\${logical_interface}=\${physical_interface}"
done
for physical_interface in \${physical_interfaces}; do
eval "logical_interface=\\${physical_interface_mapping_\${physical_interface}}"
echo "physical interface \${physical_interface} map to logical interface \${logical_interface}" >> /tmp/network_log
done
for logical_interface in \${logical_interfaces}; do
eval "physical_interface=\\${logical_interface_mapping_\${logical_interface}}"
echo "logical interface \${logical_interface} map to physical interface \${physical_interface}" >> /tmp/network_log
done
#if $numbondingdevs > 0
# we have bonded interfaces, so set max_bonds
if [ -f "/etc/modprobe.conf" ]; then
echo "options bonding max_bonds=$numbondingdevs" >> /etc/modprobe.conf
fi
#end if
## =============================================================================
## Rewrite the interfaces file and make sure we preserve the loopback device
echo "auto lo" > /etc/network/interfaces
echo " iface lo inet loopback" >> /etc/network/interfaces
echo "" >> /etc/network/interfaces
#set $nameserver_set = 0
#for $iname in $ikeys
#set $idata = $interfaces[$iname]
#set $mac = $idata.get("mac_address", "").upper()
#set $iface_type = $idata.get("interface_type", "").lower()
#set $mtu = $idata.get("mtu", "")
#set $static = $idata.get("static", "")
#set $management = $idata.get("management", "")
#set $ip = $idata.get("ip_address", "")
#set $netmask = $idata.get("netmask", "")
#set $if_gateway = $idata.get("if_gateway", "")
#set $static_routes = $idata.get("static_routes", "")
#set $iface_master = $idata.get("interface_master", "")
#set $bonding_opts = $idata.get("bonding_opts", "")
#set $bridge_opts = $idata.get("bridge_opts", "")
#if $iface_type in ("slave","bond_slave","bridge_slave","bonded_bridge_slave")
#set $static = 1
#end if
## ===================================================================
## Things every interface get, no matter what
## ===================================================================
echo "auto $iname" >> /etc/network/interfaces
#if $static
#if $iface_type not in ("slave","bond_slave","bridge_slave","bonded_bridge_slave")
echo "iface $iname inet static" >> /etc/network/interfaces
#if $iname in $promisc_interfaces
echo " address 0.0.0.0" >> /etc/network/interfaces
echo " up ip link set $iname up" >> /etc/network/interfaces
echo " up ip link set $iname promisc on" >> /etc/network/interfaces
echo " down ip link set $iname promisc off" >> /etc/network/interfaces
echo " down ip link set $iname down" >> /etc/network/interfaces
#else
#if $ip != ""
echo " address $ip" >> /etc/network/interfaces
#end if
#if $if_gateway != ""
echo " gateway $if_gateway" >> /etc/network/interfaces
#end if
#if $netmask == ""
## Default to 255.255.255.0?
#set $netmask = "255.255.255.0"
#end if
echo " netmask $netmask" >> /etc/network/interfaces
#if $gateway != "" and $if_gateway == "" and $ip != ""
#import netaddr
#set interface_network = $netaddr.IPNetwork('%s/%s' % ($ip, $netmask))
#set gateway_address = $netaddr.IPAddress($gateway)
#if $gateway_address in $interface_network
echo " gateway $gateway" >> /etc/network/interfaces
#end if
#end if
#if $mtu != ""
echo " mtu $mtu" >> /etc/network/interfaces
#end if
#end if
#else
echo "iface $iname inet manual" >> /etc/network/interfaces
#end if
#else
echo "iface $iname inet dhcp" >> /etc/network/interfaces
#end if
#if $iface_type not in ("master","bond","bridge","bonded_bridge_slave")
#if $vlanpattern.match($iname)
#pass
#else
#if $subinterfacepattern.match($iname)
#pass
#else
physical_interface=\${logical_interface_mapping_$iname}
if [ ! -z "\${physical_interface}" ]; then
eval "physical_mac=\\${physical_interface_mac_\${physical_interface}}"
fi
if [ ! -z "\${physical_mac}" ]; then
physical_mac=\$(echo \${physical_mac} | tr '_' ':')
echo " hwaddress ether \${physical_mac}" >> /etc/network/interfaces
fi
if [ -f "/etc/modprobe.conf" ] && [ ! -z "\${physical_interface}" ]; then
grep \${physical_interface} /etc/modprobe.conf | sed "s/\$physical_interface/$iname/" >> /etc/modprobe.conf.cobbler
grep -v \${physical_interface} /etc/modprobe.conf >> /etc/modprobe.conf.new
rm -f /etc/modprobe.conf
mv /etc/modprobe.conf.new /etc/modprobe.conf
fi
#end if
#end if
#end if
#if $iface_type in ("master","bond","bonded_bridge_slave")
## if this is a bonded interface, configure it in modprobe.conf
if [ -f "/etc/modprobe.conf" ]; then
#if $osversion == "rhel4"
echo "install $iname /sbin/modprobe bonding -o $iname $bonding_opts" >> /etc/modprobe.conf.cobbler
#else
echo "alias $iname bonding" >> /etc/modprobe.conf.cobbler
#end if
fi
#set $bondslaves = ""
#for $bondiname in $ikeys
#set $bondidata = $interfaces[$bondiname]
#set $bondiface_type = $bondidata.get("interface_type", "").lower()
#set $bondiface_master = $bondidata.get("interface_master", "")
#if $bondiface_master == $iname
#set $bondslaves += $bondiname + " "
#end if
#end for
echo " bond-slaves $bondslaves" >> /etc/network/interfaces
#if $bonding_opts != ""
#for $bondopts in $bonding_opts.split(" ")
#set [$bondkey, $bondvalue] = $bondopts.split("=")
echo " bond-$bondkey $bondvalue" >> /etc/network/interfaces
#end for
#end if
#elif $iface_type in ("slave","bond_slave") and $iface_master != ""
echo " bond-master $iface_master" >> /etc/network/interfaces
#end if
#if $iface_type == "bridge"
#set $bridgeslaves = ""
#for $bridgeiname in $ikeys
#set $bridgeidata = $interfaces[$bridgeiname]
#set $bridgeiface_type = $bridgeidata.get("interface_type", "").lower()
#set $bridgeiface_master = $bridgeidata.get("interface_master", "")
#if $bridgeiface_master == $iname
#set $bridgeslaves += $bridgeiname + " "
#end if
#end for
echo " bridge_ports $bridgeslaves" >> /etc/network/interfaces
#if $bridge_opts != ""
#for $bridgeopts in $bridge_opts.split(" ")
#set [$bridgekey, $bridgevalue] = $bridgeopts.split("=")
echo " bridge_$bridgekey $bridgevalue" >> /etc/network/interfaces
#end for
#end if
#end if
#if not $nameserver_set
#if $iface_type not in ("slave","bond_slave","bridge_slave","bonded_bridge_slave")
#set $nameservers = ' '.join($name_servers)
#set $nameserver_search = ' '.join($name_servers_search)
#if $nameservers != ""
echo " dns-nameservers $nameservers" >> /etc/network/interfaces
#end if
#if $nameserver_search != ""
echo " dns-search $nameserver_search" >> /etc/network/interfaces
#end if
#set $nameserver_set = 1
#end if
#end if
#for $route in $static_routes
#set routepattern = $re.compile("[0-9/.]+:[0-9.]+")
#if $routepattern.match($route)
#set [$network, $router] = $route.split(":")
echo " up ip route add $network via $router dev $iname" >> /etc/network/interfaces
#else
echo " # Warning: invalid route: $route" >> /etc/network/interfaces
#end if
#end for
echo "" >> /etc/network/interfaces
## ===================================================================
## Done with this interface
## ===================================================================
#end for
for physical_interface in \${sorted_physical_interfaces}; do
echo "auto \${physical_interface}" >> /etc/network/interfaces
echo "iface \${physical_interface} inet static" >> /etc/network/interfaces
eval "physical_mac=\\${physical_interface_mac_\${physical_interface}}"
if [ ! -z "\${physical_mac}" ]; then
physical_mac=\$(echo \${physical_mac} | tr '_' ':')
echo " hwaddress ether \${physical_mac}" >> /etc/network/interfaces
fi
echo "" >> /etc/network/interfaces
done
if [ -f "/etc/modprobe.conf" ]; then
cat /etc/modprobe.conf.cobbler >> /etc/modprobe.conf
rm -f /etc/modprobe.conf.cobbler
fi
if [ -f "/etc/udev/rules.d/70-persistent-net.rules" ]; then
rm -f /etc/udev/rules.d/70-persistent-net.rules
fi
## End post_install_network_config generated code