ceph-opennebula-playbooks/network.sh

109 lines
3.2 KiB
Bash
Executable File

#!/bin/bash
set -e -x -a
# Run once
if [ -z "$eth10g" -o -z "$eth1g" ]; then
### Check host variables
if [ -z "$play_host" -o -z "$node_name" -o -z "$int_ip" ]; then
echo "play_host/node_name/int_ip not specified"
exit 1
fi
### Configure network
ssh root@$play_host <<EOF
apt-get purge -y network-manager
EOF
### Rename 1G interfaces to eth1g[1,2,3...], 10G to eth10g[1,2,3...]
ssh root@$play_host '
set -e -x
i10g=1
i1g=1
changed=
>tmp$$
for i in /sys/class/net/eth1g* /sys/class/net/eth10g* `ls -d /sys/class/net/* | grep -vP "eth10?g"`; do
if [ -e "$i/device" ]; then
mac=`cat $i/address`
oldname=${i##/sys/class/net/}
newname=eth10g
if ethtool $oldname | grep -q 10000; then
newname=eth10g$i10g
i10g=$((i10g+1))
else
newname=eth1g$i1g
i1g=$((i1g+1))
fi
if [ "$newname" != "$oldname" ]; then
changed=1
ip link set $oldname down
ip link set dev $oldname name $newname
ip link set $newname up
if [ -e /run/network/ifstate.$oldname -a x`cat /run/network/ifstate.$oldname` != "x" ]; then
rm -f /run/network/ifstate.$oldname
echo $newname > /run/network/ifstate.$newname
fi
fi
cat >>tmp$$ <<EOF
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="$mac", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="$newname"
EOF
fi
done
if [ "$changed" = "1" ]; then
cp tmp$$ /etc/udev/rules.d/70-persistent-net.rules
udevadm control --reload-rules
update-initramfs -u -k all
fi
rm -f tmp$$'
### Find connected 1G and 10G network interfaces (10G is used for keepalived and galera)
eth10g=
eth1g=
export $(ssh root@$play_host '
for i in /sys/class/net/*; do
ip link set ${i##/sys/class/net/} up
if [ x`cat $i/carrier 2>/dev/null` = "x1" ]; then
if [ x`cat $i/speed 2>/dev/null` = "x10000" -a "$eth10g" = "" ]; then
eth10g=${i##/sys/class/net/}
echo eth10g=$eth10g
elif [ x`cat $i/speed 2>/dev/null` = "x1000" -a "$eth1g" = "" ]; then
eth1g=${i##/sys/class/net/}
echo eth1g=$eth1g
fi
fi
done')
if [ -z "$eth10g" ]; then
echo "10GbE network not found on $play_host"
exit 1
fi
### Configure network
envsubst < ./etc/rc.local.env | ssh root@$play_host 'cat > /etc/rc.local'
envsubst < ./etc/network/interfaces.env | ssh root@$play_host 'cat > /etc/network/interfaces.new'
ssh root@$play_host <<EOF
set -e -x
DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::="--force-confdef" \
-o Dpkg::Options::="--force-confold" install -y \
bridge-utils resolvconf
chmod 755 /etc/rc.local
/etc/rc.local
systemctl enable rc-local
if ! cmp -s /etc/network/interfaces /etc/network/interfaces.new; then
nmcli dev disconnect $eth1g || true
mv /etc/network/interfaces.new /etc/network/interfaces
ifup br0
service networking restart
fi
if ! (ip a s | grep $int_ip); then
ifdown $eth10g || true
ifup $eth10g
fi
EOF
# -z eth10g / eth1g
fi