Building Embedded Linux With Buildroot
Building your own embedded Linux environment doesn't have to be a hassle. Buildroot and a few other tools can get you where you need to be.
Join the DZone community and get the full member experience.Join For Free
If you followed my previous article on why Buildroot is a good thing and why you should use it, you'll have installed Buildroot in Vagrant VM running with either VMWare or Virtualbox. You can also install it locally on any Linux system by downloading and decompressing the latest version from the website. Follow the README — you need to install ncurses for the nconfig and menuconfig options to work and gcc-multilib to get the system working (e.g. sudo apt install libncurses5 libncurses5-dev gcc-multilib should do the trick).
Now, let's build an image. This first one will run on a 32-bit Intel processor and use the EXT2 filesystem. When we're finished, we'll be able to run it via QEMU.
First, a note on notation. I won't show you each menu, but I will give you the paths through it. For example, for this path...
...I'll describe as Target Options -> Target Architecture. So let's get started.
First, make sure you start from a default .config file. The .config file, located in the buildroot home directory, contains the configuration for the image you're building. Be careful with these! In a production project, you'll want to keep these under some form of version control. Once you have one configured and working, you don't want to lose it.
Next, start up the configuration system with make nconfig. The default build won't need much in the way of changes or additions. First, go to Kernel -> Linux Kernel, and select it (press either space or enter to select). It's going to ask for a defconfig name; go to that option, hit return, and enter i386.
This screen should look like this:
Now, go to Target Packages -> Networking Applications, and select openssh. Next, go to Filesystem Images and select ext 2/3/4 root filesystem. Finally, go to Bootloaders and select syslinux.
Now exit, and save the configuration.
Next, at the command line, type make. Go do something else for a bit, this takes a while.
You should have a couple of files in output/images - bzImage and rootfs.ext2. We're going to use these to start up QEMU.
QEMU is pretty powerful. I suggest you use a script like this to boot it up:
LOCATION=$PWD ROOT="output/images/" KERNEL=$ROOT"bzImage" DISK=$ROOT"rootfs.ext2" QEMU=qemu-system-i386 NAME=i386Linux $QEMU \ -kernel $LOCATION/$KERNEL \ -drive format=raw,file=$LOCATION/$DISK,index=0,media=disk \ -boot c \ -m 128 \ -append "root=/dev/sda rw" \ -localtime \ -no-reboot \ -name $NAME \ -net nic -net user \ -redir tcp:2222::22
Go ahead and create this script file. I call it qemu.sh, and I keep it in the Buildroot home directory. Start it up. You should get a login, so go ahead and log in as root and hit return. We didn't configure a password, so you'll go right in.
That's it! You've created your first embedded Linux environment. If you'd like, you can add a user (via the adduser command) and configure the new image to use networking (add the lines auto eth0 and iface eth0 inet dhcp to the bottom of the /etc/network/interfaces file, in that order). If you do, restart the image via /sbin/poweroff or /sbin/reboot. After it comes back up, you should be able to ssh to the image via the command ssh -p 2222 username@localhost. The last line in the script file forwards traffic to port 2222 on the host to port 22 on the image. Root won't allow remote connections, so you'll need a different account to test SSH.
That's it! Buildroot is very powerful, but with a little work, you can take much of the drudgery out of creating embedded system images.
Opinions expressed by DZone contributors are their own.