Unlike other systems like Linux, where the kernel and userland components are often built separately, NetBSD offers the unique advantage of generating the entire operating system - kernel and userland - from a single source code tree. This unified approach simplifies the build process and ensures a cohesive and consistent system environment.
In addition to that, NetBSD’s build process is well-documented, straightforward, and canonical. This means that even newcomers can quickly grasp the essentials and dive into customizing and optimizing their NetBSD installations with ease.
In this guide, I’ll take you through the steps of building NetBSD from source using build.sh, leveraging its simplicity and clarity to craft tailored and optimized NetBSD environments. Whether you’re a seasoned NetBSD user or new to the ecosystem, you’ll find that building from source offers unparalleled control and flexibility. Let’s embark on this journey together!
Get the Starter Kit
While this guide provides detailed explanations of each step involved in building NetBSD from source using build.sh, I’ve also prepared a convenient “Starter Kit” to streamline the process further. This package contains a set of scripts that can be executed sequentially, guiding you through the necessary actions in a simple and error-free manner.
These scripts are designed to be versatile - they can be utilized directly to automate the build process or serve as a reference for users who prefer a more hands-on approach. Whether you’re a beginner seeking a guided path or an experienced user looking for a quick and efficient way to build NetBSD, the welcome package provides a valuable resource to suit your needs.
You can the the starter kit from my Forgejo Repository.
For all the following steps, we assume that the welcome package is checked out and that a variable BUILDROOT is set, which points to a directory in which the current user has write permissions.
$ git clone https://forge.petermann-it.de/mpeterma/netbsd-cloud.git
$ cd netbsd-cloud
$ export BUILDROOT=/home/mpeterma/Projects/netbsd
Get The Sources
The source code of NetBSD is still primarily managed in CVS. For some time now, however, there have been efforts to transfer this to a more modern VCS. The initiative is already bearing fruit - in parallel to CVS, there are replications to HG, Git and Fossil. For our specifications, we use replication on Github and clone the repositories first.
$ ./001-clone_repos.sh
It has not been long since NetBSD 10 was released. In the meantime, the first bug fixes have been published on the “netbsd-10” branch. We will therefore check out this branch first:
$ ./002-checkout_branches.sh
Build for amd64 Architecture
As described in the introductory article, we want to use NetBSD in a variety of ways. The variant based on the x86_64 architecture, which runs in NetBSD under the code name “amd64”, still offers the most options. This targets Laptops, Desktops, Servers and Virtual Machines. Therefore, we will first create binaries and installation media for it:
$ ./003-build_netbsd_amd64.sh
This script bundles various individual steps, whereby it is important to know that the first one first creates a crossbuild toolchain. I.e. compilers and co. that can run on the current platform (e.g. Linux on x86_64) and generate binaries for the target platform.
The result can be found in the obj/releasedir directory, whereby the content of the images directory is the most interesting:
$ ls -la $BUILDROOT/obj/releasedir/images/
insgesamt 1494268
drwxr-xr-x 2 mpeterma users 166 Mai 9 12:31 ./
drwxr-xr-x 5 mpeterma users 47 Mai 9 12:29 ../
-rw-r--r-- 1 mpeterma users 167 Mai 9 12:31 MD5
-r--r--r-- 1 mpeterma users 444543876 Mai 9 12:31 NetBSD-10.0_STABLE-amd64-bios-install.img.gz
-r--r--r-- 1 mpeterma users 444643818 Mai 9 12:30 NetBSD-10.0_STABLE-amd64-install.img.gz
-r--r--r-- 1 mpeterma users 640929792 Mai 9 12:29 NetBSD-10.0_STABLE-amd64.iso
-rw-r--r-- 1 mpeterma users 365 Mai 9 12:31 SHA512
The ISO file can be burned to an optical medium as it is and used as an installation disk. The IMG.GZ files can be written to a USB stick after unpacking and also serve as installation media. The variant with “BIOS” in the name is intended for older systems that do not yet have EFI firmware.
Build for evbarm
As a practical example of the cross-build capabilities of NetBSD, we would now like to build NetBSD for the evbarm architecture. In NetBSD, this includes a whole range of platforms that use ARM CPUs and SoCs - including the Raspberry PI, for which we want to use the system.
$ ./004-build_netbsd_evbarm.sh
A closer look at the script reveals that there are fewer steps compared to the amd64 version. This is mainly due to the fact that the live image required for the Raspberry Pi is already part of the release build step. We are interested in this result:
$ ls -la $BUILDROOT/obj/releasedir/evbarm-earmv7hf/binary/gzimg/
insgesamt 346104
drwxr-xr-x 2 mpeterma users 26 Mai 9 13:35 ./
drwxr-xr-x 5 mpeterma users 45 Mai 9 13:32 ../
-rw-r--r-- 1 mpeterma users 354410484 Mai 9 13:33 armv7.img.gz
After unpacking, the IMG.GZ file can be written to an SD card, which is directly bootable on a Raspberry PI (and a whole range of other ARM SoCs).
In this guide, we’ve explored the simplicity and versatility of building NetBSD for various architectures from its source code. What stands out is the remarkable efficiency and completeness of the build process. Unlike other systems, NetBSD’s build process encompasses not only the kernel but also the entire userland, culminating in ready-to-use installation images with an installer, making it exceptionally accessible. This comprehensive approach underscores NetBSD’s claim as the most portable operating system available. While theoretically, Linux and other systems can also be ported, none match the inviting, consistent, and comprehensive nature of this process in NetBSD.