1
votes

I am new to linux device driver development. I am trying to write a simple hello world module that would display the version of kernel running on the system where I try to insert hello_world module into kernel.

I used LINUX_VERSION_CODE in version.h to get linux version and built the module. When I try to insert the ko file on a different system other than where it was built, it still shows the version of kernel where it was built

I believe issue lies with using C macro. Can someone help me how to find linux version of local machine where the ko is to be inserted instead of finding version of kernel where my module gets built

1

1 Answers

1
votes

You're right - LINUX_VERSION_CODE is a macro that provides compile-time info about the version of the Linux headers that you're using to compile the module. The macro cannot have any knowledge about the version of the kernel that the module will actually be loaded into.

The utsname() function in <linux/utsname.h> provides a pointer to a new_utsname struct, which has sysname, release and version members that contain what you're looking for.

The information from these members is used in /proc/version, as shown in fs/proc/version.c :

static int version_proc_show(struct seq_file *m, void *v)
{
    seq_printf(m, linux_proc_banner,
        utsname()->sysname,
        utsname()->release,
        utsname()->version);
    return 0;
}

linux_proc_banner is a string that is currently defined as follows :

const char linux_proc_banner[] =
    "%s version %s"
    " (" LINUX_COMPILE_BY "@" LINUX_COMPILE_HOST ")"
    " (" LINUX_COMPILER ") %s\n";

On my system, reading /proc/version - and thus reading these members - results in obtaining the following string :

Linux version 4.1.6-1-ARCH (builduser@tobias) (gcc version 5.2.0 (GCC) ) #1 SMP PREEMPT Mon Aug 17 08:52:28 CEST 2015

Thus, sysname is Linux, release is 4.1.6-1-ARCH, and version is #1 SMP PREEMPT Mon Aug 17 08:52:28 CEST 2015.