简体   繁体   English

switch_root busybox初始化有问题吗?

[英]switch_root busybox init problems?

I'm in an embedded linux environment with busybox. 我在带有busybox的嵌入式Linux环境中。 I've read through several posts trying to learn how to use switch_root . 我已经通读了几篇 文章,试图学习如何使用switch_root I tried this: 我尝试了这个:

exec switch_root -c /dev/console /mnt/newroot /bin/busybox init

The switch_root help prints and I am presented with a new login: 将显示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:

When I login, newroot has not been loaded, old one is still there. 当我登录时, newroot尚未加载,旧的仍然存在。 Is this because I am running this command directly from command line and not from some kind of init script? 这是因为我直接从命令行而不是某种初始化脚本运行此命令吗?

I read in this article and found they perform other steps before running switch_root : 我阅读了本文,发现它们在运行switch_root之前执行其他步骤:

mount --move /sys /newroot/sys
mount --move /proc /newroot/proc
mount --move /dev /newroot/dev

First off, this confuses me. 首先,这使我感到困惑。 Why would I want to run these commands before running switch_root ? 为什么要在运行switch_root之前运行这些命令? Does switch_root not do this for me? switch_root不帮我吗?

Anyways, I went ahead and tried running them first and then running my switch_root command. 无论如何,我继续尝试先运行它们,然后再运行switch_root命令。 However, this hoses things up entirely: 但是,这完全可以解决问题:

[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 ...

So it looks like because I've moved the mount for dev that when my init runs and trys to put the getty on my serial port it can't find it? 如此看来,因为我已经移动了dev的安装架,所以当我的init运行并尝试将getty放在我的串行端口上时,找不到它了吗? Confusion........ 混乱........

Am I missing something fundamental about switch_root here? 我在这里缺少关于switch_root基本switch_root吗? Or some simply command line modifications required? 还是需要一些简单的命令行修改?

First, as its helptext says switch_root has to be executed as PID 1. Therefore it needs to be called by an initscript using exec . 首先,正如其帮助文本所说, switch_root必须作为PID 1执行。因此,它必须由使用exec的初始化脚本来调用。

Second, moving the tmpfilesystems manually is (as you saw) a bad idea. 其次,(如您所见)手动移动tmpfilesystems是一个坏主意。 The error you get is because your console ( /dev/ttyS0 ) was moved away with your mount --move call. 您收到的错误是因为您的控制台( /dev/ttyS0 )已通过mount --move调用mount --move switch_root will delete those mounts automatically. switch_root将自动删除那些安装。 Therefore your second init (which is called by switch_root ) needs to mount them again. 因此,您的第二次初始化(由switch_root )需要再次安装它们。

Third, here's a short version of the init script I'm using within my initramfs which should help you: 第三,这是我在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" "$@"

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM