sethmlarson / virtualbox-python

Complete implementation of VirtualBox's COM API with a Pythonic interface.
https://pypi.org/project/virtualbox
Apache License 2.0
354 stars 75 forks source link

Can not attach Medium to new created Machine #105

Closed brean closed 6 years ago

brean commented 6 years ago
ENVIRONMENT
SUMMARY

I want to create a whole VM from scratch using just python, I don't know how/when to create my medium. When I run attach_device before I register the machine I get "0x80020009 (Cannot attach storage devices to an unregistered machine)" and when I run it after register_machine I get the error message "0x80020009 (The machine is not mutable or running (state is PoweredOff))" although I create and lock a session.

STEPS TO REPRODUCE

Create a new machine, attach storage controller and call machine.attach_device. For my exact code take a look at this as well as this config

EXPECTED RESULTS

I should be able to attach the device somehow either before or after I register the machine.

ACTUAL RESULTS
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\virtualbox\library_base.py", line 195, in _call_method
    ret = method(*in_params)
  File "C:\Users\user\AppData\Local\Temp\gen_py\3.6\D7569351-1750-46F0-936E-BD127D5BC264x0x1x3.py", line 4937, in AttachDevice
    , aControllerPort, aDevice, aType, aMedium)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'MachineWrap', 'The machine is not mutable or running (state is PoweredOff)', None, 0, -2135228414), None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "install_xenial_virtualbox.py", line 92, in <module>
    main()
  File "install_xenial_virtualbox.py", line 88, in main
    install(**config)
  File "install_xenial_virtualbox.py", line 73, in install
    attach_device(machine=machine, port=port, **m)
  File "install_xenial_virtualbox.py", line 32, in attach_device
    machine.attach_device(name, port, 0, device_type, om)
  File "C:\ProgramData\Anaconda3\lib\site-packages\virtualbox\library.py", line 11289, in attach_device
    in_p=[name, controller_port, device, type_p, medium])
  File "C:\ProgramData\Anaconda3\lib\site-packages\virtualbox\library_base.py", line 186, in _call
    return self._call_method(method, in_p=in_p)
  File "C:\ProgramData\Anaconda3\lib\site-packages\virtualbox\library_base.py", line 213, in _call_method
    raise errobj
virtualbox.library_base.VBoxError: 0x80020009 (The machine is not mutable or running (state is PoweredOff))
sethmlarson commented 6 years ago

Have you tried calling the methods on session.machine instead of machine?

sethmlarson commented 6 years ago

Also have you tried create_session(LockType.write)?

brean commented 6 years ago

I tried create_session(LockType.write) which did not work but using session.machine instead of machine did the trick. Thank you very much!

sethmlarson commented 6 years ago

I'm glad I was able to help. :)