From a5e0093671fe53db533dc7a5056e68f7f482cb80 Mon Sep 17 00:00:00 2001 From: alexhimmel Date: Mon, 26 Dec 2022 04:53:24 -0500 Subject: [PATCH] CP-41461: add vm_setup_scripts in ACK repo Signed-off-by: alexhimmel --- vm_setup_scripts/init-run.sh | 22 ++++++++ vm_setup_scripts/startup-ip.service | 9 +++ vm_setup_scripts/static-ip.sh | 88 +++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 vm_setup_scripts/init-run.sh create mode 100644 vm_setup_scripts/startup-ip.service create mode 100644 vm_setup_scripts/static-ip.sh diff --git a/vm_setup_scripts/init-run.sh b/vm_setup_scripts/init-run.sh new file mode 100644 index 0000000..5635c4a --- /dev/null +++ b/vm_setup_scripts/init-run.sh @@ -0,0 +1,22 @@ +#!/bin/bash +# This script is used to setup Driod VM template base on Rocky 8 Linux 8.6 + +# install dependencies for test tools +dnf update -y +dnf install perl -y +dnf --enablerepo=powertools install perl-List-MoreUtils -y +dnf --enablerepo=powertools install perl-Readonly -y + +# setup firewall port for 4/tcp +firewall-cmd --zone=public --add-port=4/tcp --permanent +firewall-cmd --reload + +# setup static-ip service +cp /root/setup-scripts/static-ip.sh /root +chmod 755 /root/static-ip.sh +semanage fcontext -a -t bin_t '/root/static-ip.sh' +restorecon -Fv /root/static-ip.sh +cp /root/setup-scripts/startup-ip.service /lib/systemd/system/ +systemctl enable startup-ip.service +systemctl start startup-ip.service +systemctl status startup-ip.service \ No newline at end of file diff --git a/vm_setup_scripts/startup-ip.service b/vm_setup_scripts/startup-ip.service new file mode 100644 index 0000000..c605e41 --- /dev/null +++ b/vm_setup_scripts/startup-ip.service @@ -0,0 +1,9 @@ +[Unit] +Description=startupscript +Before=network-online.target +Wants=network-online.target +[Service] +ExecStart=/root/static-ip.sh +StandardOutput=journal+console +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/vm_setup_scripts/static-ip.sh b/vm_setup_scripts/static-ip.sh new file mode 100644 index 0000000..d658905 --- /dev/null +++ b/vm_setup_scripts/static-ip.sh @@ -0,0 +1,88 @@ +#!/bin/sh +# static ip Set static IP details for interfaces +# chkconfig: 2345 5 1 +# description: Sets ethernet devices IP configuration statically using XenStore. + +network_file="/etc/sysconfig/network" +get_config() { +device="$1" +key="$2" +xenstore-read "vm-data/control/${device}/${key}" +} + +write_config_file() { +mfile="$1" +key="$2" +val="$3" +echo -e "$key=$val" >> "$mfile" +} + +write_net_config() { +device="$1" +file="/etc/sysconfig/network-scripts/ifcfg-$device" +path="vm-data/control/$device" + +#Backup and Initialise File +cp "$file" "$file.bak" +echo "" > "$file" + +if xenstore-exists "$path/ip"; then + write_config_file "$file" "DEVICE" $device + write_config_file "$file" "BOOTPROTO" "static" + write_config_file "$file" "IPADDR" $(xenstore-read "$path/ip") + + if xenstore-exists "$path/netmask"; then + write_config_file "$file" "NETMASK" $(xenstore-read "$path/netmask") + echo "change ip" + ifconfig $device $(xenstore-read "$path/ip") netmask $(xenstore-read "$path/netmask") + fi + + if xenstore-exists "$path/gateway"; then + write_config_file "$file" "GATEWAY" $(xenstore-read "$path/gateway") + echo "change gw" + route add default gw $(xenstore-read "$path/ip") $device + fi +fi + +if xenstore-exists "$path/ipv6"; then + #Make sure no other 'NETWORKING_IPV6' variable has been set in file + sed -i "/NETWORKING_IPV6/d" "/etc/sysconfig/network" + write_config_file "/etc/sysconfig/network" "NETWORKING_IPV6" "yes" + write_config_file "$file" "IPV6INIT" "yes" + write_config_file "$file" "DEVICE" "$device" + + items=$(xenstore-list "$path/ipv6") + secaddrs="" + for key in $items; do + if xenstore-exists "$path/ipv6/$key"; then + if [[ "$key" == "0" ]]; then + val=$(xenstore-read "$path/ipv6/$key/addr") + write_config_file "$file" "IPV6ADDR" "$val" + elif [[ "$key" == "gateway" ]]; then + write_config_file "$file" "IPV6_DEFAULTGW" $(xenstore-read "$path/ipv6/$key") + else + val=$(xenstore-read "$path/ipv6/$key/addr") + if [[ "$secaddrs" == "" ]]; then + secaddrs="$val" + else + secaddrs=`echo -e "${secaddrs}\n${val}"` + fi + fi + fi + done + if [[ "$secaddrs" != "" ]]; then + write_config_file "$file" "IPV6ADDR_SECONDARIES" "$secaddrs" + fi + fi +} + +keys=$(xenstore-list vm-data/control) +for item in $keys +do + expr_match=$(expr $item : 'eth[0-9]\+$') + if [ $expr_match -gt 0 ]; then + write_net_config "$item" + fi +done + +exit 0 \ No newline at end of file