Module 4 · Lesson 41 of 156

The tiny Linux plan and a minimal kernel

Plan a tiny QEMU Linux system and configure a trimmed arm64 kernel that still boots on the virt machine.

  • Beginner to advanced
  • 9 min read
  • 43 min lab
What you will need

Ubuntu 24.04 in WSL2 or a VM; qemu-system-arm; gcc-aarch64-linux-gnu; a stable kernel source tarball from kernel.org

Your place in the module
By the end you can 4 objectives
  • Name the two artifacts of the tiny system and the QEMU options that load them
  • Build an arm64 kernel Image from defconfig with a cross toolchain
  • List the config symbols a virt boot with an initramfs depends on
  • Trim unused subsystems safely and verify the kernel still boots
Video coming soonThe tiny Linux plan and a minimal kernel
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from Block devices and the limits of emulation
  • -drive describes the backend file and its format; the frontend device decides what the guest sees, and virtio-blk appears as /dev/vda
  • Always set format= explicitly, because format probing can be abused by a guest that writes an image header into a raw file
  • virtio devices use shared-memory virtqueues and virtio-aware drivers: fast and simple, but with no timing model of real silicon
01

The plan: one kernel, one initramfs, one command

3 min read

You can now boot and drive QEMU, so the next step is to assemble your own tiny Linux from scratch. This lesson answers what the full plan is and how a defconfig trimmed down to the virt machine still boots.

The system you will build has exactly two artifacts. The first is a Linux kernel Image, cross-compiled on your Ubuntu machine for AArch64. The second is an . That archive will hold BusyBox, one binary that provides the shell and the standard utilities, plus a small init script. QEMU's virt machine loads both artifacts directly, -kernel for the Image and -initrd for the archive. Linux installers and rescue systems start in exactly this shape, which is a good sign that it is a sane minimum rather than a trick.

Notice how the four building blocks from the first lesson of this module survive, only smaller. The toolchain stays on the host and never ships. QEMU's built-in loader replaces U-Boot. The kernel is configured for one machine instead of every machine. The root filesystem is one binary living in RAM. Nothing essential is missing; everything unnecessary is gone. That is also why the previous lesson's block-device limits do not block us: with the root in RAM, the kernel needs no storage driver at all to reach a shell.

The session walks the plan in order. This lesson produces the kernel. "Building BusyBox and the init script" produces the second artifact. "Booting to a shell and adding a hello program" gets you to a prompt and runs your own code. "Measuring sizes and the build script" turns the whole thing into one repeatable script with real size numbers, and "Debugging common boot failures" covers what to do when the console goes quiet.

The command the whole plan aims at looks like this:

bash
qemu-system-aarch64 -M virt -cpu cortex-a57 -m 512M -nographic \
  -kernel arch/arm64/boot/Image \
  -initrd rootfs.cpio.gz \
  -append "console=ttyAMA0"

-M virt selects the machine, -cpu cortex-a57 a CPU it supports, -m 512M the RAM size, and -nographic wires the virt machine's PL011 serial port to your terminal. console=ttyAMA0 tells the kernel to put its messages and the shell on that port, exactly as in the serial console lesson. Until the next lesson supplies rootfs.cpio.gz, the kernel will boot, print its messages, and then panic because it has no root filesystem to run an init program from; the real message in the test with an initramfs that had no /init was Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0). That panic is the expected result of a correct kernel with no root filesystem, not a failure of your build.

The tiny Linux build and boot pipelinemakedefconfigarch/arm64defaultconfigtrim.configdrop unusedsubsystemsmake Imagecross-compiled kernelpackinitramfsBusyBoxcpio.gzQEMU virt-kernel and-initrdshellconsole onttyAMA0makedefconfigarch/arm64defaultconfigtrim.configdrop unusedsubsystemsmake Imagecross-compiled kernelpackinitramfsBusyBoxcpio.gzQEMU virt-kernel and-initrdshellconsole onttyAMA0
The build and boot pipeline: two artifacts, one QEMU command
02

A defconfig kernel that boots on virt

3 min read

A kernel build starts from a configuration, a file named .config that decides which of the thousands of kernel options are compiled in. You do not write it by hand. You start from a . For arm64 there is one shared defconfig, and it is deliberately generous: it enables many drivers and subsystems so that one config boots a wide range of machines, including QEMU virt. It is a starting point that boots, not a minimal config. Trimming it is the next section.

Step by stepFrom tarball to a bootable Image
  1. Install the cross toolchain and build dependencies on Ubuntu 24.04: the gcc-aarch64-linux-gnu package plus the tools the kernel build needs.

  2. Fetch the source: download a stable tarball from kernel.org, unpack it, and work inside that directory.

  3. Generate the config: make defconfig with ARCH=arm64 reads arch/arm64/configs/defconfig and writes .config.

  4. Build the Image: the Image target produces the uncompressed arm64 boot image at arch/arm64/boot/Image.

  5. Boot it: pass that file to QEMU with -kernel and watch the serial console.

bash
sudo apt install gcc-aarch64-linux-gnu make flex bison libssl-dev bc
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- defconfig
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j"$(nproc)" Image

ARCH=arm64 selects the architecture, and CROSS_COMPILE=aarch64-linux-gnu- is the prefix of the cross tools, so the build calls aarch64-linux-gnu-gcc instead of the host compiler. In the real run (Linux 6.12.120, gcc 13.3 cross compiler, -j2 on a 2-core container with other jobs running) the build took close to an hour, and bc was needed (the first attempt stopped with bc: not found). The result, arch/arm64/boot/Image, was 46133760 bytes and is the raw image QEMU loads; the build also leaves a vmlinux ELF file in the source root (158362896 bytes in that build because the defconfig has CONFIG_DEBUG_INFO=y), which is useful for debugging but is not what -kernel takes here.

Some options are load-bearing for this exact boot, and you must keep them through every later trim:

  • CONFIG_SERIAL_AMBA_PL011 and CONFIG_SERIAL_AMBA_PL011_CONSOLE: the virt machine's UART and its use as the console. Without them the kernel may boot fine but you see nothing.
  • CONFIG_BLK_DEV_INITRD and CONFIG_RD_GZIP: support for loading an external initramfs and decompressing gzip. Without them -initrd is ignored.
  • CONFIG_DEVTMPFS: the kernel-populated /dev your init script will mount.
  • CONFIG_BINFMT_ELF and CONFIG_BINFMT_SCRIPT: loading ELF binaries and script files. BusyBox is ELF; a shell-script /init needs the script loader.
  • CONFIG_PRINTK: the kernel messages you will rely on to debug every failure.

The arm64 defconfig already enables these, so the first build boots without editing anything. If you later add virtio disks or networking, enable CONFIG_VIRTIO_MMIO and the matching device drivers; the plain serial-plus-initramfs boot does not need them.

03

Trimming: CONFIG_EXPERT and dropping subsystems

5 min read

A defconfig kernel carries support for hardware you do not have: sound cards, GPUs, USB controllers, wireless chips, dozens of disk filesystems. On a real product that costs flash space, RAM and boot time, because every built-in driver gets a chance to initialize. On QEMU it costs you build time and a larger Image. Trimming means removing what the virt machine and a RAM root never use, and the question "how small can the kernel get?" has a concrete answer: the floor is the architecture code, the scheduler and memory management, the few drivers virt actually needs, and the interfaces your user space depends on. Everything above that floor is negotiable. The exact numbers are yours to measure in "Measuring sizes and the build script"; do not trust anyone else's.

The first lever is CONFIG_EXPERT ("Configure standard kernel features (expert users)" under General setup). Older guides name a different lever, CONFIG_EMBEDDED ("Embedded system"), which only selected CONFIG_EXPERT. That symbol no longer exists: init/Kconfig of Linux v6.1 to v6.5 contains config EMBEDDED with select EXPERT, and v6.6, v6.8, v6.10 and v6.12 contain no EMBEDDED at all (checked on raw.githubusercontent.com, and in the 6.12.120 tree used for this lesson). In that tree the defconfig has # CONFIG_EXPERT is not set, and EXPERT unhides a set of base options that are normally forced on, for example bool "Enable the block layer" if EXPERT for CONFIG_BLOCK and bool "Enable support for printk" if EXPERT for CONFIG_PRINTK. Enabling it does not shrink anything by itself: it gives you permission to disable things the kernel assumes every system wants. With that permission, the safe removals for this system are whole subsystems the virt boot never touches: CONFIG_SOUND, CONFIG_DRM (graphics), CONFIG_USB_SUPPORT, CONFIG_WIRELESS, and disk filesystems such as ext4 and btrfs, since the root lives in RAM. Keep CONFIG_PROC_FS, CONFIG_SYSFS and CONFIG_TMPFS: user space expects /proc and /sys, and tmpfs backs a writable RAM root. You can also set CONFIG_MODULES=n so everything is built in and the module loader disappears, and choose CONFIG_CC_OPTIMIZE_FOR_SIZE to compile with -Os.

Going deeperExplore the mechanism

The expert-gated options go deeper than drivers. CONFIG_BLOCK, the whole block layer, is only visible when CONFIG_EXPERT is set, and a system whose root is an initramfs can disable it entirely: no disk, no block layer. CONFIG_PRINTK and CONFIG_BUG are also expert-gated; dropping them saves space but leaves you with a silent kernel, so keep them at least until you have been through the boot-failure lesson. Be careful with CONFIG_FUTEX and CONFIG_EPOLL: they look optional, but glibc-based programs use futexes for locking and many programs use epoll, so disabling them breaks normal user space even though the kernel itself boots. And note a measurement trap: CONFIG_DEBUG_INFO inflates vmlinux and the build directory, not arch/arm64/boot/Image, because debug sections are not loaded into memory. Measure the artifact QEMU actually boots.

Trim mechanically, one group at a time, and boot after every change. The kernel tree ships scripts/config for scripted edits (symbol names without the CONFIG_ prefix), and olddefconfig resolves an edited .config into a valid one:

bash
cp .config config-before-trim.bak
./scripts/config --file .config -d SOUND -d DRM -d USB_SUPPORT
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- olddefconfig
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j"$(nproc)" Image

In the real test these three edits changed the number of =y lines of .config from 3178 to 2976 and the =m lines from 1164 to 778, and # CONFIG_DRM is not set, # CONFIG_SOUND is not set and # CONFIG_USB_SUPPORT is not set appeared; the kernel was not rebuilt after the trim in this review, so no size number is given. Then run the same QEMU command and confirm the kernel messages still appear and the boot ends the same way it did before (the expected init panic, or a shell once you have an initramfs). If you prefer a menu, make ARCH=arm64 menuconfig works too and needs libncurses-dev. Keep the backup configs: when a trim breaks the boot, the diff between two .config files is your suspect list.

Never trim blind

A kernel that fails to boot silently is much harder to fix than one that prints where it stopped. Keep the PL011 console, CONFIG_PRINTK, the initramfs options and the ELF/script loaders enabled until the system reliably reaches a shell. Size savings are worthless if you cannot see why the board stopped.

You now have the plan and a kernel that boots on virt, from a generous defconfig down toward a config that contains only what this system uses. The kernel alone still ends in a panic, because there is no init to run. The next lesson, "Building BusyBox and the init script", builds the second artifact: a static BusyBox and an /init that turns the panic into a shell.

Keep versus drop for a virt initramfs bootKeep for virtSafe to dropConsolePL011, printkother UARTdriversRootinitramfs,gzipext4, btrfs,NFSDevicesdevtmpfs,virtioUSB, sound,DRMExecELF, scriptsbinfmt_misc
What a virt-plus-initramfs kernel must keep, and what it can drop

Key takeaways

  • The tiny system is two artifacts: a cross-compiled arm64 Image and a BusyBox initramfs, loaded by QEMU with -kernel and -initrd.
  • arch/arm64/configs/defconfig boots the virt machine unmodified; it is a starting point, not a minimal config.
  • CONFIG_EXPERT unhides base options such as CONFIG_BLOCK and CONFIG_PRINTK so they can be disabled (the older CONFIG_EMBEDDED, which selected it, was removed in v6.6).
  • The console, initramfs and program-loading symbols must stay enabled, or the trimmed kernel fails to boot usefully.
Reflect
  1. Explain to a colleague, in two sentences: name the two artifacts of the tiny system and the QEMU options that load them
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Find your next step

↑ ↓ to move · Enter to open · Esc to close