BootCD-HDimage.txt Submission for initial review (needs some work)

Jason Cooper jcooper at
Wed Feb 26 08:20:33 PST 2003

Okay, here it is.  I'll readily admit, this needs some work and verification
before it's committed (if it's committed).  This is definitely fbtsomp.  

I'll be running through the boot image part again to double check it.  I just
added the correction about using fdisk.  Read the "hard disk image" reference to
see what I'm trying to do.  I eventually want to encrypt the CD, which means
losetup and some modules will be in initrd-rootfs.gz.  This could take it over
2.88MB, hence the whole hard disk image track. 

If you have experience with any aspect of this, please give me your two cents, as
this is a learning experience for me.  Any and all input would be appreciated.


-------------- next part --------------
TITLE:			Creating an LFS Boot CD 
LFS VERSION:	LFS 4.0 (created from upgraded 3.0 host)
AUTHOR:			Jason Cooper <jcooper at>


This was a learning exercise for me.  Pratical applications are possible, but
there are most likely easier ways to do it.  Two constraints led to this 
approach.  I don't have a floppy drive, and I don't want to reboot my 
primary system twenty plus times since it doubles as my mail server among 
other things.

With that being said, if you want to make a boot CD of LFS and you have no 
floppy drive, this will show you how to take advantage of a few quirks in the 
"El Torito" specifications.  Namely, it doesn't have to be a floppy-sized image 
for the boot image. 


The Linux From Scratch Boot-CD hint
		by: Nicholas Dille <webmaster at>

Infomation on Making a Bootable Linux CD with a Hard Disk Image
		by: <tmelcon at>

The Linux Boot Disk HOWTO
		by: Tom Fawcett <Bootdisk-HOWTO at>

The Linux From Scratch cryptoapi hint
		by: Christophe Devine <devine at>

Making Bootable Linux CDs
		by: Paul ? [Unable to locate author info]



	0.) Changelog

	1.) Install LFS 4.0 to a fresh partition

	2.) Check requirements
	3.) Make the initrd root image
	4.) Make the boot image
	5.) Burn it to CD
	6.) To Be Done (2 critical items)

0.) Changelog

		26Feb2003 - Hint written.

		13Feb2003 - Project started.

1.) Install LFS 4.0 to a fresh partition 

LFS should be installed on a single partition, in this case '/dev/lfs-cd'.  This
will be the image written to the CD.

2.) Check requirements

You must have loopback device support enabled in your kernel.  Check the 
cryptoapi.txt hint for how to do that.

Use lilo.  I have no clue how this would work with grub.  Definitely don't use
rdev to put the kernel in the boot sector, it doesn't work with CD booting 
because the kernel bypasses the BIOS.  And it's the BIOS that allows booting
from CD by emulating the CD as a floppy.  Lilo and Grub both go through the 
BIOS which is why they work.

3.) Make the root image for the initial ramdisk
Make a 4MB file.

# dd if=/dev/zero of=/tmp/root.image bs=1M count=4

Loop the file.

# losetup /dev/loop2 /tmp/root.image

Put a filesystem on the image.  '-m 0' reserves no space for root, '-N 2000'
gives us plenty of inodes.

# mke2fs -m 0 -N 2000 /dev/loop2

Mount the image.  Note 'root-image' is a directory, while 'root.image' is the

# mount /dev/loop2 /mnt/root-image

Create a basic directory structure.

# mkdir -p /mnt/root-image/{bin,dev,etc,lib,mnt,proc,sbin,usr}

Copy the device files over.

# cp -dpR /dev/fd[01]* /mnt/root-image/dev/
# cp -dpR /dev/tty[0-6] /mnt/root-image/dev/
# cp -dpR /dev/hd[abcd]* /mnt/root-image/dev/
# cp -dpR /dev/{console,kmem,mem,null,ram0} /mnt/root-image/dev/

Create the config files.

# cat >/mnt/root-image/etc/rc <<EOF
  /bin/mount -av
# cat >/mnt/root-image/etc/fstab <<EOF
  /dev/ram0	/		ext2	defaults
  /dev/fd0	/		ext2	defaults
  /proc		/proc	proc	defaults
# cat >/mnt/root-image/etc/inittab <<EOF
  1:2345:respawn:/sbin/agetty 9600 tty1
  2:23:respawn:/sbin/agetty 9600 tty2
Copy over some etc files.  [Edit group, passwd, shadow.]

# cp /etc/{group,passwd,shadow,,,nsswitch.conf,profile} \

Copy some binaries over.

# cp /bin/{bash,login,mount} /mnt/root-image/bin/
# ln -s /mnt/root-image/bin/bash /mnt/root-image/bin/sh
# cp /sbin/{agetty,init} /mnt/root-image/sbin/

Copy the libraries over.  I use the first command to find out what libraries
I need, then copy the symlinks and the libraries over.  [I think just cp'ing
the symlinks over may be easier, since this is only for initrd.]

# ldd /mnt/root-image/{bin/*,sbin/*}

For each symlink listed, do this.  Make sure to get the actual library also.

# ls -l /lib/<symlink>
# cp -dpR /lib/{<symlink>,<library>} /mnt/root-image/lib/

Create var. [These will end up symlinks to a ramdisk later]

# mkdir -p /mnt/root-image/var/{log,run}

Update the stuff.

# /mnt/lfs-cd/sbin/ldconfig -r /mnt/root-image/ 

Unmount and create the gzip file.

# cd /
# umount /dev/loop2
# dd if=/dev/loop2 bs=1k | gzip -v9 >/mnt/lfs-cd/boot/initrd-rootfs.gz
# losetup -d /dev/loop2

4.) Create the boot harddisk image

Make a 4MB file.

# dd if=/dev/zero of=/tmp/boot.hd.image bs=1M count=4

Loop the file.

# losetup /dev/loop1 /tmp/boot.hd.image

Force the image to emulate a harddisk.  [Fun with fdisk]

# fdisk /dev/loop1
	> x 	#for expert mode
	> h		#change number of heads
	> 64
	> s		#change number of sectors/track
	> 32
	> c		#change number of cylinders (= to MB of image)
	> 4
	> r		#return to main menu
	> n		#add a new partition
	> 1
	> a		#make partition bootable
	> 1
	> w		#write table to disk and exit

Put a filesystem on the disk. [This may have to be /dev/loop1p1]

# mke2fs -m 0 /dev/loop1

Mount the image. [May have to be /dev/loop1p1]

# mount /dev/loop1 /mnt/boot-image

Put basic directories on the disk.

# mkdir -p /mnt/boot-image/{boot,etc,dev}

Copy the boot files, kernel over.

# cp /mnt/lfs-cd/boot/{boot.b,map,vmlinuz,initrd-rootfs.gz} \

Add the lilo.conf file.  **Caution** I'm not responsible if you fubar your
MBR on your real harddisk. Read this completely and understand it fully
before you do it.  Each case will be unique.  You have been warned.

NOTE: This lilo.conf is in the image, and should only be used when 'lilo -v -r 
<path-to-image>' is used.

# cat >/mnt/boot-image/etc/lilo.conf <<EOF
  #Current info about the image
  #This line varies, equal to number of megs of image.

  #This should match disk= line above.
  #This path is *relative* to <path-to-image> when
  #'lilo -v -r <path-to-image>' is executed.
  #This specifies the root device after exiting initrd.  Can be
  #changed while in initrd.
  #This path is *relative* to <path-to-image> when
  #'lilo -v -r <path-to-image>' is executed.
  #This path is *relative* to <path-to-image> when
  #'lilo -v -r <path-to-image>' is executed.
	  append="ramdisk_size=4096" #May not be necessary

  #End lilo.conf  
Now, execute lilo like this.  I had an older one on my host, so I specified
which lilo to run.

# /mnt/lfs-cd/sbin/lilo -v -r /mnt/boot-image/

Unmount the image.

# cd /
# umount /dev/loop1
# losetup -d /dev/loop1

Copy the image over.

# cp /tmp/boot.hd.image /mnt/lfs-cd/boot/

5.) Burn it to CD

Make the iso and burn to cd.  'boot/*' pathnames in the mkisofs command are
relative to the first pathname given.  In this case, '/mnt/lfs-cd'.  Also,
your device will probably be different.

# mkisofs -rlDJLV "LFS" /mnt/lfs-cd -hard-disk-boot -b boot/boot.hd.image \
	-c boot/catalog | cdrecord -v -eject dev=1,0,0 speed=4 -data -

That should be it.  Please note this is definitely a work in progress.  
Constructive criticism is welcome, solutions are encouraged.

6.) To Be Done

	a.) Must locate the cdrom (search /proc), set the symlink, and mount the
		CD as / while running in ramdisk.  Then pivot_root the ramdisk to 
	b.) Symlink /var and /tmp to /initrd/{var,tmp} so the system can write to
		those directories.
	c.) Add xfree86-svga, fvwm2 to the CD.
	d.) ?

More information about the hints mailing list