# Installing bootcode



## Robert Fitzpatrick (Dec 20, 2017)

Not sure how it happened, but after an upgrade from 10.1 to get 10.4, the server boots to an OK prompt and I have to do the following to boot:


```
set currdev=disk0s1a
```

I understand I need to install the bootcode to fix, what command would I use? Here is the disk info:


```
root@www:~ # gpart show
=>       34  419430333  ada0  GPT  (200G)
         34  417333246     1  freebsd-ufs  (199G)
  417333280    2097087        - free -  (1.0G)

=>      63  41942977  ada1  MBR  (20G)
        63  41929587     1  freebsd  [active]  (20G)
  41929650     13390        - free -  (6.5M)

=>       0  41929587  ada1s1  BSD  (20G)
         0   1024000       1  freebsd-ufs  (500M)
   1024000    524288       2  freebsd-swap  (256M)
   1548288  20480000       4  freebsd-ufs  (9.8G)
  22028288  19901299       5  freebsd-ufs  (9.5G)
root@www:~ # cat /etc/fstab
# Device                Mountpoint      FStype  Options         Dump    Pass
/dev/ada1s1b            none            swap    sw              0       0
/dev/ada1s1a            /               ufs     rw              1       1
/dev/ada1s1d            /usr            ufs     rw              2       2
/dev/ada1s1e            /var            ufs     rw              2       2
/dev/ada0p1             /www            ufs     rw              0       0
/dev/acd0               /cdrom          cd9660  ro,noauto       0       0
```


----------



## bookwormep (Dec 20, 2017)

If you need to overwrite the MBR:
https://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/boot-introduction.html


```
# fdisk -B -b /boot/boot0 ada0
```
I use a dual-boot setup on my systems and I've used the above to reset the MBR with this.


----------



## linux->bsd (Dec 20, 2017)

Robert Fitzpatrick said:


> Not sure how it happened, but after an upgrade from 10.1 to get 10.4, the server boots to an OK prompt and I have to do the following to boot:
> 
> 
> ```
> ...



You might merely need to install the bootcode, which would be something like this: `gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 ada0`.

gpart():

```
GPT
     In    this example, we will format ada0 with the GPT scheme and create boot,
     swap and root partitions.    First, we need to create the partition table:

       /sbin/gpart create -s GPT ada0

     Next, we install a    protective MBR with the    first-stage bootstrap code.
     The protective MBR    lists a    single,    bootable partition spanning the    entire
     disk, thus    allowing non-GPT-aware BIOSes to boot from the disk and    pre-
     venting tools which do not    understand the GPT scheme from considering the
     disk to be    unformatted.

       /sbin/gpart bootcode    -b /boot/pmbr ada0

     We    then create a dedicated    freebsd-boot partition to hold the second-
     stage boot    loader,    which will load    the FreeBSD kernel and modules from a
     UFS or ZFS    filesystem.  This partition must be larger than    the bootstrap
     code (either /boot/gptboot    for UFS    or /boot/gptzfsboot for    ZFS), but
     smaller than 545 kB since the first-stage loader will load    the entire
     partition into memory during boot,    regardless of how much data it actu-
     ally contains.  We    create a 472-block (236    kB) boot partition at offset
     40, which is the size of the partition table (34 blocks or    17 kB) rounded
     up    to the nearest 4 kB boundary.

       /sbin/gpart add -b 40 -s 472    -t freebsd-boot    ada0
       /sbin/gpart bootcode    -p /boot/gptboot -i 1 ada0
```


----------



## Robert Fitzpatrick (Dec 20, 2017)

Ok, makes sense and ada0 is GPT, but I get an error when trying to do so:


```
root@www:~ # gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 ada0
gpart: /dev/ada0p1: Operation not permitted
```


----------



## SirDice (Dec 20, 2017)

It looks like you're booting from ada1, not ada0. Why the odd order?

Don't write the gptboot(8) to anywhere, you don't have the necessary freebsd-boot partition for it. Which leads me to believe you're actually booting from ada1 using a 'traditional' MBR boot.


----------



## Robert Fitzpatrick (Dec 20, 2017)

This is a VM, it put the secondary disk first when we migrated from another host. I should remove and put back in the order to make the system partitions disk first ada0 and then use the fdisk below to reinstall the boot code?


```
fdisk -B -b /boot/boot0 ada0
```

Or does this really matter and I should be able to install to ada1 as is?


----------



## SirDice (Dec 20, 2017)

You can try both, it won't hurt to have a boot record on both disks. It's also possible it picks up the boot record from ada0 and then continues to boot from ada1. But it's a confusing set up, so if you can swap them it would be better in the long-run as there's less confusion about which drive is used to boot from. Note however if you do swap them you'd need to adjust fstab too.


----------



## linux->bsd (Dec 20, 2017)

SirDice said:


> Don't write the gptboot(8) to anywhere, you don't have the necessary freebsd-boot partition for it.



 I should have looked closer at the `gpart` stdout. Good save.


----------

