# Linux From Scratch

This book is an open source attempt at discussing how to create a Linux computer system by starting with a blank hard disk and assembling all the "parts" needed for a Linux distribution such as Debian, Fedora core, or Slackware. If you do not know what Linux is, STOP. Go read some other book on the subject of Linux first -- perhaps Using Ubuntu Linux, Puredyne, Linux Guide, or Knowing Knoppix. Reading this book first will only make you more confused. This is not intended to be a newbie guide nor an installation manual. It is more of a map on discussing all the aspects of a real world attempt to create a Linux-controlled computer, specifically an x86 computer, as this is what the original kernel was written for. There are thousands of tutorials on how to create Linux, even a website dedicated to this very topic (linuxfromscratch.org). Those instructions are not very detailed and are closed to editing; therefore, this is a Wiki-style attempt at reproducing the instructions on how to build a Linux system from scratch, for non-computer scientists. This attempt at a new interpretation is meant as simply another resource to be used with the book from www.linuxfromscratch.org as well as any other HOW-TOs available from the Internet.
As for distros that accomplish this task, Gentoo is pretty close to installing everything from scratch. However, after the compiling of the kernel, it's highly automated from there on, using the emerge tool to download, build, and install everything. That is where this approach differs. Everything will be edited by "hand" and compiled individually.
Before you begin, read this checklist and make sure you have all of the information asked.
In order to determine if the system you are going to install Linux on is adequate, please read the "Before You Begin" section of the Linux HOW-TO.
The Linuxfromscratch site will be referred to as LFS in this wikibook.
This book will use the latest stable version of the Linux kernel.

## Contents

* Packages

## Setting up the Hard Disk & Partitions

This book assumes that the hard disk is completely blank with no existing partitions. Make sure you are logged in as root, check by typing whoami.
The first item needed is a LINUX boot disk to run FDISK or CFDISK (or you could use a non-destructive partitioning program such as FIPS, QTParted, or Paragon Partition Manager -- DO NOT use Partition Magic, it seems to have lots of trouble understanding Linux partitions). The best way to do this part is to get a live Linux CD distribution, such as Knoppix, KLAX, SLAX or LFS, which have all the packages included. The GParted LiveCD has been released specifically for this purpose. This way you will have a booted system, access to FDISK or CFDISK, as well as shell access to run utilities.
There are several partitions to make. In theory, a Linux system could run under a single partition (like Windows or MS-DOS systems do), but almost no one does this. Therefore, the creation of specific partitions is the first step.

### The naming convention for IDE devices

hd designated the device "harddisk" a,b, etc... designates the first drive, second drive, etc... 1,2,3,etc... designates the partition.
| drive name | drive number | partition number |
| /dev/hda1 | 1 | 1 |
| /dev/hda2 | 1 | 2 |
| /dev/hdb1 | 2 | 1 |
| /dev/hdb2 | 2 | 2 |
SCSI/SATA Drives are named /dev/sda1, /dev/sda2, /dev/sdb1, etc...

### The disk-partitioning scheme for a Linux system

| Type | Partition | Filesystem | Code | Partition type | Size |
| boot | /dev/hda1 | ext2 | 0x83 | Primary | 30-100MB (determined by kernel size(s)) |
| swap partition | /dev/hda2 | swap | 0x82 | Primary or Logical | equal to twice the amount of physical RAM |
| root partition | /dev/hda3 | ext3 | 0x83 | Primary or Logical | 80 meg or more |
| /usr system software partition | /dev/hda4 | ext3 | ox83 | Primary or Logical | up to 300 meg or so |
| Home partition† | /dev/hda5 | ext3 | 0x83 | Primary or Logical | whatever is needed |
†Optional This scheme is by no means static, one could add more partitions, such as /var or /tmp (or even just use one for the entire system -- this is not recommended, however).

### Creating the partitions

To create a partition on the first hard disk, use a program like FDISK or CFDISK. Since fdisk is the most commonly used program in most Linux installation HOW-TOs, we will use this in our example.
Pay attention to the names of the partitions as this information will be needed later for the /etc/fstab file.
To see the partitions available, type:
    fdisk -l
Here we will create three partitions: the boot, swap and root. Other partitions can be created later. To start the program, type: fdisk /dev/hda (replace hda with what ever drive you are installing Linux on). To display the partition table within fdisk, type p. This guide will assume there are no other partitions. For a system with other partitions (such as Windows), please consult other guides.

#### The boot Partition

* Type n to create a new partition, fdisk will display:
    Command (m for help): n
    Command action
       e extended
       p primary partition (1-4)
* Select p to select a primary partition, (/dev/hda1)
* Then 1 to select the first primary partition.
* Then choose the default the 1-xxxxx refers to the number of cylinders on your specific drive, which will vary according to size.
* Hit enter for the default
    First cylinder (1-xxxx, default 1)
* Then fdisk will display:
    Last cylinder or +size or +sizeM or +sizeK (1-xxxx, default xxxx):
** For this part you will create a 100MB boot partition, so enter +100m
** If you print the partition (p), it should display your partitions
* To make this partition bootable, Type a then 1 to toggle the bootable flag. It should now print like this:

#### The Swap Partition

* Follow the same procedure for creating the boot partition,
** Type n to create a new partition, fdisk will display
** Select p to select a primary partition,
** Then 2 to select the second primary partition. (/dev/hda2)
* fdisk will start at the next available cylinder(196), hit enter for the default.
* Then type +512M to create a partition 512MB swap partition
* Now, type t to set the partition type, 2 to select the partition you just created and then type in 82 to set the partition type to "Linux Swap".

#### The Root Partition

* Type n to create a new partition, then p to tell fdisk that you want a primary partition. Then type 3 to create the third primary partition (/dev/hda3).
* fdisk will start at the next available cylinder(1189), hit enter for the default.
* As for the end, make it whatever size you want, but at least 80MB
* Now hit w to write the partition table. This step finalizes the partition creation process.
* Type fdisk -l to see the new partitions.

## Creating the filesystems

| Filesystem | Creation Command |
| ext2 | mke2fs |
| ext3 | mke2fs -j |
| reiserfs | mkreiserfs |
| xfs | mkfs.xfs |
| jfs | mkfs.jfs |
* Format the boot and root partitions using ext2 filesystem. (ext3 can also be used)
    mke2fs /dev/hda1
    mke2fs /dev/hda3
* Initialize the swap partition; you can label it with the -l command
    mkswap /dev/hda2
* Activate the swap partition;
    swapon /dev/hda2

## Mounting filesystems on Partitions

Mounting a partition means attaching it to the Linux filesystem, you don't mount a device or partition, you mount the filesystem "somewhere" in order to continue building Linux.
Before mounting the filesystems, they should be checked, unmounted.
* First type mount to see what is mounted, if any /hda partition is mounted, umount it. (Note: The swap partition will show as mounted per the previous step. If you want to check it you will need to unmount it with swapoff /dev/hda2 first, then use swapon again.)
* To check the file system, type
    e2fsck /dev/hda1
Information about the partitions can be displayed using the df command with the -h or human readable option which greatly improves readability.
For the initial installation, a mount point need to be created and assigned to an environment variable. Think of it as a space in memory to mount the partitions to. The variable can be anything you want, here it will be LFS and created by running:
export LFS=/mnt/lfs
* Create the mount point variable. (the -p switch os the parents option, it makes parent directories as needed.
    mkdir -p $LFS
* Mount the LFS (root) file system
    mount /dev/hda3 $LFS
* Check that the LFS variable is set up properly:
    echo $LFS
** It should respond /mnt/lfs
* Check the new partition with mount to see if it is not mounted with permissions that are too restrictive (nosuid, nodev, or noatime). the response should be:
    /dev/hda3 on /mnt type ext2 (rw)
** All programs to be compiled will be done so under the $LFS/tools directory, so create it.
    mkdir $LFS/tools
* Next a symlink needs to be created for the tools directory.
    ln -s $LFS/tools /
* Make a directory to store the source tarballs with the correct permissions:
    mkdir $LFS/sources
    chmod a+wt $LFS/sources
* to see your directories, type
    ls $lfs
** if you want to build the packages as a SU, rather than root, see LFS chapter 4.3
* create a new .bash_profile
    cat > ~/.bash_profile << "EOF"
    exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
    EOF
* Create the .bashrc file for a non-login shell:
    cat > ~/.bashrc << "EOF"
    set +h
    umask 022
    LFS=/mnt/lfs
    LC_ALL=POSIX
    PATH=/tools/bin:/bin:/usr/bin
    export LFS LC_ALL PATH
    EOF
* prepare the environment for building the temporary tools, source the just-created user profile:
    source ~/.bash_profile
Now an environment is available for the build.

## Building Temporary System

This section will guide you through the process of building all the packages you downloaded into the sources folder.
First we must move into our sources folder where all our packages are. Note: If you can't download the source files 1) look /lfs-sources directory 2) use cp command to copy the source files to $LFS/sources directory
    cd $LFS/sources
Then follow the LFS book for installing packages in version 7.0(the one I am using) we install binutils-2.21.1a. An important thing to remember you must do this for all packages: 1. extract the content of the package. 2. go to the package 3. follow the instructions in the book 4. after building the package delete all content. AN example of this is shown below for bintuils-2.21.1a
when extracting packages we will use the tar command. For files that end with .bz2 i use tar xvjf "package name here" (without quotes) and for .gz i use tar xvf"package name here"(without quotes).
* tar xvjf binutils-2.21.1a.tar.bz2
    cd bintutils-2.21.1a (faster way is cd binutils*)
binutils recommends that we build a separate build directory
    mkdir -v ../binutils-build
    cd ../binutils-build
now we prepare binutils for compilation
    ../binutils-2.21.1/configure \
--target=$LFS_TGT --prefix=/tools \ --disable-nls --disable-werror
after it is finished we use the make command to compile the package note that if you have more than one core you can use the -j"number" command so i have 2 cores so i'd use make -j2
    make or make -j2(for 2 core processor only)
After this is complete for 64 bit computers we must create a symlink to ensure the sanity of the toolchain
    case $(uname -m) in
x86_64) mkdir -v /tools/lib && ln -sv lib /tools/lib64 ;; esac
and then install the package
    make install
after this is done we must remove the files/folders so go back to the sources folder
    cd ../ or cd $LFS/sources
then remove the files/folders
    rm -rv binutils-2.21.1 binutils-build
and voila you have successfully built your first package now use this format to build the rest of your packages, remember extract,go in extracted directory, follow book, remove content

## Installing software packages

In this section, the package versions used by the LFS book will be used as they have been tested, and are known to work. The reason being that, for example, package "foo" might need package "bar" to be at a certain stage of release, and so on. Therefore, this part of the book will almost mirror the original LFS book, adding details where they are absent from LFS.
The LFS website offers a LIVE CD via emule or bittorent. To use this option, go to the LFS page and click on the packages link. Download the LIVE CD.

## The Packages & Patches

The list is found at LFS Packages and LFS Patches.
Download or otherwise obtain the packages. For a more detailed list of the packages used, read this subpage

## GNOME
### The Linux File System Structure (FSSTND)

Linux Filesystem Hierarchy FSSTND

## External links

* Linux Kernel
* Linux Documentation Project Guides
* Super man pages (command help pages)
* freshmeat access to official download sites for software