2014-07-12 19:18:25 +00:00
|
|
|
# -*- 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/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
VirtualBox VM instance.
|
|
|
|
"""
|
|
|
|
|
2014-10-31 00:53:17 +00:00
|
|
|
import sys
|
|
|
|
import shlex
|
|
|
|
import re
|
2014-07-12 19:18:25 +00:00
|
|
|
import os
|
2014-10-31 00:53:17 +00:00
|
|
|
import tempfile
|
2014-11-09 18:50:47 +00:00
|
|
|
import json
|
2014-10-31 00:53:17 +00:00
|
|
|
import socket
|
2015-01-20 01:30:57 +00:00
|
|
|
import asyncio
|
2014-07-12 19:18:25 +00:00
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
from pkg_resources import parse_version
|
2015-05-28 03:06:18 +00:00
|
|
|
from gns3server.utils.telnet_server import TelnetServer
|
2015-11-02 01:51:12 +00:00
|
|
|
from gns3server.utils.asyncio import wait_for_file_creation, wait_for_named_pipe_creation
|
2014-07-12 19:18:25 +00:00
|
|
|
from .virtualbox_error import VirtualBoxError
|
2015-02-24 02:00:34 +00:00
|
|
|
from ..nios.nio_udp import NIOUDP
|
2015-05-06 21:21:39 +00:00
|
|
|
from ..nios.nio_nat import NIONAT
|
2015-01-18 23:26:56 +00:00
|
|
|
from ..adapters.ethernet_adapter import EthernetAdapter
|
2015-01-20 01:30:57 +00:00
|
|
|
from ..base_vm import BaseVM
|
2014-10-31 00:53:17 +00:00
|
|
|
|
|
|
|
if sys.platform.startswith('win'):
|
|
|
|
import msvcrt
|
|
|
|
import win32file
|
2014-07-12 19:18:25 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2014-07-18 02:02:18 +00:00
|
|
|
|
2015-01-20 01:30:57 +00:00
|
|
|
class VirtualBoxVM(BaseVM):
|
2015-01-31 21:34:49 +00:00
|
|
|
|
2014-07-12 19:18:25 +00:00
|
|
|
"""
|
|
|
|
VirtualBox VM implementation.
|
|
|
|
"""
|
|
|
|
|
2015-03-03 00:28:28 +00:00
|
|
|
def __init__(self, name, vm_id, project, manager, vmname, linked_clone, console=None, adapters=0):
|
2015-01-20 01:30:57 +00:00
|
|
|
|
2015-03-03 00:28:28 +00:00
|
|
|
super().__init__(name, vm_id, project, manager, console=console)
|
2015-01-20 01:30:57 +00:00
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
self._maximum_adapters = 8
|
2015-01-21 02:02:22 +00:00
|
|
|
self._linked_clone = linked_clone
|
|
|
|
self._system_properties = {}
|
2015-01-22 02:26:39 +00:00
|
|
|
self._telnet_server_thread = None
|
|
|
|
self._serial_pipe = None
|
2015-01-20 01:30:57 +00:00
|
|
|
|
2014-07-12 19:18:25 +00:00
|
|
|
# VirtualBox settings
|
2015-01-31 02:36:05 +00:00
|
|
|
self._adapters = adapters
|
2015-04-03 10:08:18 +00:00
|
|
|
self._ethernet_adapters = {}
|
2014-07-17 21:28:02 +00:00
|
|
|
self._headless = False
|
2015-06-02 22:30:35 +00:00
|
|
|
self._acpi_shutdown = False
|
2015-01-22 02:26:39 +00:00
|
|
|
self._enable_remote_console = False
|
2014-07-17 21:28:02 +00:00
|
|
|
self._vmname = vmname
|
2015-02-07 00:31:13 +00:00
|
|
|
self._use_any_adapter = False
|
2015-03-13 23:13:36 +00:00
|
|
|
self._ram = 0
|
2014-11-05 02:00:01 +00:00
|
|
|
self._adapter_type = "Intel PRO/1000 MT Desktop (82540EM)"
|
2014-07-12 19:18:25 +00:00
|
|
|
|
2015-01-21 22:21:15 +00:00
|
|
|
def __json__(self):
|
2014-11-09 18:50:47 +00:00
|
|
|
|
2015-06-26 15:09:19 +00:00
|
|
|
json = {"name": self.name,
|
2015-02-04 20:48:29 +00:00
|
|
|
"vm_id": self.id,
|
2015-01-23 23:38:46 +00:00
|
|
|
"console": self.console,
|
2015-02-04 20:48:29 +00:00
|
|
|
"project_id": self.project.id,
|
2015-01-23 02:07:09 +00:00
|
|
|
"vmname": self.vmname,
|
|
|
|
"headless": self.headless,
|
2015-06-02 22:30:35 +00:00
|
|
|
"acpi_shutdown": self.acpi_shutdown,
|
2015-01-23 04:31:26 +00:00
|
|
|
"enable_remote_console": self.enable_remote_console,
|
2015-01-31 02:36:05 +00:00
|
|
|
"adapters": self._adapters,
|
2015-01-23 04:31:26 +00:00
|
|
|
"adapter_type": self.adapter_type,
|
2015-03-13 23:13:36 +00:00
|
|
|
"ram": self.ram,
|
2015-02-07 00:31:13 +00:00
|
|
|
"use_any_adapter": self.use_any_adapter}
|
2015-06-26 15:09:19 +00:00
|
|
|
if self._linked_clone:
|
|
|
|
json["vm_directory"] = self.working_dir
|
|
|
|
else:
|
|
|
|
json["vm_directory"] = None
|
|
|
|
return json
|
2014-07-12 19:18:25 +00:00
|
|
|
|
2015-01-20 01:30:57 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def _get_system_properties(self):
|
|
|
|
|
2015-01-24 00:57:54 +00:00
|
|
|
properties = yield from self.manager.execute("list", ["systemproperties"])
|
2015-01-20 01:30:57 +00:00
|
|
|
for prop in properties:
|
|
|
|
try:
|
|
|
|
name, value = prop.split(':', 1)
|
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
self._system_properties[name.strip()] = value.strip()
|
2014-07-12 19:18:25 +00:00
|
|
|
|
2015-01-20 01:30:57 +00:00
|
|
|
@asyncio.coroutine
|
2015-01-22 02:26:39 +00:00
|
|
|
def _get_vm_state(self):
|
|
|
|
"""
|
|
|
|
Returns the VM state (e.g. running, paused etc.)
|
2014-07-12 19:18:25 +00:00
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
:returns: state (string)
|
|
|
|
"""
|
2015-01-20 01:30:57 +00:00
|
|
|
|
2015-01-24 00:57:54 +00:00
|
|
|
results = yield from self.manager.execute("showvminfo", [self._vmname, "--machinereadable"])
|
2015-01-22 02:26:39 +00:00
|
|
|
for info in results:
|
2015-04-14 12:35:48 +00:00
|
|
|
if '=' in info:
|
|
|
|
name, value = info.split('=', 1)
|
|
|
|
if name == "VMState":
|
|
|
|
return value.strip('"')
|
2015-01-22 02:26:39 +00:00
|
|
|
raise VirtualBoxError("Could not get VM state for {}".format(self._vmname))
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def _control_vm(self, params):
|
|
|
|
"""
|
|
|
|
Change setting in this VM when running.
|
|
|
|
|
|
|
|
:param params: params to use with sub-command controlvm
|
|
|
|
|
|
|
|
:returns: result of the command.
|
|
|
|
"""
|
|
|
|
|
|
|
|
args = shlex.split(params)
|
2015-01-24 00:57:54 +00:00
|
|
|
result = yield from self.manager.execute("controlvm", [self._vmname] + args)
|
2015-01-22 02:26:39 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def _modify_vm(self, params):
|
|
|
|
"""
|
|
|
|
Change setting in this VM when not running.
|
|
|
|
|
|
|
|
:param params: params to use with sub-command modifyvm
|
|
|
|
"""
|
2014-07-12 19:18:25 +00:00
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
args = shlex.split(params)
|
2015-01-24 00:57:54 +00:00
|
|
|
yield from self.manager.execute("modifyvm", [self._vmname] + args)
|
2014-07-12 19:18:25 +00:00
|
|
|
|
2015-01-23 02:07:09 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def create(self):
|
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
yield from self._get_system_properties()
|
2015-04-14 12:32:44 +00:00
|
|
|
if "API version" not in self._system_properties:
|
2015-07-09 03:38:58 +00:00
|
|
|
raise VirtualBoxError("Can't access to VirtualBox API version:\n{}".format(self._system_properties))
|
2015-01-22 02:26:39 +00:00
|
|
|
if parse_version(self._system_properties["API version"]) < parse_version("4_3"):
|
|
|
|
raise VirtualBoxError("The VirtualBox API version is lower than 4.3")
|
2015-02-04 20:48:29 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}] created".format(name=self.name, id=self.id))
|
2015-01-22 02:26:39 +00:00
|
|
|
|
|
|
|
if self._linked_clone:
|
2015-02-04 20:48:29 +00:00
|
|
|
if self.id and os.path.isdir(os.path.join(self.working_dir, self._vmname)):
|
2015-01-22 02:26:39 +00:00
|
|
|
vbox_file = os.path.join(self.working_dir, self._vmname, self._vmname + ".vbox")
|
2015-01-24 00:57:54 +00:00
|
|
|
yield from self.manager.execute("registervm", [vbox_file])
|
2016-04-26 21:44:11 +00:00
|
|
|
yield from self._reattach_linked_hdds()
|
2015-01-22 02:26:39 +00:00
|
|
|
else:
|
2015-01-23 02:07:09 +00:00
|
|
|
yield from self._create_linked_clone()
|
2014-07-12 19:18:25 +00:00
|
|
|
|
2015-01-31 19:01:23 +00:00
|
|
|
if self._adapters:
|
|
|
|
yield from self.set_adapters(self._adapters)
|
2015-01-23 04:31:26 +00:00
|
|
|
|
2015-03-13 23:13:36 +00:00
|
|
|
vm_info = yield from self._get_vm_info()
|
2015-03-14 19:16:27 +00:00
|
|
|
if "memory" in vm_info:
|
|
|
|
self._ram = int(vm_info["memory"])
|
2015-03-13 23:13:36 +00:00
|
|
|
|
2015-07-22 04:58:28 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def check_hw_virtualization(self):
|
|
|
|
"""
|
|
|
|
Returns either hardware virtualization is activated or not.
|
|
|
|
|
|
|
|
:returns: boolean
|
|
|
|
"""
|
|
|
|
|
|
|
|
vm_info = yield from self._get_vm_info()
|
|
|
|
if "hwvirtex" in vm_info and vm_info["hwvirtex"] == "on":
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
@asyncio.coroutine
|
2015-01-20 01:30:57 +00:00
|
|
|
def start(self):
|
2015-01-22 02:26:39 +00:00
|
|
|
"""
|
|
|
|
Starts this VirtualBox VM.
|
|
|
|
"""
|
2015-01-20 01:30:57 +00:00
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
# resume the VM if it is paused
|
|
|
|
vm_state = yield from self._get_vm_state()
|
|
|
|
if vm_state == "paused":
|
|
|
|
yield from self.resume()
|
|
|
|
return
|
2015-01-20 01:30:57 +00:00
|
|
|
|
2015-09-08 07:20:46 +00:00
|
|
|
# VM must be powered off to start it
|
|
|
|
if vm_state != "poweroff":
|
|
|
|
raise VirtualBoxError("VirtualBox VM not powered off")
|
2015-01-20 01:30:57 +00:00
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
yield from self._set_network_options()
|
|
|
|
yield from self._set_serial_console()
|
2015-01-20 01:30:57 +00:00
|
|
|
|
2015-10-12 21:57:37 +00:00
|
|
|
# check if there is enough RAM to run
|
|
|
|
self.check_available_ram(self.ram)
|
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
args = [self._vmname]
|
|
|
|
if self._headless:
|
|
|
|
args.extend(["--type", "headless"])
|
2015-01-24 00:57:54 +00:00
|
|
|
result = yield from self.manager.execute("startvm", args)
|
2015-02-04 20:48:29 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}] started".format(name=self.name, id=self.id))
|
2015-01-22 02:26:39 +00:00
|
|
|
log.debug("Start result: {}".format(result))
|
|
|
|
|
|
|
|
# add a guest property to let the VM know about the GNS3 name
|
2015-01-24 00:57:54 +00:00
|
|
|
yield from self.manager.execute("guestproperty", ["set", self._vmname, "NameInGNS3", self.name])
|
2015-01-22 02:26:39 +00:00
|
|
|
# add a guest property to let the VM know about the GNS3 project directory
|
2015-01-24 00:57:54 +00:00
|
|
|
yield from self.manager.execute("guestproperty", ["set", self._vmname, "ProjectDirInGNS3", self.working_dir])
|
2015-01-22 02:26:39 +00:00
|
|
|
|
2015-04-01 15:59:58 +00:00
|
|
|
if self._enable_remote_console and self._console is not None:
|
2015-10-11 21:50:50 +00:00
|
|
|
try:
|
2015-11-02 01:51:12 +00:00
|
|
|
# wait for VirtualBox to create the pipe file.
|
|
|
|
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())
|
2015-10-11 21:50:50 +00:00
|
|
|
except asyncio.TimeoutError:
|
|
|
|
raise VirtualBoxError('Pipe file "{}" for remote console has not been created by VirtualBox'.format(self._get_pipe_name()))
|
2015-01-22 02:26:39 +00:00
|
|
|
self._start_remote_console()
|
2014-07-12 19:18:25 +00:00
|
|
|
|
2015-07-22 04:58:28 +00:00
|
|
|
if (yield from self.check_hw_virtualization()):
|
|
|
|
self._hw_virtualization = True
|
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def stop(self):
|
|
|
|
"""
|
|
|
|
Stops this VirtualBox VM.
|
2014-07-12 19:18:25 +00:00
|
|
|
"""
|
|
|
|
|
2015-07-22 04:58:28 +00:00
|
|
|
self._hw_virtualization = False
|
2015-01-22 02:26:39 +00:00
|
|
|
self._stop_remote_console()
|
|
|
|
vm_state = yield from self._get_vm_state()
|
|
|
|
if vm_state == "running" or vm_state == "paused" or vm_state == "stuck":
|
2015-06-02 22:30:35 +00:00
|
|
|
if self.acpi_shutdown:
|
|
|
|
# use ACPI to shutdown the VM
|
|
|
|
result = yield from self._control_vm("acpipowerbutton")
|
|
|
|
log.debug("ACPI shutdown result: {}".format(result))
|
|
|
|
else:
|
|
|
|
# power off the VM
|
|
|
|
result = yield from self._control_vm("poweroff")
|
|
|
|
log.debug("Stop result: {}".format(result))
|
2014-07-17 21:28:02 +00:00
|
|
|
|
2015-06-02 22:30:35 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}] stopped".format(name=self.name, id=self.id))
|
2015-04-25 15:36:28 +00:00
|
|
|
# yield from asyncio.sleep(0.5) # give some time for VirtualBox to unlock the VM
|
2015-01-22 02:26:39 +00:00
|
|
|
try:
|
|
|
|
# deactivate the first serial port
|
|
|
|
yield from self._modify_vm("--uart1 off")
|
|
|
|
except VirtualBoxError as e:
|
|
|
|
log.warn("Could not deactivate the first serial port: {}".format(e))
|
2014-07-12 19:18:25 +00:00
|
|
|
|
2015-04-03 10:08:18 +00:00
|
|
|
for adapter_number in range(0, self._adapters):
|
2015-02-13 22:41:56 +00:00
|
|
|
nio = self._ethernet_adapters[adapter_number].get_nio(0)
|
2015-02-07 00:31:13 +00:00
|
|
|
if nio:
|
2015-02-13 22:41:56 +00:00
|
|
|
yield from self._modify_vm("--nictrace{} off".format(adapter_number + 1))
|
|
|
|
yield from self._modify_vm("--cableconnected{} off".format(adapter_number + 1))
|
|
|
|
yield from self._modify_vm("--nic{} null".format(adapter_number + 1))
|
2014-07-12 19:18:25 +00:00
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def suspend(self):
|
|
|
|
"""
|
|
|
|
Suspends this VirtualBox VM.
|
2014-07-12 19:18:25 +00:00
|
|
|
"""
|
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
vm_state = yield from self._get_vm_state()
|
|
|
|
if vm_state == "running":
|
|
|
|
yield from self._control_vm("pause")
|
2015-02-04 20:48:29 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}] suspended".format(name=self.name, id=self.id))
|
2015-01-22 02:26:39 +00:00
|
|
|
else:
|
2015-02-04 20:48:29 +00:00
|
|
|
log.warn("VirtualBox VM '{name}' [{id}] cannot be suspended, current state: {state}".format(name=self.name,
|
|
|
|
id=self.id,
|
|
|
|
state=vm_state))
|
2014-07-12 19:18:25 +00:00
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def resume(self):
|
2014-07-12 19:18:25 +00:00
|
|
|
"""
|
2015-01-22 02:26:39 +00:00
|
|
|
Resumes this VirtualBox VM.
|
2014-07-12 19:18:25 +00:00
|
|
|
"""
|
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
yield from self._control_vm("resume")
|
2015-02-04 20:48:29 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}] resumed".format(name=self.name, id=self.id))
|
2015-01-22 02:26:39 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def reload(self):
|
|
|
|
"""
|
|
|
|
Reloads this VirtualBox VM.
|
|
|
|
"""
|
2014-07-12 19:18:25 +00:00
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
result = yield from self._control_vm("reset")
|
2015-02-04 20:48:29 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}] reloaded".format(name=self.name, id=self.id))
|
2015-01-22 02:26:39 +00:00
|
|
|
log.debug("Reload result: {}".format(result))
|
2014-07-12 19:18:25 +00:00
|
|
|
|
2015-01-23 02:07:09 +00:00
|
|
|
@asyncio.coroutine
|
2014-11-09 18:50:47 +00:00
|
|
|
def _get_all_hdd_files(self):
|
|
|
|
|
|
|
|
hdds = []
|
2015-01-24 00:57:54 +00:00
|
|
|
properties = yield from self.manager.execute("list", ["hdds"])
|
2014-11-09 18:50:47 +00:00
|
|
|
for prop in properties:
|
|
|
|
try:
|
|
|
|
name, value = prop.split(':', 1)
|
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
if name.strip() == "Location":
|
|
|
|
hdds.append(value.strip())
|
|
|
|
return hdds
|
|
|
|
|
2015-01-23 02:07:09 +00:00
|
|
|
@asyncio.coroutine
|
2016-04-26 21:44:11 +00:00
|
|
|
def _reattach_linked_hdds(self):
|
|
|
|
"""
|
|
|
|
Reattach linked cloned hard disks.
|
|
|
|
"""
|
2014-11-09 18:50:47 +00:00
|
|
|
|
2015-01-23 02:07:09 +00:00
|
|
|
hdd_info_file = os.path.join(self.working_dir, self._vmname, "hdd_info.json")
|
2014-11-09 18:50:47 +00:00
|
|
|
try:
|
2015-04-25 17:58:34 +00:00
|
|
|
with open(hdd_info_file, "r", encoding="utf-8") as f:
|
2014-11-09 18:50:47 +00:00
|
|
|
hdd_table = json.load(f)
|
2015-09-29 20:15:01 +00:00
|
|
|
except (ValueError, OSError) as e:
|
2014-11-09 18:50:47 +00:00
|
|
|
raise VirtualBoxError("Could not read HDD info file: {}".format(e))
|
|
|
|
|
|
|
|
for hdd_info in hdd_table:
|
2015-01-23 02:07:09 +00:00
|
|
|
hdd_file = os.path.join(self.working_dir, self._vmname, "Snapshots", hdd_info["hdd"])
|
2014-11-09 18:50:47 +00:00
|
|
|
if os.path.exists(hdd_file):
|
2015-02-04 20:48:29 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}] attaching HDD {controller} {port} {device} {medium}".format(name=self.name,
|
|
|
|
id=self.id,
|
|
|
|
controller=hdd_info["controller"],
|
|
|
|
port=hdd_info["port"],
|
|
|
|
device=hdd_info["device"],
|
|
|
|
medium=hdd_file))
|
2015-02-07 00:31:13 +00:00
|
|
|
|
2016-04-26 21:44:11 +00:00
|
|
|
try:
|
|
|
|
yield from self._storage_attach('--storagectl "{}" --port {} --device {} --type hdd --medium "{}"'.format(hdd_info["controller"],
|
|
|
|
hdd_info["port"],
|
|
|
|
hdd_info["device"],
|
|
|
|
hdd_file))
|
|
|
|
|
|
|
|
except VirtualBoxError as e:
|
|
|
|
log.warn("VirtualBox VM '{name}' [{id}] error reattaching HDD {controller} {port} {device} {medium}: {error}".format(name=self.name,
|
|
|
|
id=self.id,
|
|
|
|
controller=hdd_info["controller"],
|
|
|
|
port=hdd_info["port"],
|
|
|
|
device=hdd_info["device"],
|
|
|
|
medium=hdd_file,
|
|
|
|
error=e))
|
|
|
|
continue
|
2014-11-09 18:50:47 +00:00
|
|
|
|
2015-01-23 02:07:09 +00:00
|
|
|
@asyncio.coroutine
|
2016-04-26 21:44:11 +00:00
|
|
|
def save_linked_hdds_info(self):
|
2014-07-12 19:18:25 +00:00
|
|
|
"""
|
2016-04-26 21:44:11 +00:00
|
|
|
Save linked cloned hard disks information.
|
2014-07-12 19:18:25 +00:00
|
|
|
|
2016-04-26 21:44:11 +00:00
|
|
|
:returns: disk table information
|
|
|
|
"""
|
2015-02-24 02:00:34 +00:00
|
|
|
|
2016-04-26 21:44:11 +00:00
|
|
|
hdd_table = []
|
2014-11-09 18:50:47 +00:00
|
|
|
if self._linked_clone:
|
2015-01-23 23:38:46 +00:00
|
|
|
if os.path.exists(self.working_dir):
|
2015-01-23 02:07:09 +00:00
|
|
|
hdd_files = yield from self._get_all_hdd_files()
|
2015-02-03 00:00:29 +00:00
|
|
|
vm_info = yield from self._get_vm_info()
|
2014-11-15 23:05:55 +00:00
|
|
|
for entry, value in vm_info.items():
|
2016-04-26 21:06:22 +00:00
|
|
|
match = re.search("^([\s\w]+)\-(\d)\-(\d)$", entry) # match Controller-PortNumber-DeviceNumber entry
|
2014-11-15 23:05:55 +00:00
|
|
|
if match:
|
|
|
|
controller = match.group(1)
|
|
|
|
port = match.group(2)
|
|
|
|
device = match.group(3)
|
2016-04-26 21:06:22 +00:00
|
|
|
if value in hdd_files and os.path.exists(os.path.join(self.working_dir, self._vmname, "Snapshots", os.path.basename(value))):
|
2015-02-04 20:48:29 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}] detaching HDD {controller} {port} {device}".format(name=self.name,
|
|
|
|
id=self.id,
|
|
|
|
controller=controller,
|
|
|
|
port=port,
|
|
|
|
device=device))
|
2014-11-15 23:05:55 +00:00
|
|
|
hdd_table.append(
|
|
|
|
{
|
|
|
|
"hdd": os.path.basename(value),
|
|
|
|
"controller": controller,
|
|
|
|
"port": port,
|
|
|
|
"device": device,
|
|
|
|
}
|
|
|
|
)
|
2014-11-09 18:50:47 +00:00
|
|
|
|
2014-11-15 23:05:55 +00:00
|
|
|
if hdd_table:
|
|
|
|
try:
|
2015-01-23 23:38:46 +00:00
|
|
|
hdd_info_file = os.path.join(self.working_dir, self._vmname, "hdd_info.json")
|
2015-04-25 17:58:34 +00:00
|
|
|
with open(hdd_info_file, "w", encoding="utf-8") as f:
|
2014-11-15 23:05:55 +00:00
|
|
|
json.dump(hdd_table, f, indent=4)
|
|
|
|
except OSError as e:
|
2015-02-04 20:48:29 +00:00
|
|
|
log.warning("VirtualBox VM '{name}' [{id}] could not write HHD info file: {error}".format(name=self.name,
|
|
|
|
id=self.id,
|
|
|
|
error=e.strerror))
|
2014-11-09 18:50:47 +00:00
|
|
|
|
2016-04-26 21:44:11 +00:00
|
|
|
return hdd_table
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def close(self):
|
|
|
|
"""
|
|
|
|
Closes this VirtualBox VM.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self._closed:
|
|
|
|
# VM is already closed
|
|
|
|
return
|
|
|
|
|
2016-04-28 14:52:29 +00:00
|
|
|
if not (yield from super().close()):
|
|
|
|
return False
|
|
|
|
|
2016-04-26 21:44:11 +00:00
|
|
|
log.debug("VirtualBox VM '{name}' [{id}] is closing".format(name=self.name, id=self.id))
|
|
|
|
if self._console:
|
|
|
|
self._manager.port_manager.release_tcp_port(self._console, self._project)
|
|
|
|
self._console = None
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
self.acpi_shutdown = False
|
|
|
|
yield from self.stop()
|
|
|
|
|
|
|
|
if self._linked_clone:
|
|
|
|
hdd_table = yield from self.save_linked_hdds_info()
|
|
|
|
for hdd in hdd_table.copy():
|
|
|
|
log.info("VirtualBox VM '{name}' [{id}] detaching HDD {controller} {port} {device}".format(name=self.name,
|
|
|
|
id=self.id,
|
|
|
|
controller=hdd["controller"],
|
|
|
|
port=hdd["port"],
|
|
|
|
device=hdd["device"]))
|
|
|
|
try:
|
|
|
|
yield from self._storage_attach('--storagectl "{}" --port {} --device {} --type hdd --medium none'.format(hdd["controller"],
|
|
|
|
hdd["port"],
|
|
|
|
hdd["device"]))
|
|
|
|
except VirtualBoxError as e:
|
|
|
|
log.warn("VirtualBox VM '{name}' [{id}] error detaching HDD {controller} {port} {device}: {error}".format(name=self.name,
|
|
|
|
id=self.id,
|
|
|
|
controller=hdd["controller"],
|
|
|
|
port=hdd["port"],
|
|
|
|
device=hdd["device"],
|
|
|
|
error=e))
|
|
|
|
continue
|
|
|
|
|
|
|
|
log.info("VirtualBox VM '{name}' [{id}] unregistering".format(name=self.name, id=self.id))
|
|
|
|
yield from self.manager.execute("unregistervm", [self._name])
|
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}] closed".format(name=self.name, id=self.id))
|
2015-02-03 01:56:13 +00:00
|
|
|
self._closed = True
|
2014-07-12 19:18:25 +00:00
|
|
|
|
2014-07-17 21:28:02 +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:
|
2015-02-04 20:48:29 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}] has enabled the headless mode".format(name=self.name, id=self.id))
|
2014-07-17 21:28:02 +00:00
|
|
|
else:
|
2015-02-04 20:48:29 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}] has disabled the headless mode".format(name=self.name, id=self.id))
|
2014-07-17 21:28:02 +00:00
|
|
|
self._headless = headless
|
|
|
|
|
2015-06-02 22:30:35 +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("VirtualBox VM '{name}' [{id}] has enabled the ACPI shutdown mode".format(name=self.name, id=self.id))
|
|
|
|
else:
|
|
|
|
log.info("VirtualBox VM '{name}' [{id}] has disabled the ACPI shutdown mode".format(name=self.name, id=self.id))
|
|
|
|
self._acpi_shutdown = acpi_shutdown
|
|
|
|
|
2014-08-26 21:27:43 +00:00
|
|
|
@property
|
2014-11-06 20:56:19 +00:00
|
|
|
def enable_remote_console(self):
|
2014-08-26 21:27:43 +00:00
|
|
|
"""
|
2014-11-06 20:56:19 +00:00
|
|
|
Returns either the remote console is enabled or not
|
2014-08-26 21:27:43 +00:00
|
|
|
|
|
|
|
:returns: boolean
|
|
|
|
"""
|
|
|
|
|
2014-11-06 20:56:19 +00:00
|
|
|
return self._enable_remote_console
|
2014-08-26 21:27:43 +00:00
|
|
|
|
2015-03-03 00:28:28 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def set_enable_remote_console(self, enable_remote_console):
|
2014-08-26 21:27:43 +00:00
|
|
|
"""
|
|
|
|
Sets either the console is enabled or not
|
|
|
|
|
2014-11-06 20:56:19 +00:00
|
|
|
:param enable_remote_console: boolean
|
2014-08-26 21:27:43 +00:00
|
|
|
"""
|
|
|
|
|
2014-11-06 20:56:19 +00:00
|
|
|
if enable_remote_console:
|
2015-02-04 20:48:29 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}] has enabled the console".format(name=self.name, id=self.id))
|
2015-03-03 00:28:28 +00:00
|
|
|
vm_state = yield from self._get_vm_state()
|
|
|
|
if vm_state == "running":
|
|
|
|
self._start_remote_console()
|
2014-08-26 21:27:43 +00:00
|
|
|
else:
|
2015-02-04 20:48:29 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}] has disabled the console".format(name=self.name, id=self.id))
|
2015-02-07 00:31:13 +00:00
|
|
|
self._stop_remote_console()
|
2014-11-06 20:56:19 +00:00
|
|
|
self._enable_remote_console = enable_remote_console
|
2014-08-26 21:27:43 +00:00
|
|
|
|
2015-03-13 23:13:36 +00:00
|
|
|
@property
|
|
|
|
def ram(self):
|
|
|
|
"""
|
|
|
|
Returns the amount of RAM allocated to this VirtualBox VM.
|
|
|
|
|
|
|
|
:returns: amount RAM in MB (integer)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._ram
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def set_ram(self, ram):
|
|
|
|
"""
|
|
|
|
Set the amount of RAM allocated to this VirtualBox VM.
|
|
|
|
|
|
|
|
:param ram: amount RAM in MB (integer)
|
|
|
|
"""
|
|
|
|
|
|
|
|
if ram == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
yield from self._modify_vm('--memory {}'.format(ram))
|
|
|
|
|
|
|
|
log.info("VirtualBox VM '{name}' [{id}] has set amount of RAM to {ram}".format(name=self.name, id=self.id, ram=ram))
|
|
|
|
self._ram = ram
|
|
|
|
|
2014-07-17 21:28:02 +00:00
|
|
|
@property
|
|
|
|
def vmname(self):
|
|
|
|
"""
|
2015-03-08 18:32:36 +00:00
|
|
|
Returns the VirtualBox VM name.
|
2014-07-17 21:28:02 +00:00
|
|
|
|
|
|
|
:returns: VirtualBox VM name
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._vmname
|
|
|
|
|
2015-03-08 18:32:36 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def set_vmname(self, vmname):
|
2014-07-17 21:28:02 +00:00
|
|
|
"""
|
2015-03-08 18:32:36 +00:00
|
|
|
Renames the VirtualBox VM.
|
2014-07-17 21:28:02 +00:00
|
|
|
|
|
|
|
:param vmname: VirtualBox VM name
|
|
|
|
"""
|
|
|
|
|
2015-03-08 18:32:36 +00:00
|
|
|
if self._linked_clone:
|
|
|
|
yield from self._modify_vm('--name "{}"'.format(vmname))
|
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}] has set the VM name to '{vmname}'".format(name=self.name, id=self.id, vmname=vmname))
|
2014-07-17 21:28:02 +00:00
|
|
|
self._vmname = vmname
|
|
|
|
|
2015-02-07 00:31:13 +00:00
|
|
|
@property
|
|
|
|
def adapters(self):
|
|
|
|
"""
|
|
|
|
Returns the number of adapters configured for this VirtualBox VM.
|
|
|
|
|
|
|
|
:returns: number of adapters
|
|
|
|
"""
|
2015-02-05 18:58:10 +00:00
|
|
|
|
2015-02-07 00:31:13 +00:00
|
|
|
return self._adapters
|
2015-02-05 18:58:10 +00:00
|
|
|
|
2015-01-23 04:31:26 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def set_adapters(self, adapters):
|
2014-07-17 21:28:02 +00:00
|
|
|
"""
|
|
|
|
Sets the number of Ethernet adapters for this VirtualBox VM instance.
|
|
|
|
|
|
|
|
:param adapters: number of adapters
|
|
|
|
"""
|
|
|
|
|
2014-10-31 23:41:12 +00:00
|
|
|
# check for the maximum adapters supported by the VM
|
2015-01-23 04:31:26 +00:00
|
|
|
self._maximum_adapters = yield from self._get_maximum_supported_adapters()
|
2015-04-03 10:08:18 +00:00
|
|
|
if adapters > self._maximum_adapters:
|
2014-10-31 23:41:12 +00:00
|
|
|
raise VirtualBoxError("Number of adapters above the maximum supported of {}".format(self._maximum_adapters))
|
|
|
|
|
2014-07-17 21:28:02 +00:00
|
|
|
self._ethernet_adapters.clear()
|
2015-02-13 22:41:56 +00:00
|
|
|
for adapter_number in range(0, adapters):
|
2015-04-03 10:08:18 +00:00
|
|
|
self._ethernet_adapters[adapter_number] = EthernetAdapter()
|
2014-07-17 21:28:02 +00:00
|
|
|
|
2015-01-31 02:36:05 +00:00
|
|
|
self._adapters = len(self._ethernet_adapters)
|
2015-02-04 20:48:29 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}] has changed the number of Ethernet adapters to {adapters}".format(name=self.name,
|
|
|
|
id=self.id,
|
|
|
|
adapters=adapters))
|
2014-08-26 21:27:43 +00:00
|
|
|
|
|
|
|
@property
|
2015-02-07 00:31:13 +00:00
|
|
|
def use_any_adapter(self):
|
2014-08-26 21:27:43 +00:00
|
|
|
"""
|
2015-02-07 00:31:13 +00:00
|
|
|
Returns either GNS3 can use any VirtualBox adapter on this instance.
|
2014-08-26 21:27:43 +00:00
|
|
|
|
2015-05-22 03:48:59 +00:00
|
|
|
:returns: boolean
|
2014-08-26 21:27:43 +00:00
|
|
|
"""
|
|
|
|
|
2015-02-07 00:31:13 +00:00
|
|
|
return self._use_any_adapter
|
2014-08-26 21:27:43 +00:00
|
|
|
|
2015-02-07 00:31:13 +00:00
|
|
|
@use_any_adapter.setter
|
|
|
|
def use_any_adapter(self, use_any_adapter):
|
2014-08-26 21:27:43 +00:00
|
|
|
"""
|
2015-02-07 00:31:13 +00:00
|
|
|
Allows GNS3 to use any VirtualBox adapter on this instance.
|
2014-08-26 21:27:43 +00:00
|
|
|
|
2015-02-07 00:31:13 +00:00
|
|
|
:param use_any_adapter: boolean
|
2014-08-26 21:27:43 +00:00
|
|
|
"""
|
|
|
|
|
2015-02-07 00:31:13 +00:00
|
|
|
if use_any_adapter:
|
|
|
|
log.info("VirtualBox VM '{name}' [{id}] is allowed to use any adapter".format(name=self.name, id=self.id))
|
|
|
|
else:
|
2015-05-22 03:48:59 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}] is not allowed to use any adapter".format(name=self.name, id=self.id))
|
2015-02-07 00:31:13 +00:00
|
|
|
self._use_any_adapter = use_any_adapter
|
2014-07-17 21:28:02 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def adapter_type(self):
|
|
|
|
"""
|
|
|
|
Returns the adapter type for this VirtualBox 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 VirtualBox VM instance.
|
|
|
|
|
|
|
|
:param adapter_type: adapter type (string)
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._adapter_type = adapter_type
|
2015-02-04 20:48:29 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}]: adapter type changed to {adapter_type}".format(name=self.name,
|
|
|
|
id=self.id,
|
|
|
|
adapter_type=adapter_type))
|
2014-10-31 00:53:17 +00:00
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
@asyncio.coroutine
|
2014-10-31 00:53:17 +00:00
|
|
|
def _get_vm_info(self):
|
|
|
|
"""
|
|
|
|
Returns this VM info.
|
|
|
|
|
|
|
|
:returns: dict of info
|
|
|
|
"""
|
|
|
|
|
|
|
|
vm_info = {}
|
2015-01-24 00:57:54 +00:00
|
|
|
results = yield from self.manager.execute("showvminfo", [self._vmname, "--machinereadable"])
|
2014-10-31 00:53:17 +00:00
|
|
|
for info in results:
|
|
|
|
try:
|
2014-11-09 18:50:47 +00:00
|
|
|
name, value = info.split('=', 1)
|
2014-10-31 00:53:17 +00:00
|
|
|
except ValueError:
|
|
|
|
continue
|
2014-11-09 18:50:47 +00:00
|
|
|
vm_info[name.strip('"')] = value.strip('"')
|
2014-10-31 00:53:17 +00:00
|
|
|
return vm_info
|
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
@asyncio.coroutine
|
2014-10-31 00:53:17 +00:00
|
|
|
def _get_maximum_supported_adapters(self):
|
|
|
|
"""
|
|
|
|
Returns the maximum adapters supported by this VM.
|
|
|
|
|
|
|
|
:returns: maximum number of supported adapters (int)
|
|
|
|
"""
|
|
|
|
|
|
|
|
# check the maximum number of adapters supported by the VM
|
2015-01-22 02:26:39 +00:00
|
|
|
vm_info = yield from self._get_vm_info()
|
2014-10-31 00:53:17 +00:00
|
|
|
maximum_adapters = 8
|
2015-07-25 22:39:38 +00:00
|
|
|
if "chipset" in vm_info:
|
|
|
|
chipset = vm_info["chipset"]
|
|
|
|
if chipset == "ich9":
|
|
|
|
maximum_adapters = int(self._system_properties["Maximum ICH9 Network Adapter count"])
|
2014-10-31 00:53:17 +00:00
|
|
|
return maximum_adapters
|
|
|
|
|
|
|
|
def _get_pipe_name(self):
|
|
|
|
"""
|
|
|
|
Returns the pipe name to create a serial connection.
|
|
|
|
|
|
|
|
:returns: pipe path (string)
|
|
|
|
"""
|
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
if sys.platform.startswith("win"):
|
2015-04-23 02:29:52 +00:00
|
|
|
pipe_name = r"\\.\pipe\gns3_vbox\{}".format(self.id)
|
2014-10-31 00:53:17 +00:00
|
|
|
else:
|
2015-04-23 02:29:52 +00:00
|
|
|
pipe_name = os.path.join(tempfile.gettempdir(), "gns3_vbox", "{}".format(self.id))
|
|
|
|
try:
|
|
|
|
os.makedirs(os.path.dirname(pipe_name), exist_ok=True)
|
|
|
|
except OSError as e:
|
|
|
|
raise VirtualBoxError("Could not create the VirtualBox pipe directory: {}".format(e))
|
2014-10-31 00:53:17 +00:00
|
|
|
return pipe_name
|
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
@asyncio.coroutine
|
2014-10-31 00:53:17 +00:00
|
|
|
def _set_serial_console(self):
|
|
|
|
"""
|
|
|
|
Configures the first serial port to allow a serial console connection.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# activate the first serial port
|
2015-01-22 02:26:39 +00:00
|
|
|
yield from self._modify_vm("--uart1 0x3F8 4")
|
2014-10-31 00:53:17 +00:00
|
|
|
|
|
|
|
# set server mode with a pipe on the first serial port
|
|
|
|
pipe_name = self._get_pipe_name()
|
|
|
|
args = [self._vmname, "--uartmode1", "server", pipe_name]
|
2015-01-24 00:57:54 +00:00
|
|
|
yield from self.manager.execute("modifyvm", args)
|
2014-10-31 00:53:17 +00:00
|
|
|
|
2015-01-23 02:07:09 +00:00
|
|
|
@asyncio.coroutine
|
2014-11-09 18:50:47 +00:00
|
|
|
def _storage_attach(self, params):
|
|
|
|
"""
|
|
|
|
Change storage medium in this VM.
|
|
|
|
|
|
|
|
:param params: params to use with sub-command storageattach
|
|
|
|
"""
|
|
|
|
|
|
|
|
args = shlex.split(params)
|
2015-01-24 00:57:54 +00:00
|
|
|
yield from self.manager.execute("storageattach", [self._vmname] + args)
|
2014-11-09 18:50:47 +00:00
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
@asyncio.coroutine
|
2014-10-31 00:53:17 +00:00
|
|
|
def _get_nic_attachements(self, maximum_adapters):
|
|
|
|
"""
|
|
|
|
Returns NIC attachements.
|
|
|
|
|
|
|
|
:param maximum_adapters: maximum number of supported adapters
|
|
|
|
:returns: list of adapters with their Attachment setting (NAT, bridged etc.)
|
|
|
|
"""
|
|
|
|
|
|
|
|
nics = []
|
2015-01-22 02:26:39 +00:00
|
|
|
vm_info = yield from self._get_vm_info()
|
2015-02-13 22:41:56 +00:00
|
|
|
for adapter_number in range(0, maximum_adapters):
|
|
|
|
entry = "nic{}".format(adapter_number + 1)
|
2014-10-31 00:53:17 +00:00
|
|
|
if entry in vm_info:
|
|
|
|
value = vm_info[entry]
|
2015-03-11 18:05:22 +00:00
|
|
|
nics.append(value.lower())
|
2014-10-31 00:53:17 +00:00
|
|
|
else:
|
|
|
|
nics.append(None)
|
|
|
|
return nics
|
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
@asyncio.coroutine
|
2014-10-31 23:41:12 +00:00
|
|
|
def _set_network_options(self):
|
2014-10-31 00:53:17 +00:00
|
|
|
"""
|
|
|
|
Configures network options.
|
|
|
|
"""
|
|
|
|
|
2015-02-07 00:31:13 +00:00
|
|
|
nic_attachments = yield from self._get_nic_attachements(self._maximum_adapters)
|
2015-04-03 10:08:18 +00:00
|
|
|
for adapter_number in range(0, self._adapters):
|
2015-03-06 02:11:33 +00:00
|
|
|
attachment = nic_attachments[adapter_number]
|
|
|
|
if attachment == "null":
|
|
|
|
# disconnect the cable if no backend is attached.
|
2015-04-04 01:08:29 +00:00
|
|
|
yield from self._modify_vm("--cableconnected{} off".format(adapter_number + 1))
|
|
|
|
if attachment == "none":
|
|
|
|
# set the backend to null to avoid a difference in the number of interfaces in the Guest.
|
|
|
|
yield from self._modify_vm("--nic{} null".format(adapter_number + 1))
|
|
|
|
yield from self._modify_vm("--cableconnected{} off".format(adapter_number + 1))
|
2015-02-13 22:41:56 +00:00
|
|
|
nio = self._ethernet_adapters[adapter_number].get_nio(0)
|
2015-02-07 00:31:13 +00:00
|
|
|
if nio:
|
2015-05-06 21:21:39 +00:00
|
|
|
if not isinstance(nio, NIONAT) and not self._use_any_adapter and attachment not in ("none", "null", "generic"):
|
2015-02-07 00:31:13 +00:00
|
|
|
raise VirtualBoxError("Attachment ({}) already configured on adapter {}. "
|
|
|
|
"Please set it to 'Not attached' to allow GNS3 to use it.".format(attachment,
|
2015-02-13 22:41:56 +00:00
|
|
|
adapter_number + 1))
|
2014-10-31 00:53:17 +00:00
|
|
|
|
2015-05-06 21:21:39 +00:00
|
|
|
yield from self._modify_vm("--nictrace{} off".format(adapter_number + 1))
|
2014-10-31 00:53:17 +00:00
|
|
|
vbox_adapter_type = "82540EM"
|
2015-02-07 00:31:13 +00:00
|
|
|
if self._adapter_type == "PCnet-PCI II (Am79C970A)":
|
|
|
|
vbox_adapter_type = "Am79C970A"
|
|
|
|
if self._adapter_type == "PCNet-FAST III (Am79C973)":
|
|
|
|
vbox_adapter_type = "Am79C973"
|
|
|
|
if self._adapter_type == "Intel PRO/1000 MT Desktop (82540EM)":
|
|
|
|
vbox_adapter_type = "82540EM"
|
|
|
|
if self._adapter_type == "Intel PRO/1000 T Server (82543GC)":
|
|
|
|
vbox_adapter_type = "82543GC"
|
|
|
|
if self._adapter_type == "Intel PRO/1000 MT Server (82545EM)":
|
|
|
|
vbox_adapter_type = "82545EM"
|
|
|
|
if self._adapter_type == "Paravirtualized Network (virtio-net)":
|
|
|
|
vbox_adapter_type = "virtio"
|
2015-02-13 22:41:56 +00:00
|
|
|
args = [self._vmname, "--nictype{}".format(adapter_number + 1), vbox_adapter_type]
|
2015-02-07 00:31:13 +00:00
|
|
|
yield from self.manager.execute("modifyvm", args)
|
2014-10-31 00:53:17 +00:00
|
|
|
|
2015-05-06 21:21:39 +00:00
|
|
|
if isinstance(nio, NIOUDP):
|
|
|
|
log.debug("setting UDP params on adapter {}".format(adapter_number))
|
|
|
|
yield from self._modify_vm("--nic{} generic".format(adapter_number + 1))
|
|
|
|
yield from self._modify_vm("--nicgenericdrv{} UDPTunnel".format(adapter_number + 1))
|
|
|
|
yield from self._modify_vm("--nicproperty{} sport={}".format(adapter_number + 1, nio.lport))
|
|
|
|
yield from self._modify_vm("--nicproperty{} dest={}".format(adapter_number + 1, nio.rhost))
|
|
|
|
yield from self._modify_vm("--nicproperty{} dport={}".format(adapter_number + 1, nio.rport))
|
|
|
|
yield from self._modify_vm("--cableconnected{} on".format(adapter_number + 1))
|
|
|
|
elif isinstance(nio, NIONAT):
|
|
|
|
yield from self._modify_vm("--nic{} nat".format(adapter_number + 1))
|
|
|
|
yield from self._modify_vm("--cableconnected{} on".format(adapter_number + 1))
|
2014-10-31 00:53:17 +00:00
|
|
|
|
|
|
|
if nio.capturing:
|
2015-02-13 22:41:56 +00:00
|
|
|
yield from self._modify_vm("--nictrace{} on".format(adapter_number + 1))
|
2015-03-10 17:05:52 +00:00
|
|
|
yield from self._modify_vm('--nictracefile{} "{}"'.format(adapter_number + 1, nio.pcap_output_file))
|
2014-10-31 00:53:17 +00:00
|
|
|
|
2015-04-03 10:08:18 +00:00
|
|
|
for adapter_number in range(self._adapters, self._maximum_adapters):
|
2015-02-13 22:41:56 +00:00
|
|
|
log.debug("disabling remaining adapter {}".format(adapter_number))
|
|
|
|
yield from self._modify_vm("--nic{} none".format(adapter_number + 1))
|
2014-10-31 00:53:17 +00:00
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
@asyncio.coroutine
|
2014-11-09 18:50:47 +00:00
|
|
|
def _create_linked_clone(self):
|
2014-11-15 23:05:55 +00:00
|
|
|
"""
|
|
|
|
Creates a new linked clone.
|
|
|
|
"""
|
2014-11-09 18:50:47 +00:00
|
|
|
|
|
|
|
gns3_snapshot_exists = False
|
2015-01-22 02:26:39 +00:00
|
|
|
vm_info = yield from self._get_vm_info()
|
2014-11-09 18:50:47 +00:00
|
|
|
for entry, value in vm_info.items():
|
|
|
|
if entry.startswith("SnapshotName") and value == "GNS3 Linked Base for clones":
|
|
|
|
gns3_snapshot_exists = True
|
|
|
|
|
|
|
|
if not gns3_snapshot_exists:
|
2015-01-24 00:57:54 +00:00
|
|
|
result = yield from self.manager.execute("snapshot", [self._vmname, "take", "GNS3 Linked Base for clones"])
|
2014-11-15 23:05:55 +00:00
|
|
|
log.debug("GNS3 snapshot created: {}".format(result))
|
2014-11-09 18:50:47 +00:00
|
|
|
|
|
|
|
args = [self._vmname,
|
|
|
|
"--snapshot",
|
|
|
|
"GNS3 Linked Base for clones",
|
|
|
|
"--options",
|
|
|
|
"link",
|
|
|
|
"--name",
|
2015-01-23 02:07:09 +00:00
|
|
|
self.name,
|
2014-11-09 18:50:47 +00:00
|
|
|
"--basefolder",
|
2015-01-23 02:07:09 +00:00
|
|
|
self.working_dir,
|
2014-11-09 18:50:47 +00:00
|
|
|
"--register"]
|
|
|
|
|
2015-01-24 00:57:54 +00:00
|
|
|
result = yield from self.manager.execute("clonevm", args)
|
2015-02-03 01:56:13 +00:00
|
|
|
log.debug("VirtualBox VM: {} cloned".format(result))
|
2015-01-11 12:22:59 +00:00
|
|
|
|
2014-11-09 18:50:47 +00:00
|
|
|
self._vmname = self._name
|
2015-01-24 00:57:54 +00:00
|
|
|
yield from self.manager.execute("setextradata", [self._vmname, "GNS3/Clone", "yes"])
|
2015-01-12 01:24:13 +00:00
|
|
|
|
2015-02-03 00:00:29 +00:00
|
|
|
args = [self._vmname, "take", "reset"]
|
2015-01-24 00:57:54 +00:00
|
|
|
result = yield from self.manager.execute("snapshot", args)
|
2015-02-03 01:56:13 +00:00
|
|
|
log.debug("Snapshot 'reset' created: {}".format(result))
|
2014-11-09 18:50:47 +00:00
|
|
|
|
2015-01-04 21:56:17 +00:00
|
|
|
def _start_remote_console(self):
|
|
|
|
"""
|
|
|
|
Starts remote console support for this VM.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# starts the Telnet to pipe thread
|
|
|
|
pipe_name = self._get_pipe_name()
|
2015-01-22 02:26:39 +00:00
|
|
|
if sys.platform.startswith("win"):
|
2015-01-04 21:56:17 +00:00
|
|
|
try:
|
|
|
|
self._serial_pipe = open(pipe_name, "a+b")
|
|
|
|
except OSError as e:
|
|
|
|
raise VirtualBoxError("Could not open the pipe {}: {}".format(pipe_name, e))
|
2015-03-06 04:20:02 +00:00
|
|
|
try:
|
|
|
|
self._telnet_server_thread = TelnetServer(self._vmname, msvcrt.get_osfhandle(self._serial_pipe.fileno()), self._manager.port_manager.console_host, self._console)
|
|
|
|
except OSError as e:
|
|
|
|
raise VirtualBoxError("Unable to create Telnet server: {}".format(e))
|
2015-01-04 21:56:17 +00:00
|
|
|
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 VirtualBoxError("Could not connect to the pipe {}: {}".format(pipe_name, e))
|
2015-03-06 04:20:02 +00:00
|
|
|
try:
|
|
|
|
self._telnet_server_thread = TelnetServer(self._vmname, self._serial_pipe, self._manager.port_manager.console_host, self._console)
|
|
|
|
except OSError as e:
|
|
|
|
raise VirtualBoxError("Unable to create Telnet server: {}".format(e))
|
2015-01-04 21:56:17 +00:00
|
|
|
self._telnet_server_thread.start()
|
|
|
|
|
|
|
|
def _stop_remote_console(self):
|
|
|
|
"""
|
|
|
|
Stops remote console support for this VM.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self._telnet_server_thread:
|
2015-03-06 04:20:02 +00:00
|
|
|
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():
|
2015-01-22 02:26:39 +00:00
|
|
|
log.warn("Serial pipe thread is still alive!")
|
2015-01-04 21:56:17 +00:00
|
|
|
self._telnet_server_thread = None
|
|
|
|
|
|
|
|
if self._serial_pipe:
|
2015-03-06 04:20:02 +00:00
|
|
|
if sys.platform.startswith("win"):
|
2015-01-04 21:56:17 +00:00
|
|
|
win32file.CloseHandle(msvcrt.get_osfhandle(self._serial_pipe.fileno()))
|
|
|
|
else:
|
|
|
|
self._serial_pipe.close()
|
|
|
|
self._serial_pipe = None
|
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
@asyncio.coroutine
|
2015-02-13 22:41:56 +00:00
|
|
|
def adapter_add_nio_binding(self, adapter_number, nio):
|
2014-07-17 21:28:02 +00:00
|
|
|
"""
|
2015-02-07 00:31:13 +00:00
|
|
|
Adds an adapter NIO binding.
|
2014-07-17 21:28:02 +00:00
|
|
|
|
2015-02-13 22:41:56 +00:00
|
|
|
:param adapter_number: adapter number
|
2014-07-17 21:28:02 +00:00
|
|
|
:param nio: NIO instance to add to the slot/port
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
2015-02-13 22:41:56 +00:00
|
|
|
adapter = self._ethernet_adapters[adapter_number]
|
2015-05-27 14:38:57 +00:00
|
|
|
except KeyError:
|
2015-02-13 22:41:56 +00:00
|
|
|
raise VirtualBoxError("Adapter {adapter_number} doesn't exist on VirtualBox VM '{name}'".format(name=self.name,
|
|
|
|
adapter_number=adapter_number))
|
2014-07-17 21:28:02 +00:00
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
vm_state = yield from self._get_vm_state()
|
2014-10-31 00:53:17 +00:00
|
|
|
if vm_state == "running":
|
2015-06-02 14:27:48 +00:00
|
|
|
if isinstance(nio, NIOUDP):
|
|
|
|
# dynamically configure an UDP tunnel on the VirtualBox adapter
|
|
|
|
yield from self._control_vm("nic{} generic UDPTunnel".format(adapter_number + 1))
|
|
|
|
yield from self._control_vm("nicproperty{} sport={}".format(adapter_number + 1, nio.lport))
|
|
|
|
yield from self._control_vm("nicproperty{} dest={}".format(adapter_number + 1, nio.rhost))
|
|
|
|
yield from self._control_vm("nicproperty{} dport={}".format(adapter_number + 1, nio.rport))
|
|
|
|
yield from self._control_vm("setlinkstate{} on".format(adapter_number + 1))
|
2016-02-11 02:08:34 +00:00
|
|
|
|
|
|
|
# check if the UDP tunnel has been correctly set
|
|
|
|
vm_info = yield from self._get_vm_info()
|
|
|
|
generic_driver_number = "generic{}".format(adapter_number + 1)
|
2016-02-11 08:02:38 +00:00
|
|
|
if generic_driver_number not in vm_info and vm_info[generic_driver_number] != "UDPTunnel":
|
|
|
|
log.warning("UDP tunnel has not been set on nic: {}".format(adapter_number + 1))
|
|
|
|
self.project.emit("log.warning", {"message": "UDP tunnel has not been set on nic: {}".format(adapter_number + 1)})
|
2016-02-11 02:08:34 +00:00
|
|
|
|
2015-06-02 14:27:48 +00:00
|
|
|
elif isinstance(nio, NIONAT):
|
2015-06-02 15:00:37 +00:00
|
|
|
yield from self._control_vm("nic{} nat".format(adapter_number + 1))
|
|
|
|
yield from self._control_vm("setlinkstate{} on".format(adapter_number + 1))
|
2014-07-17 21:28:02 +00:00
|
|
|
|
|
|
|
adapter.add_nio(0, nio)
|
2015-02-13 22:41:56 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}]: {nio} added to adapter {adapter_number}".format(name=self.name,
|
|
|
|
id=self.id,
|
|
|
|
nio=nio,
|
|
|
|
adapter_number=adapter_number))
|
2014-07-17 21:28:02 +00:00
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
@asyncio.coroutine
|
2015-02-13 22:41:56 +00:00
|
|
|
def adapter_remove_nio_binding(self, adapter_number):
|
2014-07-17 21:28:02 +00:00
|
|
|
"""
|
2015-02-07 00:31:13 +00:00
|
|
|
Removes an adapter NIO binding.
|
2014-07-17 21:28:02 +00:00
|
|
|
|
2015-02-13 22:41:56 +00:00
|
|
|
:param adapter_number: adapter number
|
2014-07-17 21:28:02 +00:00
|
|
|
|
|
|
|
:returns: NIO instance
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
2015-02-13 22:41:56 +00:00
|
|
|
adapter = self._ethernet_adapters[adapter_number]
|
2015-05-27 14:38:57 +00:00
|
|
|
except KeyError:
|
2015-02-13 22:41:56 +00:00
|
|
|
raise VirtualBoxError("Adapter {adapter_number} doesn't exist on VirtualBox VM '{name}'".format(name=self.name,
|
|
|
|
adapter_number=adapter_number))
|
2014-07-17 21:28:02 +00:00
|
|
|
|
2015-01-22 02:26:39 +00:00
|
|
|
vm_state = yield from self._get_vm_state()
|
2014-10-31 00:53:17 +00:00
|
|
|
if vm_state == "running":
|
|
|
|
# dynamically disable the VirtualBox adapter
|
2015-02-13 22:41:56 +00:00
|
|
|
yield from self._control_vm("setlinkstate{} off".format(adapter_number + 1))
|
|
|
|
yield from self._control_vm("nic{} null".format(adapter_number + 1))
|
2014-07-17 21:28:02 +00:00
|
|
|
|
|
|
|
nio = adapter.get_nio(0)
|
2015-02-24 02:00:34 +00:00
|
|
|
if isinstance(nio, NIOUDP):
|
2015-03-21 23:19:12 +00:00
|
|
|
self.manager.port_manager.release_udp_port(nio.lport, self._project)
|
2014-07-17 21:28:02 +00:00
|
|
|
adapter.remove_nio(0)
|
2015-01-24 22:32:58 +00:00
|
|
|
|
2015-02-13 22:41:56 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}]: {nio} removed from adapter {adapter_number}".format(name=self.name,
|
|
|
|
id=self.id,
|
|
|
|
nio=nio,
|
|
|
|
adapter_number=adapter_number))
|
2014-07-17 21:28:02 +00:00
|
|
|
return nio
|
|
|
|
|
2015-05-27 19:56:27 +00:00
|
|
|
@asyncio.coroutine
|
2015-02-13 22:41:56 +00:00
|
|
|
def start_capture(self, adapter_number, output_file):
|
2014-07-17 21:28:02 +00:00
|
|
|
"""
|
|
|
|
Starts a packet capture.
|
|
|
|
|
2015-02-13 22:41:56 +00:00
|
|
|
:param adapter_number: adapter number
|
2014-07-17 21:28:02 +00:00
|
|
|
:param output_file: PCAP destination file for the capture
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
2015-02-13 22:41:56 +00:00
|
|
|
adapter = self._ethernet_adapters[adapter_number]
|
2015-05-27 14:38:57 +00:00
|
|
|
except KeyError:
|
2015-02-13 22:41:56 +00:00
|
|
|
raise VirtualBoxError("Adapter {adapter_number} doesn't exist on VirtualBox VM '{name}'".format(name=self.name,
|
|
|
|
adapter_number=adapter_number))
|
2014-07-17 21:28:02 +00:00
|
|
|
|
2015-05-27 19:56:27 +00:00
|
|
|
vm_state = yield from self._get_vm_state()
|
|
|
|
if vm_state == "running" or vm_state == "paused" or vm_state == "stuck":
|
|
|
|
raise VirtualBoxError("Sorry, packet capturing on a started VirtualBox VM is not supported.")
|
|
|
|
|
2014-07-17 21:28:02 +00:00
|
|
|
nio = adapter.get_nio(0)
|
2015-06-01 21:42:17 +00:00
|
|
|
|
|
|
|
if not nio:
|
|
|
|
raise VirtualBoxError("Adapter {} is not connected".format(adapter_number))
|
|
|
|
|
2014-07-17 21:28:02 +00:00
|
|
|
if nio.capturing:
|
2015-02-13 22:41:56 +00:00
|
|
|
raise VirtualBoxError("Packet capture is already activated on adapter {adapter_number}".format(adapter_number=adapter_number))
|
2014-07-17 21:28:02 +00:00
|
|
|
|
|
|
|
nio.startPacketCapture(output_file)
|
2015-02-13 22:41:56 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}]: starting packet capture on adapter {adapter_number}".format(name=self.name,
|
|
|
|
id=self.id,
|
|
|
|
adapter_number=adapter_number))
|
2014-07-17 21:28:02 +00:00
|
|
|
|
2015-02-13 22:41:56 +00:00
|
|
|
def stop_capture(self, adapter_number):
|
2014-07-17 21:28:02 +00:00
|
|
|
"""
|
|
|
|
Stops a packet capture.
|
|
|
|
|
2015-02-13 22:41:56 +00:00
|
|
|
:param adapter_number: adapter number
|
2014-07-17 21:28:02 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
2015-02-13 22:41:56 +00:00
|
|
|
adapter = self._ethernet_adapters[adapter_number]
|
2015-05-27 14:38:57 +00:00
|
|
|
except KeyError:
|
2015-02-13 22:41:56 +00:00
|
|
|
raise VirtualBoxError("Adapter {adapter_number} doesn't exist on VirtualBox VM '{name}'".format(name=self.name,
|
|
|
|
adapter_number=adapter_number))
|
2014-07-17 21:28:02 +00:00
|
|
|
|
|
|
|
nio = adapter.get_nio(0)
|
2015-06-01 21:42:17 +00:00
|
|
|
|
|
|
|
if not nio:
|
|
|
|
raise VirtualBoxError("Adapter {} is not connected".format(adapter_number))
|
|
|
|
|
2014-07-17 21:28:02 +00:00
|
|
|
nio.stopPacketCapture()
|
|
|
|
|
2015-02-13 22:41:56 +00:00
|
|
|
log.info("VirtualBox VM '{name}' [{id}]: stopping packet capture on adapter {adapter_number}".format(name=self.name,
|
|
|
|
id=self.id,
|
|
|
|
adapter_number=adapter_number))
|