From 63520d0a8089ba4929fc6767442e85bde4a1ccad Mon Sep 17 00:00:00 2001 From: "Brian J. Murrell" Date: Thu, 18 Nov 2010 02:27:19 +0800 Subject: [PATCH 1/1] b=24091 find_linux_rpms utility Write a utility to find the the kernel "devel" package needed. Restructure some code to facilitate reuse of existing code to use in the utility. i=cliff i=minh --- build/autoMakefile.am.toplevel | 3 +- build/find_linux_rpms | 34 ++++++++ build/funcs.sh | 177 +++++++++++++++++++++++++++++++++++++++++ build/lbuild | 152 +---------------------------------- 4 files changed, 214 insertions(+), 152 deletions(-) create mode 100644 build/find_linux_rpms create mode 100644 build/funcs.sh diff --git a/build/autoMakefile.am.toplevel b/build/autoMakefile.am.toplevel index 185b06f..300e76f 100644 --- a/build/autoMakefile.am.toplevel +++ b/build/autoMakefile.am.toplevel @@ -100,7 +100,8 @@ EXTRA_DIST = @PACKAGE_TARNAME@.spec \ build/autoconf/lustre-build-linux.m4 \ build/autoconf/lustre-build-darwin.m4 \ build/autoconf/lustre-build.m4 build/rdac_spec \ - build/mptlinux.spec.patch build/patches + build/mptlinux.spec.patch build/patches \ + build/funcs.sh build/find_linux_rpms rpms-real: @PACKAGE_TARNAME@.spec dist Makefile CONFIGURE_ARGS=$$(echo $$(eval echo $(ac_configure_args)) | sed -re 's/--(en|dis)able-tests//'); \ diff --git a/build/find_linux_rpms b/build/find_linux_rpms new file mode 100644 index 0000000..94fd400 --- /dev/null +++ b/build/find_linux_rpms @@ -0,0 +1,34 @@ +#!/bin/bash + +# this is an alternative FD for stdout, to be used especially when we are +# taking stdout from a function as it's return value. i.e. foo=$(bar) +# this is a workaround until a version of bash where we can put xtrace +# on a specific FD +#exec 3>&1; STDOUT=3 +STDOUT=2 + +. ${0%/find_linux_rpms}/funcs.sh + +TOPDIR=$PWD + +# same as lbuild's --kernelrpm switch +KERNELRPMSDIR="$1" + +DISTRO=$(autodetect_distro) + +source ${0%/*}/lbuild-$DISTRO + +TARGET_ARCH="$(uname -m)" +TARGET_ARCHS="$(uname -m)" +TARGET_ARCHS_ALL="$(uname -m)" + +# need lnxmaj from the target file +TARGET=$(autodetect_target "$DISTRO") +TARGET_FILE="$TOPDIR/lustre/kernel_patches/targets/$TARGET.target" +. "$TARGET_FILE" + +if ! kernelrpm=$(find_linux_rpms "-$DEVEL_KERNEL_TYPE" "$KERNELRPMSDIR"); then + fatal 1 "Could not find the kernel-$DEVEL_KERNEL_TYPE RPM in ${KERNELRPMSDIR}" +fi + +echo "$kernelrpm" diff --git a/build/funcs.sh b/build/funcs.sh new file mode 100644 index 0000000..acb1c51 --- /dev/null +++ b/build/funcs.sh @@ -0,0 +1,177 @@ +cleanup() { + + true +} + +error() { + local msg="$1" + + [ -n "$msg" ] && echo -e "\n${0##*/}: $msg" >&$STDOUT + +} + +fatal() { + + cleanup + error "$2" + exit $1 + +} + +# +# in a given directory, find the first rpm matching given requirements +# +find_rpm() { + local dir="$1" + local match_type="$2" + local match="$3" + + pushd "$dir" > /dev/null || \ + fatal 1 "Unable to chdir to directory \"$dir\" in find_rpm()" + + local file + for file in $(ls *.rpm); do + if [ ! -f "$file" ]; then + continue + fi + case "$match_type" in + provides) + # match is any valid ERE (i.e. given to egrep) match + if rpm -q --provides -p "$file" 2>&$STDOUT | egrep "$match" >&$STDOUT; then + echo "$file" + popd >/dev/null + return 0 + fi + ;; + *) + popd >/dev/null + fatal 1 "Unknown match type \"$match_type\" given to find_rpm()" + ;; + esac + done + + popd >/dev/null + return 1 +} + +find_linux_rpms() { + local prefix="$1" + local pathtorpms=${2:-"${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}/${TARGET_ARCH}"} + + local kernel_rpms=$(find_linux_rpm "$prefix" "$pathtorpms") + # call a distro specific hook, if available + if type -p find_linux_rpms-$DISTRO; then + local rpm + if rpm=$(find_linux_rpms-$DISTRO "$prefix" "$pathtorpms"); then + kernel_rpms="$kernel_rpms $rpm" + else + return 255 + fi + fi + + echo "$kernel_rpms" + return 0 + +} + +# a noop function which can be overridden by a distro method implementation +resolve_arch() { + local arch="$1" + + echo "$arch" +} + +# XXX this needs to be re-written as a wrapper around find_rpm +# or just gotten rid of. :-) +find_linux_rpm() { + local prefix="$1" + local pathtorpms=${2:-"${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}/${TARGET_ARCH}"} + + [ -d $pathtorpms ] || return 255 + + local kernelbinaryrpm rpmfile + local wanted_kernel="${lnxmaj}${lnxmin}-${lnxrel}" + + local arch ret=1 + local found_rpm="" rpm + for rpm in $(ls ${pathtorpms}/*.$(resolve_arch $TARGET_ARCH $PATCHLESS).rpm); do + if rpm -q --provides -p "$rpm" 2>&$STDOUT | grep -q "kernel${prefix} = $wanted_kernel" 2>&$STDOUT; then + found_rpm="$rpm" + ret=0 + break + fi + done + # see above "XXX" + # [ -f "$found_rpm" ] && break + # done + + echo "$found_rpm" + return $ret + +} + +# autodetect used Distro +autodetect_distro() { + + local name + local version + + if which lsb_release >/dev/null 2>&1; then + name="$(lsb_release -s -i)" + version="$(lsb_release -s -r)" + case "$name" in + "EnterpriseEnterpriseServer") + name="oel" + version="${version%%.*}" + ;; + "RedHatEnterpriseServer" | "ScientificSL") + name="rhel" + version="${version%%.*}" + ;; + "SUSE LINUX") + name="sles" + ;; + *) + fatal 1 "I don't know what distro name $name and version $version is.\nEither update autodetect_distro() or use the --distro argument." + ;; + esac + else + echo "You really ought to install lsb_release for accurate distro identification" + # try some heuristics + if [ -f /etc/SuSE-release ]; then + name=sles + version=$(grep ^VERSION /etc/SuSE-release) + version=${version#*= } + elif [ -f /etc/redhat-release ]; then + #name=$(head -1 /etc/redhat-release) + name=rhel + version=$(echo "$distroname" | + sed -e 's/^[^0-9.]*//g' | sed -e 's/[ \.].*//') + fi + if [ -z "$name" -o -z "$version" ]; then + fatal 1 "I don't know how to determine distro type/version.\nEither update autodetect_distro() or use the --distro argument." + fi + fi + + echo ${name}${version} + return 0 + +} + +# autodetect target +autodetect_target() { + local distro="$1" + + local target="" + case ${distro} in + oel5) target="2.6-oel5";; + rhel5) target="2.6-rhel5";; + sles10) target="2.6-sles10";; + sles11) target="2.6-sles11";; + *) fatal 1 "I don't know what distro $distro is.\nEither update autodetect_target() or use the --target argument.";; + esac + + echo ${target} + return 0 + +} diff --git a/build/lbuild b/build/lbuild index 931a216..37fe5f7 100755 --- a/build/lbuild +++ b/build/lbuild @@ -17,6 +17,7 @@ shopt -s extdebug # include the exit_traps library . ${0%/lbuild}/exit_traps.sh +. ${0%/lbuild}/funcs.sh # our children should die when we do push_exit_trap "kill -INT -$$ || true" kill_children @@ -117,26 +118,6 @@ readlink() { fi } -cleanup() { - - true -} - -error() { - local msg="$1" - - [ -n "$msg" ] && echo -e "\n${0##*/}: $msg" >&3 - -} - -fatal() { - - cleanup - error "$2" - exit $1 - -} - usage() { cat <] @@ -398,71 +379,6 @@ check_options() { } -# autodetect target -autodetect_target() { - local distro="$1" - - local target="" - case ${distro} in - oel5) target="2.6-oel5";; - rhel5) target="2.6-rhel5";; - sles10) target="2.6-sles10";; - *) fatal 1 "I don't know what distro $distro is.\nEither update autodetect_target() or use the --target argument.";; - esac - - echo ${target} - return 0 - -} - -# autodetect used Distro -autodetect_distro() { - - local name - local version - - if which lsb_release >/dev/null 2>&1; then - name="$(lsb_release -s -i)" - version="$(lsb_release -s -r)" - case "$name" in - "EnterpriseEnterpriseServer" | "ScientificSL") - name="oel" - version="${version%%.*}" - ;; - "RedHatEnterpriseServer") - name="rhel" - version="${version%%.*}" - ;; - "SUSE LINUX") - name="sles" - ;; - *) - fatal 1 "I don't know what distro name $name and version $version is.\nEither update autodetect_distro() or use the --distro argument." - ;; - esac - else - echo "You really ought to install lsb_release for accurate distro identification" - # try some heuristics - if [ -f /etc/SuSE-release ]; then - name=sles - version=$(grep ^VERSION /etc/SuSE-release) - version=${version#*= } - elif [ -f /etc/redhat-release ]; then - #name=$(head -1 /etc/redhat-release) - name=rhel - version=$(echo "$distroname" | - sed -e 's/^[^0-9.]*//g' | sed -e 's/[ \.].*//') - fi - if [ -z "$name" -o -z "$version" ]; then - fatal 1 "I don't know how to determine distro type/version.\nEither update autodetect_distro() or use the --distro argument." - fi - fi - - echo ${name}${version} - return 0 - -} - uniqify() { echo $(echo "$*" | xargs -n 1 | sort -u) @@ -1007,36 +923,6 @@ find_linux_release() { } -# XXX this needs to be re-written as a wrapper around find_rpm -# or just gotten rid of. :-) -find_linux_rpm() { - local prefix="$1" - - local pathtorpms="${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}" - [ -d $pathtorpms ] || return 255 - - local kernelbinaryrpm rpmfile - local wanted_kernel="${lnxmaj}${lnxmin}-${lnxrel}" - - local arch ret=1 - for arch in $TARGET_ARCHS_ALL; do - local found_rpm="" rpm - for rpm in ${pathtorpms}/${arch}/*.rpm; do - if rpm -q --provides -p "$rpm" 2>&3 | grep -q "kernel${prefix} = $wanted_kernel" 2>&3; then - - found_rpm="$rpm" - ret=0 - break - fi - done - [ -f "$found_rpm" ] && break - done - - echo "$found_rpm" - return $ret - -} - # unpack kernel(/source/devel) RPM # # This function and it's setting of $LINUX and $LINUXOBJ is a total hack that @@ -1269,42 +1155,6 @@ basearch() { } -# -# in a given directory, find the first rpm matching given requirements -# -find_rpm() { - local dir="$1" - local match_type="$2" - local match="$3" - - pushd "$dir" > /dev/null || \ - fatal 1 "Unable to chdir to directory \"$dir\" in find_rpm()" - - local file - for file in $(ls *.rpm); do - if [ ! -f "$file" ]; then - continue - fi - case "$match_type" in - provides) - # match is any valid ERE (i.e. given to egrep) match - if rpm -q --provides -p "$file" 2>&3 | egrep -q "$match"; then - echo "$file" - popd >/dev/null - return 0 - fi - ;; - *) - popd >/dev/null - fatal 1 "Unknown match type \"$match_type\" given to find_rpm()" - ;; - esac - done - - popd >/dev/null - return 1 -} - build_kernel_with_srpm() { local outfd=$1 -- 1.8.3.1