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

Unable to create shared folder with pyvbox #48

Closed cacoyle closed 8 years ago

cacoyle commented 8 years ago
Python       : 2.7.5
pyvbox       : 1.0.0
Host OS      : CentOS 7.2 x64
VirtualBox   : VirtualBox-5.0-5.0.20_106931_el7-1.x86_64
Guest OS     : Windows 2012 R2 x64

Unable to create a shared folder between the Host and Guest using pyvbox, trying to accomplish the same result as the following command: vboxmanage sharedfolder add Windows2012_64_base --name exports --hostpath /tmp --automount

Code:

import virtualbox

vbox = virtualbox.VirtualBox()

vm = vbox.find_machine("Windows2012_64_base")

vm.create_shared_folder(
        name="export",
        host_path="/tmp",
        writable=True,
        automount=True
)

Output (Guest powered off)

Traceback (most recent call last):
  File "vbox_portion.py", line 81, in <module>
    automount=True
  File "/root/cc/packer/env/lib/python2.7/site-packages/virtualbox/library.py", line 12445, in create_shared_folder
    in_p=[name, host_path, writable, automount])
  File "/root/cc/packer/env/lib/python2.7/site-packages/virtualbox/library_base.py", line 172, in _call
    return self._call_method(method, in_p=in_p)
  File "/root/cc/packer/env/lib/python2.7/site-packages/virtualbox/library_base.py", line 198, in _call_method
    raise errobj
virtualbox.library.VBoxErrorInvalidVmState: 0x80bb0002 (The machine is not mutable or running (state is PoweredOff))

Output (Guest powered on)

Traceback (most recent call last):
  File "vbox_portion.py", line 81, in <module>
    automount=True
  File "/root/cc/packer/env/lib/python2.7/site-packages/virtualbox/library.py", line 12445, in create_shared_folder
    in_p=[name, host_path, writable, automount])
  File "/root/cc/packer/env/lib/python2.7/site-packages/virtualbox/library_base.py", line 172, in _call
    return self._call_method(method, in_p=in_p)
  File "/root/cc/packer/env/lib/python2.7/site-packages/virtualbox/library_base.py", line 198, in _call_method
    raise errobj
virtualbox.library.VBoxErrorInvalidVmState: 0x80bb0002 (The machine is not mutable or running (state is Running))

Let me know if there's any further detail I can provide, thanks!

cacoyle commented 8 years ago

Credit to @capsterx for figuring this out:

import virtualbox

vbox = virtualbox.VirtualBox()
vm = vbox.find_machine("Windows2012_64_base")
s = virtualbox.Session()
try:
  vm.lock_machine(s, virtualbox.library.LockType.shared)
  s.machine.create_shared_folder("export", "/tmp", True, True)
  s.machine.save_settings()
finally:
  s.unlock_machine()