Building the binary image
The final step in the process is to take the miniBSD system you created and actually assemble it into a single, binary file which you can then write to a CF memory card, or hard-drive.

Before you get started on this step, if you know you are going to need SSH, then you should read the Adding SSH part of the Expanding miniBSD 6.x section and do those steps now, before you build the binary. I left SSH out of the default setup because some people might not have any use for it. And if you don't need it, it's just a potential security hole.

Right up front, let me point you to my build-image script. Using my script will automate everything in this section. You should still read through this page in its entirety so you understand what is happening. But in the end, you're probably going to want to script this process because it gets monotonous after you've manually gone through it for the umpteenth time while trying to perfect your miniBSD image. If you want to use my script, type the following:

cd /root
chmod 0700 /root/

If you run my script, the output should look like this once you have the script variables set properly:

MiniBSD ~ # ./ 0.1.0

Generating an empty binary image file...[ OK ]
Linking binary image to device file.....[ OK ]
Creating MBR on binary image............[ OK ]
Partitioning binary image...............[ OK ]
Formatting the binary image.............[ OK ]
Mounting temporary mount point..........[ OK ]
Writing miniBSD to the binary image.....[ OK ]
Unmounting temporary mount point........[ OK ]
Unlinking binary image..................[ OK ]
Compressing the binary image............[ OK ]
Checking file integrity.................[ OK ]
Generating MD5 SUM of binary image......[ OK ]

If you want to do it step by step (and I recommend that you do at least one time so you understand it), you need to read the rest of this page very carefully and follow the steps as outlined.

Generating the flash image

Although we could use the CF card like a hard disk, bsdlabel and newfs it and explode our tar archive directly on it, I feel it's easier and faster to do that task in a disk image and dd that directly onto the flash card - besides, it prolongs the life of the CF card by writing every sector only once.

We'll use vnconfig to get a virtual disk that we can bsdlabel. You will have to know the size of your flash card in sectors (512 byte units), but you cannot figure this out without actually connecting the card to your FreeBSD Host Machine (see the beginning of the next section for information on how to do this). Once you have it connected, type the following command to find out the number of sectors:

bsdlabel -Awn ad[n] auto | grep sectors/unit

...of course substituting the proper device number of your flash card for [n].

Hint: if bsdlabel reports errors like "No space left on device", type the following command first:

dd if=/dev/zero of=/dev/ad[n] bs=1k count=20

This will erase any partition information that may already be present on the CF card.

Now we'll create a disk image of the same size, initially filled with zeroes. Type the following:

dd if=/dev/zero of=/usr/minibsd-disk.bin bs=512 count=[number of sectors on your flash card]

Let's use this disk image file as a vn device so we can bsdlabel it. Type the following:

mdconfig -a -t vnode -u 0 -f /usr/minibsd-disk.bin

Our new virtual disk has to be partitioned and the file system created. Type the following:

bsdlabel -Bw md0 auto
bsdlabel -e md0

You'll be dropped into your editor. Find the line that starts with c: and duplicate it, changing the c: to a: and the fstype to 4.2BSD. This is our root partition, which will span over the whole slice. If you want a second partition (e.g. for configuration information), reduce the size of the a: slice by the amount of space you want for your second partition, then make another line starting with e:, an offset equal to the size of a:, and a size equal to what you subtracted from a:. Save. Note that we don't have a swap partition - there's nowhere to put it. You can't put a swap partition on your flash card - it's way too slow and would soon ruin your card. So you better make sure that you've got enough memory for your applications!

Hint: After you have saved the bsdlabel partitions, you should create an ASCII file containing the table information so you can easily import that ASCII file in the future. This is essential for scripting the image building process. Type the following:

bsdlabel md0 > /root/minibsd-disklabel

Now the next time you get to the step mentioned earlier where you need to run "bsdlabel -e md0", you can instead type the following:

bsdlabel -R md0 /root/minibsd-disklabel

Now let's newfs and mount our partition. It's worth mentioning that newfs will complain that /etc/fstab doesn't exist because you don't have an /etc/fstab in your FreeBSD jail. As far as I know, this warning is harmless, but never-the-less, we can suppress it by creating an empty file before running newfs. Type the following:

touch /etc/fstab
newfs -b 8192 -f 1024 -U /dev/md0a
mount /dev/md0a /mnt

(if you made a second partition earlier on, don't forget to newfs that one, too). So we got our virtual disk mounted on /mnt, and we're now ready to copy the miniBSD files onto it. Type the following:

(cd /usr/minibsd ; tar cPf - .) | (cd /mnt ; tar xf - );

That's it! Let's clean up. Type the following:

cd /
umount /mnt
mdconfig -d -u 0

(Note: if, after completing the next section, you find that you can't boot off the CF card, there might be a C/H/S mismatch problem. See this message from Jeremy Cooper for details and a solution.)

Depending on the size of your binary image, you may want to compress it if you are going to transfer the image across a network, or if you are going to store it. Just to give an example, I have a 128MB CF card which means my binary image ends up being 128MB also. But my actual miniBSD system currently weighs in at 15MB. That means there is about 113MB of 0's at the end of my binary image file. When I compress my binary image file, it goes from 128MB all the way down to 7MB! A 7MB file is a lot easier to transfer via the Internet or even a LAN. Just don't forget to uncompress it before you write it to your CF card or you'll end up with an unbootable system.

That's it. Now continue to the next section.