~ubuntu-branches/ubuntu/saucy/s390-tools/saucy

« back to all changes in this revision

Viewing changes to zconf/chchp

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2008-07-15 23:55:41 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080715235541-r79vu6eqh4qim413
Tags: 1.6.2-1
* New upstream version.
* Install udev rules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
#==============================================================================
 
3
# Copyright IBM Corp. 2007
 
4
#
 
5
# chchp
 
6
#
 
7
# Script to modify channel-path state.
 
8
#
 
9
# Author(s): Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
 
10
#
 
11
# This file is part of s390-tools
 
12
#
 
13
# s390-tools is free software; you can redistribute it and/or modify
 
14
# it under the terms of the GNU General Public License as published by
 
15
# the Free Software Foundation; either version 2 of the License, or
 
16
# (at your option) any later version.
 
17
#
 
18
# s390-tools is distributed in the hope that it will be useful,
 
19
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
# GNU General Public License for more details.
 
22
#
 
23
# You should have received a copy of the GNU General Public License
 
24
# along with s390-tools; if not, write to the Free Software
 
25
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
26
#==============================================================================
 
27
 
 
28
SYSFS="/sys"
 
29
VERSION="%S390_TOOLS_VERSION%"
 
30
TOOLNAME=${0##*/}
 
31
MAX_CHPID_CSS=255
 
32
MAX_CHPID_ID=255
 
33
 
 
34
# Print help text
 
35
function print_help()
 
36
{
 
37
        cat <<EOF
 
38
Usage: ${TOOLNAME} COMMAND CHPID
 
39
 
 
40
Modify the state of channel-path CHPID. CHPID can be a single, hexadecimal
 
41
channel-path identifier, a comma-separated list or a range of identifiers.
 
42
 
 
43
COMMANDS:
 
44
-v, --vary VALUE           Logically vary channel-path to VALUE (1=on, 0=off)
 
45
-c, --configure VALUE      Configure channel-path to VALUE (1=on, 0=standby)
 
46
-a, --attribute KEY=VALUE  Set channel-path attribute KEY to VALUE
 
47
-h, --help                 Print this help, then exit
 
48
    --version              Print version information, then exit
 
49
EOF
 
50
}
 
51
 
 
52
# Print version information
 
53
function print_version()
 
54
{
 
55
        cat <<EOF
 
56
${TOOLNAME}: version ${VERSION}.
 
57
Copyright IBM Corp. 2007
 
58
EOF
 
59
}
 
60
 
 
61
# Print channel-path directory
 
62
function get_chp_dir()
 
63
{
 
64
        local DIR;
 
65
 
 
66
        DIR=$(printf "%s/devices/css%x/chp%x.%x" "$SYSFS" "$1" "$1" "$2")
 
67
        if [ ! -d $DIR ] ; then
 
68
                DIR=$(printf "%s/devices/css%x/chp%x.%02x" "$SYSFS" "$1" "$1" \
 
69
                      "$2")
 
70
        fi
 
71
        echo $DIR
 
72
}
 
73
 
 
74
# Write and check attribute value
 
75
function write_value()
 
76
{
 
77
        local DIR=$1
 
78
        local KEY=$2
 
79
        local VAL=$3
 
80
        local VAL2
 
81
 
 
82
        if [ ! -f $DIR/$KEY ] ; then
 
83
                echo "failed - no such attribute"
 
84
                exit 1
 
85
        fi
 
86
        if [ ! -w $DIR/$KEY ] ; then
 
87
                echo "failed - attribute not writable"
 
88
                exit 1
 
89
        fi
 
90
        if ! echo $VAL > $DIR/$KEY ; then
 
91
                echo "failed - write failed"
 
92
                exit 1
 
93
        fi
 
94
        if ! read < $DIR/$KEY VAL2 ; then
 
95
                echo "failed - could not determine new attribute value"
 
96
                exit 1
 
97
        fi
 
98
        # Skip value comparison for 'status' attribute because input
 
99
        # can be specified in different ways
 
100
        if [ "$KEY" != "status" ] ; then
 
101
                if [ "$VAL" != "$VAL2" ] ; then
 
102
                        echo "failed - attribute value not as expected"
 
103
                        exit 1
 
104
                fi
 
105
        fi
 
106
        echo done.
 
107
}
 
108
 
 
109
# Configure channel-path
 
110
function configure()
 
111
{
 
112
        local CSS=$1
 
113
        local NUM=$2
 
114
        local DIR=$3
 
115
        local VAL=$4
 
116
 
 
117
        if [ "$VAL" == "0" ] ; then
 
118
                OP="standby"
 
119
        else
 
120
                OP="online"
 
121
        fi
 
122
 
 
123
        printf "Configure %s %x.%02x... " $OP $CSS $NUM
 
124
        write_value $DIR "configure" $VAL
 
125
}
 
126
 
 
127
# Vary channel-path
 
128
function vary()
 
129
{
 
130
        local CSS=$1
 
131
        local NUM=$2
 
132
        local DIR=$3
 
133
 
 
134
        if [ "$4" == "0" ] ; then
 
135
                VAL="offline"
 
136
        else
 
137
                VAL="online"
 
138
        fi
 
139
 
 
140
        printf "Vary %s %x.%02x... " $VAL $CSS $NUM
 
141
        write_value $DIR "status" $VAL
 
142
}
 
143
 
 
144
# Modify channel-path attribute
 
145
function attribute()
 
146
{
 
147
        local CSS=$1
 
148
        local NUM=$2
 
149
        local DIR=$3
 
150
        local KEY=${4%%=*}
 
151
        local VAL=${4#*=}
 
152
 
 
153
        printf "Attribute %s=%s %x.%02x... " $KEY $VAL $CSS $NUM
 
154
        write_value $DIR $KEY $VAL
 
155
}
 
156
 
 
157
# Make sure only one command is specified
 
158
function check_and_set_command()
 
159
{
 
160
        if [ ! -z "$COMMAND" ] ; then
 
161
                echo "$TOOLNAME: Only one of --vary, --configure or " \
 
162
                     "--attribute allowed" >&2
 
163
                exit 1
 
164
        fi
 
165
        COMMAND=$1
 
166
}
 
167
 
 
168
# Make sure command argument was specified correctly
 
169
function check_and_set_value()
 
170
{
 
171
        if [ -z "$1" ] ; then
 
172
                echo "$TOOLNAME: --${COMMAND} requires an argument" >&2
 
173
                exit 1
 
174
        fi
 
175
        if [ "$COMMAND" == "attribute" ] ; then
 
176
                local KEY=${1%%=*}
 
177
                local VAL=${1#*=}
 
178
 
 
179
                if [ "$KEY" == "$1" ] || [ -z "$KEY" ] || [ -z "$VAL" ] ; then
 
180
                        echo "$TOOLNAME: --attribute requires an argument " \
 
181
                             "KEY=VALUE" >&2
 
182
                        exit 1
 
183
                fi
 
184
        fi
 
185
        VALUE=$1
 
186
}
 
187
 
 
188
# Extract css id from channel-path id string
 
189
function get_chpid_css()
 
190
{
 
191
        local VAR=$1
 
192
        local CHPID=$2
 
193
        local CSS
 
194
        local RESULT
 
195
 
 
196
        CSS=${CHPID%%.*}
 
197
        if [ "$CSS" == "$CHPID" ] ; then
 
198
                RESULT=0
 
199
        else
 
200
                let RESULT="0x$CSS" 2>/dev/null
 
201
                if [ -z "$RESULT" ] ; then
 
202
                        echo "$TOOLNAME: Invalid channel-path identifier " \
 
203
                             "'$CHPID'" >&2
 
204
                        exit 1
 
205
                fi
 
206
                if [ "$RESULT" -lt 0 ] || [ $RESULT -gt $MAX_CHPID_CSS ] ; then
 
207
                        echo "$TOOLNAME: Invalid channel-path identifier " \
 
208
                             "'$CHPID'" >&2
 
209
                        exit 1
 
210
                fi
 
211
        fi
 
212
        eval $VAR=$RESULT
 
213
}
 
214
 
 
215
# Extract id from channel-path id string
 
216
function get_chpid_id()
 
217
{
 
218
        local VAR=$1
 
219
        local CHPID=$2
 
220
        local ID
 
221
        local RESULT
 
222
 
 
223
        ID=${CHPID##*.}
 
224
        let RESULT="0x$ID" 2>/dev/null
 
225
        if [ -z "$RESULT" ] ; then
 
226
                echo "$TOOLNAME: Invalid channel-path identifier '$CHPID'" >&2
 
227
                exit 1
 
228
        fi
 
229
        if [ "$RESULT" -lt 0 ] || [ $RESULT -gt $MAX_CHPID_ID ] ; then
 
230
                echo "$TOOLNAME: Invalid channel-path identifier '$CHPID'" >&2
 
231
                exit 1
 
232
        fi
 
233
        eval $VAR=$RESULT
 
234
}
 
235
 
 
236
# Perform command specified by COMMAND and VALUE
 
237
function perform_command()
 
238
{
 
239
        local CSS=$1
 
240
        local ID=$2
 
241
        local DIR
 
242
 
 
243
        DIR=$(get_chp_dir "$CSS" "$ID")
 
244
        if [ ! -d "$DIR" ] ; then
 
245
                printf "Skipping unknown channel-path "
 
246
                printf "%x.%02x\n" $CSS $ID
 
247
                return
 
248
        fi
 
249
        case $COMMAND in
 
250
        vary)
 
251
                vary $CSS $ID $DIR $VALUE
 
252
                ;;
 
253
        configure)
 
254
                configure $CSS $ID $DIR $VALUE
 
255
                ;;
 
256
        attribute)
 
257
                attribute $CSS $ID $DIR $VALUE
 
258
                ;;
 
259
        esac
 
260
}
 
261
 
 
262
# Calculate iterator steps for chpid loop
 
263
function get_iterator_step()
 
264
{
 
265
        local VAR=$1
 
266
        local CSS1=$2
 
267
        local ID1=$3
 
268
        local CSS2=$4
 
269
        local ID2=$5
 
270
        local RESULT
 
271
 
 
272
        if [ $CSS1 -eq $CSS2 ] ;then
 
273
                if [ $ID1 -le $ID2 ] ; then     
 
274
                        RESULT=1
 
275
                else
 
276
                        RESULT=-1
 
277
                fi
 
278
        elif [ $CSS1 -lt $CSS2 ] ; then
 
279
                RESULT=1
 
280
        else
 
281
                RESULT=-1
 
282
        fi
 
283
 
 
284
        eval $VAR=$RESULT
 
285
}
 
286
 
 
287
function loop_chpids()
 
288
{
 
289
        local CSS1=$1
 
290
        local ID1=$2
 
291
        local CSS2=$3
 
292
        local ID2=$4
 
293
        local FUNC=$5
 
294
        local STEP
 
295
 
 
296
        get_iterator_step STEP $1 $2 $3 $4
 
297
        # Loop
 
298
        while true ; do
 
299
                # Perform function
 
300
                eval $FUNC $CSS1 $ID1
 
301
                # Check for loop end
 
302
                if [ $CSS1 -eq $CSS2 ] && [ $ID1 -eq $ID2 ] ; then
 
303
                        break
 
304
                fi
 
305
                # Advance iterator
 
306
                let ID1=$ID1+$STEP
 
307
                if [ $ID1 -lt 0 ] ; then
 
308
                        let CSS1=$CSS1-1
 
309
                        ID1=255
 
310
                elif [ $ID2 -gt 255 ] ; then
 
311
                        let CSS1=$CSS1+1
 
312
                        ID1=0
 
313
                fi
 
314
        done
 
315
}
 
316
 
 
317
# Parse command line parameters
 
318
COMMAND=""
 
319
VALUE=""
 
320
CHPID_LIST=""
 
321
while [ $# -gt 0 ]; do
 
322
        case $1 in
 
323
        -h|--help)
 
324
                print_help
 
325
                exit 0
 
326
                ;;
 
327
        --version)
 
328
                print_version
 
329
                exit 0
 
330
                ;;
 
331
        -v|--vary)
 
332
                check_and_set_command "vary"
 
333
                check_and_set_value "$2"
 
334
                shift
 
335
                ;;
 
336
        -c|--configure)
 
337
                check_and_set_command "configure"
 
338
                check_and_set_value "$2"
 
339
                shift
 
340
                ;;
 
341
        -a|--attribute)
 
342
                check_and_set_command "attribute"
 
343
                check_and_set_value "$2"
 
344
                shift
 
345
                ;;
 
346
        -*|--*)
 
347
                echo "$TOOLNAME: Invalid option $1" >&2
 
348
                echo "Try '$TOOLNAME --help' for more information" >&2
 
349
                exit 1
 
350
                ;;
 
351
        *)
 
352
                if [ -z "$CHPID_LIST" ] ; then
 
353
                        CHPID_LIST="$1"
 
354
                else
 
355
                        CHPID_LIST="$CHPID_LIST,$1"
 
356
                fi
 
357
                ;;
 
358
        esac
 
359
        shift
 
360
done
 
361
 
 
362
if [ -z "$COMMAND" ] ; then
 
363
        echo "$TOOLNAME: One of --vary, --configure or --attribute required" >&2
 
364
        exit 1
 
365
fi
 
366
 
 
367
if [ -z "$CHPID_LIST" ] ; then
 
368
        echo "$TOOLNAME: Need to specify at least one channel-path ID" >&2
 
369
        exit 1
 
370
fi
 
371
 
 
372
if [ ! -e "$SYSFS" ] ; then
 
373
        echo "$TOOLNAME: $SYSFS does not exist" >&2
 
374
        exit 1
 
375
fi
 
376
 
 
377
if [ ! -d "$SYSFS" ] ; then
 
378
        echo "$TOOLNAME: $SYSFS is not a directory" >&2
 
379
        exit 1
 
380
fi
 
381
 
 
382
# Loop over comma-separated list
 
383
IFS=','
 
384
for CHPID in $CHPID_LIST ; do
 
385
        if [ -z "$CHPID" ] ; then
 
386
                continue
 
387
        fi
 
388
        CHPID_FROM=${CHPID%%-*}
 
389
        if [ "$CHPID" == "$CHPID_FROM" ] ; then
 
390
                CHPID_TO=$CHPID_FROM
 
391
        else
 
392
                CHPID_TO=${CHPID##*-}
 
393
                if [ -z "$CHPID_TO" ] ; then
 
394
                        echo "$TOOLNAME: Invalid channel-path identifier " \
 
395
                             "range $CHPID" >&2
 
396
                        exit 1
 
397
                fi
 
398
        fi
 
399
        get_chpid_css FROM_CSS $CHPID_FROM
 
400
        get_chpid_id FROM_ID $CHPID_FROM
 
401
        get_chpid_css TO_CSS $CHPID_TO
 
402
        get_chpid_id TO_ID $CHPID_TO
 
403
        loop_chpids $FROM_CSS $FROM_ID $TO_CSS $TO_ID perform_command
 
404
done