# lustre.spec

# rpmbuild --with/--without parameters
%bcond_without servers
%bcond_without lustre_tests
%bcond_with zfs

# parameter sanity checks
%if %{without servers} && %{with zfs}
  %{error:"with zfs" is not compatible with "without servers"}
%endif

%{!?version: %global version @VERSION@}

# as an alternative to this implementation we could simply "make -C $kdir kernelversion"
%{!?kversion: %global kversion %(if test -s %kobjdir/include/linux/utsrelease.h ; then LINUXRELEASEHEADER=utsrelease.h; else LINUXRELEASEHEADER=version.h; fi; sed -ne '/^#define UTS_RELEASE/s/.*"\\(.*\\)"$/\\1/p' %kobjdir/include/linux/$LINUXRELEASEHEADER)}

%{!?downstream_release: %global downstream_release "@DOWNSTREAM_RELEASE@"}

%define buildid %(if [ -n "@BUILDID@" ]; then echo "_@BUILDID@"; fi)

%{!?myrelease: %global myrelease %(if [ -n "%downstream_release" ]; then echo -n "%{downstream_release}_"; fi; echo %kversion | tr '-' '_')}

# always append the buildid, even when the caller defines %release
%define fullrelease %{myrelease}%{buildid}

%if %{undefined lustre_name}
  %if %{with servers}
    %global lustre_name lustre
  %else
    %global lustre_name lustre-client
%endif

# in order to get kernel symset and/or kernel module dependencies into
# the RPM, in order to support weak-modules, the internal dependency gen-
# erator needs to be disabled
# this is done with (reduce the double % down to a single %):
#
# %%global _use_internal_dependency_generator 0
#
# on SLES10, /usr/lib/rpm/macros already sets this, so no harm in also
# defining it here (until Suse changes their mind)
#
# on RHEL5, however, we do need to explicitly disable the internal dep-
# endency generator and allow the external one be used
# but since RedHat's kABI is only a subset of the total kernel ABI, it
# doesn't include all of the symbols we (or OFED for that matter) need
# until RedHat includes all of the symbols we need in their symsets we
# cannot support weak-modules
# we did e-mail the maintainer of all of this stuff @redhat but got no
# response from them
#%%global _use_internal_dependency_generator 0

# RHEL 6.x / CHAOS 5.x
%if 0%{?el6}
  %define klnk %{_usrsrc}/kernels/*/include/config
  %{!?kver: %global kver %((echo ""; %{__cat} %{klnk}/kernel.release 2>/dev/null) | tail -1)}
  %{!?kdir: %global kdir %(dir=$(echo "%configure_args" | sed -ne 's/.*--with-linux=\\([^ ][^ ]*\\).*$/\\1/p'); if [ -n "$dir" ]; then echo "$dir"; else if [ -n "%kver" ]; then kversion="%kver"; else kversion="$(uname -r)"; fi; echo "%{_usrsrc}/kernels/$kversion"; fi)}
  %{!?kobjdir: %global kobjdir %kdir}
  %if %{undefined buildrequires}
    %if %{with servers}
      %define br_arg2 kernel-debuginfo-common-%{_target_cpu}
    %endif
    %global buildrequires kernel-devel %{?br_arg2}
  %endif
  %if %{undefined modules_requires}
    # for those uses that don't want the -smp/-bigsmp
    # (or the .arch) on the end of %kversion
    %define krequires %(bash -c "echo %{kversion} | sed -e 's/\.x86_64$//' -e 's/\.i[3456]86$//' -e 's/-smp$//' -e 's/-bigsmp$//' -e 's/-ppc64$//' -e 's/-default$//'")
    %global modules_requires kernel=%{krequires}
  %endif
%endif

# SLES
%if %{defined suse_version}
  %if %{undefined modules_requires}
    %define flavor %(bash -c "echo %{kversion} | sed -e 's/^.*-//'")
    %global modules_requires kernel-%{flavor}
  %endif
%endif

# Other platforms
%{!?kver: %global kver ""}
%{!?kdir: %global kdir %(dir=$(echo "%configure_args" | sed -ne 's/.*--with-linux=\\([^ ][^ ]*\\).*$/\\1/p'); if [ -n "$dir" ]; then echo "$dir"; else if [ -n "%kver" ]; then kversion="%kver"; else kversion="$(uname -r)"; fi; echo "/lib/modules/$kversion/source"; fi)}
%{!?kobjdir: %global kobjdir %(dir=$(echo "%configure_args" | sed -ne 's/.*--with-linux-obj=\\([^ ][^ ]*\\).*$/\\1/p'); if [ -n "$dir" ]; then echo "$dir"; else if [ -n "%kver" ]; then kversion="%kver"; else kversion="$(uname -r)"; fi; if [ "%kdir" = "/lib/modules/$kversion/source" ]; then echo "/lib/modules/$kversion/build"; else echo "%kdir"; fi; fi)}


Summary: Lustre File System
Name: %{lustre_name}
Version: %{version}
Release: %{fullrelease}
License: GPL
Group: Utilities/System
Source: lustre-%{version}.tar.gz
URL: http://wiki.whamcloud.com/
BuildRoot: %{_tmppath}/lustre-%{version}-root
Obsoletes: lustre-lite, lustre-lite-utils, lustre-ldap nfs-utils-lustre
Provides: lustre-lite = %{version}, lustre-lite-utils = %{version}
Requires: %{name}-modules = %{version}
BuildRequires: %{?buildrequires}
%if %{with zfs}
BuildRequires: spl-modules-devel zfs-modules-devel zfs-devel
%endif

%description
Userspace tools and files for the Lustre file system.

%package modules
Summary: Kernel Lustre modules for Linux %{kversion}
Requires: %{?modules_requires}
%if %{with servers}
Requires: lustre-backend-fs
%endif
%if %{with zfs}
Requires: spl-modules zfs-modules spl zfs
%endif
Group: Development/Kernel

%description modules
Lustre file system, server and network drivers for Linux %{kversion}.

%package source
Summary: Object-Based Disk storage driver source
Group: Development/Kernel

%description source
Lustre sources for further development

%package tests
Summary: Lustre testing framework
Group: Development/Kernel
Provides: %{name}-tests = %{version}
Requires: %{name} = %{version}, %{name}-modules = %{version}

%description tests
This package contains a set of test binaries and scripts that are intended
to be used by the Lustre testing framework.

%if 0%{?suse_version}
%debug_package
%endif
%prep
%setup -qn lustre-%{version}
ln lustre/ChangeLog ChangeLog-lustre
ln lnet/ChangeLog ChangeLog-lnet

%build
# if RPM_BUILD_NCPUS unset, set it
if [ -z "$RPM_BUILD_NCPUS" ] ; then
    RPM_BUILD_NCPUS=$(egrep -c "^cpu[0-9]+" /proc/stat 2>/dev/null || echo 0 :)
    if [ $RPM_BUILD_NCPUS -eq 0 ] ; then
        RPM_BUILD_NCPUS=1
    fi
    if [ $RPM_BUILD_NCPUS -gt 8 ] ; then
        RPM_BUILD_NCPUS=8
    fi
fi

rm -rf $RPM_BUILD_ROOT

# Set an explicit path to our Linux tree, if we can.
cd $RPM_BUILD_DIR/lustre-%{version}
# override %optflags so that the vendor's overzealous flags don't create
# build failures
%define optflags -g -O2 -Werror
%if %{with lustre_tests}
%define conf_tests --enable-tests --enable-liblustre-tests
%else
%define conf_tests --disable-tests --disable-liblustre-tests
%endif

CONFIGURE_ARGS="--with-release=%release %{?conf_tests} %{?conf_disable_server} %{?configure_args}"
%endif

# if %%kdir was given, make sure it's not in the configure arguments
if [ -n "%kdir" ]; then
	CONFIGURE_ARGS=$(echo $CONFIGURE_ARGS | sed -e 's/"\?--with-linux=[^ ][^ ]* \?//')
fi
# ditto for %%kobjdir
if [ -n "%kobjdir" ]; then
	CONFIGURE_ARGS=$(echo $CONFIGURE_ARGS | sed -e 's/"\?--with-linux-obj=[^ ][^ ]* \?//')
fi

# we need to eval "configure" because $CONFIGURE_ARGS could have a quoted
# string in it which we don't want word splitted by the shell
%define eval_configure %(echo '%configure' | sed -e 's#\./configure#eval ./configure#')

%eval_configure \
	%{?kdir: --with-linux=%kdir} %{?kobjdir: --with-linux-obj=%kobjdir} \
	$CONFIGURE_ARGS
make -j $RPM_BUILD_NCPUS -s

%install
make install DESTDIR=$RPM_BUILD_ROOT
# hack to avoid changing the libsysio code for "make install"
rm -f $RPM_BUILD_ROOT%{_libdir}/libsysio.a
# Remove ldiskfs module(s) - they are packaged by the ldiskfs .spec.
rm -rf $RPM_BUILD_ROOT/lib/modules/%{kversion}/updates/kernel/fs/lustre-ldiskfs

# hack to include the llog_test module in lustre-tests
llog_base=$RPM_BUILD_DIR/lustre-%{version}/lustre/obdclass/llog_test
if [ -e ${llog_base}.ko ]; then
  cp ${llog_base}.ko $RPM_BUILD_ROOT/lib/modules/%{kversion}/updates/kernel/fs/lustre
elif [ -e ${llog_base}.o ]; then
  cp ${llog_base}.o $RPM_BUILD_ROOT/lib/modules/%{kversion}/updates/kernel/fs/lustre
fi

# Create the pristine source directory.
cd $RPM_BUILD_DIR/lustre-%{version}
mkdir -p $RPM_BUILD_ROOT/usr/src
rm -f lustre-source
ln -s $RPM_BUILD_ROOT/usr/src lustre-source
make distdir distdir=lustre-source/lustre-%{version}
chmod -R go-w lustre-source/lustre-%{version}

cat >lustre.files <<EOF
%attr(-, root, root) /sbin/mount.lustre
%attr(-, root, root) /usr/sbin/*
%attr(-, root, root) /usr/bin/*

%attr(-, root, root) /usr/share/lustre

%attr(-, root, root) %{_libdir}/libptlctl.a
%attr(-, root, root) %{_libdir}/liblustreapi.a
%attr(-, root, root) /usr/include/lustre
%attr(-, root, root) /usr/include/libcfs

%attr(-, root, root) %{_mandir}/man?/*

%attr(-, root, root) %{_libexecdir}/lustre/lc_common

%attr(-, root, root) %{_sysconfdir}/udev/rules.d/99-lustre.rules

%attr(-, root, root) %{_sysconfdir}/init.d/lnet
%attr(-, root, root) %{_sysconfdir}/init.d/lustre
%attr(-, root, root) %{_sysconfdir}/ldev.conf
%attr(-, root, root) %{_sysconfdir}/sysconfig/lustre
%attr(-, root, root) %{_libexecdir}/lustre/haconfig
%attr(-, root, root) %{_sysconfdir}/ha.d/resource.d/Lustre
EOF

if [ -f $RPM_BUILD_ROOT%{_libdir}/libcfsutil.a ] ; then
  echo '%attr(-, root, root) %{_libdir}/libcfsutil.a' >>lustre.files
fi

if [ -f $RPM_BUILD_ROOT%{_libdir}/liblustre.so ] ; then
  echo '%attr(-, root, root) %{_libdir}/liblustre.a' >>lustre.files
  echo '%attr(-, root, root) %{_libdir}/liblustre.so' >>lustre.files
fi

if [ -f $RPM_BUILD_DIR/lustre-%{version}/lustre/utils/libiam.a ] ; then
  echo '%attr(-, root, root) %{_libdir}/libiam.a' >>lustre.files
fi

if [ -d $RPM_BUILD_ROOT%{_libdir}/lustre/snmp ] ; then
  echo '%attr(-, root, root) %{_libdir}/lustre/snmp' >>lustre.files
  echo '%attr(-, root, root) %{_datadir}/lustre/snmp/mibs' >>lustre.files
fi

# Have universal lustre headers 
if [ -f $RPM_BUILD_DIR/lustre-%{version}/lustre/include/lustre/lustre_idl.h ] ; then
  echo '%attr(-, root, root) /usr/include/linux/lustre_user.h' >>lustre.files
else
  echo '%attr(-, root, root) /usr/include/linux/lustre_idl.h' >>lustre.files
fi

if [ -f $RPM_BUILD_DIR/lustre-%{version}/lustre/include/linux/lustre_types.h ] ; then
  echo '%attr(-, root, root) /usr/include/linux/lustre_types.h' >>lustre.files
fi

%if %{with lustre_tests}
echo '%attr(-, root, root) %{_libdir}/lustre/tests/*' >lustre-tests.files
echo '%attr(-, root, root) /lib/modules/%{kversion}/updates/kernel/fs/lustre/llog_test.*' >>lustre-tests.files
modules_excludes="|llog_test"
if [ -d $RPM_BUILD_ROOT%{_libdir}/lustre/liblustre/tests ] ; then
  echo '%attr(-, root, root) %{_libdir}/lustre/liblustre/tests/*' >>lustre-tests.files
fi
%endif

pushd $RPM_BUILD_ROOT >/dev/null
> $RPM_BUILD_DIR/lustre-%{version}/lustre-modules.files
find lib/modules/%{kversion}/updates -type f | awk "!/(ZZZZZZZZZZ$modules_excludes)/ {print \"%attr(-, root, root) /\"\$0}" >>$RPM_BUILD_DIR/lustre-%{version}/lustre-modules.files
popd >/dev/null

%files -f lustre.files

%files modules -f lustre-modules.files
%attr(-, root, root) %doc COPYING
%attr(-, root, root) %doc ChangeLog-lustre
%attr(-, root, root) %doc ChangeLog-lnet

%files source
%attr(-, root, root) /usr/src/lustre-%{version}

# uncomment these lines to enable deps packages
# %files deps-sles
# %files deps-rhel

%if %{with lustre_tests}
%files tests -f lustre-tests.files
%endif

%post modules
if [ -f /boot/System.map-%{kversion} ]; then
	depmod -ae -F /boot/System.map-%{kversion} %{kversion} || exit 0
else
	depmod -ae %{kversion} || exit 0
fi

MODULES_RPM_NAME=$(rpm -q %{name}-modules | grep "%{version}-%{release}")
# "weak modules" support
# Suse
if [ -x /usr/lib/module-init-tools/weak-modules ]; then
    rpm -ql $MODULES_RPM_NAME | grep '\.ko$' |
        /usr/lib/module-init-tools/weak-modules --add-modules
fi
# RedHat
if [ -x /sbin/weak-modules ]; then
    rpm -ql $MODULES_RPM_NAME | grep '\.ko$' |
        /sbin/weak-modules --add-modules
fi

# If the kernel was built to reject unsupported modules (e.g. not a Lustre
# kernel), and this hasn't been overridden in /etc/modprobe.d yet, then
# print a warning so that users are aware of this issue.
if sysctl kernel.unsupported >/dev/null 2>&1 &&
   [ "$(sysctl -n kernel.unsupported 2>/dev/null)" = "0" ] &&
    ! modprobe -c | grep -q "^allow_unsupported_modules[ \t]1" ; then
     echo "
     warning: the Lustre modules are not supported by Novell. To use Lustre
              on this system, you should put

     allow_unsupported_modules 1

     into /etc/modprobe.d/unsupported_modules"
fi

%preun modules
MODULES_RPM_NAME=$(rpm -q %{name}-modules | grep "%{version}-%{release}")
rpm -ql $MODULES_RPM_NAME | grep '\.ko$' > /var/run/%{name}-modules || true

%postun modules
if [ -f /boot/System.map-%{kversion} ]; then
	depmod -ae -F /boot/System.map-%{kversion} %{kversion} || exit 0
else
	depmod -ae %{kversion} || exit 0
fi

# "weak modules" support
# Suse
if [ -x /usr/lib/module-init-tools/weak-modules ]; then
    cat /var/run/%{name}-modules |
        /usr/lib/module-init-tools/weak-modules --remove-modules
fi
# RedHat
if [ -x /sbin/weak-modules ]; then
    cat /var/run/%{name}-modules |
        /sbin/weak-modules --remove-modules
fi
rm /var/run/%{name}-modules

%if %{with lustre_tests}
%post tests
if [ -f /boot/System.map-%{kversion} ]; then
	depmod -ae -F /boot/System.map-%{kversion} %{kversion} || exit 0
else
	depmod -ae %{kversion} || exit 0
fi

%postun tests
if [ -f /boot/System.map-%{kversion} ]; then
	depmod -ae -F /boot/System.map-%{kversion} %{kversion} || exit 0
else
	depmod -ae %{kversion} || exit 0
fi
%endif

%clean
rm -rf $RPM_BUILD_ROOT
