[Solved] File operations not working using python API

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
LordKaoS
Posts: 11
Joined: 29. Oct 2018, 09:04
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Ubuntu 18.04, MS Windows 7

[Solved] File operations not working using python API

Post 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)
Last edited by LordKaoS on 8. Nov 2018, 16:40, edited 1 time in total.
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: File operations not working using python API

Post 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.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
LordKaoS
Posts: 11
Joined: 29. Oct 2018, 09:04
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Ubuntu 18.04, MS Windows 7

Re: File operations not working using python API

Post by LordKaoS »

Thanks for the help. I'll try and see if I can make it work
LordKaoS
Posts: 11
Joined: 29. Oct 2018, 09:04
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Ubuntu 18.04, MS Windows 7

Re: File operations not working using python API

Post 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]
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

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

Post 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)
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
Post Reply