通过bootloader向内核传输启动参数

作者:Younger Liu,本作品采用知识共享署名-非商业性使用-相同方式共享 3.0 未本地化版本许可协议进行许可。

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

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

__setup("para_name=", parse_func)

其中:@para_name为参数名;@parse_func为分析参数值的函数,它负责把该参数的值转换成相应的内核变量的值并设置那个内核变量。

内核为整数参数值的分析提供了函数 get_option get_options,前者用于分析参数值为一个整数的情况,而后者用于分析参数值为逗号分割的一系列整数的情况,对于参数值为字符串的情况,需要开发者自定义相应的分析函数。

 

1          setup使用案例

在文件./mm/slub.c

static int __init setup_slub_min_order(char *str)
{
    get_option(&str, &slub_min_order);
    return 1;
}
__setup("slub_min_order=", setup_slub_min_order);

static int __init setup_slub_max_order(char *str)
{
    get_option(&str, &slub_max_order);
    slub_max_order = min(slub_max_order, MAX_ORDER - 1);
    return 1;
}
__setup("slub_max_order=", setup_slub_max_order);

 

2          内核程序kern-boot-params测试

在源代码包中的内核程序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文件相应的位置。做完以上工作就可以按照内核构建步骤去构建新的内核。

//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_example_int=", parse_int);
__setup("setup_example_int_array=", parse_int_string);
__setup("setup_example_string=", parse_string);

 

3          变量配置方法

在构建好内核并设置好lilogrub为该内核的启动条目后,就可以启动该内核,然后使用lilogrub的编辑功能为该内核的启动参数行增加如下参数串:

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

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

[root@RedHat ~]# cat /boot/grub/menu.lst
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/mapper/VolGroup-lv_root
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=3
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux Server (3.2.0trace)
root (hd0,0)
kernel /vmlinuz-3.2.0trace ro root=/dev/mapper/VolGroup-lv_root rd_LVM_LV=VolGroup/lv_root rd_LVM_LV=VolGroup/lv_swap rd_NO_LUKS rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet setup_example_int=1234 setup_example_int_array=10,20,30,40 setup_example_string=lewiyon
        initrd /initramfs-3.2.0trace.img

title Red Hat Enterprise Linux (2.6.32-71.el6.i686)
root (hd0,0)
kernel /vmlinuz-2.6.32-71.el6.i686 ro root=/dev/mapper/VolGroup-lv_root rd_LVM_LV=VolGroup/lv_root rd_LVM_LV=VolGroup/lv_swap rd_NO_LUKS rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet
initrd /initramfs-2.6.32-71.el6.i686.img
[root@RedHat ~]#

 

使用dmesg | grep setup来查看该程序的输出。
[root@RedHat ~]# dmesg | grep setup
setup_percpu: NR_CPUS:32 nr_cpumask_bits:32 nr_cpu_ids:1 nr_node_ids:1
 
Kernel command line: ro root=/dev/mapper/VolGroup-lv_root rd_LVM_LV=VolGroup/lv_root rd_LVM_LV=VolGroup/lv_swap rd_NO_LUKS rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet setup_example_int=1234 setup_example_int_array=10,20,30,40 setup_example_string=lewiyon
setup_example_int=1234
setup_example_int_array=10,20,30,40,
setup_example_int_array includes 4 intergers
setup_example_string=lewiyon
[root@RedHat ~]#
 

4          参考文献

http://www.ibm.com/developerworks/cn/linux/l-kerns-usrs/

作者:Younger Liu,本作品采用知识共享署名-非商业性使用-相同方式共享 3.0 未本地化版本许可协议进行许可。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YoungerChina

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值