# Disk setup



## balanga (May 2, 2017)

I've learnt a lot from reading Warren Block's excellent guide to using Gpart, but I think it needs an update as far as setting up gpt. I read somewhere that a freebsd-boot partition does not need to be created, and only efi and freebsd-ufs are required to get FreeBSD to boot. If that is the case how do I install the bootcode?
And can someone explain what this does?
`gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 da0`


----------



## SirDice (May 3, 2017)

balanga said:


> And can someone explain what this does?
> `gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 da0`


It writes the data from pmbr to the disk's boot sector (Master Boot Record) and gptboot to the freebsd-boot partition.


```
Both types of bootstrap code are used to boot from the GUID Partition Ta-
     ble.  First, a protective MBR is embedded into the first disk sector from
     the /boot/pmbr image.  It searches through the GPT for a freebsd-boot
     partition (see the PARTITION TYPES section) and runs the next bootstrap
     stage from it.  The freebsd-boot partition should be smaller than 545 KB.
     It can be located either before or after other FreeBSD partitions on the
     disk.  There are two variants of bootstrap code to write to this parti-
     tion: /boot/gptboot and /boot/gptzfsboot.
```


----------



## balanga (May 3, 2017)

I thought I had done that but get 'Missing boot loader' when the system boots...

I guess I've messed up my EFI partition...


----------



## SirDice (May 3, 2017)

I'm still a bit hazy when it comes to EFI booting but as far as I understood it it doesn't use the master boot record at all. It just looks for the efi partition and loads a specific file from it (usually bootx64.efi).

If I recall correctly the "Missing boot loader" message comes from the master boot record. It's possible you have an UEFI implementation that automatically falls back to CSM booting if it detects a master boot record.


----------



## balanga (May 3, 2017)

Presumably under EFI booting bootx64.efi (under FreeBSD) is the file that should get loaded first....

I'm not sure if the EFI partition is being set up correctly...

http://www.wonkity.com/~wblock/docs/html/disksetup.html :-


```
# mount -t msdosfs /dev/da0p2 /mnt
# mkdir -p /mnt/EFI/BOOT
# cp /boot/boot1.efi /mnt/EFI/BOOT/
# umount /mnt
```

Does this depend on which version of FreeBSD is being used? 

Of course it should be /dev/da0p1 if there is no freebsd-boot partition.


----------



## SirDice (May 3, 2017)

Typically the efi partition is the first partition on the disk but the EFI specification states that it doesn't matter if it's the first, second or last, as long as it has the efi type.

The file however _must_ be named bootx64.efi (for 64 bit systems). 

If FreeBSD is the only OS (you're not trying to dualboot) this should also work:
`dd if=/boot/boot1.efifat of=/dev/da0p2`
This assumes /dev/da0p2 is the efi partition. Note however that this will destroy everything that was already in there.


```
root@c1:~# mdconfig -a -t vnode -f /boot/boot1.efifat
md0
root@c1:~# mount -t msdosfs /dev/md0 /mnt/
root@c1:~# ll /mnt/
total 1
drwxr-xr-x  1 root  wheel  512 Jan  8  2016 efi
root@c1:~# ll /mnt/efi/
total 1
drwxr-xr-x  1 root  wheel  512 Jan  8  2016 boot
root@c1:~# ll /mnt/efi/boot/bootx64.efi
-rwxr-xr-x  1 root  wheel  131072 Jan  8  2016 /mnt/efi/boot/bootx64.efi
```


----------



## k.jacker (May 5, 2017)

I can confirm that it doesn't matter where on the disk the efipartition resides, just tried it out.
Disabled and removed swap from /dev/ada0p3 and then `gpart delete -i3 /dev/ada0` and `gpart add -i3 -s 800k -t efi /dev/ada0`
and then dd'ed like SirDice suggestet `dd if=/boot/boot1.efifat of=/dev/ada0p3`.
Then completely disabled CMS/Legacy mode in the BIOS and it bootet in UEFI mode right away, then again forced the BIOS to only boot in legacy mode and could boot from the old freebsd-boot partition as well. Nice.
I still was a little confused with how booting in UEFI mode works, but that's gone now. I'm on FreeBSD11 RELEASE-p9


----------



## wblock@ (May 6, 2017)

balanga said:


> but I think it needs an update as far as setting up gpt. I read somewhere that a freebsd-boot partition does not need to be created, and only efi and freebsd-ufs are required to get FreeBSD to boot.


That only works if the computer boots in UEFI mode, and there are a lot of BIOS-booting computers still around.

Some UEFI computers do not like having a freebsd-boot partition, even when booting in UEFI.  I've seen this on Dell notebooks.


----------

