#!/bin/sh -e
# Task Description:
# Used for initializing configuration in single NIC EIP full Bypass mode scenarios.
# Supports from 1 public IP up to 254 public IPs.
# Execute this task after external resource environment is ready.
# Adapted for ubuntu20.04, 22.04, 24.04, 26.04, debian10, 11, 12, centos7, centos9, rocky9.6, rocky8.10 (11 systems)
# ===== Global Variable Definitions =====
TASK_NAME=single_nic_bypass_init
META_CURL="curl -sf"
META_URL="http://169.254.169.254/latest/meta-data/network/interfaces/macs"
NIC_NAME=eth0
PRI_GW4=
PRI_IP4=
PRI_PREFIX=
SYSTEM=
DEFAULT_PUB_EIP4=
ALL_PUB_EIP4s=
NAME_SERVER1=8.8.8.8
NAME_SERVER2=1.1.1.1
# =======================
CheckSupportedOS() {
if [ ! -f /etc/os-release ]; then
echo "Error: Unsupported system: /etc/os-release not found"
exit 1
fi
. /etc/os-release
if [ "$ID" = "ubuntu" ]; then
if [ "$VERSION_ID" = "20.04" ] || [ "$VERSION_ID" = "22.04" ] || [ "$VERSION_ID" = "24.04" ] || [ "$VERSION_ID" = "26.04" ]; then
echo "Supported OS: Ubuntu $VERSION_ID"
SYSTEM=ubuntu
return 0
else
echo "Error: Unsupported Ubuntu version: $VERSION_ID"
exit 1
fi
elif [ "$ID" = "debian" ]; then
if [ "$VERSION_ID" = "11" ] || [ "$VERSION_ID" = "10" ] || [ "$VERSION_ID" = "12" ]; then
echo "Supported OS: Debian $VERSION_ID"
SYSTEM=debian
return 0
else
echo "Unsupported Debian version: $VERSION_ID"
exit 1
fi
elif [ "$ID" = "centos" ] || [ "$ID" = "rhel" ]; then
if [ "$VERSION_ID" = "7" ] && [ -f /etc/centos-release ]; then
echo "Supported OS: CentOS $VERSION_ID"
SYSTEM=centos
return 0
elif [ "$VERSION_ID" = "9" ] && [ -f /etc/centos-release ]; then
echo "Supported OS: CentOS $VERSION_ID"
SYSTEM=centos
return 0
else
echo "Unsupported CentOS/RHEL version: $VERSION_ID"
exit 1
fi
elif [ "$ID" = "rocky" ]; then
if [ "$VERSION_ID" = "9.6" ] || [ "$VERSION_ID" = "8.10" ]; then
echo "Supported OS: rocky $VERSION_ID"
SYSTEM=rocky
return 0
else
echo "Unsupported rocky version: $VERSION_ID"
exit 1
fi
else
echo "Unsupported OS: $ID $VERSION_ID"
exit 1
fi
}
ConfigUbuntuNetwork() {
cat > /etc/netplan/50-cloud-init.yaml << EOF
network:
version: 2
ethernets:
eth0:
addresses:
- ${PRI_IP4}/${PRI_PREFIX}
EOF
for EIP in $ALL_PUB_EIP4s; do
echo " - ${EIP}/32" >> /etc/netplan/50-cloud-init.yaml
done
cat >> /etc/netplan/50-cloud-init.yaml << EOF
nameservers:
addresses:
- ${NAME_SERVER1}
- ${NAME_SERVER2}
EOF
# Default route
cat >> /etc/netplan/50-cloud-init.yaml << EOF
routes:
- to: default
via: ${PRI_GW4}
from: ${DEFAULT_PUB_EIP4}
EOF
}
ConfigCentOSNetwork() {
rm -f /etc/sysconfig/network-scripts/ifcfg-eth0*
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
IPADDR=${PRI_IP4}
PREFIX=${PRI_PREFIX}
EOF
index=0
for EIP in $ALL_PUB_EIP4s; do
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-eth0:$index
DEVICE=eth0:$index
BOOTPROTO=static
ONBOOT=yes
IPADDR=${EIP}
PREFIX=32
EOF
index=$((index+1))
done
# Default route
cat << EOF > /etc/sysconfig/network-scripts/route-eth0
default via ${PRI_GW4} dev eth0 src ${DEFAULT_PUB_EIP4}
EOF
# Disable DHCP on eth2
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-eth2
DEVICE=eth2
BOOTPROTO=none
ONBOOT=yes
EOF
}
ConfigDebianNetwork() {
cat > /etc/network/interfaces.d/50-cloud-init << EOF
auto lo
iface lo inet loopback
dns-nameservers ${NAME_SERVER1} ${NAME_SERVER2}
auto eth0
iface eth0 inet static
address ${PRI_IP4}/${PRI_PREFIX}
auto eth0:0
iface eth0:0 inet static
address ${DEFAULT_PUB_EIP4}/32
up ip route add default via ${PRI_GW4} dev eth0 src ${DEFAULT_PUB_EIP4}
down ip route del default via ${PRI_GW4} dev eth0 src ${DEFAULT_PUB_EIP4}
EOF
index=1
for EIP in $ALL_PUB_EIP4s; do
if [ "$EIP" = "$DEFAULT_PUB_EIP4" ]; then
continue
fi
cat >> /etc/network/interfaces.d/50-cloud-init << EOF
auto eth0:$index
iface eth0:$index inet static
address ${EIP}/32
EOF
index=$((index+1))
done
}
FetchMetaData() {
pri_macs=$($META_CURL $META_URL/ | sed 's/\///g')
nic_count=$(echo "$pri_macs" | wc -w | tr -d ' ')
if [ "$nic_count" -ne 1 ]; then
echo "[ERR] Expected exactly one nic, got $nic_count: $pri_macs" >&2
exit 4
fi
PRI_IP4=$($META_CURL $META_URL/$pri_macs/primary-ip-address)
pri_subnet_cidr=$($META_CURL $META_URL/$pri_macs/subnet-cidr-block)
PRI_PREFIX=$(echo "$pri_subnet_cidr" | cut -d'/' -f2)
PRI_GW4=$($META_CURL $META_URL/$pri_macs/gateway)
DEFAULT_PUB_EIP4=$($META_CURL $META_URL/$pri_macs/public-ipv4)
ALL_PUB_EIP4s=$($META_CURL $META_URL/$pri_macs/public-ipv4s | sed 's/\[//;s/\]//;s/"//g;s/,/ /g')
if [ -z "$pri_macs" ] || [ -z "$PRI_IP4" ] || [ -z "$pri_subnet_cidr" ] || [ -z "$PRI_PREFIX" ] || [ -z "$PRI_GW4" ] || [ -z "$DEFAULT_PUB_EIP4" ] || [ -z "$ALL_PUB_EIP4s" ]; then
echo "[ERR] Missing metadata, check EC2 metadata service." >&2
exit 3
fi
echo " Private MacAddr: $pri_macs"
echo " Private IPv4: $PRI_IP4"
echo " Private Prefix: $PRI_PREFIX"
echo " Private GWv4: $PRI_GW4"
echo " ALl Public IPv4: $ALL_PUB_EIP4s"
echo " Default Public IPv4: $DEFAULT_PUB_EIP4"
}
ConfigNetwork() {
# Disable RPF (Reverse Path Filtering)
if ! grep -q "net.ipv4.conf.all.rp_filter=0" /etc/sysctl.conf; then
echo "net.ipv4.conf.all.rp_filter=0" | tee -a /etc/sysctl.conf > /dev/null
sysctl -p /etc/sysctl.conf
fi
# Disable cloud-init network configuration
echo "network: {config: disabled}" > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
if [ "$SYSTEM" = "ubuntu" ]; then
ConfigUbuntuNetwork
elif [ "$SYSTEM" = "centos" ]; then
ConfigCentOSNetwork
elif [ "$SYSTEM" = "debian" ]; then
if [ "$VERSION_ID" = "11" ] || [ "$VERSION_ID" = "10" ]; then
ConfigDebianNetwork
elif [ "$VERSION_ID" = "12" ]; then
ConfigUbuntuNetwork
else
echo "Unsupported Debian version: $VERSION_ID"
exit 1
fi
elif [ "$SYSTEM" = "rocky" ]; then
ConfigCentOSNetwork
else
echo "Unsupported OS for network config: $SYSTEM"
exit 1
fi
}
echo "1. Check OS..."
CheckSupportedOS
echo
echo "2. FetchMetaData..."
FetchMetaData
echo
echo "3. Configuring networks..."
ConfigNetwork
echo
echo
echo "Done."
sync