113 lines
3.6 KiB
Plaintext
113 lines
3.6 KiB
Plaintext
#!/usr/bin/bash
|
|
# Copyright 2015 Mellanox Technologies, Ltd
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
# implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
# Constants
|
|
readonly SCOPE=`basename $0`
|
|
readonly UDEV_FILE='/etc/udev/rules.d/70-persistent-net.rules'
|
|
|
|
# Variables
|
|
STORAGE_PORT='<%=@storage_parent%>'
|
|
ISER_NAME='<%=@iser_interface_name%>'
|
|
PARENT_FIRST_VF="/sys/class/net/$STORAGE_PORT/device/virtfn0"
|
|
|
|
# This functions print logs to /var/log/messages
|
|
function logger_print () {
|
|
priority=$1
|
|
msg=$2
|
|
logger -t $SCOPE "$priority: $msg"
|
|
}
|
|
|
|
DEVICES='/sys/class/net/*/device'
|
|
if [ -e $PARENT_FIRST_VF ];then
|
|
CHILD_BUS=`basename $(readlink /sys/class/net/$STORAGE_PORT/device/virtfn0)`
|
|
logger_print info "Found first probed port of ${STORAGE_PORT} with bus ID: ${CHILD_BUS}."
|
|
else
|
|
PARENT_BUS=`cat /sys/class/net/$STORAGE_PORT/device/uevent | grep -i pci_slot | cut -d = -f 2`
|
|
CHILD_BUS=`echo $PARENT_BUS | sed -e s/0$/1/g`
|
|
PROBED_PORT_NAME="eth_iser_guess"
|
|
logger_print info "Did not find probed ports of ${STORAGE_PORT}, guessing its bus is: ${CHILD_BUS}."
|
|
fi
|
|
STORAGE_PORT_NUMBER=`cat /sys/class/net/$STORAGE_PORT/dev_id`
|
|
|
|
# Find the probe VF port that fits the storage ports number and BUS
|
|
for dev in $DEVICES; do
|
|
# Check for correct bus
|
|
CANDIDATE_BUS=`readlink -nq $dev`;
|
|
if [[ $CANDIDATE_BUS != *$CHILD_BUS* ]]; then
|
|
continue;
|
|
fi
|
|
|
|
# Check for correct dev_id
|
|
CANDIDATE_DIRNAME=`dirname $dev`
|
|
PORT_NUMBER=`cat $CANDIDATE_DIRNAME/dev_id`
|
|
if [ $PORT_NUMBER = $STORAGE_PORT_NUMBER ]; then
|
|
PROBED_DIRNAME=`dirname $dev`
|
|
PROBED_PORT_NAME=`basename $PROBED_DIRNAME`
|
|
fi
|
|
done
|
|
|
|
# Verify that we find the appropriate virtual port
|
|
if [ -z "$PROBED_PORT_NAME" ]; then
|
|
logger_print error "Did not find $STORAGE_PORT_NUMBER probed ports of $STORAGE_PORT, exiting."
|
|
exit 1
|
|
fi
|
|
|
|
# Verify that udev file exists
|
|
if [ ! -r "$UDEV_FILE" ]; then
|
|
logger_print error "Did not find $UDEV_FILE to rename iser port."
|
|
exit 1
|
|
fi
|
|
|
|
# Persistantly rename the matched probed port
|
|
if [ $PROBED_PORT_NAME != $ISER_NAME ]; then
|
|
# Prepare line for udev
|
|
UDEV_LINE="SUBSYSTEM==\"net\", ACTION==\"add\", "
|
|
UDEV_LINE+="ATTR{dev_id}==\"$STORAGE_PORT_NUMBER\", KERNELS==\"$CHILD_BUS\", "
|
|
UDEV_LINE+="ATTR{type}==\"1\", KERNEL==\"eth*\", NAME=\"$ISER_NAME\""
|
|
|
|
# Change/add line in udev file
|
|
grep $PROBED_PORT_NAME $UDEV_FILE > /dev/null 2>&1
|
|
if [ $? -eq 0 ]; then
|
|
OLD_LINE_NUMBER=`grep -n $PROBED_PORT_NAME $UDEV_FILE | cut -d : -f 1`
|
|
eval "sed '"$OLD_LINE_NUMBER"d' -i $UDEV_FILE"
|
|
fi
|
|
|
|
# Insert new name config if not exists
|
|
grep $ISER_NAME $UDEV_FILE > /dev/null 2>&1
|
|
if [ $? -ne 0 ]; then
|
|
echo >> $UDEV_FILE
|
|
echo $UDEV_LINE >> $UDEV_FILE
|
|
fi
|
|
|
|
# restart OFED modules for udev changes to take effect
|
|
modprobe -r mlx4_en
|
|
modprobe -r mlx5_ib && modprobe -r mlx5_core
|
|
udevadm control --reload-rules
|
|
udevadm trigger
|
|
modprobe mlx4_en
|
|
modprobe mlx5_ib && modprobe mlx5_core
|
|
|
|
if [ $? -ne 0 ]; then
|
|
logger_print error "Mellanox drivers restart failed."
|
|
exit 1
|
|
fi
|
|
logger_print info "Changed probed port name from $PROBED_PORT_NAME to $ISER_NAME."
|
|
else
|
|
logger_print info "Probed port name is configured properly to $ISER_NAME."
|
|
fi
|
|
|
|
exit 0
|