How to create a bootable USB flash drive under Mac OS X, in the special case of a NetBSD installation disc

Dear Reader,

you probably noticed that this is a rather silly title for a posting. You could argue also, that creating a blog with the sole purpose of explaining how to create a bootable usb flash drive under mac os x is rather silly, too. W E L L   Y O U   S U R E L Y   W O U L D N ‘ T   I F   Y O U   W A S T E D   T H R E E   F * * * I N G   D A Y S   T R Y I N G   T O   F I G U R E   O U T   H O W ! ! !

Anyway, so really how to create a bootable USB stick now, under Mac OS X ? I shall describe one way to do this, for the example of a NetBSD installation. But first some

0. Preliminaries

What happens when you start a typical PC, is that the BIOS will – after having gone through some testing/self-check stuff we don’t care about – try to start the OS. It will look for existing bootable drives, which are e.g. hard drives, floppy disks, CDs and well USB flash drives. If there is more than one bootable drive, it will choose the one with the highest priority. If this is a hard drive, it will expect that the first 512 bytes of this disk constitute a valid MBR = Master Boot Record (cf. http://en.wikipedia.org/wiki/Master_Boot_Record ), and will start executing the boot manager code in this MBR. This boot manager will usually try to boot from the partition, which is flagged as bootable. The bootable partition’s analog of the MBR is the VBR = Volume Boot Record, whose format will depend on the filesystem type of that partition. The MBR boot code will then give control to the executable code in the VBR, which will then start the OS.

If you are booting from a floppy, there are no partitions, just one volume. Hence there is no MBR, and the BIOS will directly try to execute the VBR boot loader. For CDs the situation is different, but basically the same. How a BIOS boots from a CD is specified in the “El Torito”-Standard.

USB flash drives are treated like normal hard drives, so we have to have MBR and VBR.

1. Creating a NetBSD installation disc on a USB flash drive

If you insert a USB flash drive into your Mac, the OS will associate to it a device file /dev/disk<n>, where n is some natural number. The easiest way to find out which device file it assigns to, is to type

mount

in command line, while the stick is mounted (i.e. appears under /Volumes/). For sake of concreteness let’s assume it is bound to /dev/disk2. The next thing you have to do, is set up the MBR and create suitable partitions on your stick. There are at least two ways to do this under Mac OS X. The first way is to use the Mac OS X Disk Utility. Since this tool is rather user-friendly and self-explaining, I won’t describe it here. Instead I will to describe setting up your stick with “fdisk”. Before you start, make sure that you don’t have any volumes of your stick mounted using

diskutil umountDisk /dev/disk2

This should unmount all the volumes on your flash drive. Next type e.g.

fdisk -a dos -c 1024 -h 255 -s 63 -b 512 -i /dev/disk2

This will overwrite any MBR present (-i) and create a single FAT partition (-a dos) on your drive. It will use some default boot strapping. If you want to use your own MBR boot loader, you have to specify an MBR template for fdisk to use via “-f /path-to-a-mbr-template-file”). If you choose to do so, you will have to append the “-f /path-to….” to every fdisk command below. The option “-c 1024 -h 255 -s 63” specifies the “BIOS geometry” fdisk should use. Note that the numbers given in the above example don’t matter actually. We have to give these options to fdisk, because we have also given “-b 512”, whichs sets the sector size to 512 bytes per sector (fdisk man says “block size”, but it’s really sector size). This is a crucial point. If you leave out “-b 512”, our stick will not be bootable.

Now to mark the partition as bootable, we have to use fdisk in interactive mode. But before we do this, we do

diskutil umountDisk /dev/disk2

again to make sure Mac OS X didn’t re-mount it meanwhile (which it usually does, everytime you mess around with your drive). Then type

fdisk -e /dev/disk2

and then

flag 1

This will flag the partition number 1 as bootable. However, for our purposes the file system type is not the right. To install the NetBSD boot image, we need to have a partition of type “NetBSD” = 0xa9 = 169. There do

edit 1

a9

[enter]

[enter]

[enter]

quit

The MBR part of the USB stick is now correctly set up, and we can copy the boot image onto partition 1. Download a suitable boot image. If you’re planning to install NetBSD on a x86 machine for instance, this would be “/NetBSD-<version>/i386/installation/floppy/boot-big.fs”, on your favourite ftp-server. Now do

diskutil umountDisk /dev/disk2

dd if=/whereever-you-have-download-it-to/boot-big.fs of=/dev/disk2s1

Note that it’s really “disk2s1” and not “disk2”. You don’t want to overwrite your MBR, but rather your boot volume and your VBR. At this point, you should have created a bootable USB installation stick (at least it worked for me). Really the important thing to note here is the “-b 512” option. Otherwise, it’s pretty straight forward.

References:

http://wiki.netbsd.se/How_NetBSD_boots_on_x86

http://en.wikipedia.org/wiki/Master_Boot_Record

http://www.nu2.nu/mkbt/

http://support.microsoft.com/kb/140418