gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 da0

Niatross

Active Member

Thanks: 2
Messages: 224

#1
I am a little confused with this "freebsd-boot" partition.

What does the following line do?

Code:
gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 da0
Does it install the /boot/pmbr into the first sector (sector 0) of the device?
Does it install the /boot/gptboot file into the first sector of the root partition (AKA:"/") ?

The articles that I have been reading seem to say that the /boot/pmbr and /boot/gptboot are just installed into the boot partition (AKA: "freebsd-boot")

If they are only installed into the boot partition (AKA: "freebsd-boot"), then how the hell is the /boot/gptboot file going to be installed into the first sector of the root partition (AKA:"/")?

If I break down the command, it looks to me as if the "-b /boot/pmbr" file is installed into the the first sector (sector 0) of the device and the "-p /boot/gptboot -i 1" file is installed into the first sector of the boot partition (AKA: "freebsd-boot").

Shouldn't the "-p /boot/gptboot -i 1" be "-p /boot/gptboot -i 2", so that it is installed into the first sector of the root partition (AKA:"/")?

Let me ask it in another way:

If the "/boot/gptboot" file is installed into the the first sector of the boot partition (AKA: "freebsd-boot"), then how does it know how to boot the operating system on the root partition (AKA:"/")?
 

SirDice

Administrator
Staff member
Administrator
Moderator

Thanks: 6,627
Messages: 28,191

#2
The file pmbr is written to the protective MBR on the first sector of the disk. The file gptboot is written to the freebsd-boot partition. See the BOOTSTRAPPING section in gpart(8):
Code:
     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.

     /boot/gptboot is used to boot from UFS partitions.  gptboot searches
     through freebsd-ufs partitions in the GPT and selects one to boot based
     on the bootonce and bootme attributes.  If neither attribute is found,
     /boot/gptboot boots from the first freebsd-ufs partition.  /boot/loader
     (the third bootstrap stage) is loaded from the first partition that
     matches these conditions.  See gptboot(8) for more information.
 

asteriskRoss

Well-Known Member

Thanks: 133
Messages: 418

#3
Edit: @SirDice beat me to it :)

Your question sounds like you expect two stages of bootloader, when there are actually three.

Useful excerpts from gpart(8), firstly regarding the boot sequence:
Bootstrap code can be separated into two types. The first type is embedded in the partitioning scheme's metadata, while the second type is located on a specific partition. Embedding bootstrap code should only be done with the gpart bootcode command with the -b bootcode option. The GEOM PART class knows how to safely embed bootstrap code into specific partitioning scheme metadata without causing any damage.
[...]
Both types of bootstrap code are used to boot from the GUID Partition Table. 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.
[...]
/boot/gptboot is used to boot from UFS partitions. gptboot searches through freebsd-ufs partitions in the GPT and selects one to boot based on the bootonce and bootme attributes. If neither attribute is found, /boot/gptboot boots from the first freebsd-ufs partition. /boot/loader (the third bootstrap stage) is loaded from the first partition that matches these conditions.
And secondly regarding the command you posted:
bootcode
Embed bootstrap code into the partitioning scheme's metadata on the geom (using -b bootcode) or write bootstrap code into a partition (using -p partcode and -i index).
[...]
The -b bootcode option specifies a file that contains the bootstrap code.
[...]
The -p partcode option specifies a file that contains the bootstrap code intended to be written to a partition. The partition is specified by the -i index option.
Armed with this, let's answer your questions:
Niatross said:
What does the following line do?
Code:
gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 da0
Does it install the /boot/pmbr into the first sector (sector 0) of the device?
Does it install the /boot/gptboot file into the first sector of the root partition (AKA:"/") ?
[...]
Shouldn't the "-p /boot/gptboot -i 1" be "-p /boot/gptboot -i 2", so that it is installed into the first sector of the root partition (AKA:"/")?
So, yes to the first question, the first stage bootloader /boot/pmbr is installed at the start of the device into the protective MBR area at the start of the GPT scheme layout. Wikipedia has a nice diagram of GPT scheme layout. The warning "Embedding bootstrap code should only be done with the gpart bootcode command" in gpart(8) suggests there may be some subtlety here but I have not delved into the source code to look; others may provide more information.

To the second and third questions, no, if you want a working system. The command will also install the second stage bootloader, /boot/gptboot into the partition with index 1 on device da0; that is, what you would see as /dev/da0p1 in your running system. This should be a partition of type "freebsd-boot" and not the UFS partition containing your system root, as described in the first man excerpt. This second stage bootloader then finds a suitable partition of type "freebsd-ufs" (which on a typical system might be /dev/da0p2 or /dev/da0p3), according to the conditions described in the man excerpt and executes the third stage bootloader.
 
Last edited by a moderator:

kpa

Beastie's Twin

Thanks: 1,792
Messages: 6,305

#5
asteriskRoss said:
So, yes to the first question, the first stage bootloader /boot/pmbr is installed at the start of the device into the protective MBR area at the start of the GPT scheme layout. Wikipedia has a nice diagram of GPT scheme layout. The warning "Embedding bootstrap code should only be done with the gpart bootcode command" in gpart(8) suggests there may be some subtlety here but I have not delved into the source code to look; others may provide more information.
The "subtlely" here is that the MBR block is the first 512 byte block on the disk and it's actually in two parts, the code and then the MBR partition table. The bootcode is the first 446 bytes out of the 512 and the rest is reserved for the partition table. With this in mind it should be obvious that if you want to replace only the code part of the MBR block you have to know to write only 446 bytes and not the full 512 or you will mess up the partition table. That's why the warning is there. Oh and I think you can't just write 446 bytes out of the 512 to a block device, you probably have to read the MBR and do the replace of the MBR code in memory and write the changed block back keeping the old partition table contents.
 
Top