• 通过 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文件相应的位置。做完以上工作就可以按照内核构建步骤去构建新的内核。


    #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
    ", 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 != '') { 
                    printk("incorrect  setup_example_int_array paramters: %s
    ", 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("
    "); 
                    printk("setup_example_int_array includes %d intergers
    ",  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, 
    "); 
                    printk("maximum length  is less than or equal to 15
    "); 
            } else { 
                    memcpy(setup_example_string,  s, strlen(s) + 1); 
                    printk("setup_example_string=%s
    ", 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=1234setup_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 未本地化版本许可协议进行许可。

    
  • 相关阅读:
    Abstract与Virtual
    List 常用方法
    控制数据采样分布 计算概念
    中期答辩感想
    软件开发之团队理解
    详细设计理解
    竞赛系统需求分析
    软件代码规范之理解
    AngularJS 整理资料
    AngularJS合集
  • 原文地址:https://www.cnblogs.com/youngerchina/p/5624549.html
Copyright © 2020-2023  润新知