-
Notifications
You must be signed in to change notification settings - Fork 3
/
mr_info.sh
executable file
·189 lines (165 loc) · 4.29 KB
/
mr_info.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
active_loop_count()
{
COUNT="0"
while read -r LINE; do
DIMG=`ls -1 ${LINE}/${NAME}/${NAME}.?.rimg`
if ${LOBIN} -a | grep -qw ${DIMG}; then COUNT=$(( COUNT+1 )); fi
done < ${MAP}
echo ${COUNT}
}
list_all()
{
MAP="$1"
INDEX="0"
declare -a loc_array
while read -r LINE; do
loc_array[${INDEX}]="${LINE}"
INDEX=$(( INDEX+1 ))
done < ${MAP}
if [ "${#loc_array[@]}" == "0" ]; then
echo "${MAP} has no locations"
exit 0
fi
MRCOUNT=`echo "${loc_array[@]}" | xargs -n1 ls -1 | sort -u | wc -l`
if [ "${MRCOUNT}" == "0" ]; then
echo "Found 0 microraids"
exit 0
fi
echo "Found ${MRCOUNT} microraids:"
# echo "${loc_array[@]}" | xargs -n1 ls -1 | sort -u
for NAME in `echo "${loc_array[@]}" | xargs -n1 ls -1 | sort -u`; do
ALC=`active_loop_count "${MAP}" "${NAME}"`
if [ "${ALC}" == "0" ]; then
echo "${NAME}"
else
echo "${NAME}: (active)"
fi
done
}
mr_info()
{
MAP="$1"
NAME="$2"
INDEX="0"
RAIDCOUNT="0"
declare -a dimg_array
declare -a loop_array
declare -a raid_array
while read -r LINE; do
DIMG=`ls -1 ${LINE}/${NAME}/${NAME}.?.rimg`
if [ ! -r "${DIMG}" ]; then
echo "Could not find disk images for ${NAME} using this map!"
exit 7
fi
dimg_array[${INDEX}]="${DIMG}"
LOOP=`${LOBIN} -a | grep -w ${DIMG} | cut -d: -f1`
if [ -n "${LOOP}" ]; then
loop_array[${INDEX}]="${LOOP}"
BN=`basename ${LOOP}`
RD=`grep -w ${BN} /proc/mdstat | awk '{print $1}'`
if [ -n "${RD}" ]; then
raid_array[${INDEX}]="${RD}"
fi
fi
INDEX=$(( INDEX+1 ))
done < ${MAP}
#echo "dimg_array: ${dimg_array[@]}"
#echo "loop_array: ${loop_array[@]}"
#echo "raid_array: ${raid_array[@]}"
#echo "INDEX: ${INDEX}"
echo "Found ${#dimg_array[@]} disk images for ${NAME}:"
for DI in ${dimg_array[@]}; do
USED=`du -sh ${DI} | awk '{print $1}'`
AVAIL=`du -sh --apparent-size ${DI} | awk '{print $1}'`
BYTES=`du -b ${DI} | awk '{print $1}'`
BLKCOUNT=`${CALCBIN} "${BYTES}/4096" | awk '{print $1}'`
echo "${DI} (${USED}/${AVAIL}) [${BLKCOUNT} 4k-blocks]"
done
echo
# If ${NAME} has no loops, we are done here
if [ "${#loop_array[@]}" == "0" ]; then
echo "${NAME} has no loops active"
exit 0
fi
echo "Found ${#loop_array[@]} loops active for ${NAME}:"
for LOOP in ${loop_array[@]}; do
DI=`${LOBIN} -a | grep -w ${LOOP} | cut -d\( -f2 | cut -d\) -f1`
echo -n "${LOOP}: ${DI}"; echo
done
echo
if [ "${#raid_array[@]}" == "0" ]; then
echo "${NAME} has no assembled raid"
exit 0
fi
# Check to make sure we are about to stop the correct raid device
UNIQUERAIDCOUNT=`echo "${raid_array[@]}" | xargs -n1 echo | sort -u | wc -l`
if [ "${UNIQUERAIDCOUNT}" != "1" ]; then
echo "${NAME} appears to be attached to multiple devices?"
echo "${raid_array[@]}"
exit 8
fi
# Does ${NAME} have a named raid device?
RAIDNAME="0"
RAIDDEV="/dev/${raid_array[0]}"
for DEV in /dev/md/*; do
RP=`realpath ${DEV}`
if [ "${RP}" == "${RAIDDEV}" ]; then
RAIDNAME="${DEV}"
fi
done
if [ "${RAIDNAME}" != "0" ]; then
echo "${NAME} appears to be assembled into ${RAIDNAME} (${RAIDDEV})"
else
echo "${NAME} appears to be assembled into ${RAIDDEV}"
fi
echo -n "${raid_array[0]} :"
${MDBIN} --detail ${RAIDDEV} | grep 'State :' | cut -d: -f2-
grep -A1 -w "${raid_array[0]}" /proc/mdstat
if mount | grep -qw ${raid_array[0]}; then
echo
echo "${NAME} appears to be mounted: "
mount | grep ${raid_array[0]}
echo
df -h /dev/${raid_array[0]}
fi
}
if [ -z "$1" ]; then
echo "$0: <MAP> [NAME]"
exit 1
fi
MAP="$1"
if [ -n "$2" ]; then
NAME="$2"
fi
if [ `id -u` -ne "0" ]; then
echo "Got Root?"
exit 2
fi
if [ ! -r ${MAP} ]; then
echo "${MAP} is not readable!"
exit 3
fi
LOBIN=`PATH="/sbin:/usr/sbin:$PATH" which losetup`
if [ "$?" != "0" ]; then
echo "losetup not found!"
exit 4
fi
MDBIN=`PATH="/sbin:/usr/sbin:$PATH" which mdadm`
if [ "$?" != "0" ]; then
echo "mdadm not found!"
exit 5
fi
CALCBIN=`which calc`
if [ "$?" != "0" ]; then
echo "calc not found!"
exit 6
fi
# Place this after all the which commands
# So that which does trigger an exit w/o error message
set -e
if [ "$#" == "1" ]; then
list_all "${MAP}"
elif [ "$#" == "2" ]; then
mr_info "${MAP}" "${NAME}"
fi