Whamcloud - gitweb
0ee4bb52aefa1f54203d26989005d9b6e482095d
[fs/lustre-release.git] / contrib / lbuild / funcs.sh
1 cleanup() {
2
3     true
4 }
5
6 error() {
7     local msg="$1"
8
9     if [ -n "$msg" ]; then
10         echo -e "\n${0##*/}: $msg" >&$STDOUT
11     fi
12 }
13
14 fatal() {
15     cleanup
16     error "$2"
17     exit $1
18 }
19
20 #
21 # in a given directory, find the first rpm matching given requirements
22 #
23 find_rpm() {
24     local dir="$1"
25     local match_type="$2"
26     local match="$3"
27
28     pushd "$dir" > /dev/null || \
29         fatal 1 "Unable to chdir to directory \"$dir\" in find_rpm()"
30
31     local file
32     for file in $(ls *.rpm); do
33         if [ ! -f "$file" ]; then
34             continue
35         fi
36         case "$match_type" in
37             provides)
38                 # match is any valid ERE (i.e. given to egrep) match
39                 if rpm -q --provides -p "$file" 2>&$STDOUT | egrep "$match" >&$STDOUT; then
40                     echo "$file"
41                     popd >/dev/null
42                     return 0
43                 fi
44                 ;;
45             *)
46                 popd >/dev/null
47                 fatal 1 "Unknown match type \"$match_type\" given to find_rpm()"
48                 ;;
49         esac
50     done
51
52     popd >/dev/null
53     return 1
54 }
55
56 find_linux_rpms() {
57     local prefix="$1"
58     local pathtorpms=${2:-"$KERNELRPMSBASE/$lnxmaj/$DISTROMAJ/$TARGET_ARCH"}
59
60     local wanted_kernel="${lnxmaj}${lnxmin}-${lnxrel}${rpmfix}"
61     local kernel_rpms=$(find_linux_rpm "$prefix" "$pathtorpms")
62     # call a distro specific hook, if available
63     if type -p find_linux_rpms-$DISTROMAJ; then
64         local rpm
65         if rpm=$(find_linux_rpms-$DISTROMAJ "$prefix" "$wanted_kernel" "$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/$DISTROMAJ/$TARGET_ARCH"}
89
90     local found_rpm=""
91     local wanted_kernel="${lnxmaj}${lnxmin}-${lnxrel}${rpmfix}"
92     local ret=1
93     if [ -d "$pathtorpms" ]; then
94         local rpm
95         for rpm in $(ls ${pathtorpms}/*.$(resolve_arch $TARGET_ARCH $PATCHLESS).rpm); do
96             if rpm -q --provides -p "$rpm" 2>&$STDOUT | grep -q "kernel${prefix} = $wanted_kernel" 2>&$STDOUT; then
97                 found_rpm="$rpm"
98                 ret=0
99                 break
100             fi
101         done
102     else
103         mkdir -p "$pathtorpms"
104     fi
105     # see above "XXX"
106     #     [ -f "$found_rpm" ] && break
107     # done
108     if [ -z "$found_rpm" ]; then
109         # see if there is a distro specific way of getting the RPM
110         if type -p find_linux_rpm-$DISTROMAJ; then
111             if found_rpm=$(find_linux_rpm-$DISTROMAJ "$prefix" "$wanted_kernel" "$pathtorpms"); then
112                 found_rpm="${pathtorpms}/$found_rpm"
113                 ret=0
114             else
115                 ret=${PIPESTATUS[0]}
116             fi
117         fi
118     fi
119
120     echo "$found_rpm"
121     return $ret
122
123 }
124
125 # autodetect used Distro
126 autodetect_distro() {
127
128     local name
129     local version
130
131     if which lsb_release >/dev/null 2>&1; then
132         name="$(lsb_release -s -i)"
133         version="$(lsb_release -s -r)"
134         case "$name" in
135             "EnterpriseEnterpriseServer")
136                 name="oel"
137                 ;;
138             "RedHatEnterpriseServer" | "ScientificSL" | "CentOS")
139                 name="rhel"
140                 ;;
141             "SUSE LINUX")
142                 name="sles"
143                 PATCHLEVEL=$(sed -n -e 's/^PATCHLEVEL = //p' /etc/SuSE-release)
144                 if [ "$PATCHLEVEL" -ne "0" ]; then
145                         version="${version}.$PATCHLEVEL"
146                 fi
147                 ;;
148             "Fedora")
149                 name="fc"
150                 ;;
151             *)
152                 fatal 1 "I don't know what distro name $name and version $version is.\nEither update autodetect_distro() or use the --distro argument."
153                 ;;
154         esac
155     else
156         error "You really ought to install lsb_release for accurate distro identification"
157         # try some heuristics
158         if [ -f /etc/SuSE-release ]; then
159             name=sles
160             version=$(sed -n -e 's/^VERSION = //p' /etc/SuSE-release)
161             PATCHLEVEL=$(sed -n -e 's/^PATCHLEVEL = //p' /etc/SuSE-release)
162             if [ "$PATCHLEVEL" -ne "0" ]; then
163                     version="${version}.$PATCHLEVEL"
164             fi
165         elif [ -f /etc/redhat-release ]; then
166             #name=$(head -1 /etc/redhat-release)
167             name=rhel
168             version=$(echo "$distroname" |
169                       sed -e 's/^[^0-9.]*//g' | sed -e 's/[ ].*//')
170         fi
171         if [ -z "$name" -o -z "$version" ]; then
172             fatal 1 "I don't know how to determine distro type/version.\nEither update autodetect_distro() or use the --distro argument."
173         fi
174     fi
175
176     echo ${name}-${version}
177     return 0
178
179 }
180
181 # autodetect target
182 autodetect_target() {
183     local distro="$1"
184
185     local target=""
186     case ${distro} in
187           oel5*)  target="2.6-oel5";;
188          rhel5*)  target="2.6-rhel5";;
189          rhel6.7) target="2.6-rhel6.7";;
190          rhel6.6) target="2.6-rhel6.6";;
191          rhel6*)  target="2.6-rhel6";;
192          rhel7*)  target="3.10-rhel7";;
193         sles10*)  target="2.6-sles10";;
194         sles11.4) target="$(uname -r | cut -d . -f 1,2)-sles11sp4";;
195         sles11.3) target="$(uname -r | cut -d . -f 1,2)-sles11sp3";;
196         sles11*)  target="$(uname -r | cut -d . -f 1,2)-sles11";;
197         sles12*)  target="$(uname -r | cut -d . -f 1,2)-sles12";;
198           fc15)   target="2.6-fc15";;
199           fc18)   target="3.x-fc18";;
200              *)   fatal 1 "I don't know what distro $distro is.\nEither update autodetect_target() or use the --target argument.";;
201     esac
202
203     echo ${target}
204     return 0
205
206 }