Showing posts with label grub. Show all posts
Showing posts with label grub. Show all posts

Aug 6, 2014

Booting FreeBSD with the RAM disk as root file system from GRUB 2

Recently I needed to boot FreeBSD on our development laptop that normally boots from the network using PXEGRUB. Traditionally the kernel is loaded using PXE and then mounts a NFS root file system. In my case I didn't have any ready NFS shares, and I didn't really want to setup any -- after all, I needed the running FreeBSD system to do a couple of quick tests.

In Linux world, there is a thing called initrd -- a small (or, well, sometimes not so small) disk image that is loaded into memory and that is used by the kernel to setup a minimal environment. Loading initrd is implemented in GRUB using the "initrd" command.

*BSD kernels may be booted using the GRUB as well. Additionally, there are a couple of extra commands for dealing with *BSD modules and ramdisks, for example kopenbsd_ramdisk command. As the name suggests, it loads an OpenBSD ramdisk into the memory so the kernel is able to mount it after boot.

There is no such command for the FreeBSD kernel, which made me a bit sad.
But after looking through the source code of GRUB and FreeBSD kernel, I have found a way to do it without any modifications of either kernel or GRUB.
There is an undocumented parameter for the kfreebsd_module command, which allows to specify the type of an object being loaded as a module. Its name is, unsurprisingly, "type" :-) GRUB adjusts the type attribute of the loaded object so that kernel is able to initialize the memory disk. Thanks to the flexible FreeBSD module loading system, no special GRUB code is needed to load the FreeBSD disk image.

So the configuration entry for loading the FreeBSD kernel with the ramdisk from some location is:

menuentry "FreeBSD" {
        kfreebsd $rootloc/fbsd-kernel
        kfreebsd_module $rootloc/fbsd-rd.img type=mfs_root
}
Of course the GRUB should be able to load "bsd" GRUB module (or have it statically compiled in), otherwise kfreebsd* commands won't be available.

fbsd-rd.img is a normal disk image with an UFS file system on it.
If $rootloc is somewhere in the network (for example, we load the binaries using GRUB HTTP loader), you can compress the image using gzip, since GRUB automatically detects compressed data and uncompresses it before loading into the memory. I use a compressed FreeBSD 10/amd64 memstick image that can be downloaded from the FreeBSD website.