How to make a LFS Linux bootable CD



Acknowledgments

Thanks to:

Gerard Beekmans for the LFS system

Martin L. Purschke for the original idea about this CD

Ideas, reports, and fixes from users, and the LFS lists that have been used in this package

Copyright Chris Lingard - GNU GENERAL PUBLIC LICENSE

LFS bootable CD

Introduction

This is my method of making a bootable CD. The Linux system used is LFS, details of which can be found at www.linuxfromscratch.org. I started this project when I realised that I had no proper recovery system, and it just grew from there.

I tried various other systems; the best being cd_template by Martin L. Pursche. Extending this package, to boot a full Linux system, became my goal.

Here are the standard warnings. You must be confident enough to build systems, with various hacks and modifications. You must be careful enough to avoid wrecking your systems; many of the scripts will mess up your base system if run wrongly.

How it boots

First a few words on how to make a bootable CD, (using lilo).

When you burn a CD using mkisofs, you have the -b option to specify the boot image. This boot image must be something that the hardware understands, therefore you need to emulate a floppy. Since we are only pretending that is a floppy, we may choose any type, so we choose the largest -- a 2.88Mb floppy.

We make a file system using a loop device that is exactly 2.88Mb, copy files to it, then run lilo on it.

Here is a lilo.conf:

cat > $TOPDIR/lilo.conf << EOF
disk=/dev/loop1
bios=0x00 # bios ID from drive A, we need that here because lilo
# need to know which boot device to use
sectors=36 # 2.88MB disk geometry
heads=2
cylinders=80
timeout=30

 boot=/dev/loop1
 message=/mnt/loop1/boot/boot.msg
 install=/mnt/loop1/boot/boot.b
 map=/mnt/loop1/boot/map
 prompt
 compact

 image=/mnt/loop1/boot/vmlinuz
   label=linux
   initrd=/mnt/loop1/initrd.img
   append = "root=/dev/ram init=/linuxrc rw"
   ramdisk=$ISIZE

EOF
# run lilo
/sbin/lilo -C $TOPDIR/lilo.conf


At the start of the above, you can see us telling lies to the hardware, that this is a 2.88Mb floppy.

We have put a kernel and boot message into this file system.

The initrd.img is a compressed file system.

linuxrc is an executable script in the root of this compressed file system.

This must all fit inside 2.88Mb.

The file system may be 6.5Mb, before compression, and still be small enough.

linuxrc may be a symbolic link to bash

The file system may contain anything you like, but this is all you will have after booting the CD

You can make this system inside a directory; it will look something like:
/dev  #  The devices that you need
/bin  ->  sbin
/lib  # Needed if you are using dynamic programs
/linuxrc  #  The script
/sbin     #  You selection of programs



Once you have made this; you calculate its size, make a file system on a loop device, copy the files, then compress it into initrd.img.

During the linuxrc script you would mount the cdrom, giving you access to whatever you put on the CD. See the mount_cdrom script below that auto-detects the cdrom, and modify it to do what you want.

The base system

The base system's kernel will need loop devices enabled. I use mkisofs and cdrecord from package cdrtools-1.10a17. You will also need directories to use the loop back devices; /mnt/loop1 and /mnt/loop2 are what I use.

The image system

You must decide if you just want a CD that will work on your machine, (and any identical); or you want a CD that will work on any PC. If you just want a recovery/repair disk then download the package and go to the final chapter to make your CD.

If you want a generic CD, then you must build a generic system to act as the image.

To do this you must do the following:

Change the .bash_profile

Both the building systems .bash-profile and the .bash_profile that is created when building the LFS system must have the following lines added
CFLAGS=' -m486 -march=i486 -Os '
export CFLAGS

The 486 stuff forces the compiler to build code that will work on 486 and Pentium PCs. The -Os flag optimizes for minimum size, and suppresses the -g flag causing stripped binaries and libraries to be made.

Apply the uname hack

On your building system apply this hack to uname
mv uname uname.real
cat > uname << EOF
uname.real $@  |  sed  's/[456]86/486'
EOF
chmod 755 uname


On the static build of sh-utils append these instructions to the book:
# Modification for false uname to answer i486
mv  $LFS/bin/uname  $LFS/bin/uname.real
cat  > $LFS/bin/uname <<EOF
#!/bin/bash
/bin/uname.real "\$@"  |  sed 's/[456]86/486/'
EOF
chmod 755  $LFS/bin/uname
cp  $LFS/bin/uname  $LFS/bin/uname.false

The \ in the "\$@" hides the $ from the shell.
On the dynamic build of uname append these instructions to the book:
# Mofification for false uname to answer i486

mv  $LFS/bin/uname  $LFS/bin/uname.real
mv  $LFS/bin/uname.false $LFS/bin/uname

You can now build a generic, (i486), system.

The kernel must be built i486 and the following must be set:
CONFIG_BLK_DEV_RAM_SIZE=32768
The system used for the image can be any working combination you choose. Things that use many shared libraries, such as KDE, should be avoided, because they will be too slow for any effective work on a CD. I use a standard LFS plus a few extras such as gpm, X and lynx. Remember to build everything i486

So, following on from the above here are some notes on how to make a bootable CD, and what you should put into the boot image.


Making the package

You will need two LFS systems to generate a bootable CD. One is the base system that will be used for building. The other is the system that provides the image.
If you do not want to build you own boot package, then you can download mine from www.stockwith.uklinux.net. If you do not have a small optimised system, then your programs and libraries may be too large to make a boot image. Here is how it works.

Let us first consider what we want. We need a writable root partition, so we will make one out of RAM. We will want /proc and /cdrom mounted on the root. I also wanted /root, /dev and /etc writeable, so these are unpacked from tar files during the boot process. (How the image system as prepared is described later).

The start up linuxrc will achieve this, then pass control to init. Here is my linuxrc script:
#!/bin/sh

echo "Welcome to the LFS Linux CD system"

# /proc is needed to find the CD
mount -t proc none /proc

# Make a 25 Mb file system for root
dd if=/dev/zero  of=/dev/ram  bs=1k  count=25000
mkdir  -p  ram
mke2fs -q /dev/ram
# Mount our new root file system
mount  /dev/ram  /ram
cd /ram
mkdir cdrom
# Find the CDROM
# This will auto-detect and mount the CD
/mount_cdrom
# Mount proc and set links to the CD
mkdir proc
mount  -t proc  none /ram/proc
ln -s  cdrom/bin  bin
ln -s  cdrom/sbin  sbin
ln -s  cdrom/lib  lib
ln -s  cdrom/boot  boot
ln -s  /cdrom/usr  usr
# Set up some swap space
dd  if=/dev/zero  of=swapspace bs=1k  count=5000
mkswap  swapspace
echo "Preparing file systems for pivot"
# Finish off setting up
mkdir home
mkdir disk
mkdir tmp
mkdir mnt
mkdir mnt/lfs
mkdir var
cd var
mkdir lib lock log mail run spool tmp opt cache lib/misc local
cd /ram
echo "Unpacking tar files for /root /dev and /etc"
# These are the only writeable directories
tar xf /ram/cdrom/root.tar
tar xf /ram/cdrom/dev.tar
tar xf /ram/cdrom/etc.tar
mkdir initrd
# Swap root file systems /dev/ram <-->  /dev/root
pivot_root  .  initrd
# Change to new root and swap to new root's devices
exec /usr/sbin/chroot . /sbin/init <dev/console >dev/console 2>&1
# It should never reach here
exit


The only non standard thing used here is mount_cdrom, this is it.
#!/bin/sh
for ide in ide0 ide1; do
for disk in hda hdb hdc hdd hde hdf hdg hdh; do
   if [ -r /proc/ide/$ide/$disk/media ] ; then
     if grep -s -q "cdrom" /proc/ide/$ide/$disk/media; then
         if  mount -t iso9660 -o ro -n /dev/$disk /ram/cdrom 2> /dev/null  ; then
            echo "Found the CD-ROM on $disk"
            exit
         fi
     fi
   fi
done
done

if [ -f /proc/scsi/scsi ] ; then

    if ! grep -q "Attached devices: none" /proc/scsi/scsi
      then
      for drive in `echo /dev/scd?`
      do
        if ! mount -r -t iso9660 $drive /ram/cdrom 2> /dev/null
        then
          continue
        else
          echo "Found the CD-ROM on $drive"
          exit
        fi
      done
    fi
fi
echo "No CD-ROM found"



From this is can be seen that the following programs are needed during the boot process:

bash cp echo gzip mkdir mkswap mv rm tar chroot grep ln mke2fs mount pivot_root sh

Because shared libraries plus programs are smaller than static programs, the following libraries are needed:
ld-2.2.4.so
ld-linux.so.2 -> ld-2.2.4.so
libc-2.2.4.so
libc.so.6 -> libc-2.2.4.so
libcom_err.so.2 -> libcom_err.so.2.0
libcom_err.so.2.0
libdl-2.2.4.so
libdl.so.2 -> libdl-2.2.4.so
libe2p.so.2 -> libe2p.so.2.3
libe2p.so.2.3
libext2fs.so.2 -> libext2fs.so.2.4
libext2fs.so.2.4
libm-2.2.4.so
libm.so.6 -> libm-2.2.4.so
libncurses.so.5 -> libncurses.so.5.2
libncurses.so.5.2
libpthread-0.9.so
libpthread.so.0 -> libpthread-0.9.so
librt-2.2.4.so
librt.so.1 -> librt-2.2.4.so
libuuid.so.1 -> libuuid.so.1.2
libuuid.so.1.2

We can therefore make a system like this in a directory called initrdtree
ls  initrdtree/
bin   dev  etc  initrd  lib linuxrc
mount_cdrom proc root sbin tmp usr var

Where bin a link to sbin, sbin contains the programs and lib the libraries. linuxrc and mount_cdrom are the scripts, and dev has the devices.

This file system must be small enough, ( 6-7Mb ), so that its compressed size, ( 2Mb ), plus a kernel is less than 2.88Mb.

A file system can be made on a loop device, and the directory initrdtree copied to it. The file system is then compressed, and this compressed file is put into a futher file system containing a kernel. This file system must be exactly 2.88Mb, (it is a floppy to the hardware). This is also mounted on a loop device and lilo is run on it. This is then the boot image that mkisofs sees with the -b flag

Here is the script build.sh that builds the iso; note that the system providing the CD image is mounted on cdtree/ The build directory looks like:
bootimagetree # Contains the kernel and the compressed file system
build.sh  # The script
initrdtree    # Contains the boot image system before compression
cdtree    #  The system providing the CD image is mounted here



#! /bin/sh 

if [ -z $TOPDIR ] ; then
  echo "you must define TOPDIR"
  exit
fi

oldpwd=`pwd`

cd $TOPDIR

# we need to set aside a few loop devices. I chose (in reverse order of their appearance)
# -- loop1 for the boot image
# -- loop2 for the ram disk image
# since the loop1 choice is reflected in the lilo.loopfix file, 
# you should not change that (or you need to change the file).
# I had used loop0 first, but I found that this is used by the Samba daemon.
# Also, I assume that the mount points are /mnt/loop{1,2}.
# In principle we could do with one, but it comes in handy to be able to
# leave one mounted, so I took two different ones. 

# we first assume that a proper directory tree of the later ramdisk 
# is in the initrdtree directory. Put everything in there what you think
# will be needed. We assume that this is the case.

echo -n "Creating the Initial Ramdisk image.... "

# first find out how much space we need. 
ISIZE=`du -s -k  $TOPDIR/initrdtree/ | awk '{print $1}'`

# is that true? Anyway, we are smaller than that.
if [ $ISIZE -gt 8192 ]; then
   echo "Initial Ramdisk max size exceeded ($ISIZE, max is 8192KB)"
   exit 1
fi

ISIZE=`expr $ISIZE + 1024`
echo "Initial Ramdisk contents will be $ISIZE KB"

# delete the existing ramdisk image, if there is one
rm -f $TOPDIR/ramdisk

# create a file of 4MB (4096 KB)
# Modified to calculated $ISIZE
dd if=/dev/zero of=$TOPDIR/ramdisk bs=1k count=$ISIZE

# associate it with /dev/loop2
losetup /dev/loop2 $TOPDIR/ramdisk

# make an ext2 filesystem on it. We set the amount of unused space to 0%
# and turn down the number of inodes to save space
#mkfs  -t ext2 -i 16384 -m 0 /dev/loop2
#  A plain mke2fs works better
mke2fs  /dev/loop2

# we mount it...
mount /dev/loop2 /mnt/loop2 

# ... and delete the lost+found directory 
rm -rf /mnt/loop2/lost+found 

# then we copy the contents of our initrdtree to this filesystem
cp -dpR $TOPDIR/initrdtree/* /mnt/loop2/

# and unmount and divorce /dev/loop2
umount /mnt/loop2
losetup -d /dev/loop2 

echo "done"

# Now we have the image of the ramdisk in $TOPDIR/ramdisk. We
# compress this one and write the compressed image to the boot tree:

echo -n "Compressing the Ramdisk image.... "

# delete any existing one
rm -f $TOPDIR/bootimagetree/initrd.img

# and gzip our ramdisk image and put it in the right place.
gzip -9 -c $TOPDIR/ramdisk > $TOPDIR/bootimagetree/initrd.img

# we are done with the uncompresses ramdisk image, delete it
rm  $TOPDIR/ramdisk

# how much is the contents of the bootimagetree?
ISIZE=`du -s -k  $TOPDIR/bootimagetree/ | awk '{print $1}'`
echo "Boot image size is $ISIZE KB"

echo "done"

# Part II. We work the boot tree (with the image of the ramdisk) now.
# we put that into yet another image which we put on the CD. 
# This image has to be 2.88 MB exactly, because we emulate a 2.88MB floppy.

echo -n "Creating the boot image.... "

# delete any leftover version
rm -f $TOPDIR/cdtree/boot/boot.img

# and make a file of the proper size (this time it's fixed at 2880 KB)
# note that the file gets created already in the right place to be the boot image.
dd if=/dev/zero of=$TOPDIR/cdtree/boot/boot.img bs=1k count=2880

# this one gets associated with loop1 and gets a ext2 file system
losetup /dev/loop1 $TOPDIR/cdtree/boot/boot.img
mkfs -t ext2 /dev/loop1

# mount it...
mount /dev/loop1 /mnt/loop1

# ... and copy the contents of our bootimagetree over
cp -dpR $TOPDIR/bootimagetree/* /mnt/loop1/ 

#and we copy our system's boot loader...
cp /boot/boot.* /mnt/loop1/boot/

# now we calculate the ramdisk size for the lilo.conf
# Hard code the size we want; also see linuxrc for dd's count=
ISIZE=30000
echo "Ram disk size will be $ISIZE KB"
cat > $TOPDIR/lilo.conf <<EOF
disk=/dev/loop1
bios=0x00 # bios ID from drive A, we need that here because lilo
# need to know which boot device to use
sectors=36 # 2.88MB disk geometry
heads=2
cylinders=80
timeout=30
 
 boot=/dev/loop1
 message=/mnt/loop1/boot/boot.msg
 install=/mnt/loop1/boot/boot.b
 map=/mnt/loop1/boot/map
 prompt
 compact
  
 image=/mnt/loop1/boot/vmlinuz
   label=linux
   initrd=/mnt/loop1/initrd.img
   append = "root=/dev/ram init=/linuxrc rw"
   ramdisk=$ISIZE

EOF


# run lilo
/sbin/lilo -C $TOPDIR/lilo.conf

rm -f $TOPDIR/lilo.conf

# unmount and divorce from the loop device
umount /mnt/loop1
losetup -d /dev/loop1 

echo "done"

# note that after running lilo, we cannot mount the image back. That's why 
# we make it a throwaway.

# go to the top directory of the future CD
cd $TOPDIR/cdtree

# and create the CD image
# you can fill in the info below as follows if you like
# -p "preparer id" - that's your email, for example
# -P "publisher_id" - again you
# -A "Application_id"

echo -n "Creating the CD iso image, $TOPDIR/bootcd.iso... "
mkisofs -b boot/boot.img -c boot.catalog \
               -o $TOPDIR/bootcd.iso \
               -J -r -T \
               -p "Your email" \
               -P "Your name" \
               -A "LFS Disk" \
	       .

echo "done"

# go back where we came from
cd $oldpwd

# now we can burn this image to a cd. 

Making the bootable CD

/etc/mtab must be a pointer to /proc/mounts
cd $LFS/etc
ls -l mtab
and if it is a file
mv mtab mtab.bak
ln -s /proc/mounts mtab

You need to replace the $LFS/etc/fstab
cd $LFS/etc
mv fstab fstab.bak
cat > fstab << EOF
/dev/ram                /                       ext2    defaults        0 0
proc                    /proc                   proc    defaults        0 0
swapspace               swap                    swap    defaults        0 0
EOF
You need the script checkcd to replace checkfs and mountfs
cat > $LFS/etc/init.d << EOF
#!/bin/sh
# Begin /etc/init.d/checkcd
#
# Include the functions declared in the /etc/init.d/functions file
#
source /etc/init.d/functions
#
# Activate all the swap partitions declared in the /etc/fstab file
#
echo -n "Activating swap..."
/sbin/swapon -a
evaluate_retval
echo -n "Remounting root file system in read-write mode..."
/bin/mount -n -o remount,rw /
evaluate_retval

echo "Making /tmp writeable"
chmod 1777 /tmp

umount  /initrd/proc
echo "Goodbye to the RAMDISK"
umount /initrd
EOF

cd $LFS/etc/rcS.d
ln -s ../init.d/checkcd  S250checkcd
mv S200checkfs ZZZS200checkfs
mv S300mountfs ZZZS300mountfs
Note that the umount of /initrd/proc, then /initrd releases the boot image, it is no longer needed.

Check that your $LFS/etc/inittab will start up at level 3 Inspect your level 3 start up scripts and disable any that you think are inappropriate
I just have S100sysklog; the ethernet would work; except that no ethernet modules are built into the supplied kernel. Finally you need to tar $LFS/root $LFS/dev and $LFS/etc
cd $LFS
tar cf root.tar root
tar cf dev.tar dev
tar cf etc.tar etc
After this the LFS partition can be restored by reversing the changes; the tar files will be used during the boot.

Building the image

Return to the build directory and set TOPDIR
cd where_ever_it_is/cd_builder
export TOPDIR=`pwd`
The directory cdtree must have the LFS root directory mounted. On my system it is /dev/hdb6 so:
mount  /dev/hdb6  cdtree
If you have a separate partition for usr then mount this too
mount  /dev/hdxx  cdtree/usr
If you have a partition containing source you may also mount this. Mine is /dev/hdb5 so:
mount  /dev/hdb5  cdtree/usr/src
You can now build the image:
bash  build.sh >& Build &
Check the output for any warning or any "file system full". A warning from lilo about lilo.conf not having the correct permissions is normal; the latest lilo will also warn about lba32 and compact. If you have both the LFS and the LFS/usr/src mounted the image in bootcd.iso will be about 500Mb.

Write this to a CD and try the system.

Restoration of the LFS system

There are three major changes to be made before the LFS system will reboot:


$LFS/etc/mtab
$LFS/etc/fstab
$LFS/etc/rcS.d
First the mtab
cd $LFS/etc
rm mtab
cp mtab.bak mtab

Note: If you have lost your mtab, then just delete the symbolic link.
You will get some warnings the first time you reboot, but an mtab will be created.

Then the fstab
cd $LFS/etc
cp fstab  fstab.cd
cp fstab.bak  fstab

And finaly the links
cd $LFS/etc/rcS.d
mv S250checkcd ZZZS250checkcd
mv ZZZS200checkfs S200checkfs
mv ZZZS300mountfs S300mountfs


Your LFS system should now boot; enabling you to change and tune it. Then add more software before building a better CD.

Some uses for the boot CD

Broken files


Any partition can be mounted; files edited or replaced by those on the CD.

Mending a broken lilo


Suppose that the machine just does LILILILI on boot up.
Boot from the CD and mount your root partition to /disk; if your not sure which partition; then test the partitions listed in /proc/partition until you get the right one.
Once you have the broken root partition mounted do:
chroot /disk
cd /etc
vi lilo.conf and fix what is wrong
then
lilo -C lilo.conf

Exit (from the chroot), unmount the disk and reboot.