This is an automated email from the ASF dual-hosted git repository.

pearl11594 pushed a commit to branch cks-enhancements-part2
in repository https://gitbox.apache.org/repos/asf/cloudstack.git

commit e4e9e470ae6b6329f09d315813a4ef4ab83c53c8
Author: Pearl Dsilva <[email protected]>
AuthorDate: Tue Jan 28 15:20:41 2025 -0500

    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
---
 .../cks/ubuntu/22.04/cks-ubuntu-2204.json          |  2 +
 .../cks/ubuntu/22.04/scripts/add-interface-rule.sh | 25 ++++++++++++
 .../cks/ubuntu/22.04/scripts/setup-interfaces.sh   | 47 ++++++++++++++++++++++
 3 files changed, 74 insertions(+)

diff --git a/tools/appliance/cks/ubuntu/22.04/cks-ubuntu-2204.json 
b/tools/appliance/cks/ubuntu/22.04/cks-ubuntu-2204.json
index c7ee09f0354..edaa11f96ce 100644
--- a/tools/appliance/cks/ubuntu/22.04/cks-ubuntu-2204.json
+++ b/tools/appliance/cks/ubuntu/22.04/cks-ubuntu-2204.json
@@ -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"
diff --git a/tools/appliance/cks/ubuntu/22.04/scripts/add-interface-rule.sh 
b/tools/appliance/cks/ubuntu/22.04/scripts/add-interface-rule.sh
new file mode 100644
index 00000000000..a21d978012e
--- /dev/null
+++ b/tools/appliance/cks/ubuntu/22.04/scripts/add-interface-rule.sh
@@ -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."
\ No newline at end of file
diff --git a/tools/appliance/cks/ubuntu/22.04/scripts/setup-interfaces.sh 
b/tools/appliance/cks/ubuntu/22.04/scripts/setup-interfaces.sh
new file mode 100644
index 00000000000..61983d75dd8
--- /dev/null
+++ b/tools/appliance/cks/ubuntu/22.04/scripts/setup-interfaces.sh
@@ -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
\ No newline at end of file

Reply via email to