vmdk parmeter "-mbr"

Discussions related to using VirtualBox on Linux hosts.
Post Reply
lpmanjaro
Posts: 55
Joined: 13. May 2021, 18:51

vmdk parmeter "-mbr"

Post by lpmanjaro »

I need help in understanding how to use the -mbr parm of the vmdk create vboxmanage command.
If I have a large ssd running (gpt uefi) manjaro and add 3 distros, would building a file that contains the "MBR" after each installation be useful in defining the seperate distros for virtualbox vmdk usage ?
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: vmdk parmeter "-mbr"

Post by scottgus1 »

In section 9.7.1.2 it says:
In some configurations it may be necessary to change the MBR code of the created image. For
example, to replace the Linux boot loader that is used on the host by another boot loader. This
enables for example the guest to boot directly to Windows, while the host boots Linux from the
“same” disk. For this purpose the -mbr option is provided. It specifies a file name from which to
take the MBR code.
The partition table is not modified at all, so a MBR file from a system with
totally different partitioning can be used. An example of this is:

$ VBoxManage internalcommands createrawvmdk -filename
<path-to-file>.vmdk -rawdisk /dev/sda -partitions 1,5 -mbr winxp.mbr

The modified MBR will be stored inside the image, not on the host disk.
The created image can be attached to a storage controller in a VM configuration as usual.
That's all I can find. I don't know how to create the MBR file.
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: vmdk parmeter "-mbr"

Post by mpack »

The MBR is the first sector of an MBR formatted (i.e. not GPT formatted) drive. The easiest way to create one is to copy the first 512 bytes of an existing image to a file, then edit that file using your favorite tool, e.g. a hex editor or something you knock up in C.

This is NOT a prescriptive solution. The MBR contains a partition map which must correctly describe the layout of the disk.
 Edit:  CloneVDI contains source code for an MBR, see attachment. This includes a declaration for the partition table (at the end), which is currently blank but you could conceivably construct the partition table there and then reassemble to create your MBR file. This may be more convenient than using a hex editor. 
Attachments
mbr.zip
(3.25 KiB) Downloaded 10 times
fth0
Volunteer
Posts: 5668
Joined: 14. Feb 2019, 03:06
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Linux, Windows 10, ...
Location: Germany

Re: vmdk parmeter "-mbr"

Post by fth0 »

mpack wrote:The MBR contains a partition map which must correct describe the layout of the disk.
Not in this special case: VirtualBox uses only the first 190 446 (0x01be) bytes from the given MBR file.
Last edited by fth0 on 10. Sep 2022, 15:41, edited 1 time in total.
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: vmdk parmeter "-mbr"

Post by mpack »

fth0 wrote: Not in this special case: VirtualBox uses only the first 190 (0x01be) bytes from the given MBR file.
Are you referring to the VirtualBox feature? Are you saying that VirtualBox constructs a new partition map and ignores the one in the MBR?

Come to think of it, the CloneVDI feature is similar. CloneVDI can treat partition images as complete disks, that's where the above MBR source came from. CloneVDI too synthesizes the partition map, hence why the one in the source is just a stub. Only the boot code is honoured. But the partition map offset is at 446, not 190. 0x1BE is correct tho.

Oh well, in that case the mbr.bin file I provided above may be directly usable in a classically booted VM (not GPT, not grub).
fth0
Volunteer
Posts: 5668
Joined: 14. Feb 2019, 03:06
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Linux, Windows 10, ...
Location: Germany

Re: vmdk parmeter "-mbr"

Post by fth0 »

mpack wrote:
fth0 wrote: Not in this special case: VirtualBox uses only the first 190 (0x01be) bytes from the given MBR file.
Are you referring to the VirtualBox feature? Are you saying that VirtualBox constructs a new partition map and ignores the one in the MBR?
Yes, no and yes. ;) VirtualBox uses the partition map of the physical disk as follows:

As documented in 9.7.1.2. Access to Individual Physical Hard Disk Partitions, VirtualBox lets the guest "see all partitions that exist on the physical disk", but "access will be filtered in that reading from partitions for which no access is allowed the partitions will only yield zeroes, and all writes to them are ignored. [...] In some configurations it may be necessary to change the MBR code of the created image. [...] For this purpose the -mbr option is provided. It specifies a file name from which to take the MBR code. The partition table is not modified at all, so a MBR file from a system with totally different partitioning can be used."
mpack wrote:But the partition map offset is at 446, not 190. 0x1BE is correct tho.
Ok, you got me. :oops: (I know "0xC0" by heart from the well-known local IPv4 addresses and my brain ignored the "1".)
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: vmdk parmeter "-mbr"

Post by mpack »

fth0 wrote: As documented in 9.7.1.2. Access to Individual Physical Hard Disk Partitions, VirtualBox lets the guest "see all partitions that exist on the physical disk"
You (and that paragraph from the manual), seems to be citing the case when an MBR is not provided, which is not the case raised by the OP.

I would cite this other paragraph in the same user manual section.
User manual section 9.7.1.2 wrote: In some configurations it may be necessary to change the MBR code of the created image. For example, to replace the Linux boot loader that is used on the host by another boot loader. This enables for example the guest to boot directly to Windows, while the host boots Linux from the "same" disk. For this purpose the -mbr option is provided. It specifies a file name from which to take the MBR code. The partition table is not modified at all, so a MBR file from a system with totally different partitioning can be used.
So it appears I was right the first time: VirtualBox honors the partition table in the MBR.
fth0
Volunteer
Posts: 5668
Joined: 14. Feb 2019, 03:06
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Linux, Windows 10, ...
Location: Germany

Re: vmdk parmeter "-mbr"

Post by fth0 »

I cited the same sentences (leaving out the example part), but it looks like I interpret them differently:
User manual section 9.7.1.2 wrote:It specifies a file name from which to take the MBR code.
VirtualBox takes the MBR code, not the partition table.
User manual section 9.7.1.2 wrote:The partition table is not modified at all, [...]
Here the partition table of the physical disk is meant.
User manual section 9.7.1.2 wrote:[...] so a MBR file from a system with totally different partitioning can be used
Used as a parameter for the -mbr option.

For example, you can take an MBR from any Windows XP installation with any partition table (or none at all) and provide it to VirtualBox, because the partition table within that MBR file is ignored. If you're not convinced, have a look into the VirtualBox source code, file src/VBox/Frontends/VBoxManage/VBoxInternalManage.cpp, function CmdCreateRawVMDK() and follow the variable pszMBRFilename.

But perhaps I'm misunderstanding what you mean:
mpack wrote:VirtualBox honors the partition table in the MBR.
Which MBR are you talking about. The one in the given MBR file or the one of the physical disk?
Last edited by fth0 on 10. Sep 2022, 19:10, edited 1 time in total.
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: vmdk parmeter "-mbr"

Post by mpack »

fth0 wrote: Which MBR are you talking about. The one in the given MBR file or the one of the physical disk?
The one in the MBR file, that being the whole point of the exercise!
fth0
Volunteer
Posts: 5668
Joined: 14. Feb 2019, 03:06
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Linux, Windows 10, ...
Location: Germany

Re: vmdk parmeter "-mbr"

Post by fth0 »

Ok, I think I understand both your and my reasoning, and both can be possible interpretations of the documentation. Finally, the VirtualBox source code decides what really happens. :)
lpmanjaro
Posts: 55
Joined: 13. May 2021, 18:51

Re: vmdk parmeter "-mbr"

Post by lpmanjaro »

Actually after reading other info (I had read vbox manual before posting.) I realized that since my disk is UEFI/GPT the -mbr parm probably has nothing to do with my reason for asking. IIRC UEFI in my PC's microcode doesn't use the mbr code installed IE (Core grub code below 2048).
fth0
Volunteer
Posts: 5668
Joined: 14. Feb 2019, 03:06
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Linux, Windows 10, ...
Location: Germany

Re: vmdk parmeter "-mbr"

Post by fth0 »

AFAICS, the VirtualBox (U)EFI BIOS can only boot from (virtual or raw) GPT disks, and the VirtualBox legacy BIOS can only boot from MBR disks. If you want to use raw disk access for the boot partition(s), you should use the (U)EFI BIOS for your VMs.
lpmanjaro
Posts: 55
Joined: 13. May 2021, 18:51

Re: vmdk parmeter "-mbr"

Post by lpmanjaro »

I do set the EFI flag in virtualbox settings for each VM client I set up. It works fine. My original question was in regard to the -mbr parm when creating the vmdk file. Clearly the -mbr parm would not apply to a uefi boot process so my question should be withdrawn. Sorry for the miss leading question, though it did spark a good discussion regarding mbr.
fth0
Volunteer
Posts: 5668
Joined: 14. Feb 2019, 03:06
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Linux, Windows 10, ...
Location: Germany

Re: vmdk parmeter "-mbr"

Post by fth0 »

Thanks for reporting back! :)
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: vmdk parmeter "-mbr"

Post by mpack »

lpmanjaro wrote:Clearly the -mbr parm would not apply to a uefi boot process
Technically true, but someone who understands VMDK and GPT could do something equivalent - which is what I assume is going on in the background with the -mbr case.

To clarify: a VMDK has a descriptor which lists one or more "extents" which are appended together virtually to make a complete disk image. Each extent can be the contents of a file, or direct access to a block device, or zero filled space for padding. Someone who knew what he was doing could provide the primary and backup GPT partition tables and potentially the EFI system (boot manager) partition too, to boot a host partition in isolation from the host OS.
Post Reply