Cross building NetBSD on Linux

Frederic Cambus August 29, 2014 [NetBSD]

Known for its portability, it might come at no surprise that NetBSD (and pkgsrc as well) is also easy to cross build on other platforms and even other hosts. Building NetBSD on Linux can be very convenient, as it's far easier to get access to high-performance Linux powered machines than to NetBSD ones. So let's go through the process and build a NetBSD/amd64 release on a Debian host, using an unprivileged user account.

First, we will need to install some development packages on the host machine:

apt-get install build-essential zlib1g-dev flex

Then, we can download and extract source sets for the NetBSD version we want to build. At the time of writing, latest NetBSD release is 6.1.4:

wget ftp://ftp.netbsd.org/pub/NetBSD/NetBSD-6.1.4/source/sets/gnusrc.tgz
wget ftp://ftp.netbsd.org/pub/NetBSD/NetBSD-6.1.4/source/sets/sharesrc.tgz
wget ftp://ftp.netbsd.org/pub/NetBSD/NetBSD-6.1.4/source/sets/src.tgz
wget ftp://ftp.netbsd.org/pub/NetBSD/NetBSD-6.1.4/source/sets/syssrc.tgz

for file in *.tgz
do
	tar xfz $file
done

We can now build the cross compiler:

cd usr/src
./build.sh -U -u -m amd64 tools

Compilation results:

===> Summary of results:
	 build.sh command:    ./build.sh -U -u -m amd64 tools
	 build.sh started:    Thu Aug 28 21:49:19 UTC 2014
	 NetBSD version:      6.1.4
	 MACHINE:             amd64
	 MACHINE_ARCH:        x86_64
	 Build platform:      Linux 3.2.0-4-amd64 x86_64
	 HOST_SH:             /bin/sh
	 No $TOOLDIR/bin/nbmake, needs building.
	 Bootstrapping nbmake
	 TOOLDIR path:        /home/admin/usr/src/obj/tooldir.Linux-3.2.0-4-amd64-x86_64
	 DESTDIR path:        /home/admin/usr/src/obj/destdir.amd64
	 RELEASEDIR path:     /home/admin/usr/src/obj/releasedir
	 Created /home/admin/usr/src/obj/tooldir.Linux-3.2.0-4-amd64-x86_64/bin/nbmake
	 Updated makewrapper: /home/admin/usr/src/obj/tooldir.Linux-3.2.0-4-amd64-x86_64/bin/nbmake-amd64
	 Tools built to /home/admin/usr/src/obj/tooldir.Linux-3.2.0-4-amd64-x86_64
	 build.sh ended:      Thu Aug 28 21:56:48 UTC 2014

Here is more information about configuration options used in those examples:

    -U             Set MKUNPRIVED=yes; build without requiring root privileges,
                   install from an UNPRIVED build with proper file permissions.
    -u             Set MKUPDATE=yes; do not run "make cleandir" first.
                   Without this, everything is rebuilt, including the tools.
    -m mach        Set MACHINE to mach; not required if NetBSD native.

Once our toolchain is ready, we can now build NetBSD itself:

./build.sh -U -u -m amd64 release

Compilation results:

===> Summary of results:
         build.sh command:    ./build.sh -U -u -m amd64 release
         build.sh started:    Thu Aug 28 21:57:54 UTC 2014
         NetBSD version:      6.1.4
         MACHINE:             amd64
         MACHINE_ARCH:        x86_64
         Build platform:      Linux 3.2.0-4-amd64 x86_64
         HOST_SH:             /bin/sh
         TOOLDIR path:        /home/admin/usr/src/obj/tooldir.Linux-3.2.0-4-amd64-x86_64
         DESTDIR path:        /home/admin/usr/src/obj/destdir.amd64
         RELEASEDIR path:     /home/admin/usr/src/obj/releasedir
         Updated makewrapper: /home/admin/usr/src/obj/tooldir.Linux-3.2.0-4-amd64-x86_64/bin/nbmake-amd64
         Successful make release
         build.sh ended:      Thu Aug 28 23:21:28 UTC 2014

From there, building an ISO image takes only a few seconds:

./build.sh -U -u -m amd64 iso-image

We can now try to boot the resulting ISO image:

NetBSD Primary Bootstrap

NetBSD Installer

Unsurprisingly, it works as expected! Welcome to NetBSD :-)

Back to top