简体   繁体   中英

how to fetch distribution or OS name in kernel module

Grabing the distribution name (ie Ubuntu, Red Hat Enterprise Linux ) is very easy through command line for example - one can fetch the information from /proc/version or lsb_release -a or uname -a or /etc/*-release but my problem is different. I want to fetch distribution name in kernel module in order to enable features on the bases of OS name.

I tried below module. Unfortunately, it didn't work as expected. I want OS name such as Ubuntu, Red Hat Enterprise Linux.

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

#define current get_current()

static int __init dummy_ModInit(void)
{
    int retVal;

    pr_info("module init\n");
    pr_info("utsname - %s\n",utsname());
    pr_info("current->nsproxy->uts_ns->name - %s\n", &current->nsproxy->uts_ns->name);
    pr_info("current->nsproxy->uts_ns->name.release - %s\n", &current->nsproxy->uts_ns->name.release);
    pr_info("current->nsproxy->uts_ns->name.version - %s\n", &current->nsproxy->uts_ns->name.version);
    pr_info("current->nsproxy->uts_ns->name.machine - %s\n", &current->nsproxy->uts_ns->name.machine);
    pr_info("current->nsproxy->uts_ns->name.domainname - %s\n", &current->nsproxy->uts_ns->name.domainname);
    pr_info("current->nsproxy->uts_ns->name.nodename - %s\n", &current->nsproxy->uts_ns->name.nodename);
    pr_info("current->nsproxy->uts_ns->name.sysname - %s\n", &current->nsproxy->uts_ns->name.sysname);

    return retVal;
}

static void __exit dummy_ModExit(void)
{
        pr_info("module exist\n");
}


module_init(dummy_ModInit);
module_exit(dummy_ModExit);

MODULE_VERSION("1.0.1.1" );
MODULE_DESCRIPTION("fetching kernel version and OSname");
MODULE_LICENSE("GPL");

Output on redhat machine:

[5254795.119390] &current->nsproxy->uts_ns->name - Linux
[5254795.119492] &current->nsproxy->uts_ns->name.release - 4.18.0-372.26.1.el8_6.x86_64
[5254795.119597] &current->nsproxy->uts_ns->name.version - #1 SMP Sat Aug 27 02:44:20 EDT 2022
[5254795.119709] &current->nsproxy->uts_ns->name.machine - x86_64
[5254795.119822] &current->nsproxy->uts_ns->name.domainname - (none)
[5254795.119936] &current->nsproxy->uts_ns->name.nodename - shaslinux
[5254795.120053] &current->nsproxy->uts_ns->name.sysname - Linux

Output on ubuntu machine:

[1075545.219864] &current->nsproxy->uts_ns->name - Linux
[1075545.219865] &current->nsproxy->uts_ns->name.release - 4.15.0-142-generic
[1075545.219865] &current->nsproxy->uts_ns->name.version - #146~16.04.1-Ubuntu SMP Tue Apr 13 09:27:15 UTC 2021
[1075545.219865] &current->nsproxy->uts_ns->name.machine - x86_64
[1075545.219866] &current->nsproxy->uts_ns->name.domainname - (none)
[1075545.219866] &current->nsproxy->uts_ns->name.nodename - shashank-linux
[1075545.219867] &current->nsproxy->uts_ns->name.sysname - Linux

Is there any way that we can read information in /proc/version by using procfs kernel api's.

Any other method such as call_usermodehelper() . I mean execute linux command in a.c or.sh file, trigger it from kernel module and runs in usermode. By using this, it will execute a program and returns success or failure. Saving results is the question here.

Appreciating any help in advance.

If you only want to tell between 2 specific distributions like redhat and ubuntu, the utsname function gives you enough info to tell them apart (mostly the release member.)

However, OS specific features are usually enabled according to a kernel version number and not by a distribution name because there isn't much difference between the kernel versions that some distributions use.

Can you be more specific about the features you want to enable so we can find an optimal solution?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM