-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add netplan config parding sh utility for ubuntu static ip #1026
Changes from all commits
c237860
f7a8cab
88465dd
7bb3e01
8564390
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,17 @@ V2V_MAP_FILE="${V2V_MAP_FILE:-/tmp/macToIP}" | |
NETWORK_SCRIPTS_DIR="${NETWORK_SCRIPTS_DIR:-/etc/sysconfig/network-scripts}" | ||
NETWORK_CONNECTIONS_DIR="${NETWORK_CONNECTIONS_DIR:-/etc/NetworkManager/system-connections}" | ||
UDEV_RULES_FILE="${UDEV_RULES_FILE:-/etc/udev/rules.d/70-persistent-net.rules}" | ||
NETPLAN_DIR="${NETPLAN_DIR:-/}" | ||
|
||
# Dump debug strings into a new file descriptor and redirect it to stdout. | ||
exec 3>&1 | ||
log() { | ||
echo $@ >&3 | ||
} | ||
|
||
# Sanity checks | ||
# ------------- | ||
|
||
# Check if mapping file does not exist | ||
if [ ! -f "$V2V_MAP_FILE" ]; then | ||
log "File $V2V_MAP_FILE does not exist. Exiting." | ||
|
@@ -24,9 +28,12 @@ if [ -f "$UDEV_RULES_FILE" ]; then | |
exit 0 | ||
fi | ||
|
||
# Helper functions | ||
# ---------------- | ||
|
||
# Clean strigs in case they have quates | ||
remove_quotes() { | ||
echo "$1" | tr -d '"' | ||
echo "$1" | tr -d '"' | tr -d "'" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' | ||
} | ||
|
||
# Validate MAC address and IPv4 address and extract them | ||
|
@@ -39,6 +46,9 @@ extract_mac_ip() { | |
fi | ||
} | ||
|
||
# Network infrastructure reading functions | ||
# ---------------------------------------- | ||
|
||
# Create udev rules based on the macToip mapping + ifcfg network scripts | ||
udev_from_ifcfg() { | ||
# Check if the network scripts directory exists | ||
|
@@ -48,29 +58,33 @@ udev_from_ifcfg() { | |
fi | ||
|
||
# Read the mapping file line by line | ||
while IFS= read -r line; do | ||
cat "$V2V_MAP_FILE" | while read line; | ||
do | ||
# Extract S_HW and S_IP | ||
extract_mac_ip "$line" | ||
|
||
# If S_HW and S_IP were not extracted, skip the line | ||
if [ -z "$S_HW" ] || [ -z "$S_IP" ]; then | ||
log "Warning: invalide mac to ip line: $line." | ||
continue | ||
fi | ||
|
||
# Find the matching network script file | ||
IFCFG=$(grep -l "IPADDR=$S_IP" "$NETWORK_SCRIPTS_DIR"/*) | ||
if [ -z "$IFCFG" ]; then | ||
log "Info: no ifcg config file name foud for $S_IP." | ||
continue | ||
fi | ||
|
||
# Source the matching file, if found | ||
DEVICE=$(grep '^DEVICE=' "$IFCFG" | cut -d'=' -f2) | ||
if [ -z "$DEVICE" ]; then | ||
log "Info: no interface name found to $S_IP." | ||
continue | ||
fi | ||
|
||
echo "SUBSYSTEM==\"net\",ACTION==\"add\",ATTR{address}==\"$(remove_quotes "$S_HW")\",NAME=\"$(remove_quotes "$DEVICE")\"" | ||
done < "$V2V_MAP_FILE" | ||
done | ||
} | ||
|
||
# Create udev rules based on the macToip mapping + network manager connections | ||
|
@@ -82,31 +96,90 @@ udev_from_nm() { | |
fi | ||
|
||
# Read the mapping file line by line | ||
while IFS= read -r line; do | ||
cat "$V2V_MAP_FILE" | while read line; | ||
do | ||
# Extract S_HW and S_IP | ||
extract_mac_ip "$line" | ||
|
||
# If S_HW and S_IP were not extracted, skip the line | ||
if [ -z "$S_HW" ] || [ -z "$S_IP" ]; then | ||
log "Warning: invalide mac to ip line: $line." | ||
continue | ||
fi | ||
|
||
# Find the matching NetworkManager connection file | ||
NM_FILE=$(grep -El "address[0-9]*=$S_IP" "$NETWORK_CONNECTIONS_DIR"/*) | ||
if [ -z "$NM_FILE" ]; then | ||
log "Info: no nm config file name foud for $S_IP." | ||
continue | ||
fi | ||
|
||
# Extract the DEVICE (interface name) from the matching file | ||
DEVICE=$(grep '^interface-name=' "$NM_FILE" | cut -d'=' -f2) | ||
if [ -z "$DEVICE" ]; then | ||
log "Info: no interface name found to $S_IP." | ||
continue | ||
fi | ||
|
||
echo "SUBSYSTEM==\"net\",ACTION==\"add\",ATTR{address}==\"$(remove_quotes "$S_HW")\",NAME=\"$(remove_quotes "$DEVICE")\"" | ||
done < "$V2V_MAP_FILE" | ||
done | ||
} | ||
|
||
# Create udev rules based on the macToIP mapping + output from parse_netplan_file | ||
udev_from_netplan() { | ||
# Check if netplan command exist | ||
if ! command -v netplan >/dev/null 2>&1; then | ||
log "Warning: netplan is not installed." | ||
return 0 | ||
fi | ||
|
||
# netplan with root dir | ||
netplan_get() { | ||
netplan get --root-dir "$NETPLAN_DIR" "$@" 2>/dev/null | ||
} | ||
|
||
# Loop over all interface names and treturn the one with target_ip, or null | ||
find_interface_by_ip() { | ||
target_ip="$1" | ||
|
||
# Loop through all interfaces and check for the given IP address | ||
netplan_get ethernets | grep -Eo "^[^[:space:]]+[^:]" | while read IFNAME; do | ||
if netplan_get ethernets."$IFNAME".addresses | grep -q "$target_ip"; then | ||
echo "$IFNAME" | ||
return | ||
fi | ||
done | ||
} | ||
|
||
# Read the mapping file line by line | ||
cat "$V2V_MAP_FILE" | while read line; | ||
do | ||
# Extract S_HW and S_IP from the current line in the mapping file | ||
extract_mac_ip "$line" | ||
|
||
# If S_HW and S_IP were not extracted, skip the line | ||
if [ -z "$S_HW" ] || [ -z "$S_IP" ]; then | ||
log "Warning: invalide mac to ip line: $line." | ||
continue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. worth adding a log message? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added logs for invalid mac to ip lines |
||
fi | ||
|
||
# Search the parsed netplan output for a matching IP address | ||
interface_name=$(find_interface_by_ip "$S_IP") | ||
|
||
# If no interface is found, skip this entry | ||
if [ -z "$interface_name" ]; then | ||
log "Info: no interface name found to $S_IP." | ||
continue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. worth adding a log message? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added logs for device not found |
||
fi | ||
|
||
# Create the udev rule based on the extracted MAC address and interface name | ||
echo "SUBSYSTEM==\"net\",ACTION==\"add\",ATTR{address}==\"$(remove_quotes "$S_HW")\",NAME=\"$(remove_quotes "$interface_name")\"" | ||
done | ||
} | ||
|
||
# Write to udev config | ||
# ---------------------------------------- | ||
|
||
# Checks for duplicate hardware addresses | ||
check_dupe_hws() { | ||
input=$(cat) | ||
|
@@ -128,6 +201,7 @@ main() { | |
{ | ||
udev_from_ifcfg | ||
udev_from_nm | ||
udev_from_netplan | ||
} | check_dupe_hws > "$UDEV_RULES_FILE" 2>/dev/null | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
either we should
return
hereor later we need to check that we only found one ifname
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added return here