Page 1 of 1

[Solved] File operations not working using python API

Posted: 29. Oct 2018, 09:11
by LordKaoS
Host: Windows 64
Guest: Ubuntu 64
Version: 5.2.20
Guest Additions: 5.2.20

Code: Select all

import vboxapi
SOURCE_PATH = r"K:\Downloads\first.sh"
DEST_PATH = r"/home/myuser/Downloads/first.sh"
UNAME = "myuser"
PASSWD = "mypass"
MACH_NAME = "Test"
virtualBoxManager = vboxapi.VirtualBoxManager(None, None)
vbox = virtualBoxManager.getVirtualBox()
todo = vbox.findMachine(MACH_NAME)
session = virtualBoxManager.getSessionObject(vbox)
progress = todo.launchVMProcess(session, "headless", None)
progress.waitForCompletion(-1)
console = session.console
runner = console.guest.createSession(UNAME, PASSWD, "", "")
print(runner.waitFor(virtualBoxManager.constants.GuestSessionWaitForFlag_Start, 0))
copy = runner.fileCopyToGuest(SOURCE_PATH, DEST_PATH, None)
copy.waitForCompletion(-1)
print(runner.fileExists(DEST_PATH, False))
console.powerDown()
# session.unlockMachine()
# virtualBoxManager.closeMachineSession(session)
File does not get copied. fileExists() doesn't work, raises
Traceback (most recent call last):
File "K:\WorkSpace\GOC\VBinit.py", line 18, in <module>
print(runner.fileExists(DEST_PATH, False))
File "C:\Users\user\AppData\Local\Temp\gen_py\3.7\D7569351-1750-46F0-936E-BD127D5BC264x0x1x3.py", line 3902, in FileExists
, aFollowSymlinks)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'GuestSessionWrap', 'Querying file information for "/home/myuser/Downloads/first.sh" failed: VERR_BROKEN_PIPE', None, 0, -2135228411), None)

Re: File operations not working using python API

Posted: 29. Oct 2018, 14:25
by noteirak
You do not check for guest additions capabilities and is most likely why it fails: your timing might not be right.
See the code sample for the high-level steps. Adapt to your version.

Re: File operations not working using python API

Posted: 31. Oct 2018, 20:37
by LordKaoS
Thanks for the help. I'll try and see if I can make it work

Re: File operations not working using python API

Posted: 8. Nov 2018, 16:38
by LordKaoS
LordKaoS wrote:Thanks for the help. I'll try and see if I can make it work
My solution, for anyone who comes looking.

Code: Select all

import vboxapi
from time import time
SOURCE_PATH = r"K:\Downloads\first.sh"
DEST_PATH = r"/home/myuser/Downloads/first.sh"
UNAME = "myuser"
PASSWD = "mypass"
MACH_NAME = "Test"
virtualBoxManager = vboxapi.VirtualBoxManager(None, None)
try:
    vbox = virtualBoxManager.getVirtualBox()
    todo = vbox.findMachine(MACH_NAME)
    alreadyRunning = todo.state is not virtualBoxManager.constants.MachineState_PoweredOff
    print("Machine ", "started" if alreadyRunning else "starting")
    session = virtualBoxManager.getSessionObject(vbox)
    if not alreadyRunning:
        progress = todo.launchVMProcess(session, "headless", None)
    else:
        todo.lockMachine(session, virtualBoxManager.constants.LockType_Shared)
    try:
        if not alreadyRunning:
            progress.waitForCompletion(-1)
            if progress.resultCode is not 0:
                raise Exception("Machine failed to start: ",
                                progress.errorInfo.text)
        print("Machine locked")
        start = time()
        while session.console.guest.additionsRunLevel is not virtualBoxManager.constants.AdditionsRunLevelType_Userland and time() - start < 180:
            pass
        if not time() - start < 180:
            raise Exception("OS took too long to load")
        print("OS loaded in ", time() - start, "sec")
        runner = session.console.guest.createSession(UNAME, PASSWD, None, None)
        try:
            print("Session created")
            runner.waitFor(
                virtualBoxManager.constants.GuestSessionWaitForFlag_Start, 30 * 1000)
            if runner.status is not virtualBoxManager.constants.GuestSessionStatus_Started:
                raise Exception(
                    "Guest session did not start after ", 30, " sec")
            copy = runner.fileCopyToGuest(SOURCE_PATH, DEST_PATH, None)
            copy.waitForCompletion(-1)
            if not runner.fileExists(DEST_PATH, False):
                raise Exception("Copying failed")
        except Exception as err:
            print(str(err))
        finally:
            runner.close()
            session.console.powerButton()
    except Exception as err:
        print(str(err))
    finally:
        session.unlockMachine()
finally:
    print('Done')
Marking as [Solved]

Re: [Solved] File operations not working using python API

Posted: 8. Nov 2018, 17:18
by socratis
LordKaoS wrote:My solution, for anyone who comes looking.
LordKaoS wrote:Marking as [Solved]
Impressive, in your first post! Kudos, both for sharing the solution and marking it as solved! 8)