Linux From Scratch Journey – Part 1

Last week, I decided to try to build linux from scratch, which means compiling it (the Linux kernel) from the source code. I was so excited about what I would face and I believed keeping track of what I did in a blog series was a good idea. So let’s jump to it!

First, I would assume the readers had tried using any OS that has Linux kernel before (except Android, which is pretty much so different now). After using some Linux distros, you will have a taste of what does Linux do and will understand what I’m talking about in this blog series.

In order to start doing this project, I need to follow a guide, which is called “Linux from scratch” itself: Welcome to Linux From Scratch!. If you are also interested in doing this project as well, you can follow that book as well as my series for some interesting discovery.

(Note: I will call Linux from Scratch as LFS for short)

I. Prepare the environment.

You need a Linux distro to start building LFS by your own (what an irony, but still). This is because the pre compiled Linux distro has some essential compiler, linker,… we need to build the very core of the Linux kernel. Well, if we are “guru” enough, we can write it our own and discard the pre compiled Linux distro, but that is not the point of this blog series. I don’t know anything about Assembly anyway to build these kinds of tools.

I chose Debian 8.x as the host environment, as the suggestion from the book. For me, Debian is easier to use than Gentoo, Slackware,… Actually, wandering some forums to find help building LFS, some more user-friendly Linux distros such as Linux Mint and Ubuntu is also possible to be the host environtment for building LFS.

Setting up Debian or any modern Linux distro is easy, I just need to follow the instruction in the Live CD Install. Note that I just use a VM to do this project, but the experience will be pretty much the same as in a real computer.

I booted into Debian. The book provided a pre-written version-check.sh file. This is used for checking some essential binaries and settings (symlinks) before I should proceed. This is required because in the later part of the book, it would assume these settings to be available. After version checking is completed, I proceeded to check 3 vital C library, which is gmp, mpfr and mpc (you can easily Google those). Unfortunately, they are not available. I had to go to these library’s website and download the source code to compile. However, after installing these library, the checker still said it could not find those libraries. Googling times again, some guy on LFS Forum said I need to link these lib from /usr/local/lib to /usr/lib. Problem solved!

The next thing I did is to make new partitions for my new soon-to-be LFS system. I restart my VM to boot into the Debian live CD again to do the partitioning. I set 10GB for root partition (ext4 system file) as the book suggest and 100MB for /boot partition (ext2 system file). Then I restart the VM again to enter Debian.

Those just-made partitions should not be pre mounted to Debian system, I then create LFS variable ($LFS) = /mnt/lfs and mount the root partition of new soon-tobe LFS system to $LFS. The reason why we need to use $LFS variable is that it would make the compiling and setting up easier, saving time (we can just tyle $LFS instead of /mnt/lfs everytime).

II. Proceeding to the building of LFS

In this section, currently there are nothing interesting to talk about, as the book provided very clear, even to the smallest detail why this and that happens and is necessary. Only one thing I would like to note here is to read carefully what the book put into Caution box (with Red borders) and some explanation note in Black borders. Anyway, reading carefully line by line will do most of the job. Eventually I am a newbie the LFS, being ignorant to what the author provided is stupid.

This is all I want to note in my LFS journey, as I am currently still building the Core Linux system stuff. Any comments and shares are appreciated!

 

How did I contribute to a coding project for the first time?

Last week, I managed to author a merge request to a C++ computational programming project in my university (It is not open source, yet, so I cannot share it in this blog post). It was such a thrilling moment when I first saw my name on the contribution record of the project. My “journey” to make my first contribution was kinda similar to what I read on the Internet about every first contribution. To mark this moment, I just want to write a short post about what I have done and experienced.

I was admitted as a Trainee (Programmer) to a Physics/Math research group in Tampere University of Technology, Finland. My task was to make programs/scripts to calculate complex mathematics calculations (including high-rank matrixes, linear algebra,… ).

In order to know what’s under the hood, I spent almost 6 meetings with my small group to understand what was the program supposed to run. This was the most time-consuming part of the project since I had never tried to read a pre-coded project. When I first saw the codebase, I was filled with anxiety that I might not even have the ability to fix a small part of it.

My mistake was that I tried to read every single file of the codebase, from top to bottom (of course, for now I know that is not the way I should do to read the codebase). That caused my confusion from lots of part in the source. Later I realized that first I need to make a map of the source by looking at the function declarations only, with the relationship with other parts of the codebase. After having the map, I did have a clearer vision of what should I start to comprehend the program.

I believe my approach was good that I managed to make my first patch only 1 week after I was assigned the task. So, I do recommend and I will remember this: Always have a broad vision of the project first, then jump into detailed parts.

How to keep programming as a hobby?

Disclaimer: I’m not affiliating the following courses in this blog. These are what I experienced and I would love to share with you readers.
I’m currently a student in an Udemy course and the lecturer just released a new announcement, which I think really is really relevant to the topic I’m going to talk about:
“Are you in the 60%? 

Let’s face it.

According to statistics, 60% of people who start learning programming end up quitting.

I am author of quite a few Python courses and I communicate with students every day and I know the exact reason why that happens. So let me tell you the reason and more importantly let me tell you how you can avoid falling in the same trap.

Have a quick look at this tiny Python program:

d = dict(weather = “clima”, earth = “terra”, rain = “chuva”)
def translate_to_portuguese(word):
return d[word]

word = input(“Enter word: “)
print(translate_to_portuguese(word))

If you know Python already you can easily tell that’s a program that let’s the user type in an English word and the program will output the Portuguese version of the word. For the sake of this reading, I included only three words there, but you can include a whole dictionary and the program will output every word. That’s not the issue here.

The issue is who’s going to buy or need such a fragile program. The program has two big problems:

1. The data of the dictionary is being stored inside the Python script. In real life you never store data in a program script.

2. The program needs Python to run. You can’t expect users to have Python installed in order to use the program.

So, that program works, but there’s a big gap between writing a program that works and making a program work for real users.

And there’s where beginners fail. They don’t know how to bridge that gap. In our specific example, they don’t know where to store that dictionary and how to make the program user friendly by transforming it into a desktop graphical program or a web app.

To bridge that gap, you need to take tutorials that cover how to create real world programs. Otherwise it can take ages until you learn such a skill.”[1]

This announcement from the instructor really reminded me to the very first day when I learned programming. I searched for online courses, programming books and after 1 or 2 weeks later. I lose all the courage and quitted.

Looking back to these days, besides what the above citation said, I realised I made a lot of mistakes that contributed so much to the reason I quitted programming quite a few times in the past, those were:

  • I was not patient enough to learn the basic carefully. I just wanted to have some “real things” like a Facebook clone or an OS… I then skipped reading the programming a little bit and searched for some tutorials to make those things on the Internet. Well, the problem was I did not have enough basic knowledge so I did not actually understand much of the tutorial. That depressed me hardly to continue learning. This is why knowing the basics well is so important. Now I do suggest you if you are planning to learn to program, take this course: https://www.edx.org/course/introduction-computer-science-mitx-6-00-1x-9 (MIT – Introduction to Computer Science 6.001×9). Do it carefully and you will be confident to achieve further.
  • I did not have a mentor/ I did not search for help on the Internet. Programming is very difficult. It is not about programming languages, but about problem solving with computers. When the problem is too hard and there is no help, what would you do? I suggest new programmers to find a guy to help you, maybe your senior, friend, … And do use Quora/Stack Overflow to search for problems. However, the first recommendation is more important. You will more likely to be helped more quickly and clearly as that mentor is at least closer to you.
  • I did not try to convert my own everyday thing to a program. I did regret this, a lot! It is actually very fun to do. You can convert a math problem like finding a all prime numbers from 1 to 100, sort a list to the ascending order,… At least do this once per day, you will learn really quick 🙂
  • I did not want to try to clone current program and always wanted to create a shiny new ones. I believe this is a damn huge mistake. Current programs have been designed nicely and they already had the structure for you to start thinking like a real software developer. Now this relates to the first quote in this post. You will need some tutorials to make a real application. This will be easily be found on the Internet. An easier approach is finding courses/bootcamps that teach you how to make real softwares, well a good example is the one in my reference.

I do think some of these links are also helpful. Please do read them if you have time and really want to learn to program 🙂

Reference:

[1]: Sulce, Ardit. “The Python Mega Course: Build 10 Real World Applications”. Lecture. Url: https://www.udemy.com/the-python-mega-course/