Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mailinglists35/cb7f05d057fde5ee53f7e0f6c01d5752 to your computer and use it in GitHub Desktop.
Save mailinglists35/cb7f05d057fde5ee53f7e0f6c01d5752 to your computer and use it in GitHub Desktop.
this could vanish anytime from the web. rebuild kernel module. patch kernel centos redhat oracle
Building external modules is well documented, but I found the info for rebuilding a single distro kernel module quite disjoint, I guess because it spans both distribution and kernel specific info. Now rebuilding your whole distro kernel is going to take ages due to the number of modules enabled, so the instructions below show how to quickly rebuild and install the minimum required. Note these instructions are based on rpm using distros but should be easily translatable to ones using dpkg.
The specific examples below are to patch the snd_intel8x0 driver to get sound working on Fedora 11, and enabling experimental write support on the ufs module which I used to unbrick the FreeBSD based m0n0wall router in the office.
Get kernel versions and paths for automation
#cat ~/kernel.sh ~/args.sh
kernver=$(uname -r)
kernbase=$(echo $kernver | sed 's/\([0-9]*\.[0-9]*\.[0-9]*\).*/\1/')
rpmarch=$(rpm -q --qf="%{ARCH}\n" kernel | head -n1)
rpmbase=$(rpm -q kernel | sed -n "\$s/\.$rpmarch$//p")
kernextraver=$(echo $kernver | sed "s/$kernbase\(.*\)/\1/")
arch=$(uname -m)
rpmbuild_base=~/rpmbuild
echo kernver=$(echo $kernver)
echo kernbase=$(echo $kernbase)
echo rpmarch=$(echo $rpmarch)
echo rpmbase=$(echo $rpmbase)
echo kernextraver=$(echo $kernextraver)
echo arch=$(echo $arch)
echo rpmbuild_base=$(echo $rpmbuild_base)
#cut -d= -f1 kernel.sh |while read arg; do echo -n echo $arg=; echo \$\(echo \$$arg\); done |sed '/ =\$/d' > args.sh
#cat kernel.sh args.sh > setup.sh
. ./setup.sh
Install and prepare kernel source
Note the kernel-devel package only contains files required to build external modules against the kernel, not the full source.
cd /usr/src/kernels #optional
yum install yum-utils
sudo yum-builddep kernel
yumdownloader --source kernel-$kernver
rpm -ivh $rpmbase.src.rpm
rm $rpmbase.src.rpm #optional
rpmbuild_base=~/rpmbuild #use /usr/src/redhat for older systems
rpmbuild -bp $rpmbuild_base/SPECS/kernel.spec --target=$rpmarch
kernsrc=$rpmbuild_base/BUILD/kernel-$kernbase/linux-$kernbase.$rpmarch
sudo ln -s /home/user/rpmbuild/BUILD/kernel-4.4.fc22/linux-4.4.14-200.fc22.x86_64 /usr/src/kernels/4.4.14-200.fc22.x86_64
Rebuild a single kernel module
config preparation
cd $kernsrc
#copy existing distro config
cp /boot/config-$kernver .config
Note if building a large module, you may want to disable debugging info by unsetting CONFIG_DEBUG_INFO in .config at this stage.
apply changes
#make config changes
sed -i 's/# CONFIG_UFS_FS_WRITE is not set/CONFIG_UFS_FS_WRITE=y/' .config
#or make code changes
$ patch -p1 < ~/alsa-fix.diff
build preparation
Note ignore the Module.symvers warning as that file should be present from the existing kernel build
sed -i "s/EXTRAVERSION =.*/EXTRAVERSION = $kernextraver/" Makefile
make oldconfig
make prepare
make modules_prepare
make SUBDIRS=scripts/mod
rebuild the module directory
Note I don't know how to build a particular module.ko within a dir, but no directory has too many modules, so the build should complete in a reasonable amount of time.
make SUBDIRS=fs/ufs/ modules
make SUBDIRS=sound/pci modules
Install and use the new module
#don't install, just use once
rmmod ufs
insmod fs/ufs/ufs.ko
mount -t ufs -o ufstype=44bsd /dev/sdb4 mnt_monowall
sudo install -m 744 snd-intel8x0.ko /lib/modules/$(uname -r)/kernel/sound/pci
killall pulseaudio
rmmod snd_intel8x0; modprobe snd_intel8x0
#relogin to start pulseaudio. Don't know how to otherwise?
Note if the module is needed in early boot it will be part of the initrd which is these days in initramfs format. To update the one in /boot manually, one can do:
mkdir tmp
cd tmp
gzip -dc /boot/initramfs-$kernver.img | cpio -i --make-directories
# copy in the new module
rm -f lib/modules/$kernver/*.bin # in case not updated by current depmod
depmod -b $PWD $(basename lib/modules/[23]*)
find . | cpio -H newc -o | gzip > /boot/initramfs-$kernver.img
Alternatively you could try the more standard
mkinitrd --force /boot/initramfs-$kernver.img $kernver
rebuild the main image
If you want to update the main kernel image rather than a module, then just build it as follows and copy it to /boot
make bzImage
optional cleanup
rm -Rf $rpmbuild_base/BUILD/kernel-$kernbase/
© Aug 18 2009
Comments...
@mailinglists35
Copy link
Author

sed -i 's/CONFIG_MODULE_SIG=y/# CONFIG_MODULE_SIG is not set/' .config
sed -i 's/CONFIG_MODULE_SIG_ALL=y/# CONFIG_MODULE_SIG_ALL is not set/' .config

@mailinglists35
Copy link
Author

extra module https://www.kernel.org/doc/Documentation/kbuild/modules.txt
--- 2.1 Command Syntax

The command to build an external module is:

	$ make -C <path_to_kernel_src> M=$PWD

The kbuild system knows that an external module is being built
due to the "M=<dir>" option given in the command.

To build against the running kernel use:

	$ make -C /lib/modules/`uname -r`/build M=$PWD

Then to install the module(s) just built, add the target
"modules_install" to the command:

	$ make -C /lib/modules/`uname -r`/build M=$PWD modules_install

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment