• 用户空间与内核空间数据交换的方式(5)内核启动参数


    Linux 提供了一种通过 bootloader 向其传输启动参数的功能,内核开发者可以通过这种方式来向内核传输数据,从而控制内核启动行为。

    通常的使用方式是,定义一个分析参数的函数,而后使用内核提供的宏 __setup把它注册到内核中,该宏定义在 linux/init.h 中,因此要使用它必须包含该头文件:

    __setup("para_name=", parse_func)

    para_name 为参数名,parse_func 为分析参数值的函数,它负责把该参数的值转换成相应的内核变量的值并设置那个内核变量。内核为整数参数值的分析提供了函数 get_option 和 get_options,前者用于分析参数值为一个整数的情况,而后者用于分析参数值为逗号分割的一系列整数的情况,对于参数值为字符串的情况,需要开发者自定义相应的分析函数。在源代码包中的内核程序kern-boot-params.c 说明了三种情况的使用。该程序列举了参数为一个整数、逗号分割的整数串以及字符串三种情况,读者要想测试该程序,需要把该程序拷贝到要使用的内核的源码目录树的一个目录下,为了避免与内核其他部分混淆,作者建议在内核源码树的根目录下创建一个新目录,如 examples,然后把该程序拷贝到 examples 目录下并重新命名为 setup_example.c,并且为该目录创建一个 Makefile 文件:

    obj-y = setup_example.o

    Makefile 仅许这一行就足够了,然后需要修改源码树的根目录下的 Makefile文件的一行,把下面行

    core-y          := usr/

    修改为

    core-y          := usr/ examples/
     
    注意:如果读者创建的新目录和重新命名的文件名与上面不同,需要修改上面所说 Makefile 文件相应的位置。 做完以上工作就可以按照内核构建步骤去构建新的内核,在构建好内核并设置好lilo或grub为该内核的启动条目后,就可以启动该内核,然后使用lilo或grub的编辑功能为该内核的启动参数行增加如下参数串:

    setup_example_int=1234 setup_example_int_array=100,200,300,400 setup_example_string=Thisisatest

    当然,该参数串也可以直接写入到lilo或grub的配置文件中对应于该新内核的内核命令行参数串中。读者可以使用其它参数值来测试该功能。

    下面是作者系统上使用上面参数行的输出:

    setup_example_int=1234

    setup_example_int_array=100,200,300,400

    setup_example_int_array includes 4 intergers

    setup_example_string=Thisisatest
     

    读者可以使用$dmesg | grep setup  来查看该程序的输出。

    //filename: kern-boot-params.c
    #include <linux/kernel.h>
    #include
    <linux/init.h>
    #include
    <linux/string.h>

    #define MAX_SIZE 5
    static int setup_example_int;
    static int setup_example_int_array[MAX_SIZE];
    static char setup_example_string[16];

    static int __init parse_int(char * s)
    {
    int ret;

    ret
    = get_option(&s, &setup_example_int);
    if (ret == 1) {
    printk(
    "setup_example_int=%d\n", setup_example_int);
    }
    return 1;
    }

    static int __init parse_int_string(char *s)
    {
    char * ret_str;
    int i;

    ret_str
    = get_options(s, MAX_SIZE, setup_example_int_array);
    if (*ret_str != '\0') {
    printk(
    "incorrect setup_example_int_array paramters: %s\n", ret_str);
    }
    else {
    printk(
    "setup_example_int_array=");
    for (i=1; i<MAX_SIZE; i++) {
    printk(
    "%d", setup_example_int_array[i]);
    if (i < (MAX_SIZE -1)) {
    printk(
    ",");
    }
    }
    printk(
    "\n");
    printk(
    "setup_example_int_array includes %d intergers\n", setup_example_int_array[0]);
    }
    return 1;
    }

    static int __init parse_string(char *s)
    {
    if (strlen(s) > 15) {
    printk(
    "Too long setup_example_string parameter, \n");
    printk(
    "maximum length is less than or equal to 15\n");
    }
    else {
    memcpy(setup_example_string, s, strlen(s)
    + 1);
    printk(
    "setup_example_string=%s\n", setup_example_string);
    }
    return 1;
    }

    /*宏__setup()将分析参数的函数注册到内核中*/
    __setup(
    "setup_example_int=", parse_int);
    __setup(
    "setup_example_int_array=", parse_int_string);
    __setup(
    "setup_example_string=", parse_string);
  • 相关阅读:
    C# WebSocket 实现客户端和服务端的通信(二)
    C# WebSocket 实现客户端和服务端的通信(一)
    regsvr32 将dll写入注册表
    Dictionary 添加重复的键值对
    C# Math.Round()的银行家算法
    DataGridView 合并数据相同的行
    获取系统当前日期,分布获取年月日和时分秒
    [Err] ORA-00923: 未找到要求的 FROM 关键字
    正则表达式常用的字符类
    Spring注解作用
  • 原文地址:https://www.cnblogs.com/hoys/p/2011459.html
Copyright © 2020-2023  润新知