private void MenuItemDoubleClick(object sender, EventArgs e) { var obj = (ToolStripMenuItem)sender; Session ses = new Session(); IMachine vm = vbox.FindMachine(obj.Tag.ToString()); if (vm.SessionState == SessionState.SessionState_Locked) { vm.LockMachine(ses, LockType.LockType_Shared); IProgress vmProgress = ses.Machine.SaveState(); CatchStatus(obj, vmProgress, vm); if (vm.SessionState == SessionState.SessionState_Locked) { ses.UnlockMachine(); } } else { IProgress vmProgress = vm.LaunchVMProcess(ses, "headless", new string[0]); CatchStatus(obj, vmProgress, vm); if (vm.SessionState == SessionState.SessionState_Locked) { ses.UnlockMachine(); } } }
private void Start() { try { var process = machine.LaunchVMProcess(session, guiType, ""); process.WaitForCompletion(-1); } catch (COMException e) { if (e.Message.Contains("is already locked by a session")) { Process.GetProcessesByName("VBoxSVC").Each(p => p.Kill()); Process.GetProcessesByName("VirtualBox").Each(p => p.Kill()); Thread.Sleep(3000); Init(); var process = machine.LaunchVMProcess(session, guiType, ""); process.WaitForCompletion(-1); } } }
/// <summary> /// Start server. /// </summary> /// <param name="server">Server</param> public void StartServer(Server server) { if (!machines.ContainsKey(server)) { throw new VirtualBoxServerException("Server " + server.GetName() + " is not registered."); } if (server.GetStatus() == Status.RUNNING) { return; } else if (server.GetStatus() != Status.POWEREDOFF) { throw new VirtualBoxServerException("Server " + server.GetName() + " is not powered off."); } try { IMachine machine = machines[server]; Session serverSession = getSession(server); DateTime startTime = DateTime.Now; while (machine.SessionState != SessionState.SessionState_Unlocked) { long ticks = DateTime.Now.Ticks - startTime.Ticks; if (ticks >= (WAIT_FOR_RESTART_SERVER_SECONDS * 10000000)) { break; } } if (machine.SessionState == SessionState.SessionState_Unlocked) { server.SetStarting(); IProgress progress = machine.LaunchVMProcess(serverSession, "headless", new string[0]); } } catch { throw new VirtualBoxServerException("Server " + server.GetName() + " can't be run."); } }
public override void StartVM(string mode) { Session session = new SessionClass(); if (machine.State == MachineState.MachineState_PoweredOff || machine.State == MachineState.MachineState_Aborted || machine.State == MachineState.MachineState_Saved) { var prog = machine.LaunchVMProcess(session, mode, ""); prog.WaitForCompletion(10000); while (machine.State != MachineState.MachineState_Running) { Thread.Sleep(100); } if (session.State == SessionState.SessionState_Locked) { session.UnlockMachine(); } } else if (machine.State != MachineState.MachineState_Running) { throw new Exception(machine.Name + " cannot be launched, because the current status is not Poweredoff or Aborted"); } }
public void RunTests() { ContinueType ret = ContinueType.EXIT_DONT_CONTINUE; IProgress vmProgress; // TODO: Load settings /* Open the testing machine */ Session vmSession = new Session(); try { rosVM = vBox.FindMachine(machineName); } catch (COMException exc) { /* Opening failed. Probably we need to create it */ Console.WriteLine("Opening the vm failed: " + exc); rosVM = CreateVm(); } rosVM.LockMachine(vmSession, LockType.LockType_Write); /* Configure the virtual machine */ ConfigVm(vmSession); /* Empty or create the HDD, prepare for the first run */ EmptyHardDisk(vmSession); /* Close VM session */ vmSession.UnlockMachine(); /* Empty the debug log file */ if (logName != null) { EmptyDebugLog(); } /* Start main testing loop */ for (int stage = 0; stage < numStages; stage++) { int retries; for (retries = 0; retries < maxRetries; retries++) { /* Start the VM */ try { vmProgress = rosVM.LaunchVMProcess(vmSession, "gui", null); vmProgress.WaitForCompletion(-1); if (vmProgress.ResultCode != 0) { /* Print out error's text */ Console.WriteLine("Error starting VM: " + vmProgress.ErrorInfo.Text); /* Close VM session */ if (vmSession.State == SessionState.SessionState_Locked) { vmSession.UnlockMachine(); } break; } Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("[SYSREG] Running stage {0}...", stage + 1); Console.WriteLine("[SYSREG] Domain {0} started.\n", rosVM.Name); ret = ProcessDebugOutput(vmSession, stage); /* Kill the VM if it's not already powered off */ if (vmSession.State != SessionState.SessionState_Unlocked && rosVM.State >= MachineState.MachineState_FirstOnline && rosVM.State <= MachineState.MachineState_LastOnline) { #if TRACE Console.WriteLine("[SYSREG] Killing VM (state " + rosVM.State.ToString() + ")"); #endif try { vmProgress = vmSession.Console.PowerDown(); vmProgress.WaitForCompletion(-1); } catch (System.Runtime.InteropServices.COMException comEx) { Console.WriteLine("[SYSREG] Failed to shutdown VM: " + comEx.ToString()); if (rosVM.State != MachineState.MachineState_PoweredOff) { throw; } } } try { /* Close the VM session without paying attention to any problems */ if (vmSession.State == SessionState.SessionState_Locked) { vmSession.UnlockMachine(); /* Wait till the machine state is actually closed (no vmProgress alas) */ int waitingTimeout = 0; while (vmSession.State != SessionState.SessionState_Unlocked || waitingTimeout < 5) { Thread.Sleep(1000); waitingTimeout++; } } } catch { } /* If we have a checkpoint to reach for success, assume that * the application used for running the tests (probably "rosautotest") * continues with the next test after a VM restart. */ if (stage == 2 && ret == ContinueType.EXIT_CONTINUE) { Console.WriteLine("[SYSREG] Rebooting VM (retry {0})", retries + 1); } else { /* Empty the debug log file */ if (logName != null) { EmptyDebugLog(); } break; } } catch (Exception exc) { Console.WriteLine("[SYSREG] Running the VM failed with exception: " + exc); //break; } } /* Check for a maximum number of retries */ if (retries >= maxRetries) { Console.WriteLine("[SYSREG] Maximum number of allowed retries exceeded, aborting!"); break; } /* Stop executing if asked so */ if (ret == ContinueType.EXIT_DONT_CONTINUE) { break; } } switch (ret) { case ContinueType.EXIT_CHECKPOINT_REACHED: Console.WriteLine("[SYSREG] Status: Reached the checkpoint!"); Environment.ExitCode = 0; break; case ContinueType.EXIT_CONTINUE: Console.WriteLine("[SYSREG] Status: Failed to reach the checkpoint!!"); break; case ContinueType.EXIT_DONT_CONTINUE: Console.WriteLine("[SYSREG] Status: Testing process aborted!"); break; } }