Whamcloud - gitweb
b24336 ldlm_resource::lr_lvb_data is protected by wrong lock
[fs/lustre-release.git] / build / funcs.sh
1 cleanup() {
2
3     true
4 }
5
6 error() {
7     local msg="$1"
8
9     [ -n "$msg" ] && echo -e "\n${0##*/}: $msg" >&$STDOUT
10
11 }
12
13 fatal() {
14
15     cleanup
16     error "$2"
17     exit $1
18
19 }
20
21 #
22 # in a given directory, find the first rpm matching given requirements
23 #
24 find_rpm() {
25     local dir="$1"
26     local match_type="$2"
27     local match="$3"
28
29     pushd "$dir" > /dev/null || \
30         fatal 1 "Unable to chdir to directory \"$dir\" in find_rpm()"
31
32     local file
33     for file in $(ls *.rpm); do
34         if [ ! -f "$file" ]; then
35             continue
36         fi
37         case "$match_type" in
38             provides)
39                 # match is any valid ERE (i.e. given to egrep) match
40                 if rpm -q --provides -p "$file" 2>&$STDOUT | egrep "$match" >&$STDOUT; then
41                     echo "$file"
42                     popd >/dev/null
43                     return 0
44                 fi
45                 ;;
46             *)
47                 popd >/dev/null
48                 fatal 1 "Unknown match type \"$match_type\" given to find_rpm()"
49                 ;;
50         esac
51     done
52
53     popd >/dev/null
54     return 1
55 }
56
57 find_linux_rpms() {
58     local prefix="$1"
59     local pathtorpms=${2:-"${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}/${TARGET_ARCH}"}
60
61     local kernel_rpms=$(find_linux_rpm "$prefix" "$pathtorpms")
62     # call a distro specific hook, if available
63     if type -p find_linux_rpms-$DISTRO; then
64         local rpm
65         if rpm=$(find_linux_rpms-$DISTRO "$prefix" "$pathtorpms"); then
66             kernel_rpms="$kernel_rpms $rpm"
67         else
68             return 255
69         fi
70     fi
71
72     echo "$kernel_rpms"
73     return 0
74
75 }
76
77 # a noop function which can be overridden by a distro method implementation
78 resolve_arch() {
79     local arch="$1"
80
81     echo "$arch"
82 }
83
84 # XXX this needs to be re-written as a wrapper around find_rpm
85 #     or just gotten rid of.  :-)
86 find_linux_rpm() {
87     local prefix="$1"
88     local pathtorpms=${2:-"${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}/${TARGET_ARCH}"}
89
90     [ -d $pathtorpms ] || return 255
91
92     local kernelbinaryrpm rpmfile
93     local wanted_kernel="${lnxmaj}${lnxmin}-${lnxrel}"
94
95     local arch ret=1
96         local found_rpm="" rpm
97         for rpm in $(ls ${pathtorpms}/*.$(resolve_arch $TARGET_ARCH $PATCHLESS).rpm); do
98             if rpm -q --provides -p "$rpm" 2>&$STDOUT | grep -q "kernel${prefix} = $wanted_kernel" 2>&$STDOUT; then
99                 found_rpm="$rpm"
100                 ret=0
101                 break
102             fi
103         done
104     # see above "XXX"
105     #     [ -f "$found_rpm" ] && break
106     # done
107
108     echo "$found_rpm"
109     return $ret
110
111 }
112
113 # autodetect used Distro
114 autodetect_distro() {
115
116     local name
117     local version
118
119     if which lsb_release >/dev/null 2>&1; then
120         name="$(lsb_release -s -i)"
121         version="$(lsb_release -s -r)"
122         case "$name" in
123             "EnterpriseEnterpriseServer")
124                 name="oel"
125                 version="${version%%.*}"
126                 ;;
127             "RedHatEnterpriseServer" | "ScientificSL")
128                 name="rhel"
129                 version="${version%%.*}"
130                 ;;
131             "SUSE LINUX")
132                 name="sles"
133                 ;;
134             *)
135                 fatal 1 "I don't know what distro name $name and version $version is.\nEither update autodetect_distro() or use the --distro argument."
136                 ;;
137         esac
138     else
139         echo "You really ought to install lsb_release for accurate distro identification"
140         # try some heuristics
141         if [ -f /etc/SuSE-release ]; then
142             name=sles
143             version=$(grep ^VERSION /etc/SuSE-release)
144             version=${version#*= }
145         elif [ -f /etc/redhat-release ]; then
146             #name=$(head -1 /etc/redhat-release)
147             name=rhel
148             version=$(echo "$distroname" |
149                       sed -e 's/^[^0-9.]*//g' | sed -e 's/[ \.].*//')
150         fi
151         if [ -z "$name" -o -z "$version" ]; then
152             fatal 1 "I don't know how to determine distro type/version.\nEither update autodetect_distro() or use the --distro argument."
153         fi
154     fi
155
156     echo ${name}${version}
157     return 0
158
159 }
160
161 # autodetect target
162 autodetect_target() {
163     local distro="$1"
164
165     local target=""
166     case ${distro} in
167           oel5) target="2.6-oel5";;
168          rhel5) target="2.6-rhel5";;
169         sles10) target="2.6-sles10";;
170         sles11) target="2.6-sles11";;
171             *) fatal 1 "I don't know what distro $distro is.\nEither update autodetect_target() or use the --target argument.";;
172     esac
173
174     echo ${target}
175     return 0
176
177 }