summaryrefslogtreecommitdiffstats
path: root/normalize_dasd_arg
blob: 2f622e39a5315ad7edc153c211be18138fa73e93 (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
#!/bin/sh
#
# Copyright 2012 Red Hat, Inc.
# License: GPLv2
# Author: Jesse Keating <jkeating@redhat.com>
#
# Normalize DASD data into valid dasd.conf format
#
# Standard input should be the DASD argument
# Standard otuput is the properly formatted content
#
# it is used in
#   dracut generated initramfs
#
# Much of this code was salvaged from linuxrc.s390 from Anaconda:
#
# License GPLv2+
#
# Copyright (C) 2000-2004 by                                                    
#        Bernhard Rosenkraenzer <bero@redhat.com>                               
#        Oliver Paukstadt <opaukstadt@millenux.com>                             
#        Karsten Hopp <karsten@redhat.de>                                       
#        Florian La Roche <laroche@redhat.com>                                  
#        Nils Philippsen <nils@redhat.de>                                       
#        Helge Deller <hdeller@redhat.de>                                       
#        David Sainty <dsainty@redhat.com>                                      
# Copyright (C) IBM Corp. 2008,2009                                             
#        Author: Steffen Maier <maier@de.ibm.com>


function canonicalize_devno()
{
    case ${#1} in
        3) echo "0.0.0${1}" ;;
        4) echo "0.0.${1}" ;;
        *) echo "${1}" ;;
    esac
    return 0
}

read DASD
for dasditem in $(echo $DASD |sed 's/,/ /g')
    do
    unset range features lo hi attrs devno lodevno hidevno devbusid sys
    case $dasditem in
        autodetect|probeonly|nopav|nofcx|"") continue ;; # these don't gen a config
        *)
            IFS='('
            read range features <<< "$dasditem"
            unset IFS
            lo=${range%%-*}
            [[ "$lo" =~ (^[[:xdigit:]]+\.[0-3]\.[[:xdigit:]]{4}$)|(^[[:xdigit:]]{3,4}$) ]]
            case $? in
                0)  # string matched the pattern
                    lo=$(canonicalize_devno $lo) ;;
                1)  # string did not match the pattern
                    echo $"Incorrect format for lower bound of DASD range $range: $lo"
                    exit 1
                    ;;
                2)  echo "l.$LINENO: syntax error in regex of match operator =~, code needs to be fixed" 1>&2 ;;
                *)  echo "l.$LINENO: unexpected return code of regex match operator =~, code needs to be fixed" 1>&2 ;;
            esac
            if [ "${range//*-*/}" = "" ]; then
                hi=${range##*-}
                [[ "$hi" =~ (^[[:xdigit:]]+\.[0-3]\.[[:xdigit:]]{4}$)|(^[[:xdigit:]]{3,4}$) ]]
                case $? in
                    0)  # string matched the pattern
                        hi=$(canonicalize_devno $hi)
                        if [ "${lo%.*}" != "${hi%.*}" ]; then
                            echo $"Prefixes of DASD range $range do not match: ${lo%.*} != ${hi%.*}"
                            exit 1
                        fi
                        ;;
                    1)  # string did not match the pattern
                        echo $"Incorrect format for upper bound of DASD range $range: $hi"
                        exit 1
                        ;;
                    2)  echo "l.$LINENO: syntax error in regex of match operator =~, code needs to be fixed" 1>&2 ;;
                    *)  echo "l.$LINENO: unexpected return code of regex match operator =~, code needs to be fixed" 1>&2 ;;
                esac
            fi
            if [ "${features//*)/}" != "" ]; then
                echo $"Missing closing parenthesis at features of DASD range $range: ($features"
                exit 1
            fi
            if [ -n "$features" ]; then
                attrs=""
                features="${features%)}"
                for feature in $(echo $features |sed 's/:/\n/g'); do
                    case $feature in
                        ro) attrs=$attrs" readonly" ;;
                        diag) attrs=$attrs" use_diag" ;;
                        erplog|failfast) attrs=$attrs" "$feature ;;
                        *) echo $"Unknown DASD feature for device range $range: $feature"
                           exit 1
                           ;;
                    esac
                done
            fi
            [ -z "$hi" ] && hi=$lo
            lodevno=$((0x${lo##*.}))
            hidevno=$((0x${hi##*.}))
            for ((devno=$lodevno; $devno <= $hidevno; ++devno)); do
                devbusid=$(printf "%s.%04x" ${lo%.*} $devno)
                echo -n "$devbusid"
                for attr in $attrs; do
                    echo -n " $attr=1"
                done
                echo
            done
    esac
done