/// <summary> /// Stop the VM's where the extradata key is set /// </summary> private void stopvms() { if (machines.Length == 0) { return; } foreach (VirtualBox.IMachine m in machines) { string xtrakeys = m.GetExtraData(this.extradatakey.ToString()); if (xtrakeys.ToLower() == "yes") { if (m.State == VirtualBox.MachineState.MachineState_Running) { this.EventLog.WriteEntry(String.Format("Stopping VM {0} ({1})", m.Name, m.Id)); VirtualBox.Session session = new VirtualBox.Session(); try { m.Parent.OpenExistingSession(session, m.Id); session.Console.PowerDown().WaitForCompletion(-1); session.Close(); } catch (Exception e) { this.EventLog.WriteEntry(String.Format("Error stopping VM {0} ({1})\r\n\r\n{2}\r\n\r\n{3}", m.Name, m.Id, e.ToString(), m.State), EventLogEntryType.Error); } } } } }
/// <summary> /// Start the VM's where the extradata key is set /// </summary> private void startvms() { if (machines.Length == 0) { return; } foreach (VirtualBox.IMachine m in machines) { string xtrakeys = m.GetExtraData(this.extradatakey.ToString()); if (xtrakeys.ToLower() == "yes") { if (m.State == VirtualBox.MachineState.MachineState_PoweredOff || m.State == VirtualBox.MachineState.MachineState_Saved) { this.EventLog.WriteEntry(String.Format("Starting VM {0} ({1})", m.Name, m.Id)); VirtualBox.Session session = new VirtualBox.Session(); try { VirtualBox.IProgress progress = m.Parent.OpenRemoteSession(session, m.Id, "vrdp", ""); progress.WaitForCompletion(-1); } catch (Exception e) { this.EventLog.WriteEntry(String.Format("Error starting VM {0} ({1})\r\n\r\n{2}", m.Name, m.Id, e.ToString()), EventLogEntryType.Error); } } } } }
/// <summary> /// Returns a Wox.Plugin.Result object corresponding to this machine. /// </summary> /// <param name="machine">The machine to resultify</param> public static Result ToResult(this VirtualBoxApi.IMachine machine) { return(new Result() { Title = machine.Name, SubTitle = machine.State.ToDisplayString(), IcoPath = Main.IconPath, Action = context => { // the machine cannot be launched if it's already online if (machine.Online()) { return false; } var session = new VirtualBoxApi.Session(); var progress = machine.LaunchVMProcess(session, "gui", string.Empty); /* * Although everything appears to work fine if we don't unlock the machine after launching, * the VirtualBox SDK reference (http://download.virtualbox.org/virtualbox/SDKRef.pdf) says * that we must unlock it (see the documentation of IMachine::launchVMProcess()). We do this * asynchronously to keep the Wox UI responsive. */ Task.Run(() => { progress.WaitForCompletion(10000); // 10s if (progress.ResultCode == 0) { session.UnlockMachine(); } }); return true; } }); }
public Machine(vbCOM.IMachine machine) { Name = machine.Name; comMachine = machine; MachineSession = new vbCOM.Session(); ID = machine.Id; MAC = machine.GetNetworkAdapter(0).MACAddress; //todo: handle multiple adapters CurrentProgress = null; }
public virtual ProgressToken Start() { Trace.Assert(getState() != VBoxWrapper.MachineState.Running); comVB.Session session = new comVB.Session(); comVB.IProgress progress = _comMachine.LaunchVMProcess(session, "headless", ""); return(new IProgressProgressToken(progress)); }
private void Button7_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(currentMachineName)) { MessageBox.Show("choose machine name first"); return; } VirtualBox.IVirtualBox vb = new VirtualBox.VirtualBoxClass(); var mc = vb.FindMachine(currentMachineName); session = new VirtualBox.SessionClass(); machine = mc; UpdateSnapshots(mc); }
private void button2_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(currentMachineName)) { MessageBox.Show("choose machine name first"); return; } VirtualBox.IVirtualBox vb = new VirtualBox.VirtualBoxClass(); var mc = vb.FindMachine(currentMachineName); session = new VirtualBox.SessionClass(); //s.UnlockMachine(); var ret = mc.LaunchVMProcess(session, "gui", ""); }
private void button4_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(currentMachineName)) { MessageBox.Show("choose machine name first"); return; } VirtualBox.IVirtualBox vb = new VirtualBox.VirtualBoxClass(); var mc = vb.FindMachine(currentMachineName); if (mc.State != MachineState.MachineState_Running) { MessageBox.Show("run machine first"); return; } session = new VirtualBox.SessionClass(); //s.UnlockMachine(); machine = mc; mc.LockMachine(session, VirtualBox.LockType.LockType_Shared); UpdateSnapshots(mc); }
public virtual ProgressToken Shutdown(ShutdownType type) { Trace.Assert(getState() == VBoxWrapper.MachineState.Running); comVB.Session session = acquireClientLock(); switch (type) { case ShutdownType.ACPI: return(acpiShutdown(session)); case ShutdownType.HardOff: return(hardOff(session)); case ShutdownType.SaveState: return(saveState(session)); default: throw new InvalidProgramException("ShutdownType unknown -> Normally never reached..."); } }
/// <summary> /// Stop the VM's where the extradata key is set /// </summary> private void stopvms() { if (machines.Length == 0) return; foreach(VirtualBox.IMachine m in machines) { string xtrakeys = m.GetExtraData(this.extradatakey.ToString()); if (xtrakeys.ToLower() == "yes") { if (m.State==VirtualBox.MachineState.MachineState_Running) { this.EventLog.WriteEntry(String.Format("Stopping VM {0} ({1})",m.Name,m.Id)); VirtualBox.Session session = new VirtualBox.Session(); try { m.Parent.OpenExistingSession(session, m.Id); session.Console.PowerDown().WaitForCompletion(-1); session.Close(); } catch (Exception e) { this.EventLog.WriteEntry(String.Format("Error stopping VM {0} ({1})\r\n\r\n{2}\r\n\r\n{3}",m.Name,m.Id,e.ToString(),m.State),EventLogEntryType.Error); } } } } }
public void Rename(string _old, string _new) { OnEvent("Finalizowanie instalacji", 1); if(Session != null && Session.State == SessionState.SessionState_Locked) Session.UnlockMachine(); Session tmps = new VirtualBox.Session(); try { IMachine mach = vb.FindMachine(_old); Thread.Sleep(1000); mach.LockMachine(tmps, LockType.LockType_Write); tmps.Machine.SaveSettings(); tmps.UnlockMachine(); mach.LockMachine(tmps, LockType.LockType_Write); tmps.Machine.Name = _new; tmps.Machine.SaveSettings(); tmps.UnlockMachine(); } catch (Exception) { //TODO: Remove Devel directory return; } finally { if(tmps.State==SessionState.SessionState_Locked) tmps.UnlockMachine(); } OnEvent("Zakończono instalację", 1); }
public string PowerOn(bool headless = true) { if (State == "Running") { return string.Format("{0} is already running", Name); } if (MachineSession == null) { MachineSession = new vbCOM.Session(); } string startupType = headless ? "headless" : ""; try { CurrentProgress = comMachine.LaunchVMProcess(MachineSession, startupType, ""); } catch (COMException e) { return e.Message; } return CurrentProgress.Description; //find out a better way of dermining success; }
private ProgressToken hardOff(comVB.Session session) { comVB.IProgress progress = session.Console.PowerDown(); return(new IProgressProgressToken(progress)); }
private comVB.Session acquireClientLock() { comVB.Session session = new comVB.Session(); //possible bug: Class can only be created, if running in according platform (Virtualbox running x64 => Service has to run also as x64!) _comMachine.LockMachine(session, comVB.LockType.LockType_Shared); return(session); }
private ProgressToken saveState(comVB.Session session) { comVB.IProgress progress = session.Console.SaveState(); return(new IProgressProgressToken(progress)); }
/// <summary> /// Start the VM's where the extradata key is set /// </summary> private void startvms() { if (machines.Length == 0) return; foreach(VirtualBox.IMachine m in machines) { string xtrakeys = m.GetExtraData(this.extradatakey.ToString()); if (xtrakeys.ToLower() == "yes") { if (m.State==VirtualBox.MachineState.MachineState_PoweredOff || m.State==VirtualBox.MachineState.MachineState_Saved) { this.EventLog.WriteEntry(String.Format("Starting VM {0} ({1})",m.Name,m.Id)); VirtualBox.Session session = new VirtualBox.Session(); try { VirtualBox.IProgress progress = m.Parent.OpenRemoteSession(session, m.Id, "vrdp", ""); progress.WaitForCompletion(-1); } catch (Exception e) { this.EventLog.WriteEntry(String.Format("Error starting VM {0} ({1})\r\n\r\n{2}",m.Name,m.Id,e.ToString()),EventLogEntryType.Error); } } } } }
public string PowerOff(bool immediate = false) { if (State == "PoweredOff") { return string.Format("{0} is already powered off", Name); } if (MachineSession == null) { MachineSession = new vbCOM.Session(); } comMachine.LockMachine(MachineSession, vbCOM.LockType.LockType_Shared); if (immediate) { CurrentProgress = MachineSession.Console.PowerDown(); return CurrentProgress.Description; //todo: test this } MachineSession.Console.PowerButton(); return string.Format("Power button event sent: {0}", MachineSession.Console.GetPowerButtonHandled() > 0); }
private ProgressToken acpiShutdown(comVB.Session session) { session.Console.PowerButton(); return(new MachineEventProgressToken(session.Console, x => comVB.MachineState.MachineState_PoweredOff.Equals(x.State))); }