Multiple nics support on Ubuntu template

* Multiple nics support on Ubuntu template

* supports allocating IP to the nic when VM is added to another network - no delay
This commit is contained in:
Pearl Dsilva 2025-01-28 15:20:41 -05:00
parent 5e7f86b84f
commit e4e9e470ae
3 changed files with 74 additions and 0 deletions

View File

@ -46,6 +46,8 @@
"scripts/apt_upgrade.sh",
"scripts/configure_networking.sh",
"scripts/configure-cloud-init.sh",
"scripts/setup-interfaces.sh",
"scripts/add-interface-rule.sh",
"scripts/cleanup.sh"
],
"type": "shell"

View File

@ -0,0 +1,25 @@
#!/bin/bash
# File and rule definition
RULE_FILE="/etc/udev/rules.d/90-new-interface.rules"
RULE='ACTION=="add|change|remove", SUBSYSTEM=="net", DRIVERS=="?*", RUN+="/bin/systemctl --no-block start update-netplan.service"'
# Ensure the file exists, or create it
if [[ ! -f $RULE_FILE ]]; then
touch "$RULE_FILE"
echo "Created $RULE_FILE."
fi
# Check if the rule already exists to prevent duplication
if grep -Fxq "$RULE" "$RULE_FILE"; then
echo "Rule already exists in $RULE_FILE."
else
# Add the rule to the file
echo "$RULE" | tee -a "$RULE_FILE" > /dev/null
echo "Rule added to $RULE_FILE."
fi
# Reload udev rules and apply the changes
udevadm control --reload-rules
udevadm trigger
echo "Udev rules reloaded and triggered."

View File

@ -0,0 +1,47 @@
#!/bin/bash
# Create the script in the /opt/bin directory
SCRIPT_PATH="/usr/local/bin/update-netplan.sh"
cat <<'EOF' > $SCRIPT_PATH
#!/bin/bash
echo "New interface detected: $INTERFACE" >> /var/log/interface-events.log
CONFIG_FILE="/etc/netplan/config.yaml"
# Generate a new netplan configuration
echo "network:" > $CONFIG_FILE
echo " ethernets:" >> $CONFIG_FILE
# Loop through all available interfaces
for iface in $(ls /sys/class/net | grep -vE '^lo$'); do
cat <<EOL >> $CONFIG_FILE
$iface:
dhcp4: true
EOL
done
chmod 600 $CONFIG_FILE
netplan apply
EOF
tee /etc/systemd/system/update-netplan.service <<EOF
[Unit]
Description=Update netplan configuration on boot
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/update-netplan.sh
[Install]
WantedBy=multi-user.target
EOF
chmod 600 /etc/netplan/config.yaml
chmod 777 $SCRIPT_PATH
systemctl daemon-reload || true
systemctl enable update-netplan.service || true
systemctl start update-netplan.service || true