Skip to content

Instantly share code, notes, and snippets.

@dimtass
Last active August 19, 2020 08:38
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 dimtass/9ff4660cc0547f1ff24e532c7e6f9f4a to your computer and use it in GitHub Desktop.
Save dimtass/9ff4660cc0547f1ff24e532c7e6f9f4a to your computer and use it in GitHub Desktop.
Using netlink with docker

Use netlink with docker

Build kernel module

Place the knetlink.c and Makefile into a folder. Then build the module using your host (in my case Ubuntu)

KERNEL_SRC=/usr/src/linux-headers-$(uname -r) make

Build the user-space tool

Run this command to build the executable

gcc netlinkuser.c -o netlink

Trace the dmesg

Use this command on a separate console to trace the dmesg

sudo dmesg -wH

Load the module

Now load the kernel module on your host

sudo insmod knetlink.ko

Test the executable with docker

You can use any docker image, but I'm using the image that I use for building Yocto images. Run this command to execute the user-space netlink client inside docker.

docker run -it --rm --privileged --net=host --cap-add=ALL -v $(pwd):/docker -w /docker allwinner-yocto-image /bin/bash -c "./netlink"

This should output:

Sending message to kernel
Waiting for message from kernel
Received message payload: Hello from kernel
obj-m := knetlink.o
SRC := $(shell pwd)
all:
$(MAKE) -C $(KERNEL_SRC) M=$(SRC)
modules_install:
$(MAKE) -C $(KERNEL_SRC) M=$(SRC) modules_install
clean:
rm -f *.o *~ core .depend .*.cmd *.ko *.mod.c
rm -f Module.markers Module.symvers modules.order
rm -rf .tmp_versions Modules.symvers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment