mitchellh / virtualbox

[ABANDONED] Create and modify virtual machines in VirtualBox using pure ruby.
http://mitchellh.github.com/virtualbox/
MIT License
245 stars 45 forks source link

Support addStorageController #64

Closed hh closed 13 years ago

hh commented 13 years ago

I think we are very close to getting addStorageController working, so I wanted to open this issue which has stopped forward motion on passionengine/ii#2

When running:

vbox.interface.add_storage_controller 'My IDE Controller', :ide

We get an InvalidVMStateException.

lib/virtualbox/com/implementer/ffi.rb:106:in `call_and_check': Error in API call to add_storage_controller: 2159738882
(VirtualBox::Exceptions::InvalidVMStateException)
    from ///lib/virtualbox/com/implementer/ffi.rb:80:in `call_vtbl_function'
    from ///lib/virtualbox/com/implementer/ffi.rb:61:in `call_function'
    from ///lib/virtualbox/com/abstract_interface.rb:145:in `call_function'
    from ///lib/virtualbox/com/abstract_interface.rb:62:in `add_storage_controller'
    from diskcreate.rb:3

Looking at the vboxshell.py source:

def attachCtr(ctx,mach,args):
    [name, bus, type] = args
    ctr = mach.addStorageController(name, bus)
    if type != None:
      ctr.controllerType = type

Looks like they are using the IMachine::addStorageController xpcom interface.

If you execute these commands, you can add and remove a simple ide controller. (Assuming you don't already have one present)

VBoxManage storagectl myvboxname --name myide --add ide
VBoxManage storagectl myvboxname --name myide --remove
/usr/lib/virtualbox/vboxshell.py -c "attachCtr myvboxname myide IDE PIIX4"
/usr/lib/virtualbox/vboxshell.py -c "detachCtr myvboxname myide"

They don't seem to get the box into an invalid state, but I suspect they approach saving state a bit differently than we do.

hh commented 13 years ago

I just figured out that you need to do some setting changes with_open_session:

newhd=VirtualBox::HardDrive.new
newhd.location="newhd.vdi"
newhd.logical_size=10*1000000

controller_name='My IDE Controller'
vbox.with_open_session do |session|
  machine = session.machine
  machine.add_storage_controller controller_name, :ide
  machine.attach_device(controller_name, 0, 0, :hard_disk, newhd.interface)
end

vbox.storage_controllers[0].controller_type = :ich6 #:piix4
vbox.save