From e6ed55f7593a0f4bc2700bc7b90903bb012e27f6 Mon Sep 17 00:00:00 2001 From: Tom Grennan Date: Thu, 13 Dec 2007 18:35:20 -0800 Subject: process device persistence through udev rules rather than init script --- scripts/vyatta_net_name | 111 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 scripts/vyatta_net_name (limited to 'scripts/vyatta_net_name') diff --git a/scripts/vyatta_net_name b/scripts/vyatta_net_name new file mode 100644 index 00000000..d93dd288 --- /dev/null +++ b/scripts/vyatta_net_name @@ -0,0 +1,111 @@ +#!/bin/bash +# **** License **** +# Version: VPL 1.0 +# +# The contents of this file are subject to the Vyatta Public License +# Version 1.0 ("License"); you may not use this file except in +# compliance with the License. You may obtain a copy of the License at +# http://www.vyatta.com/vpl +# +# Software distributed under the License is distributed on an "AS IS" +# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +# the License for the specific language governing rights and limitations +# under the License. +# +# This code was originally developed by Vyatta, Inc. +# Portions created by Vyatta are Copyright (C) 2007 Vyatta, Inc. +# All Rights Reserved. +# +# Author: Tom Grennan +# Description: search Vyatta config for interface name given address +# +# **** End License **** + +debug='' +attr_address=0:0:0:0:0:0 + +test -r /etc/default/vyatta && source /etc/default/vyatta + +# process command line variable overrides + +for arg ; do + case "$arg" in + --debug ) + debug=echo + ;; + --*=* ) + arg=${arg#--} + eval ${arg%=*}=\"${arg#*=}\" + ;; + *=* ) + eval ${arg%=*}=\"${arg#*=}\" + ;; + *:*:*:*:*:* ) + attr_address=$arg + ;; + * ) + kname=$arg + ;; + esac +done + +: ${vyatta_prefix:=/opt/vyatta} +: ${vyatta_sysconfdir:=${vyatta_prefix}/etc} +: ${BOOTFILE:=${vyatta_sysconfdir:-/opt/vyatta/etc}/config/config.boot} + +shopt -s extglob nullglob + +# load cfg_eth_hwid array from config file as follows +# interface { +# ... +# ethernet eth# { +# ... +# hw-id: XX:XX:XX:XX:XX:XX +# ... +# } +# } +# +# cfg_eth_hwid=( "eth#=xx:xx:xx:xx:xx:xx" ... ) + +declare -a cfg_net_hwid=( $( sed -ne ' + /^interfaces {/,/^}/ { + /^ *ethernet eth[0-9]* {/,/^ $/ { + /^ *ethernet/ { + s/.* eth\([0-9]\+\) {$/ eth\1=/ +# hold interface name + h + } + /^.*hw-id:/ { +# translate field name + s/.*hw-id: *// +# tolower hex mac address + y/ABCDEF/abcdef/ +# exchange hold and pattern space + x +# concatenate hold and pattern + G + s/\n//p + } + } + }' $BOOTFILE )) + +for name_hwid in ${cfg_net_hwid[@]} ; do + name=${name_hwid%=*} + hwid=${name_hwid#*=} + if [ "$hwid" == "$attr_address" ] ; then + echo $name + exit 0 + fi +done + +if [ -n "$kname" ] ; then + echo $kname + exit 0 +fi + +exit 1 + +# Local Variables: +# mode: shell-script +# sh-indentation: 4 +# End: -- cgit v1.2.3 From b484ad3193f65a790e6f534902252c98b7217884 Mon Sep 17 00:00:00 2001 From: Tom Grennan Date: Fri, 14 Dec 2007 13:10:21 -0800 Subject: test for config file missing interface or interface hwid --- scripts/vyatta_net_name | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'scripts/vyatta_net_name') diff --git a/scripts/vyatta_net_name b/scripts/vyatta_net_name index d93dd288..56d90f14 100644 --- a/scripts/vyatta_net_name +++ b/scripts/vyatta_net_name @@ -21,7 +21,8 @@ # # **** End License **** -debug='' +debug= +match= attr_address=0:0:0:0:0:0 test -r /etc/default/vyatta && source /etc/default/vyatta @@ -96,11 +97,25 @@ for name_hwid in ${cfg_net_hwid[@]} ; do echo $name exit 0 fi + [ "$name" == "$kname" ] && match=$name_hwid done +# have not found matching hwid in config, see if we can use kernel name if [ -n "$kname" ] ; then - echo $kname - exit 0 + # if interface missing entirely from config + # or it does not yet have a recorded hwid + # it is safe to use kernel name + if [[ -z "$match" || -z "${match#*=}" ]] ; then + echo $kname + exit 0 + fi + # FIXME! need to figure out what to do here... + + # The device mac address isn't yet in the config but the config + # has another hwid associated with the kernel name. This + # indicates that the device may have been replaced or a new device + # installed. Udev may not have scanned all devices yet so we + # donot know which case. fi exit 1 -- cgit v1.2.3 From 8f9c81cd448ece7199aa6f97fc27338839cabfd1 Mon Sep 17 00:00:00 2001 From: Tom Grennan Date: Fri, 14 Dec 2007 18:15:03 -0800 Subject: move config file interface mods to udev handler --- scripts/rl-system.init | 76 ------------------------------------------------- scripts/vyatta_net_name | 58 ++++++++++++++++++++++++++----------- 2 files changed, 41 insertions(+), 93 deletions(-) (limited to 'scripts/vyatta_net_name') diff --git a/scripts/rl-system.init b/scripts/rl-system.init index dbcc1526..96f648c1 100755 --- a/scripts/rl-system.init +++ b/scripts/rl-system.init @@ -38,85 +38,11 @@ export PATH . /lib/lsb/init-functions -IPROUTE2IP=ip -INIT_PID=$$ - ## BOOTFILE is provided by ofr.init : ${BOOTFILE:=$prefix/etc/config/config.boot} shopt -s extglob nullglob -# load cfg_eth_hwid array from config file as follows -# interface { -# ... -# ethernet eth# { -# ... -# hw-id: XX:XX:XX:XX:XX:XX -# ... -# } -# } -# -# cfg_eth_hwid=( "eth#=xx:xx:xx:xx:xx:xx" ... ) - -declare -a cfg_net_hwid=( $( sed -ne ' - /^interfaces {/,/^}/ { - /^ *ethernet eth[0-9]* {/,/^ $/ { - /^ *ethernet/ { - s/.* eth\([0-9]\+\) {$/ eth\1=/ -# hold interface name - h - } - /^.*hw-id:/ { -# translate field name - s/.*hw-id: *// -# tolower hex mac address - y/ABCDEF/abcdef/ -# exchange hold and pattern space - x -# concatenate hold and pattern - G - s/\n//p - } - } - }' $BOOTFILE )) - -add_bootfile_eth_hwid () -{ - local bootfile=$1 eth=$2 hwid=$3 - - sed -i '/^interfaces {$/,/^}$/ { - /^}$/i\ - ethernet '"$eth"' {\ - hw-id: '"$hwid"'\ - link-detect\ - } - }' $bootfile -} - -update_bootfile_eths () -{ - for address_file in /sys/class/net/eth*/address ; do - eth_path=${address_file%/address} - eth=${eth_path##*/} - address=`cat $address_file` - match= - for name_hwid in ${cfg_net_hwid[@]} ; do - name=${name_hwid%=*} - hwid=${name_hwid#*=} - if [ $eth == "$name" ] ; then - ${vyatta_sbindir}/mod_bootfile_eth_hwid $BOOTFILE $eth $address - match=$name_hwid - elif [ "$address" == "$hwid" ] ; then - log_failure_msg "kernel $eth != config $name for $hwid" - return 1 - fi - done - if [ -z "$match" ] ; then - ${vyatta_sbindir}/add_bootfile_eth_hwid $BOOTFILE $eth $address - fi - done -} - search_config_if_wan () { grep "\.*\<$1\>" $BOOTFILE >/dev/null } @@ -202,8 +128,6 @@ start () { log_failure_msg "can\'t reset config files" set_reboot_on_panic || \ log_failure_msg "can\'t set reboot on panic" - update_bootfile_eths || \ - log_failure_msg "can\'t update config interfaces" add_new_serial_if || \ log_failure_msg "can\'t add serial interfaces" proc_flags || \ diff --git a/scripts/vyatta_net_name b/scripts/vyatta_net_name index 56d90f14..5cc1f4c5 100644 --- a/scripts/vyatta_net_name +++ b/scripts/vyatta_net_name @@ -24,6 +24,7 @@ debug= match= attr_address=0:0:0:0:0:0 +declare -i ethn=0 last_ethn=0 test -r /etc/default/vyatta && source /etc/default/vyatta @@ -51,8 +52,16 @@ for arg ; do done : ${vyatta_prefix:=/opt/vyatta} +: ${vyatta_sbindir:=${vyatta_prefix}/sbin} : ${vyatta_sysconfdir:=${vyatta_prefix}/etc} : ${BOOTFILE:=${vyatta_sysconfdir:-/opt/vyatta/etc}/config/config.boot} +: ${DEFAULT_BOOTFILE:=${vyatta_sysconfdir:-/opt/vyatta/etc}/config.boot.default} + +if [ ! -f $BOOTFILE ] ; then + cp $DEFAULT_BOOTFILE $BOOTFILE + chgrp quaggavty $BOOTFILE + chmod 660 $BOOTFILE +fi shopt -s extglob nullglob @@ -93,32 +102,47 @@ declare -a cfg_net_hwid=( $( sed -ne ' for name_hwid in ${cfg_net_hwid[@]} ; do name=${name_hwid%=*} hwid=${name_hwid#*=} + ethn=${name/eth/} + [[ $ethn -gt $last_ethn ]] && \ + last_ethn=$ethn if [ "$hwid" == "$attr_address" ] ; then + # we mod the config file interface sub-clock in case it is missing + # "link-detect" + ${vyatta_sbindir}/mod_bootfile_eth_hwid $BOOTFILE $name $attr_address echo $name exit 0 fi - [ "$name" == "$kname" ] && match=$name_hwid + [ "$name" == "$kname" ] && \ + match=$name_hwid done -# have not found matching hwid in config, see if we can use kernel name -if [ -n "$kname" ] ; then - # if interface missing entirely from config - # or it does not yet have a recorded hwid - # it is safe to use kernel name - if [[ -z "$match" || -z "${match#*=}" ]] ; then - echo $kname - exit 0 - fi - # FIXME! need to figure out what to do here... +[ -z "$kname" ] && \ + exit 1 + - # The device mac address isn't yet in the config but the config - # has another hwid associated with the kernel name. This - # indicates that the device may have been replaced or a new device - # installed. Udev may not have scanned all devices yet so we - # donot know which case. +# have not found matching hwid in config, see if we can use kernel name +if [ -z "$match" ] ; then + # the kernel interface name isnot in config + # so, we might as well use it + name=$kname + ${vyatta_sbindir}/add_bootfile_eth_hwid $BOOTFILE $name $attr_address +elif [ -z "${match#*=}" ] ; then + # the config has this interface but the sub-block is missing the hwid + # so again, we might as well use the kernel name + name=$kname + ${vyatta_sbindir}/mod_bootfile_eth_hwid $BOOTFILE $name $attr_address +else + # The device mac address is not in the config but the config + # has another hwid associated with the device name. This + # indicates that the device is either a replacement or new but + # detected earlier than the device configured with this name. + # Since this is non-deterministic, we make a new name. + (( ethn = last_ethn + 1 )) + name=eth$ethn + ${vyatta_sbindir}/add_bootfile_eth_hwid $BOOTFILE $name $attr_address fi -exit 1 +echo $name # Local Variables: # mode: shell-script -- cgit v1.2.3 From e21cc4704648b85f8eae737b1ca6194413231f72 Mon Sep 17 00:00:00 2001 From: Tom Grennan Date: Mon, 17 Dec 2007 17:14:11 -0800 Subject: support stand-alone validation --- scripts/vyatta_net_name | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) mode change 100644 => 100755 scripts/vyatta_net_name (limited to 'scripts/vyatta_net_name') diff --git a/scripts/vyatta_net_name b/scripts/vyatta_net_name old mode 100644 new mode 100755 index 5cc1f4c5..43c71c1d --- a/scripts/vyatta_net_name +++ b/scripts/vyatta_net_name @@ -108,7 +108,8 @@ for name_hwid in ${cfg_net_hwid[@]} ; do if [ "$hwid" == "$attr_address" ] ; then # we mod the config file interface sub-clock in case it is missing # "link-detect" - ${vyatta_sbindir}/mod_bootfile_eth_hwid $BOOTFILE $name $attr_address + [[ "$BOOTFILE" != *test_* ]] && \ + ${vyatta_sbindir}/mod_bootfile_eth_hwid $BOOTFILE $name $attr_address echo $name exit 0 fi @@ -119,18 +120,17 @@ done [ -z "$kname" ] && \ exit 1 - # have not found matching hwid in config, see if we can use kernel name if [ -z "$match" ] ; then # the kernel interface name isnot in config # so, we might as well use it name=$kname - ${vyatta_sbindir}/add_bootfile_eth_hwid $BOOTFILE $name $attr_address + cmd=add elif [ -z "${match#*=}" ] ; then # the config has this interface but the sub-block is missing the hwid # so again, we might as well use the kernel name name=$kname - ${vyatta_sbindir}/mod_bootfile_eth_hwid $BOOTFILE $name $attr_address + cmd=mod else # The device mac address is not in the config but the config # has another hwid associated with the device name. This @@ -139,9 +139,12 @@ else # Since this is non-deterministic, we make a new name. (( ethn = last_ethn + 1 )) name=eth$ethn - ${vyatta_sbindir}/add_bootfile_eth_hwid $BOOTFILE $name $attr_address + cmd=add fi +[[ "$BOOTFILE" != *test_* ]] && \ + ${vyatta_sbindir}/${cmd}_bootfile_eth_hwid $BOOTFILE $name $attr_address + echo $name # Local Variables: -- cgit v1.2.3