2015-05-01 01:05:37 +00:00
# -*- coding: utf-8 -*-
#
2015-09-05 20:38:11 +00:00
# Copyright (C) 2015 GNS3 Technologies Inc.
2015-05-01 01:05:37 +00:00
#
# 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/>.
"""
VMware VM instance .
"""
import sys
import os
2015-05-28 03:06:18 +00:00
import socket
2015-05-01 01:05:37 +00:00
import asyncio
2015-05-28 03:06:18 +00:00
import tempfile
2015-05-01 01:05:37 +00:00
2015-05-28 03:06:18 +00:00
from gns3server . utils . telnet_server import TelnetServer
2016-03-26 01:53:36 +00:00
from gns3server . utils . interfaces import interfaces
2016-11-04 18:39:17 +00:00
from gns3server . utils . asyncio import wait_for_file_creation , wait_for_named_pipe_creation , locked_coroutine
2015-05-22 03:48:59 +00:00
from collections import OrderedDict
2015-05-01 01:05:37 +00:00
from . vmware_error import VMwareError
from . . nios . nio_udp import NIOUDP
2015-05-15 02:11:57 +00:00
from . . adapters . ethernet_adapter import EthernetAdapter
2016-05-11 17:35:36 +00:00
from . . base_node import BaseNode
2015-05-01 01:05:37 +00:00
2015-05-28 03:06:18 +00:00
if sys . platform . startswith ( ' win ' ) :
import msvcrt
import win32file
2015-05-21 01:05:26 +00:00
2015-05-01 01:05:37 +00:00
import logging
log = logging . getLogger ( __name__ )
2015-05-21 01:05:26 +00:00
2016-05-11 17:35:36 +00:00
class VMwareVM ( BaseNode ) :
2015-05-01 01:05:37 +00:00
"""
VMware VM implementation .
"""
2016-10-24 19:39:35 +00:00
def __init__ ( self , name , node_id , project , manager , vmx_path , linked_clone = False , console = None ) :
2015-05-01 01:05:37 +00:00
2016-10-24 19:39:35 +00:00
super ( ) . __init__ ( name , node_id , project , manager , console = console , linked_clone = linked_clone )
2015-05-01 01:05:37 +00:00
2015-05-22 03:48:59 +00:00
self . _vmx_pairs = OrderedDict ( )
2015-05-28 03:06:18 +00:00
self . _telnet_server_thread = None
self . _serial_pipe = None
2015-05-22 03:48:59 +00:00
self . _vmnets = [ ]
self . _maximum_adapters = 10
2015-05-28 03:06:18 +00:00
self . _started = False
2015-05-01 01:05:37 +00:00
self . _closed = False
2016-10-24 19:39:35 +00:00
if not self . linked_clone :
for node in self . manager . nodes :
if node . vmx_path == vmx_path :
raise VMwareError ( " Sorry a node without the linked clone setting enabled can only be used once on your server. {} is already used by {} " . format ( vmx_path , node . name ) )
2015-05-01 01:05:37 +00:00
# VMware VM settings
self . _headless = False
self . _vmx_path = vmx_path
2015-06-18 21:02:31 +00:00
self . _acpi_shutdown = False
2015-05-15 02:11:57 +00:00
self . _adapters = 0
self . _ethernet_adapters = { }
self . _adapter_type = " e1000 "
2015-05-22 03:48:59 +00:00
self . _use_any_adapter = False
2015-05-15 02:11:57 +00:00
if not os . path . exists ( vmx_path ) :
2016-05-11 17:35:36 +00:00
raise VMwareError ( ' VMware VM " {name} " [ {id} ]: could not find VMX file " {vmx_path} " ' . format ( name = name , id = node_id , vmx_path = vmx_path ) )
2015-05-01 01:05:37 +00:00
def __json__ ( self ) :
2015-06-26 15:09:19 +00:00
json = { " name " : self . name ,
2016-05-11 17:35:36 +00:00
" node_id " : self . id ,
2015-05-01 01:05:37 +00:00
" console " : self . console ,
" project_id " : self . project . id ,
" vmx_path " : self . vmx_path ,
2015-05-15 02:11:57 +00:00
" headless " : self . headless ,
2015-06-18 21:02:31 +00:00
" acpi_shutdown " : self . acpi_shutdown ,
2015-05-15 02:11:57 +00:00
" adapters " : self . _adapters ,
2015-05-22 03:48:59 +00:00
" adapter_type " : self . adapter_type ,
2015-07-08 23:48:34 +00:00
" use_any_adapter " : self . use_any_adapter ,
2016-05-17 17:51:06 +00:00
" status " : self . status ,
2016-07-12 14:29:58 +00:00
" node_directory " : self . working_dir ,
2016-10-24 19:39:35 +00:00
" linked_clone " : self . linked_clone }
2015-06-26 15:09:19 +00:00
return json
2015-05-22 03:48:59 +00:00
@property
def vmnets ( self ) :
return self . _vmnets
2015-05-01 01:05:37 +00:00
2016-11-04 18:39:17 +00:00
@locked_coroutine
2015-05-01 01:05:37 +00:00
def _control_vm ( self , subcommand , * additional_args ) :
args = [ self . _vmx_path ]
args . extend ( additional_args )
result = yield from self . manager . execute ( subcommand , args )
log . debug ( " Control VM ' {} ' result: {} " . format ( subcommand , result ) )
return result
2015-09-05 20:38:11 +00:00
def _read_vmx_file ( self ) :
"""
Reads from the VMware VMX file corresponding to this VM .
"""
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 ) )
def _write_vmx_file ( self ) :
"""
Writes pairs to the VMware VMX file corresponding to this VM .
"""
try :
self . manager . write_vmx_file ( self . _vmx_path , self . _vmx_pairs )
except OSError as e :
raise VMwareError ( ' Could not write VMware VMX file " {} " : {} ' . format ( self . _vmx_path , e ) )
2015-10-11 22:41:55 +00:00
@asyncio.coroutine
def is_running ( self ) :
result = yield from self . manager . execute ( " list " , [ ] )
if self . _vmx_path in result :
return True
return False
2015-05-31 02:26:38 +00:00
@asyncio.coroutine
def create ( self ) :
2015-06-17 23:36:52 +00:00
"""
Creates this VM and handle linked clones .
"""
2015-05-31 02:26:38 +00:00
2016-02-04 01:15:33 +00:00
yield from self . manager . check_vmrun_version ( )
2016-10-24 19:39:35 +00:00
if self . linked_clone and not os . path . exists ( os . path . join ( self . working_dir , os . path . basename ( self . _vmx_path ) ) ) :
2016-04-04 18:10:48 +00:00
if self . manager . host_type == " player " :
raise VMwareError ( " Linked clones are not supported by VMware Player " )
2015-05-31 02:26:38 +00:00
# create the base snapshot for linked clones
base_snapshot_name = " GNS3 Linked Base for clones "
vmsd_path = os . path . splitext ( self . _vmx_path ) [ 0 ] + " .vmsd "
if not os . path . exists ( vmsd_path ) :
raise VMwareError ( " {} doesn ' t not exist " . format ( vmsd_path ) )
try :
vmsd_pairs = self . manager . parse_vmware_file ( vmsd_path )
except OSError as e :
raise VMwareError ( ' Could not read VMware VMSD file " {} " : {} ' . format ( vmsd_path , e ) )
gns3_snapshot_exists = False
for value in vmsd_pairs . values ( ) :
if value == base_snapshot_name :
gns3_snapshot_exists = True
break
if not gns3_snapshot_exists :
log . info ( " Creating snapshot ' {} ' " . format ( base_snapshot_name ) )
yield from self . _control_vm ( " snapshot " , base_snapshot_name )
# create the linked clone based on the base snapshot
new_vmx_path = os . path . join ( self . working_dir , self . name + " .vmx " )
yield from self . _control_vm ( " clone " ,
new_vmx_path ,
" linked " ,
" -snapshot= {} " . format ( base_snapshot_name ) ,
" -cloneName= {} " . format ( self . name ) )
try :
vmsd_pairs = self . manager . parse_vmware_file ( vmsd_path )
except OSError as e :
raise VMwareError ( ' Could not read VMware VMSD file " {} " : {} ' . format ( vmsd_path , e ) )
snapshot_name = None
for name , value in vmsd_pairs . items ( ) :
if value == base_snapshot_name :
snapshot_name = name . split ( " . " , 1 ) [ 0 ]
break
if snapshot_name is None :
raise VMwareError ( " Could not find the linked base snapshot in {} " . format ( vmsd_path ) )
num_clones_entry = " {} .numClones " . format ( snapshot_name )
if num_clones_entry in vmsd_pairs :
try :
nb_of_clones = int ( vmsd_pairs [ num_clones_entry ] )
except ValueError :
raise VMwareError ( " Value of {} in {} is not a number " . format ( num_clones_entry , vmsd_path ) )
vmsd_pairs [ num_clones_entry ] = str ( nb_of_clones - 1 )
for clone_nb in range ( 0 , nb_of_clones ) :
clone_entry = " {} .clone {} " . format ( snapshot_name , clone_nb )
if clone_entry in vmsd_pairs :
del vmsd_pairs [ clone_entry ]
try :
self . manager . write_vmware_file ( vmsd_path , vmsd_pairs )
except OSError as e :
raise VMwareError ( ' Could not write VMware VMSD file " {} " : {} ' . format ( vmsd_path , e ) )
# update the VMX file path
self . _vmx_path = new_vmx_path
2015-05-22 03:48:59 +00:00
def _get_vmx_setting ( self , name , value = None ) :
2015-05-21 01:05:26 +00:00
2015-05-22 03:48:59 +00:00
if name in self . _vmx_pairs :
if value is not None :
if self . _vmx_pairs [ name ] == value :
return value
else :
return self . _vmx_pairs [ name ]
return None
2015-05-21 01:05:26 +00:00
2015-05-15 02:11:57 +00:00
def _set_network_options ( self ) :
2015-06-17 23:36:52 +00:00
"""
Set up VMware networking .
"""
2015-05-15 02:11:57 +00:00
2015-09-05 20:38:11 +00:00
# first some sanity checks
2015-05-21 01:05:26 +00:00
for adapter_number in range ( 0 , self . _adapters ) :
2015-09-05 20:38:11 +00:00
# we want the vmnet interface to be connected when starting the VM
2015-05-22 03:48:59 +00:00
connected = " ethernet {} .startConnected " . format ( adapter_number )
if self . _get_vmx_setting ( connected ) :
del self . _vmx_pairs [ connected ]
2015-09-05 20:38:11 +00:00
# then configure VMware network adapters
2016-06-01 01:16:55 +00:00
self . manager . refresh_vmnet_list ( )
2015-05-22 03:48:59 +00:00
for adapter_number in range ( 0 , self . _adapters ) :
2015-09-05 20:38:11 +00:00
# add/update the interface
2016-05-23 20:14:42 +00:00
if self . _adapter_type == " default " :
# force default to e1000 because some guest OS don't detect the adapter (i.e. Windows 2012 server)
# when 'virtualdev' is not set in the VMX file.
adapter_type = " e1000 "
else :
adapter_type = self . _adapter_type
2015-05-22 03:48:59 +00:00
ethernet_adapter = { " ethernet {} .present " . format ( adapter_number ) : " TRUE " ,
2015-06-18 23:53:08 +00:00
" ethernet {} .addresstype " . format ( adapter_number ) : " generated " ,
2016-05-23 20:14:42 +00:00
" ethernet {} .generatedaddressoffset " . format ( adapter_number ) : " 0 " ,
" ethernet {} .virtualdev " . format ( adapter_number ) : adapter_type }
2015-05-22 03:48:59 +00:00
self . _vmx_pairs . update ( ethernet_adapter )
2015-06-18 23:53:08 +00:00
connection_type = " ethernet {} .connectiontype " . format ( adapter_number )
2015-05-22 03:48:59 +00:00
if not self . _use_any_adapter and connection_type in self . _vmx_pairs and self . _vmx_pairs [ connection_type ] in ( " nat " , " bridged " , " hostonly " ) :
continue
2015-09-05 20:38:11 +00:00
2016-05-23 21:53:03 +00:00
self . _vmx_pairs [ " ethernet {} .connectiontype " . format ( adapter_number ) ] = " custom "
2016-06-01 01:16:55 +00:00
# make sure we have a vmnet per adapter if we use uBridge
allocate_vmnet = False
2015-09-05 20:38:11 +00:00
2016-06-01 01:16:55 +00:00
# first check if a vmnet is already assigned to the adapter
vnet = " ethernet {} .vnet " . format ( adapter_number )
if vnet in self . _vmx_pairs :
vmnet = os . path . basename ( self . _vmx_pairs [ vnet ] )
if self . manager . is_managed_vmnet ( vmnet ) or vmnet in ( " vmnet0 " , " vmnet1 " , " vmnet8 " ) :
# vmnet already managed, try to allocate a new one
2015-09-05 20:38:11 +00:00
allocate_vmnet = True
2015-09-13 20:52:25 +00:00
else :
2016-06-01 01:16:55 +00:00
# otherwise allocate a new one
allocate_vmnet = True
if allocate_vmnet :
try :
vmnet = self . manager . allocate_vmnet ( )
except :
# clear everything up in case of error (e.g. no enough vmnets)
self . _vmnets . clear ( )
raise
# mark the vmnet managed by us
if vmnet not in self . _vmnets :
self . _vmnets . append ( vmnet )
self . _vmx_pairs [ " ethernet {} .vnet " . format ( adapter_number ) ] = vmnet
2015-05-22 03:48:59 +00:00
# disable remaining network adapters
for adapter_number in range ( self . _adapters , self . _maximum_adapters ) :
if self . _get_vmx_setting ( " ethernet {} .present " . format ( adapter_number ) , " TRUE " ) :
log . debug ( " disabling remaining adapter {} " . format ( adapter_number ) )
2015-06-18 23:53:08 +00:00
self . _vmx_pairs [ " ethernet {} .startconnected " . format ( adapter_number ) ] = " FALSE "
2015-05-21 01:05:26 +00:00
2015-07-20 04:55:10 +00:00
@asyncio.coroutine
def _add_ubridge_connection ( self , nio , adapter_number ) :
"""
Creates a connection in uBridge .
2015-05-21 01:05:26 +00:00
2015-07-20 04:55:10 +00:00
: param nio : NIO instance
: param adapter_number : adapter number
2015-05-21 01:05:26 +00:00
"""
2015-07-20 04:55:10 +00:00
2016-05-28 19:39:21 +00:00
block_host_traffic = self . manager . config . get_section_config ( " VMware " ) . getboolean ( " block_host_traffic " , False )
2015-07-20 04:55:10 +00:00
vnet = " ethernet {} .vnet " . format ( adapter_number )
if vnet not in self . _vmx_pairs :
raise VMwareError ( " vnet {} not in VMX file " . format ( vnet ) )
2016-05-31 04:07:37 +00:00
yield from self . _ubridge_send ( " bridge create {name} " . format ( name = vnet ) )
2015-07-20 04:55:10 +00:00
vmnet_interface = os . path . basename ( self . _vmx_pairs [ vnet ] )
if sys . platform . startswith ( " linux " ) :
2016-06-23 22:56:06 +00:00
yield from self . _ubridge_send ( ' bridge add_nio_linux_raw {name} " {interface} " ' . format ( name = vnet , interface = vmnet_interface ) )
2015-07-20 04:55:10 +00:00
elif sys . platform . startswith ( " win " ) :
2016-03-26 01:53:36 +00:00
windows_interfaces = interfaces ( )
2015-07-20 04:55:10 +00:00
npf = None
2016-03-15 00:27:51 +00:00
source_mac = None
2015-07-20 04:55:10 +00:00
for interface in windows_interfaces :
if " netcard " in interface and vmnet_interface in interface [ " netcard " ] :
npf = interface [ " id " ]
2016-03-15 00:27:51 +00:00
source_mac = interface [ " mac_address " ]
2015-07-20 04:55:10 +00:00
elif vmnet_interface in interface [ " name " ] :
npf = interface [ " id " ]
2016-03-15 00:27:51 +00:00
source_mac = interface [ " mac_address " ]
2015-07-20 04:55:10 +00:00
if npf :
2016-06-23 22:56:06 +00:00
yield from self . _ubridge_send ( ' bridge add_nio_ethernet {name} " {interface} " ' . format ( name = vnet , interface = npf ) )
2015-07-20 04:55:10 +00:00
else :
raise VMwareError ( " Could not find NPF id for VMnet interface {} " . format ( vmnet_interface ) )
2016-03-15 00:27:51 +00:00
2016-05-28 19:39:21 +00:00
if block_host_traffic :
if source_mac :
2016-06-23 22:56:06 +00:00
yield from self . _ubridge_send ( ' bridge set_pcap_filter {name} " not ether src {mac} " ' . format ( name = vnet , mac = source_mac ) )
2016-05-28 19:39:21 +00:00
else :
log . warn ( " Could not block host network traffic on {} (no MAC address found) " . format ( vmnet_interface ) )
2016-03-15 00:27:51 +00:00
2015-09-10 09:44:56 +00:00
elif sys . platform . startswith ( " darwin " ) :
2016-06-23 22:56:06 +00:00
yield from self . _ubridge_send ( ' bridge add_nio_fusion_vmnet {name} " {interface} " ' . format ( name = vnet , interface = vmnet_interface ) )
2015-07-20 04:55:10 +00:00
else :
2016-05-31 04:07:37 +00:00
yield from self . _ubridge_send ( ' bridge add_nio_ethernet {name} " {interface} " ' . format ( name = vnet ,
2016-07-13 15:47:00 +00:00
interface = vmnet_interface ) )
2015-07-20 04:55:10 +00:00
if isinstance ( nio , NIOUDP ) :
2016-05-31 04:07:37 +00:00
yield from self . _ubridge_send ( ' bridge add_nio_udp {name} {lport} {rhost} {rport} ' . format ( name = vnet ,
2016-06-23 22:56:06 +00:00
lport = nio . lport ,
rhost = nio . rhost ,
rport = nio . rport ) )
2015-07-20 04:55:10 +00:00
if nio . capturing :
2016-06-23 22:56:06 +00:00
yield from self . _ubridge_send ( ' bridge start_capture {name} " {pcap_file} " ' . format ( name = vnet , pcap_file = nio . pcap_output_file ) )
2015-07-20 04:55:10 +00:00
2016-05-31 04:07:37 +00:00
yield from self . _ubridge_send ( ' bridge start {name} ' . format ( name = vnet ) )
2015-07-20 04:55:10 +00:00
2016-05-28 19:39:21 +00:00
# TODO: this only work when using PCAP (NIO Ethernet): current default on Linux is NIO RAW LINUX
2016-03-15 00:27:51 +00:00
# source_mac = None
# for interface in interfaces():
# if interface["name"] == vmnet_interface:
# source_mac = interface["mac_address"]
# if source_mac:
2016-05-31 04:07:37 +00:00
# yield from self._ubridge_send('bridge set_pcap_filter {name} "not ether src {mac}"'.format(name=vnet, mac=source_mac))
2016-03-15 00:27:51 +00:00
2015-07-20 04:55:10 +00:00
@asyncio.coroutine
def _delete_ubridge_connection ( self , adapter_number ) :
2015-05-21 01:05:26 +00:00
"""
2015-07-20 04:55:10 +00:00
Deletes a connection in uBridge .
2015-05-15 02:11:57 +00:00
2015-07-20 04:55:10 +00:00
: param adapter_number : adapter number
"""
2015-05-21 01:05:26 +00:00
2015-07-20 04:55:10 +00:00
vnet = " ethernet {} .vnet " . format ( adapter_number )
if vnet not in self . _vmx_pairs :
raise VMwareError ( " vnet {} not in VMX file " . format ( vnet ) )
2016-05-31 04:07:37 +00:00
yield from self . _ubridge_send ( " bridge delete {name} " . format ( name = vnet ) )
2015-05-21 01:05:26 +00:00
2015-09-13 15:40:09 +00:00
@asyncio.coroutine
def _start_ubridge_capture ( self , adapter_number , output_file ) :
"""
Start a packet capture in uBridge .
: param adapter_number : adapter number
: param output_file : PCAP destination file for the capture
"""
vnet = " ethernet {} .vnet " . format ( adapter_number )
if vnet not in self . _vmx_pairs :
raise VMwareError ( " vnet {} not in VMX file " . format ( vnet ) )
2016-01-26 21:45:55 +00:00
if not self . _ubridge_hypervisor :
raise VMwareError ( " Cannot start the packet capture: uBridge is not running " )
2016-05-31 04:07:37 +00:00
yield from self . _ubridge_send ( ' bridge start_capture {name} " {output_file} " ' . format ( name = vnet ,
2016-07-13 15:47:00 +00:00
output_file = output_file ) )
2015-09-13 15:40:09 +00:00
@asyncio.coroutine
def _stop_ubridge_capture ( self , adapter_number ) :
"""
Stop a packet capture in uBridge .
: param adapter_number : adapter number
"""
vnet = " ethernet {} .vnet " . format ( adapter_number )
if vnet not in self . _vmx_pairs :
raise VMwareError ( " vnet {} not in VMX file " . format ( vnet ) )
2016-01-26 21:45:55 +00:00
if not self . _ubridge_hypervisor :
raise VMwareError ( " Cannot stop the packet capture: uBridge is not running " )
2016-05-31 04:07:37 +00:00
yield from self . _ubridge_send ( " bridge stop_capture {name} " . format ( name = vnet ) )
2015-09-13 15:40:09 +00:00
2015-07-27 01:21:30 +00:00
def check_hw_virtualization ( self ) :
"""
Returns either hardware virtualization is activated or not .
: returns : boolean
"""
2015-09-05 20:38:11 +00:00
self . _read_vmx_file ( )
2015-07-27 01:21:30 +00:00
if self . _get_vmx_setting ( " vhv.enable " , " TRUE " ) :
return True
return False
2015-05-01 01:05:37 +00:00
@asyncio.coroutine
def start ( self ) :
"""
Starts this VMware VM .
"""
2015-10-11 22:41:55 +00:00
if ( yield from self . is_running ( ) ) :
raise VMwareError ( " The VM is already running in VMware " )
2015-05-25 21:49:28 +00:00
2015-05-21 01:05:26 +00:00
ubridge_path = self . ubridge_path
if not ubridge_path or not os . path . isfile ( ubridge_path ) :
raise VMwareError ( " ubridge is necessary to start a VMware VM " )
2016-06-01 01:16:55 +00:00
yield from self . _start_ubridge ( )
2015-09-05 20:38:11 +00:00
self . _read_vmx_file ( )
2015-10-12 21:57:37 +00:00
# check if there is enough RAM to run
if " memsize " in self . _vmx_pairs :
self . check_available_ram ( int ( self . _vmx_pairs [ " memsize " ] ) )
2015-05-15 02:11:57 +00:00
self . _set_network_options ( )
2015-05-28 03:06:18 +00:00
self . _set_serial_console ( )
2015-09-05 20:38:11 +00:00
self . _write_vmx_file ( )
2015-05-21 01:05:26 +00:00
2015-05-01 01:05:37 +00:00
if self . _headless :
yield from self . _control_vm ( " start " , " nogui " )
else :
yield from self . _control_vm ( " start " )
2015-05-28 03:06:18 +00:00
2016-03-23 16:54:45 +00:00
try :
2016-06-01 01:16:55 +00:00
if self . _ubridge_hypervisor :
2016-03-23 16:54:45 +00:00
for adapter_number in range ( 0 , self . _adapters ) :
nio = self . _ethernet_adapters [ adapter_number ] . get_nio ( 0 )
if nio :
yield from self . _add_ubridge_connection ( nio , adapter_number )
2015-07-20 04:55:10 +00:00
2016-11-03 17:17:50 +00:00
# if self._console is not None:
2016-11-02 17:59:09 +00:00
# try:
# if sys.platform.startswith("win"):
# yield from wait_for_named_pipe_creation(self._get_pipe_name())
# else:
# yield from wait_for_file_creation(self._get_pipe_name()) # wait for VMware to create the pipe file.
# except asyncio.TimeoutError:
# raise VMwareError('Pipe file "{}" for remote console has not been created by VMware'.format(self._get_pipe_name()))
# self._start_remote_console()
2016-03-23 16:54:45 +00:00
except VMwareError :
yield from self . stop ( )
raise
2015-05-28 03:06:18 +00:00
2015-07-22 04:58:28 +00:00
if self . _get_vmx_setting ( " vhv.enable " , " TRUE " ) :
self . _hw_virtualization = True
2015-05-28 03:06:18 +00:00
self . _started = True
2016-05-14 02:41:58 +00:00
self . status = " started "
2015-05-01 01:05:37 +00:00
log . info ( " VMware VM ' {name} ' [ {id} ] started " . format ( name = self . name , id = self . id ) )
@asyncio.coroutine
def stop ( self ) :
"""
Stops this VMware VM .
"""
2015-07-22 04:58:28 +00:00
self . _hw_virtualization = False
2015-05-28 03:06:18 +00:00
self . _stop_remote_console ( )
2016-05-31 04:07:37 +00:00
yield from self . _stop_ubridge ( )
2015-05-21 01:05:26 +00:00
2015-05-22 03:48:59 +00:00
try :
2016-03-19 18:08:44 +00:00
if ( yield from self . is_running ( ) ) :
if self . acpi_shutdown :
# use ACPI to shutdown the VM
yield from self . _control_vm ( " stop " , " soft " )
else :
yield from self . _control_vm ( " stop " )
2015-05-22 03:48:59 +00:00
finally :
2015-05-28 03:06:18 +00:00
self . _started = False
2016-05-14 02:41:58 +00:00
self . status = " stopped "
2015-05-22 03:48:59 +00:00
2015-09-05 20:38:11 +00:00
self . _read_vmx_file ( )
2016-06-01 01:16:55 +00:00
self . _vmnets . clear ( )
# remove the adapters managed by GNS3
for adapter_number in range ( 0 , self . _adapters ) :
vnet = " ethernet {} .vnet " . format ( adapter_number )
if self . _get_vmx_setting ( vnet ) or self . _get_vmx_setting ( " ethernet {} .connectiontype " . format ( adapter_number ) ) is None :
if vnet in self . _vmx_pairs :
vmnet = os . path . basename ( self . _vmx_pairs [ vnet ] )
if not self . manager . is_managed_vmnet ( vmnet ) :
continue
log . debug ( " removing adapter {} " . format ( adapter_number ) )
self . _vmx_pairs [ vnet ] = " vmnet1 "
self . _vmx_pairs [ " ethernet {} .connectiontype " . format ( adapter_number ) ] = " custom "
2015-05-22 03:48:59 +00:00
# re-enable any remaining network adapters
for adapter_number in range ( self . _adapters , self . _maximum_adapters ) :
if self . _get_vmx_setting ( " ethernet {} .present " . format ( adapter_number ) , " TRUE " ) :
log . debug ( " enabling remaining adapter {} " . format ( adapter_number ) )
2015-06-18 23:53:08 +00:00
self . _vmx_pairs [ " ethernet {} .startconnected " . format ( adapter_number ) ] = " TRUE "
2015-09-05 20:38:11 +00:00
self . _write_vmx_file ( )
2015-05-22 03:48:59 +00:00
2015-05-01 01:05:37 +00:00
log . info ( " VMware VM ' {name} ' [ {id} ] stopped " . format ( name = self . name , id = self . id ) )
@asyncio.coroutine
def suspend ( self ) :
"""
Suspends this VMware VM .
"""
2015-05-16 01:09:48 +00:00
if self . manager . host_type != " ws " :
raise VMwareError ( " Pausing a VM is only supported by VMware Workstation " )
2015-05-02 00:47:46 +00:00
yield from self . _control_vm ( " pause " )
2016-05-14 02:41:58 +00:00
self . status = " suspended "
2015-05-02 00:47:46 +00:00
log . info ( " VMware VM ' {name} ' [ {id} ] paused " . format ( name = self . name , id = self . id ) )
2015-05-01 01:05:37 +00:00
@asyncio.coroutine
def resume ( self ) :
"""
Resumes this VMware VM .
"""
2015-05-16 01:09:48 +00:00
if self . manager . host_type != " ws " :
raise VMwareError ( " Unpausing a VM is only supported by VMware Workstation " )
2015-05-02 00:47:46 +00:00
yield from self . _control_vm ( " unpause " )
2016-05-14 02:41:58 +00:00
self . status = " started "
2015-05-01 01:05:37 +00:00
log . info ( " VMware VM ' {name} ' [ {id} ] resumed " . format ( name = self . name , id = self . id ) )
@asyncio.coroutine
def reload ( self ) :
"""
Reloads this VMware VM .
"""
yield from self . _control_vm ( " reset " )
log . info ( " VMware VM ' {name} ' [ {id} ] reloaded " . format ( name = self . name , id = self . id ) )
@asyncio.coroutine
def close ( self ) :
"""
2015-09-05 20:38:11 +00:00
Closes this VMware VM .
2015-05-01 01:05:37 +00:00
"""
2016-02-29 09:38:30 +00:00
if not ( yield from super ( ) . close ( ) ) :
return False
2015-05-01 01:05:37 +00:00
2015-05-21 01:05:26 +00:00
for adapter in self . _ethernet_adapters . values ( ) :
if adapter is not None :
for nio in adapter . ports . values ( ) :
if nio and isinstance ( nio , NIOUDP ) :
self . manager . port_manager . release_udp_port ( nio . lport , self . _project )
2015-05-02 00:47:46 +00:00
try :
2015-06-18 21:02:31 +00:00
self . acpi_shutdown = False
2015-05-02 00:47:46 +00:00
yield from self . stop ( )
except VMwareError :
pass
2015-05-01 01:05:37 +00:00
2016-10-24 19:39:35 +00:00
if self . linked_clone :
2016-01-26 21:23:01 +00:00
yield from self . manager . remove_from_vmware_inventory ( self . _vmx_path )
2015-05-31 02:26:38 +00:00
2015-05-01 01:05:37 +00:00
@property
def headless ( self ) :
"""
Returns either the VM will start in headless mode
: returns : boolean
"""
return self . _headless
@headless.setter
def headless ( self , headless ) :
"""
Sets either the VM will start in headless mode
: param headless : boolean
"""
if headless :
log . info ( " VMware VM ' {name} ' [ {id} ] has enabled the headless mode " . format ( name = self . name , id = self . id ) )
else :
log . info ( " VMware VM ' {name} ' [ {id} ] has disabled the headless mode " . format ( name = self . name , id = self . id ) )
self . _headless = headless
2015-06-18 21:02:31 +00:00
@property
def acpi_shutdown ( self ) :
"""
Returns either the VM will use ACPI shutdown
: returns : boolean
"""
return self . _acpi_shutdown
@acpi_shutdown.setter
def acpi_shutdown ( self , acpi_shutdown ) :
"""
Sets either the VM will use ACPI shutdown
: param acpi_shutdown : boolean
"""
if acpi_shutdown :
log . info ( " VMware VM ' {name} ' [ {id} ] has enabled the ACPI shutdown mode " . format ( name = self . name , id = self . id ) )
else :
log . info ( " VMware VM ' {name} ' [ {id} ] has disabled the ACPI shutdown mode " . format ( name = self . name , id = self . id ) )
self . _acpi_shutdown = acpi_shutdown
2015-05-01 01:05:37 +00:00
@property
def vmx_path ( self ) :
"""
Returns the path to the vmx file .
: returns : VMware vmx file
"""
return self . _vmx_path
@vmx_path.setter
def vmx_path ( self , vmx_path ) :
"""
Sets the path to the vmx file .
: param vmx_path : VMware vmx file
"""
log . info ( " VMware VM ' {name} ' [ {id} ] has set the vmx file path to ' {vmx} ' " . format ( name = self . name , id = self . id , vmx = vmx_path ) )
self . _vmx_path = vmx_path
2015-05-15 02:11:57 +00:00
@property
def adapters ( self ) :
"""
Returns the number of adapters configured for this VMware VM .
: returns : number of adapters
"""
return self . _adapters
@adapters.setter
def adapters ( self , adapters ) :
"""
Sets the number of Ethernet adapters for this VMware VM instance .
: param adapters : number of adapters
"""
2015-07-20 04:55:10 +00:00
# VMware VMs are limited to 10 adapters
2015-05-15 02:11:57 +00:00
if adapters > 10 :
raise VMwareError ( " Number of adapters above the maximum supported of 10 " )
self . _ethernet_adapters . clear ( )
for adapter_number in range ( 0 , adapters ) :
self . _ethernet_adapters [ adapter_number ] = EthernetAdapter ( )
self . _adapters = len ( self . _ethernet_adapters )
log . info ( " VMware VM ' {name} ' [ {id} ] has changed the number of Ethernet adapters to {adapters} " . format ( name = self . name ,
id = self . id ,
adapters = adapters ) )
@property
def adapter_type ( self ) :
"""
Returns the adapter type for this VMware VM instance .
: returns : adapter type ( string )
"""
return self . _adapter_type
@adapter_type.setter
def adapter_type ( self , adapter_type ) :
"""
Sets the adapter type for this VMware VM instance .
: param adapter_type : adapter type ( string )
"""
self . _adapter_type = adapter_type
log . info ( " VMware VM ' {name} ' [ {id} ]: adapter type changed to {adapter_type} " . format ( name = self . name ,
id = self . id ,
adapter_type = adapter_type ) )
2015-05-16 01:09:48 +00:00
2015-05-22 03:48:59 +00:00
@property
def use_any_adapter ( self ) :
"""
Returns either GNS3 can use any VMware adapter on this instance .
: returns : boolean
"""
return self . _use_any_adapter
@use_any_adapter.setter
def use_any_adapter ( self , use_any_adapter ) :
"""
Allows GNS3 to use any VMware adapter on this instance .
: param use_any_adapter : boolean
"""
if use_any_adapter :
log . info ( " VMware VM ' {name} ' [ {id} ] is allowed to use any adapter " . format ( name = self . name , id = self . id ) )
else :
log . info ( " VMware VM ' {name} ' [ {id} ] is not allowed to use any adapter " . format ( name = self . name , id = self . id ) )
self . _use_any_adapter = use_any_adapter
2015-07-20 04:55:10 +00:00
@asyncio.coroutine
2015-05-16 01:09:48 +00:00
def adapter_add_nio_binding ( self , adapter_number , nio ) :
"""
Adds an adapter NIO binding .
: param adapter_number : adapter number
: param nio : NIO instance to add to the slot / port
"""
try :
adapter = self . _ethernet_adapters [ adapter_number ]
except IndexError :
raise VMwareError ( " Adapter {adapter_number} doesn ' t exist on VMware VM ' {name} ' " . format ( name = self . name ,
adapter_number = adapter_number ) )
2015-09-05 20:38:11 +00:00
self . _read_vmx_file ( )
2016-06-01 01:06:48 +00:00
# check if trying to connect to a nat, bridged or host-only adapter
if not self . _use_any_adapter and self . _get_vmx_setting ( " ethernet {} .present " . format ( adapter_number ) , " TRUE " ) :
# check for the connection type
connection_type = " ethernet {} .connectiontype " . format ( adapter_number )
if connection_type in self . _vmx_pairs and self . _vmx_pairs [ connection_type ] in ( " nat " , " bridged " , " hostonly " ) :
raise VMwareError ( " Attachment ( {} ) already configured on network adapter {} . "
" Please remove it or allow GNS3 to use any adapter. " . format ( self . _vmx_pairs [ connection_type ] ,
adapter_number ) )
adapter . add_nio ( 0 , nio )
2016-06-01 01:16:55 +00:00
if self . _started and self . _ubridge_hypervisor :
2016-06-01 01:06:48 +00:00
yield from self . _add_ubridge_connection ( nio , adapter_number )
2015-07-20 04:55:10 +00:00
2015-05-16 01:09:48 +00:00
log . info ( " VMware VM ' {name} ' [ {id} ]: {nio} added to adapter {adapter_number} " . format ( name = self . name ,
id = self . id ,
nio = nio ,
adapter_number = adapter_number ) )
2015-07-20 04:55:10 +00:00
@asyncio.coroutine
2015-05-16 01:09:48 +00:00
def adapter_remove_nio_binding ( self , adapter_number ) :
"""
Removes an adapter NIO binding .
: param adapter_number : adapter number
: returns : NIO instance
"""
try :
adapter = self . _ethernet_adapters [ adapter_number ]
except IndexError :
raise VMwareError ( " Adapter {adapter_number} doesn ' t exist on VMware VM ' {name} ' " . format ( name = self . name ,
adapter_number = adapter_number ) )
nio = adapter . get_nio ( 0 )
if isinstance ( nio , NIOUDP ) :
self . manager . port_manager . release_udp_port ( nio . lport , self . _project )
adapter . remove_nio ( 0 )
2016-06-01 01:16:55 +00:00
if self . _started and self . _ubridge_hypervisor :
2015-07-20 04:55:10 +00:00
yield from self . _delete_ubridge_connection ( adapter_number )
2015-05-16 01:09:48 +00:00
log . info ( " VMware VM ' {name} ' [ {id} ]: {nio} removed from adapter {adapter_number} " . format ( name = self . name ,
id = self . id ,
nio = nio ,
adapter_number = adapter_number ) )
2015-06-17 23:36:52 +00:00
2015-05-16 01:09:48 +00:00
return nio
2015-05-28 03:06:18 +00:00
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 " ,
2015-06-18 23:53:08 +00:00
" serial0.filetype " : " pipe " ,
" serial0.filename " : pipe_name ,
" serial0.pipe.endpoint " : " server " }
2015-05-28 03:06:18 +00:00
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
2015-09-13 15:40:09 +00:00
@asyncio.coroutine
def start_capture ( self , adapter_number , output_file ) :
"""
Starts a packet capture .
: param adapter_number : adapter number
: param output_file : PCAP destination file for the capture
"""
try :
adapter = self . _ethernet_adapters [ adapter_number ]
except KeyError :
raise VMwareError ( " Adapter {adapter_number} doesn ' t exist on VMware VM ' {name} ' " . format ( name = self . name ,
adapter_number = adapter_number ) )
nio = adapter . get_nio ( 0 )
if not nio :
raise VMwareError ( " Adapter {} is not connected " . format ( adapter_number ) )
if nio . capturing :
raise VMwareError ( " Packet capture is already activated on adapter {adapter_number} " . format ( adapter_number = adapter_number ) )
nio . startPacketCapture ( output_file )
if self . _started :
yield from self . _start_ubridge_capture ( adapter_number , output_file )
log . info ( " VMware VM ' {name} ' [ {id} ]: starting packet capture on adapter {adapter_number} " . format ( name = self . name ,
id = self . id ,
adapter_number = adapter_number ) )
def stop_capture ( self , adapter_number ) :
"""
Stops a packet capture .
: param adapter_number : adapter number
"""
try :
adapter = self . _ethernet_adapters [ adapter_number ]
except KeyError :
2016-02-09 15:07:33 +00:00
raise VMwareError ( " Adapter {adapter_number} doesn ' t exist on VMware VM ' {name} ' " . format ( name = self . name ,
adapter_number = adapter_number ) )
2015-09-13 15:40:09 +00:00
nio = adapter . get_nio ( 0 )
if not nio :
raise VMwareError ( " Adapter {} is not connected " . format ( adapter_number ) )
nio . stopPacketCapture ( )
if self . _started :
yield from self . _stop_ubridge_capture ( adapter_number )
log . info ( " VMware VM ' {name} ' [ {id} ]: stopping packet capture on adapter {adapter_number} " . format ( name = self . name ,
id = self . id ,
adapter_number = adapter_number ) )