How To Quickly Make A Bootable USB Stick With FreeBSD

June 16, 2017 - Computer

Install FreeBSD, or use an existing FreeBSD installation, and follow these steps:

1) First, you need to prepare and format your USB stick:

fdisk -BI /dev/da0
bsdlabel -B -w da0s1
newfs -U -O1 /dev/da0s1a
boot0cfg -v -B da0
(“-U -O1” [“O” like in Olympus, not zero] is for UFS1 which provides much faster copying than UFS2; if you decide for UFS2, type “-U -O2” – but expect that the copying will be slower)

2) Then mount it: mount /dev/da0s1a /usb
3) Copy all directories (FreeBSD) to the stick
4) After copying, modify the /usb/boot/loader.conf (explained below)
5) In the /boot directory on your USB stick you must have MFS (Memory File System – mfsroot.gz), which you will make (instructions are below)
6) Modify your /etc/fstab in MFS and put the following line (only) there:
/dev/md0 / ufs rw 0 0
7) After you boot your computer with the stick, you will be in the MFS environment from which you will mount your USB stick with mount_nullfs (described below)

Modification of /boot/loader.conf on your USB stick

You must have the following lines in your /boot/loader.conf (some lines are optional):


# Additional filesystem drivers


Making your own MFS

FreeBSD, after the kernel boots, can use the root file system in memory (mfsroot_load=”YES” command in /boot/loader.conf will do the trick). To build such a memory file system, type the command:
dd if = /dev/zero of = mfsroot bs = 1024k count = 42

The mfsroot file of about 40 MB in size will be created. You need to format it, mount it and copy the most important files into it from your FreeBSD system (/bin, /sbin, /etc, /root….):

mdconfig -a -f mfsroot md0
newfs /dev/md0
mount /dev/md0 /mnt

Once copied, you must unmount it and gzip it: gzip mfsroot

Optionally, you can chroot it to see if everything works, then copy the mfsroot.gz to /usb/boot onto your USB flash drive (or disk). If you think it may be a problem to pick the most important files for your MFS (from your FreeBSD installation), search for mfsbsd in Google and either use its toolset or the MFS image alone (contained in the downloadable ISO of mfsbsd).

After booting from the USB stick (you will jump into MFS), you must mount the physical USB stick:

/sbin/mount -o ro /dev/da0s1a /usb
/sbin/mount_nullfs /usb/boot /boot
/sbin/mount_nullfs /usb/usr /usr

The above commands will help you use the big /usr directory on your USB stick instead of the /usr dir in MFS. mount_nullfs /usb/boot /boot is optional, as in your MFS/boot directory only the following files are needed for the little MFS to boot (/boot/kernel directory in MFS): geom_label.ko, geom_uzip.ko, zlib.ko a their debug symbols (zlib.ko.symbols, etc.). By mounting the /usb/boot dir via mount_nullfs into the /boot directory in your MFS you will be able to load kernel modules.

Source by Juraj Sipos