Python virtualbox api cannot add disk to SATA controller
Question: I'm having a hard time to ass a disk to a SATA controller with virtualbox API
when using a SAS controller everything works great but here I have a huge
traceback that I do not understand. Do I have to do something special with the
SATA controller? Thanks for helping

cheers,

    
    
    import time
    import os
    import virtualbox
    from virtualbox.library import StorageBus, IMachine
    from virtualbox.library import IStorageController, LockType
    from virtualbox.library import DeviceType, MediumVariant
    from virtualbox.library import VBoxErrorObjectNotFound
    from virtualbox.library import IStorageController, LockType, IVirtualBox
    from virtualbox.library import IVirtualBox, AccessMode
    
    session = virtualbox.Session()
    sup = virtualbox.VirtualBox().find_machine("test_machine")
    sup.lock_machine(session,LockType.write)
    current_interface = IVirtualBox()
    medium = current_interface.create_hard_disk("VDI", "/home/luffy/mine.vdi")
    progress = medium.create_base_storage(1024*1024, [MediumVariant.fixed])
    progress.wait_for_completion()
    opened_medium = current_interface.open_medium("/home/luffy/mine.vdi", DeviceType.hard_disk, AccessMode.read_write,False)
    session.machine.attach_device("SAS",2, 0, DeviceType.hard_disk,opened_medium) # This one works
    session.machine.attach_device("SATA",2 ,0 ,DeviceType.hard_disk,opened_medium)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python2.7/dist-packages/virtualbox/library.py", line 10264, in attach_device
        in_p=[name, controller_port, device, type_p, medium])
      File "/usr/local/lib/python2.7/dist-packages/virtualbox/library_base.py", line 173, in _call
        return self._call_method(method, in_p=in_p)
      File "/usr/local/lib/python2.7/dist-packages/virtualbox/library_base.py", line 199, in _call_method
        raise errobj
    virtualbox.library.OleErrorInvalidarg: 0x80070057 (The port and/or device parameter are out of range: port=2 (must be in range [0, 0]), device=0 (must be in range [0, 0]))
    #session.machine.save_settings()
    session.unlock_machine()
    


Answer: First you have to set the max port count and then add the SATA hard disk

    
    
    IStorageController sc = machine.getStorageControllerByName("SATA");
    sc.setPortCount(sc.getMaxPortCount());
    

