# Booting FreeBSD via GRUB



## balanga (Apr 6, 2017)

After install Grub2, I'm unable to boot FreeBSD. The Grub installation is on an Archlinux partition and boots up OK. I couldn't figure out how to get Grub to automatically add FeeBSD so have been struggling to add it manually using whatever guides I could find such as:-

http://unix.stackexchange.com/questions/109272/add-freebsd-to-grub2-boot-menu

I put the following in /etc/grub.d/40_custom on my Arch Linux partition (/dev/sdb3)


```
menuentry "FreeBSD" {
set root='(hd1,2)'
kfreebsd /boot/loader
}
```

run `grub-mkconfig -o /boot/grub/grub.cfg`

FreeBSD appears on the Grub menu, but when I select it I get

```
error: not an assignment.
error: file '/boot/loader' not found

Press any key to continue...
```

I've tried several other guides but none work.

Can anyone suggest what I'm missing?


----------



## scottro (Apr 6, 2017)

You could try with chainloader, which sometimes works if /boot/loader doesn't.


```
menuentry "FreeBSD" {
set root='(hd1,2)
chainloader (hd1,2)+1
}
```

Do you have to run grub-mkconfig when you use 40_custom? (I just create a /boot/grub (or grub2, depending upon distribution) custom.cfg which doesn't require a grub-mkconfig afterwards. However, that's ot the issue, that part is just my idle curiosity.

Is the FreeBSD partition definitely on second hard drive's second partition?


----------



## balanga (Apr 6, 2017)

I'm using a ThinkPad X220 laptop which has an mSATA disk, an internal disk and a USB attached disk, which appears as /dev/da0 in FreeBSD, so I can't be certain how Grub enumerates the disks. The USB disk is first in the BIOS boot order.

I have been running `grub-mkconfig -o /boot/grub/grub.cfg` using 40_custom where grub is install on a Linux partition.
I'll create a custom.cfg as you suggest and see how it goes.


----------



## Dean Switzer (Apr 20, 2017)

I'm dual booting Linux Mint DE x32 and FreeBSD 11 x32 using grub.  Grub is installed in the Mint partition.  The /etc/grub.d/40_custom entry that works for me is:

menuentry "FreeBSD 11.0" {
insmod ufs2
set root=(hd0,2)
kfreebsd /boot/loader
}

And then update-grub and reboot.
Hope this helps.  Cheers


----------



## redpill (Apr 21, 2017)

If the device order is changing, you can use the `search` command to set the _root_ environment variable using the target partition's UUID or, if you have FreeBSD installed on ZFS, using the name of your zpool. For example:


```
search --fs-uuid --set=root --no-floppy 1234567890abcdef
# or
search --label --set=root --no-floppy myzpool
```
If you are using ZFS, the syntax for specifying file paths in GRUB is also a little bit funky. For example, the file /boot/loader found on a dataset named myzpool/ROOT/mybootenv would be addressed as:


```
/ROOT/mybootenv@/boot/loader
```
(And, yes, the pool name is supposed to be omitted like that.)

If you're really stuck, the best tool for debugging a problem like this is probably the GRUB command line. It's pretty easy to use if you have a reference handy. Also, `grub-probe` might be useful; you can use it to get a partition's UUID, test if GRUB can read the target filesystem, among other things.

Good luck.


----------



## balanga (Apr 21, 2017)

Dean Switzer said:


> I'm dual booting Linux Mint DE x32 and FreeBSD 11 x32 using grub.  Grub is installed in the Mint partition.  The /etc/grub.d/40_custom entry that works for me is:
> 
> menuentry "FreeBSD 11.0" {
> insmod ufs2
> ...



After spending a few days messing with Grub to get it working, my preferred way of installing things is to install FreeBSD first and use `pkg install grub2` to install Grub and manually create grub.cfg. 

https://forums.freebsd.org/threads/19701/page-2#post-347591

Once I get better acquainted with Grub I'll experiment with the custom entries. One thing I don't understand is what the `insmod ufs2`provides.. I didn't include it, but things worked... maybe because grub.conf is on a ufs partition.. I also not that chainloader is *not *required, contrary to what I read in some guides and that *kfreebsd* should be used instead.


----------



## balanga (Apr 21, 2017)

Having succeeded in booting FreeBSD via Grub, I am now stuck in my quest for creating multiple boot USB disk. I wanted to create such a universal boot disk for repair/maintenance/installation purposes but soon realised that using a disk partitioned with MBR severely restricts the disk's capability irrespective as to how big the disk is. So I've created a GPT disk and installed FreeBSD, thinking that installing Grub would be straightforward enough, but.....

```
grub-install --boot-directory=/boot /dev/da0
/usr/local/sbin/grub-bios-setup: warning: this GPT partition label contains no BIOS Partition; embedding won't be possible.
```

Anyone know how to get around this?


----------



## redpill (Apr 22, 2017)

On a GPT-formatted disk, GRUB looks for a partition with a specific GUID to install its core image to rather than using the traditional post-MBR gap. You can create this partition easily with gpart(8). For example:


```
gpart add -t bios-boot -b 2048 -s 1M -l grubcore da0
```
Note that this partition doesn't have to be very big.


----------



## balanga (Apr 22, 2017)

Many thanks for this. Does the GRUB partition have to be the first partition or can it be anywhere on the disk? ie can I add it after I have installed FreeBSD?


----------



## redpill (Apr 23, 2017)

I'd be inclined to call it a bug if it had to be the first partition. But it's probably not a bad idea given BIOS' traditional addressing limitations.

If you already have FreeBSD installed, there's no harm in tacking a _bios-boot_ partition onto the end and giving `grub-install` a shot. Resizing your swap partition if you have one is also an easy option if you need to make room.

But there should be plenty of unallocated space in the gap between the GPT header and the first partition. The typical image that gets generated by GRUB doesn't even break 200KiB. Often, 1-2MiB for the partition is suggested, but that's largely for future-proofing and aligning disk reads.


----------



## balanga (Apr 25, 2017)

I have only just come across the grub2-efi pkg which I believe is what I need to get Grub playing nicely with efi.... Trouble is I can't find an example of how to install it.. apart from 
https://forums.freebsd.org/threads/60009/" href="/index.php?threads/https://forums.freebsd.org/threads/60009//">Thread https://forums.freebsd.org/threads/60009/ where this command is used `grub-install --target=x86_64-efi --root-directory=/efi /dev/da0`...  I'm not sure what this '--root-directory' is supposed to specify. I see I have an efi partition but there is not /efi directory. I also note that grub-install() does not include such an option but does not flag it as an error when it is specified.


----------



## free-and-bsd (Oct 21, 2017)

balanga said:


> I have only just come across the grub2-efi pkg which I believe is what I need to get Grub playing nicely with efi.... Trouble is I can't find an example of how to install it.. apart from
> https://forums.freebsd.org/threads/60009/" href="/index.php?threads/https://forums.freebsd.org/threads/60009//">Thread https://forums.freebsd.org/threads/60009/ where this command is used `grub-install --target=x86_64-efi --root-directory=/efi /dev/da0`...  I'm not sure what this '--root-directory' is supposed to specify. I see I have an efi partition but there is not /efi directory. I also note that grub-install() does not include such an option but does not flag it as an error when it is specified.


I would recommend that your get better acquainted with GRUB2 first. Really, Not everything is smooth with grub2-efi booting FreeBSD, at least I had problems with X...


----------



## dfirebug (Nov 12, 2022)

For my setup with UEFI and GBT

```
menuentry "FreeBSD" {
        insmod ufs2   
        chainloader (hd0,7)/boot/loader.efi 
}
```
substitute the 7 for the disk partition number (if on MBR also add "msdos" before the number such as "msdos7")
for a BIOS system substitute loader.efi for loader(I have not tested this)
hd0 is the disk this might also need to be changed if multiple disks exist(I don't know how to tell how you should change it)
I am also not sure what to do if you are not on ufs2


----------

