-
Notifications
You must be signed in to change notification settings - Fork 4
/
find-usb-disk
executable file
·177 lines (134 loc) · 3.21 KB
/
find-usb-disk
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#! /bin/bash
# Find and print info about USB devices on linux.
# (c) 2005-2018 Sudhi Herle <[email protected]>
# License: GPLv2
Z=`basename $0`
e=echo
Verbose=0
Partition=0
die() {
echo "$Z: $@" 1>&2
exit 0
}
warn() {
echo "$Z: $@" 1>&2
}
Uname=$(uname)
case $Uname in
Linux*)
;;
*) die "This only works on Linux!"
;;
esac
Fdisk=$(type -p fdisk)
[ -z $Fdisk ] && warn "Can't find fdisk; No partition info .."
main() {
local ac_prev=
local args=
local ac_option=
local ac_optarg=
for ac_option
do
shift
if [ -n "$ac_prev" ]; then
eval "$ac_prev=\$ac_option"
ac_prev=
continue
fi
case "$ac_option" in
-*=*) ac_optarg=`echo "$ac_option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
*) ac_optarg= ;;
esac
case "$ac_option" in
--help|-h|--hel|--he|--h)
usage;
;;
-v|--verbose)
Verbose=1
;;
-p|--show-partitions)
Partition=1
;;
--debug|-x)
set -x
;;
*) # first non option terminates option processing.
# we gather all remaining args and bundle them up.
args="$args $ac_option"
for xx
do
args="$args $xx"
done
break
;;
esac
done
find_usb_disk
}
find_usb_disk() {
local root=/dev/disk/by-path
for e in $(ls $root | grep usb); do
local fp=$root/$e
local nm=$(readlink $fp)
nm=$(basename $nm)
local disk=${nm%%[1-9]*}
[ $nm == $disk ] && print_disk "/dev/$disk" /sys/block/$disk
done
return 0
}
# Print details of a disk
print_disk() {
local dev=$1
local sysfs=$2
local sz=$(cat $sysfs/size)
# XXX Implcitly convert to GB
# size is in sectors (512 bytes).
sz=$(humanize $sz)
if [ $Verbose -gt 0 ]; then
local mf=$sysfs/device/model
local vf=$sysfs/device/vendor
local model="UNKNOWN"
local vendor="UNKNOWN"
test -f $mf && model=$(cat $mf | sed -e 's/ *$//')
test -f $vf && vendor=$(cat $vf | sed -e 's/ *$//')
echo "$dev: $sz; $vendor [$model]"
else
echo "$dev: $sz"
fi
if [ $Partition -gt 0 ]; then
[ -n "$Fdisk" ] && $Fdisk -l $dev
fi
return 0
}
usage() {
cat <<EOF
$0 - List USB Drives and their partitions.
Usage: $0 [options]
Options:
-h, --help Show this help message and quit
-v, --verbose Show verbose info about disk vendor etc.
-p, --show-partition Show partition info as well
-x, --debug Run script in trace mode (set -x)
EOF
exit 0
}
# humanize the size provided in sectors
humanize() {
local sz=$1
if [ $sz -lt 2097152 ]; then
sz=$(( $sz / 2048 ))
echo "$sz MB"
return 0
fi
if [ $sz -lt 2147483648 ]; then
sz=$(( $sz / 2097152 ))
echo "$sz GB"
return 0
fi
sz=$(( $sz / 2147483648 ))
echo "$sz TB"
return 0
}
main "$@"
#
# EOF