ELS Howto: Build ELS From Scratch


Synopsis

This document explains how the Elemental Linux Server (ELS) is built from source code. It is not intended to be a detailed tutorial, but rather an overview of the process for those who are interested.


Prerequisites

In order to transform souce code into the executable programs needed for the ELS distribution I must have another Linux system available with a compiler and associated tools. I'll also need a PC with no operating system installed to use as a test environment. I use a VirtualBox OSE Virtual Machine (VM) for both of these systems.

I always start with the latest version of Slackware Linux for my development system. ELS can probably be built using other distributions' compilers, but Slackware was the first distro I ever used and therefore holds a special place in my little penguin heart. I install Slackware, minus any GUI components, into a Virtual Machine and I'm ready to start building.

For the test environment, I create a second virtual hard drive attached to my Slackware VM. In the beginning the second disk will have its partition mounted on /mnt of the Slackware system. Later on the hard drive order will be switched and it will be the boot drive.

Note

At the time this was written, the latest Slackware version is 13.1 and requires downloading the disc one (d1) ISO image in order to have a working development system.

Development System Installation

The Slackware installer still uses the concept of disk sets, a holdover from the days of floppy-based installs. I do a full install of disk sets A, AP, D, L and N. This gives me all of the tools I need without too much extra stuff. Here are the steps:

  1. Create a VirtualBox Virtual machine for Slackware with the following charcteristics:
  2. Boot from the Slackware CD-ROM image.
  3. Install Slackware disk sets A, AP, D, L and N.
  4. When finished, unmount the ISO image from the virtual CD-ROM and reboot the virtual machine.

Configure the Development System For Building

Before getting started, I need to download all of the source code for the ELS packages as well as the build scripts that contain the commands to turn the source code into binary packages.

  1. Download the latest stable kernel source code, extract it and create a symbolic link to it named /usr/src/linux.
  2. Download the source code for the Stage 1 packages (listed below) into /usr/src. I can choose to extract it all at once or save disk space by extracting one package at a time as I build.
  3. Download the ELS build scripts into the /root directory.

Stage 1

The first round of packages to be built can be divided into two categories, those that are required for basic system functionality, indicated with an (S) in the list below, and those that are used as develpment tools, indicated with a (D). What they all have in common is that most of them have no dependency on any software packages other than glibc (the few exceptions are binutils which requires zlib and gcc which requires gmp and mpfr.) This means when the binaries are moved from the Slackware system to the test system, they won't fail to run because of missing libraries or complain about missing companion programs. I just have to be careful about the versions of the glibc, zlib, gmp and mpfr libraries that I choose so that they are compatible with the ones used on my Slackware development system.

Building Stage 1 Packages

The ELS build scripts are essentially just shell scripts that automate the commands used to create a package. They can be run with the command sh ./script-name. I'll need to run the build script for each of the packages in Stage 1.

In alphabetical order, these packages are:

Installing Stage 1 Packages

After after all of the stage 1 packages have been built it's time to install them onto a test system. There is no installation CD at this point, so I install everything manually onto a second virtual hard drive attached to my Slackware development VM. If everything works as planned, I should be able to build the remaining code using the development tools on the second hard drive instead of using the Slackware compiler.

Here is a high-level overview of the steps involved:

  1. Create another 8G hard drive using VirtualBox's Virtual Media Manager and attach it to port 1 of the Slackware VM's SATA controller. This will be the test environment.
  2. Partition the drive as one large primary partition and create a Second Extended (ext2) filesystem on it.
  3. Configure /etc/fstab to mount the new filesystem on /mnt and mount it.
  4. Change the working directory (cd) to /mnt and extract all of the binary packages built in stage 1.

Here's a handy one-line script I use to speed up package extraction:

cd /mnt; for PKG in /var/tmp/staging/*.tar.gz; do echo $PKG; tar -zxf $PKG; done

Testing Stage 1

I test the installation by changing root (chroot) to the new system's hard drive and running some common commands. It is important that the commands listed below actually run successfully, because in addition to testing, they prepare the environment for Stage 2.

  1. Execute the command chroot /mnt /bin/sh to enter a chroot environment.
  2. Set the PATH variable to /opt/bin:/usr/sbin:/usr/bin:/sbin:/bin.
  3. Create simple /etc/passwd and /etc/group files using the ed editor.
  4. Create a symlink called /bin/bash that points to /bin/bash-static.
  5. Create the directory /opt/bin.
  6. Create symbolic links in /opt/bin to all of the gcc binaries (e.g. ln -s ../gcc-/bin/* /opt/bin).
  7. Check that the compiler is working by compiling a simple "Hello World" C program.

If all tests were successful, I can exit the chroot environment and get ready for the next stage.

Simple /etc/passwd file

root::0:0:Super User:/root:/bin/sh
bin:x:1:1:Legacy UID:/bin:/bin/false
daemon:x:2:2:Legacy UID:/sbin:/bin/false
nobody:x:99:98:Nobody:/tmp:/bin/false

Simple /etc/group file

root::0:root
bin:x:1:root,bin,daemon
daemon:x:2:root,bin,daemon
nobody:x:98:nobody
nogroup:x:99:

Hello World in C

#include <stdio.h>

int main(void)

{
  printf("Hello World!\n");
}

Stage 2

The second round of packages to be built are the libraries and other programs needed to build packages in the upcoming stage 3. I build these in the chroot environment, but I could also choose to set up Slackware to dual-boot off the second virtual hard drive. Using chroot is more forgiving. If I neglect to download a build script or source code I can always hit <ALT>+<F2> to change to another virtual console on the development system and get what I need.

Preparing for Stage 2

Before entering the chroot environment, I should confirm that I have all of the things I need to build Stage 2. I'll use the following checklist to make sure I'm ready.

  1. Verify that the second virtual hard drive is mounted on /mnt.
  2. Copy the build scripts for all Stage 2 packages into the /mnt/root directory.
  3. Copy the source code for all Stage 2 packages into the /mnt/usr/src directory.
  4. Enter the chroot environment.
  5. Set the PATH variable.

Building Stage 2 Packages

In alphabetical order, these packages are:

Installing Stage 2 Packages

While still in the chroot environment, I do the following:

  1. Change the working directory (cd) to /.
  2. Extract all of the binary packages built in Stage 2.

Again, I can do this with a one-line script:

cd /; for PKG in /var/tmp/staging/*.tar.gz; do echo $PKG; tar -zxf $PKG; done

Testing Stage 2

I'll know everything in stage two went well if I can build all of the packages in Stage 3.


Stage 3

The third round of packages will complete the system in terms of essential binaries. I build and install all of these packages in the chroot environment. I can build them all first and then install at the end or I can install as I go. One nice thing about installing as I go is that I can quickly start using a full-featured BASH, with the nice features of command completion and command history, instead of the stripped-down static root shell.

Building Stage 3

In alphabetical order, these packages are:

Installing Stage 3 Packages

If I have not been installing packages as I go, I'll need to install everything now. While still in the chroot environment, I do the following:

  1. Change the working directory (cd) to /.
  2. Extract all of the binary packages built in stage 3.

Here's the handy one-line script I use to speed things up:

cd /; for PKG in /var/tmp/staging/*.i586.tar.gz; do echo $PKG; tar -zxf $PKG; done

Configuration Files

At this point all of the packages are installed for a working system, but I'm lacking configuration files. A checklist of configuration files is below.

Basic system configuration

Users and logins

Basic networking

Network services


Stand-alone ELS

Now it's time to cut loose from the development system and tart booting from the test environment hard drive.

Note:

Keep in mind that the Slackware development system is running with the ELS environment installed on the first partition of the second SATA disk and that filesystem is mounted on /mnt. Linux refers to this partition as /dev/sdb1 and grub calls it (hd1,1). When I swap the order of the drives, the ELS partition will change to /dev/sda1 for Linux and (hd0,1) for grub.

Here are the steps I take:

  1. Enter the chroot environment on the development system (i.e. boot Slackware and chroot /mnt /bin/sh).
  2. Install grub on Master Boot Record (MBR) the 2nd SATA disk with the command grub-install /dev/sdb
  3. Create a grub configuration file with the command grub-mkconfig -o /boot/grub/grub.cfg
  4. Edit /boot/grub/grub.cfg replacing any reference to (hd1,0) with (hd0,0) and any reference to /dev/sdb with /dev/sda
  5. Shutdown the Virtual Machine (VM).
  6. Swap the order of the virtual hard drives configuring the disk with the ELS system as SATA port 0 and the disk with Slackware as SATA port 1.
  7. Start the VM.

If all goes well, I'll be looking at a grub menu and then booting ELS. If I get a grub> prompt then there is a problem and I need to put the drives back on their original SATA ports and try the steps again.


Building extra packages

Due to dependencies on other extras, PHP should be built last. Otherwise, I can build and install in any particular order.

Note

Part of the installation of extra packages includes setting up symbolic links in /opt/bin and /opt/sbin that point to the package binaries. Aquick and easy way to do that is with a couple of one-line scripts:

cd /opt/bin && for FILE in ../*/bin/*; do ln -s $FILE .; done
cd /opt/sbin && for FILE in ../*/sbin/*; do ln -s $FILE .; done

Here are the extras


Installation Media

The preferred way of installing Elemental Linux Server is using a bootable CD-ROM. I need to create a CD-ROM that has enough utilities to perform the following installation tasks:

The way ELS is designed, all of the funtionality required for installation is in the / (root) filesystem and all of the extra stuff is in /usr and /opt. The extra stuff will be nice to have for a 'live CD'. However, one of the goals of ELS is to support older systems, so I need to balance the added functionality with low system requirements.

For the live CD I'll be putting the / filesystem in a ramdisk and /usr will be mounted read-only from the CD-ROM. The / filesystem is slim (under 32M) so this helps keep the RAM requirements low for the target system.

Building root on a ramdisk

I start by creating a temporary directory for the ramdisk files and another for the cd-rom files. First, I extract all of the packages in the base set into the ramdisk directory. Next, I move the /boot and /usr directories into the cd-rom files directory. I then create symbolic links for the /boot and /usr directories that point to /media/cdrom/boot and /media/cdrom/usr respectively. Once everything is put together it will look like one contiguous system, but / will be running in RAM and /usr will be running from the CD-ROM.

Edit fstab and remove noauto from /dev/sr0. Create the ramdisk. Move rootdisk image to cd-rom boot directory. Create a grub.cfg to boot the cd-rom. Copy base and extra directories to cd-rom. Use grub-mkrescue to create the ISO image.

More to come ...