Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using populate_sdk to include kernel headers

How do I include the linux kernel headers as part of the SDK package in Yocto?

I'm using Yocto 1.8 (fido) in an embedded project and want to do out-of-tree kernel module development. Currently, I can build my kernel modules (apart from bitbake) by pointing my $KERNEL_PATH to the poky/build/tmp/work-shared/<machine>/kernel-source/ directory when I run make. I don't want to do this in the long term, however, since others need to easily build the modules without installing and building a full image from bitbake.

I can generate an SDK using bitbake myimage -c populate_sdk. However, this doesn't include the kernel headers (all I ever see is sysroots/<mach>/usr/include/linux). How do I cause the kernel headers to be included in the SDK package? Also, I don't want the kernel headers to show up as part of my target image.

[Edit] My image recipe is as follows:

EXTRA_IMAGE_FEATURES_append = " eclipse-debug debug-tweaks"
TOOLCHAIN_HOST_TASK_append = " nativesdk-cmake"
IMAGE_INSTALL = "packagegroup-core-boot  ${CORE_IMAGE_EXTRA_INSTALL} util-linux kernel-modules netbase busybox base-passwd base-files sysvinit initscripts bash gdbserver strace sysfsutils dtc gawk ethtool grep sed wget  iptables oprofile net-tools dropbear rsync stress-ng rt-tests i2c-tools"
inherit core-image

The kernel I'm using is linux-altera-ltsi-rt in the meta-altera layer.

like image 700
benf Avatar asked Jul 06 '15 22:07

benf


People also ask

Do I need to install kernel headers?

The kernel headers are only needed when you're using a stock kernel and you want to compile a new driver module or such that links directly with the kernel. This is because the distro maintainers deliberately separate the kernel and its headers into separate packages.

How do I get kernel headers in Kali Linux?

One of the methods you can use to install Kernel Headers is to use the Debian package manager with Kali Linux repositories. You may require to run a full system upgrade before installing the Kernel Headers successfully. Once completed, reboot your Kali Linux installation, and install the headers.

Where are kernel headers stored?

The system's libc headers are usually installed at the default location /usr/include and the kernel headers in subdirectories under that (most notably /usr/include/linux and /usr/include/asm).


2 Answers

From the fido release, the handling of kernel builds has been changed. In previous releases, you could usually just skip down to the usage example below.

In fido or any 1.8+, if you want the kernel src and build system available in the SDK, you should add

TOOLCHAIN_TARGET_TASK_append = " kernel-devsrc"

to your image recipe. That will ensure that the new, kernel-devsrc package is installed into your toolchain.

The procedure below is just to ensure that the rest of the workflow is fully understood (even though it's not strictly part of the original question).

Usage Example

Lets assume a module Makefile as follows:

obj-m += hello-1.o
all:
    make -C  $(KERNEL_SRC) M=$(PWD) modules

clean:
    make -C  $(KERNEL_SRC) M=$(PWD) clean

Example taken from The Linux Kernel Module Programming Guide (Note that the actual commands needs to have a tab character for indentation).

Then you'll have to define KERNEL_SRC to be sysroots/<mach>/usr/src/kernel/, either in the Makefile, or from your make call. (Using a variable like KERNEL_SRC will ensure that your module recipe automatically picks the right location when building using bitbake).

To manually build your kernel module:

  1. Source the environment-* file for your SDK.

  2. Go to you modules directory.

  3. KERNEL_SRC=<sdk-install-path>/sysroots/<mach>/usr/src/kernel LDFLAGS="" make However, this will fail, as fixdep can't be found. We'll fix this manually.

  4. cd <sdk-install-path>/sysroots/<mach>/usr/src/kernel

  5. make modules_prepare

    If this needs to be run with sudo, be sure to source the environment file in the sudo environment: sudo bash -c "source <sdk-install-path>/environment-setup-<mach> && make modules_prepare"

  6. Go back to your modules directory.

  7. KERNEL_SRC=<sdk-install-path>/sysroots/<mach>/usr/src/kernel LDFLAGS="" make

This should now allow you to build your modules.

If you don't have the kernel source under sysroots/<mach>/usr/src/kernel/, we'll have to look into that.

like image 180
Anders Avatar answered Sep 22 '22 16:09

Anders


anders answer is very good, but in recent versions of yocto the way to add kernel-devsrc seems to be

IMAGE_INSTALL += "kernel-devsrc"

which I found here: https://www.mail-archive.com/[email protected]/msg36448.html

like image 44
othiman Avatar answered Sep 23 '22 16:09

othiman