To automatically configure the correct graphics controller for a VirtualBox VM when starting it with VBoxManage, you can use a combination of VM OS type detection and predefined settings. Here's how to implement this:
1. Determine the Default Graphics Controller by OS Type
VirtualBox assigns default graphics controllers based on the guest OS type specified during VM creation:
VBoxSVGA: Default for Linux and Windows 7+[1][5][7].
VMSVGA: Default for Linux guests (emulates VMware SVGA)[1][7].
VBoxVGA: Legacy option for older Windows (pre-7) and Solaris[1][5].
Use VBoxManage list ostypes to see OS identifiers, then set the graphics controller dynamically:
bash
VM_NAME="your_vm_name"
OS_TYPE=$(VBoxManage showvminfo "$VM_NAME" --machinereadable | grep '^ostype=' | cut -d'"' -f2)
case "$OS_TYPE" in
"Windows10"|"Windows11"|"Linux_64"|"Ubuntu_64")
VBoxManage modifyvm "$VM_NAME" --graphicscontroller VBoxSVGA
;;
"WindowsXP"|"Windows2003")
VBoxManage modifyvm "$VM_NAME" --graphicscontroller VBoxVGA
;;
*)
# Fallback to VMSVGA for VMware compatibility or Linux
VBoxManage modifyvm "$VM_NAME" --graphicscontroller VMSVGA
;;
esac
VBoxManage startvm "$VM_NAME" --type gui
2. Handle EFI vs. Legacy Boot Modes
Some graphics controllers have compatibility issues with EFI:
VBoxSVGA: May cause a black screen in EFI mode[1].
VMSVGA: Works in EFI but may require driver adjustments[4][8].
Check the VM’s firmware setting and adjust if needed:
bash
# Set firmware to BIOS for VBoxSVGA compatibility
VBoxManage modifyvm "$VM_NAME" --firmware bios
3. Ensure Guest Additions Are Installed
Certain controllers (e.g., VBoxSVGA, VMSVGA) require Guest Additions for features like auto-resize and 3D acceleration[1][5][8]:
bash
# Install Guest Additions from the host’s ISO
VBoxManage storageattach "$VM_NAME" --storagectl "IDE Controller" --port 0 --device 0 --type dvddrive --medium /usr/share/virtualbox/VBoxGuestAdditions.iso
4. Troubleshooting Common Issues
Invalid Settings Error: Occurs if the OS type and graphics controller mismatch (e.g., using VBoxVGA for Windows 10)[3]. Recheck the OS type with VBoxManage showvminfo.
Resolution Problems: For VMSVGA, install xserver-xorg-video-vmware on Linux guests[4][8].
3D Acceleration: Enable it in VM settings for VBoxSVGA/VMSVGA[5][7]:
bash
VBoxManage modifyvm "$VM_NAME" --accelerate3d on
By combining OS type detection, firmware settings, and Guest Additions, you can automate graphics controller selection while ensuring optimal performance.