Logging my quest into the NetBSD universe.

Tuesday, August 3, 2010

Dual booting NetBSD with Linux using Grub

So far I've been playing with NetBSD and virtualbox, this is nice as a starting point, but next to this I also like something running on the bare metal.So I picked a desktop of mine which had a Debian stable installed and turned this into a dual boot system. Doing this is something very simple.

The first thing you need to do is make sure you start off the right foot, this means that in this case when I partitioned the disk and installed Debian on it, I already foresaw that I would install NetBSD on it, so I left some spare space in an empty partition. In the NetBSD installer I let NetBSD do its thing (create the BSD disklabel) in that partition.

And once you're at this point you're already at one third of the effort. When NetBSD prompts you that it did not find a valid bootloader in the MBR and asks you to install one for you, just say no in this case GRUB (which _is_ a valid bootloader) is already installed and we will use GRUB to boot into NetBSD.

So far so good, the installer will complete and when the system is done and reboots you should see GRUB and at this point you should see no trace of NetBSD whatsoever, don't panic, this is what we expect and what we will repair now.


For grub 0.9x you can access a command line by pressing 'c', from there you can enter some commands at the "grub>" prompt. If you type here

grub> rootnoverify (hd0,1)
grub> chainloader +1
grub> boot

Et voila, you should see the NetBSD loader appearing. Note that this change is not permanent and you will need to type this each time you want to boot into NetBSD (which is not part of the plan) hence we will edit the grub configuration file, typically this lives in /boot/grub/menu.lst  (but your experience may vary, sometimes this is called grub.conf as well), and also beware that at least a part of this file is typically auto generated by your distribution (typically when the kernel is upgraded, e.g. based upon rules in /etc/grub.d/* ), but in this file you can add (append at the bottom is typically a good idea) something like:

title NetBSD
rootnoverify (hd0,1)
chainloader +1

And the next time grub will show you to option to boot NetBSD from the list of GRUB options. For those not familiar with GRUB's notation of disks and partition (hd0,1) means the second partition of the first hard disk (all grown up people start counting at zero ;-) ).

For grub 2 (or 1.xx) the steps are almost similar, one important difference however is that they start counting disks at 0 (as with the old one) but partitions start to count at 1. Meaning that my earlier example from the commandline would look like:


grub> set root=(hd0,2)
grub> chainloader +1
grub> boot

Or you can extend the configuration file (by editing /boot/grub/grub.cfg directly, or by editing /etc/grub.d/40_custom and running something like update-grub2, the latter probably being the recommended way). In my case this last file contains:

#!/bin/sh
exec tail -n +3 $0
# This file is an example on how to add custom entries
menuentry "NetBSD" {
     set root=(hd0,2)
     chainloader +1
}

Or just add the last three lines to the grub.cfg file directly if you don't want to use the upgrade-grub2 mechanism.