4. Building a File System from Scratch

Because ATA compatible compact-flash cards are seen by FreeBSD as normal IDE hard drives, you could theoretically install FreeBSD from the network using the kern and mfsroot floppies or from a CD.

However, even a small installation of FreeBSD using normal installation procedures can produce a system in size of greater than 200 megabytes. Because most people will be using smaller flash memory devices (128 megabytes is considered fairly large - 32 or even 16 megabytes is common) an installation using normal mechanisms is not possible—there is simply not enough disk space for even the smallest of conventional installations.

The easiest way to overcome this space limitation is to install FreeBSD using conventional means to a normal hard disk. After the installation is complete, pare down the operating system to a size that will fit onto your flash media, then tar the entire filesystem. The following steps will guide you through the process of preparing a piece of flash memory for your tarred filesystem. Remember, because a normal installation is not being performed, operations such as partitioning, labeling, file-system creation, etc. need to be performed by hand. In addition to the kern and mfsroot floppy disks, you will also need to use the fixit floppy.

  1. Partitioning Your Flash Media Device

    After booting with the kern and mfsroot floppies, choose custom from the installation menu. In the custom installation menu, choose partition. In the partition menu, you should delete all existing partitions using d. After deleting all existing partitions, create a partition using c and accept the default value for the size of the partition. When asked for the type of the partition, make sure the value is set to 165. Now write this partition table to the disk by pressing w (this is a hidden option on this screen). If you are using an ATA compatible compact flash card, you should choose the FreeBSD Boot Manager. Now press q to quit the partition menu. You will be shown the boot manager menu once more - repeat the choice you made earlier.

  2. Creating Filesystems on Your Flash Memory Device

    Exit the custom installation menu, and from the main installation menu choose the fixit option. After entering the fixit environment, enter the following command:

    # disklabel -e /dev/ad0c

    At this point you will have entered the vi editor under the auspices of the disklabel command. Next, you need to add an a: line at the end of the file. This a: line should look like:

    a:      123456  0       4.2BSD  0       0

    Where 123456 is a number that is exactly the same as the number in the existing c: entry for size. Basically you are duplicating the existing c: line as an a: line, making sure that fstype is 4.2BSD. Save the file and exit.

    # disklabel -B -r /dev/ad0c
    # newfs /dev/ad0a
  3. Placing Your Filesystem on the Flash Media

    Mount the newly prepared flash media:

    # mount /dev/ad0a /flash

    Bring this machine up on the network so we may transfer our tar file and explode it onto our flash media filesystem. One example of how to do this is:

    # ifconfig xl0 192.168.0.10 netmask 255.255.255.0
    # route add default 192.168.0.1

    Now that the machine is on the network, transfer your tar file. You may be faced with a bit of a dilemma at this point - if your flash memory part is 128 megabytes, for instance, and your tar file is larger than 64 megabytes, you cannot have your tar file on the flash media at the same time as you explode it - you will run out of space. One solution to this problem, if you are using FTP, is to untar the file while it is transferred over FTP. If you perform your transfer in this manner, you will never have the tar file and the tar contents on your disk at the same time:

    ftp> get tarfile.tar "| tar xvf -"

    If your tarfile is gzipped, you can accomplish this as well:

    ftp> get tarfile.tar "| zcat | tar xvf -"

    After the contents of your tarred filesystem are on your flash memory filesystem, you can unmount the flash memory and reboot:

    # cd /
    # umount /flash
    # exit

    Assuming that you configured your filesystem correctly when it was built on the normal hard disk (with your filesystems mounted read-only, and with the necessary options compiled into the kernel) you should now be successfully booting your FreeBSD embedded system.

All FreeBSD documents are available for download at http://ftp.FreeBSD.org/pub/FreeBSD/doc/

Questions that are not answered by the documentation may be sent to <[email protected]>.
Send questions about this document to <[email protected]>.