def remote_start_guest_machine(): """ Starts the virtual machine. If it was not running before, it waits 50 seconds for the system to boot up. """ mgr = VirtualBoxManager(None, None) vbox = mgr.vbox log_host(f"Running VirtualBox version {vbox.version}") machine = vbox.findMachine(commons.VM_NAME) session = mgr.getSessionObject(vbox) if machine.state == mgr.constants.all_values('MachineState')['Running']: log_host('VM is already running') elif machine.state == mgr.constants.all_values('MachineState')['PoweredOff'] or \ machine.state == mgr.constants.all_values('MachineState')['Saved']: # TODO split this condition progress = machine.launchVMProcess(session, 'gui', '') progress.waitForCompletion(-1) log_host('VM succesfully started') log_host('Waiting for boot') # Waiting till virtual machine is ready to accept SSH connection time.sleep(50) # TODO try ping else: machine_state = str(machine.state) raise RuntimeError('Unexpected virtual machine state')
def startVM(vmname): from vboxapi import VirtualBoxManager mgr = VirtualBoxManager(None, None) vbox = mgr.getVirtualBox() name = vmname mach = vbox.findMachine(name) session = mgr.getSessionObject(vbox) progress = mach.launchVMProcess(session, "gui", []) progress.waitForCompletion(-1) mgr.closeMachineSession(session)
def stopVM(vmname): from vboxapi import VirtualBoxManager mgr = VirtualBoxManager(None, None) vbox = mgr.getVirtualBox() name = vmname mach = vbox.findMachine(name) session = mgr.getSessionObject(vbox) vboxConstants = mgr.constants mach.lockMachine(session, vboxConstants.LockType_Shared) console = session.console console.powerDown() session.unlockMachine()
class VirtualBoxController : machineName = "android_x86" def __init__(self) : self.mgr = VirtualBoxManager(None,None) self.vbox = self.mgr.vbox def createMachine(self,userName) : exMachine = self.vbox.findMachine(self.machineName) result = self.vbox.createMachine("",self.machineName + "_" + userName,None,'Linux',"") #result.setSettingsFilePath(exMachine.settingsFilePath) print "'%s" %(result) def findMachine(self,machineNum) : try : mach = self.vbox.findMachine(self.machineName + "_" + machineNum) #mach = self.vbox.findMachine(self.machineName + "_" + userName) return mach except pywintypes.com_error : nMachine = MachineCreator() nMachine.createNewMachine(int(machineNum)) mach = self.vbox.findMachine(self.machineName + "_" + machineNum) #print "WRONG MACHINE NAME" return mach def checkAllMachines(self) : for machines in self.mgr.getArray(self.vbox,'machines') : print "Machine '%s' " %(machines.name) def getSession(self) : self.session = self.mgr.getSessionObject(self.vbox) def makeProcess(self,mach) : self.process = mach.launchVMProcess(self.session,"gui","") self.process.waitForCompletion(-1) def closeSession(self) : self.mgr.closeMachineSession(self.session) def connectVmSocket(self,vmIp,vmPort) : mVmSocket = VmSocket(vmIp,vmPort) mVmSocket.bindSocket() mVmSocket.aceeptVm() vmReturn = mVmSocket.waitVm() print "VmReturn " + vmReturn if "Vm_Boot_Complete" in vmReturn : return True else : return False
class VirtualBox(): # Hypervisor def __init__(self): self.mgr = VirtualBoxManager(None, None) self.vbox = self.mgr.vbox def getsession(self, vm): try: mach = self.vbox.findMachine(vm) session = self.mgr.getSessionObject(mach) mach.lockMachine(session, 1) return session except Exception, e: print e return "error"
def main(argv): from vboxapi import VirtualBoxManager # This is a VirtualBox COM/XPCOM API client, no data needed. mgr = VirtualBoxManager(None, None) # Get the global VirtualBox object vbox = mgr.getVirtualBox() print "Running VirtualBox version %s" % (vbox.version) # Get all constants through the Python manager code vboxConstants = mgr.constants # Enumerate all defined machines for mach in mgr.getArray(vbox, 'machines'): try: # Be prepared for failures - the VM can be inaccessible vmname = '<inaccessible>' try: vmname = mach.name except Exception, e: None vmid = '' try: vmid = mach.id except Exception, e: None # Print some basic VM information even if there were errors print "Machine name: %s [%s]" % (vmname, vmid) if vmname == '<inaccessible>' or vmid == '': continue # Print some basic VM information print " State: %s" % (enumToString( vboxConstants, "MachineState", mach.state)) print " Session state: %s" % (enumToString( vboxConstants, "SessionState", mach.sessionState)) # Do some stuff which requires a running VM if mach.state == vboxConstants.MachineState_Running: # Get the session object session = mgr.getSessionObject() # Lock the current machine (shared mode, since we won't modify the machine) mach.lockMachine(session, vboxConstants.LockType_Shared) # Acquire the VM's console and guest object console = session.console guest = console.guest # Retrieve the current Guest Additions runlevel and print # the installed Guest Additions version addRunLevel = guest.additionsRunLevel print " Additions State: %s" % (enumToString( vboxConstants, "AdditionsRunLevelType", addRunLevel)) if addRunLevel != vboxConstants.AdditionsRunLevelType_None: print " Additions Ver: %s" % (guest.additionsVersion) # Get the VM's display object display = console.display # Get the VM's current display resolution + bit depth + position screenNum = 0 # From first screen (screenW, screenH, screenBPP, screenX, screenY, _) = display.getScreenResolution(screenNum) print " Display (%d): %dx%d, %d BPP at %d,%d" % ( screenNum, screenW, screenH, screenBPP, screenX, screenY) # We're done -- don't forget to unlock the machine! session.unlockMachine()
def main(argv): from vboxapi import VirtualBoxManager # This is a VirtualBox COM/XPCOM API client, no data needed. mgr = VirtualBoxManager(None, None) # Get the global VirtualBox object vbox = mgr.getVirtualBox() print "Running VirtualBox version %s" %(vbox.version) # Get all constants through the Python manager code vboxConstants = mgr.constants # Enumerate all defined machines for mach in mgr.getArray(vbox, 'machines'): try: # Be prepared for failures - the VM can be inaccessible vmname = '<inaccessible>' try: vmname = mach.name except Exception, e: None vmid = ''; try: vmid = mach.id except Exception, e: None # Print some basic VM information even if there were errors print "Machine name: %s [%s]" %(vmname,vmid) if vmname == '<inaccessible>' or vmid == '': continue # Print some basic VM information print " State: %s" %(enumToString(vboxConstants, "MachineState", mach.state)) print " Session state: %s" %(enumToString(vboxConstants, "SessionState", mach.sessionState)) # Do some stuff which requires a running VM if mach.state == vboxConstants.MachineState_Running: # Get the session object session = mgr.getSessionObject() # Lock the current machine (shared mode, since we won't modify the machine) mach.lockMachine(session, vboxConstants.LockType_Shared) # Acquire the VM's console and guest object console = session.console guest = console.guest # Retrieve the current Guest Additions runlevel and print # the installed Guest Additions version addRunLevel = guest.additionsRunLevel print " Additions State: %s" %(enumToString(vboxConstants, "AdditionsRunLevelType", addRunLevel)) if addRunLevel != vboxConstants.AdditionsRunLevelType_None: print " Additions Ver: %s" %(guest.additionsVersion) # Get the VM's display object display = console.display # Get the VM's current display resolution + bit depth + position screenNum = 0 # From first screen (screenW, screenH, screenBPP, screenX, screenY, _) = display.getScreenResolution(screenNum) print " Display (%d): %dx%d, %d BPP at %d,%d" %(screenNum, screenW, screenH, screenBPP, screenX, screenY) # We're done -- don't forget to unlock the machine! session.unlockMachine()
from vboxapi import VirtualBoxManager mgr = VirtualBoxManager(None, None) vbox = mgr.getVirtualBox() machine = vbox.findMachine('CentOS') session = mgr.getSessionObject(vbox) machine.LockMachine(session, mgr.constants.LockType_Shared) session.Console.Mouse.putMouseEventAbsolute(100, 100, 0, 0, 1)