switch_root busybox初始化有问题吗?

dtm*_*and 3 linux rootfs

我在带有busybox的嵌入式Linux环境中。我已经通读了几篇 文章,试图学习如何使用switch_root。我尝试了这个:

exec switch_root -c /dev/console /mnt/newroot /bin/busybox init
Run Code Online (Sandbox Code Playgroud)

switch_root帮助打印,我提出了一个新的登录:

[root@buildroot ~]# exec switch_root -c /dev/console /mnt/newroot /bin/busybox init
BusyBox v1.21.0 (2015-04-24 18:14:40 MDT) multi-call binary.
busybox init
Usage: switch_root [-c /dev/console] NEW_ROOT NEW_INIT [ARGS]root /bin/busybox in

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.

        -c DEV  Reopen stdio to DEV after switch


Welcome to Buildroot
buildroot login:
Run Code Online (Sandbox Code Playgroud)

当我登录时,newroot尚未加载,旧的仍然存在。这是因为我直接从命令行而不是某种初始化脚本运行此命令吗?

我阅读了这篇文章,发现它们在运行之前执行了其他步骤switch_root

mount --move /sys /newroot/sys
mount --move /proc /newroot/proc
mount --move /dev /newroot/dev
Run Code Online (Sandbox Code Playgroud)

首先,这使我感到困惑。为什么要在运行之前运行这些命令switch_root?不switch_root为我这样做吗?

无论如何,我继续尝试先运行它们,然后再运行switch_root命令。但是,这完全可以解决问题:

[root@buildroot /]# switch_root -c /dev/console /mnt/newroot /bin/busybox init
BusyBox v1.21.0 (2015-04-24 18:14:40 MDT) 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.

        -c DEV  Reopen stdio to DEV after switch

can't open /dev/ttyS0: No such file or directoryole /mnt/newroot /bin/busybox init
can't open /dev/ttyS0: No such file or directory
can't open /dev/ttyS0: No such file or directory
can't open /dev/ttyS0: No such file or directory
can't open /dev/ttyS0: No such file or directory
can't open /dev/ttyS0: No such file or directory
can't open /dev/ttyS0: No such file or directory
... message continues to repeat ...
Run Code Online (Sandbox Code Playgroud)

如此看来,因为我已经移动了dev的安装架,所以当我的init运行并尝试将getty放在我的串行端口上时,找不到它了吗?混乱........

switch_root在这里缺少基本的东西吗?还是需要一些简单的命令行修改?

g0h*_*l1n 5

首先,正如其帮助文本所说的那样,switch_root必须将其作为PID 1执行。因此,它需要由初始化脚本使用调用exec

其次,(如您所见)手动移动tmpfilesystems是一个坏主意。您收到的错误是因为您的控制台(/dev/ttyS0)已随mount --move呼叫移开。 switch_root将自动删除那些安装。因此,您的第二个init(由调用switch_root)需要再次安装它们。

第三,这是我在initramfs中使用的init脚本的简短版本,应该可以帮助您:

#!/bin/sh

ROOT="/mnt/.root"
ROOT_DEV="/dev/sdb2"

echo "init from initramfs"

# mount temporary filesystems
mount -n -t devtmpfs devtmpfs /dev
mount -n -t proc     proc     /proc
mount -n -t sysfs    sysfs    /sys
mount -n -t tmpfs    tmpfs    /run

# mount new root
[ -d ${ROOT} ] || mkdir -p ${ROOT}
mount ${ROOT_DEV} ${ROOT}

# switch to new rootfs and exec init
cd ${ROOT}
exec switch_root . "/sbin/init" "$@"
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。