fuel-main/bootstrap/sync/usr/bin/setup-bootdev

156 lines
3.7 KiB
Bash
Executable File

#!/bin/bash
set -e
service_try_count=15
service_sleep_time=1
while [ 0 -lt ${service_try_count} ]; do
_stat=$(systemctl show systemd-udev-trigger -p SubState)
if [[ "${_stat##*=}" != "exited" ]]; then
echo "Waiting for udev exited (${service_try_count})"
sleep ${service_sleep_time}
service_try_count=$(expr ${service_try_count} - 1)
else
echo "Udev is exited, continue to setup boot device"
break
fi
done
source /etc/sysconfig/network-scripts/network-functions
get_bootdev() {
local bootaddr
local ethdevice_timeout
local wait4dev_interval=5
local max_attempts
local attempt=0
ethdevice_timeout=$(sed -rne 's/^.*\s*\<ethdevice_timeout=([0-9]+)\s*.*$/\1/p' /proc/cmdline)
ethdevice_timeout="${ethdevice_timeout:-120}"
max_attempts=$((ethdevice_timeout/wait4dev_interval))
if [ $max_attempts -lt 2 ]; then
max_attempts=2
fi
bootaddr=$(ruby -e \
'File.open("/proc/cmdline") { |fo|
l = fo.read
m = l.match( /BOOTIF=([0-9A-Fa-f]{2}(-[0-9A-Fa-f]{2})+)/ )
print m[1].split("-")[-6..-1].join(":")
}' 2> /dev/null)
if [ -z "$bootaddr" ]; then
return 1
fi
while [ $attempt -lt $max_attempts ]; do
dev=$(get_device_by_hwaddr $bootaddr)
if [ -n "$dev" ]; then
break
else
attempt=$((attempt+1))
echo "Failed to find boot NIC (mac $bootaddr), retrying in $wait4dev_interval sec." >&2
sleep $wait4dev_interval
continue
fi
done
if [ -z "$dev" ]; then
return 2
fi
echo $dev
}
get_all_devices() {
# Take all not virtual network devices given by links (to omit files)
# Choose ethernet, but exclude wireless, bond, vlan, loopback, tunnels ...
for DEV in /sys/class/net/* ; do
# Take only links into account, skip files
if [ ! -L $DEV ]; then
continue
fi
DEVPATH=$(readlink -f $DEV)
# Drop virtual devices like loopback, tunnels, bonding, vlans ...
case $DEVPATH in
*/virtual/*)
continue
;;
esac
IF=${DEVPATH##*/}
# Check ethernet only
case "$(cat $DEV/type)" in
1)
# TYPE=1 is ethernet, may also be wireless
# Virtual (lo, bound, vlan, tunnel ...) have been skipped before
if [ -d $DEV/wireless -o -L $DEV/phy80211 ]; then
continue
else
# Catch ethernet non-virtual device
echo $IF
fi
;;
*)
continue
;;
esac
done
}
set_interfaces_up_when_booted() {
for name in $(get_all_devices); do
cat > "./ifcfg-${name}" <<EOF
DEVICE=$name
BOOTPROTO=none
ONBOOT=yes
LINKDELAY=30
EOF
done
}
cd /etc/sysconfig/network-scripts/
echo -n "Waiting for network hardware to initialize..."
udevadm trigger --subsystem-match=net
udevadm settle
echo -n "Configure all interfaces as active..."
set_interfaces_up_when_booted
echo "ok."
echo -n "Obtaining boot interface name..."
dev=$(get_bootdev)
rc=$?
if [ $rc -ne 0 ]; then
echo "failed."
echo -en "Obtaining all eth interfaces..."
dev=$(get_all_devices)
if [ -z "$dev" ]; then
rc=1
echo "failed."
else
rc=0
dev_str=$(echo "$dev"|tr "\n" " "|sed 's/ *$//')
echo "ok ($dev_str)."
fi
else
echo "ok ($dev)."
fi
if [ $rc -eq 0 ]; then
for name in $dev; do
# sometimes shell is very picky about tabs and spaces
# tabs used here to make here-document work
cat > "./ifcfg-${name}" <<EOF
DEVICE=$name
BOOTPROTO=dhcp
ONBOOT=yes
LINKDELAY=30
PERSISTENT_DHCLIENT=yes
EOF
done
fi
exit $rc