通过VirtualBox Java API恢复快照时出现问题

时间:2017-06-08 23:34:01

标签: java virtualbox restore snapshot locked

我想恢复VirtualBox VM的快照,然后立即启动带有快照的VM。 我使用VirtualBox API在Java中编写,我在Windows中使用VBoxWebSrv.exe。

大部分时间恢复和开始工作,但有时我得到以下异常:

Exception in thread "main" org.virtualbox_5_1.VBoxException: VirtualBox error: rc=0x80bb0007 The machine 'win7test' is already locked by a session (or being locked or unlocked) (0x80bb0007)

执行以下行时发生异常:     vm.launchVMProcess(session," gui","");

我在应用程序完成时调用 session.unlockMachine() ,因此机器不应再被锁定。

这是我的代码(我标记了上述行):

public static void main(String[] args) {

    // Number of Snapshot to restore
    int snapNo = 3;
    snapshotUUID = UUIDs[snapNo];


    System.out.println("CONNECT...");

    // Connect to VboxWebSrv
    VirtualBoxManager mgr = VirtualBoxManager.createInstance(null);
    mgr.connect("http://localhost:18083", "MY_WINDOWS_USERNAME", "MY_WINDOWS_PASSWORD");

    // Get VM by name
    IMachine vm = mgr.getVBox().findMachine("win7test");
    // get Session
    ISession session = mgr.getSessionObject();

    // Lock VM to Session
    vm.lockMachine(session, LockType.Shared);

    IProgress progress;



    // ------------ STOP VM ------------

    // get Console to manage the VM
    IConsole console = session.getConsole();

    if (console != null) {
        // STOP VM
        progress = console.powerDown();
        progress.waitForCompletion(25000);
    }



    // ------------ RESTORE SNAPSHOT ------------

    // get Snapshot by UUID
    ISnapshot snapshot = vm.findSnapshot(snapshotUUID);

    progress = session.getMachine().restoreSnapshot(snapshot);
    progress.waitForCompletion(25000);

    try {
        System.out.println("unlock machine...");
        // Put this in a try-catch-block because sometimes this caused an exception too during some tests
        session.unlockMachine();

    } catch (Exception e) {
        e.printStackTrace();
    }



    // ------------ START VM ------------

    progress = vm.launchVMProcess(session, "gui", ""); // <<<<< HERE THE EXCEPTION OCCURS!
    progress.waitForCompletion(25000);

    // Unlock VM
    session.unlockMachine();

    System.out.println("DONE...");

}

0 个答案:

没有答案