Skip to content
This repository has been archived by the owner on Apr 10, 2020. It is now read-only.

Commit

Permalink
Add enable console option (True by default).
Browse files Browse the repository at this point in the history
Disable the serial port when a VM is stopped.
Support for adapter start index.
  • Loading branch information
grossmj committed Aug 26, 2014
1 parent 09c6ec1 commit 1a78c0a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 28 deletions.
14 changes: 11 additions & 3 deletions vboxwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,12 @@ def __init__(self, name):
self.image = ''
self.nic = {}
self.nics = '6'
self.nic_start_index = '0'
self.udp = {}
self.capture = {}
self.netcard = 'Automatic'
self.headless_mode = False
self.enable_console = True
self.process = None
self.pipeThread = None
self.pipe = None
Expand All @@ -124,7 +126,9 @@ def __init__(self, name):
'console',
'nics',
'netcard',
'headless_mode']
'headless_mode',
'enable_console',
'nic_start_index']

def _start_vbox_service(self, vmname):

Expand Down Expand Up @@ -157,8 +161,12 @@ def start(self):
self._vboxcontroller.console = int(self.console)
self._vboxcontroller.adapter_type = self.netcard
self._vboxcontroller.headless = self.headless_mode
self._vboxcontroller.enable_console = self.enable_console
self._ethernet_adapters = []
for adapter_id in range(0, int(self.nics)):
for adapter_id in range(0, int(self.nic_start_index) + int(self.nics)):
if adapter_id < int(self.nic_start_index):
self._ethernet_adapters.append(None)
continue
adapter = EthernetAdapter()
if adapter_id in self.udp:
udp_info = self.udp[adapter_id]
Expand Down Expand Up @@ -606,7 +614,7 @@ def do_vbox_setattr(self, data):
self.send_reply(self.HSC_ERR_UNK_OBJ, 1,
"Cannot set attribute '%s' for '%s" % (attr, name))
return
log.info("!! {}.{} = {}".format(name, attr, value))
print("!! {}.{} = {}".format(name, attr, value))
setattr(VBOX_INSTANCES[name], attr, value)
self.send_reply(self.HSC_INFO_OK, 1, "%s set for '%s'" % (attr, name))

Expand Down
78 changes: 53 additions & 25 deletions virtualbox_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __init__(self, vmname, vboxmanager, host):
self._console = 0
self._adapters = []
self._headless = False
self._enable_console = True
self._adapter_type = "Automatic"

try:
Expand Down Expand Up @@ -101,6 +102,16 @@ def headless(self, headless):

self._headless = headless

@property
def enable_console(self):

return self._enable_console

@enable_console.setter
def enable_console(self, enable_console):

self._enable_console = enable_console

@property
def adapters(self):

Expand All @@ -123,13 +134,17 @@ def adapter_type(self, adapter_type):

def start(self):

if len(self._adapters) > self._maximum_adapters:
raise VirtualBoxError("Number of adapters above the maximum supported of {}".format(self._maximum_adapters))

if self._machine.state == self._vboxmanager.constants.MachineState_Paused:
self.resume()
return

self._get_session()
self._set_network_options()
self._set_console_options()
if self._enable_console:
self._set_console_options()

progress = self._launch_vm_process()
log.info("VM is starting with {}% completed".format(progress.percent))
Expand All @@ -145,25 +160,26 @@ def start(self):
except Exception:
pass

# starts the Telnet to pipe thread
pipe_name = self._get_pipe_name()
if sys.platform.startswith('win'):
try:
self._serial_pipe = open(pipe_name, "a+b")
except OSError as e:
raise VirtualBoxError("Could not open the pipe {}: {}".format(pipe_name, e))
self._serial_pipe_thread = PipeProxy(self._vmname, msvcrt.get_osfhandle(self._serial_pipe.fileno()), self._host, self._console)
#self._serial_pipe_thread.setDaemon(True)
self._serial_pipe_thread.start()
else:
try:
self._serial_pipe = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self._serial_pipe.connect(pipe_name)
except OSError as e:
raise VirtualBoxError("Could not connect to the pipe {}: {}".format(pipe_name, e))
self._serial_pipe_thread = PipeProxy(self._vmname, self._serial_pipe, self._host, self._console)
#self._serial_pipe_thread.setDaemon(True)
self._serial_pipe_thread.start()
if self._enable_console:
# starts the Telnet to pipe thread
pipe_name = self._get_pipe_name()
if sys.platform.startswith('win'):
try:
self._serial_pipe = open(pipe_name, "a+b")
except OSError as e:
raise VirtualBoxError("Could not open the pipe {}: {}".format(pipe_name, e))
self._serial_pipe_thread = PipeProxy(self._vmname, msvcrt.get_osfhandle(self._serial_pipe.fileno()), self._host, self._console)
#self._serial_pipe_thread.setDaemon(True)
self._serial_pipe_thread.start()
else:
try:
self._serial_pipe = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self._serial_pipe.connect(pipe_name)
except OSError as e:
raise VirtualBoxError("Could not connect to the pipe {}: {}".format(pipe_name, e))
self._serial_pipe_thread = PipeProxy(self._vmname, self._serial_pipe, self._host, self._console)
#self._serial_pipe_thread.setDaemon(True)
self._serial_pipe_thread.start()

def stop(self):

Expand Down Expand Up @@ -196,8 +212,14 @@ def stop(self):
log.info("VM is stopping with {}% completed".format(self.vmname, progress.percent))

self._lock_machine()

for adapter_id in range(0, len(self._adapters)):
if self._adapters[adapter_id] is None:
continue
self._disable_adapter(adapter_id, disable=True)
if self._enable_console:
serial_port = self._session.machine.getSerialPort(0)
serial_port.enabled = False
self._session.machine.saveSettings()
self._unlock_machine()
except Exception as e:
Expand Down Expand Up @@ -252,11 +274,17 @@ def _set_network_options(self):
#raise VirtualBoxError("VirtualBox error: {}".format(e))

for adapter_id in range(0, len(self._adapters)):

try:
# VirtualBox starts counting from 0
adapter = self._session.machine.getNetworkAdapter(adapter_id)
vbox_adapter_type = adapter.adapterType
if self._adapters[adapter_id] is None:
# force enable to avoid any discrepancy in the interface numbering inside the VM
# e.g. Ethernet2 in GNS3 becoming eth0 inside the VM when using a start index of 2.
adapter.enabled = True
continue

vbox_adapter_type = adapter.adapterType
if self._adapter_type == "PCnet-PCI II (Am79C970A)":
vbox_adapter_type = self._vboxmanager.constants.NetworkAdapterType_Am79C970A
if self._adapter_type == "PCNet-FAST III (Am79C973)":
Expand Down Expand Up @@ -310,9 +338,9 @@ def _set_network_options(self):
except Exception as e:
raise VirtualBoxError("VirtualBox error: {}".format(e))

#for adapter_id in range(len(self._ethernet_adapters), self._maximum_adapters):
# log.debug("disabling remaining adapter {}".format(adapter_id))
# self._disable_adapter(adapter_id)
for adapter_id in range(len(self._adapters), self._maximum_adapters):
log.debug("disabling remaining adapter {}".format(adapter_id))
self._disable_adapter(adapter_id)

try:
self._session.machine.saveSettings()
Expand Down Expand Up @@ -526,4 +554,4 @@ def _unlock_machine(self):
log.warn("cannot unlock the machine for {}, retrying {}: {}".format(self._vmname, retry + 1, e))
time.sleep(1)
last_exception = e
continue
continue

0 comments on commit 1a78c0a

Please sign in to comment.