en/blog/2021-11-03-dualboot-linux-and-openbsd-with-grub.md (view raw)
1# dualboot linux and openbsd with grub
2
3```data
4
5date: 2021-11-03
6author: la-ninpre
7tags: openbsd, linux, grub, tutorial
8```
9
10i've been trying to dualboot openbsd with linux using grub on both bios and
11uefi machines and here's a solution that i've come up with.
12
13there are some guides about this on the internet, but there's no single guide
14that covers both bios and uefi. @rootbsd has a video where he shows how to
15do this, but his solution has one little disadvantage. he's specifying drives
16in a grub config using relative drive and partition numbers, such as
17`(hd0,gpt2)`. since these numbers could be different if one inserts a new drive
18to the computer, or changes drive order, the boot option could fail
19(which happened).
20
21all partitions and drives have their unique identifier -- uuid. there's no
22direct way to specify uuid in grub configuration, but there is a workaround.
23grub manual describes the `search` command which has an option to set the root
24device if it is found. so we can use it for our purposes.
25
26## dualbooting in bios/legacy mode
27
28this guide assumes that you have two drives, one of which has linux system installed
29and another has openbsd installed.
30
31on linux system, use commands like `blkid` or `lsblk -f` to get a list of drives with
32their uuids. there should be a partition with type 'ufs2' on a drive with openbsd installed.
33write down or copy uuid of that partition.
34
35depending on your linux distribution, you may have different options to edit
36the grub config file. many distributions provide `/etc/grub.d` directory, which
37has separate files that then get combined into `/boot/grub/grub.cfg`. if you have it,
38then you can edit the `/etc/grub.d/40_custom` file, which is a good place
39for custom boot options and such. if it is not your case, you can edit `grub.cfg` directly,
40but note that it may be overwritten on a system update.
41
42add this to your grub config (`40_custom` or `grub.cfg`, see above):
43
44```grub.cfg
45
46menuentry 'OpenBSD' {
47 search -sun <UUID>
48 chainloader +1
49}
50```
51
52where \<UUID\> is the uuid of your openbsd partition (with type 'ufs2').
53
54you can review options for a `search` command in grub's info page,
55but basically they are needed to use uuid instead of drive number,
56to set the root variable and to avoid searching floppies (which is not required,
57but added just in case).
58
59if you edited the `40_custom` file, don't forget to run `grub-mkconfig` or `update-grub`
60(check your distribution's manual on updating the grub configuration).
61
62after rebooting, you should see openbsd boot option in grub menu.
63
64## dualbooting in uefi/gpt mode
65
66openbsd creates few partitions if you choose gpt partitioning scheme during installation.
67one of these partitions has fat12 file system and is of our interest.
68on linux side you need to get its uuid.
69
70as with bios/legacy boot described earlier, you need to add a boot option to grub,
71but this time it's a bit different:
72
73```grub.cfg
74
75menuentry 'OpenBSD' {
76 search -sun <UUID>
77 chainloader /efi/boot/bootx64.efi
78}
79```
80
81where \<UUID\> is the uuid of openbsd's fat12 partition.
82
83don't forget to update grub configuration if you edited `40_custom` file.
84
85this also works even if you used full-disk encryption on openbsd.