问题描述
我想通过引导时在引导程序中配置的引导选项将一些参数传递给自定义的Linux初始化.
I would like to pass some parameters to a customized Linux init via the boot options configured in the bootloader at boot.
我已经用Python和C编写了测试init.Python版本能够看到内核引导选项中没有'='或''的任何内容.在里面.可以在sys.argv中找到这些值.但是,C程序似乎没有传递值.我以为Python中的sys.argv列表是通过解析** argv数组生成的.以下是测试脚本和屏幕截图,有望有助于澄清这些问题.
I've written test init's in both Python and C. The Python version is able to see anything in the kernel boot options that doesn't have a '=' or '.' in it. The values are found in sys.argv. However, the C program doesn't seem to get passed the values. I would have thought the sys.argv list in Python was generated by parsing the **argv array. Below are the test scripts and screen shots that will hopefully help clarify.
内核启动行是:
kernel /grub/linux-2.6.38.4 root=/dev/vda1 init=/argv-{p|c} one two three four five
Python版本:
#!/usr/bin/python
import sys
i = 0
print("Printing argv[] (Python) ...")
for each in range(i, len(sys.argv)):
print("argv[%d] - %s" % (i, sys.argv[i]))
i += 1
print("...finished printing argv[]")
C版本:
#include <stdio.h>
int main(int argc, char **argv)
{
int i;
printf("Printing argv[] (C) ...\n");
for(i; i < argc; i++) {
printf("argv[%d] - %s\n", i, argv[i]);
}
printf("...finished printing argv[]\n");
}
您可以看到在测试程序退出(并引起恐慌)之前,python版本吐出了内核没有消化而C版本没有消化的启动选项.我查看了sysvinit源代码,对我(不是C开发人员)来说,它的工作方式相同吗?
You can see just before the test programs exit (and causes panic) the python version spits out the boot options the kernel didn't digest while the C version didn't. I've looked at the sysvinit source code and it looks to me (not being a C dev) that it works the same way?
如何将引导选项传递给C初始化程序?
How do I get the boot options passed to my C init program?
(哦,C程序在不作为init运行时会按预期工作)
(oh, and the C program works as expected when not being run as init)
推荐答案
我不知道C,但是我认为int i;
在哪里(第4行)应该是int i = 0;
.如果输入有误,请在答案中添加评论,然后将其删除.
I don't know C, but I think where is int i;
(line 4) should be int i = 0;
. If I am wrong, add a comment to my answer and I will delete it.
您也可以在for循环中执行i = 0
:for(i = 0; i < argc; i++)
.
you could also do i = 0
in the for loop: for(i = 0; i < argc; i++)
.
这篇关于将Linux引导传递给Init的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!