summaryrefslogtreecommitdiffstats
path: root/device_cio_free
blob: e7ffbc771d8c818b62a92e517c453d9c40b2dff2 (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/bin/sh
#
# Copyright 2009, 2010 Red Hat, Inc.
# License: GPLv2
# Author: Dan Horák <dhorak@redhat.com>
#
# unblock devices listed in various config files and wait until they are ready
#
# it uses dasd and zfcp config file
# config file syntax:
# deviceno   options
# or
# deviceno   WWPN   FCPLUN
#
# also processes the system ccw config file and network interface configurations
#

DASDCONFIG=/etc/dasd.conf
ZFCPCONFIG=/etc/zfcp.conf
ZNETCONFIG=/etc/ccw.conf
BLACKLIST=/proc/cio_ignore
VERBOSE=
PATH=/bin:/usr/bin:/sbin:/usr/sbin
ALL_DEVICES=
WAITING_TIMEOUT=60	# maximum time to wait for all devices to appear
WAITING_TOTAL=0		# actual time spent waiting for devices

usage()
{
    echo "Usage: $CMD [-h|--help] [-V|--verbose]"
    exit 1
}

# accepts single device, comma-separated lists and dash separated ranges and their combinations
free_device()
{
    local DEV

    [ -z "$1" ] && return

    DEV=$(echo $1 | tr "A-Z" "a-z")
    
    [ $VERBOSE ] && echo "Freeing device(s) $DEV"
    if ! echo "free $DEV" > $BLACKLIST 2> /dev/null ; then
	echo "Error: can't free device(s) $DEV"
    else
	if [ -z $ALL_DEVICES ]; then
	    ALL_DEVICES=$DEV
	else
	    ALL_DEVICES="$ALL_DEVICES,$DEV"
	fi
    fi
}

# wait until a device appears on the ccw bus
wait_on_device()
{
    local DEVICE_ONLINE DEV
    
    [ -z "$1" ] && return
    
    DEV=$1
    DEVICE_ONLINE=/sys/bus/ccw/devices/$DEV/online

    [ $VERBOSE ] && echo "Waiting on device $DEV"
    [ -f "$DEVICE_ONLINE" ] && return

    for t in 1 2 3 4 5
    do
	if [ $WAITING_TOTAL -ge $WAITING_TIMEOUT ]; then
	    [ $VERBOSE ] && echo "Waiting timeout of $WAITING_TIMEOUT seconds reached"
	    break
	fi
	WAITING_TOTAL=$(($WAITING_TOTAL + $t))
        [ $VERBOSE ] && echo "Waiting additional $t second(s) and $WAITING_TOTAL second(s) in total"
	sleep $t
	[ -f "$DEVICE_ONLINE" ] && return
    done
    echo "Error: device $DEV still not ready"
}

process_config_file()
{
    local CONFIG

    [ -z "$1" ] && return
    
    CONFIG="$1"
    if [ -f "$CONFIG" ]; then
        while read line; do
	    case $line in
		\#*) ;;
		*)
		    [ -z "$line" ] && continue
		    set $line
		    free_device $1
		    ;;
	    esac
	done < $CONFIG
    fi
}

# check how we were called
CMD=${0##*/}
DIR=${0%/*}
ARGS=$@
case $CMD in
    "dasd_cio_free")
	MODE_DASD="yes"
	;;
    "zfcp_cio_free")
	MODE_ZFCP="yes"
	;;
    "znet_cio_free")
	MODE_ZNET="yes"
	;;
    "device_cio_free")
	MODE_DASD="yes"
	MODE_ZFCP="yes"
	MODE_ZNET="yes"
	;;
    *)
	echo "Error: unknown alias '$CMD'."
	echo "Supported aliases are dasd_cio_free, zfcp_cio_free and znet_cio_free."
	exit 1
	;;
esac

# process command line options
while [ $# -gt 0 ]; do
    case $1 in
	-V|--verbose)
	    VERBOSE=yes
	    ;;
	-h|--help)
	    usage
	    ;;
	*)
	    echo "Error: unknown option $1"
	    usage
	    ;;
    esac
    shift
done

if [ ! -f $BLACKLIST ]; then
    echo "Error: $BLACKLIST kernel interface doesn't exist"
    exit 2
fi


if [ $VERBOSE ]; then
    echo -n "Freeing devices:"
    [ $MODE_DASD ] && echo -n " dasd"
    [ $MODE_ZFCP ] && echo -n " zfcp"
    [ $MODE_ZNET ] && echo -n " znet"
    echo
fi

[ $MODE_DASD ] && process_config_file $DASDCONFIG
[ $MODE_ZFCP ] && process_config_file $ZFCPCONFIG

if [ $MODE_DASD ]; then
    # process the device list defined as option for the dasd module
    DEVICES=$(modprobe --showconfig | grep "options[[:space:]]\+dasd_mod" | \
	sed -e 's/.*[[:space:]]dasd=\([^[:space:]]*\).*/\1/' -e 's/([^)]*)//g' \
	-e 's/nopav\|nofcx\|autodetect\|probeonly//g' -e 's/,,/,/g' -e 's/^,//' -e 's/,$//')

    for DEVRANGE in $(echo $DEVICES | tr ',' ' '); do
	free_device $DEVRANGE
    done
fi

if [ $MODE_ZNET ]; then
    # process the config file
    if [ -f "$ZNETCONFIG" ]; then
        while read line; do
	    case $line in
		\#*) ;;
		*)
		    [ -z "$line" ] && continue
		    # grep 2 or 3 channels from each "<nettype>,<subchannels>,<options>" line
		    DEVICES=$(echo $line | grep -E -i -o "([0-9]\.[0-9]\.[a-f0-9]+,){1,2}([0-9]\.[0-9]\.[a-f0-9]+)")
		    free_device $DEVICES
		    ;;
	    esac
	done < $ZNETCONFIG
    fi
    # process channels from network interface configurations
    for line in $(grep -E -i -h "^[[:space:]]*SUBCHANNELS=['\"]?([0-9]\.[0-9]\.[a-f0-9]+,){1,2}([0-9]\.[0-9]\.[a-f0-9]+)['\"]?([[:space:]]+#|[[:space:]]*$)" /etc/sysconfig/network-scripts/ifcfg-* 2> /dev/null)
    do
	eval "$line"
        free_device $SUBCHANNELS
    done
fi

[ -z "$ALL_DEVICES" ] && exit 0

# wait until recently unblocked devices are ready
# at this point we know the content of ALL_DEVICES is syntacticly correct
OLD_IFS=$IFS
IFS=","
set $ALL_DEVICES
for DEV in $*
do
    IFS="."

    # get the lower bound for range or get the single device
    LOWER=${DEV%%-*}
    set $LOWER
    if [ $# -eq 1 ]; then
        L0=0
        L1=0
        L2=$(printf "%d" "0x$1")
    else
        L0=$(printf "%d" "0x$1")
        L1=$(printf "%d" "0x$2")
        L2=$(printf "%d" "0x$3")
    fi

    # get the upper bound for range or get the single device
    UPPER=${DEV##*-}
    set $UPPER
    if [ $# -eq 1 ]; then
        U0=0
        U1=0
        U2=$(printf "%d" "0x$1")
    else
        U0=$(printf "%d" "0x$1")
        U1=$(printf "%d" "0x$2")
        U2=$(printf "%d" "0x$3")
    fi

    IFS=$OLD_IFS

    # iterate thru all devices
    for i in $(seq $L0 $U0); do
        [ $i -eq $L0 ] && LJ=$L1 || LJ=0
        [ $i -eq $U0 ] && UJ=$U1 || UJ=3

        for j in $(seq $LJ $UJ); do
            [ $i -eq $L0 -a $j -eq $L1 ] && LK=$L2 || LK=0
            [ $i -eq $U0 -a $j -eq $U1 ] && UK=$U2 || UK=65535

            for k in $(seq $LK $UK); do
                wait_on_device "$(printf %x.%x.%04x $i $j $k)"
            done
        done
    done
done