Building an OS for dummies
[Dynamic Link — Static Link]
Posted: 2020-10-10
Last Edited: 2020-10-10
I did a thing!
Today i finished a semi-long-term project. I managed to complete the
CLFS guide and produce a
working distro
of linux for my raspberry pi. It is rather barebones at the
moment and is not configured for even networking out of the box, but the fact
that nothing segfaults immediately after booting makes this a great success!
(My version also contains updated packages, as the ones on the guide were all
outdated)
Compiling a (cross-)compiler
For the longest time i was trying to properly configure the toolchain. Turns out that building a toolchain is rather simple, as long as you stick to the correct schedule:
1) Build barebones binutils for your chosen arch [no libc]
2) Build barebones gcc for your chosen arch [no libc]
3) Compile your libc for your chosen arch
3.1) Compile your libstdcxx for your chosen arch
4) Recompile binutils for your chosen arch
5) Recompile gcc for your chosen arch
Step 3.1 is optional if you don't want to enable c++ support for your xgcc.
Compiling the base
After compiling the toolchain, i was able to start compiling the basic packages that are required for a working OS (that is the system libc, libstdc++, kernel, and bootloader). Since the libc and libstdcxx we compiled in the previous step are for the xbinutils and xgcc, we need to recompile (i placed the sysroot for the xgcc in the root of the clfs system, instead of the cross-toolchain root). This allows us to compile whichever packages we want in the sysroot, and be able to link to them afterwards, instead of having to compile in the toolchain sysroot and copy libraries to the clfs sysroot later.
Compiling more packages
In addition to the base packages in the system, we compile dropbox to provide a basic unix environment, and zlib to provide compression. In addition to these core packages, we also compile dropbear, netplug, and wireless tools to provide a ssh server and a few convenience net utilities. Some configuration is also done via files placed in /etc/.
Conclusion
This was a very fun/frustrating project. At multiple points my builds were failing mysteriously, and i ended up banging my head against a wall. After hopping onto irc and asking for help, these issues were eventually resolved and i managed to continue with the project. The source code for the build script can be found here.