Linux From Scratch
From Wikibooks, the open-content textbooks collection
Contents |
[edit] Introduction
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. This book will 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-TO 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 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, which is currently:
- 2.6.12 17-Jun-2005
The version this book will use is:
- 2.6.8.1 14-Aug-2004
[edit] Subpages
[edit] Setting up the Hard Disk & Partitions
This book assumes 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.
[edit] 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 Drives are named /dev/sda1, /dev/sda2, /dev/sdb1, etc...
[edit] The disk-partitioning scheme for a Linux system
| Type | Partition | Filesystem | Code | Partition type | Size |
| boot | /dev/hda1 | ext2 | 0x82 | Primary | 30-100MB (determined by kernel size(s)) |
| swap partition | /dev/hda2 | swap | 0x82 | Primary or Logical | equal or twice the amount of physical RAM |
| root partition | /dev/hda3 | ext3 | 0x83 | Primary or Logical | 80 meg or less |
| /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).
[edit] 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.
[edit] 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
Device Boot Start End Blocks Id System /dev/hda1 1 14 xxxxx+ 83 Linux
- To make this partition bootable, Type a then 1 to toggle the bootable flag. It should now print like this:
Device Boot Start End Blocks Id System /dev/hda1 * 1 195 xxxxx+ 83 Linux
[edit] 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".
[edit] 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.
[edit] 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
[edit] 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.
- 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.
[edit] 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[1], 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.
[edit] 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
[edit] Constructing a Temporary System
[edit] The Kernel
[edit] Shell Interface
[edit] The X Window system
[edit] Desktop
[edit] KDE
[edit] Gnome
[edit] The Linux File System Structure (FSSTND)
Linux Filesystem Hierarchy FSSTND
/bin ---- system binaries, user programs with normal user permissions
/boot
/dev ---- system device tree
/etc ---- system configuration files
/home --- users' subdirectories
/home/{username} users' subdirectories
/initrd
/lib --- libraries needed for installed programs to run
/lost+found
/media
/mnt
/opt
/proc
/root
/sbin --- executables that need root permission
/usr ---- applications software
/usr/bin - executables for programs with user permission1
/var ---- system variables
/srv
/tmp ---- system temporary files
[edit] External links
- Wikibooks:Computer_software_bookshelf#Linux
- Linux Kernel
- Linux Documentation Project Guides
- Super man pages (command help pages)
- freshmeat access to official download sites for software

