Building NVIDIA akmod package for RHEL/CentOS using Fedora and mock

Unfortunately there are no NVIDIA driver packages for RHEL/CentOS available on RPMFusion (that I could find), so this is how I built them. Disclaimer: This is just the way I did it and is probably dodgy. Suggestions welcome.

I have a dedicated build server and use mock so that all of the building is kept separate from my running system in a chroot environment. Packages are built against epel (Extra Packages for Enterprise Linux) and I’m using 64 bit.

We’re building an akmod package (rather than a static kmod) which will push the compilation of the driver on a per kernel basis to the client. The benefit of this is you only need to install the akmod once and the system will compile the kmod drivers after every kernel update.

Install Fedora
Install Fedora and then configure RPMFusion repositories (free, free-updates, non-free, non-free-updates).

Install mock and add yourself to the mock group (replace [username]). Log out and in or run newgrp:
$ sudo yum install mock
$ sudo gpasswd -a [username]
$ newgrp mock

Create a temporary location to perform the tasks below (makes copy and paste from here easier)
$ mkdir ~/building-nvidia
$ cd ~/building-nvidia

Source RPMS
Now we’re ready to get the source RPMs:
$ yumdownloader --source akmod-nvidia akmods buildsys-build-rpmfusion-kerneldevpkgs-current kmodtool libvdpau nvidia-kmod-common nvidia-settings nvidia-xconfig

Initialise mock
Let’s initialise the chroot environment for epel:
$ mock -r epel-6-x86_64 --init

Build and install dependencies
Ordinarily, building an RPM using mock will automatically install any build dependencies into the chroot. By default however it can (obviously) only automatically install any packages that are available in the target distribution. This presents a little problem with RHEL and CentOS because some of the dependencies (such as kmodtool and akmods) are not available in these distributions. So, how do we get around this? We can tell mock to install any RPM we like into the chroot, they don’t have to be available in the target distribution.

But it gets a little more tricky. If said dependency doesn’t exist for RHEL/CentOS, then we need to build it (using mock) and then install it into mock’s chroot manually. Make sense?

Once we have these dependencies manually installed the trick is telling mock not to clean the chroot environment for any post build processes, else it would remove those dependencies we manually installed (and therefore you couldn’t build the packages!).

Building kmodtool
The first dependency we need to build is kmodtool, because it does much of the grunt work for kmods (akmods requires this). Note we will add the “–no-clean” option when building packages, as we don’t want the packages we installed to be removed from the chroot (they’re dependencies).

Go for it:
$ mock -r epel-6-x86_64 --no-clean --no-cleanup-after --resultdir=/var/lib/mock/epel-6-x86_64/result/ --rebuild kmodtool*.src.rpm

Installing kmodtool
Install kmodtool into the chroot:
$ mock -r epel-6-x86_64 --install /var/lib/mock/epel-6-x86_64/result/kmodtool*.noarch.rpm

Building akmods
It gets slightly more tricky. The akmods package we are using is designed for Fedora and fails to build because it can’t detect the version of Fedora we are using and so tries to use systemd (Fedora’s boot system) which is not yet used in RHEL/CentOS. This means we need to install the akmod source rpm, modify the spec file and then re-create the source rpm for compilation (this will happen in your home directory).
$ rpm -ivh akmods*.src.rpm
$ sed -i 's/%fedora\ <=16/0%{?rhel}/g' ~/rpmbuild/SPECS/akmods.spec $ rpmbuild -bs ~/rpmbuild/SPECS/akmods.spec $ mock -r epel-6-x86_64 --no-clean --no-cleanup-after --rebuild --resultdir=/var/lib/mock/epel-6-x86_64/result/ ~/rpmbuild/SRPMS/akmods*.src.rpm

This will build akmods RPM and place it in mock's result dir which we will install next.

Installing akmods
Install akmods into the chroot:
$ mock -r epel-6-x86_64 --install /var/lib/mock/epel-6-x86_64/result/akmods*.noarch.rpm

Building buildsys-build-rpmfusion
The kmod package has some smarts to work out which kernel to build the kmod for. We need to build and install the buildsys-build-rpmfusion package which will give us an akmod compatible version of the package (actually it's a "provides"). This also requires kmodtool, hence we had to do this after the others:
$ mock -r epel-6-x86_64 --no-clean --no-cleanup-after --resultdir=/var/lib/mock/epel-6-x86_64/result/ --rebuild buildsys-build-rpmfusion*.src.rpm

Installing buildsys-build-rpmfusion
We only want to install the base package (not the 'current' package as that would require kernel-devel for your Fedora kernel).
$ rm /var/lib/mock/epel-6-x86_64/result/buildsys-build-rpmfusion-kerneldevpkgs-current*.x86_64.rpm
$ mock -r epel-6-x86_64 --install /var/lib/mock/epel-6-x86_64/result/buildsys-build*.x86_64.rpm

That's it for manual dependencies!

From now on, the packages we build will not be installed into the chroot, they will be copied and installed on the client machine.

Building nvidia-kmod package
The spec file for the nvidia-kmod package has a "buildforkernels" definition to tell it what kernel you want to build for, either current or latest kernel. Alternatively, it can build an akmod package (dynamically build package on boot), which is what we want. By default this option is set to "current" so we need to change this definition to "akmod" (hard-coded in the spec file).

To do this, we need to install the source RPM, change the option in its spec file, and rebuild the SRPM:
$ rpm -ivh nvidia-kmod*src.rpm
$ sed -i s/^%define\ buildforkernels\ current/%define\ buildforkernels\ akmod/ ~/rpmbuild/SPECS/nvidia-kmod.spec
$ rpmbuild -bs ~/rpmbuild/SPECS/nvidia-kmod.spec
$ mock -r epel-6-x86_64 --no-clean --rebuild ~/rpmbuild/SRPMS/nvidia-kmod*.src.rpm

If you don't want an akmod, then you need to specify the kernel that you're building for (this should install kernel-devel into the chroot). If you don't, you'll run into problems because the chroot will detect your Fedora kernel, not the kernel you need to build for. Note this is a different definition we are specifying (replace with the kernel version you want):
$ mock -r epel-6-x86_64 --define "kernels 2.6.32-220.17.1.el6.x86_64" --no-clean --rebuild nvidia-kmod*.src.rpm

Building remaining packages
Now build the rest of the packages:
$ mock -r epel-6-x86_64 --no-clean --rebuild \
libvdpau*.src.rpm nvidia-settings*.src.rpm \
nvidia-xconfig*.src.rpm xorg-x11-drv-nvidia*.src.rpm

Copy built packages
All of the build packages will be under mock's result directory, so we can now copy these out and install them on a RHEL/CentOS box!
$ mkdir {packages,source}
$ cp -r /var/lib/mock/epel-6-x86_64/result/*{noarch,x86_64}.rpm packages/
$ cp -r /var/lib/mock/epel-6-x86_64/result/*src.rpm source/

Now these can be published to a repository and pushed out to machines.

Install akmod-nvidia package
All you need to do is install the nvidia akmod package:
$ sudo yum install akmod-nvidia

This should automatically pull in all dependencies required to build the package, like kernel-devel, gcc, make, etc.

Finally, make sure that akmods is set to start on boot:
$ sudo chkconfig akmods on

Now each time a kernel update is released, the machine will automatically build the nvidia driver for you.

6 thoughts on “Building NVIDIA akmod package for RHEL/CentOS using Fedora and mock

  1. Thanks for this good explanation, but do you know elrepo.org? They have very good rpms for different hardware drivers, including NVidia.

  2. You say fedora/Redhat and then you you just use epl-6. What are people with fedora supposed to do?

    roger

Leave a Reply

Your email address will not be published. Required fields are marked *