You end with an image with your own name, banner and password, your own files and a new tool, all repeatable with make.
Every change in this phase is one of three kinds. A setting in menuconfig, a package we turn on, or a file we copy into the image. All three are common in real products.
Step 2.1: Where does a file come from? (about 20 minutes)
Pick a file in the booted system and ask which package put it there. Buildroot keeps a list:
grep "^busybox," output/build/packages-file-list.txt | head
grep "etc/inittab" output/build/packages-file-list.txt
Each line has the package name and a path. This is how you find out why a file is in the image. When the image is too big, the same list tells you which package to drop.
Now look at the size:
du -sh output/target
ls -lh output/images/rootfs.ext*
du -sh output/target/* | sort -h | tail -5
The target folder is around a few tens of megabytes. Most of it is the C library and the kernel modules or BusyBox, depending on the board. Write down the size of rootfs.ext*. We compare against it later.
Step 2.2: A name and a banner (about 30 minutes)
Open the menu again:
make menuconfig
Go to System configuration. Change these:
System hostname to knaxus.
System banner to Welcome to the Knaxus training image.
Save and exit. Now rebuild. Time it:
time make
It takes a minute or less, not an hour. Buildroot noticed that only the system settings changed. It did not rebuild the kernel or the compiler. It only rebuilt the root filesystem and the image.
Boot again with ./output/images/start-qemu.sh serial-only. The banner is now ours, and the prompt reads knaxus login:. The shell prompt and uname -n show the new hostname.
Think it through: which settings would force a full rebuild? A different C library? A different kernel version? A new package? Try to guess each before you read the manual's chapter on rebuilds.
Step 2.3: A password (about 20 minutes)
The stock image lets anyone log in as root with no password. In make menuconfig, under System configuration, set Root password to knaxus. Run make, boot, and log in. The system now asks for it.
Pitfall: the password is stored in plain text in .config, and later in the config file we commit. That is fine for a training image. For a real product you would not put a password in a settings file. You would give each device its own, or use keys, and disable password login entirely.
Step 2.4: Your own files (about 40 minutes)
Settings change what Buildroot builds. A root filesystem overlay adds your own files on top. It is a plain folder that looks like the root of the system. Every file in it is copied into the image at the end of the build.
Tell Buildroot to use the folder. In make menuconfig, under System configuration, set Root filesystem overlay directories to /work/overlay. Save, run make, and boot.
Log in. You should see your line, ll should work, and sysinfo should print a summary. Look at the permissions with ls -l /usr/bin/sysinfo: it must be executable.
Pitfall: if you forget chmod +x, the shell says sysinfo: Permission denied. The overlay keeps the permissions your files have. A file that is not executable in the overlay is not executable in the image.
Step 2.5: Add a package (about 25 minutes)
Packages are the other way to add things. We add strace, a tool that prints every system call a program makes. It is small and it is the first thing you reach for on a device when a program misbehaves.
Run make menuconfig, press /, type strace, and press Enter. The search result shows the menu path and a number. Press that number to jump there. Under Target packages you will find Debugging, profiling and benchmark, and strace inside it. Select it with the space bar. Save and run make.
It builds one small package and reassembles the image, so it takes a few minutes. Boot and try it:
strace -c ls /
You should see a table of system calls with counts: openat, read, write and so on. That is how ls talks to the kernel.
Check the image size again against the number you noted in Step 2.1. Adding one small tool made it bigger by a small amount. Every package has a cost, and a real product keeps that list short.
Step 2.6: Is the network up? (about 15 minutes)
QEMU gives the guest a virtual network card, and a tiny built-in network with an address for the guest and a gateway. Check it from inside:
ip addr
ping -c 1 10.0.2.2
The guest usually gets 10.0.2.15, and 10.0.2.2 is the gateway, which is QEMU itself. A reply to the ping means the network card, the driver, the DHCP client and the virtual network all work. This is QEMU's "user" network. It lets the guest reach out but does not let anything reach in unless you forward a port.
Phase 2 done
You changed settings, added files with an overlay, and added a package, and each change cost a short rebuild. But your work lives in places that do not survive: the overlay is in /work, the settings are in a generated .config. Phase 4 fixes that. First we look inside.