pin*_*ept 5 linux util-linux busybox sd-card
我正在研究使用 initramfs 启动的嵌入式 ARM Linux 系统。(这里有一些早期 问题的背景知识,如果你感兴趣的话。)到目前为止,部分感谢在这里收到的帮助,我可以通过带有嵌入式 initramfs 的 TFTP 引导内核。MMC 驱动程序检测到包含新根文件系统的 SD 卡,然后我可以挂载它。但是,我无法完成最后一步,即使用 busybox switch_root 切换到 SD 卡上的文件系统来工作。
在 initramfs shell 提示符下,我认为这应该使内核切换到新的文件系统:
switch_root -c /dev/console /mnt/root /sbin/init.sysvinit
Run Code Online (Sandbox Code Playgroud)
然而,它只是让busybox( switch_root 别名)打印其手册页,如下所示:
/ # switch_root -c /dev/console /mnt/root /sbin/init.sysvinit
BusyBox v1.17.4 (2010-12-08 17:01:07 EST) multi-call binary.
Usage: switch_root [-c /dev/console] NEW_ROOT NEW_INIT [ARGS]
Free initramfs and switch to another root fs:
chroot to NEW_ROOT, delete all in /, move NEW_ROOT to /,
execute NEW_INIT. PID must be 1. NEW_ROOT must be a mountpoint.
Options:
-c DEV Reopen stdio to DEV after switch
Run Code Online (Sandbox Code Playgroud)
我认为 -c 选项是正确的,因为它与示例中包含的内容相同,并且 /dev/console 存在。
/ # ls -l /dev
total 0
crw-r--r-- 1 0 0 5, 1 Jan 1 00:28 console
brw-r--r-- 1 0 0 7, 0 Dec 21 2010 loop0
brw-r--r-- 1 0 0 179, 0 Dec 21 2010 mmcblk0
brw-r--r-- 1 0 0 179, 1 Dec 21 2010 mmcblk0p1
brw-r--r-- 1 0 0 179, 2 Dec 21 2010 mmcblk0p2
brw-r--r-- 1 0 0 179, 3 Dec 21 2010 mmcblk0p3
brw-r--r-- 1 0 0 179, 4 Dec 21 2010 mmcblk0p4
Run Code Online (Sandbox Code Playgroud)
/mnt/root 也存在。
/ # ls /mnt/root
bin etc linuxrc mnt sys var
boot home lost+found proc tmp
dev lib media sbin usr
Run Code Online (Sandbox Code Playgroud)
init 可执行文件存在:
/ # ls -lh /mnt/root/sbin/
<snip>
lrwxrwxrwx 1 0 0 19 Dec 21 2010 init -> /sbin/init.sysvinit
-rwxr-xr-x 1 0 0 22.8K Dec 21 2010 init.sysvinit
Run Code Online (Sandbox Code Playgroud)
但这里有一些奇怪的事情:
/mnt/root/sbin # pwd
/mnt/root/sbin
/mnt/root/sbin # ls -l | grep init.sysvinit
lrwxrwxrwx 1 0 0 19 Dec 21 2010 init -> /sbin/init.sysvinit
-rwxr-xr-x 1 0 0 23364 Dec 21 2010 init.sysvinit
/mnt/root/sbin # ./init.sysvinit
/bin/sh: ./init.sysvinit: not found
/mnt/root/sbin # /mnt/root/sbin/init.sysvinit
/bin/sh: /mnt/root/sbin/init.sysvinit: not found
Run Code Online (Sandbox Code Playgroud)
这完全令人费解。我不确定我哪里出错了。我查看了源代码,它位于http://git.busybox.net/busybox/tree/util-linux/switch_root.c?id=1_17_4
这不仅仅是 init.sysvinit 可执行文件。我无法从 SD 卡执行任何操作。例如:
/mnt/root/bin # ./busybox
/bin/sh: ./busybox: not found
/mnt/root/bin # /mnt/root/busybox
/bin/sh: /mnt/root/busybox: not found
/mnt/root/bin # ls -l | grep "2010 busybox"
-rwxr-xr-x 1 0 0 462028 Dec 21 2010 busybox
Run Code Online (Sandbox Code Playgroud)
有人知道这里有什么问题吗?我认为挂载卡 noexec 可能存在一些问题,但我相信 exec 是默认值,我尝试在挂载时显式传递 exec 选项,但没有成功。
cam*_*amh 14
switch_root
在命令行上不起作用的原因是busybox中的这段代码:
if (st.st_dev == rootdev || getpid() != 1) {
// Show usage, it says new root must be a mountpoint
// and we must be PID 1
bb_show_usage();
}
Run Code Online (Sandbox Code Playgroud)
您不是 PID 1,因此您正在陷入困境bb_show_usage
。这意味着switch_root
initramfs init 脚本中的命令应该switch_root
以exec
. IE
exec switch_root ...
Run Code Online (Sandbox Code Playgroud)
“未找到”错误的另一个问题可能是因为找不到可执行文件所需的共享库,因为 initramfs 根文件系统没有它们。如果您可以开始switch_root
使用exec
,那么“未找到”错误很可能会消失。