Multiboot Windows and OpenBSD with rEFInd

       1349 words, 7 minutes

A year ago, I wrote about multibooting Windows, Linux and OpenBSD on my laptop. Since then, lots have happened. The most relevant part is that Linux is gone and I only multiboot Windows and OpenBSD.

If I had done it from the beginning, I would have used rEFInd rather than Grub. And here’s how.

Before anything else, have a look at the OpenBSD Multibooting FAQ .

Resize MS Windows

My ThinkPad has been restored using the Lenovo Recovery USB Drive. This means the internal disk is partitioned with several parts that are supposed to ease later restorations from the disk.

To install, and multiboot, OpenBSD, I first needed to get some free disk space.

Start a Windows Command as an Administrator.
Run the diskpart command.
Select the relevant disk and have a look at the partition layout:

DISKPART> LIST DISK

  N° disque  Statut         Taille   Libre    Dyn  GPT
  ---------  -------------  -------  -------  ---  ---
  Disque 0    En ligne        238 G octets      0 octets        *

DISKPART> SELECT DISK 0

Le disque 0 est maintenant le disque sélectionné.

DISKPART> LIST PARTITION

  N° partition   Type              Taille   Décalage
  -------------  ----------------  -------  --------
  Partition 1    Système            260 M   1024 K
  Partition 2    Réservée            16 M    261 M
  Partition 3    Principale         236 G    277 M
  Partition 4    Récupération      2000 M    236 G

Select the partition to shrink and apply reduction to. In my case, it is partition #3, the biggest, named “Principale”. I decided to cut it in half so that Windows and OpenBSD will get the same disk space.

DISKPART> SELECT PARTITION 3

La partition 3 est maintenant la partition sélectionnée.

DISKPART> SHRINK QUERYMAX

Le nombre maximal d’octets récupérables est :  180 GB (184734 Mo)

DISKPART> SHRINK DESIRED=120832

DiskPart a réduit la taille du volume de :  118 G octets

DISKPART> DETAIL PARTITION

Partition 3
Type    : ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
Masqué  : Non
Requis  : Non
Attrib  : 0000000000000000
Décalage en octets : 290455552

  N° volume   Ltr  Nom          Fs     Type        Taille   Statut     Info
  ----------  ---  -----------  -----  ----------  -------  ---------  --------
* Volume 0     C   Windows      NTFS   Partition    118 G   Sain       Démarrag

Finally, create a partition for OpenBSD in the remaining free space:

DISKPART> CREATE PARTITION PRIMARY

DiskPart a réussi à créer la partition spécifiée.

DISKPART> LIST PARTITION

  N° partition   Type              Taille   Décalage
  -------------  ----------------  -------  --------
  Partition 1    Système            260 M   1024 K
  Partition 2    Réservée            16 M    261 M
  Partition 3    Principale         118 G    277 M
* Partition 5    Principale         118 G    118 G
  Partition 4    Récupération      2000 M    236 G

DISKPART> EXIT

Everything is now ready to install OpenBSD. You may want to reboot and check if Windows is still loading, just in case. Also note that BitLocker is not enabled on Windows.

Install OpenBSD

Reboot the computer and proceed to the OpenBSD installation.

Special care is required when it comes to the disk selection part. Don’t use any “whole disk” option as this would wipe out the Windows installation. You’d rather have to edit partition #3 (which was partition #5 in the previous diskpart output) and set the partition ID to A6, which means “an OpenBSD partition”.

Which disk is the root disk? [sd0] <ENTER>

Encrypt the root disk with a (p)assphrase or (k)eydisk? [no] p<ENTER>

Configuring the crypto chunk sd0...

Disk: sd0     Usable LBA: 334 to 500118158 [500118192 Sectors]
   #: type                                 [     start:      size ]
-------------------------------------------------------------------
   0: EFI Sys                              [      2048:    532480 ]
   1: e3c9e316-0b5c-4db8-817d-f92df00215ae [    534528:     32768 ]
   2: Microsoft basic data                 [    567296: 247990272 ]
   3: Microsoft basic data                 [ 248557568: 247463936 ]
   4: Win Recovery                         [ 496021504:   4096000 ]
Use Whole disk MBR, whole disk (G)PT or (E)dit? [gpt] e<ENTER>

Enter 'help' for information
sd0: 1> edit 3
Partition id ('0' to disable) [01 -EFF, <uuid>]: [01] (? for help) A6<ENTER>
Partition offset [34 - 500118158]: [248557568] <ENTER>
Partition size [1 - 247463936]: [247463936] <ENTER>
Partition name: [Basic data partition] OpenBSD<ENTER>
sd0*: 1> write
Writing GPT.
sd0: 1> quit

New passphrase: xxxxx<ENTER>
(...)

The sd2 encrypted volume will be used as usual to install the OpenBSD material.

In the end, the boot loader installation fails.

Making all device nodes... done
installboot: no OpenBSD partition

Failed to install bootblocks.
You will not be able to boot OpenBSD from sd2.

When you have a look at disklabel sd0, you can see that the EFI partition is not referenced properly. Let’s solve this and install the OpenBSD boot files in the EFI partition. The previous fdisk outputs helps settings the proper values in the disklabel editor.

# chroot /mnt
# export TERM=vt100

# disklabel -e sd0
(...)
  i:     532480    2048  MSDOS # EFI System Partition
  j:  247990272  567296  MSDOS # MS Windows

# mount -t msdos /dev/sd0i /mnt
# mkdir /mnt/EFI/OpenBSD
# cp /usr/mdec/BOOTX64.EFI /mnt/EFI/OpenBSD/
# umount /mnt

Windows expects the BIOS date to be localtime. But OpenBSD expects it to be UTC. Let’s tweak OpenBSD so deal with this ; Europe/Paris being UTC+2, the offset is 120 (minutes).

# echo kern.utc_offset=120 >> /etc/sysctl.conf

If you like it better, you can teach Windows to use UTC. But last time I checked, that requires modifying the registry. And you never know when this will be rolled back…

Now quit and reboot to Windows to install rEFInd.

# exit
# reboot

Deploy rEFInd

The directions for installing rEFInd using Windows are very detailed. Applied to my use case, it goes likes this.

Start a Command Prompt as an Administrator and make the ESP visible.

C:\Windows\System32> mountvol R: /S

Download rEFInd and extract the archive.

Using the Command Prompt, copy the relevant file to the UEFI partition.

C:\Windows\System32> xcopy /E                      \
  C:\Users\joel\Downloads\refind-bin-0.14.2\refind \
  R:\EFI\refind\

Remove unwanted rEFInd files and rename the configuration file.

C:\Windows\System32> r:
R:\> cd EFI\refind
R:\EFI\refind> rmdir /S /Q drivers_aa64 drivers_ia32 tools_aa64 tools_ia32
R:\EFI\refind> del refind_aa64.efi refind_ia32.efi
R:\EFI\refind> ren refind.conf-sample refind.conf

Set rEFInd as the default EFI boot program.

R:\EFI\refind> bcdedit /set "{bootmgr}" path \EFI\refind\refind_x64.efi
R:\EFI\refind> bcdedit /set "{bootmgr}" description "rEFInd boot loader"

You can now restart the computer and rEFInd should appear. If all went ok, you can choose to boot Windows or OpenBSD.
Because of the Windows restoration I’ve done, I also have the Lenovo Rescue Boot option.

More rEFInd tuning can be done in OpenBSD once the EFI partition is mounted.

# mkdir /mnt/efi
# mount -t msdos /dev/sd0i /mnt/efi

Speeding up loading time

By default, rEFInd takes a little time to load ; like 4 or 5 seconds. It turns out to be mostly due to drivers loading.

I strongly recommend that you remove some or all of the drivers for the architecture you are using. If you don’t need them, they’ll slow down the start process, and worse, loading unnecessary drivers can cause some systems to hang or interfere with the drivers you do need.

For my Windows + OpenBSD configuration, I don’t need any of the drivers at all. So let’s just delete them:

# cd /mnt/efi/EFI/REFIND/drivers_x64
# tar czf refind-drivers.tar.gz *EFI *efi
# rm *EFI *efi

rEFInd also scans a bunch of places for things to boot. Most of the time, I only want to have the choice between the two installed OSes. So I limited rEFInd to displaying only what I want.

# cd /mnt/efi/EFI/REFIND/
# vi refind.conf
(...)
scanfor manual
(...)
menuentry "Windows 11" {
     loader \EFI\Microsoft\Boot\bootmgfw.efi
}

menuentry "OpenBSD" {
    loader EFI\OpenBSD\BOOTX64.EFI
}

menuentry "LenovoBT" {
    loader EFI\Boot\LenovoBT.EFI
    icon /EFI/REFIND/ICONS/tool_rescue.png
}

I’m not sure what will happen when some firmware update will be triggered from Windows. There may be some manual steps to run the updates. So if you don’t want to deal with this, keep the default scanfor value.

Theming

The default rEFInd look is … well the default.
There are a bunch of other options you can use.

I went for something looking like this:

Grab the theme source.

# pkg_add git
# git clone https://github.com/1j01/refind-theme-regular.git
# cd refind-theme-regular

Copy the icons to the EFI partition.

# mkdir -p /mnt/efi/EFI/REFIND/themes/refind-theme-regular
# cp -r icons/128-48 /mnt/efi/EFI/REFIND/themes/refind-theme-regular/icons

Configure and enable the theme.

# cat >> /mnt/efi/EFI/REFIND/themes/refind-theme-regular/theme.conf
banner themes/refind-theme-regular/icons/bg-black.png
icons_dir themes/refind-theme-regular/icons
big_icon_size 128
small_icon_size 48
selection_big themes/refind-theme-regular/icons/selection-big-dark.png
selection_small themes/refind-theme-regular/icons/selection-small-dark.png
^D

# echo 'include themes/refind-theme-regular/theme.conf' \
  >> /mnt/efi/EFI/REFIND/refind.conf

Reboot. And voilà!
Happy boot selection!