Serial console implementation for VMware VMs.

This commit is contained in:
grossmj committed 2015-05-27 21:06:18 -06:00
1 parent c782784bf5
commit 98e01ff21d
3 files changed
+110 -15

No files matched your search

@@ -1,442 +0,0 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2014 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import time
import threading
import socket
import select
import logging
log = logging.getLogger(__name__)
if sys.platform.startswith("win"):
import win32pipe
import win32file
class TelnetServer(threading.Thread):
"""
Mini Telnet Server.
:param vm_name: Virtual machine name
:param pipe_path: path to VM pipe (UNIX socket on Linux/UNIX, Named Pipe on Windows)
:param host: server host
:param port: server port
"""
def __init__(self, vm_name, pipe_path, host, port):
threading.Thread.__init__(self)
self._vm_name = vm_name
self._pipe = pipe_path
self._host = host
self._port = port
self._reader_thread = None
self._use_thread = False
self._write_lock = threading.Lock()
self._clients = {}
self._timeout = 1
self._alive = True
if sys.platform.startswith("win"):
# we must a thread for reading the pipe on Windows because it is a Named Pipe and it cannot be monitored by select()
self._use_thread = True
if ":" in self._host:
# IPv6 address support
self._server_socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
else:
self._server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self._server_socket.bind((self._host, self._port))
self._server_socket.listen(socket.SOMAXCONN)
log.info("Telnet server initialized, waiting for clients on {}:{}".format(self._host, self._port))
def run(self):
"""
Thread loop.
"""
while True:
recv_list = [self._server_socket.fileno()]
if not self._use_thread:
recv_list.append(self._pipe.fileno())
for client in self._clients.values():
if client.is_active():
recv_list.append(client.socket().fileno())
else:
del self._clients[client.socket().fileno()]
try:
client.socket().shutdown(socket.SHUT_RDWR)
except OSError as e:
log.warn("shutdown: {}".format(e))
client.socket().close()
break
try:
rlist, slist, elist = select.select(recv_list, [], [], self._timeout)
except OSError as e:
log.critical("fatal select error: {}".format(e))
return False
if not self._alive:
log.info("Telnet server for {} is exiting".format(self._vm_name))
return True
for sock_fileno in rlist:
if sock_fileno == self._server_socket.fileno():
try:
sock, addr = self._server_socket.accept()
host, port = addr
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
log.info("new client {}:{} has connected".format(host, port))
except OSError as e:
log.error("could not accept new client: {}".format(e))
continue
new_client = TelnetClient(self._vm_name, sock, host, port)
self._clients[sock.fileno()] = new_client
if self._use_thread and not self._reader_thread:
self._reader_thread = threading.Thread(target=self._reader, daemon=True)
self._reader_thread.start()
elif not self._use_thread and sock_fileno == self._pipe.fileno():
data = self._read_from_pipe()
if not data:
log.warning("pipe has been closed!")
return False
for client in self._clients.values():
try:
client.send(data)
except OSError as e:
log.debug(e)
client.deactivate()
elif sock_fileno in self._clients:
try:
data = self._clients[sock_fileno].socket_recv()
if not data:
continue
# For some reason, windows likes to send "cr/lf" when you send a "cr".
# Strip that so we don't get a double prompt.
data = data.replace(b"\r\n", b"\n")
self._write_to_pipe(data)
except Exception as msg:
log.info(msg)
self._clients[sock_fileno].deactivate()
def _write_to_pipe(self, data):
"""
Writes data to the pipe.
:param data: data to write
"""
if sys.platform.startswith('win'):
win32file.WriteFile(self._pipe, data)
else:
self._pipe.sendall(data)
def _read_from_pipe(self):
"""
Reads data from the pipe.
:returns: data
"""
if sys.platform.startswith('win'):
(read, num_avail, num_message) = win32pipe.PeekNamedPipe(self._pipe, 0)
if num_avail > 0:
(error_code, output) = win32file.ReadFile(self._pipe, num_avail, None)
return output
return b""
else:
return self._pipe.recv(1024)
def _reader(self):
"""
Loops forever and copy everything from the pipe to the socket.
"""
log.debug("reader thread has started")
while self._alive:
try:
data = self._read_from_pipe()
if not data and not sys.platform.startswith('win'):
log.debug("pipe has been closed! (no data)")
break
self._write_lock.acquire()
try:
for client in self._clients.values():
client.send(data)
finally:
self._write_lock.release()
if sys.platform.startswith('win'):
# sleep every 10 ms
time.sleep(0.01)
except Exception as e:
log.debug("pipe has been closed! {}".format(e))
break
log.debug("reader thread exited")
self.stop()
def stop(self):
"""
Stops the server.
"""
if self._alive:
self._alive = False
for client in self._clients.values():
client.socket().close()
client.deactivate()
# Mostly from https://code.google.com/p/miniboa/source/browse/trunk/miniboa/telnet.py
# Telnet Commands
SE = 240 # End of sub-negotiation parameters
NOP = 241 # No operation
DATMK = 242 # Data stream portion of a sync.
BREAK = 243 # NVT Character BRK
IP = 244 # Interrupt Process
AO = 245 # Abort Output
AYT = 246 # Are you there
EC = 247 # Erase Character
EL = 248 # Erase Line
GA = 249 # The Go Ahead Signal
SB = 250 # Sub-option to follow
WILL = 251 # Will; request or confirm option begin
WONT = 252 # Wont; deny option request
DO = 253 # Do = Request or confirm remote option
DONT = 254 # Don't = Demand or confirm option halt
IAC = 255 # Interpret as Command
SEND = 1 # Sub-process negotiation SEND command
IS = 0 # Sub-process negotiation IS command
# Telnet Options
BINARY = 0 # Transmit Binary
ECHO = 1 # Echo characters back to sender
RECON = 2 # Reconnection
SGA = 3 # Suppress Go-Ahead
TMARK = 6 # Timing Mark
TTYPE = 24 # Terminal Type
NAWS = 31 # Negotiate About Window Size
LINEMO = 34 # Line Mode
class TelnetClient(object):
"""
Represents a Telnet client connection.
:param vm_name: VM name
:param sock: socket connection
:param host: IP of the Telnet client
:param port: port of the Telnet client
"""
def __init__(self, vm_name, sock, host, port):
self._active = True
self._sock = sock
self._host = host
self._port = port
sock.send(bytes([IAC, WILL, ECHO,
IAC, WILL, SGA,
IAC, WILL, BINARY,
IAC, DO, BINARY]))
welcome_msg = "{} console is now available... Press RETURN to get started.\r\n".format(vm_name)
sock.send(welcome_msg.encode('utf-8'))
def is_active(self):
"""
Returns either the client is active or not.
:return: boolean
"""
return self._active
def socket(self):
"""
Returns the socket for this Telnet client.
:returns: socket instance.
"""
return self._sock
def send(self, data):
"""
Sends data to the remote end.
:param data: data to send
"""
try:
self._sock.send(data)
except OSError as e:
self._active = False
raise Exception("Socket send: {}".format(e))
def deactivate(self):
"""
Sets the client to disconnect on the next server poll.
"""
self._active = False
def socket_recv(self):
"""
Called by Telnet Server when data is ready.
"""
try:
buf = self._sock.recv(1024)
except BlockingIOError:
return None
except ConnectionResetError:
buf = b''
# is the connection closed?
if not buf:
raise Exception("connection closed by {}:{}".format(self._host, self._port))
# Process and remove any telnet commands from the buffer
if IAC in buf:
buf = self._IAC_parser(buf)
return buf
def _read_block(self, bufsize):
"""
Reads a block for data from the socket.
:param bufsize: size of the buffer
:returns: data read
"""
buf = self._sock.recv(1024, socket.MSG_WAITALL)
# If we don't get everything we were looking for then the
# client probably disconnected.
if len(buf) < bufsize:
raise Exception("connection closed by {}:{}".format(self._host, self._port))
return buf
def _IAC_parser(self, buf):
"""
Processes and removes any Telnet commands from the buffer.
:param buf: buffer
:returns: buffer minus Telnet commands
"""
skip_to = 0
while self._active:
# Locate an IAC to process
iac_loc = buf.find(IAC, skip_to)
if iac_loc < 0:
break
# Get the TELNET command
iac_cmd = bytearray([IAC])
try:
iac_cmd.append(buf[iac_loc + 1])
except IndexError:
buf.extend(self._read_block(1))
iac_cmd.append(buf[iac_loc + 1])
# Is this just a 2-byte TELNET command?
if iac_cmd[1] not in [WILL, WONT, DO, DONT]:
if iac_cmd[1] == AYT:
log.debug("Telnet server received Are-You-There (AYT)")
self._sock.send(b'\r\nYour Are-You-There received. I am here.\r\n')
elif iac_cmd[1] == IAC:
# It's data, not an IAC
iac_cmd.pop()
# This prevents the 0xff from being
# interrupted as yet another IAC
skip_to = iac_loc + 1
log.debug("Received IAC IAC")
elif iac_cmd[1] == NOP:
pass
else:
log.debug("Unhandled telnet command: "
"{0:#x} {1:#x}".format(*iac_cmd))
# This must be a 3-byte TELNET command
else:
try:
iac_cmd.append(buf[iac_loc + 2])
except IndexError:
buf.extend(self._read_block(1))
iac_cmd.append(buf[iac_loc + 2])
# We do ECHO, SGA, and BINARY. Period.
if iac_cmd[1] == DO and iac_cmd[2] not in [ECHO, SGA, BINARY]:
self._sock.send(bytes([IAC, WONT, iac_cmd[2]]))
log.debug("Telnet WON'T {:#x}".format(iac_cmd[2]))
else:
log.debug("Unhandled telnet command: "
"{0:#x} {1:#x} {2:#x}".format(*iac_cmd))
# Remove the entire TELNET command from the buffer
buf = buf.replace(iac_cmd, b'', 1)
# Return the new copy of the buffer, minus telnet commands
return buf
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
if sys.platform.startswith('win'):
import msvcrt
pipe_name = r'\\.\pipe\VBOX\Linux_Microcore_4.7.1'
pipe = open(pipe_name, 'a+b')
telnet_server = TelnetServer("VBOX", msvcrt.get_osfhandle(pipe.fileno()), "127.0.0.1", 3900)
else:
pipe_name = "/tmp/pipe_test"
try:
unix_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
unix_socket.connect(pipe_name)
except OSError as e:
print("Could not connect to UNIX socket {}: {}".format(pipe_name, e))
sys.exit(False)
telnet_server = TelnetServer("VBOX", unix_socket, "127.0.0.1", 3900)
telnet_server.setDaemon(True)
telnet_server.start()
try:
telnet_server.join()
except KeyboardInterrupt:
telnet_server.stop()
telnet_server.join(timeout=3)
@@ -29,11 +29,11 @@ import socket
import asyncio
from pkg_resources import parse_version
from gns3server.utils.telnet_server import TelnetServer
from .virtualbox_error import VirtualBoxError
from ..nios.nio_udp import NIOUDP
from ..nios.nio_nat import NIONAT
from ..adapters.ethernet_adapter import EthernetAdapter
from .telnet_server import TelnetServer # TODO: port TelnetServer to asyncio
from ..base_vm import BaseVM
if sys.platform.startswith('win'):
+108 -14
View File
@@ -21,25 +21,30 @@ VMware VM instance.
import sys
import os
import socket
import subprocess
import configparser
import shutil
import asyncio
import tempfile
from gns3server.utils.asyncio import wait_for_process_termination
from gns3server.utils.asyncio import monitor_process
from gns3server.utils.telnet_server import TelnetServer
from collections import OrderedDict
from .vmware_error import VMwareError
from ..nios.nio_udp import NIOUDP
from ..adapters.ethernet_adapter import EthernetAdapter
from ..base_vm import BaseVM
if sys.platform.startswith('win'):
import msvcrt
import win32file
import logging
log = logging.getLogger(__name__)
class VMwareVM(BaseVM):
"""
@@ -54,8 +59,11 @@ class VMwareVM(BaseVM):
self._vmx_pairs = OrderedDict()
self._ubridge_process = None
self._ubridge_stdout_file = ""
self._telnet_server_thread = None
self._serial_pipe = None
self._vmnets = []
self._maximum_adapters = 10
self._started = False
self._closed = False
# VMware VM settings
@@ -109,11 +117,6 @@ class VMwareVM(BaseVM):
def _set_network_options(self):
try:
self._vmx_pairs = self.manager.parse_vmware_file(self._vmx_path)
except OSError as e:
raise VMwareError('Could not read VMware VMX file "{}": {}'.format(self._vmx_path, e))
# first do some sanity checks
for adapter_number in range(0, self._adapters):
connected = "ethernet{}.startConnected".format(adapter_number)
@@ -179,7 +182,6 @@ class VMwareVM(BaseVM):
log.debug("disabling remaining adapter {}".format(adapter_number))
self._vmx_pairs["ethernet{}.startConnected".format(adapter_number)] = "FALSE"
self.manager.write_vmx_file(self._vmx_path, self._vmx_pairs)
self._update_ubridge_config()
def _update_ubridge_config(self):
@@ -242,7 +244,7 @@ class VMwareVM(BaseVM):
:returns: path to uBridge
"""
path = self._manager.config.get_section_config("VMware").get("ubridge_path", "ubridge")
path = self._manager.config.get_section_config("Server").get("ubridge_path", "ubridge")
if path == "ubridge":
path = shutil.which("ubridge")
return path
@@ -333,13 +335,26 @@ class VMwareVM(BaseVM):
if not ubridge_path or not os.path.isfile(ubridge_path):
raise VMwareError("ubridge is necessary to start a VMware VM")
self._set_network_options()
yield from self._start_ubridge()
try:
self._vmx_pairs = self.manager.parse_vmware_file(self._vmx_path)
except OSError as e:
raise VMwareError('Could not read VMware VMX file "{}": {}'.format(self._vmx_path, e))
self._set_network_options()
self._set_serial_console()
self.manager.write_vmx_file(self._vmx_path, self._vmx_pairs)
yield from self._start_ubridge()
if self._headless:
yield from self._control_vm("start", "nogui")
else:
yield from self._control_vm("start")
if self._enable_remote_console and self._console is not None:
yield from asyncio.sleep(1) # give some time to VMware to create the pipe file.
self._start_remote_console()
self._started = True
log.info("VMware VM '{name}' [{id}] started".format(name=self.name, id=self.id))
@asyncio.coroutine
@@ -348,6 +363,7 @@ class VMwareVM(BaseVM):
Stops this VMware VM.
"""
self._stop_remote_console()
if self.is_ubridge_running():
self._terminate_process_ubridge()
try:
@@ -361,9 +377,8 @@ class VMwareVM(BaseVM):
try:
yield from self._control_vm("stop")
finally:
self._started = False
self._vmnets.clear()
try:
self._vmx_pairs = self.manager.parse_vmware_file(self._vmx_path)
except OSError as e:
@@ -518,10 +533,11 @@ class VMwareVM(BaseVM):
if enable_remote_console:
log.info("VMware VM '{name}' [{id}] has enabled the console".format(name=self.name, id=self.id))
#self._start_remote_console()
if self._started:
self._start_remote_console()
else:
log.info("VMware VM '{name}' [{id}] has disabled the console".format(name=self.name, id=self.id))
#self._stop_remote_console()
self._stop_remote_console()
self._enable_remote_console = enable_remote_console
@property
@@ -647,3 +663,81 @@ class VMwareVM(BaseVM):
nio=nio,
adapter_number=adapter_number))
return nio
def _get_pipe_name(self):
"""
Returns the pipe name to create a serial connection.
:returns: pipe path (string)
"""
if sys.platform.startswith("win"):
pipe_name = r"\\.\pipe\gns3_vmware\{}".format(self.id)
else:
pipe_name = os.path.join(tempfile.gettempdir(), "gns3_vmware", "{}".format(self.id))
try:
os.makedirs(os.path.dirname(pipe_name), exist_ok=True)
except OSError as e:
raise VMwareError("Could not create the VMware pipe directory: {}".format(e))
return pipe_name
def _set_serial_console(self):
"""
Configures the first serial port to allow a serial console connection.
"""
pipe_name = self._get_pipe_name()
serial_port = {"serial0.present": "TRUE",
"serial0.fileType": "pipe",
"serial0.fileName": pipe_name,
"serial0.pipe.endPoint": "server"}
self._vmx_pairs.update(serial_port)
def _start_remote_console(self):
"""
Starts remote console support for this VM.
"""
# 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 VMwareError("Could not open the pipe {}: {}".format(pipe_name, e))
try:
self._telnet_server_thread = TelnetServer(self.name, msvcrt.get_osfhandle(self._serial_pipe.fileno()), self._manager.port_manager.console_host, self._console)
except OSError as e:
raise VMwareError("Unable to create Telnet server: {}".format(e))
self._telnet_server_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 VMwareError("Could not connect to the pipe {}: {}".format(pipe_name, e))
try:
self._telnet_server_thread = TelnetServer(self.name, self._serial_pipe, self._manager.port_manager.console_host, self._console)
except OSError as e:
raise VMwareError("Unable to create Telnet server: {}".format(e))
self._telnet_server_thread.start()
def _stop_remote_console(self):
"""
Stops remote console support for this VM.
"""
if self._telnet_server_thread:
if self._telnet_server_thread.is_alive():
self._telnet_server_thread.stop()
self._telnet_server_thread.join(timeout=3)
if self._telnet_server_thread.is_alive():
log.warn("Serial pipe thread is still alive!")
self._telnet_server_thread = None
if self._serial_pipe:
if sys.platform.startswith("win"):
win32file.CloseHandle(msvcrt.get_osfhandle(self._serial_pipe.fileno()))
else:
self._serial_pipe.close()
self._serial_pipe = None