# Can anyone help me with booting FreeBSD from GRUB?



## Mr_Dragon (Dec 9, 2018)

I have got installed Debian 9 Stretch and FreeBSD with UFS boot partition and root filesystem on ZFS. I use GPT scheme of partitioning on a legacy BIOS. Here's content of `/etc/grub.d/40_custom`:


```
menuentry "FreeBSD" {
    insmod bsd
    insmod ufs2
    set root='(hd0,gpt5)'
    chainloader +1
}
```


When I'm trying to boot FreeBSD through GRUB I receive `error: attempt to read or write outside of disk 'hd0'`.

Can anyone help me?


----------



## ShelLuser (Dec 9, 2018)

You probably also need a boot partition, one which is of type freebsd-boot, then point Grub to it. That way you'll trigger FreeBSD's boot menu which in its turn will load the kernel and boot the system.


----------



## rufwoof (Dec 9, 2018)

In the absence of anything else/better, methods I've used to boot OBSD from Debian grub2 ...

menuentry 'OpenBSD' { set root=(hd0,4) chainloader +1 } menuentry 'OpenBSD bsd.rd' { kopenbsd /bsd.rd }

1st line is to boot a installed version
2nd line is to boot the ramdisk version (for install/updates).

Another working method I've seen is

menuentry "OpenBSD" {        chainloader (hd1,gpt2)/EFI/Boot/bootx64.efi }

where gpt2 is mounted at /boot/efi so the OS's full path would be /boot/efi/EFI/Boot/bootx64.efi

Don't know if you might use similar for FreeBSD.


----------



## Vull (Dec 9, 2018)

I also have a GPT drive with legacy bios. Here's what I use:

Partition layout for FreeBSD:

```
ada0p1  freebsd-boot  512 KB  mountpoint none
ada0p2  freebsd-ufs   100 GB  mountpoint = /
ada0p3  freebsd-swap  5.8 GB  mountpoint none
```

Entry for /etc/grub.d/40_custom:

```
menuentry "FreeBSD 11.2-RELEASE" {
  insmod ufs2
  set root=(hd0,gpt2)
  kfreebsd /boot/loader
}
```


----------



## Bobi B. (Dec 9, 2018)

Here is my /boot/grub/grub.cfg since I couldn't make standard FreeBSD loader to load kernel & OS from a different physical drive:

```
menuentry "FreeBSD (or GNU/kFreeBSD), via /boot/loader" {
    insmod part_gpt
    set root=(hd5,2)
    kfreebsd /boot/loader
}
```

The box itself is HP MicroServer Gen8 (I believe) with 4 hard drives, 1 SSD for the OS and a flash drive with GRUB only to enable booting.


----------



## drhowarddrfine (Dec 9, 2018)

Mr_Dragon Lesson learned. If you want to know how to do something, never ask on reddit.


----------



## Mr_Dragon (Dec 9, 2018)

My partition layout is:

```
ada0 - primary 3 TB hard drive with GPT partition scheme
ada0p1 - BIOS boot
ada0p2 - linux filesystem, mountpoint: /boot
ada0p3 - linux filesystem LVM on LUKS
ada0p4 - FreeBSD boot, gptboot
ada0p5 - FreeBSD UFS, mountpoint: /boot, required by geli to boot FreeBSD
ada0p6 - FreeBSD swap
ada0p7 - FreeBSD ZFS on geli
```

My actual config

```
menuentry "FreeBSD" {
    insmod bsd
    insmod ufs2
    insmod part_gpt
    set root='(hd0,gpt4)'
    kfreebsd /boot/loader
}
```
doesn't work.

It returns an error: unknown filesystem.



			
				drhowarddrfine said:
			
		

> Mr_Dragon Lesson learned. If you want to know how to do something, never ask on reddit.


Why?


----------



## Sensucht94 (Dec 9, 2018)

Hi, first of all you shouldn't use nor need chainloading, as FreeBSD is officially supported by GRUB2

Personally when I decided to multiboot FreeBSD on FFSv2 with Linux and NetBSD, I ended  up making kfreebsd  directly point to the kernel (by bypassing loader), in your case something like:


```
menuentry "FreeBSD" {
          insmod bsd
          insmod ufs2
          insmod part_gpt
          set root='(hd0,gpt4)'
          search --no-floppy --fs-uuid --set $UUID
          kfreebsd /boot/kernel/kernel
          kfreebsd_loadenv /boot/device.hints
          kfreebsd_loadenv /boot/kernel/linker.hints
          set kFreeBSD.vfs.root.mountfrom=ufs:/dev/ada0p4
          set kFreeBSD.vfs.root.mountfrom.options=rw
          set kFreeBSD.module_path="/boot/kernel;/boot/modules"
}
```

Any additional module can be loaded early adding:

```
kfreebsd_module_elf  /boot/kernel/$module.ko
```

Any sysctl-tunable kernel parameter can be set early by adding:

```
set kFreeBSD.$parameter=$value
```
like shown above

EDIT: from your brief above it was unclear to me where root is actually located, if either on ada0p4 or ada0p5; grub should look for the freebsd-ufs partition  where root is located


----------



## Vull (Dec 9, 2018)

That agrees with my understanding too. It should not be on ada0p4 if that's your "freebsd-boot" partition. I'm not sure about your system because I've never used a separate ufs partition for my "/boot" mountpoint, however, If I add the following entry, it will fail to boot the system, but for my later benefit, it will return an error message that indicates that it finds the loader on "hd0,gpt2" which is  the correct location on my system, which I can then put into the final configuration. I'm guessing that on your system it should be ada0p5.

```
menuentry "FreeBSD11.2-RELEASE" {
  insmod ufs2
  search --no-floppy --file /boot/loader
  kfreebsd /boot/loader
}
```


----------



## drhowarddrfine (Dec 10, 2018)

Mr_Dragon said:


> Why?


How many answers do you have here? How many did you get on reddit? Mainly from 15-year old kids but I don't think you got any at all when I saw that. And, no, I do not go to reddit except when someone tells me I need to see something. Usually cause it's so dumb they want me to have a laugh.


----------

