2.1.3 Kernel command line: ro root=/dev/hda1

来源:互联网 发布:mysql增删改查 面试 编辑:程序博客网 时间:2024/05/01 11:01

from 精通Linux设备驱动程序开发

 

2.1.3 Kernel command line: ro root=/dev/hda1

Linux的引导装入程序通常会给内核传递一个命令行。命令行中的参数类似于传递给C程序中main()函数的argv[]列表,唯一的不同在于它们是传递给内核的。可以在引导装入程序的配置文件中增加命令行参数,当然,也可以在运行过程中通过引导装入程序修改Linux的命令行 。如果使用的是GRUB这个引导装入程序,由于发行版本的不同,其配置文件可能是/boot/grub/grub.conf或者是/boot/grub/menu.lst。如果使用的是LILO,配置文件为/etc/lilo.conf。下面给出了一个grub.conf文件的例子(增加了一些注释),看了紧接着title kernel 2.6.23的那行代码之后,你会明白前述打印信息的由来。

  1. default 0  #Boot the 2.6.23 kernel by default  
  2. timeout 5  #5 second to alter boot order or parameters  
  3.  
  4. title kernel 2.6.23     #Boot Option 1  
  5.   #The boot image resides in the first partition of the first disk  
  6.   #under the /boot/ directory and is named vmlinuz-2.6.23. 'ro'  
  7.   #indicates that the root partition should be mounted read-only.  
  8.   kernel (hd0,0)/boot/vmlinuz-2.6.23 ro root=/dev/hda1  
  9.  
  10.   #Look under section "Freeing initrd memory:387k freed"  
  11.   initrd (hd0,0)/boot/initrd  
  12.  
  13. #... 

命令行参数将影响启动过程中的代码执行路径。举一个例子,假设某命令行参数为bootmode,如果该参数被设置为1,意味着你希望在启动过程中打印一些调试信息并在启动结束时切换到runlevel的第3级(初始化进程的启动信息打印后就会了解runlevel的含义);如果bootmode参数被设置为0,意味着你希望启动过程相对简洁,并且设置runlevel为2。既然已经熟悉了init/main.c文件,下面就在该文件中增加如下修改:

  1. static unsigned int bootmode = 1;  
  2. static int __init  
  3. is_bootmode_setup(char *str)  
  4. {  
  5.   get_option(&str, &bootmode);  
  6.   return 1;  
  7. }  
  8.  
  9. /* Handle parameter "bootmode=" */  
  10. __setup("bootmode=", is_bootmode_setup);  
  11.  
  12. if (bootmode) {  
  13.   /* Print verbose output */  
  14.   /* ... */  
  15. }  
  16.  
  17. /* ... */  
  18.  
  19. /* If bootmode is 1, choose an init runlevel of 3, else  
  20.    switch to a run level of 2 */  
  21. if (bootmode) {  
  22.   argv_init[++args] = "3";  
  23. } else {  
  24.   argv_init[++args] = "2";  
  25. }  
  26.  
  27. /* ... */ 

请重新编译内核并尝试运行新的修改。另外,本书18.5节也将对内核命令行参数进行更详细的讲解。