简体   繁体   English

busybox初始化脚本。 找不到挂载

[英]busybox init script. Mount not found

I'm running in to what appears to be a bit of an odd one. 我正在尝试看似有点奇怪的东西。

Base machine is Ubuntu 18.04. 基本计算机是Ubuntu 18.04。 I'm experimenting with creating a custom initramfs + init script to use with custom compiled kernels that are being used with qemu instances. 我正在尝试创建一个自定义initramfs + init脚本,以与正与qemu实例一起使用的自定义编译内核一起使用。

From the directory I'm using as the base for the initramfs: 从目录中,我将其用作initramfs的基础:

[~/initramfs] $ find .
.
./proc
./root
./dev
./dev/console
./dev/sda1
./dev/null
./dev/tty
./sbin
./init
./etc
./lib64
./mnt
./mnt/root
./lib
./bin
./bin/busybox
./sys

Just the basics needed for now. 只是目前所需的基础知识。 The busybox binary comes from the busybox-static package, and I've confirmed it's statically complied: busybox二进制文件来自busybox-static软件包,我已经确认它是静态遵从的:

[~/initramfs]$ ldd bin/busybox
        not a dynamic executable

In the init script, I have: 在初始化脚本中,我有:

#!/bin/busybox sh

mount -t proc none /proc
mount -t sysfs none /sys

echo "Hi there"

umount /sys
umount /proc

poweroff

From there, create an initramfs.gz: 从那里创建一个initramfs.gz:

find . -print0 | cpio --null --create --verbose --format=newc | pigz --best > ~/initramfs.gz

When I set that as the target initrd for qemu, kernel starts up as expected, then: 当我将其设置为qemu的目标initrd时,内核按预期启动,然后:

[    0.777443] Run /init as init process
/init: line 3: mount: not found
/init: line 4: mount: not found
Hi there
/init: line 8: umount: not found
/init: line 9: umount: not found
/init: line 11: poweroff: not found

mount is part of busybox. 坐骑是busybox的一部分。 So that's strange. 所以这很奇怪。

If I modify the init script and put in /bin/busybox sh as the first command to be executed, that gets me to a busybox shell as you'd expect. 如果我修改init脚本并将/bin/busybox sh作为要执行的第一个命令,这将使我进入您期望的busybox shell。

[    0.789949] Run /init as init process


BusyBox v1.27.2 (Ubuntu 1:1.27.2-2ubuntu3.2) built-in shell (ash)
Enter 'help' for a list of built-in commands.

sh: can't access tty; job control turned off
/ # [    1.364618] input: ImExPS/2 Generic Explorer Mouse as /devices/platform/i8042/serio1/input/input3
[    1.386482] tsc: Refined TSC clocksource calibration: 3392.105 MHz
[    1.388387] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x30e52cb7a6c, max_idle_ns: 440795310382 ns
[    1.391965] clocksource: Switched to clocksource tsc

/ #

And then help shows: 然后帮助显示:

/ # help
Built-in commands:
------------------
        . : [ [[ alias bg break cd chdir command continue echo eval exec[   71.772009] random: fast init done

        exit export false fg getopts hash help history jobs kill let
        local printf pwd read readonly return set shift source test times
        trap true type ulimit umask unalias unset wait [ [[ acpid adjtimex
        ar arp arping ash awk basename blkdiscard blockdev brctl bunzip2
        bzcat bzip2 cal cat chgrp chmod chown chpasswd chroot chvt clear
        cmp cp cpio crond crontab cttyhack cut date dc dd deallocvt depmod
        devmem df diff dirname dmesg dnsdomainname dos2unix dpkg dpkg-deb
        du dumpkmap dumpleases echo ed egrep env expand expr factor fallocate
        false fatattr fdisk fgrep find fold free freeramdisk fsfreeze
        fstrim ftpget ftpput getopt getty grep groups gunzip gzip halt
        head hexdump hostid hostname httpd hwclock i2cdetect i2cdump
        i2cget i2cset id ifconfig ifdown ifup init insmod ionice ip ipcalc
        ipneigh kill killall klogd last less link linux32 linux64 linuxrc
        ln loadfont loadkmap logger login logname logread losetup ls
        lsmod lsscsi lzcat lzma lzop md5sum mdev microcom mkdir mkdosfs
        mke2fs mkfifo mknod mkpasswd mkswap mktemp modinfo modprobe more
        mount mt mv nameif nc netstat nl nproc nsenter nslookup od openvt
        partprobe passwd paste patch pidof ping ping6 pivot_root poweroff
        printf ps pwd rdate readlink realpath reboot renice reset rev
        rm rmdir rmmod route rpm rpm2cpio run-parts sed seq setkeycodes
        setpriv setsid sh sha1sum sha256sum sha512sum shred shuf sleep
        sort ssl_client start-stop-daemon stat static-sh strings stty
        su sulogin svc swapoff swapon switch_root sync sysctl syslogd
        tac tail tar taskset tee telnet telnetd test tftp time timeout
        top touch tr traceroute traceroute6 true truncate tty tunctl
        ubirename udhcpc udhcpd uevent umount uname uncompress unexpand
        uniq unix2dos unlink unlzma unshare unxz unzip uptime usleep
        uudecode uuencode vconfig vi w watch watchdog wc wget which who
        whoami xargs xxd xz xzcat yes zcat

So I go looking for mount, and discover which isn't found either. 因此,我去寻找挂载,并发现找不到。 Oh but it works if I prepend it with /bin/busybox to call it direct...: 哦,但是如果我在/ bin / busybox前面加上它直接调用它,它就可以工作...

/ # type mount
mount is mount
/ # which mount
sh: which: not found
/ # /bin/busybox which mount
/ #

and I can execute the commands successfully if I add /bin/busybox to them: 如果将/ bin / busybox添加到命令,则可以成功执行命令:

/ # /bin/busybox mount -t proc none /proc
/ #

It seems really random what will and what won't work from busybox, what does and what doesn't get found, eg find is fine: 看来从busybox可以正常工作的东西和不能正常工作的东西看起来真的是随机的,可以找到什么和没有发现什么,例如,find很好:

/ # find
.
./test
./sys
./bin
./bin/busybox
./lib
./mnt
./mnt/root
./lib64
./etc
./init
./sbin
./proc
./root
./dev
./dev/tty
./dev/null
./dev/sda1
./dev/console

I can work around this by prepending every command in the init file with /bin/busybox , but I'd really rather not if I don't have to! 我可以通过在初始化文件中的/bin/busybox之前添加每个命令来解决此问题,但是如果不需要的话,我真的不愿意!

You have to symlink all the applets you want, eg ln -s /bin/busybox /bin/mount . 您必须符号链接所需的所有小程序,例如ln -s /bin/busybox /bin/mount See USAGE in the busybox docs : 请参阅busybox文档中的用法

USAGE 用法

BusyBox is a multi-call binary. BusyBox是一个多调用二进制文件。 A multi-call binary is an executable program that performs the same job as more than one utility program. 多调用二进制文件是一种可执行程序,可执行与多个实用程序相同的任务。 That means there is just a single BusyBox binary, but that single binary acts like a large number of utilities. 这意味着只有一个BusyBox二进制文件,但是那个二进制文件的作用就像大量的实用程序。 This allows BusyBox to be smaller since all the built-in utility programs (we call them applets) can share code for many common operations. 由于所有内置实用程序(我们称为小程序)都可以共享许多常规操作的代码,因此BusyBox可以更小。

You can also invoke BusyBox by issuing a command as an argument on the command line. 您也可以通过在命令行上将命令作为参数发出来调用BusyBox。 For example, entering 例如,输入

  /bin/busybox ls 

will also cause BusyBox to behave as 'ls'. 也将导致BusyBox表现为“ ls”。

Of course, adding '/bin/busybox' into every command would be painful. 当然,在每个命令中添加“ / bin / busybox”会很痛苦。 So most people will invoke BusyBox using links to the BusyBox binary. 因此,大多数人会使用到BusyBox二进制文件的链接来调用BusyBox。

For example, entering 例如,输入

  ln -s /bin/busybox ls ./ls 

will cause BusyBox to behave as 'ls' (if the 'ls' command has been compiled into BusyBox). 将导致BusyBox表现为“ ls”(如果已将“ ls”命令编译到BusyBox中)。 Generally speaking, you should never need to make all these links yourself, as the BusyBox build system will do this for you when you run the 'make install' command. 一般而言,您永远不需要自己进行所有这些链接,因为当您运行“ make install”命令时,BusyBox构建系统将为您执行此操作。

If you invoke BusyBox with no arguments, it will provide you with a list of the applets that have been compiled into your BusyBox binary. 如果不带任何参数调用BusyBox,它将为您提供已编译到BusyBox二进制文件中的applet列表。

The commands that happen to work without it are the ones implemented as fork-free and therefore can be invoked as builtins. 没有它而起作用的命令是实现为无前叉的命令,因此可以作为内置函数调用。

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

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