My Linux From Scratch Experience
The Linux philosophy is ‘Laugh in the face of danger’. Oops. Wrong One. ‘Do it yourself’. Yes, that’s it.
~ Linus Torvalds
Since I started working with Linux a few years ago, I started having unconditional love towards this technology. Being able to customize it to fit my needs, easily changing themes and installing tools, and best of all, completely free and open source!
Many tools have been built on top Linux, including Docker. It also powers more than 96% of servers worldwide, according to www.enterpriseappstoday.com. It has many distributions built on top of it, including Debian, Ubuntu, CentOS, Arch, and many more. There is even a Hannah Montana theme.
With all this passion in me, especially having been using Linux throughout the past few years as my main Operating System for both the servers I work on, as well as my own PC, I decided to go ahead and attempt to build my own Linux from scratch.
What is Linux From Scratch?
Linux From Scratch is a project by Gerard Beekmans that includes a book to help you create your own Linux setup, completely from scratch. It includes many aspects, from creating a separate volume, to building and compiling the necessary tools. It allows you to build a very simple version of Linux for a better understanding of how it is created, and what the process is.
The Process
The process is very long, it took me around 3-4 weeks to fully complete it. It includes many steps, which I will describe briefly below.
My Setup and Prerequisites
My main PC has an Intel i7 CPU, 64 GB RAM, and 2 TB of SSD. I created an Ubuntu Virtual Machine on top of it using VirtualBox. The Virtual Machine had 12 GB RAM, 6 Processors, and around 80 GB of storage on 1 volume.
Regarding the virtual machine, I had to have a GUI because some tools needed to be installed using the browser. I used Ubuntu because I am familiar with it the most and it is easy to setup and get started with it.
If you plan to take on this challenge, you also need to create a Virtual Machine to begin. This is necessary because you will have to change a few things in the main host where you will build your Linux From Scratch (or LFS for short), and it is very risky to change some critical files on your main PC because it can cause it to break and stop working properly.
According to the book, the partition where thr system will be built should be around 30 GB. I ended up creating the Virtual Machine with 80 GB, and created the partition with around 35 GB. I also recommend using a good amount of RAM and a good CPU because there will be lots of heavy computing and processing going on, so it is better to increase the resources in order to reduce the time spent doing all the building and computing.
The Setup
After you create a virtual machine (or VM for short), there are a few more steps needed to setup the environment. The setup is mainly divided into three steps, which need to be done sequentially.
The first step to create a partition on the VM, the book recommends using the path /mnt/lfs which is also the path I used in my build.
The second step is to download all the necessary files. These files include the main Linux kernel, GCC for compiling, Glibc, and many more. Some of these files are necessary for building the main system, and some other tools are needed so that they can be used later, such as cp for copying files, and many more.
The third step is to create the LFS user which will deal with a lot of the compiling of the packages. You also need to change a few other configuration files, such as adding the path of the LFS system as an environment variable.
hazem@LFSHostUbuntu:~$ echo $LFS
/mnt/lfs
The Building
The building of packages is divided into many sections, as can be seen in the Table of Contents of the book.
The first step, is to build a minimal version of the necessities. These include compiling and install the GCC, Linux kernel, Ncruses, Bash, and a few more. It will be a minimal version because we might need a few packages in order to compile a more complete version, and these packages can only be built after the necessary packages like GCC have been installed.
After downloading and installing the necessary packages. You can now enter the chroot.
What is chroot?
According to Wikipedia, A chroot on Unix and Unix-like operating systems is an operation that changes the apparent root directory for the current running process and its children. This is also referred to as a jail. It forces the user to use that set path as the main directory of the system (the root directory). This forces all changes to be done inside the chroot directories only and perhaps even as the root user. This also blocks access from all the files are that outside of this directory. For example, say you entered chroot at /mnt/lfs. If you try to access the root path (cd /
) it will basically take you to /mnt/lfs.
After Entering Chroot
Once you are in the Chroot on the path /mnt/lfs, you will need to complete the building and compiling of all the other applications and tools. The process for compiling the tools takes a long time for two reasons:
- There are many tools that need to be configured, compiled, and then installed, sometimes having more steps in between.
- Compiling and building a tool can take lots of time. Some tools took me only a few seconds to complete, while others took me hours.
Completing The Building
Once all the tools are setup properly, the last step consists of adding and changing the configurations. The files that need to be changed are related to the network, the disks/volumes and file system, hosts, and more. This will ensure that the system is setup properly, allowing it to boot using Grub. You can restart the VM, then you should be able to boot into your newly created Linux system. Congratulations!
Advantages & Disadvantes
I went through the whole purpose, simply for the purpose of learning more about the internals of the Linux system, and how one can go about building their own customized version of it. I saw many advantages during the process:
- You will learn more about the Linux system, from how the tools are compiled, the benefit of the configuration files that used to confuse me before, and much more.
- You will understand the process of compiling a tool. How you can configure it to fit your need, then use the included Makefile to build the system and also install it.
There are also a few disdvantages that might keep you from going through this long process:
- It take lots of time. Most of my time went to waiting for a tool to be configured and built, since some of them can take hours to complete for one command.
- It is difficult to expand the system afterwards. Once I completed the process and ensured it worked fine, I realized how much MORE time I needed to make it a usable system that I can use on a daily basis, whether on a server or on my personal PC.
Conclusion
I enjoyed the process and learned a lot from it. I can see myself doing it againfor the purpose of building my own, small, distribution. It was a huge learning curve since I never went this deep in any software I had previously used.
I recommend this process to people who have some free time, have access to a powerful PC, and have lots of patience (trust me, you need it). You will learn lots of stuff about Linux and you will understand the benefit of following guidelines for working on a project (I understood the value of Makefiles after building this system).
Thanks for reading. I hope this article was informative and it made you more excited about Linux From Scratch and Linux itself. Best of luck to you!