-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpacket-block-storage-detach
executable file
·88 lines (76 loc) · 2.16 KB
/
packet-block-storage-detach
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
#!/bin/bash
usage(){
echo "usage: $0 [-h] [volume_name]"
echo " -h display help"
echo " volume_name specify the volume name to detach, eg: volume-f9a8a263"
exit 1
}
while getopts "h" OPTION
do
case $OPTION in
h) usage
;;
esac
done
shift $(($OPTIND - 1))
if [ $1 ] ; then
target=$1
fi
function detach_volume () {
if [ -z "$1" ]
then
echo "Error: no iqn passed to detach_volume () function"
exit
fi
if [ -z "$2" ]
then
echo "Error: no volume name passed to detach_volume () function"
exit
fi
iqntarget="$1"
volname="$2"
if grep $volname /proc/mounts &>/dev/null; then
wheremnt=$(grep $volname /proc/mounts | awk '{print $2}')
echo "Error: volume $volname is currently mounted on $wheremnt. Please unmount it first and try again."
exit 1
fi
for iqn in `iscsiadm -m session | awk {'print $4'}`; do
if [ "$iqntarget" = "$iqn" ] ; then
portal=`iscsiadm -m session | grep $iqn | awk {'print $3'} | egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'`
echo "Logging out $iqn on $portal"
iscsiadm --mode node --targetname $iqn --portal $portal --logout
sed -i "/^$volname.*/d" /etc/multipath/bindings
multipath -f $volname
fi
done
}
export LOCALMD=/tmp/metadata.tmp
curl -sSL https://metadata.packet.net/metadata > $LOCALMD
volumecnt=`jq '.volumes[].iqn' $LOCALMD | wc -l`
# cleanup mpath entries in bindings file
sed -i "/^mpath.*/d" /etc/multipath/bindings
if [ `ls /dev/mapper/mpath* >/dev/null 2>/dev/null` ]; then
[ $_V -eq 1 ] && echo "mpath volume entries found, cleaning up"
for i in `ls /dev/mapper/mpath* | cut -d / -f4`; do
multipath -f $i
done
fi
for (( idx=0; idx<$volumecnt; idx++ )); do
volname=`jq '.volumes['$idx'].name ' $LOCALMD | sed 's/"//g'`
if [ "$target" ] ; then
if [ "$volname" = "$target" ] ; then
iqn=`jq '.volumes['$idx'].iqn ' $LOCALMD | sed 's/"//g'`
detach_volume $iqn $volname
continue
fi
else
iqn=`jq '.volumes['$idx'].iqn ' $LOCALMD | sed 's/"//g'`
detach_volume $iqn $volname
fi
done
# debian / centos
#rm -rf /var/lib/iscsi/send_targets/*
#rm -rf /var/lib/iscsi/nodes/*
#ubuntu
#rm -rf /etc/iscsi/send_targets/*
#rm -rf /etc/iscsi/nodes/*