gpt4 book ai didi

linux - chroot "no such file or directory"打印错误的丢失文件

转载 作者:行者123 更新时间:2023-12-02 16:03:32 27 4
gpt4 key购买 nike

我知道类似的问题在这里被问了十亿次,但与我让我的系统正常工作的问题不同。当我破坏它时,我只是收到了错误的错误消息(我想使用这些调试另一个问题,所以它们的工作至关重要)。

从工作系统开始:

$ tree
.
├── bin
│   └── bash
├── lib
│   ├── libc.so.6
│   ├── libdl.so.2
│   └── libtinfo.so.6
└── lib64
└── ld-linux-x86-64.so.2
$ sudo chroot . /bin/bash
bash-5.0#

正如我们所期望的那样,运行 bash 的一切都在那里并且 bash 运行。

现在,当我删除 lib 文件夹中的任何内容时,我收到一条错误消息,提示我缺少该库:

$ rm -f lib/libdl.so.2
$ sudo chroot . /bin/bash
/bin/bash: error while loading shared libraries: libdl.so.2: cannot open shared object file: No such file or directory

也符合预期。但是,当我删除 lib64 文件夹中的 ld-linux-x86-64.so.2 时:

$ rm -f lib64/ld-linux-x86-64.so.2
$ sudo chroot . /bin/bash
chroot: failed to run command ‘/bin/bash’: No such file or directory

它告诉我 /bin/bash 丢失了。该消息与我实际删除它时的消息相同。

$ rm -f bin/bash
$ sudo chroot . /bin/bash
chroot: failed to run command ‘/bin/bash’: No such file or directory

所以出于某种原因,它认为 bash 丢失了,而实际上,我认为是动态链接器丢失了。我认为这是因为它首先使用此链接器加载小 Sprite ,但这并不能使消息更正确。

我什至通过在 qemu 中运行 bash 来检查 ld-linux-x86-64.so.2 > 在不同的系统上我得到正确的错误信息:

<some arm system>$ qemu-x86_64 -L /tmp/nowhere bin/bash
/lib64/ld-linux-x86-64.so.2: No such file or directory

这是一个错误吗?是否有一些选项可以告诉 chroot 不要这样做并打印实际丢失的文件?这个文件有什么魔力吗?这是怎么回事?

TLDR:为什么 chroot 告诉我可执行文件丢失,而实际上 lib64/ld-linux-x86-64.so.2是吗?

最佳答案

Why does chroot tell me the executable is missing when actuallylib64/ld-linux-x86-64.so.2 is?

假设您正在使用来自 GNU coreutils 的 chroot 程序,我们可以查看代码以了解发生了什么(希望“魔法”会消失)。这里是github mirror of chroot.c .

如果我们在错误消息 failed to run command 中搜索字符串,我们会立即找到打印它的(唯一)代码行:

  /* Execute the given command.  */
execvp (argv[0], argv);

int exit_status = errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE;
error (0, errno, _("failed to run command %s"), quote (argv[0]));
return exit_status;
}

如您所见,它是在 execvp() 系统调用之后打印的。 execvp() 是(的一种变体)允许执行程序的系统调用(在您的情况下为 /bin/bash )。 execvp() 如果程序执行成功,不返回,因为:

The exec() family of functions replaces the current process image with a new process image.

在出错的情况下返回并适本地设置errno

代码然后检查 errno 来决定退出状态:

int exit_status = errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE;

最后打印错误:

error (0, errno, _("failed to run command %s"), quote (argv[0]));

如您所见,argv[0] 总是运行命令失败后打印(/bin/bash 在你的例子中,即它试图在 chroot 环境中执行的程序。

errno 是由 execvp() “返回”的错误编号,并确定在(error() 定义如下)之后打印的内容在我的系统上):

/* Print a message with `fprintf (stderr, FORMAT, ...)';
if ERRNUM is nonzero, follow it with ": " and strerror (ERRNUM).
If STATUS is nonzero, terminate the program with `exit (STATUS)'. */

extern void error (int __status, int __errnum, const char *__format, ...)

No such file or directory 是错误编号 ENOENT 并且在以下情况下由 execvp() 和 friend “返回”:

ENOENT The file pathname or a script or ELF interpreter does not exist.

(“ELF 解释器”是动态链接器的同义词 I guess )

chroot 实际上无法知道真正出了什么问题,它只能报告 execvp() 放入 errno< 中的内容 我认为这就是错误含糊不清且有点误导的原因。

关于linux - chroot "no such file or directory"打印错误的丢失文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70056765/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com