busybox build error in archlinux
Environment
Win11 WSL2:
busybox Compilation
1 2 3 4 5 wget https://www.busybox.net/downloads/busybox-1.37.0.tar.bz2 bzip2 -d busybox-1.37.0.tar.bz2 tar xvf busybox-1.37.0.tar cd busybox-1.37.0
Patch
This version has some issues when compiling directly for aarch64 — a patch is needed:
1 emacs libbb-sha-add-missing-shaNI-guard.patch
Reference:
The patch is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 libbb/hash_md5_sha.c | 2 ++ 1 file changed, 2 insertions(+) @@ -1313,7 +1313,9 @@ unsigned FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf) hash_size = 8; if (ctx->process_block == sha1_process_block64 #if ENABLE_SHA1_HWACCEL +# if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) || ctx->process_block == sha1_process_block64_shaNI +# endif #endif ) { hash_size = 5; -- 2.25.1
Enter the busybox source directory and execute:
1 patch -p1 < libbb-sha-add-missing-shaNI-guard.patch
Compilation
1 2 3 4 5 6 7 8 make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- distclean make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- defconfig make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j$(nproc ) make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- install
The make menuconfig step may fail on Arch Linux or with higher GCC versions:
1 2 3 4 5 6 7 8 9 10 [zhaohang@cyberboy busybox-1.37.0]$ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig *** Unable to find the ncurses libraries or the *** required header files. *** 'make menuconfig' requires the ncurses libraries. *** *** Install ncurses (ncurses-devel) and try again. *** make[2]: *** [.../scripts/kconfig/lxdialog/Makefile:15: ...] Error 1 make[1]: *** [.../scripts/kconfig/Makefile:14: menuconfig] Error 2 make: *** [Makefile:444: menuconfig] Error 2
Even with ncurses installed, it still fails:
Reference solution:
The discussion above notes that higher GCC versions have a breaking change — the main function must have a return type:
1 2 3 emacs scripts/kconfig/lxdialog/check-lxdialog.sh
tc Build Error
If using a higher Linux kernel version, you need to disable tc. Reference:
Disable it in make menuconfig:
1 2 Networking Utilities ---> [ ] TC
Creating inittab, rcS, and fstab Files
1 2 3 4 5 6 7 8 9 10 11 12 13 cd _installmkdir -p dev etc home lib/modules lib64 mnt proc root sys tmp vartouch etc/inittabmkdir -p etc/init.d/touch etc/init.d/rcStouch etc/fstabchmod 755 etc/inittabchmod 755 etc/init.d/rcS
inittab syntax:
1 <id>:<runlevels>:<action>:<process>
id : /dev/id
runlevels : ignored
action : when to execute — options: sysinit, respawn, askfirst, wait, once, restart, ctrlaltdel, and shutdown
process : application or script
etc/inittab content:
1 2 3 4 ::sysinit:/etc/init.d/rcS ::respawn:-/bin/sh ::askfirst:-/bin/sh ::ctrlaltdel:/bin/umount -a -r
etc/init.d/rcS content:
1 2 3 4 5 6 7 8 /bin/mount -a mkdir -p /dev/ptsmount -t devpts devpts /dev/pts mount -t configfs none /sys/kernel/config if [ -e /proc/sys/kernel/hotplug ]; then echo /sbin/mdev > /proc/sys/kernel/hotplug fi mdev -s
etc/fstab content:
1 2 3 4 5 6 proc /proc proc defaults 0 0 tmpfs /tmp tmpfs defaults 0 0 none /tmp ramfs defaults 0 0 sysfs /sys sysfs defaults 0 0 mdev /dev ramfs defaults 0 0
Adding Device Files
1 2 3 cd devsudo mknod console c 5 1sudo mknod null c 1 3
Adding Library Files
This step is optional:
1 2 3 4 cd _installcp /usr/aarch64-linux-gnu/lib/ld-linux-aarch64.so.1 lib/cp /usr/aarch64-linux-gnu/lib/libc.so.6 lib/cp /usr/aarch64-linux-gnu/lib/libm.so.6 lib/
Packaging
1 2 3 cd busybox-1.37.0/_installfind . | cpio -o -H newc | gzip -c > ../initramfs.cpio.gz cp ../initramfs.cpio.gz ~/tftp
Linux Kernel Compilation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 sudo apt-get install gcc-aarch64-linux-gnusudo apt install libssl-devwget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.10.238.tar.xz tar xvf linux-5.10.238.tar.xz cd linux-5.10.238make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- clean make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- defconfig make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- modules_prepare make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- Image -j$(nproc ) cp arch /arm64/boot/Image ~/tftp
Emacs + clangd for Reading Linux Source Code
Create .clangd in the Linux source root:
1 2 3 CompileFlags: Add: -Wno-unknown-warning-option Remove: [-m*, -f*]
Generate compile_commands.json (requires python3):
1 ./scripts/clang-tools/gen_compile_commands.py
Emacs configuration reference:
Emacs + dts-lsp for Reading Device Trees
Uses dts-lsp :
1 $ npm i -g devicetree-language-server
Create .dir-locals.el in the kernel root directory with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 ((dts-mode . ((eglot-workspace-configuration . ((devicetree (cwd . "/home/zhaohang/repository/linux/rk3568_linux_5.10/kernel" ) (defaultIncludePaths . ["include" ]) (defaultBindingType . "DevicetreeOrg" ) (defaultDeviceOrgBindingsMetaSchema . ["/home/zhaohang/repository/dt-schema/dtschema/meta-schemas" ]) (defaultDeviceOrgTreeBindings . [ "/home/zhaohang/repository/linux/rk3568_linux_5.10/kernel/Documentation/devicetree/bindings" ]) (defaultShowFormattingErrorAsDiagnostics . :json-false ) ))))))
Where dt-schema comes from:
eglot configuration in Emacs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 (use-package dts-mode :ensure t :defer t ) (use-package eglot :ensure t :defer 1 :config (setq eglot-server-programs '(((c-mode-hook c++-mode-hook c++-ts-mode c-ts-mode) . ("clangd" "--enable-config" "--background-index" ;; "--background-index-priority=low" "--clang-tidy" "--completion-style=detailed" "--header-insertion=never" "--pretty" )) ((dts-mode) . ("devicetree-language-server" "--stdio" )) ((rust-mode rust-ts-mode) . ("rust-analyzer" :initializationOptions (:cargo (:buildScripts (:enable t )) :procMacro (:enable t )))) ((python-mode python-ts-mode) . ("pyright-langserver" "--stdio" )) )) (setq eglot-ignored-server-capabilities '(:documentOnTypeFormattingProvider )) ) (dolist (hook '(c-mode-hook c-ts-mode-hook c++-mode-hook c++-ts-mode-hook dts-mode-hook rust-mode-hook rust-ts-mode-hook python-mode-hook python-ts-mode-hook )) (add-hook hook 'eglot-ensure))
Running in Qemu
1 2 3 4 5 6 7 8 9 10 qemu-system-aarch64 \ -cpu cortex-a57 \ -m 512M \ -machine type =virt \ -nographic \ -smp 2 \ -kernel ~/tftp/Image \ -initrd ~/tftp/initramfs.cpio.gz \ -append "rdinit=/linuxrc console=ttyAMA0" \ -device virtio-scsi-device
Boot log:
1 2 3 4 5 6 7 8 [ 0.000000] Booting Linux on physical CPU 0x0000000000 [0x411fd070] [ 0.000000] Linux version 5.10.238 ... ... Please press Enter to activate this console. ~ ~ bin etc lib mnt root sys usr dev home linuxrc proc sbin tmp var
Press command + a , then x to exit Qemu.