-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlist-pcie-ports.sh
executable file
·155 lines (139 loc) · 4.3 KB
/
list-pcie-ports.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/bash
# Copyright 2018 Jose Delarosa
#
# 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.
# List devices attached to PCIe ports. Shows output as:
# PCI1: Empty
# PCI2: LSI Logic / Symbios Logic MegaRAID SAS 2208 [Thunderbolt] (rev 01)
# PCI3: Broadcom Corporation NetXtreme II BCM5709 Gigabit Ethernet (rev 20)
bold="\033[1m"
end="\033[0m"
declare -a Port
DMIDECODE=/usr/sbin/dmidecode
# Minimum dmidecode version required
majreq=2
minreq=10
usage() {
echo "Usage: `basename $0` [options]"
echo
echo "Options are mutually exclusive:"
echo " -l list devices"
echo " -t print type of port, i.e. x4 PCI Express"
echo " -h print this menu"
exit 1
}
areweroot() {
[ `id | sed 's/uid=\([0-9]*\)(.*/\1/'` -eq 0 ] && return
echo "You must be root to run this script."; exit 1
}
checkvers() {
# scripts don't handle floating numbers, split into major and minor versions
maj=`$DMIDECODE -V | cut -d"." -f1`
min=`$DMIDECODE -V | cut -d"." -f2 | cut -d"-" -f1`
if [ $maj -gt $majreq ] ; then
return 0 # ok
elif [ $maj -eq $majreq ] ; then
[ $min -ge $minreq ] && return 0 # ok
fi
echo "dmidecode version requirement not met!"
echo -e "minimum version required is ${bold}${majreq}.${minreq}${end} --> found ${bold}${maj}.${min}${end}"
echo "you can find latest at http://download.savannah.gnu.org/releases/dmidecode/"
echo "exiting"
exit 1
}
get_slot() {
line=$*
slot=`echo $line | sed 's/Designation: //g'`
Port=("${Port[@]}" slot=$slot)
}
get_type() {
line=$*
desc=`echo $line | sed 's/Type: //g' | sed 's/ /-/g'` # replace blanks with -
Port=("${Port[@]}" type=$desc "addr=Empty")
}
get_addr() {
line=$*
addr=`echo $line | sed 's/Bus Address: //g'`
# remove last element ("Empty") and replace it with valid address
pos=${#Port[@]}
unset Port[$pos-1]
Port=("${Port[@]}" addr=$addr)
}
getdmidecode() {
# Get PCIe port information, store in temporary file
output=$(mktemp)
$DMIDECODE -t slot > $output
exec 3<$output
while read line 0<&3 ; do
f=`echo $line | grep Designation`
[ ! X"$f" = "X" ] && get_slot $line
f=`echo $line | grep "Type"`
[ ! X"$f" = "X" ] && get_type $line
f=`echo $line | grep "Bus Address"`
[ ! X"$f" = "X" ] && get_addr $line
done
exec 3<&-
rm -f $output # remove tmp file
}
printtype() {
for el in ${Port[@]} ; do
if [ `echo $el | cut -d"=" -f1` = "slot" ] ; then
slot=`echo $el | cut -d"=" -f2`
elif [ `echo $el | cut -d"=" -f1` = "type" ] ; then
type=`echo $el | cut -d"=" -f2- | sed 's/-/ /g'`
echo -e "${bold}${slot}:${end} $type"
fi
done
}
printlist() {
for el in ${Port[@]} ; do
if [ `echo $el | cut -d"=" -f1` = "slot" ] ; then
slot=`echo $el | cut -d"=" -f2`
elif [ `echo $el | cut -d"=" -f1` = "addr" ] ; then
addr=`echo $el | cut -d"=" -f2`
echo -ne "${bold}${slot}:${end}"
if [ ! $addr = "Empty" ] ; then
addrs=`echo $addr | cut -d":" -f2-` # strip PCI domain from address
dev=`lspci -s $addrs` # clean up first line
dev=`echo $dev | cut -d" " -f2- | cut -d":" -f2- | sed -e 's/([^()]*)//g'`
echo -e "${bold}${dev}${end}"
lspci -s $addrs -v | sed 1d # print details except first line (doing above)
else
echo " $addr"
fi
fi
done
}
areweroot
checkvers # check for version as early releases don't provide all data
# Process args
[ $# -eq 0 ] && usage
argCnt=0
listf=0
typef=0
while [ $# -ge 1 ] ; do
argList[$argCnt]=$1
shift 1
((argCnt++))
done
for arg in ${argList[@]} ; do
[ "$arg" = "-h" ] && usage
[ "$arg" = "-t" ] && typef=1
[ "$arg" = "-l" ] && listf=1
done
getdmidecode
if [ $typef = 1 ] ; then
printtype
elif [ $listf = 1 ] ; then
printlist
fi