Showing results for 
Show  only  | Search instead for 
Did you mean: 

Create Linux Modules External to PetaLinux

Xilinx Employee
Xilinx Employee
10 2 2,658

PetaLinux is a great utility that allows designers to easily create Linux images to run on their target platforms. PetaLinux will also create user applications and modules with a template Makefile and BB files so that they can be built and added to a rootfs.

However, for users trying to develop a module, creating, building, and deploying from the command line speeds up the process.

In this blog entry we will discuss how to create a module and then build and deploy it on a ZCU104 board outside of the PetaLinux flow. Once users are content that the module is working, they can then add it to the rootfs.

Building the Linux Image:

petalinux-create -t project -s <path to bsp>.bsp
cd <plnx proj>

PetaLinux uses a TMP directory for the staging files. These are normally removed to save space on your disk. Open the petalinuxbsp.conf and add the line below:

RM_WORK_EXCLUDE += "linux-xlnx"

Note the location of the TMP directory.

This can be found below:

  • petalinux-config
    • Yocto Settings
      • TMPDIR Location


Now build the PetaLinux Project:

cd images/linux
petalinux-package --boot --fpga system.bit --u-boot

Creating the Module:

I have a very simple module shown below that we can use for demo purposes:


#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */

static int __init hello_start(void)
printk(KERN_INFO "Loading hello module...\n");
printk(KERN_INFO "Hello world\n");
return 0;

static void __exit hello_end(void)
printk(KERN_INFO "Goodbye!! \n");



 Creating the Makefile:


export ARCH:=arm64
export CROSS_COMPILE:=aarch64-linux-gnu-


obj-m = test_module.o

KVERSION = /tmp/xilinx-zcu104-2020.1-2020.10.23-10.47.08-32S/work/zcu104_zynqmp-xilinx-linux/linux-xlnx/5.4+gitAUTOINC+22b71b4162-r0/linux-zcu104_zynqmp-standard-build/
PWD := $(shell pwd)

make -C $(KVERSION) M=$(PWD) modules
make -C $(KVERSION) M=$(PWD) clean


Note the TMPDIR, which is the location we obtained above.

Building the module:

Then simply call the makefile to build:


Testing on Hardware:

Users can boot Linux, and use SSH to copy the module to the rootfs, or they can copy this onto an SD card.

Here, I used insmod and rmmod to add and then remove the module:



I tried to use this method to compile a kernel module but, after running petalinux-build with #INHERIT += "rm_work", I cannot identify the correct directory to assign to KVERSION.  What should one look for?  I'm using Petalinux 2019.2.

Xilinx Employee
Xilinx Employee

would be something similar to below for 2019.2: