Scripting VM create with uefi boot entries

This is for discussing general topics about how to use VirtualBox.
Post Reply
ctxnop
Posts: 2
Joined: 7. Apr 2021, 14:19
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: gnu/linux

Scripting VM create with uefi boot entries

Post by ctxnop »

Hello,

First of all, some context:
I'm working in embedded software. We are building a Linux using yocto and flash it to some embedded devices.
For some reasons I can't change, yocto build a tar.gz that we have to uncompress instead of a .img file to dd.
Our workstations are windows 10, on which we have a virtualbox 6.1.16 r140961.
I can't change that as well (the workstations are provided to us as is by another service).
There is almost nothing installed on the workstation and I can't install anything (obviously...)

I'm trying to make a script that is able to create a VirtualBox's VM that mimic the embedded hardware as close as possible.
The goal is to provide the developers a VM that they can use to deploy and test most of their code on, without risking to brick the expensive hardware.
Those developers know nothing about yocto, the OS it-self or even the hardware for some of them, and they should not need to know.
This is why I'm want to write a script that basically create a VM, install the OS on it, and send the VM to the developer so that he/she only has to add the VM to his/her VirtualBox.

My current script is (simplified):
  1. dd if=/dev/zero of=disk.img
  2. parted --script disk.img mkpart primary (...)
  3. losetup --find --partscan disk.img
  4. mkfs.fat -F32 /dev/loop0p1; mkfs.ext4 /dev/loop0p2; ...
  5. mount /dev/loop0p2 /mnt/my-vm; mount /dev/loop0p1 /mnt/my-vm/boot; ...
  6. tar xvzf my-os.tar.gz -C /mnt/my-vm
  7. umount /mnt/my-vm/boot; ...; umount /mnt/my-vm;
  8. losetup -d /dev/loop0
  9. vboxmanage convertfromraw disk.img my-vm.vdi --format VDI
So now I have a VDI file which is correct, it contains the OS and if I create a VM manually and set the efi boot entry using efi shell I can boot it.

Now the issue:
I can play with vboxmanage createvm/modifyvm to create a VM and tweak it to my needs. Enabling EFI isn't an issue.
But I didn't find any way to set the boot entries. I have to run the VM, which boot into UEFI shell, then manually add the boot entries using "bcfg boot add".
Is there a way to make this step part of my script?
Also, keep in mind that my script is running inside a linux guest, I would like to avoid VM-in-VM if possible. Also, the VM my script is creating will be run by a windows host (in case that matter).

Bonus questions:
- I would love to avoid running a linux guest to to all of that, but can't find any way to do most steps using windows (dd, parted, losetup, mount, etc...). Do you know another way to achieve this?
- I can't fine tune the VM using createvm, so I have to createvm first then modifyvm. But the modifyvm only works on registered VM, wich I don't want. So I have to unregistervm after I'm done. Is there a better way?

Having a --default flag on createvm but not accepting all the flags accepted by modifyvm feels odd to me, because it means that you have to create the vm then modify it, instead of directly create it with the settings you need.
It also feels weird to have a --register flag as it means that by default it's not registered and gives the impression that registering is not mandatory. But then all the other commands works only on registered VM...
Post Reply