Summary: The Linux kernel (the core of the Linux operating system)

# normal/debug/debuginfo
%define buildnormal 1
%define builddebug 1
%define builddebuginfo 0

# kernels we want to build
%define buildup 1
%define buildsmp 1
%define buildenterprise 1
%define buildentnosplit 1

# versions
%define kversion 2.6.9
%define krelease 023stab054
%define ksubrelease 1


%define rpmversion %{kversion}
%define release %{krelease}.%{ksubrelease}

%define KVERREL %{PACKAGE_VERSION}-%{PACKAGE_RELEASE}

# groups of related archs
%define all_x86 i586 i686

# Override generic defaults with per-arch defaults

# Second, per-architecture exclusions (ifarch)

%ifarch %{all_x86}
%define all_arch_configs $RPM_SOURCE_DIR/kernel-%{kversion}-i?86*.config.ovz
%define image_install_path boot
%endif

%ifarch x86_64
%define all_arch_configs $RPM_SOURCE_DIR/kernel-%{kversion}-x86_64*.config.ovz
%define buildenterprise 0
%define buildentnosplit 0
%define image_install_path boot
%endif

%ifarch ia64
%define all_arch_configs $RPM_SOURCE_DIR/kernel-%{kversion}-ia64.config.ovz
%define buildsmp 0
%define buildenterprise 0
%define buildentnosplit 0
%define image_install_path boot/efi
%endif

%define _unpackaged_files_terminate_build 0

#
# Three sets of minimum package version requirements in the form of Conflicts:
# to versions below the minimum
#

#
# First the general kernel 2.6 required versions as per
# Documentation/Changes
#
%define kernel_dot_org_conflicts  ppp <= 2.3.15, pcmcia-cs <= 3.1.20, isdn4k-utils <= 3.0, mount < 2.10r-5, nfs-utils < 1.0.3, e2fsprogs < 1.29, util-linux < 2.10, jfsutils < 1.0.14, reiserfsprogs < 3.6.3, xfsprogs < 2.1.0, procps < 2.0.9, oprofile < 0.5.3

#
# Then a series of requirements that are distribution specific, either
# because we add patches for something, or the older versions have
# problems with the newer kernel or lack certain things that make
# integration in the distro harder than needed.
#
%define package_conflicts  cipe < 1.4.5, tux < 2.1.0, kudzu <= 0.92, dev < 3.2-7, iptables < 1.2.5-3, bcm5820 < 1.81, nvidia-rh72 <= 1.0

#
# Several packages had bugs in them that became obvious when the NPTL
# threading code got integrated.
#
%define nptl_conflicts SysVinit < 2.84-13, pam < 0.75-48, vixie-cron < 3.0.1-73, privoxy < 3.0.0-8, spamassassin < 2.44-4.8.x,  cups < 1.1.17-13

#
# Packages that need to be installed before the kernel is, because the %post
# scripts use them.
#
%define kernel_prereq  fileutils, module-init-tools, initscripts >= 5.83, mkinitrd >= 3.5.5

Name: ovzkernel
Group: System Environment/Kernel
License: GPLv2
Version: %{rpmversion}
Release: %{release}
ExclusiveArch: noarch %{all_x86} x86_64 ia64
ExclusiveOS: Linux
Provides: kernel = %{version}
Provides: kernel-drm = 4.3.0
Provides: vzkernel = %{KVERREL}
Provides: vzquotamod
Provides: vzeventmod
Prereq: %{kernel_prereq}
Conflicts: %{kernel_dot_org_conflicts}
Conflicts: %{package_conflicts}
Conflicts: %{nptl_conflicts}
# We can't let RPM do the dependencies automatic because it'll then pick up
# a correct but undesirable perl dependency from the module headers which
# isn't required for the kernel proper to function
AutoReqProv: no

#
# List the packages used during the kernel build
#
BuildPreReq: module-init-tools, patch >= 2.5.4, bash >= 2.03, sh-utils, tar
BuildPreReq: bzip2, findutils, gzip, m4, perl, make >= 3.78, gnupg
BuildRequires: gcc >= 2.96-98, binutils >= 2.12
BuildConflicts: rhbuildsys(DiskFree) < 500Mb

Source0: ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-%{kversion}.tar.bz2
Source10: redhat-patches.tar.bz2

Source20: kernel-%{kversion}-i686.config.ovz
Source21: kernel-%{kversion}-i686-smp.config.ovz
Source22: kernel-%{kversion}-i686-enterprise.config.ovz
Source23: kernel-%{kversion}-x86_64.config.ovz
Source24: kernel-%{kversion}-x86_64-smp.config.ovz
Source25: kernel-%{kversion}-ia64.config.ovz
Source26: kernel-%{kversion}-i686-entnosplit.config.ovz

%if %{!?_without_source:0}%{?_without_source:1}
NoSource: 0
%endif

# ----------------------------------------------------------------------------
# List of patches
# ----------------------------------------------------------------------------
Patch100: patch-%{krelease}-core

#---1000------ IA64 --------------------------

#---2000------ x86_64 ------------------------

#---5000------ other patches ----------------
Patch5000: diff-rh-update-version
Patch5100: linux-2.6.8.1-drbd-0.7.19.patch
Patch5101: linux-2.6.8.1-drbd-0.7.19-0.7.20.patch
Patch5102: linux-2.6.9-drbd-0.7.20-0.7.22.patch
Patch5125: linux-2.6.9-3w_9xxx-2.26.05.007-07.003.patch
#Patch5126:
Patch5127: linux-2.6.9-sk98lin-8.31.2.3.patch
#Patch5128:
#Patch5129:
#Patch5130:
Patch5131: linux-2.6.9-dell_rbu-0.9.patch
Patch5132: linux-2.6.9-aoe-14.patch
Patch5133: linux-2.6.9-dpt_i2o-2.5.0-2426.patch
Patch5134: linux-2.6.9-i2o-1.325.patch
#Patch5135:
#Patch5136:
#Patch5137:
#Patch5138:

Patch5210: diff-drv-megaraid-entropy-20051025
#Patch5211:
#Patch5212:
Patch5224: diff-cciss-timeout-20060228
#Patch5232
Patch5233: diff-scsi-mpt-fusion-sleeps
Patch5234: diff-drv-adp94xx-freeze-20060906
Patch5235: diff-wrn-implicit-funcs-20060906
#Patch5236:
Patch5237: diff-ide-taskfile-gcc4-20061106
Patch5238: diff-i2c-gcc4-compilefix-20051103
Patch5239: diff-acenic-gcc4-compile-20061107
Patch5240: diff-adp94xx-gcc4-compile-20061107
Patch5241: diff-adp94xx-gcc4-compile2-20061107
Patch5242: diff-adp94xx-gcc4-compile3-20061107
Patch5243: diff-ia64-sn-gcc4-compile-20061108
Patch5244: linux-2.6.9-serverworks-return.patch
Patch5245: diff-gfs-20061221
Patch5246: diff-gfs-cman-20061221
Patch5247: diff-gfs-dlm-20061221
Patch5248: diff-gfs-invalinodes-20061220
Patch5249: diff-gfs-permission-20061220
Patch5250: diff-cfq-timeslice-20070129
#Patch5251:
#Patch5252:
Patch5253: diff-i2o-msgleak-10070423
Patch5254: diff-i2o-msgget-errh-10070423
Patch5255: diff-i2o-cfg-passthru-20070423
Patch5256: diff-i2o-proc-perms-20060304
Patch5257: diff-md-bio-init-20070504

Patch5260: diff-i2o-procread-20070510
#Patch5261:

# CPT support for TUN/TAP
Patch5300: diff-tuntap-upgrade-1.5-1.6-20071106
Patch5301: diff-cpt-object-type-20071106
Patch5302: diff-cpt-tun-tap-20071106
Patch5303: diff-tuntap-rst-fix-20071121
Patch5304: diff-cpt-tuntap-bindrst-20071123
Patch5305: diff-cpt-exports-c-20071206

# Support Containers based on Fedora 11 template
Patch5401: diff-01-warn-unsupported-syscalls
Patch5402: diff-02-vfs-lookupat-core
Patch5403: diff-03-bunch-of-xxxat-syscalls
Patch5404: diff-04-new-times-syscalls
Patch5405: diff-05-new-statat-syscall
Patch5406: diff-06-wire-new-xxxat-syscalls
Patch5407: diff-07-fd-closexec-flag
Patch5408: diff-08-sock-flag-and-accept
Patch5409: diff-09-epoll_create1-syscall
Patch5410: diff-10-dup3-syscalls
Patch5411: diff-11-pipe2-syscall
Patch5412: diff-12-wire-accept4
Patch5413: diff-13-wire-extflag-syscalls
Patch5414: diff-14-add-fstatat64-syscall
Patch5415: diff-15-wire-all-new-compat-syscalls
Patch5416: diff-ms-net-permanent-loopback-in_dev-20100205
Patch5417: diff-ve-net-permanent-loopback-in_dev-20100205-2
Patch5418: diff-ve-net-permanent-loopback-in_dev-b-20100215-2
Patch5419: diff-ms-compat-readahead-fadvise64-20100209
Patch5420: diff-ve-kernel-version-20100217
Patch5421: diff-ms-plist-20100217
Patch5422: diff-ms-rtmutex-pick-20100217
Patch5423: diff-ms-rtmutex-timeout-20100217
Patch5424: diff-ms-rtmutex-wire-20100217
Patch5425: diff-ms-futex-pi-pick-20100217
Patch5426: diff-ms-futex-pi-wire-20100217
Patch5427: diff-ms-futex-pi-compat-20100217
Patch5428: diff-ms-futex-pi-switch-20100217
Patch5429: diff-ms-robust-list-wire-20100217
Patch5430: diff-ms-kmap-atomic-user-page-20100217
Patch5431: diff-fix-ptl-locking-in-__kmap_atomic_user_page-20100225

#Patch6000:
Patch6001: diff-ubc-debug-slabinfo
Patch6002: diff-ubc-debug-slabinfo-slm-fix
Patch6003: diff-ve-net-set-execub-for-async-netlink

# Bells and whistles
#Patch10000:
# ----------------------------------------------------------------------------

BuildRoot: %{_tmppath}/kernel-%{KVERREL}-root

%description
This package contains the OpenVZ kernel. This is the Linux kernel with added
virtualization functionality.

%package debug
Summary: The Linux kernel compiled with debug parameters

Group: System Environment/Kernel
Provides: kernel = %{version}
Provides: kernel-drm = 4.3.0
Provides: vzkernel = %{KVERREL}
Provides: vzquotamod
Provides: vzeventmod
Prereq: %{kernel_prereq}
Conflicts: %{kernel_dot_org_conflicts}
Conflicts: %{package_conflicts}
Conflicts: %{nptl_conflicts}
AutoReqProv: no

%description debug
Debug kernel

%package smp
Summary: The Linux kernel compiled for SMP machines.

Group: System Environment/Kernel
Provides: kernel = %{version}
Provides: kernel-drm = 4.3.0
Provides: vzkernel = %{KVERREL}
Provides: vzquotamod
Provides: vzeventmod
Prereq: %{kernel_prereq}
Conflicts: %{kernel_dot_org_conflicts}
Conflicts: %{package_conflicts}
Conflicts: %{nptl_conflicts}
AutoReqProv: no

%description smp
This package includes a SMP version of the OpenVZ kernel. Install the
ovzkernel-smp package if your machine uses two or more CPUs or CPUs with
hyperthreading technology.


%package smp-debug
Summary: The Linux kernel compiled with debug parameters

Group: System Environment/Kernel
Provides: kernel = %{version}
Provides: kernel-drm = 4.3.0
Provides: vzkernel = %{KVERREL}
Provides: vzquotamod
Provides: vzeventmod
Prereq: %{kernel_prereq}
Conflicts: %{kernel_dot_org_conflicts}
Conflicts: %{package_conflicts}
Conflicts: %{nptl_conflicts}
AutoReqProv: no

%description smp-debug
Debug kernel

%package enterprise
Summary: The Linux kernel compiled for SMP enterprise machines.

Group: System Environment/Kernel
Provides: kernel = %{version}
Provides: kernel-drm = 4.3.0
Provides: vzkernel = %{KVERREL}
Provides: vzquotamod
Provides: vzeventmod
Prereq: %{kernel_prereq}
Conflicts: %{kernel_dot_org_conflicts}
Conflicts: %{package_conflicts}
Conflicts: %{nptl_conflicts}
AutoReqProv: no

%description enterprise
This package includes a SMP enterprise version of the OpenVZ kernel. Install
the ovzkernel-enterprise package if your machine uses two or more CPUs
(or CPUs with hyperthreading technology) and have more than 4GB RAM.

%package enterprise-debug
Summary: The Linux kernel compiled with debug parameters

Group: System Environment/Kernel
Provides: kernel = %{version}
Provides: kernel-drm = 4.3.0
Provides: vzkernel = %{KVERREL}
Provides: vzquotamod
Provides: vzeventmod
Prereq: %{kernel_prereq}
Conflicts: %{kernel_dot_org_conflicts}
Conflicts: %{package_conflicts}
Conflicts: %{nptl_conflicts}
AutoReqProv: no

%description enterprise-debug
Debug kernel

%package entnosplit
Summary: The Linux kernel compiled for SMP hugemem machines without 4GB split.

Group: System Environment/Kernel
Provides: kernel = %{version}
Provides: kernel-drm = 4.3.0
Provides: vzkernel
Provides: vzquotamod
Provides: vzeventmod
Obsoletes: ovzkernel-entnosplit = %{KVERREL}
Prereq: %{kernel_prereq}
Conflicts: %{kernel_dot_org_conflicts}
Conflicts: %{package_conflicts}
Conflicts: %{nptl_conflicts}
AutoReqProv: no

%description entnosplit
This package includes a SMP hugemem version of the Virtuozzo kernel without
4GB split.

%package entnosplit-debug
Summary: The Linux kernel compiled with debug parameters

Group: System Environment/Kernel
Provides: kernel = %{version}
Provides: kernel-drm = 4.3.0
Provides: vzkernel
Provides: vzquotamod
Provides: vzeventmod
Obsoletes: ovzkernel-entnosplit-debug = %{KVERREL}
Prereq: %{kernel_prereq}
Conflicts: %{kernel_dot_org_conflicts}
Conflicts: %{package_conflicts}
Conflicts: %{nptl_conflicts}
AutoReqProv: no

%description entnosplit-debug
Debug kernel

# ----------------------------------------------------------------------------
# prepare
# ----------------------------------------------------------------------------
%prep

%setup -q -n %{name}-%{version} -c
%setup -q -n %{name}-%{version} -D -T -a10
# apply RedHat patches first
cd redhat-patches
./apply ../linux-%{kversion}

cd ../linux-%{kversion}

# ----------------------------------------------------------------------------
# applying OpenVZ patches
# ----------------------------------------------------------------------------

%patch100 -p1

%patch5000 -p1
%patch5100 -p1
%patch5101 -p1
%patch5102 -p1
%patch5125 -p1
#%patch5126 -p1
%patch5127 -p1
#%patch5128 -p1
#%patch5129 -p1
#%patch5130 -p1
%patch5131 -p1
%patch5132 -p1
%patch5133 -p1
%patch5134 -p1
#%patch5135 -p1
#%patch5136 -p1
#%patch5137 -p1
#%patch5138 -p1

%patch5210 -p1
#%patch5211 -p1
#%patch5212 -p1
%patch5224 -p1
#%patch5232 -p1
%patch5233 -p1
%patch5234 -p1
%patch5235 -p1
#%patch5236 -p1
%patch5237 -p1
%patch5238 -p1
%patch5239 -p1
%patch5240 -p1
%patch5241 -p1
%patch5242 -p1
%patch5243 -p1
%patch5244 -p1
%patch5245 -p1
%patch5246 -p1
%patch5247 -p1
%patch5248 -p1
%patch5249 -p1
%patch5250 -p1
#%patch5251 -p1
#%patch5252 -p1
%patch5253 -p1
%patch5254 -p1
%patch5255 -p1
%patch5256 -p1
%patch5257 -p1

%patch5260 -p1
#%patch5261 -p1

%patch5300 -p1
%patch5301 -p1
%patch5302 -p1
%patch5303 -p1
%patch5304 -p1
%patch5305 -p1

%patch5401 -p1
%patch5402 -p1
%patch5403 -p1
%patch5404 -p1
%patch5405 -p1
%patch5406 -p1
%patch5407 -p1
%patch5408 -p1
%patch5409 -p1
%patch5410 -p1
%patch5411 -p1
%patch5412 -p1
%patch5413 -p1
%patch5414 -p1
%patch5415 -p1
%patch5416 -p1
%patch5417 -p1
%patch5418 -p1
%patch5419 -p1
%patch5420 -p1
%patch5421 -p1
%patch5422 -p1
%patch5423 -p1
%patch5424 -p1
%patch5425 -p1
%patch5426 -p1
%patch5427 -p1
%patch5428 -p1
%patch5429 -p1
%patch5430 -p1
%patch5431 -p1

#%patch6000 -p1
%patch6001 -p1
%patch6002 -p1
%patch6003 -p1

#%patch10000 -p1
# ----------------------------------------------------------------------------

mkdir -p configs

cp -fv %{all_arch_configs} .


# now run oldconfig over all the config files
for i in *.config*
do
	mv $i .config
	make ARCH=`echo $i | cut -d"-" -f3 | cut -d"." -f1 | sed -e s/i.86/i386/ -e s/s390x/s390/ -e s/ppc64.series/ppc64/ -e s/ppc8.../ppc/ -e s/ppc32dy4/ppc/ ` nonint_oldconfig > /dev/null
	cp .config configs/$i
done

# get rid of unwanted files resulting from patch fuzz
find . -name "*.orig" -exec rm -fv {} \;
find . -name "*~" -exec rm -fv {} \;

# ----------------------------------------------------------------------------
# build
# ----------------------------------------------------------------------------
%build

cd linux-%{kversion}


MakeDebugInfoConfig() {
	sed -e "s/.*\(CONFIG_DEBUG_INFO\).*/\1=y/" .config > .config.xxx
	mv -f .config.xxx .config
}

MakeDebugConfig() {
	sed							\
	  -e "s/.*\(CONFIG_DEBUG_KERNEL\>\).*/\1=y/"		\
	  -e "s/.*\(CONFIG_DEBUG_STACKOVERFLOW\>\).*/\1=y/"	\
	  -e "s/.*\(CONFIG_DEBUG_SLAB\>\).*/\1=y/"		\
	  -e "s/.*\(CONFIG_MAGIC_SYSRQ\>\).*/\1=y/"		\
	  -e "s/.*\(CONFIG_DEBUG_SPINLOCK\>\).*/\1=y/"		\
	  -e "s/.*\(CONFIG_DEBUG_PAGEALLOC\>\).*/\1=y/"		\
	  -e "s/.*\(CONFIG_DEBUG_SPINLOCK_SLEEP\>\).*/\1=y/"	\
	  -e "s/.*\(CONFIG_FRAME_POINTER\>\).*/\1=y/"		\
	  .config > .config.xxx
	mv -f .config.xxx .config
}

BuildKernel() {

    VER=$1
    DBG=$2

    [ -z "$VER" ] && SUFF="" || SUFF=-$VER
    if [ -z "$VER" ]; then
       VER=$DBG;
    else
       [ -n "$DBG" ] && VER=$VER-$DBG
    fi
    [ -z "$VER" ] && FULLSUFF="" || FULLSUFF=-$VER

    # Pick the right config file for the kernel we're building
    Config=kernel-%{kversion}-%{_target_cpu}${SUFF}.config.ovz
    KernelVer=%{version}-%{release}${FULLSUFF}
    perl -p -i -e "s/^EXTRAVERSION.*/EXTRAVERSION = -%{release}${FULLSUFF}/" Makefile

    echo BUILDING A KERNEL FOR $VER %{_target_cpu}...

    # and now to start the build process

    make -s mrproper
    cp configs/$Config .config
    arch=`echo $Config | cut -d"-" -f3 | cut -d"." -f1 | sed -e s/i.86/i386/ \
    	-e s/s390x/s390/ -e s/ppc64.series/ppc64/ -e s/ppc8.../ppc/ -e s/ppc32dy4/ppc/ `
    [ "$DBG" == "debug" ] && MakeDebugConfig
%if %{builddebuginfo}
    MakeDebugInfoConfig
%endif

    make ARCH=${arch} -s nonint_oldconfig > /dev/null
    make -s include/linux/version.h

    make ARCH=${arch} -s %{?_smp_mflags} bzImage
    make ARCH=${arch} -s %{?_smp_mflags} modules || exit 1
    make ARCH=${arch} buildcheck

    # Start installing the results

    mkdir -p $RPM_BUILD_ROOT/usr/lib/debug/boot
    mkdir -p $RPM_BUILD_ROOT/%{image_install_path}
    install -m 644 System.map $RPM_BUILD_ROOT/usr/lib/debug/boot/System.map-$KernelVer
    install -m 644 System.map $RPM_BUILD_ROOT/boot/System.map-$KernelVer
    cp arch/*/boot/bzImage $RPM_BUILD_ROOT/%{image_install_path}/vmlinuz-$KernelVer
    cp vmlinux $RPM_BUILD_ROOT/%{image_install_path}/vmlinux-$KernelVer

    mkdir -p $RPM_BUILD_ROOT/lib/modules/$KernelVer
    make -s INSTALL_MOD_PATH=$RPM_BUILD_ROOT modules_install KERNELRELEASE=$KernelVer

    # And save the headers/makefiles etc for building modules against
    #
    # This all looks scary, but the end result is supposed to be:
    # * all arch relevant include/ files
    # * all Makefile/Kconfig files
    # * all script/ files

    rm -f $RPM_BUILD_ROOT/lib/modules/$KernelVer/build
    rm -f $RPM_BUILD_ROOT/lib/modules/$KernelVer/source
    mkdir -p $RPM_BUILD_ROOT/lib/modules/$KernelVer/build
    (cd $RPM_BUILD_ROOT/lib/modules/$KernelVer ; ln -s build source)
    # first copy everything
    cp --parents `find  -type f -name "Makefile*" -o -name "Kconfig*"` $RPM_BUILD_ROOT/lib/modules/$KernelVer/build
    cp Module.symvers $RPM_BUILD_ROOT/lib/modules/$KernelVer/build
    # then drop all but the needed Makefiles/Kconfig files
    rm -rf $RPM_BUILD_ROOT/lib/modules/$KernelVer/build/Documentation
    rm -rf $RPM_BUILD_ROOT/lib/modules/$KernelVer/build/scripts
    rm -rf $RPM_BUILD_ROOT/lib/modules/$KernelVer/build/include
    cp arch/%{_arch}/kernel/asm-offsets.s $RPM_BUILD_ROOT/lib/modules/$KernelVer/build/arch/%{_arch}/kernel || :
    cp .config $RPM_BUILD_ROOT/lib/modules/$KernelVer/build
    cp -a scripts $RPM_BUILD_ROOT/lib/modules/$KernelVer/build
    cp -a arch/%{_arch}/scripts $RPM_BUILD_ROOT/lib/modules/$KernelVer/build/arch/%{_arch} || :
    cp -a arch/%{_arch}/*lds $RPM_BUILD_ROOT/lib/modules/$KernelVer/build/arch/%{_arch}/ || :
    rm -f $RPM_BUILD_ROOT/lib/modules/$KernelVer/build/scripts/*.o
    rm -f $RPM_BUILD_ROOT/lib/modules/$KernelVer/build/scripts/*/*.o
    mkdir -p $RPM_BUILD_ROOT/lib/modules/$KernelVer/build/include
    cd include
    cp -a acpi config linux math-emu media net pcmcia rxrpc scsi sound video asm asm-generic ub $RPM_BUILD_ROOT/lib/modules/$KernelVer/build/include
    cp -a `readlink asm` $RPM_BUILD_ROOT/lib/modules/$KernelVer/build/include
    # Make sure the Makefile and version.h have a matching timestamp so that
    # external modules can be built
    touch -r $RPM_BUILD_ROOT/lib/modules/$KernelVer/build/Makefile $RPM_BUILD_ROOT/lib/modules/$KernelVer/build/include/linux/version.h
    touch -r $RPM_BUILD_ROOT/lib/modules/$KernelVer/build/.config $RPM_BUILD_ROOT/lib/modules/$KernelVer/build/include/linux/autoconf.h
    cd ..

    #
    # save the vmlinux file for kernel debugging into the kernel-debuginfo rpm
    #
    mkdir -p $RPM_BUILD_ROOT/usr/lib/debug/lib/modules/$KernelVer
    cp vmlinux $RPM_BUILD_ROOT/usr/lib/debug/lib/modules/$KernelVer

    # mark modules executable so that strip-to-file can strip them
    find $RPM_BUILD_ROOT/lib/modules/$KernelVer -name "*.ko" -type f  | xargs chmod u+x
#    cp -r $RPM_BUILD_ROOT/lib/modules/$KernelVer/kernel $RPM_BUILD_ROOT/usr/lib/debug/lib/modules/$KernelVer

    # detect missing or incorrect license tags
    for i in `find $RPM_BUILD_ROOT/lib/modules/$KernelVer -name "*.ko" ` ; do echo -n "$i " ; /sbin/modinfo -l $i >> modinfo ; done
    cat modinfo | grep -v "^GPL" | grep -v "^Dual BSD/GPL" | grep -v "^Dual MPL/GPL" | grep -v "^GPL and additional rights" | grep -v "^GPL v2" && exit 1
    rm -f modinfo
    # remove files that will be auto generated by depmod at rpm -i time
    rm -f $RPM_BUILD_ROOT/lib/modules/$KernelVer/modules.*

}

XBuildKernel() {
  # build normal kernel
%if %{buildnormal}
  BuildKernel $1
%endif
%if %{builddebug}
  BuildKernel "$1" "debug"
%endif
}

###
# DO it...
###

# prepare directories
rm -rf $RPM_BUILD_ROOT
mkdir -p $RPM_BUILD_ROOT/boot

%if %{buildup}
XBuildKernel
%endif

%if %{buildsmp}
XBuildKernel smp
%endif

%if %{buildenterprise}
XBuildKernel enterprise
%endif

%if %{buildentnosplit}
XBuildKernel entnosplit
%endif

###
### install
###

%install

cd linux-%{kversion}

# architectures that don't get kernel-source (i586/i686/athlon) dont need
# much of an install because the build phase already copied the needed files

###
### clean
###

%clean
rm -rf $RPM_BUILD_ROOT

###
### scripts
###

# load the loop module for upgrades...in case the old modules get removed we have
# loopback in the kernel so that mkinitrd will work.
%pre
/sbin/modprobe loop 2> /dev/null > /dev/null  || :
exit 0

%pre debug
/sbin/modprobe loop 2> /dev/null > /dev/null  || :
exit 0

%pre smp
/sbin/modprobe loop 2> /dev/null > /dev/null  || :
exit 0

%pre smp-debug
/sbin/modprobe loop 2> /dev/null > /dev/null  || :
exit 0

%pre enterprise
/sbin/modprobe loop 2> /dev/null > /dev/null  || :
exit 0

%pre enterprise-debug
/sbin/modprobe loop 2> /dev/null > /dev/null  || :
exit 0

%pre entnosplit
/sbin/modprobe loop 2> /dev/null > /dev/null  || :
exit 0

%pre entnosplit-debug
/sbin/modprobe loop 2> /dev/null > /dev/null  || :
exit 0

%post
%ifarch ia64
[ -f /etc/redhat-release ] && mv /%{image_install_path}/vmlinu{z,x}-%{KVERREL} /%{image_install_path}/efi/redhat/
[ -f /etc/SuSE-release ] && mv /%{image_install_path}/vmlinu{z,x}-%{KVERREL} /boot
%endif
[ -x /sbin/new-kernel-pkg ] && /sbin/new-kernel-pkg --mkinitrd --depmod --install %{KVERREL}

%post debug
%ifarch ia64
[ -f /etc/redhat-release ] && mv /%{image_install_path}/vmlinu{z,x}-%{KVERREL}-debug /%{image_install_path}/efi/redhat/
[ -f /etc/SuSE-release ] && mv /%{image_install_path}/vmlinu{z,x}-%{KVERREL}-debug /boot
%endif
[ -x /sbin/new-kernel-pkg ] && /sbin/new-kernel-pkg --mkinitrd --depmod --install %{KVERREL}-debug

%post smp
[ -x /sbin/new-kernel-pkg ] && /sbin/new-kernel-pkg --mkinitrd --depmod --install %{KVERREL}-smp

%post smp-debug
[ -x /sbin/new-kernel-pkg ] && /sbin/new-kernel-pkg --mkinitrd --depmod --install %{KVERREL}-smp-debug

%post enterprise
[ -x /sbin/new-kernel-pkg ] && /sbin/new-kernel-pkg --mkinitrd --depmod --install %{KVERREL}-enterprise

%post enterprise-debug
[ -x /sbin/new-kernel-pkg ] && /sbin/new-kernel-pkg --mkinitrd --depmod --install %{KVERREL}-enterprise-debug

%post entnosplit
[ -x /sbin/new-kernel-pkg ] && /sbin/new-kernel-pkg --install --mkinitrd --depmod %{KVERREL}-entnosplit
exit 0

%post entnosplit-debug
[ -x /sbin/new-kernel-pkg ] && /sbin/new-kernel-pkg --install --mkinitrd --depmod %{KVERREL}-entnosplit-debug
exit 0

%preun
%ifarch ia64
[ -f /etc/redhat-release ] && mv /%{image_install_path}/efi/redhat/vmlinu{z,x}-%{KVERREL} /%{image_install_path}/
[ -f /etc/SuSE-release ] && mv /boot/vmlinu{z,x}-%{KVERREL} /%{image_install_path}/
%endif
/sbin/modprobe loop 2> /dev/null > /dev/null  || :
[ -x /sbin/new-kernel-pkg ] && /sbin/new-kernel-pkg --rminitrd --rmmoddep --remove %{KVERREL}

%preun debug
%ifarch ia64
[ -f /etc/redhat-release ] && mv /%{image_install_path}/efi/redhat/vmlinu{z,x}-%{KVERREL}-debug /%{image_install_path}/
[ -f /etc/SuSE-release ] && mv /boot/vmlinu{z,x}-%{KVERREL}-debug /%{image_install_path}/
%endif
/sbin/modprobe loop 2> /dev/null > /dev/null  || :
[ -x /sbin/new-kernel-pkg ] && /sbin/new-kernel-pkg --rminitrd --rmmoddep --remove %{KVERREL}-debug

%preun smp
/sbin/modprobe loop 2> /dev/null > /dev/null  || :
[ -x /sbin/new-kernel-pkg ] && /sbin/new-kernel-pkg --rminitrd --rmmoddep --remove %{KVERREL}-smp

%preun smp-debug
/sbin/modprobe loop 2> /dev/null > /dev/null  || :
[ -x /sbin/new-kernel-pkg ] && /sbin/new-kernel-pkg --rminitrd --rmmoddep --remove %{KVERREL}-smp-debug

%preun enterprise
/sbin/modprobe loop 2> /dev/null > /dev/null  || :
[ -x /sbin/new-kernel-pkg ] && /sbin/new-kernel-pkg --rminitrd --rmmoddep --remove %{KVERREL}-enterprise

%preun enterprise-debug
/sbin/modprobe loop 2> /dev/null > /dev/null  || :
[ -x /sbin/new-kernel-pkg ] && /sbin/new-kernel-pkg --rminitrd --rmmoddep --remove %{KVERREL}-enterprise-debug

%preun entnosplit
/sbin/modprobe loop 2> /dev/null > /dev/null  || :
[ -x /sbin/new-kernel-pkg ] && /sbin/new-kernel-pkg --remove --rminitrd --rmmoddep %{KVERREL}-entnosplit
exit 0

%preun entnosplit-debug
/sbin/modprobe loop 2> /dev/null > /dev/null  || :
[ -x /sbin/new-kernel-pkg ] && /sbin/new-kernel-pkg --remove --rminitrd --rmmoddep %{KVERREL}-entnosplit-debug
exit 0


###
### file lists
###

%if %{buildup} && %{buildnormal}
%files
%defattr(-,root,root)
/%{image_install_path}/vmlinuz-%{KVERREL}
/%{image_install_path}/vmlinux-%{KVERREL}
/boot/System.map-%{KVERREL}
%dir /lib/modules/%{KVERREL}
/lib/modules/%{KVERREL}/kernel
%verify(not mtime) /lib/modules/%{KVERREL}/build
%endif

%if %{buildup} && %{builddebug}
%files debug
%defattr(-,root,root)
/%{image_install_path}/vmlinuz-%{KVERREL}-debug
/%{image_install_path}/vmlinux-%{KVERREL}-debug
/boot/System.map-%{KVERREL}-debug
%dir /lib/modules/%{KVERREL}-debug
/lib/modules/%{KVERREL}-debug/kernel
%verify(not mtime) /lib/modules/%{KVERREL}-debug/build
%endif

### ------------------ SMP -----------------------

%if %{buildsmp} && %{buildnormal}
%files smp
%defattr(-,root,root)
/%{image_install_path}/vmlinuz-%{KVERREL}-smp
/%{image_install_path}/vmlinux-%{KVERREL}-smp
/boot/System.map-%{KVERREL}-smp
%dir /lib/modules/%{KVERREL}-smp
/lib/modules/%{KVERREL}-smp/kernel
%verify(not mtime) /lib/modules/%{KVERREL}-smp/build
%endif

%if %{buildsmp} && %{builddebug}
%files smp-debug
%defattr(-,root,root)
/%{image_install_path}/vmlinuz-%{KVERREL}-smp-debug
/%{image_install_path}/vmlinux-%{KVERREL}-smp-debug
/boot/System.map-%{KVERREL}-smp-debug
%dir /lib/modules/%{KVERREL}-smp-debug
/lib/modules/%{KVERREL}-smp-debug/kernel
%verify(not mtime) /lib/modules/%{KVERREL}-smp-debug/build
%endif


### --------------- ENTERPRISE---------------------

%if %{buildenterprise} && %{buildnormal}
%files enterprise
%defattr(-,root,root)
/%{image_install_path}/vmlinuz-%{KVERREL}-enterprise
/%{image_install_path}/vmlinux-%{KVERREL}-enterprise
/boot/System.map-%{KVERREL}-enterprise
%dir /lib/modules/%{KVERREL}-enterprise
/lib/modules/%{KVERREL}-enterprise/kernel
%verify(not mtime) /lib/modules/%{KVERREL}-enterprise/build
%endif

%if %{buildenterprise} && %{builddebug}
%files enterprise-debug
%defattr(-,root,root)
/%{image_install_path}/vmlinuz-%{KVERREL}-enterprise-debug
/%{image_install_path}/vmlinux-%{KVERREL}-enterprise-debug
/boot/System.map-%{KVERREL}-enterprise-debug
%dir /lib/modules/%{KVERREL}-enterprise-debug
/lib/modules/%{KVERREL}-enterprise-debug/kernel
%verify(not mtime) /lib/modules/%{KVERREL}-enterprise-debug/build
%endif

### --------------- ENTNOSPLIT ---------------------

%if %{buildentnosplit} && %{buildnormal}
%files entnosplit
%defattr(-,root,root)
/%{image_install_path}/vmlinuz-%{KVERREL}-entnosplit
/%{image_install_path}/vmlinux-%{KVERREL}-entnosplit
/boot/System.map-%{KVERREL}-entnosplit
%dir /lib/modules/%{KVERREL}-entnosplit
/lib/modules/%{KVERREL}-entnosplit/kernel
%verify(not mtime) /lib/modules/%{KVERREL}-entnosplit/build
%endif

%if %{buildentnosplit} && %{builddebug}
%files entnosplit-debug
%defattr(-,root,root)
/%{image_install_path}/vmlinuz-%{KVERREL}-entnosplit-debug
/%{image_install_path}/vmlinux-%{KVERREL}-entnosplit-debug
/boot/System.map-%{KVERREL}-entnosplit-debug
%dir /lib/modules/%{KVERREL}-entnosplit-debug
/lib/modules/%{KVERREL}-entnosplit-debug/kernel
%verify(not mtime) /lib/modules/%{KVERREL}-entnosplit-debug/build
%endif
