Whamcloud - gitweb
LU-2683 lov: release all locks in closure to release sublock
[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 wanted_kernel="${lnxmaj}${lnxmin}-${lnxrel}"
62     local kernel_rpms=$(find_linux_rpm "$prefix" "$pathtorpms")
63     # call a distro specific hook, if available
64     if type -p find_linux_rpms-$DISTRO; then
65         local rpm
66         if rpm=$(find_linux_rpms-$DISTRO "$prefix" "$wanted_kernel" "$pathtorpms"); then
67             kernel_rpms="$kernel_rpms $rpm"
68         else
69             return 255
70         fi
71     fi
72
73     echo "$kernel_rpms"
74     return 0
75
76 }
77
78 # a noop function which can be overridden by a distro method implementation
79 resolve_arch() {
80     local arch="$1"
81
82     echo "$arch"
83 }
84
85 # XXX this needs to be re-written as a wrapper around find_rpm
86 #     or just gotten rid of.  :-)
87 find_linux_rpm() {
88     local prefix="$1"
89     local pathtorpms=${2:-"${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}/${TARGET_ARCH}"}
90
91     local found_rpm=""
92     local wanted_kernel="${lnxmaj}${lnxmin}-${lnxrel}"
93     local ret=1
94     if [ -d "$pathtorpms" ]; then
95         local rpm
96         for rpm in $(ls ${pathtorpms}/*.$(resolve_arch $TARGET_ARCH $PATCHLESS).rpm); do
97             if rpm -q --provides -p "$rpm" 2>&$STDOUT | grep -q "kernel${prefix} = $wanted_kernel" 2>&$STDOUT; then
98                 found_rpm="$rpm"
99                 ret=0
100                 break
101             fi
102         done
103     else
104         mkdir -p "$pathtorpms"
105     fi
106     # see above "XXX"
107     #     [ -f "$found_rpm" ] && break
108     # done
109     if [ -z "$found_rpm" ]; then
110         # see if there is a distro specific way of getting the RPM
111         if type -p find_linux_rpm-$DISTRO; then
112             if found_rpm=$(find_linux_rpm-$DISTRO "$prefix" "$wanted_kernel" "$pathtorpms"); then
113                 found_rpm="${pathtorpms}/$found_rpm"
114                 ret=0
115             else
116                 ret=${PIPESTATUS[0]}
117             fi
118         fi
119     fi
120
121     echo "$found_rpm"
122     return $ret
123
124 }
125
126 # autodetect used Distro
127 autodetect_distro() {
128
129     local name
130     local version
131
132     if which lsb_release >/dev/null 2>&1; then
133         name="$(lsb_release -s -i)"
134         version="$(lsb_release -s -r)"
135         case "$name" in
136             "EnterpriseEnterpriseServer")
137                 name="oel"
138                 version="${version%%.*}"
139                 ;;
140             "RedHatEnterpriseServer" | "ScientificSL" | "CentOS")
141                 name="rhel"
142                 version="${version%%.*}"
143                 ;;
144             "SUSE LINUX")
145                 name="sles"
146                 ;;
147             "Fedora")
148                 name="fc"
149                 ;;
150             *)
151                 fatal 1 "I don't know what distro name $name and version $version is.\nEither update autodetect_distro() or use the --distro argument."
152                 ;;
153         esac
154     else
155         echo "You really ought to install lsb_release for accurate distro identification"
156         # try some heuristics
157         if [ -f /etc/SuSE-release ]; then
158             name=sles
159             version=$(grep ^VERSION /etc/SuSE-release)
160             version=${version#*= }
161         elif [ -f /etc/redhat-release ]; then
162             #name=$(head -1 /etc/redhat-release)
163             name=rhel
164             version=$(echo "$distroname" |
165                       sed -e 's/^[^0-9.]*//g' | sed -e 's/[ \.].*//')
166         fi
167         if [ -z "$name" -o -z "$version" ]; then
168             fatal 1 "I don't know how to determine distro type/version.\nEither update autodetect_distro() or use the --distro argument."
169         fi
170     fi
171
172     echo ${name}${version}
173     return 0
174
175 }
176
177 # autodetect target
178 autodetect_target() {
179     local distro="$1"
180
181     local target=""
182     case ${distro} in
183           oel5) target="2.6-oel5";;
184          rhel5) target="2.6-rhel5";;
185          rhel6) target="2.6-rhel6";;
186         sles10) target="2.6-sles10";;
187         sles11) target="$(uname -r | cut -d . -f 1,2)-sles11";;
188           fc15) target="2.6-fc15";;
189             *) fatal 1 "I don't know what distro $distro is.\nEither update autodetect_target() or use the --target argument.";;
190     esac
191
192     echo ${target}
193     return 0
194
195 }