Compile Kernel Modules

In this chapter, we will provide the tutorial of building Linux kernel modules for the DSM system with Package Toolkit.

If you have the requirement of compiling kernel modules manually, please refer to Appendix B: Compile Kernel Modules Manually.

Use toolkit to build kernel modules

Synology toolkit contain kernel-dev which pack all kernel developer components in. You can follow the directions in pkgscripts-ng to set the environment up.

pkgscripts-ng build framework provides several variables which are different between platforms.

  • CROSS_COMPILE: Cross-compiler toolchain prefix.
  • ARCH: Target architecture.
  • KSRC: The path of the store kernel source cross-compiled product. It contains components for compiling the kernel modules.

Example:

  • In bromolow:

    ROSS_COMPILE=/usr/local/x86_64-pc-linux-gnu/bin/x86_64-pc-linux-gnu-
    ARCH=x86_64
    KSRC=/usr/local/x86_64-pc-linux-gnu/x86_64-pc-linux-gnu/sys-root/usr/lib/modules/DSM-6.1/build
    
  • In comcerto2k:

    CROSS_COMPILE=/usr/local/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-
    ARCH=arm
    KSRC=/usr/local/arm-unknown-linux-gnueabi/arm-unknown-linux-gnueabi/sysroot/usr/lib/modules/DSM-6.1/build
    

Usage

make KSRC="$KSRC" CROSS_COMPILE="$CROSS_COMPILE" ARCH="$ARCH"

Sample package: HelloKernel

HelloKernel is a sample package for building kernel module. You can get HelloKernel at github.

Before starting

Please set up your build environment by following pkgscripts-ng.

Getting started

First, preparing a simple kernel module which prints message after insmod/rmmod the module.

#include <linux/module.h>
#include <linux/kernel.h>

int init_module(void) {
    printk(KERN_INFO "Synology HelloKernel package is installed.\n");
    return 0;
}

void cleanup_module(void) {
    printk(KERN_INFO "Synology HelloKernel package has been removed.\n");
}

Then, creating the Makefile and using KSRC variable to assign kernel source directory.

HELLO_KERNEL= hello_kernel.ko

all: $(HELLO_KERNEL)

obj-m := hello_kernel.o

$(HELLO_KERNEL):
    make -C $(KSRC) M=$(PWD) modules

install: $(HELLO_KERNEL)
    mkdir -p $(DESTDIR)/hello_kernel/
    install $< $(DESTDIR)/hello_kernel/

clean:
    rm -rf *.o $(HELLO_KERNEL) *.cmd

In order to be compatible with the framework of Synology packages, you need to create the build script(SynoBuildConf/build) and assign kernel-dev variables to Makefile for compiling kernel modules.

#!/bin/bash
# Copyright (C) 2000-2017 Synology Inc. All rights reserved.

case ${MakeClean} in
    [Yy][Ee][Ss])
        make clean
        ;;
esac

make ${MAKE_FLAGS} KSRC=$KSRC CROSS_COMPILE=$CROSS_COMPILE ARCH=$ARCH

In addition, you can use PkgCreate.py to verify the build script. Please refer to the example of armadaxp as follows:

pkgscripts-ng/PkgCreate.py -p armadaxp -I HelloKernel

After running PkgCreate.py, hello_kernel.ko will genaratedi (File path: build_env/ds.armadaxp-6.1/source/HelloKernel/hello_kernel.ko). You can run insmod hello_kernel.ko on armadaxp series model such as ds214+...etc. If kernel module insert successfully, the message "Hi~ Synology kernel package installed." will show in dmesg.

Synology provide the example programs on github[https://github.com/SynologyOpenSource/HelloKernel]. You can clone the HelloKernel repo and use PkgCreate.py to generate spk.

pkgscripts-ng/PkgCreate.py -c HelloKernel