【问题标题】:switch_root busybox init problems?switch_rootbusybox 初始化问题?
【发布时间】:2015-06-12 23:26:45
【问题描述】:

我在带有busybox 的嵌入式Linux 环境中。我已经阅读了severalposts,试图学习如何使用switch_root。我试过这个:

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

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:

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

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

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

首先,这让我很困惑。为什么我要在运行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 ...

所以看起来因为我已经为 dev 移动了挂载,当我的 init 运行并尝试将 getty 放在我的串行端口上时它找不到它?混乱…………

我在这里错过了关于switch_root 的一些基本信息吗?还是需要一些简单的命令行修改?

【问题讨论】:

    标签: linux rootfs


    【解决方案1】:

    首先,正如它的帮助文本所说,switch_root 必须作为 PID 1 执行。 因此,它需要由使用exec 的initscript 调用。

    其次,手动移动 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" "$@"
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-25
      • 2011-10-13
      • 2011-10-29
      • 2020-10-17
      • 2020-09-24
      • 2012-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多