Skip to content

Commit

Permalink
Merge pull request #191 from xenserver/develop
Browse files Browse the repository at this point in the history
CP-41461: add vm_setup_scripts in ACK repo
  • Loading branch information
tianxiahcp authored Jan 4, 2023
2 parents 6bedb88 + c6b3c02 commit 57b7333
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
22 changes: 22 additions & 0 deletions vm_setup_scripts/init-run.sh
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions vm_setup_scripts/startup-ip.service
Original file line number Diff line number Diff line change
@@ -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
88 changes: 88 additions & 0 deletions vm_setup_scripts/static-ip.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 57b7333

Please sign in to comment.