9 ms·
How to create an OS from scratch – tutorial
- giancarlostoro 8y agoI've been wanting to do this but with a Pi or any SBC board with Linux from Scratch, but not sure how I would even approach LFS on a Pi.
- craftyguy 8y agoI've not done LFS, but have worked a bit with Linux on arm devices, and I would avoid choosing an arm device for this purpose because the state of upstream kernel support for many devices that arm manufacturers use is, well, shit. You'll almost certainly have to use some old, unmaintained manufacturer kernel to get all of the devices working (e.g. graphics). Edit: It actually doesn't seem that bad for some(?) rpi devices: https://elinux.org/RPi_Upstreaming https://elinux.org/RPi_Upstreaming Not sure if that list includes all devices found on even the newer ones too.
- giancarlostoro 8y agoI'm fine with older kernels / older devices. My goal is to see how it all comes together and learn more about the internals. I like SBC's because they're usually cheap and can be replaced quickly and easily enough.
- craftyguy 8y agoOlder devices is fine, but you can start to run into trouble with older kernels. For example, newer glibc versions are notorious for requiring newer kernels [0]. So then you're stuck with an old glibc for your kernel. Not all applications (especially newer ones) will successfully link against that, so then you're looking for replacements or older versions that do (if there are any). What about other libc implementations, like musl? There are differences between musl and glibc[1] that will drive you nuts trying to find things that will link in musl that were originally written to link against glibc. 0. https://mirrors.edge.kernel.org/pub/software/libs/glibc/hjl/compat/ https://mirrors.edge.kernel.org/pub/software/libs/glibc/hjl/... 1. https://wiki.musl-libc.org/functional-differences-from-glibc.html https://wiki.musl-libc.org/functional-differences-from-glibc...
- Jaruzel 8y agoMy problems with LFS are: 1. The documentation is always in flux. You may get lucky and it's accurate, or you may not, and spend hours looking for tarballs that no longer exist online, or worse find them and they are no longer compatible with LFS - They don't host the required source archives along with the LFS book, and a lot of links to parts you need end up going nowhere. 2. As a step-by-step it does do what it says on the tin, but as a learning tool, there's a lot missing. When I was less knowledgeable about Linux I found myself following the LFS book with little to no idea about what I was actually doing and why. It would say things like 'chroot in the build folder' without actually explaining why, and what chroot actually did. 3. The whole thing can fail at any point, leaving you with a broken build, that more often than not, you'd have to wipe and start over. As most of the steps involve compiling, that's a lot of lost hours.
- mikestaszel 8y agoI'm in extremely early stages of writing up some documentation for how to build a Linux distro for a Raspberry Pi 3, gauging interest and figuring out pain points. Shoot me an email [my username]@gmail if you've got ideas or requests!
- codetrotter 8y agoThat sounds interesting. I think your guide would be extra helpful if it focused on building a distro that has as much of the system loaded into memory as possible to minimize amount of reads from the SD card, and which is configured with a read-only file system and never even tries to write to the SD card. That would be very useful. SD card corruption is a pain!
- hak8or 8y agoMaybe we can somehow refer to each other? I wrote a guide for a custom board I made running Linux, but it's very low level up to tge stage of getting the kernel running with a minimal user land. I would be happy to refer to you at the end of mine: brainyv2.hak8or.com
- uep 8y agoWhat do you mean by building a distro? Have you looked at Yocto/poky/angstrom? It's kind of a distro-generator.
- zokier 8y agoI'm not sure why you'd expect the approach for RPi needing to be significantly different from that of any other computer. Bootloader would be the biggest difference I foresee, but that is fairly small part of the whole LFS.
- Alupis 8y agoMight be worth doing the CLFS (Cross Linux From Scratch)[1] path since it'll be a LOT faster to compile things like the kernel and gcc on your deskop computer. I did this and wrote a bunch of scripts to automate the process. CLFS uses busybox and DropBear to keep things really small (~42MB binary for the entire OS, including the Raspberry Pi blobs) - but you'll learn enough to be able to swap out components of your choosing. [1] http://clfs.org/view/clfs-embedded/arm/ http://clfs.org/view/clfs-embedded/arm/
- jeffreyrogers 8y agoWriting an OS is a fun exercise and it makes you appreciate all the effort that goes into making something like Linux usable and relatively robust and performant. It's also interesting to look at how Linux (or other open source OS) implements some of these features once you've tried implementing them on your own. You'll find that much of Linux is understandable once you know what problem the code in question is trying to solve.
- justinian 8y agoI've seen a number of "build an OS" tutorials pop up recently, and that's awesome! Building an OS will definitely help you understand how to write better user-space programs, and it's fun. One thing I wish is that more of them would feature UEFI instead of BIOS, or at least come with a warning, as Intel plans to drop support for legacy BIOS in 2020. Going through UEFI is actually easier anyway, IMHO. ... Maybe I should stop complaining and write that tutorial myself instead. Hmm.
- panic 8y agoOr use the multiboot format (https://www.gnu.org/software/grub/manual/multiboot/multiboot.html https://www.gnu.org/software/grub/manual/multiboot/multiboot...), which lets you offload the dirty work to GRUB. Writing bootloaders is kind of pointless unless you want to learn about them specifically.
- tazard 8y agoAs opposed to writing an OS? I'm pretty sure the target audience for these tutorials are people interested in learning how these things work, not people who know and are writing The Next Big Thing
- msla 8y agoYou're allowed to limit how much crap from the 1980s you allow into your life. If you want to learn about the Good Stuff (schedulers, filesystems, TCP algorithms) as soon as possible, jumping a bit and leaving the completely arbitrary bootloader stuff to some pre-written system is completely reasonable.
- nostrademons 8y agoIs there any downside to using the multiboot format for your OS anyway, bootstrapping off GRUB to get started, and then if you really want to learn how everything works, writing a replacement for GRUB? It looks like multiboot is just a protocol that lets you plug in to a bunch of industry-standard software; it can still be quite educational (and easier) to go rewrite both ends of that protocol independently.
- fokker 8y agoThese kinds of posts are the reason I come to HN hoping to find. Amazing work! I'm going to come back and read some chapters over the weekend.
- jhinra 8y ago“Once you pass college, excessive theory is worse than no theory because it makes things seem more difficult than they really are.” About every third minute I’ve changed my mind about this quote. I think that means it’s a good quote.
- SmellyGeekBoy 8y agoAs someone from a computer science background who's recently been getting into electronics in a really big way, this quote really struck a chord with me.
- 3rdAccount 8y agoI'll be the guy to mention Forth as it is the only system I know of that a single person can fully understand and build up a fully functional and REPL interactive system using just assembly and a microcontroller. Most Forthers are doing embedded work though and don't need everything that comes with an OS.
- deytempo 8y agoI legit thought they wrote an OS in the scratch programming language for a second lol
- deleted 8y ago[deleted]
- bigmit37 8y agoDoes any one have other resources I can look into as well. I’m still a relatively new programmer but I really want to get a deeper understanding of how OSes work. I’m assuming I need to use either C/C++ or Rust? I only Python and am currently learning C++.
- charlysl 8y agoStanford CS140e - An Experimental Course on Operating Systems [1]: students implement a simple, clean operating system (virtual memory, processes, file system) on a Raspberry Pi 3 in the Rust programming language and use the result to run a variety of devices MIT 6.828 - Operating System Engineering [2]: This course studies fundamental design and implementation ideas in the engineering of operating systems. Lectures are based on a study of UNIX and research papers. Topics include virtual memory, threads, context switches, kernels, interrupts, system calls, interprocess communication, coordination, and the interaction between software and hardware. Individual laboratory assignments involve implementation of a small operating system in C, with some x86 assembly [1] https://web.stanford.edu/class/cs140e/about/ https://web.stanford.edu/class/cs140e/about/ [2] https://pdos.csail.mit.edu/6.828/2018/schedule.html https://pdos.csail.mit.edu/6.828/2018/schedule.html
- movedx 8y agoThanks for these links. Very useful indeed. I've been looking for something to get me into C and Rust, so I can learn the lower level stuff, and get me away from higher level web technologies (which I play with all day, everyday.)
- charlysl 8y agoYou are welcome. Please note that the MIT 6.828 is really tough but will turn you into a bad-ass C hacker. It relies on MIT 6.004 - Computation Structures [1] (has edx mooc [2,3,4]) and MIT 6.033 - Computer Systems Engineering [5] as pre-requisites given that they teach you the fundamentals of computer architecture (digital abstraction, gates, circuits, processors, assembler, memory, cashes, pipelining, virtual memory, interrupts/io, buses, what have you) and system design (basically, managing complexity); they are not programming courses, but I recommend them if you have the time and interest, they are an awesome and principled way of learning the core concepts that you really need to have down before dabbling with OSs. [1] https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-004-computation-structures-spring-2009/ https://ocw.mit.edu/courses/electrical-engineering-and-compu... [2] https://courses.edx.org/courses/course-v1:MITx+6.004.1x_3+3T2016/course/ https://courses.edx.org/courses/course-v1:MITx+6.004.1x_3+3T... [3] https://courses.edx.org/courses/course-v1:MITx+6.004.2x+3T2015/course/ https://courses.edx.org/courses/course-v1:MITx+6.004.2x+3T20... [4] https://courses.edx.org/courses/course-v1:MITx+6.004.3x+2T2016/course/ https://courses.edx.org/courses/course-v1:MITx+6.004.3x+2T20... [5] https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-033-computer-system-engineering-spring-2009/ https://ocw.mit.edu/courses/electrical-engineering-and-compu...
- simula67 8y agoCan anyone recommend good resources on writing a compiler from scratch ?
- earenndil 8y agoCrafting interpreters, also the dragon book.
- arundelo 8y agohttps://compilers.iecc.com/crenshaw/ https://compilers.iecc.com/crenshaw/
- faitswulff 8y agoDestroy All Software has (as usual) an excellent screencast on it: https://www.destroyallsoftware.com/screencasts/catalog/a-compiler-from-scratch/ https://www.destroyallsoftware.com/screencasts/catalog/a-com...
- charlysl 8y agoStanford CS143 - Compilers [1] and [2] MOOC: This self-paced course will discuss the major ideas used today in the implementation of programming language compilers, including lexical analysis, parsing, syntax-directed translation, abstract syntax trees, types and type checking, intermediate languages, dataflow analysis, program optimization, code generation, and runtime systems. As a result, you will learn how a program written in a high-level language designed for humans is systematically translated into a program written in low-level assembly more suited to machines. Along the way we will also touch on how programming languages are designed, programming language semantics, and why there are so many different kinds of programming languages. MIT 6.035 - Computer Language Engineering [3]: This course analyzes issues associated with the implementation of higher-level programming languages. Topics covered include: fundamental concepts, functions, and structures of compilers, the interaction of theory and practice, and using tools in building software. The course includes a multi-person project on compiler design and implementation. [1] https://lagunita.stanford.edu/courses/Engineering/Compilers/Fall2014/about https://lagunita.stanford.edu/courses/Engineering/Compilers/... [2] http://web.stanford.edu/class/cs143/ http://web.stanford.edu/class/cs143/ [3] https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/ https://ocw.mit.edu/courses/electrical-engineering-and-compu...
- da_murvel 8y agoI'm currently traversing through Linux From Scratch, http://www.linuxfromscratch.org/lfs/ http://www.linuxfromscratch.org/lfs/. Not necessarily because I wanted to build a Linux OS but because it was the best guide I could find. But at a first glance this looks promising to me, so I'd definitely check it out and try out a few "chapters" when time comes.
- carlesfe 8y agoHi, author here. I’m humbled by the kind words. This is a project I started when I was in between jobs and unfortunately it’s kind of stalled because I had difficulties implementing the file system. Really want to get back to it some time. Anyway, feel free to leave your comments, I appreciate them a lot.
- LaneRendell 8y agoJust shot you an email to ask you to do that! Thanks for this resource. Good work :)
- carlesfe 8y agoGlad you liked it! Thanks for the kind words
- geezerjay 8y agoThank you for your work. It's really enjoyable and inspiring.
- LaneRendell 8y agoDoes anyone else have a good list of similar tutorials? I'm highly interested in this type of implementation from scratch approach.