2015-05-21

Bootstrapping the OpenBSD Bootloader with GRUB 2.0

This posting is mostly for posterity and to help out others should they be interested in dual-booting
OpenBSD by way of GRUB.

I recently purchased an Intel NUC NUC5i7RYH as a lightweight development machine. I do most of my development and work in OpenBSD, but occasionally I need to use something that unfortunately won't run on OpenBSD, so I have placed Linux as a secondary boot option on machine. My understanding of the OpenBSD bootloader, in spite of how simple it is, is rather limited.

So how does one get GRUB 2.0 to boot OpenBSD? Let's first look at my desk setup.

Command (m for help): p

Disk /dev/sda: 1000.2 GB, 1000204886016 bytes, 1953525168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x000868f2

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048  1292916735   646457344   a6  OpenBSD
/dev/sda2   *  1292916736  1953519615   330301440   83  Linux

I have one large disk, which I have done lazy job of partitioning. The vast majority of the disk is consumed by the first partition sda1; this is where OpenBSD resides. Linux as is quite obvious is on sda2. This disk is setup with the traditional master boot record partition table, so let's talk about the glue.


GRUB is installed on the master boot record, which resides at the beginning of the sda block device. The OpenBSD boot loader is installed at the beginning of sda1.


Let's turn to the GRUB configuration now. My Linux distribution is CentOS, so that may result in some variance when applying this technique to another distribution. The file /etc/grub.d/40_custom is the standard location for for installing site-specific modifications to GRUB:


$ cat /etc/grub.d/40_custom 
#!/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.

menuentry "OpenBSD" {
  set root=(hd0,msdos1)
  kopenbsd /bsd -r sd0a
}

Unlike previous versions of GRUB, using the chainloader no longer works. It turns out GRUB 2.0 offers some ready-made facilities for certain runtime types, like the BSD family. The crux of this is the kopenbsd directive, which tells GRUB to directly load the kernel image for my OpenBSD installation. You can also use this facility, I believe, to bootstrap the OpenBSD installation ramdisk. After this modification is made, the GRUB configuration is rebuilt using some run-parts incantation (as I get older, I like these things less and less):


# grub2-mkconfig -o /boot/grub2/grub.cfg 

Et voila!


follow us in feedly
 

None of the content contained herein represents the views of my employer nor should it be construed in such a manner. . All content is copyright Matt T. Proud and may not be reproduced in whole or part without expressed permission.