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的那行代码之后,你会明白前述打印信息的由来。
- default 0 #Boot the 2.6.23 kernel by default
- timeout 5 #5 second to alter boot order or parameters
- title kernel 2.6.23 #Boot Option 1
- #The boot image resides in the first partition of the first disk
- #under the /boot/ directory and is named vmlinuz-2.6.23. 'ro'
- #indicates that the root partition should be mounted read-only.
- kernel (hd0,0)/boot/vmlinuz-2.6.23 ro root =/dev/hda1
- #Look under section "Freeing initrd memory:387k freed"
- initrd (hd0,0)/boot/initrd
- #...
命令行参数将影响启动过程中的代码执行路径。举一个例子,假设某命令行参数为bootmode,如果该参数被设置为1,意味着你希望在启动过程中打
印一些调试信息并在启动结束时切换到runlevel的第3级(初始化进程的启动信息打印后就会了解runlevel的含义);如果bootmode参数
被设置为0,意味着你希望启动过程相对简洁,并且设置runlevel为2。既然已经熟悉了init/main.c文件,下面就在该文件中增加如下修改:
- static unsigned int bootmode = 1 ;
- static int __init
- is_bootmode_setup(char *str)
- {
- get_option(&str, &bootmode);
- return 1;
- }
- /* Handle parameter " bootmode =" */
- __setup(" bootmode =", is_bootmode_setup);
- if (bootmode) {
- /* Print verbose output */
- /* ... */
- }
- /* ... */
- /* If bootmode is 1, choose an init runlevel of 3, else
- switch to a run level of 2 */
- if (bootmode) {
- argv_init[++args] = "3";
- } else {
- argv_init[++args] = "2";
- }
- /* ... */
请重新编译内核并尝试运行新的修改。另外,本书18.5节也将对内核命令行参数进行更详细的讲解。