Skip to content

Instantly share code, notes, and snippets.

@mjdorma
Last active September 16, 2019 11:40
Show Gist options
  • Save mjdorma/9044686 to your computer and use it in GitHub Desktop.
Save mjdorma/9044686 to your computer and use it in GitHub Desktop.
pyvbox: How to copy a file from a VM
"""pyvbox: How to copy a file from a VM.
"""
import os
import virtualbox
# Assume machine is already running.
vbox = virtualbox.VirtualBox()
machine = vbox.find_machine("win7")
session = machine.create_session()
# copy notepad.exe to ./notepad.exe
gs = session.console.guest.create_session("mick", "password")
gs.copy_from("C:\\Windows\\System32\\notepad.exe", "notepad.exe")
gs.close()
@z00m1n
Copy link

z00m1n commented Oct 28, 2015

This fails on my Mac OS X 10.10.5 / VirtualBox 5.0.8 r103449 / pyvbox 0.2.2 with

Traceback (most recent call last):
  File "./test.py", line 17, in <module>
    gs = session.console.guest.create_session("mick", "password")
  File "/Users/<username>/.virtualenvs/pyvbox/lib/python2.7/site-packages/virtualbox/library.py", line 24091, in console
    ret = self._get_attr("console")
  File "/Users/<username>/.virtualenvs/pyvbox/lib/python2.7/site-packages/virtualbox/library_base.py", line 156, in _get_attr
    attr = self._search_attr(name, prefix='get')
  File "/Users/<username>/.virtualenvs/pyvbox/lib/python2.7/site-packages/virtualbox/library_base.py", line 152, in _search_attr
    raise AttributeError("Failed to find attribute %s in %s" % (name, self))
AttributeError: Failed to find attribute console in <virtualbox.library_ext.session.ISession object at 0x106308f10>

How can I fix this ? Or is there an alternate way to boot up a VM ?

@z00m1n
Copy link

z00m1n commented Oct 29, 2015

Never mind, my bad: The error above occurs if the VM isn't running yet, so either starting the machine manually or programmatically by adding a line

progress = machine.launch_vm_process(session, 'gui', '')

fixes this.

However, no matter what file I try to copy, they all fail with e.g.

OSError: Failed to find /bin/nano on guest

although the file is clearly there.

@z00m1n
Copy link

z00m1n commented Oct 29, 2015

There's been an API change in the underlying VirtualBox version; quote from the PDF that comes as part of the VirtualBox SDK, page 390:

Renamed IGuestSession::copyTo to IGuestSession::fileCopyToGuest.
Renamed IGuestSession::copyFrom to IGuestSession::fileCopyFromGuest.

Using copy_to fails with

AttributeError: Failed to find attribute copyTo in <virtualbox.library_ext.guest_session.IGuestSession object at 0x107966050>

Patching the sources to call fileCopyToGuest instead of copyTo fixes this. However, the file still doesn't seem to get copied; no error message, but no file on the VM either.

copy_from fails with the error message mentioned earlier, despite the fact that the underlying method being called isn't there anymore. Maybe there's a bug in the exception handling somewhere.

Patching the sources to call fileCopyFromGuest instead of copyFrom doesn't fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment