0

I have 3 MB of SPI flash on my board and I am able to pack the bzImage, busybox initrd and coreboot ROM into this SPI flash. I am also able to boot to the shell , all using the ROM kernel-as-bootloader.

I have bigger kernel on the USB device. I am also able to detect the USB and mount it. But the problem is busybox does not seem to have kexec utility. I cannot use any other initrd package because my cpio (uncompressed) size should not go beyond 1.4 MB due to memory constraints. U-root has kexec support but the minimal image size easily reaches 3MB or at least, I couldn't find a way to built it lesser than that size.

Hence, is there a way to add kexec support to busybox (compile static binary and copy to initrd? ) or any other initrd package that can suffice the need within 1.4MB size?

EDIT

This post suggests that there may be kexec support available in busybox, but I couldn't find any trace of it. In fact the request to add kexec-tools to busybox was done over a decade ago. But when I did grep in the busybox, I saw no traces of it.

Naveen
  • 7,944
  • 12
  • 78
  • 165

2 Answers2

2

The kexec binary from kexec-tools takes about 300KB (x86_64 with -Os). Even if it would be added to busybox, it wouldn't get much smaller than that because it does need to do fairly complicated things that are not done anywhere else in busybox.

If you don't have even 300KB left, then you should probably remove configuration options from busybox itself to save space. With uClibc, you can also remove some options you don't need, like wchar and threading. Make sure you use static linking, it saves a little bit of space.

The following Buildroot defconfig generates an initramfs of exactly 1.4MB.

BR2_x86_64=y
BR2_x86_corei7=y
BR2_STATIC_LIBS=y
BR2_TOOLCHAIN_EXTERNAL=y
BR2_TOOLCHAIN_EXTERNAL_DOWNLOAD=y
BR2_TOOLCHAIN_EXTERNAL_URL="https://toolchains.bootlin.com/downloads/releases/toolchains/x86-64-core-i7/tarballs/x86-64-core-i7--uclibc--stable-2018.11-1.tar.bz2"
BR2_TOOLCHAIN_EXTERNAL_GCC_7=y
BR2_TOOLCHAIN_EXTERNAL_HEADERS_4_1=y
BR2_TOOLCHAIN_EXTERNAL_LOCALE=y
BR2_PACKAGE_KEXEC=y
BR2_PACKAGE_KEXEC_ZLIB=y
BR2_TARGET_ROOTFS_CPIO=y
# BR2_TARGET_ROOTFS_TAR is not set
Arnout
  • 2,927
  • 16
  • 24
  • I wanted it for x86_64 platform. When I tried building it, the size of kexec binary alone is 1.4MB with -O2 flag. Can you please share the exact flags that you use? I am positive that there is a way to strip it down since the sole objective is to kexec the bigger kernel from the disk, and nothing else. (I do have roughly 150 KB space left) – Naveen Oct 30 '19 at 03:30
  • I put the buildroot defconfig in my answer. That gives you the exact flags. – Arnout Oct 30 '19 at 10:05
  • But maybe you're not using Buildroot (https://buildroot.org)? I thought you did because you used the buildroot tag. – Arnout Oct 30 '19 at 10:05
  • I changed the Buildroot config and the rest of the answer to use x86_64 instead of ARM. It's still 1.4MB, although the kexec binary itself is 3 times larger on x86_64 (x86 is an insane architecture). I haven't tried, but you might be able to get it slightly smaller by building i386 instead of x86_64 - note that you need to use an i386 toolchain too for that. – Arnout Oct 30 '19 at 10:24
  • Yes, I was not using Buildroot but open to any solution. I went to https://github.com/horms/kexec-tools and did a build which gave me 1.4 MB of kexec image alone, which is too big. Your buildroot solution seems promising but I was not sure about using the above config in buildroot context as i am the first time buildroot user. I created a .config file in the root of buildroot and did menuconfig on top of it and saved and then did a make. I have got a 1.2 MB rootfs.cpio which is acceptable in size. I will test it and update the results. Thanks. – Naveen Oct 31 '19 at 08:32
1

We could build a version of kexec for busybox which results in a 220K static linked binary like so:

  1. Use musl
  2. Strip the binary with strip (Maybe it could reduce the size even more?)
  3. Disable unused features via ./configure --without-<FEATURE>
KEXEC_VERSION=2.0.24
KEXEC=kexec-tools-$KEXEC_VERSION

curl -LO https://mirrors.edge.kernel.org/pub/linux/utils/kernel/kexec/$KEXEC.tar.xz
tar xf $KEXEC.tar.xz
cd $KEXEC

CC=musl-gcc LDFLAGS=-static ./configure \
  --without-lzma \
  --without-xen \
  --sbindir=/bin
make
strip build/sbin/kexec

file buils/sbin/kexec  # -> [...] statically linked, stripped
du -h build/sbin/kexec # -> 220K [...]

Still not great, but yeah.. Let us know if you could shrink it even more :-)

netzego
  • 372
  • 4
  • 15