Modified init scripts for inclusion in Fedora
[ovirt-node/TEMP.git] / scripts / ovirt-config-networking
blob45f712933236438b6c3fadfd154dad7710619a44
1 #!/bin/bash
3 # Iterates over the list of network devices on the node and prompts the user
4 # to configure each.
6 . /etc/init.d/ovirt-functions
8 ME=$(basename "$0")
9 warn() { printf '%s: %s\n' "$ME" "$*" >&2; }
10 die() { warn "$*"; exit 1; }
12 WORKDIR=$(mktemp -d) || exit 1
14 # Remove $WORKDIR upon interrupt (and HUP, PIPE, TERM) and upon normal
15 # termination, being careful not to change the exit status.
16 trap '__st=$?; rm -rf "$WORKDIR"; stop_log; exit $__st' 0
17 trap 'exit $?' 1 2 13 15
19 IFCONFIG_FILE_ROOT="/files/etc/sysconfig/network-scripts/ifcfg"
20 NTPCONF_FILE_ROOT="/files/etc/ntp"
21 NTP_CONFIG_FILE="/etc/ntp.conf"
22 NTPSERVERS=""
23 CONFIGURED_NIC=""
24 VLAN_ID=""
25 VL_ROOT=""
27 # if local storage is not configured, then exit the script
28 if ! is_local_storage_configured; then
29 printf "Local storage must be configured prior to configuring the management interface.\n\n"
30 exit 99
33 # Checks that a network interface was already configured.
34 function has_configured_interface
36 local show_message=${1-false}
38 if [[ -n "${CONFIGURED_NIC}" ]]; then
39 return 0
40 else
41 if $show_message; then
42 printf "\nYou must configure a network interface first.\n\n"
44 return 1
48 # Configures vlan for the node.
49 # $1 - the nic
50 # $2 - the network bridge name
51 # $3 - the vlan id
52 # $4 - the VL_ROOT variable
53 # $5 - the VL_CONFIG variable
54 # $6 - the IF_ROOT value
55 # $7 - the vlan config filename variable
56 # $8 - the NIC config filename
57 function setup_vlan
59 local nic=$1
60 local bridge=$2
61 local vlan_id=$3
62 local vlroot=$4
63 local vlconfig=$5
64 local ifroot=$6
65 local vlfilename=$7
66 local iffilename=$8
68 eval $vlroot="${ifroot}.${vlan_id}"
69 eval $vlconfig=\"rm \$${vlroot}\\nset \$${vlroot}/DEVICE ${nic}.${vlan_id}\"
70 eval $vlconfig=\"\$${vlconfig}\\nset \$${vlroot}/BRIDGE ${bridge}\"
71 eval $vlconfig=\"\$${vlconfig}\\nset \$${vlroot}/VLAN yes\"
72 eval $vlfilename="${iffilename}.${vlan_id}"
75 function configure_interface
77 local NIC=$1
78 local AUTO=$2
79 if [[ "$AUTO" == "AUTO" &&
80 -n "$OVIRT_IP_ADDRESS" ]]; then
81 IPADDR=$OVIRT_IP_ADDRESS
82 NETMASK=$OVIRT_IP_NETMASK
83 GATEWAY=$OVIRT_IP_GATEWAY
84 PREFIX=$OVIRT_IP_PREFIX
87 if has_configured_interface; then
88 printf "This will delete the current configuration for ${CONFIGURED_NIC}.\n"
89 if ask_yes_or_no "Do you wish to continue ([Y]es/[N]o)?"; then
90 printf "\nDeleting existing network configuration...\n"
91 cp -a /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/backup.lo
92 remove_config /etc/sysconfig/network-scripts/ifcfg-*
93 rm -rf /etc/sysconfig/network-scripts/ifcfg-*
94 cp -a /etc/sysconfig/network-scripts/backup.lo /etc/sysconfig/network-scripts/ifcfg-lo
95 else
96 printf "\nAborting...\n"
97 return
101 rm -rf $WORKDIR/*
102 CONFIGURED_NIC=$NIC
104 local BRIDGE=br$NIC
105 local IF_FILENAME="$WORKDIR/augtool-$NIC"
106 local BR_FILENAME="$WORKDIR/augtool-$BRIDGE"
108 printf "\nConfigure $BRIDGE for use by $NIC..\n\n"
110 local IF_ROOT="$IFCONFIG_FILE_ROOT-$NIC"
111 local IF_CONFIG="rm $IF_ROOT\nset $IF_ROOT/DEVICE $NIC"
113 local BR_ROOT="$IFCONFIG_FILE_ROOT-$BRIDGE"
114 local BR_CONFIG="rm $BR_ROOT\nset $BR_ROOT/DEVICE $BRIDGE"
116 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/TYPE Bridge"
117 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/PEERNTP yes"
118 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/DELAY 0"
119 local BR_CONFIG_BASE=$BR_CONFIG
121 if [ -z "$AUTO" ]; then
122 while true; do
123 local VL_CONFIG=""
125 printf "\n"
126 LINK=`ethtool $NIC| grep "Link detected"`:u
127 [ -z "$LINK" ] && return
128 if echo $LINK | grep -q "Link detected: yes" ; then
129 NICSTATUS="ACTIVE"
130 else
131 NICSTATUS="INACTIVE"
133 echo "NIC is: $NICSTATUS"
135 if ask_yes_or_no "Help identify ${NIC} by blinking lights for 10 seconds ([Y]es/[N]o)?"; then
136 ethtool --identify $NIC 10
139 ask_yes_or_no "Include VLAN support ([Y]es/[N]o/[A]bort)? " true true
140 case $? in
142 while true; do
143 read -ep "What is the VLAN ID for this device (a=abort) "
144 case $REPLY in
145 A|a) CONFIGURED_NIC=""; return;;
147 if [[ -n "$REPLY" ]] && [[ "$REPLY" =~ "^[0-9]{1,}$" ]]; then
148 VLAN_ID=$REPLY
149 setup_vlan $NIC $BRIDGE $VLAN_ID VL_ROOT VL_CONFIG $IF_ROOT VL_FILENAME $IF_FILENAME
150 break
153 esac
154 done
156 1) IF_CONFIG="${IF_CONFIG}\nset ${IF_ROOT}/BRIDGE ${BRIDGE}" ;;
158 CONFIGURED_NIC=""
159 VLAN_ID=""
160 return;;
161 esac
163 while true; do
164 read -ep "Enable IPv4 support ([S]tatic IP, [D]HCP, [N]o or [A]bort)? "
165 case $REPLY in
166 D|d)
167 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/BOOTPROTO dhcp"
168 break
170 S|s)
171 printf "\n"
172 read -ep "IP Address: "; IPADDR=$REPLY
173 read -ep " Netmask: "; NETMASK=$REPLY
174 read -ep " Gateway: "; GATEWAY=$REPLY
176 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/BOOTPROTO none"
177 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/IPADDR $IPADDR"
178 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/NETMASK $NETMASK"
179 if [ -n "${GATEWAY}" ]; then
180 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/GATEWAY $GATEWAY"
182 break
184 A|a)
185 CONFIGURED_NIC=""
186 VLAN_ID=""
187 return
189 esac
190 done
192 printf "\n"
194 while true; do
195 read -ep "Enable IPv6 support ([S]tatic, [D]HCPv6, A[u]to, [N]o or [A]bort)? "
196 case $REPLY in
197 S|s)
198 read -ep "IPv6 Address: "; IPADDR=$REPLY
199 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/IPV6INIT yes"
200 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/IP6ADDR $IPADDR"
201 break
203 D|d)
204 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/IPV6INIT yes"
205 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/IPV6AUTCONF no"
206 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/IPV6FORWARDING no"
207 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/DHCPV6C yes"
208 break
210 U|u)
211 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/IPV6INIT yes"
212 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/IPV6FORWARDING no"
213 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/IPV6AUTOCONF yes"
214 break
216 A|a)
217 CONFIGURED_NIC=""
218 VLAN_ID=""
219 return
221 esac
222 done
224 printf "\n"
225 ask_yes_or_no "Is this correct ([Y]es/[N]o/[A]bort)?" true true
226 case $? in
228 IF_CONFIG="$IF_CONFIG\nset $IF_ROOT/ONBOOT yes"
229 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/ONBOOT yes"
230 if [[ -n "$VLAN_ID" ]]; then
231 VL_CONFIG="$VL_CONFIG\nset $VL_ROOT/ONBOOT yes"
233 printf "$IF_CONFIG\n" > $IF_FILENAME
234 printf "$BR_CONFIG\n" > $BR_FILENAME
235 if [[ -n "$VLAN_ID" ]]; then
236 printf "$VL_CONFIG\n" > $VL_FILENAME
238 break
241 BR_CONFIG=$BR_CONFIG_BASE
244 CONFIGURED_NIC=""
245 VLAN_ID=""
246 return
248 esac
249 done
250 else
251 if [ -n "$OVIRT_IPV6" ]; then
252 case "$OVIRT_IPV6" in
253 "auto")
254 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/IPV6INIT yes"
255 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/IPV6FORWARDING no"
256 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/IPV6_AUTOCONF yes"
258 "dhcp")
259 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/IPV6INIT yes"
260 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/IPV6_AUTOCONF no"
261 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/IPV6FORWARDING no"
262 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/DHCPV6C yes"
265 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/IPV6INIT yes"
266 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/IPV6ADDR $OVIRT_IPV6"
267 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/IPV6_AUTOCONF no"
268 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/IPV6FORWARDING no"
269 esac
272 if [ -n "$OVIRT_VLAN" ]; then
273 VLAN_ID=$OVIRT_VLAN
274 setup_vlan $NIC $BRIDGE $VLAN_ID VL_ROOT VL_CONFIG $IF_ROOT VL_FILENAME $IF_FILENAME
277 if [ -z "$OVIRT_IP_ADDRESS" ]; then
278 if [ -z "$VL_CONFIG" ]; then
279 IF_CONFIG="${IF_CONFIG}\nset ${IF_ROOT}/BRIDGE ${BRIDGE}"
281 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/BOOTPROTO dhcp"
282 else
283 if [ "$OVIRT_IP_ADDRESS" != "off" ]; then
284 if [ -z "$VL_CONFIG" ]; then
285 IF_CONFIG="${IF_CONFIG}\nset ${IF_ROOT}/BRIDGE ${BRIDGE}"
287 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/IPADDR $OVIRT_IP_ADDRESS"
288 if [ -n "$OVIRT_IP_NETMASK" ]; then
289 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/NETMASK $OVIRT_IP_NETMASK"
291 if [ -n "$OVIRT_IP_GATEWAY" ]; then
292 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/GATEWAY $OVIRT_IP_GATEWAY"
297 IF_CONFIG="$IF_CONFIG\nset $IF_ROOT/ONBOOT yes"
298 BR_CONFIG="$BR_CONFIG\nset $BR_ROOT/ONBOOT yes"
299 if [ -n "${VL_CONFIG}" ]; then
300 VL_CONFIG="$VL_CONFIG\nset $VL_ROOT/ONBOOT yes"
303 printf "$IF_CONFIG\n" > $IF_FILENAME
304 printf "$BR_CONFIG\n" > $BR_FILENAME
305 if [ -n "$VL_CONFIG" ]; then
306 printf "$VL_CONFIG\n" > $VL_FILENAME
311 function configure_dns
313 local DNS=$1
314 local AUTO=$2
315 if [[ "$AUTO" == "AUTO" &&
316 -n "$OVIRT_DNS" ]]; then
317 DNS=$OVIRT_DNS
320 local IF_FILENAME="$WORKDIR/augtool-br${CONFIGURED_NIC}"
321 local IF_ROOT="$IFCONFIG_FILE_ROOT-br${CONFIGURED_NIC}"
322 local IF_CONFIG=
324 if [ -z "$AUTO" ]; then
325 if has_configured_interface true; then
326 while true; do
327 for dns in first second; do
328 while true; do
329 printf "\n"
330 read -ep "Please enter the ${dns} DNS server (or ENTER to exit): "
331 if [[ -z "${REPLY}" ]]; then
332 if [[ -z "${DNS}" ]]; then
333 printf "\nAborted...\n"
334 return
335 else
336 break
339 if is_valid_ipv4 $REPLY; then
340 if [[ -z "${DNS}" ]]; then
341 DNS="${REPLY}"
342 elif [[ -n "${REPLY}" ]]; then
343 DNS="${DNS}:${REPLY}"
345 break
346 else
347 printf "${REPLY} is an invalid address.\n"
349 done
350 done
352 printf "\n"
353 ask_yes_or_no "Is this correct ([Y]es/[N]o/[A]bort)?" true true
354 case $? in
355 0) break ;;
356 1) DNS="";;
357 2) return ;;
358 esac
359 done
363 if [ -n "$DNS" ]; then
364 DNS1=$(echo "$DNS" | awk -F\: '{print $1}')
365 DNS2=$(echo "$DNS" | awk -F\: '{print $2}')
367 test -n "$DNS1" && printf "set $IF_ROOT/DNS1 $DNS1\n" >> $IF_FILENAME
368 test -n "$DNS2" && printf "set $IF_ROOT/DNS2 $DNS2\n" >> $IF_FILENAME
372 function configure_ntp
374 local AUTO=$2
375 if [[ "$AUTO" == "AUTO" && -n "$OVIRT_NTP" ]]; then
376 NTPSERVERS=$OVIRT_NTP
377 else
378 NTPSERVERS=""
381 if [ -z "$AUTO" ]; then
382 if has_configured_interface true; then
383 while true; do
384 read -ep "Enter an NTP server (hit return when finished): "
386 if [ -z "$REPLY" ]; then break; fi
388 if is_valid_ipv4 $REPLY; then
389 NTPSERVERS="${NTPSERVERS}:${REPLY}"
390 else
391 printf "${REPLY} is an invalid address.\n"
393 done
398 function save_ntp_configuration
400 local ntpconf="$WORKDIR/augtool-ntp"
401 local ntproot="/files/etc/ntp.conf"
403 printf "\
404 rm ${ntproot}\n\n
405 set ${ntproot}/driftfile /var/lib/ntp/drift\n\
406 set ${ntproot}/includefile /etc/ntp/crypto/pw\n\
407 set ${ntproot}/keys /etc/ntp/keys\n\
408 " > $ntpconf
410 if [ -n "$NTPSERVERS" ]; then
411 offset=1
412 SERVERS=$(echo $NTPSERVERS | awk 'BEGIN{FS=":"}{for (i=1; i<=NF; i++) print $i}')
413 for server in $SERVERS; do
414 printf "set /files/etc/ntp.conf/server[${offset}] ${server}\n" >> $ntpconf
415 offset=$(echo "$offset+1" | bc)
416 done
420 function setup_menu
422 NICS=""
423 udi_list=$(hal-find-by-capability --capability net.80203)
424 if [ -n "$udi_list" ]; then
425 for d in $udi_list; do
426 if [[ ! "$(hal-get-property --udi $d --key net.physical_device)" =~ computer ]]; then
427 NICS="$NICS $(hal-get-property --udi "$d" --key net.interface)"
429 done
432 # Add virtio NICs that were possibly not detected by hal
433 NICS="$NICS $(ifconfig -a | awk '/Ethernet/ {print $1}' | grep -v -E "^br|^bond|^vnet|^virbr|\." | xargs)"
434 NICS=$(echo $NICS | tr ' ' '\n' | sort -u | xargs)
436 PS3="Please select an interface or configuration option: "
439 if [ "$1" == "AUTO" ]; then
440 if [ -n "$OVIRT_BOOTIF" ]; then
441 configure_interface "$OVIRT_BOOTIF" AUTO
442 configure_dns "$OVIRT_DNS" AUTO
443 configure_ntp "$OVIRT_NTP" AUTO
444 else
445 printf "No network interface specified. Unable to configure networking."
446 exit 0
448 else
449 setup_menu
451 while true; do
452 printf "\n\nManagement Network Configuration\n\n" >&2
453 printf "***** WARNING *****\n"
454 printf "Configuring the network will destroy any existing networking\n"
455 printf "configuration on this system.\n"
456 printf "***** WARNING *****\n"
457 printf "\nPhysical Networking Devices (*=PXE boot interface)\n"
458 printf " %-10s %-12s %-18s\n" "Name" "Driver" "MAC"
459 for nic in $NICS; do
460 driver=$(basename $(readlink /sys/class/net/$nic/device/driver) 2>/dev/null)
461 mac=$(cat /sys/class/net/$nic/address)
462 if [ "$nic" = "$OVIRT_BOOTIF" ]; then
463 pxe="*"
464 else
465 pxe=" "
467 printf "%s%-10s %-12s %-18s\n" "$pxe" "$nic" "$driver" "$mac"
468 done
470 DNS="DNS"
471 NTP="NTP"
472 Abort="Abort"
473 Save="Save And Return To Menu"
474 select NIC in $NICS "$DNS" "$NTP" "$Abort" "$Save"
476 printf "\n"
477 case "$NIC" in
478 "$DNS") configure_dns "$OVIRT_DNS"; break ;;
479 "$NTP") configure_ntp "$OVIRT_NTP"; break ;;
480 "$Abort") rm -f "${WORKDIR}"/augtool-*; exit 99;;
481 "$Save")
482 save_ntp_configuration
483 break 2;;
485 if [[ -n "${NIC}" ]] && [[ "${NICS}" =~ "${NIC}" ]]; then
486 configure_interface $NIC $IFACE_NUMBER
487 else
488 printf "\nInvalid option selected.\n"
490 break
492 esac
493 done
494 done
497 start_log
498 net_configured=0
499 if ls "$WORKDIR"/augtool-* > /dev/null 2>&1 ; then
500 log "Configuring network"
502 # delete existing scripts
503 for script in /etc/sysconfig/network-scripts/ifcfg-*; do
504 if [[ ! "${script}" =~ ifcfg-lo ]]; then
505 ovirt_safe_delete_config $script
507 done
509 config="$WORKDIR"/config-augtool
510 cat "$WORKDIR"/augtool-* > $config \
511 && augtool $config
512 if [ $? = 0 ]; then
513 log "Network configured successfully"
514 net_configured=1
515 ovirt_store_config \
516 /etc/sysconfig/network-scripts/ifcfg* \
517 $NTP_CONFIG_FILE
518 else
519 log "Error configuring network, see $OVIRT_LOGFILE"
520 stop_log
521 exit 1
524 stop_log
526 if [ "$net_configured" = 1 ]; then
527 service network stop > /dev/null 2>&1
528 for i in `brctl show | grep breth | awk '{print $1}'`
530 ifconfig $i down
531 brctl delbr $i
532 done
533 service network start