Hi all,

Here's a shell script that I've been using to replace the features of
fcconf that I used frequently.  One nice thing about it is that it doesn't
require root access except to just view status.  It should work for all HBAs.

Let me know what you think.  Try it out.  We can name it anything,
but for now I've called it 'fcc'.  (I guess Federick Community 
College won't mind :-) ). 

        Joe

#! /bin/bash
#
# Copyright 2008 Cisco Systems, Inc.  All rights reserved.
#
# This program is free software; you may redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

fcoe_dir=/sys/module/fcoe
fdir=/sys/class/fc_host
sdir=/sys/class/scsi_host

# suggested name is fcc
cmdname=$0

usage() {
cat <<USAGE
usage: $cmdname '[<hba>] [<cmd>]'

cmd:
        create
        delete
        enable (or en)
        delete (or del or destroy)
        list
        luns
        stats
        reset
USAGE
}

hba_stats() {
        local x=$1
        local y

        printf "\n$x Statistics:\n"
        (
                cd $fdir/$x/statistics
                for y in *
                do
                        #
                        # avoid the write-only files.
                        # test -r doesn't help if we're root
                        #
                        if [ "$y" == "reset_statistics" ]
                        then
                                continue
                        fi
                        val=`cat $y`
                        if [ "$val" != 0xffffffffffffffff ]
                        then
                                printf "%-30s%8lld\n" $y: $val
                        fi
                done
        )
}

rport_list() {
        local x
        local hba=$1

        rdir=/sys/class/fc_remote_ports
        host=`echo $hba | sed -e 's/host//'`
        rports=`ls -d $rdir/rport-$host:* 2>/dev/null`
        if [ -z "$rports" ]
        then
                return
        fi

        printf "\n$hba Remote Ports:\n"


        hdr="%-8s %-20.16s %-10.10s %-8s %-15s\n"
        fmt="%-8s %-20.16x  %6x    %-8s %-15s\n"

        printf "$hdr" Path "Port Name" "Port ID" State Roles
        for x in $rports
        do
                btl="`echo $x | sed -e 's/.*-\(.*-.*\)/\1/'`"
                printf "$fmt" "$btl" \
                        "`cat $x/port_name`" \
                        "`cat $x/port_id`" \
                        "`cat $x/port_state`" \
                        "`cat $x/roles`"
        done
}

lun_list() {
        local x
        local lun
        local hba=$1
        local ddir=/sys/class/scsi_device

        host=`echo $hba | sed -e 's/host//'`

        local luns=`(cd $ddir && ls -d $host:*) 2>/dev/null`

        if [ -z "$luns" ]
        then
                return
        fi

        printf "\n$hba LUNs:\n"

        fmt="%-10s %-8s %6s   %-15s %-20s %-8s\n"

        printf "$fmt" Path Device Size Vendor Model State

        for lun in $luns
        do 
                (
                        local sizek=0 sizem=0 sizeg=0 sizet=0

                        cd $ddir/$lun/device
                        dev="`ls -d block:* char:* 2>/dev/null |
                                sed -e 's/.*\://'`"

                        cap=-
                        if [ -L "block:$dev" ]
                        then
                                # compute sizes in base-10 marketing units
                                local onek=1000
                                local size=`cat block:$dev/size`
                                let sizek="$size * 512 / $onek"
                                let sizem="$sizek / $onek"
                                let sizeg="$sizem / $onek"
                                let sizet="$sizeg / $onek"
                                if [ "$sizet" -gt 0 ]
                                then
                                        cap="$sizet TB"
                                elif [ "$sizeg" -gt 0 ]
                                then
                                        cap="$sizeg GB"
                                elif [ "$sizem" -gt 0 ]
                                then
                                        cap="$sizem MB"
                                else
                                        cap="$sizek KB"
                                fi
                        fi
                        
                        printf "$fmt" "$lun" "$dev" "$cap"\
                                "`cat vendor`" \
                                "`cat model`" \
                                "`cat state`"
                )
         done
}

hba_list() {
        local x

        echo "FC HBAs:"
        hdr="%-8s %-20s %-10s %-18s %s\n"
        fmt="%-8s %-20.16x  %6x    %-18s %s\n"
        printf "$hdr" HBA WWPN "Port ID" State Device
        for x in $hbas
        do
                (
                        cd $fdir/$x
                        printf "$fmt" "$x" \
                                "`cat $fdir/$x/port_name`" \
                                "`cat $fdir/$x/port_id`" \
                                "`cat $sdir/$x/state`" \
                                "`cat $fdir/$x/symbolic_name |
                                        sed -e 's/.*over //'`"
                ) 2>/dev/null
        done

        for x in $hbas
        do
                rport_list $x
                lun_list $x
        done
}

#
# Do a command for a list of arguments
#
repeat() {
        local cmd=$1
        local x
        shift

        for x 
        do
                $cmd $x
        done
}

fcoe_ctl() {
        local hba=$1
        local cmd=$2
        local file=$fcoe_dir/$2

        if [ -w "$file" ]
        then
                echo $hba > $fcoe_dir/create
        elif [ -f "$file" ]
        then
                echo "$cmdname: no permission to $cmd $hba" 1>&2
        else
                echo "$cmdname: $cmd not supported for $hba" 1>&2
        fi
}

fc_host_ctl() {
        local hba=$1
        local cmd=$2
        local value=$3
        local file=$fdir/$1/$2

        if [ -w "$file" ]
        then
                echo $value > $file
        elif [ -f "$file" ]
        then
                echo "$cmdname: no permission to $cmd $hba" 1>&2
        else
                echo "$cmdname: $cmd not supported for $hba" 1>&2
        fi
}

#
# Start of main script code.
#
if [ ! -d "$fdir" ]
then
        echo "No FC HBAs"
        exit
fi
hbas=`ls $fdir 2>/dev/null`

if [ $# -lt 1 ]
then
        hba_list
        exit 0
fi

if [ $# -lt 2 ]
then
        cmd=$1
else
        hbas=$1
        hba=$1
        cmd=$2
fi


case "$cmd" in
        create | enable | en)
                if [ ! -d $fcoe_dir ]
                then
                        modprobe fcoe
                        echo "$0: loading fcoe module" >&2
                        sleep 1
                        if [ ! -d $fcoe_dir ]
                        then
                                echo "$0: $fcoe_dir not found" >&2
                                exit 2
                        fi
                fi
                fcoe_ctl $hba create
                ;;
        delete | del | destroy)
                if [ ! -d $fcoe_dir ]
                then
                        echo "$0: $fcoe_dir not found" >&2
                        exit 2
                fi
                fcoe_ctl $hba destroy
                ;;
        list)
                hba_list
                ;;
        lun*)
                repeat lun_list $hbas
                ;;
        stat*)
                repeat hba_stats $hbas
                ;;
        stop | start)
                fcoe_ctl $hba $cmd
                ;;
        reset)
                fc_host_ctl $hba issue_lip 1
                ;;
        *)
                usage
                ;;
esac
_______________________________________________
devel mailing list
[email protected]
http://www.open-fcoe.org/mailman/listinfo/devel

Reply via email to