Creating a FreeBSD boot CD

Preamble

This describes what I think is the simplest way to build a bootable CD from a FreeBSD system. It was originally written as an amendment to the FreeBSD handbook in April 2003, but I was unable to submit it (all my mail to freebsd.org bounces), so it's ended up languishing. It seems like the handbook has been reorganised in the mean time. If anyone from the doc project sees this and would like the marked-up version, send me a note from an address that's not at freebsd.org, and I'll send you a fragment of a chapters.xml file.

Overview

To create a bootable CD, you copy a subset of your system to a new directory tree, customize it, then create an ISO image using sysutils/mkisofs. You need to include at least these files and directory trees:

You should also include as much of /usr as you can fit. In general, this means everything except /usr/local and /usr/home, plus any programs you need from those trees. Also make sure you create any directories you might want to use for mount points.

Use the tar command to copy directory trees, and be careful to copy all directories which reside on a single disk with the same command. Also, set umask to 0 before performing the copy. Following this advice ensures file ownership, permissions, and links will be preserved. For instance

umask 0
cd /
mkdir -p tmp/myboot/tmp
chmod 777 tmp/myboot/tmp
tar -cf - .[cp]* [b-tv]* usr/[A-gims]* usr/li* | (cd /tmp/myboot; tar -xvf -)

I say it's easier to copy the directory tree, but you can also create a list of files and include it using the -path-list option, usually in conjunction with -exclude-list and -graft-points so that you can customize the boot scripts.

How to customize the directory tree depends on the purpose of the CD. If the CD is to act as a glorified master boot record, handing control to a system which is installed on a system disk, you need to change boot/loader.conf to tell it where the root disk is. Add this

kernel="disk2s2a:/kernel"
vfs.root.mountfrom="ufs:/dev/ad0s2a"
replacing ad0s2a and disk2s2awith the actual device name for your root disk and the addess returned by the loader's lsdev command. In this case, you need include only the /boot directory on the CD. To boot from the CD but load the root disk from some other device, don't set the kernel name, and include /kernel and /modules on the CD.

Customizing a Boot CD

If you want to run the system from the CD, you need to make more extensive changes. First, -C must be passed to the kernel. Add

kernel_options="-C"
to boot/loader.conf. Next, you need to arrange for /tmp, /dev, and /var to be writable, which can be done by creating memory disks.

Some of the details are specific to version 4 of FreeBSD.

  1. Give these commands from the root of your copied system

    rm -rf var/tmp/* var/log/* tmp/*
    tar -zcvf boot/devvar.tar.gz dev var
    rm -rf var/*

  2. Change etc/fstab to be similar to this

    proc			/proc		procfs	rw		0	0
    swap			/tmp		mfs	rw,-s40000	0	0
    swap			/var		mfs	rw,-s30000	0	0
    swap			/dev		mfs	rw,-s3000,-i256	0	0
    /dev/acd0c		/		cd9660	ro,noauto	0	1

  3. Add these lines to etc/rc.conf

    root_rw_mount="NO"
    diskless_mount="/etc/rc.cdboot"

  4. And finally create etc/rc.cdboot, which should mount and populate the memory disks.

    mount -a -t nonfs
    cd /
    umask 0
    tar -zxf boot/devvar.tar.gz 

Given that /tmp/myboot holds a bootable FreeBSD system as described above, you could produce the image of an ISO 9660 file system in /tmp/bootable.iso like so:

mkisofs -U -R -b boot/cdboot -no-emul-boot -c boot/boot.catalog -o /tmp/bootable.iso /tmp/myboot

If you want to create a more sophisticated bootable CD, investigate the LiveCD project. It has provisions for creating persistent writable mount points from local disks which do not have FreeBSD installed.