-
Notifications
You must be signed in to change notification settings - Fork 3
/
mr_detach.sh
executable file
·80 lines (67 loc) · 1.81 KB
/
mr_detach.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
#!/bin/bash
set -e
if [ "$#" -ne "2" ]; then
echo "$0: <MAP> <NAME>"
exit 21
fi
MAP="$1"
NAME="$2"
if [ `id -u` -ne "0" ]; then
echo "Got Root?"
exit 22
fi
if [ ! -r ${MAP} ]; then
echo "${MAP} is not readable!"
exit 23
fi
LOBIN=`PATH="/sbin:/usr/sbin:$PATH" which losetup`
if [ "$?" != "0" ]; then
echo "losetup not found!"
exit 24
fi
echo "Searching for ${NAME} ..."
# Make sure that none of the loop device are still assembled in any raid device
# We want to make sure they are completely dormant before we detach them
INDEX="0"
RAIDCOUNT="0"
declare -a loop_array
while read -r LINE; do
if [ ! -d ${LINE}/${NAME} ]; then
echo "${LINE}/${NAME} is not a directory!"; exit 25
fi
DIMG=`ls -1 ${LINE}/${NAME}/${NAME}.?.rimg`
# Surround this with IF block so we don't bail early due to a missing image when array is degraded
if ${LOBIN} -a | grep -qw ${DIMG}; then
${LOBIN} -a | grep -w ${DIMG}
LOOP=`${LOBIN} -a | grep -w ${DIMG} | cut -d: -f1`
else
LOOP=""
fi
if [ -n "${LOOP}" ]; then
loop_array[${INDEX}]="${LOOP}"
BN=`basename ${LOOP}`
RD=`grep -w ${BN} /proc/mdstat | awk '{print $1}'`
if [ -n "${RD}" ] ; then
echo "${LOOP} appears to be attached to ${RD}..."
RAIDCOUNT=$(( RAIDCOUNT + 1))
fi
INDEX=$(( INDEX+1 ))
fi
done < ${MAP}
echo
# If INDEX equals 0, we found no loop devices related to ${NAME}
# If ${NAME} is not active, we could exit 0 here?
if [ "${INDEX}" == "0" ]; then
echo "${NAME} does not appear to be active"
exit 26
fi
# RAIDCOUNT should equal zero b/c the raid should have been dismantled by now
if [ "${RAIDCOUNT}" != "0" ]; then
echo "RAID appears to be active, Cowardly refusing to continue!"
exit 27
fi
# echo "${loop_array[@]}"
for LOOP in ${loop_array[@]}; do
echo "Detaching ${LOOP} ..."
${LOBIN} -d ${LOOP}
done