Hacking the FreeBSD Boot CD

From: www.bsdworld.net  read times: 86


Provided by yangyi at 2008-05-15 19:12:05


This is a disection of the FreeBSD BootCD. To learn how it all works and how to modify things. The cd that I'm going to be using is 7.0-RELEASE-i386-bootonly.iso
Topics will be:
1)Opening the iso
2)Reading the boot options
3)Working with mfsroot
4)Customize the boot up
5)Burning a new CD to boot from

1)Opening the iso
Download the iso from ftp://ftp.freebsd.org/pub/FreeBSD/ISO-IMAGES-i386/7.0/7.0-RELEASE-i386-bootonly.iso You can also download more iso's from ftp://ftp.freebsd.org/pub/FreeBSD/.
Next to open the iso to read the contents run:

# mkdir FreeBSD7iso
# mdconfig -a -t vnode -f 7.0-RELEASE-i386-bootonly.iso -u 0
# mount -t cd9660 /dev/md0 FreeBSD7iso


2)Reading the boot options
You will now see the fallowing contents:

# ls -l FreeBSD7iso/
total 6
dr-xr-xr-x 7 root wheel 4096 Aug 17 18:51 boot
-r--r--r-- 1 root wheel 2048 Aug 17 18:54 boot.catalog
-r--r--r-- 1 root wheel 32 Aug 17 18:52 cdrom.inf

The boot dir contains all the boot and CD OS commands. cdrom.inf is the cd version you are looking at for this cd it will read "CD_VERSION = 7.0-CURRENT-200708".
The boot directory will contain all the important files.

laptop# ls -l boot/
total 1826
-r--r--r-- 1 root wheel 7636 Aug 17 01:15 beastie.4th
-r--r--r-- 1 root wheel 8192 Aug 17 01:15 boot
-r--r--r-- 1 root wheel 512 Aug 17 01:15 boot0
-r--r--r-- 1 root wheel 512 Aug 17 01:15 boot0sio
-r--r--r-- 1 root wheel 512 Aug 17 01:15 boot1
-r--r--r-- 1 root wheel 7680 Aug 17 01:15 boot2
-r--r--r-- 1 root wheel 1200 Aug 17 01:15 cdboot
dr-xr-xr-x 2 root wheel 2048 Aug 17 01:15 defaults
-r--r--r-- 1 root wheel 1739 Aug 17 18:51 device.hints
dr-xr-xr-x 2 root wheel 2048 Aug 17 01:10 firmware
-r--r--r-- 1 root wheel 2249 Aug 17 01:15 frames.4th
dr-xr-xr-x 2 root wheel 141312 Aug 17 04:07 kernel
-r-xr-xr-x 1 root wheel 253952 Aug 17 01:15 loader
-r--r--r-- 1 root wheel 7545 Aug 17 01:15 loader.4th
-r--r--r-- 1 root wheel 72 Aug 17 18:51 loader.conf
-r--r--r-- 1 root wheel 15219 Aug 17 01:15 loader.help
-r--r--r-- 1 root wheel 385 Aug 17 01:15 loader.rc
-r--r--r-- 1 root wheel 512 Aug 17 01:15 mbr
-r--r--r-- 1 root wheel 1122279 Aug 17 18:51 mfsroot.gz
dr-xr-xr-x 2 root wheel 2048 Aug 17 01:10 modules
-r--r--r-- 1 root wheel 256000 Aug 17 01:15 pxeboot
-r--r--r-- 1 root wheel 692 Aug 17 01:15 screen.4th
-r--r--r-- 1 root wheel 36435 Aug 17 01:15 support.4th
dr-xr-xr-x 2 root wheel 2048 Aug 17 01:10 zfs

I'm not going to go over all the files here just some of the key files.
The boot config file is loader.rc:

$ cat loader.rc
\ Loader.rc
\ $FreeBSD: src/sys/boot/i386/loader/loader.rc,v 1.4 2005/10/30 05:41:42 scottl Exp $
\
\ Includes additional commands
include /boot/loader.4th
\ Reads and processes loader.conf variables
start
\ Tests for password -- executes autoboot first if a password was defined
check-password
\ Load in the boot menu
include /boot/beastie.4th
\ Start the boot menu
beastie-start

This file explains everything reading top to bottom. The first file load is loader.4th. This file well attach the loader with the CD kernel. It will also load /boot/defaults/loader.conf which we will get to in a min. The /boot/beastie.4th file is what shows you your boot options and the nice beastie logo or FreeBSD words on the screen.

3)Working with mfsroot
To make changes and save them you need to copy the iso contents to another dir since the iso has limited size:

$ df -h
Filesystem Size Used Avail Capacity Mounted on
/dev/ad4s1a 89G 10G 72G 13% /
devfs 1.0K 1.0K 0B 100% /dev
/dev/md0 110M 110M 0B 100% /usr/home/username/FreeBSD7iso
$ mkdir ~/newFreeBSD7
$ cp ~/FreeBSD7iso ~/newFreeBSD7

The mfsroot that is located in /boot/ of the CD iso is the real directory layout for the OS. This file is loaded from: loader.conf

$ cat loader.conf
mfsroot_load="YES"
mfsroot_type="mfs_root"
mfsroot_name="/boot/mfsroot"

To unzip and open the mfsroot do:

# gunzip mfsroot.gz
# mkdir mfsrootOS
# mdconfig -a -t vnode -f ./mfsroot
md1
# mount /dev/md1 mfsrootOS
# ls mfsrootOS/
.snap bin boot dev etc mnt sbin stand var

In the mfsrootOS dir you will see something more recognizable to a normal UNIX/Linux system. We can now edit files and change commands or install shells in the cd OS. With so many options you can do with it I'm not going to go through them to much allowing you to test them up.

4)Customize the boot up
Before we close everything and burn a new cd I will go over some simple changes, first changing the boot up logo by adding:

: fbsdw-logo ( x y -- )
2dup at-xy ." " 1+
2dup at-xy ." BSDWorld" 1+
2dup at-xy ." www.BSDWorld.net" 1+
2dup at-xy ." " 1+
2dup at-xy ." " 1+
2dup at-xy ." Easy personal boot message" 1+
2dup at-xy ." " 1+
2dup at-xy ." " 1+
2dup at-xy ." " 1+
2dup at-xy ." " 1+
2dup at-xy ." " 1+
2dup at-xy ." " 1+
at-xy ." "
;

To boot/beastie.4th where you see the other examples. To edit the loader so your new display is show when you boot edit defaults/loader.conf and change:

loader_logo="fbsdw" # Desired logo: fbsdbw, beastiebw, beastie, none

I used fbsdw since my logo id is: "fbsdw-logo"
To stop the installer program from starting to boot edit the same file defaults/loader.conf changing:

#init_path="/sbin/init:/sbin/oinit:/sbin/init.bak:/rescue/init:/stand/sysinstall"
# Sets the list of init candidates
#init_shell="/bin/sh" # The shell binary used by init(8).

to:

init_path="/sbin/init:/sbin/oinit:/sbin/init.bak:/rescue/init"
# Sets the list of init candidates
init_shell="/bin/sh" # The shell binary used by init(8).

For the sh be sure you have it in the mfsroot if not install it.

5)Burning a new CD to boot from
First close all the md* we opened with:

# umount /dev/md1 /dev/md0

To reserve space on the CD compress mfsroot backup with best:

# gzip -9 mfsroot

Now Create the new BootCD make sure the cd .. to get one up from the new ISO contents dir, just use the fallowing to create the new iso and burn the new iso to a cd (put blank CD in drive):

# mkisofs -R -no-emul-boot -b newFreeBSD7/boot/cdboot -o BSDWworld.iso newFreeBSD7
# burncd -f /dev/acd0 -s 24 data BSDWorld.iso fixate

You can play around with other files to do more and add boot splash images, etc.

Original link: http://www.bsdworld.net/tutorial...