2016-06-24 03:56:42 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 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 os
|
|
|
|
import logging
|
|
|
|
import asyncio
|
2016-08-11 21:58:29 +00:00
|
|
|
import psutil
|
2016-06-24 03:56:42 +00:00
|
|
|
|
|
|
|
from gns3server.compute.vmware import (
|
|
|
|
VMware,
|
|
|
|
VMwareError
|
|
|
|
)
|
|
|
|
|
|
|
|
from .base_gns3_vm import BaseGNS3VM
|
|
|
|
from .gns3_vm_error import GNS3VMError
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class VMwareGNS3VM(BaseGNS3VM):
|
|
|
|
|
2016-08-30 08:19:01 +00:00
|
|
|
def __init__(self, controller):
|
2016-06-24 03:56:42 +00:00
|
|
|
|
2016-07-11 23:01:18 +00:00
|
|
|
self._engine = "vmware"
|
2016-08-30 08:19:01 +00:00
|
|
|
super().__init__(controller)
|
2016-06-24 03:56:42 +00:00
|
|
|
self._vmware_manager = VMware()
|
2016-07-11 23:01:18 +00:00
|
|
|
self._vmx_path = None
|
2016-06-24 03:56:42 +00:00
|
|
|
|
2016-10-25 09:56:24 +00:00
|
|
|
@property
|
|
|
|
def vmx_path(self):
|
|
|
|
return self._vmx_path
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def _execute(self, subcommand, args, timeout=60, log_level=logging.INFO):
|
2016-06-24 03:56:42 +00:00
|
|
|
|
|
|
|
try:
|
2018-10-15 10:05:49 +00:00
|
|
|
result = await self._vmware_manager.execute(subcommand, args, timeout, log_level=log_level)
|
2016-07-11 23:01:18 +00:00
|
|
|
return (''.join(result))
|
2016-06-24 03:56:42 +00:00
|
|
|
except VMwareError as e:
|
|
|
|
raise GNS3VMError("Error while executing VMware command: {}".format(e))
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def _is_running(self):
|
|
|
|
result = await self._vmware_manager.execute("list", [])
|
2016-09-21 13:46:56 +00:00
|
|
|
if self._vmx_path in result:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def _set_vcpus_ram(self, vcpus, ram):
|
2016-07-11 23:01:18 +00:00
|
|
|
"""
|
|
|
|
Set the number of vCPU cores and amount of RAM for the GNS3 VM.
|
|
|
|
|
|
|
|
:param vcpus: number of vCPU cores
|
|
|
|
:param ram: amount of RAM
|
|
|
|
"""
|
|
|
|
|
2016-08-11 21:58:29 +00:00
|
|
|
# memory must be a multiple of 4 (VMware requirement)
|
|
|
|
if ram % 4 != 0:
|
2016-11-11 09:25:06 +00:00
|
|
|
raise GNS3VMError("Allocated memory {} for the GNS3 VM must be a multiple of 4".format(ram))
|
2016-08-11 21:58:29 +00:00
|
|
|
|
2019-03-18 10:53:14 +00:00
|
|
|
available_vcpus = psutil.cpu_count(logical=True)
|
2019-08-27 11:01:46 +00:00
|
|
|
if not float(vcpus).is_integer():
|
|
|
|
raise GNS3VMError("The allocated vCPUs value is not an integer: {}".format(vcpus))
|
2016-08-11 21:58:29 +00:00
|
|
|
if vcpus > available_vcpus:
|
|
|
|
raise GNS3VMError("You have allocated too many vCPUs for the GNS3 VM! (max available is {} vCPUs)".format(available_vcpus))
|
|
|
|
|
2016-07-11 23:01:18 +00:00
|
|
|
try:
|
|
|
|
pairs = VMware.parse_vmware_file(self._vmx_path)
|
2019-03-18 10:53:14 +00:00
|
|
|
if vcpus > 1:
|
|
|
|
pairs["numvcpus"] = str(vcpus)
|
|
|
|
cores_per_sockets = int(vcpus / psutil.cpu_count(logical=False))
|
|
|
|
if cores_per_sockets > 1:
|
|
|
|
pairs["cpuid.corespersocket"] = str(cores_per_sockets)
|
|
|
|
pairs["memsize"] = str(ram)
|
|
|
|
VMware.write_vmx_file(self._vmx_path, pairs)
|
2016-07-11 23:01:18 +00:00
|
|
|
log.info("GNS3 VM vCPU count set to {} and RAM amount set to {}".format(vcpus, ram))
|
|
|
|
except OSError as e:
|
|
|
|
raise GNS3VMError('Could not read/write VMware VMX file "{}": {}'.format(self._vmx_path, e))
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def _set_extra_options(self):
|
2017-10-18 08:32:56 +00:00
|
|
|
try:
|
|
|
|
"""
|
2017-10-18 08:53:37 +00:00
|
|
|
Due to bug/change in VMWare 14 we're not able to pass Hardware Virtualization in GNS3VM.
|
2017-10-18 08:32:56 +00:00
|
|
|
We only enable this when it's not present in current configuration and user hasn't deactivated that.
|
|
|
|
"""
|
|
|
|
extra_config = (
|
|
|
|
("vhv.enable", "TRUE"),
|
|
|
|
)
|
|
|
|
pairs = VMware.parse_vmware_file(self._vmx_path)
|
|
|
|
updated = False
|
|
|
|
for key, value in extra_config:
|
|
|
|
if key not in pairs.keys():
|
|
|
|
pairs[key] = value
|
|
|
|
updated = True
|
|
|
|
log.info("GNS3 VM VMX `{}` set to `{}`".format(key, value))
|
|
|
|
|
|
|
|
if updated:
|
|
|
|
VMware.write_vmx_file(self._vmx_path, pairs)
|
|
|
|
log.info("GNS3 VM VMX has been updated.")
|
|
|
|
except OSError as e:
|
|
|
|
raise GNS3VMError('Could not read/write VMware VMX file "{}": {}'.format(self._vmx_path, e))
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def list(self):
|
2016-07-11 23:01:18 +00:00
|
|
|
"""
|
|
|
|
List all VMware VMs
|
|
|
|
"""
|
2020-05-20 09:12:50 +00:00
|
|
|
|
2016-11-15 17:15:41 +00:00
|
|
|
try:
|
2018-10-15 10:05:49 +00:00
|
|
|
return (await self._vmware_manager.list_vms())
|
2016-11-15 17:15:41 +00:00
|
|
|
except VMwareError as e:
|
|
|
|
raise GNS3VMError("Could not list VMware VMs: {}".format(str(e)))
|
2016-07-11 23:01:18 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def start(self):
|
2016-06-24 03:56:42 +00:00
|
|
|
"""
|
|
|
|
Starts the GNS3 VM.
|
|
|
|
"""
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
vms = await self.list()
|
2016-07-11 23:01:18 +00:00
|
|
|
for vm in vms:
|
|
|
|
if vm["vmname"] == self.vmname:
|
|
|
|
self._vmx_path = vm["vmx_path"]
|
|
|
|
break
|
|
|
|
|
2016-06-24 03:56:42 +00:00
|
|
|
# check we have a valid VMX file path
|
|
|
|
if not self._vmx_path:
|
2016-08-25 09:49:06 +00:00
|
|
|
raise GNS3VMError("VMWare VM {} not found".format(self.vmname))
|
2016-06-24 03:56:42 +00:00
|
|
|
if not os.path.exists(self._vmx_path):
|
2016-07-11 23:01:18 +00:00
|
|
|
raise GNS3VMError("VMware VMX file {} doesn't exist".format(self._vmx_path))
|
|
|
|
|
2016-06-24 03:56:42 +00:00
|
|
|
# check if the VMware guest tools are installed
|
2018-10-15 10:05:49 +00:00
|
|
|
vmware_tools_state = await self._execute("checkToolsState", [self._vmx_path])
|
2016-06-24 03:56:42 +00:00
|
|
|
if vmware_tools_state not in ("installed", "running"):
|
|
|
|
raise GNS3VMError("VMware tools are not installed in {}".format(self.vmname))
|
|
|
|
|
2017-05-09 17:16:47 +00:00
|
|
|
try:
|
2018-10-15 10:05:49 +00:00
|
|
|
running = await self._is_running()
|
2017-05-09 17:16:47 +00:00
|
|
|
except VMwareError as e:
|
|
|
|
raise GNS3VMError("Could not list VMware VMs: {}".format(str(e)))
|
2016-09-21 13:46:56 +00:00
|
|
|
if not running:
|
2019-10-09 06:50:00 +00:00
|
|
|
log.info("Update GNS3 VM settings (CPU, RAM and Hardware Virtualization)")
|
2016-09-21 13:46:56 +00:00
|
|
|
# set the number of vCPUs and amount of RAM
|
2018-10-15 10:05:49 +00:00
|
|
|
await self._set_vcpus_ram(self.vcpus, self.ram)
|
|
|
|
await self._set_extra_options()
|
2016-09-21 13:46:56 +00:00
|
|
|
|
|
|
|
# start the VM
|
|
|
|
args = [self._vmx_path]
|
|
|
|
if self._headless:
|
|
|
|
args.extend(["nogui"])
|
2018-10-15 10:05:49 +00:00
|
|
|
await self._execute("start", args)
|
2016-09-21 13:46:56 +00:00
|
|
|
log.info("GNS3 VM has been started")
|
2016-10-25 16:17:49 +00:00
|
|
|
|
2016-06-24 03:56:42 +00:00
|
|
|
# get the guest IP address (first adapter only)
|
2016-10-25 16:17:49 +00:00
|
|
|
trial = 120
|
|
|
|
guest_ip_address = ""
|
2016-11-11 10:41:16 +00:00
|
|
|
log.info("Waiting for GNS3 VM IP")
|
2016-10-25 16:17:49 +00:00
|
|
|
while True:
|
2019-10-08 09:58:27 +00:00
|
|
|
try:
|
|
|
|
guest_ip_address = await self._execute("readVariable", [self._vmx_path, "guestVar", "gns3.eth0"], timeout=120, log_level=logging.DEBUG)
|
|
|
|
guest_ip_address = guest_ip_address.strip()
|
|
|
|
if len(guest_ip_address) != 0:
|
|
|
|
break
|
|
|
|
trial -= 1
|
|
|
|
# If IP address not found then fallback an old method
|
|
|
|
if trial == 0:
|
|
|
|
log.warning("No IP found for the VM via readVariable fallback to getGuestIPAddress")
|
|
|
|
guest_ip_address = await self._execute("getGuestIPAddress", [self._vmx_path, "-wait"], timeout=120)
|
|
|
|
break
|
|
|
|
except GNS3VMError as e:
|
|
|
|
log.debug("{}".format(e))
|
2018-10-15 10:05:49 +00:00
|
|
|
await asyncio.sleep(1)
|
2019-10-08 09:58:27 +00:00
|
|
|
if not guest_ip_address:
|
|
|
|
raise GNS3VMError("Could not find guest IP address for {}".format(self.vmname))
|
2016-06-24 03:56:42 +00:00
|
|
|
self.ip_address = guest_ip_address
|
|
|
|
log.info("GNS3 VM IP address set to {}".format(guest_ip_address))
|
2016-09-01 13:36:41 +00:00
|
|
|
self.running = True
|
2016-06-24 03:56:42 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def suspend(self):
|
2016-09-08 13:32:35 +00:00
|
|
|
"""
|
|
|
|
Suspend the GNS3 VM.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self._vmx_path is None:
|
|
|
|
raise GNS3VMError("No VMX path configured, can't suspend the VM")
|
|
|
|
try:
|
2018-10-15 10:05:49 +00:00
|
|
|
await self._execute("suspend", [self._vmx_path])
|
2016-09-08 13:32:35 +00:00
|
|
|
except GNS3VMError as e:
|
|
|
|
log.warning("Error when suspending the VM: {}".format(str(e)))
|
|
|
|
log.info("GNS3 VM has been suspended")
|
|
|
|
self.running = False
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def stop(self):
|
2016-06-24 03:56:42 +00:00
|
|
|
"""
|
|
|
|
Stops the GNS3 VM.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self._vmx_path is None:
|
2016-07-11 23:01:18 +00:00
|
|
|
raise GNS3VMError("No VMX path configured, can't stop the VM")
|
2016-08-29 11:53:53 +00:00
|
|
|
try:
|
2018-10-15 10:05:49 +00:00
|
|
|
await self._execute("stop", [self._vmx_path, "soft"])
|
2016-08-29 13:53:10 +00:00
|
|
|
except GNS3VMError as e:
|
2016-08-29 11:53:53 +00:00
|
|
|
log.warning("Error when stopping the VM: {}".format(str(e)))
|
2016-06-24 03:56:42 +00:00
|
|
|
log.info("GNS3 VM has been stopped")
|
|
|
|
self.running = False
|