Linux Command Line 解析

0 处理模型

Linux kernel的启动包括很多组件的初始化和相关配置,这些配置参数一般是通过command line进行配置的。在进行后续分析之前,先来理解一下command line的处理模型:
要处理的对象是一个字符串,其中包含了各种配置信息,通常各个配置之间通过空格进行分离,每个配置的表达形式是如:param=value1,value2 或者很简单就是一个rw。
那么kernel就需要提供对这些参数进行处理的处理函数列表。根据参数的作用以及执行期的先后不同,这些处理函数被定义到不同的段中。针对每一个参数,Kernel都会到相应的段中查找相应的处理函数,最终进行各个组件的配置。

1 配置格式

常见的配置格式如:
 
console=ttySAC0,115200 root=nfs nfsroot=192.168.1.9:/source/rootfs initrd=0x10800000,0x14af47
 

2 配置方式

2.1 Bootloader动态配置

由bootloader进行参数配置,command line将做为atag_list的一个节点传递到Kernel。

2.2 Kernel静态配置

通过make menuconfig进行配置:运行后配置boot options->Default kernel command string。该配置将被静态编译到Kernel中,通过变量default_command_line访问。

3  解析配置

3.1 相关定义

根据执行的先后顺序,可以将处理函数分为三个大类,他们分别存在于下面三个段中(参考top/arch/arm/kernel/vmlinux.lds):
 
__setup_start = .; *(.init.setup) __setup_end = .;
 
__early_begin = .; *(.early_param.init) __early_end = .;
 
__start___param = .; *(__param) __stop___param = .;
 
这三个段内存储的不是参数,而是command line参数所需要的处理函数。

3.1.1 .early_param.init段

“.early_param.init”所定义的处理相对靠前一些,它所处理的参数例如:initrd=,cachepolicy=,nocache, nowb, ecc=, vmalloc=, mem=,等等。
这些处理函数是通过__early_param宏来定义的,例如:
static void __init early_initrd(char **p)
{ …… }
__early_param("initrd=", early_initrd);
对于宏__early_param,可以在top/arch/arm/include/asm/Setup.h中找到如下定义:
 
struct early_params {
    const char *arg;
    void (*fn)(char **p);
};
#define __early_param(name,fn) \
static struct early_params __early_##fn __used \
__attribute__((__section__(".early_param.init"))) = { name, fn }
 
 
3.1.2 .init.setup
 
“.init.setup”定义的处理则要靠后一些,它所处理的参数例如:nfsroot=, ip=,等等。
这些处理函数是通过__setup宏来定义的,例如:
 
static int __init nfs_root_setup(char *line)
{ …… }
__setup("nfsroot=", nfs_root_setup);
 
对于宏__setup,可以在top/include/linux/Init.h中看到:
 
#define __setup_param(str, unique_id, fn, early) \
    static char __setup_str_##unique_id[] __initdata __aligned(1) = str; \
    static struct obs_kernel_param __setup_##unique_id \
           __used __section(.init.setup) \
           __attribute__((aligned((sizeof(long))))) \
           = { __setup_str_##unique_id, fn, early }

#define __setup(str, fn) \
    __setup_param(str, fn, fn, 0)

/* NOTE: fn is as per module_param, not __setup! Emits warning if fn
 * returns non-zero. */
#define early_param(str, fn) \
    __setup_param(str, fn, fn, 1)

 
注意看的话,可以看到还有一个宏 early_param,它与宏__setup的定义相似,只不过最后一个宏参数是1而不是0。1表示需要提前处理的参数。

3.1.3  __param段

这个段中保存的是build-in类型module的配置参数。该宏直接用来修饰需要的变量。

3.2  解析

3.2.1 相关变量

相关的变量包括:
default_command_line
保存memuconfig配置的参数,如果bootloader传入了命令行参数,那么这个新的配置将被更新到该变量中。
boot_command_line
存在于.init.data段。最初是default_command_line的拷贝。
command_line
存在于.init.data段。在parse_cmdline()中被赋值,数据来源是default_command_line。
saved_command_line
用于保存没有处理过的命令行参数,是boot_caommand_line的拷贝。
static_command_line
是command_line的拷贝。

3.2.2 主要函数

函数名称:parse_cmdline()
操作数据:default_command_line
函数列表:.early_param.init段(在__early_begin和__early_end之间)。
函数功能:依据函数列表对default_command_line中的参数进行处理。
 
 
函数名称:parse_early_param()
操作数据:boot_command_line
函数列表:.init.setup段中(__setup_start和__setup_end之间),主要是通过宏early_param定义的部分。
函数功能:依据函数列表对boot_command_line中的参数进行处理。
注意parse_one()的第四个入参是0,而且第五个参数是NULL。这里没有给出参数队列,不会对boot_command_line的每个参数在参数队列中进行对比查找,而是直接在do_early_param()中进行条件判断,如果满足下面的条件,那么对该参数进行对应的操作:
 
if ((p->early && strcmp(param, p->str) == 0) ||
                  (strcmp(param, "console") == 0 &&
                   strcmp(p->str, "earlycon") == 0)
              )
 
 
函数名称:parse_args()
操作数据:static_command_line
函数列表:__param段(__start___param和__stop___param之间)。
函数功能:该操作将依据函数列表,对static_command_line中的参数进行相应的操作。这个操作在parse_one()的第一部分代码完成:
 
for (i = 0; i < num_params; i++) {
          if (parameq(param, params[i].name)) {
                 DEBUGP("They are equal! Calling %p\n",
                        params[i].set);
                 return params[i].set(val, ¶ms[i]);
          }
   }
 
接下来对于不被这个列表所支持的参数,将在unknown_bootoption()中进行处理。在unknown_bootoption()中主要是obsolete_checksetup()的操作。
 
 
函数名称:obsolete_checksetup()
操作数据:static_command_line
函数列表:.init.setup段中(__setup_start和__setup_end之间),主要是通过宏__setup定义的部分。
函数功能:该操作将依据函数列表,对static_command_line中的参数进行相应的操作。如果是在parse_early_param()中已经处理的操作,那么这里不再处理;如果是查找到的条目中没有操作函数,那么这表示是过时的数据定义(有些早期的代码,没有定义这个函数);如果不是以上两种情形,那么利用找到的函数对参数进行处理。

Linux Command Line 解析的更多相关文章

  1. 《The Linux Command Line》 读书笔记04 Linux用户以及权限相关命令

    Linux用户以及权限相关命令 查看身份 id:Display user identity. 这个命令的输出会显示uid,gid和用户所属的组. uid即user ID,这是账户创建时被赋予的. gi ...

  2. 《The Linux Command Line》 读书笔记02 关于命令的命令

    <The Linux Command Line> 读书笔记02 关于命令的命令 命令的四种类型 type type—Indicate how a command name is inter ...

  3. 《The Linux Command Line》 读书笔记01 基本命令介绍

    <The Linux Command Line> 读书笔记01 基本命令介绍 1. What is the Shell? The Shell is a program that takes ...

  4. Linux Command Line Basics

    Most of this note comes from the Beginning the Linux Command Line, Second Edition by Sander van Vugt ...

  5. 15 Examples To Master Linux Command Line History

    When you are using Linux command line frequently, using the history effectively can be a major produ ...

  6. 10 Interesting Linux Command Line Tricks and Tips Worth Knowing

    I passionately enjoy working with commands as they offer more control over a Linux system than GUIs( ...

  7. Reso | The Linux Command Line 的中文版

    http://book.haoduoshipin.com/tlcl/book/zh/ 本书是 The Linux Command Line 的中文版, 为大家提供了多种不同的阅读方式. 中英文双语版- ...

  8. [笔记]The Linux command line

    Notes on The Linux Command Line (by W. E. Shotts Jr.) edited by Gopher 感觉博客园是不是搞了什么CSS在里头--在博客园显示效果挺 ...

  9. Linux Command Line(II): Intermediate

    Prerequisite: Linux Command Line(I): Beginner ================================ File I/O $ cat > a ...

随机推荐

  1. opencv车道线检测

    opencv车道线检测 完成的功能 图像裁剪:通过设定图像ROI区域,拷贝图像获得裁剪图像 反透视变换:用的是老师给的视频,没有对应的变换矩阵.所以建立二维坐标,通过四点映射的方法计算矩阵,进行反透视 ...

  2. c读取文本文档

    想数一下文本文档一共有多少行,写了个小程序 1.用fopen()以只读方式打开文件 2.用fgetc()获取文件流中的字符内容 3.如果字符内容为'\n'换行符,count++ 最后输出count的值 ...

  3. B-tree/B+tree/B*tree [转]

    (原文出处:http://blog.csdn.net/hbhhww/article/details/8206846) B~树 1.前言: 动态查找树主要有:二叉查找树(Binary Search Tr ...

  4. 四则运算出题器(C++)-BUG修复

    定制题目数量这个功能测试: (1)输入题目数为负数时: 可正确处理: (2)输入题目数量为0时: 可正确处理: (3)输入题目数量为小数时: 程序运行出错: 错误分析: 因为代码中题目数量的变量Que ...

  5. Hibernate 检索查询的几种方式(HQL,QBC,本地SQL,集成Spring等)

    1.非集成Spring hibernate的检索方式,主要有以下五种. 1.导航对象图检索方式.(根据已经加载的对象,导航到其他对象.) 2.OID检索方式.(按照对象的OID来检索对象.) 3.HQ ...

  6. 使用JavaScript+Html创建win8应用(一)

            最近在学习win8 metro app的开发,今天刚刚学了一个小的例子,分享一下 开始之前你需要准备... 1.开发win8应用需要具备Windows 8 和 Microsoft Vi ...

  7. java笔记之变量的存储方式

    1.java变量存储域 java变量的存储区域主要放在以下几个地方: (1)寄存器:可以说是最快的存储区,在C/C++中可以声明寄存器变量,但是在java中不能声明寄存器变量,只是编译器在编译时确定. ...

  8. WWDC2014 debugging_in_xcode_6 总结

    1.GCD的waiting queue 可以在Xcode左侧的面板中查看. 2.可以对View进行debug类似reveal. 3.查看运行时的对象,对于自定义对象可以实现- (id)debugQui ...

  9. SqlServer 临时表、表变量、函数 替代游标

    http://www.cnblogs.com/chongzi/archive/2011/01/19/1939106.html 临时表 存放在tempdb中 --存储过程中将多表连接结果写入到临时表中, ...

  10. hdu 3061 Battle 最大权闭合图

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3061 由于小白同学近期习武十分刻苦,很快被晋升为天策军的统帅.而他上任的第一天,就面对了一场极其困难的 ...