簡體   English   中英

switch_root busybox初始化有問題嗎?

[英]switch_root busybox init problems?

我在帶有busybox的嵌入式Linux環境中。 我已經通讀了幾篇 文章,試圖學習如何使用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基本switch_root嗎? 還是需要一些簡單的命令行修改?

首先,正如其幫助文本所說, switch_root必須作為PID 1執行。因此,它必須由使用exec的初始化腳本來調用。

其次,(如您所見)手動移動tmpfilesystems是一個壞主意。 您收到的錯誤是因為您的控制台( /dev/ttyS0 )已通過mount --move調用mount --move switch_root將自動刪除那些安裝。 因此,您的第二次初始化(由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" "$@"

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM