Search code examples
linuxbusyboxinitramfs

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. I'm experimenting with creating a custom initramfs + init script to use with custom compiled kernels that are being used with qemu instances.

From the directory I'm using as the base for the 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:

[~/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:

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:

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

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

/ # 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 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, e.g. find is fine:

/ # 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!


Solution

  • You have to symlink all the applets you want, e.g. ln -s /bin/busybox /bin/mount. See USAGE in the busybox docs:

    USAGE

    BusyBox is a multi-call binary. 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. This allows BusyBox to be smaller since all the built-in utility programs (we call them applets) can share code for many common operations.

    You can also invoke BusyBox by issuing a command as an argument on the command line. For example, entering

        /bin/busybox ls 
    

    will also cause BusyBox to behave as 'ls'.

    Of course, adding '/bin/busybox' into every command would be painful. So most people will invoke BusyBox using links to the BusyBox binary.

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

    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.

    The commands that happen to work without it are the ones implemented as fork-free and therefore can be invoked as builtins.