forked from tinkerbell/hook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add static network setup: (tinkerbell#251)
## Description <!--- Please describe what this PR is going to change --> For environments where DHCP is not available, I've added a script that will statically configure network interfaces. The IPAM info must be passed in via kernel cmdline parameters and be in the appropriate format. `ipam=<mac-address>:<vlan-id>:<ip-address>:<netmask>:<gateway>:<hostname>:<dns>:<search-domains>:<ntp>` This is probably only useful for the HookOS ISO. For the Tinkerbell stack, Smee handles patching the ISO at runtime to include this `ipam=` parameter. To facilitate this static ip configuration, scripts were placed into the host filesystem at `/etc/init.d`. Files in this location are run at startup by the init system. This makes it possible to just add the scripts as files in the linuxkit yaml file. The `vlan.sh` script was moved to an `init.d` script because it needs to run before the static-network script. The `vlan.sh` script was updated to use posix `/bin/sh` instead of bash because the host only has `/bin/sh`. ## Why is this needed <!--- Link to issue you have raised --> Fixes: # ## How Has This Been Tested? <!--- Please describe in detail how you tested your changes. --> <!--- Include details of your testing environment, and the tests you ran to --> <!--- see how your change affects other areas of the code, etc. --> ## How are existing users impacted? What migration steps/scripts do we need? <!--- Fixes a bug, unblocks installation, removes a component of the stack etc --> <!--- Requires a DB migration script, etc. --> ## Checklist: I have: - [ ] updated the documentation and/or roadmap (if required) - [ ] added unit or e2e tests - [ ] provided instructions on how to upgrade
- Loading branch information
Showing
8 changed files
with
252 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/bin/sh | ||
|
||
# This script is intended to be run on the HookOS/Linuxkit host so it must use /bin/sh. | ||
# No other shells are available on the host. | ||
|
||
# modified from alpine setup-dns | ||
# apk add alpine-conf | ||
|
||
exec 3>&1 4>&2 | ||
trap 'exec 2>&4 1>&3' 0 1 2 3 | ||
exec 1>/var/log/setup-dns.log 2>&1 | ||
|
||
while getopts "d:n:h" opt; do | ||
case $opt in | ||
d) DOMAINNAME="$OPTARG";; | ||
n) NAMESERVERS="$OPTARG";; | ||
esac | ||
done | ||
shift $(($OPTIND - 1)) | ||
|
||
|
||
conf="${ROOT}resolv.conf" | ||
|
||
if [ -f "$conf" ] ; then | ||
domain=$(awk '/^domain/ {print $2}' $conf) | ||
dns=$(awk '/^nameserver/ {printf "%s ",$2}' $conf) | ||
elif fqdn="$(get_fqdn)" && [ -n "$fqdn" ]; then | ||
domain="$fqdn" | ||
fi | ||
|
||
if [ -n "$DOMAINNAME" ]; then | ||
domain="$DOMAINNAME" | ||
fi | ||
|
||
if [ -n "$NAMESERVERS" ] || [ $# -gt 0 ];then | ||
dns="$NAMESERVERS" | ||
fi | ||
|
||
if [ -n "$domain" ]; then | ||
mkdir -p "${conf%/*}" | ||
echo "search $domain" > $conf | ||
fi | ||
|
||
if [ -n "$dns" ] || [ $# -gt 0 ] && [ -f "$conf" ]; then | ||
sed -i -e '/^nameserver/d' $conf | ||
fi | ||
for i in $dns $@; do | ||
mkdir -p "${conf%/*}" | ||
echo "nameserver $i" >> $conf | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
#!/bin/sh | ||
|
||
# This script is intended to be run on the HookOS/Linuxkit host so it must use /bin/sh. | ||
# No other shells are available on the host. | ||
|
||
# this script will statically configure a single network interface based on the ipam= parameter | ||
# passed in the kernel command line. The ipam parameter is a colon separated string with the following fields: | ||
# ipam=<mac-address>:<vlan-id>:<ip-address>:<netmask>:<gateway>:<hostname>:<dns>:<search-domains>:<ntp> | ||
# Example: ipam=de-ad-be-ef-fe-ed::192.168.2.193:255.255.255.0:192.168.2.1:myserver:1.1.1.1,8.8.8.8::132.163.97.1,132.163.96.1 | ||
# the mac address format requires it to be hyphen separated. | ||
|
||
exec 3>&1 4>&2 | ||
trap 'exec 2>&4 1>&3' 0 1 2 3 | ||
exec 1>/var/log/network_config.log 2>&1 | ||
|
||
set -xeuo pipefail | ||
|
||
# Define the location of the interfaces file | ||
INTERFACES_FILE="/var/run/network/interfaces" | ||
|
||
parse_ipam_from_cmdline() { | ||
local cmdline | ||
local ipam_value | ||
|
||
# Read the contents of /proc/cmdline | ||
cmdline=$(cat /proc/cmdline) | ||
|
||
# Use grep to find the ipam= parameter and awk to extract its value | ||
ipam_value=$(echo "$cmdline" | grep -o 'ipam=[^ ]*' | awk -F= '{print $2}') | ||
|
||
# Check if ipam= parameter was found | ||
if [ -n "$ipam_value" ]; then | ||
echo "$ipam_value" | ||
return 0 | ||
else | ||
echo "ipam= parameter not found in /proc/cmdline" >&2 | ||
return 1 | ||
fi | ||
} | ||
|
||
# Function to get interface name from MAC address | ||
# TODO(jacobweinstock): if a vlan id is provided we should match for the vlan interface | ||
get_interface_name() { | ||
local mac=$1 | ||
for interface in /sys/class/net/*; do | ||
if [ -f "$interface/address" ]; then | ||
if [ "$(cat "$interface/address")" == "$mac" ]; then | ||
echo "$(basename "$interface")" | ||
return 0 | ||
fi | ||
fi | ||
done | ||
return 1 | ||
} | ||
|
||
convert_hyphen_to_colon() { | ||
echo "$1" | tr '-' ':' | ||
} | ||
|
||
ipam=$(parse_ipam_from_cmdline) | ||
if [ $? -ne 0 ]; then | ||
echo "Failed to get IPAM value, not statically configuring network" | ||
cat /proc/cmdline | ||
exit 0 | ||
fi | ||
echo "IPAM value: $ipam" | ||
|
||
mkdir -p $(dirname "$INTERFACES_FILE") | ||
|
||
# Parse the IPAM string | ||
IFS=':' read -r mac vlan_id ip netmask gateway hostname dns search_domains ntp <<EOF | ||
${ipam} | ||
EOF | ||
|
||
# Check for required fields | ||
if [ -z "$mac" ] || [ -z "$ip" ] || [ -z "$netmask" ] || [ -z "$dns" ]; then | ||
echo "Error: MAC address, IP address, netmask, and DNS are required." | ||
echo "$ipam" | ||
exit 1 | ||
fi | ||
|
||
# convert Mac address to colon separated format | ||
mac=$(convert_hyphen_to_colon "$mac") | ||
|
||
# convert , (comma) separated values to space separated values | ||
dns=$(echo "$dns" | tr ',' ' ') | ||
search_domains=$(echo "$search_domains" | tr ',' ' ') | ||
ntp=$(echo "$ntp" | tr ',' ' ') | ||
|
||
# Get interface name | ||
interface=$(get_interface_name "$mac") | ||
if [ -z "$interface" ]; then | ||
echo "Error: No interface found with MAC address $mac" | ||
exit 1 | ||
fi | ||
|
||
# Start writing to the interfaces file | ||
{ | ||
echo "# Static Network configuration for $interface" | ||
echo "" | ||
echo "auto $interface" | ||
|
||
if [ -n "$vlan_id" ]; then | ||
echo "iface $interface inet manual" | ||
echo "" | ||
echo "auto $interface.$vlan_id" | ||
echo "iface $interface.$vlan_id inet static" | ||
else | ||
echo "iface $interface inet static" | ||
fi | ||
|
||
echo " address $ip" | ||
echo " netmask $netmask" | ||
|
||
[ -n "$gateway" ] && echo " gateway $gateway" | ||
[ -n "$hostname" ] && echo " hostname $hostname" | ||
|
||
if [ -n "$dns" ]; then | ||
echo " dns-nameserver $dns" | ||
fi | ||
|
||
if [ -n "$search_domains" ]; then | ||
echo " dns-search $search_domains" | ||
fi | ||
|
||
if [ -n "$ntp" ]; then | ||
echo " ntp-servers $ntp" | ||
fi | ||
|
||
} > "$INTERFACES_FILE" | ||
|
||
echo "Network configuration has been written to $INTERFACES_FILE" | ||
|
||
# Run ifup on the interface | ||
ifup -v -a -i "$INTERFACES_FILE" | ||
|
||
# setup DNS | ||
ROOT=/run/resolvconf/ setup-dns -d "$search_domains" "$dns" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.