我想通过引导加载程序在引导时配置的引导选项将一些参数传递给自定义的 Linux init。
我已经用 Python 和 C 编写了测试 init。Python 版本能够看到内核启动选项中没有“=”或“.”的任何内容。在其中。这些值可在 sys.argv 中找到。然而,C 程序似乎没有传递这些值。我本以为Python中的sys.argv列表是通过解析**argv数组生成的。以下是测试脚本和屏幕截图,希望有助于澄清。
内核引导行是:
kernel /grub/linux-2.6.38.4 root=/dev/vda1 init=/argv-{p|c} one 二三四五
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 开发人员)看来它的工作方式相同?
如何将引导选项传递给我的 C init 程序?
(哦,C 程序在不作为 init 运行时按预期工作)
I would like to pass some parameters to a customized Linux init via the boot options configured in the bootloader at boot.
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.
the kernel boot line is:
kernel /grub/linux-2.6.38.4 root=/dev/vda1 init=/argv-{p|c} one two three four five
Python version:
#!/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 version:
#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");
}
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?
How do I get the boot options passed to my C init program?
(oh, and the C program works as expected when not being run as init)
发布评论
评论(2)
正如 Artur 所说,您需要将 i 初始化为 0。如果你不知道, i 的值就是程序运行时内存中的值。有时它会起作用,其他情况下 i 会 >= argc 并且循环会被跳过,最坏的情况 i 为负并且你的程序会出现段错误。
另外在 python 中尝试:
You need to initialize i to 0 as Artur said. If you dont the value of i is whatever happend to be in the memory at the time the program ran. Sometimes it will work, others i would be >= argc and the loop would be skipped, the worst case i is negative and your program segfaults.
Also in python try:
我不懂 C,但我认为
int i;
在哪里(第 4 行)应该是int i = 0;
。如果我错了,请在我的答案中添加评论,我会删除它。编辑:您还可以在 for 循环中执行
i = 0
:for(i = 0; i。
I don't know C, but I think where is
int i;
(line 4) should beint i = 0;
. If I am wrong, add a comment to my answer and I will delete it.Edit: you could also do
i = 0
in the for loop:for(i = 0; i < argc; i++)
.