将 Linux 引导选项传递给 Init

发布于 2024-11-06 13:52:23 字数 1196 浏览 0 评论 0 原文

我想通过引导加载程序在引导时配置的引导选项将一些参数传递给自定义的 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[]")

Python init argv test

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");
}

C init argv test

您可以在测试程序退出(并导致恐慌)之前看到,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[]")

Python init argv test

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");
}

C init argv test

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╭ゆ眷念 2024-11-13 13:52:24

正如 Artur 所说,您需要将 i 初始化为 0。如果你不知道, i 的值就是程序运行时内存中的值。有时它会起作用,其他情况下 i 会 >= argc 并且循环会被跳过,最坏的情况 i 为负并且你的程序会出现段错误。

另外在 python 中尝试:

# i do not need to be initialized
# for counting xrange is better, it does not built the whole list on memory
for i in xrange(1, len(sys.argv)):
    print("arg[%d] - %s" % (i, sys.argv))
    # i do not need to be incremented manually

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:

# i do not need to be initialized
# for counting xrange is better, it does not built the whole list on memory
for i in xrange(1, len(sys.argv)):
    print("arg[%d] - %s" % (i, sys.argv))
    # i do not need to be incremented manually
夏了南城 2024-11-13 13:52:23

我不懂 C,但我认为 int i; 在哪里(第 4 行)应该是 int i = 0;。如果我错了,请在我的答案中添加评论,我会删除它。

编辑:您还可以在 for 循环中执行 i = 0for(i = 0; i

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.

Edit: you could also do i = 0 in the for loop: for(i = 0; i < argc; i++).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文