mirror of
https://github.com/GNS3/gns3-server
synced 2024-12-26 08:48:11 +00:00
VirtualBox VM almost done.
This commit is contained in:
parent
f231b06833
commit
87bd0d1869
@ -27,10 +27,10 @@ import subprocess
|
|||||||
import tempfile
|
import tempfile
|
||||||
import json
|
import json
|
||||||
import socket
|
import socket
|
||||||
import time
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
|
from pkg_resources import parse_version
|
||||||
from .virtualbox_error import VirtualBoxError
|
from .virtualbox_error import VirtualBoxError
|
||||||
from ..adapters.ethernet_adapter import EthernetAdapter
|
from ..adapters.ethernet_adapter import EthernetAdapter
|
||||||
from .telnet_server import TelnetServer
|
from .telnet_server import TelnetServer
|
||||||
@ -49,66 +49,42 @@ class VirtualBoxVM(BaseVM):
|
|||||||
VirtualBox VM implementation.
|
VirtualBox VM implementation.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, name, uuid, project, manager, vmname, linked_clone, console=None):
|
def __init__(self, name, uuid, project, manager, vmname, linked_clone, console=None, vbox_user=None):
|
||||||
|
|
||||||
super().__init__(name, uuid, project, manager)
|
super().__init__(name, uuid, project, manager)
|
||||||
|
|
||||||
# look for VBoxManage
|
self._vboxmanage_path = None
|
||||||
self._vboxmanage_path = manager.config.get_section_config("VirtualBox").get("vboxmanage_path")
|
self._maximum_adapters = 8
|
||||||
if not self._vboxmanage_path:
|
|
||||||
if sys.platform.startswith("win"):
|
|
||||||
if "VBOX_INSTALL_PATH" in os.environ:
|
|
||||||
self._vboxmanage_path = os.path.join(os.environ["VBOX_INSTALL_PATH"], "VBoxManage.exe")
|
|
||||||
elif "VBOX_MSI_INSTALL_PATH" in os.environ:
|
|
||||||
self._vboxmanage_path = os.path.join(os.environ["VBOX_MSI_INSTALL_PATH"], "VBoxManage.exe")
|
|
||||||
elif sys.platform.startswith("darwin"):
|
|
||||||
self._vboxmanage_path = "/Applications/VirtualBox.app/Contents/MacOS/VBoxManage"
|
|
||||||
else:
|
|
||||||
self._vboxmanage_path = shutil.which("vboxmanage")
|
|
||||||
|
|
||||||
if not self._vboxmanage_path:
|
|
||||||
raise VirtualBoxError("Could not find VBoxManage")
|
|
||||||
if not os.access(self._vboxmanage_path, os.X_OK):
|
|
||||||
raise VirtualBoxError("VBoxManage is not executable")
|
|
||||||
|
|
||||||
self._started = False
|
|
||||||
self._linked_clone = linked_clone
|
self._linked_clone = linked_clone
|
||||||
|
self._vbox_user = vbox_user
|
||||||
self._system_properties = {}
|
self._system_properties = {}
|
||||||
self._queue = asyncio.Queue()
|
self._telnet_server_thread = None
|
||||||
self._created = asyncio.Future()
|
self._serial_pipe = None
|
||||||
self._running = True
|
|
||||||
self._worker = asyncio.async(self._run())
|
|
||||||
|
|
||||||
# VirtualBox settings
|
# VirtualBox settings
|
||||||
self._console = console
|
self._console = console
|
||||||
self._ethernet_adapters = []
|
self._ethernet_adapters = []
|
||||||
self._headless = False
|
self._headless = False
|
||||||
self._enable_remote_console = True
|
self._enable_remote_console = False
|
||||||
self._vmname = vmname
|
self._vmname = vmname
|
||||||
self._adapter_start_index = 0
|
self._adapter_start_index = 0
|
||||||
self._adapter_type = "Intel PRO/1000 MT Desktop (82540EM)"
|
self._adapter_type = "Intel PRO/1000 MT Desktop (82540EM)"
|
||||||
|
|
||||||
if linked_clone:
|
#TODO: finish adapters support
|
||||||
if uuid and os.path.isdir(os.path.join(self.working_dir, self._vmname)):
|
|
||||||
vbox_file = os.path.join(self.working_dir, self._vmname, self._vmname + ".vbox")
|
|
||||||
self._execute("registervm", [vbox_file])
|
|
||||||
self._reattach_hdds()
|
|
||||||
else:
|
|
||||||
self._create_linked_clone()
|
|
||||||
|
|
||||||
#self._maximum_adapters = 8
|
|
||||||
#self.adapters = 2 # creates 2 adapters by default
|
#self.adapters = 2 # creates 2 adapters by default
|
||||||
|
|
||||||
return
|
|
||||||
|
|
||||||
self._command = []
|
|
||||||
self._vbox_user = vbox_user
|
|
||||||
|
|
||||||
self._telnet_server_thread = None
|
|
||||||
self._serial_pipe = None
|
|
||||||
|
|
||||||
def __json__(self):
|
def __json__(self):
|
||||||
|
|
||||||
|
#TODO: send more info
|
||||||
|
# {"name": self._name,
|
||||||
|
# "vmname": self._vmname,
|
||||||
|
# "adapters": self.adapters,
|
||||||
|
# "adapter_start_index": self._adapter_start_index,
|
||||||
|
# "adapter_type": "Intel PRO/1000 MT Desktop (82540EM)",
|
||||||
|
# "console": self._console,
|
||||||
|
# "enable_remote_console": self._enable_remote_console,
|
||||||
|
# "headless": self._headless}
|
||||||
|
|
||||||
return {"name": self.name,
|
return {"name": self.name,
|
||||||
"uuid": self.uuid,
|
"uuid": self.uuid,
|
||||||
"project_uuid": self.project.uuid}
|
"project_uuid": self.project.uuid}
|
||||||
@ -119,6 +95,11 @@ class VirtualBoxVM(BaseVM):
|
|||||||
command = [self._vboxmanage_path, "--nologo", subcommand]
|
command = [self._vboxmanage_path, "--nologo", subcommand]
|
||||||
command.extend(args)
|
command.extend(args)
|
||||||
try:
|
try:
|
||||||
|
if self._vbox_user and self._vbox_user.strip():
|
||||||
|
#TODO: test & review this part
|
||||||
|
sudo_command = "sudo -i -u {}".format(self._vbox_user.strip()) + " ".join(command)
|
||||||
|
process = yield from asyncio.create_subprocess_shell(sudo_command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
|
||||||
|
else:
|
||||||
process = yield from asyncio.create_subprocess_exec(*command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
|
process = yield from asyncio.create_subprocess_exec(*command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
|
||||||
except (OSError, subprocess.SubprocessError) as e:
|
except (OSError, subprocess.SubprocessError) as e:
|
||||||
raise VirtualBoxError("Could not execute VBoxManage: {}".format(e))
|
raise VirtualBoxError("Could not execute VBoxManage: {}".format(e))
|
||||||
@ -147,96 +128,175 @@ class VirtualBoxVM(BaseVM):
|
|||||||
self._system_properties[name.strip()] = value.strip()
|
self._system_properties[name.strip()] = value.strip()
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def _run(self):
|
def _get_vm_state(self):
|
||||||
|
"""
|
||||||
|
Returns the VM state (e.g. running, paused etc.)
|
||||||
|
|
||||||
try:
|
:returns: state (string)
|
||||||
yield from self._get_system_properties()
|
"""
|
||||||
#TODO: check for API version
|
|
||||||
self._created.set_result(True)
|
|
||||||
except VirtualBoxError as e:
|
|
||||||
self._created.set_exception(e)
|
|
||||||
return
|
|
||||||
|
|
||||||
while self._running:
|
results = yield from self._execute("showvminfo", [self._vmname, "--machinereadable"])
|
||||||
future, subcommand, args = yield from self._queue.get()
|
for info in results:
|
||||||
try:
|
name, value = info.split('=', 1)
|
||||||
yield from self._execute(subcommand, args)
|
if name == "VMState":
|
||||||
future.set_result(True)
|
return value.strip('"')
|
||||||
except VirtualBoxError as e:
|
raise VirtualBoxError("Could not get VM state for {}".format(self._vmname))
|
||||||
future.set_exception(e)
|
|
||||||
|
|
||||||
|
@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)
|
||||||
|
result = yield from self._execute("controlvm", [self._vmname] + args)
|
||||||
|
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
|
||||||
|
"""
|
||||||
|
|
||||||
|
args = shlex.split(params)
|
||||||
|
yield from self._execute("modifyvm", [self._vmname] + args)
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
def create(self):
|
def create(self):
|
||||||
|
|
||||||
return self._created
|
# look for VBoxManage
|
||||||
|
self._vboxmanage_path = self.manager.config.get_section_config("VirtualBox").get("vboxmanage_path")
|
||||||
|
if not self._vboxmanage_path:
|
||||||
|
if sys.platform.startswith("win"):
|
||||||
|
if "VBOX_INSTALL_PATH" in os.environ:
|
||||||
|
self._vboxmanage_path = os.path.join(os.environ["VBOX_INSTALL_PATH"], "VBoxManage.exe")
|
||||||
|
elif "VBOX_MSI_INSTALL_PATH" in os.environ:
|
||||||
|
self._vboxmanage_path = os.path.join(os.environ["VBOX_MSI_INSTALL_PATH"], "VBoxManage.exe")
|
||||||
|
elif sys.platform.startswith("darwin"):
|
||||||
|
self._vboxmanage_path = "/Applications/VirtualBox.app/Contents/MacOS/VBoxManage"
|
||||||
|
else:
|
||||||
|
self._vboxmanage_path = shutil.which("vboxmanage")
|
||||||
|
|
||||||
def _put(self, item):
|
if not self._vboxmanage_path:
|
||||||
|
raise VirtualBoxError("Could not find VBoxManage")
|
||||||
|
if not os.access(self._vboxmanage_path, os.X_OK):
|
||||||
|
raise VirtualBoxError("VBoxManage is not executable")
|
||||||
|
|
||||||
try:
|
yield from self._get_system_properties()
|
||||||
self._queue.put_nowait(item)
|
if parse_version(self._system_properties["API version"]) < parse_version("4_3"):
|
||||||
except asyncio.qeues.QueueFull:
|
raise VirtualBoxError("The VirtualBox API version is lower than 4.3")
|
||||||
raise VirtualBoxError("Queue is full")
|
log.info("VirtualBox VM '{name}' [{uuid}] created".format(name=self.name, uuid=self.uuid))
|
||||||
|
|
||||||
|
if self._linked_clone:
|
||||||
|
#TODO: finish linked clone support
|
||||||
|
if self.uuid and os.path.isdir(os.path.join(self.working_dir, self._vmname)):
|
||||||
|
vbox_file = os.path.join(self.working_dir, self._vmname, self._vmname + ".vbox")
|
||||||
|
self._execute("registervm", [vbox_file])
|
||||||
|
self._reattach_hdds()
|
||||||
|
else:
|
||||||
|
self._create_linked_clone()
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
def start(self):
|
def start(self):
|
||||||
|
"""
|
||||||
|
Starts this VirtualBox VM.
|
||||||
|
"""
|
||||||
|
|
||||||
args = [self._name]
|
# resume the VM if it is paused
|
||||||
future = asyncio.Future()
|
vm_state = yield from self._get_vm_state()
|
||||||
self._put((future, "startvm", args))
|
if vm_state == "paused":
|
||||||
return future
|
yield from self.resume()
|
||||||
|
return
|
||||||
|
|
||||||
|
# VM must be powered off and in saved state to start it
|
||||||
|
if vm_state != "poweroff" and vm_state != "saved":
|
||||||
|
raise VirtualBoxError("VirtualBox VM not powered off or saved")
|
||||||
|
|
||||||
|
yield from self._set_network_options()
|
||||||
|
yield from self._set_serial_console()
|
||||||
|
|
||||||
|
args = [self._vmname]
|
||||||
|
if self._headless:
|
||||||
|
args.extend(["--type", "headless"])
|
||||||
|
result = yield from self._execute("startvm", args)
|
||||||
|
log.info("VirtualBox VM '{name}' [{uuid}] started".format(name=self.name, uuid=self.uuid))
|
||||||
|
log.debug("Start result: {}".format(result))
|
||||||
|
|
||||||
|
# add a guest property to let the VM know about the GNS3 name
|
||||||
|
yield from self._execute("guestproperty", ["set", self._vmname, "NameInGNS3", self.name])
|
||||||
|
# add a guest property to let the VM know about the GNS3 project directory
|
||||||
|
yield from self._execute("guestproperty", ["set", self._vmname, "ProjectDirInGNS3", self.working_dir])
|
||||||
|
|
||||||
|
if self._enable_remote_console:
|
||||||
|
self._start_remote_console()
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
def stop(self):
|
def stop(self):
|
||||||
|
|
||||||
args = [self._name, "poweroff"]
|
|
||||||
future = asyncio.Future()
|
|
||||||
self._put((future, "controlvm", args))
|
|
||||||
return future
|
|
||||||
|
|
||||||
def defaults(self):
|
|
||||||
"""
|
"""
|
||||||
Returns all the default attribute values for this VirtualBox VM.
|
Stops this VirtualBox VM.
|
||||||
|
|
||||||
:returns: default values (dictionary)
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
vbox_defaults = {"name": self._name,
|
self._stop_remote_console()
|
||||||
"vmname": self._vmname,
|
vm_state = yield from self._get_vm_state()
|
||||||
"adapters": self.adapters,
|
if vm_state == "running" or vm_state == "paused" or vm_state == "stuck":
|
||||||
"adapter_start_index": self._adapter_start_index,
|
# power off the VM
|
||||||
"adapter_type": "Intel PRO/1000 MT Desktop (82540EM)",
|
result = yield from self._control_vm("poweroff")
|
||||||
"console": self._console,
|
log.info("VirtualBox VM '{name}' [{uuid}] stopped".format(name=self.name, uuid=self.uuid))
|
||||||
"enable_remote_console": self._enable_remote_console,
|
log.debug("Stop result: {}".format(result))
|
||||||
"headless": self._headless}
|
|
||||||
|
|
||||||
return vbox_defaults
|
|
||||||
|
|
||||||
@property
|
|
||||||
def working_dir(self):
|
|
||||||
"""
|
|
||||||
Returns current working directory
|
|
||||||
|
|
||||||
:returns: path to the working directory
|
|
||||||
"""
|
|
||||||
|
|
||||||
return self._working_dir
|
|
||||||
|
|
||||||
@working_dir.setter
|
|
||||||
def working_dir(self, working_dir):
|
|
||||||
"""
|
|
||||||
Sets the working directory this VirtualBox VM.
|
|
||||||
|
|
||||||
:param working_dir: path to the working directory
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
yield from asyncio.sleep(0.5) # give some time for VirtualBox to unlock the VM
|
||||||
try:
|
try:
|
||||||
os.makedirs(working_dir)
|
# deactivate the first serial port
|
||||||
except FileExistsError:
|
yield from self._modify_vm("--uart1 off")
|
||||||
pass
|
except VirtualBoxError as e:
|
||||||
except OSError as e:
|
log.warn("Could not deactivate the first serial port: {}".format(e))
|
||||||
raise VirtualBoxError("Could not create working directory {}: {}".format(working_dir, e))
|
|
||||||
|
|
||||||
self._working_dir = working_dir
|
for adapter_id in range(0, len(self._ethernet_adapters)):
|
||||||
log.info("VirtualBox VM {name} [id={id}]: working directory changed to {wd}".format(name=self._name,
|
if self._ethernet_adapters[adapter_id] is None:
|
||||||
id=self._id,
|
continue
|
||||||
wd=self._working_dir))
|
yield from self._modify_vm("--nictrace{} off".format(adapter_id + 1))
|
||||||
|
yield from self._modify_vm("--cableconnected{} off".format(adapter_id + 1))
|
||||||
|
yield from self._modify_vm("--nic{} null".format(adapter_id + 1))
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def suspend(self):
|
||||||
|
"""
|
||||||
|
Suspends this VirtualBox VM.
|
||||||
|
"""
|
||||||
|
|
||||||
|
vm_state = yield from self._get_vm_state()
|
||||||
|
if vm_state == "running":
|
||||||
|
yield from self._control_vm("pause")
|
||||||
|
log.info("VirtualBox VM '{name}' [{uuid}] suspended".format(name=self.name, uuid=self.uuid))
|
||||||
|
else:
|
||||||
|
log.warn("VirtualBox VM '{name}' [{uuid}] cannot be suspended, current state: {state}".format(name=self.name,
|
||||||
|
uuid=self.uuid,
|
||||||
|
state=vm_state))
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def resume(self):
|
||||||
|
"""
|
||||||
|
Resumes this VirtualBox VM.
|
||||||
|
"""
|
||||||
|
|
||||||
|
yield from self._control_vm("resume")
|
||||||
|
log.info("VirtualBox VM '{name}' [{uuid}] resumed".format(name=self.name, uuid=self.uuid))
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def reload(self):
|
||||||
|
"""
|
||||||
|
Reloads this VirtualBox VM.
|
||||||
|
"""
|
||||||
|
|
||||||
|
result = yield from self._control_vm("reset")
|
||||||
|
log.info("VirtualBox VM '{name}' [{uuid}] reloaded".format(name=self.name, uuid=self.uuid))
|
||||||
|
log.debug("Reload result: {}".format(result))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def console(self):
|
def console(self):
|
||||||
@ -370,8 +430,8 @@ class VirtualBoxVM(BaseVM):
|
|||||||
# error=e))
|
# error=e))
|
||||||
# return
|
# return
|
||||||
|
|
||||||
log.info("VirtualBox VM {name} [id={id}] has been deleted (including associated files)".format(name=self._name,
|
log.info("VirtualBox VM '{name}' [{uuid}] has been deleted (including associated files)".format(name=self.name,
|
||||||
id=self._id))
|
uuid=self.uuid))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def headless(self):
|
def headless(self):
|
||||||
@ -392,9 +452,9 @@ class VirtualBoxVM(BaseVM):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
if headless:
|
if headless:
|
||||||
log.info("VirtualBox VM {name} [id={id}] has enabled the headless mode".format(name=self._name, id=self._id))
|
log.info("VirtualBox VM '{name}' [{uuid}] has enabled the headless mode".format(name=self.name, uuid=self.uuid))
|
||||||
else:
|
else:
|
||||||
log.info("VirtualBox VM {name} [id={id}] has disabled the headless mode".format(name=self._name, id=self._id))
|
log.info("VirtualBox VM '{name}' [{uuid}] has disabled the headless mode".format(name=self.name, uuid=self.uuid))
|
||||||
self._headless = headless
|
self._headless = headless
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -416,10 +476,10 @@ class VirtualBoxVM(BaseVM):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
if enable_remote_console:
|
if enable_remote_console:
|
||||||
log.info("VirtualBox VM {name} [id={id}] has enabled the console".format(name=self._name, id=self._id))
|
log.info("VirtualBox VM '{name}' [{uuid}] has enabled the console".format(name=self.name, uuid=self.uuid))
|
||||||
self._start_remote_console()
|
self._start_remote_console()
|
||||||
else:
|
else:
|
||||||
log.info("VirtualBox VM {name} [id={id}] has disabled the console".format(name=self._name, id=self._id))
|
log.info("VirtualBox VM '{name}' [{uuid}] has disabled the console".format(name=self.name, uuid=self.uuid))
|
||||||
self._stop_remote_console()
|
self._stop_remote_console()
|
||||||
self._enable_remote_console = enable_remote_console
|
self._enable_remote_console = enable_remote_console
|
||||||
|
|
||||||
@ -441,7 +501,7 @@ class VirtualBoxVM(BaseVM):
|
|||||||
:param vmname: VirtualBox VM name
|
:param vmname: VirtualBox VM name
|
||||||
"""
|
"""
|
||||||
|
|
||||||
log.info("VirtualBox VM {name} [id={id}] has set the VM name to {vmname}".format(name=self._name, id=self._id, vmname=vmname))
|
log.info("VirtualBox VM '{name}' [{uuid}] has set the VM name to '{vmname}'".format(name=self.name, uuid=self.uuid, vmname=vmname))
|
||||||
if self._linked_clone:
|
if self._linked_clone:
|
||||||
self._modify_vm('--name "{}"'.format(vmname))
|
self._modify_vm('--name "{}"'.format(vmname))
|
||||||
self._vmname = vmname
|
self._vmname = vmname
|
||||||
@ -476,8 +536,8 @@ class VirtualBoxVM(BaseVM):
|
|||||||
continue
|
continue
|
||||||
self._ethernet_adapters.append(EthernetAdapter())
|
self._ethernet_adapters.append(EthernetAdapter())
|
||||||
|
|
||||||
log.info("VirtualBox VM {name} [id={id}]: number of Ethernet adapters changed to {adapters}".format(name=self._name,
|
log.info("VirtualBox VM '{name}' [{uuid}]: number of Ethernet adapters changed to {adapters}".format(name=self.name,
|
||||||
id=self._id,
|
uuid=self.uuid,
|
||||||
adapters=adapters))
|
adapters=adapters))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -500,8 +560,8 @@ class VirtualBoxVM(BaseVM):
|
|||||||
|
|
||||||
self._adapter_start_index = adapter_start_index
|
self._adapter_start_index = adapter_start_index
|
||||||
self.adapters = self.adapters # this forces to recreate the adapter list with the correct index
|
self.adapters = self.adapters # this forces to recreate the adapter list with the correct index
|
||||||
log.info("VirtualBox VM {name} [id={id}]: adapter start index changed to {index}".format(name=self._name,
|
log.info("VirtualBox VM '{name}' [{uuid}]: adapter start index changed to {index}".format(name=self.name,
|
||||||
id=self._id,
|
uuid=self.uuid,
|
||||||
index=adapter_start_index))
|
index=adapter_start_index))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -524,42 +584,11 @@ class VirtualBoxVM(BaseVM):
|
|||||||
|
|
||||||
self._adapter_type = adapter_type
|
self._adapter_type = adapter_type
|
||||||
|
|
||||||
log.info("VirtualBox VM {name} [id={id}]: adapter type changed to {adapter_type}".format(name=self._name,
|
log.info("VirtualBox VM '{name}' [{uuid}]: adapter type changed to {adapter_type}".format(name=self.name,
|
||||||
id=self._id,
|
uuid=self.uuid,
|
||||||
adapter_type=adapter_type))
|
adapter_type=adapter_type))
|
||||||
|
|
||||||
def _old_execute(self, subcommand, args, timeout=60):
|
@asyncio.coroutine
|
||||||
"""
|
|
||||||
Executes a command with VBoxManage.
|
|
||||||
|
|
||||||
:param subcommand: vboxmanage subcommand (e.g. modifyvm, controlvm etc.)
|
|
||||||
:param args: arguments for the subcommand.
|
|
||||||
:param timeout: how long to wait for vboxmanage
|
|
||||||
|
|
||||||
:returns: result (list)
|
|
||||||
"""
|
|
||||||
|
|
||||||
command = [self._vboxmanage_path, "--nologo", subcommand]
|
|
||||||
command.extend(args)
|
|
||||||
log.debug("Execute vboxmanage command: {}".format(command))
|
|
||||||
user = self._vbox_user
|
|
||||||
try:
|
|
||||||
if not user.strip() or sys.platform.startswith("win") or sys.platform.startswith("darwin"):
|
|
||||||
result = subprocess.check_output(command, stderr=subprocess.STDOUT, timeout=timeout)
|
|
||||||
else:
|
|
||||||
sudo_command = "sudo -i -u " + user.strip() + " " + " ".join(command)
|
|
||||||
result = subprocess.check_output(sudo_command, stderr=subprocess.STDOUT, shell=True, timeout=timeout)
|
|
||||||
except subprocess.CalledProcessError as e:
|
|
||||||
if e.output:
|
|
||||||
# only the first line of the output is useful
|
|
||||||
virtualbox_error = e.output.decode("utf-8").splitlines()[0]
|
|
||||||
raise VirtualBoxError("{}".format(virtualbox_error))
|
|
||||||
else:
|
|
||||||
raise VirtualBoxError("{}".format(e))
|
|
||||||
except (OSError, subprocess.SubprocessError) as e:
|
|
||||||
raise VirtualBoxError("Could not execute VBoxManage: {}".format(e))
|
|
||||||
return result.decode("utf-8", errors="ignore").splitlines()
|
|
||||||
|
|
||||||
def _get_vm_info(self):
|
def _get_vm_info(self):
|
||||||
"""
|
"""
|
||||||
Returns this VM info.
|
Returns this VM info.
|
||||||
@ -568,7 +597,7 @@ class VirtualBoxVM(BaseVM):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
vm_info = {}
|
vm_info = {}
|
||||||
results = self._execute("showvminfo", [self._vmname, "--machinereadable"])
|
results = yield from self._execute("showvminfo", [self._vmname, "--machinereadable"])
|
||||||
for info in results:
|
for info in results:
|
||||||
try:
|
try:
|
||||||
name, value = info.split('=', 1)
|
name, value = info.split('=', 1)
|
||||||
@ -577,20 +606,7 @@ class VirtualBoxVM(BaseVM):
|
|||||||
vm_info[name.strip('"')] = value.strip('"')
|
vm_info[name.strip('"')] = value.strip('"')
|
||||||
return vm_info
|
return vm_info
|
||||||
|
|
||||||
def _get_vm_state(self):
|
@asyncio.coroutine
|
||||||
"""
|
|
||||||
Returns this VM state (e.g. running, paused etc.)
|
|
||||||
|
|
||||||
:returns: state (string)
|
|
||||||
"""
|
|
||||||
|
|
||||||
results = self._execute("showvminfo", [self._vmname, "--machinereadable"])
|
|
||||||
for info in results:
|
|
||||||
name, value = info.split('=', 1)
|
|
||||||
if name == "VMState":
|
|
||||||
return value.strip('"')
|
|
||||||
raise VirtualBoxError("Could not get VM state for {}".format(self._vmname))
|
|
||||||
|
|
||||||
def _get_maximum_supported_adapters(self):
|
def _get_maximum_supported_adapters(self):
|
||||||
"""
|
"""
|
||||||
Returns the maximum adapters supported by this VM.
|
Returns the maximum adapters supported by this VM.
|
||||||
@ -599,7 +615,7 @@ class VirtualBoxVM(BaseVM):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# check the maximum number of adapters supported by the VM
|
# check the maximum number of adapters supported by the VM
|
||||||
vm_info = self._get_vm_info()
|
vm_info = yield from self._get_vm_info()
|
||||||
chipset = vm_info["chipset"]
|
chipset = vm_info["chipset"]
|
||||||
maximum_adapters = 8
|
maximum_adapters = 8
|
||||||
if chipset == "ich9":
|
if chipset == "ich9":
|
||||||
@ -615,46 +631,25 @@ class VirtualBoxVM(BaseVM):
|
|||||||
|
|
||||||
p = re.compile('\s+', re.UNICODE)
|
p = re.compile('\s+', re.UNICODE)
|
||||||
pipe_name = p.sub("_", self._vmname)
|
pipe_name = p.sub("_", self._vmname)
|
||||||
if sys.platform.startswith('win'):
|
if sys.platform.startswith("win"):
|
||||||
pipe_name = r"\\.\pipe\VBOX\{}".format(pipe_name)
|
pipe_name = r"\\.\pipe\VBOX\{}".format(pipe_name)
|
||||||
else:
|
else:
|
||||||
pipe_name = os.path.join(tempfile.gettempdir(), "pipe_{}".format(pipe_name))
|
pipe_name = os.path.join(tempfile.gettempdir(), "pipe_{}".format(pipe_name))
|
||||||
return pipe_name
|
return pipe_name
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
def _set_serial_console(self):
|
def _set_serial_console(self):
|
||||||
"""
|
"""
|
||||||
Configures the first serial port to allow a serial console connection.
|
Configures the first serial port to allow a serial console connection.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# activate the first serial port
|
# activate the first serial port
|
||||||
self._modify_vm("--uart1 0x3F8 4")
|
yield from self._modify_vm("--uart1 0x3F8 4")
|
||||||
|
|
||||||
# set server mode with a pipe on the first serial port
|
# set server mode with a pipe on the first serial port
|
||||||
pipe_name = self._get_pipe_name()
|
pipe_name = self._get_pipe_name()
|
||||||
args = [self._vmname, "--uartmode1", "server", pipe_name]
|
args = [self._vmname, "--uartmode1", "server", pipe_name]
|
||||||
self._execute("modifyvm", args)
|
yield from self._execute("modifyvm", args)
|
||||||
|
|
||||||
def _modify_vm(self, params):
|
|
||||||
"""
|
|
||||||
Change setting in this VM when not running.
|
|
||||||
|
|
||||||
:param params: params to use with sub-command modifyvm
|
|
||||||
"""
|
|
||||||
|
|
||||||
args = shlex.split(params)
|
|
||||||
self._execute("modifyvm", [self._vmname] + args)
|
|
||||||
|
|
||||||
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)
|
|
||||||
return self._execute("controlvm", [self._vmname] + args)
|
|
||||||
|
|
||||||
def _storage_attach(self, params):
|
def _storage_attach(self, params):
|
||||||
"""
|
"""
|
||||||
@ -666,6 +661,7 @@ class VirtualBoxVM(BaseVM):
|
|||||||
args = shlex.split(params)
|
args = shlex.split(params)
|
||||||
self._execute("storageattach", [self._vmname] + args)
|
self._execute("storageattach", [self._vmname] + args)
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
def _get_nic_attachements(self, maximum_adapters):
|
def _get_nic_attachements(self, maximum_adapters):
|
||||||
"""
|
"""
|
||||||
Returns NIC attachements.
|
Returns NIC attachements.
|
||||||
@ -675,7 +671,7 @@ class VirtualBoxVM(BaseVM):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
nics = []
|
nics = []
|
||||||
vm_info = self._get_vm_info()
|
vm_info = yield from self._get_vm_info()
|
||||||
for adapter_id in range(0, maximum_adapters):
|
for adapter_id in range(0, maximum_adapters):
|
||||||
entry = "nic{}".format(adapter_id + 1)
|
entry = "nic{}".format(adapter_id + 1)
|
||||||
if entry in vm_info:
|
if entry in vm_info:
|
||||||
@ -685,12 +681,13 @@ class VirtualBoxVM(BaseVM):
|
|||||||
nics.append(None)
|
nics.append(None)
|
||||||
return nics
|
return nics
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
def _set_network_options(self):
|
def _set_network_options(self):
|
||||||
"""
|
"""
|
||||||
Configures network options.
|
Configures network options.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
nic_attachements = self._get_nic_attachements(self._maximum_adapters)
|
nic_attachements = yield from self._get_nic_attachements(self._maximum_adapters)
|
||||||
for adapter_id in range(0, len(self._ethernet_adapters)):
|
for adapter_id in range(0, len(self._ethernet_adapters)):
|
||||||
if self._ethernet_adapters[adapter_id] is None:
|
if self._ethernet_adapters[adapter_id] is None:
|
||||||
# force enable to avoid any discrepancy in the interface numbering inside the VM
|
# force enable to avoid any discrepancy in the interface numbering inside the VM
|
||||||
@ -698,7 +695,7 @@ class VirtualBoxVM(BaseVM):
|
|||||||
attachement = nic_attachements[adapter_id]
|
attachement = nic_attachements[adapter_id]
|
||||||
if attachement:
|
if attachement:
|
||||||
# attachement can be none, null, nat, bridged, intnet, hostonly or generic
|
# attachement can be none, null, nat, bridged, intnet, hostonly or generic
|
||||||
self._modify_vm("--nic{} {}".format(adapter_id + 1, attachement))
|
yield from self._modify_vm("--nic{} {}".format(adapter_id + 1, attachement))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
vbox_adapter_type = "82540EM"
|
vbox_adapter_type = "82540EM"
|
||||||
@ -716,44 +713,45 @@ class VirtualBoxVM(BaseVM):
|
|||||||
vbox_adapter_type = "virtio"
|
vbox_adapter_type = "virtio"
|
||||||
|
|
||||||
args = [self._vmname, "--nictype{}".format(adapter_id + 1), vbox_adapter_type]
|
args = [self._vmname, "--nictype{}".format(adapter_id + 1), vbox_adapter_type]
|
||||||
self._execute("modifyvm", args)
|
yield from self._execute("modifyvm", args)
|
||||||
|
|
||||||
self._modify_vm("--nictrace{} off".format(adapter_id + 1))
|
yield from self._modify_vm("--nictrace{} off".format(adapter_id + 1))
|
||||||
nio = self._ethernet_adapters[adapter_id].get_nio(0)
|
nio = self._ethernet_adapters[adapter_id].get_nio(0)
|
||||||
if nio:
|
if nio:
|
||||||
log.debug("setting UDP params on adapter {}".format(adapter_id))
|
log.debug("setting UDP params on adapter {}".format(adapter_id))
|
||||||
self._modify_vm("--nic{} generic".format(adapter_id + 1))
|
yield from self._modify_vm("--nic{} generic".format(adapter_id + 1))
|
||||||
self._modify_vm("--nicgenericdrv{} UDPTunnel".format(adapter_id + 1))
|
yield from self._modify_vm("--nicgenericdrv{} UDPTunnel".format(adapter_id + 1))
|
||||||
self._modify_vm("--nicproperty{} sport={}".format(adapter_id + 1, nio.lport))
|
yield from self._modify_vm("--nicproperty{} sport={}".format(adapter_id + 1, nio.lport))
|
||||||
self._modify_vm("--nicproperty{} dest={}".format(adapter_id + 1, nio.rhost))
|
yield from self._modify_vm("--nicproperty{} dest={}".format(adapter_id + 1, nio.rhost))
|
||||||
self._modify_vm("--nicproperty{} dport={}".format(adapter_id + 1, nio.rport))
|
yield from self._modify_vm("--nicproperty{} dport={}".format(adapter_id + 1, nio.rport))
|
||||||
self._modify_vm("--cableconnected{} on".format(adapter_id + 1))
|
yield from self._modify_vm("--cableconnected{} on".format(adapter_id + 1))
|
||||||
|
|
||||||
if nio.capturing:
|
if nio.capturing:
|
||||||
self._modify_vm("--nictrace{} on".format(adapter_id + 1))
|
yield from self._modify_vm("--nictrace{} on".format(adapter_id + 1))
|
||||||
self._modify_vm("--nictracefile{} {}".format(adapter_id + 1, nio.pcap_output_file))
|
yield from self._modify_vm("--nictracefile{} {}".format(adapter_id + 1, nio.pcap_output_file))
|
||||||
else:
|
else:
|
||||||
# shutting down unused adapters...
|
# shutting down unused adapters...
|
||||||
self._modify_vm("--cableconnected{} off".format(adapter_id + 1))
|
yield from self._modify_vm("--cableconnected{} off".format(adapter_id + 1))
|
||||||
self._modify_vm("--nic{} null".format(adapter_id + 1))
|
yield from self._modify_vm("--nic{} null".format(adapter_id + 1))
|
||||||
|
|
||||||
for adapter_id in range(len(self._ethernet_adapters), self._maximum_adapters):
|
for adapter_id in range(len(self._ethernet_adapters), self._maximum_adapters):
|
||||||
log.debug("disabling remaining adapter {}".format(adapter_id))
|
log.debug("disabling remaining adapter {}".format(adapter_id))
|
||||||
self._modify_vm("--nic{} none".format(adapter_id + 1))
|
yield from self._modify_vm("--nic{} none".format(adapter_id + 1))
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
def _create_linked_clone(self):
|
def _create_linked_clone(self):
|
||||||
"""
|
"""
|
||||||
Creates a new linked clone.
|
Creates a new linked clone.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
gns3_snapshot_exists = False
|
gns3_snapshot_exists = False
|
||||||
vm_info = self._get_vm_info()
|
vm_info = yield from self._get_vm_info()
|
||||||
for entry, value in vm_info.items():
|
for entry, value in vm_info.items():
|
||||||
if entry.startswith("SnapshotName") and value == "GNS3 Linked Base for clones":
|
if entry.startswith("SnapshotName") and value == "GNS3 Linked Base for clones":
|
||||||
gns3_snapshot_exists = True
|
gns3_snapshot_exists = True
|
||||||
|
|
||||||
if not gns3_snapshot_exists:
|
if not gns3_snapshot_exists:
|
||||||
result = self._execute("snapshot", [self._vmname, "take", "GNS3 Linked Base for clones"])
|
result = yield from self._execute("snapshot", [self._vmname, "take", "GNS3 Linked Base for clones"])
|
||||||
log.debug("GNS3 snapshot created: {}".format(result))
|
log.debug("GNS3 snapshot created: {}".format(result))
|
||||||
|
|
||||||
args = [self._vmname,
|
args = [self._vmname,
|
||||||
@ -767,15 +765,15 @@ class VirtualBoxVM(BaseVM):
|
|||||||
self._working_dir,
|
self._working_dir,
|
||||||
"--register"]
|
"--register"]
|
||||||
|
|
||||||
result = self._execute("clonevm", args)
|
result = yield from self._execute("clonevm", args)
|
||||||
log.debug("cloned VirtualBox VM: {}".format(result))
|
log.debug("cloned VirtualBox VM: {}".format(result))
|
||||||
|
|
||||||
self._vmname = self._name
|
self._vmname = self._name
|
||||||
self._execute("setextradata", [self._vmname, "GNS3/Clone", "yes"])
|
yield from self._execute("setextradata", [self._vmname, "GNS3/Clone", "yes"])
|
||||||
|
|
||||||
args = [self._name, "take", "reset"]
|
args = [self._name, "take", "reset"]
|
||||||
result = self._execute("snapshot", args)
|
result = yield from self._execute("snapshot", args)
|
||||||
log.debug("snapshot reset created: {}".format(result))
|
log.debug("Snapshot reset created: {}".format(result))
|
||||||
|
|
||||||
def _start_remote_console(self):
|
def _start_remote_console(self):
|
||||||
"""
|
"""
|
||||||
@ -784,7 +782,7 @@ class VirtualBoxVM(BaseVM):
|
|||||||
|
|
||||||
# starts the Telnet to pipe thread
|
# starts the Telnet to pipe thread
|
||||||
pipe_name = self._get_pipe_name()
|
pipe_name = self._get_pipe_name()
|
||||||
if sys.platform.startswith('win'):
|
if sys.platform.startswith("win"):
|
||||||
try:
|
try:
|
||||||
self._serial_pipe = open(pipe_name, "a+b")
|
self._serial_pipe = open(pipe_name, "a+b")
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
@ -809,7 +807,7 @@ class VirtualBoxVM(BaseVM):
|
|||||||
self._telnet_server_thread.stop()
|
self._telnet_server_thread.stop()
|
||||||
self._telnet_server_thread.join(timeout=3)
|
self._telnet_server_thread.join(timeout=3)
|
||||||
if self._telnet_server_thread.isAlive():
|
if self._telnet_server_thread.isAlive():
|
||||||
log.warn("Serial pire thread is still alive!")
|
log.warn("Serial pipe thread is still alive!")
|
||||||
self._telnet_server_thread = None
|
self._telnet_server_thread = None
|
||||||
|
|
||||||
if self._serial_pipe:
|
if self._serial_pipe:
|
||||||
@ -819,93 +817,7 @@ class VirtualBoxVM(BaseVM):
|
|||||||
self._serial_pipe.close()
|
self._serial_pipe.close()
|
||||||
self._serial_pipe = None
|
self._serial_pipe = None
|
||||||
|
|
||||||
def old_start(self):
|
@asyncio.coroutine
|
||||||
"""
|
|
||||||
Starts this VirtualBox VM.
|
|
||||||
"""
|
|
||||||
|
|
||||||
# resume the VM if it is paused
|
|
||||||
vm_state = self._get_vm_state()
|
|
||||||
if vm_state == "paused":
|
|
||||||
self.resume()
|
|
||||||
return
|
|
||||||
|
|
||||||
# VM must be powered off and in saved state to start it
|
|
||||||
if vm_state != "poweroff" and vm_state != "saved":
|
|
||||||
raise VirtualBoxError("VirtualBox VM not powered off or saved")
|
|
||||||
|
|
||||||
self._set_network_options()
|
|
||||||
self._set_serial_console()
|
|
||||||
|
|
||||||
args = [self._vmname]
|
|
||||||
if self._headless:
|
|
||||||
args.extend(["--type", "headless"])
|
|
||||||
result = self._execute("startvm", args)
|
|
||||||
log.debug("started VirtualBox VM: {}".format(result))
|
|
||||||
|
|
||||||
# add a guest property to let the VM know about the GNS3 name
|
|
||||||
self._execute("guestproperty", ["set", self._vmname, "NameInGNS3", self._name])
|
|
||||||
|
|
||||||
# add a guest property to let the VM know about the GNS3 project directory
|
|
||||||
self._execute("guestproperty", ["set", self._vmname, "ProjectDirInGNS3", self._working_dir])
|
|
||||||
|
|
||||||
if self._enable_remote_console:
|
|
||||||
self._start_remote_console()
|
|
||||||
|
|
||||||
def old_stop(self):
|
|
||||||
"""
|
|
||||||
Stops this VirtualBox VM.
|
|
||||||
"""
|
|
||||||
|
|
||||||
self._stop_remote_console()
|
|
||||||
vm_state = self._get_vm_state()
|
|
||||||
if vm_state == "running" or vm_state == "paused" or vm_state == "stuck":
|
|
||||||
# power off the VM
|
|
||||||
result = self._control_vm("poweroff")
|
|
||||||
log.debug("VirtualBox VM has been stopped: {}".format(result))
|
|
||||||
|
|
||||||
time.sleep(0.5) # give some time for VirtualBox to unlock the VM
|
|
||||||
# deactivate the first serial port
|
|
||||||
try:
|
|
||||||
self._modify_vm("--uart1 off")
|
|
||||||
except VirtualBoxError as e:
|
|
||||||
log.warn("Could not deactivate the first serial port: {}".format(e))
|
|
||||||
|
|
||||||
for adapter_id in range(0, len(self._ethernet_adapters)):
|
|
||||||
if self._ethernet_adapters[adapter_id] is None:
|
|
||||||
continue
|
|
||||||
self._modify_vm("--nictrace{} off".format(adapter_id + 1))
|
|
||||||
self._modify_vm("--cableconnected{} off".format(adapter_id + 1))
|
|
||||||
self._modify_vm("--nic{} null".format(adapter_id + 1))
|
|
||||||
|
|
||||||
def suspend(self):
|
|
||||||
"""
|
|
||||||
Suspends this VirtualBox VM.
|
|
||||||
"""
|
|
||||||
|
|
||||||
vm_state = self._get_vm_state()
|
|
||||||
if vm_state == "running":
|
|
||||||
result = self._control_vm("pause")
|
|
||||||
log.debug("VirtualBox VM has been suspended: {}".format(result))
|
|
||||||
else:
|
|
||||||
log.info("VirtualBox VM is not running to be suspended, current state is {}".format(vm_state))
|
|
||||||
|
|
||||||
def resume(self):
|
|
||||||
"""
|
|
||||||
Resumes this VirtualBox VM.
|
|
||||||
"""
|
|
||||||
|
|
||||||
result = self._control_vm("resume")
|
|
||||||
log.debug("VirtualBox VM has been resumed: {}".format(result))
|
|
||||||
|
|
||||||
def reload(self):
|
|
||||||
"""
|
|
||||||
Reloads this VirtualBox VM.
|
|
||||||
"""
|
|
||||||
|
|
||||||
result = self._control_vm("reset")
|
|
||||||
log.debug("VirtualBox VM has been reset: {}".format(result))
|
|
||||||
|
|
||||||
def port_add_nio_binding(self, adapter_id, nio):
|
def port_add_nio_binding(self, adapter_id, nio):
|
||||||
"""
|
"""
|
||||||
Adds a port NIO binding.
|
Adds a port NIO binding.
|
||||||
@ -917,24 +829,25 @@ class VirtualBoxVM(BaseVM):
|
|||||||
try:
|
try:
|
||||||
adapter = self._ethernet_adapters[adapter_id]
|
adapter = self._ethernet_adapters[adapter_id]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
raise VirtualBoxError("Adapter {adapter_id} doesn't exist on VirtualBox VM {name}".format(name=self._name,
|
raise VirtualBoxError("Adapter {adapter_id} doesn't exist on VirtualBox VM '{name}'".format(name=self.name,
|
||||||
adapter_id=adapter_id))
|
adapter_id=adapter_id))
|
||||||
|
|
||||||
vm_state = self._get_vm_state()
|
vm_state = yield from self._get_vm_state()
|
||||||
if vm_state == "running":
|
if vm_state == "running":
|
||||||
# dynamically configure an UDP tunnel on the VirtualBox adapter
|
# dynamically configure an UDP tunnel on the VirtualBox adapter
|
||||||
self._control_vm("nic{} generic UDPTunnel".format(adapter_id + 1))
|
yield from self._control_vm("nic{} generic UDPTunnel".format(adapter_id + 1))
|
||||||
self._control_vm("nicproperty{} sport={}".format(adapter_id + 1, nio.lport))
|
yield from self._control_vm("nicproperty{} sport={}".format(adapter_id + 1, nio.lport))
|
||||||
self._control_vm("nicproperty{} dest={}".format(adapter_id + 1, nio.rhost))
|
yield from self._control_vm("nicproperty{} dest={}".format(adapter_id + 1, nio.rhost))
|
||||||
self._control_vm("nicproperty{} dport={}".format(adapter_id + 1, nio.rport))
|
yield from self._control_vm("nicproperty{} dport={}".format(adapter_id + 1, nio.rport))
|
||||||
self._control_vm("setlinkstate{} on".format(adapter_id + 1))
|
yield from self._control_vm("setlinkstate{} on".format(adapter_id + 1))
|
||||||
|
|
||||||
adapter.add_nio(0, nio)
|
adapter.add_nio(0, nio)
|
||||||
log.info("VirtualBox VM {name} [id={id}]: {nio} added to adapter {adapter_id}".format(name=self._name,
|
log.info("VirtualBox VM '{name}' [{uuid}]: {nio} added to adapter {adapter_id}".format(name=self.name,
|
||||||
id=self._id,
|
uuid=self.uuid,
|
||||||
nio=nio,
|
nio=nio,
|
||||||
adapter_id=adapter_id))
|
adapter_id=adapter_id))
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
def port_remove_nio_binding(self, adapter_id):
|
def port_remove_nio_binding(self, adapter_id):
|
||||||
"""
|
"""
|
||||||
Removes a port NIO binding.
|
Removes a port NIO binding.
|
||||||
@ -947,19 +860,19 @@ class VirtualBoxVM(BaseVM):
|
|||||||
try:
|
try:
|
||||||
adapter = self._ethernet_adapters[adapter_id]
|
adapter = self._ethernet_adapters[adapter_id]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
raise VirtualBoxError("Adapter {adapter_id} doesn't exist on VirtualBox VM {name}".format(name=self._name,
|
raise VirtualBoxError("Adapter {adapter_id} doesn't exist on VirtualBox VM '{name}'".format(name=self.name,
|
||||||
adapter_id=adapter_id))
|
adapter_id=adapter_id))
|
||||||
|
|
||||||
vm_state = self._get_vm_state()
|
vm_state = yield from self._get_vm_state()
|
||||||
if vm_state == "running":
|
if vm_state == "running":
|
||||||
# dynamically disable the VirtualBox adapter
|
# dynamically disable the VirtualBox adapter
|
||||||
self._control_vm("setlinkstate{} off".format(adapter_id + 1))
|
yield from self._control_vm("setlinkstate{} off".format(adapter_id + 1))
|
||||||
self._control_vm("nic{} null".format(adapter_id + 1))
|
yield from self._control_vm("nic{} null".format(adapter_id + 1))
|
||||||
|
|
||||||
nio = adapter.get_nio(0)
|
nio = adapter.get_nio(0)
|
||||||
adapter.remove_nio(0)
|
adapter.remove_nio(0)
|
||||||
log.info("VirtualBox VM {name} [id={id}]: {nio} removed from adapter {adapter_id}".format(name=self._name,
|
log.info("VirtualBox VM '{name}' [{uuid}]: {nio} removed from adapter {adapter_id}".format(name=self.name,
|
||||||
id=self._id,
|
uuid=self.uuid,
|
||||||
nio=nio,
|
nio=nio,
|
||||||
adapter_id=adapter_id))
|
adapter_id=adapter_id))
|
||||||
return nio
|
return nio
|
||||||
@ -975,7 +888,7 @@ class VirtualBoxVM(BaseVM):
|
|||||||
try:
|
try:
|
||||||
adapter = self._ethernet_adapters[adapter_id]
|
adapter = self._ethernet_adapters[adapter_id]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
raise VirtualBoxError("Adapter {adapter_id} doesn't exist on VirtualBox VM {name}".format(name=self._name,
|
raise VirtualBoxError("Adapter {adapter_id} doesn't exist on VirtualBox VM '{name}'".format(name=self.name,
|
||||||
adapter_id=adapter_id))
|
adapter_id=adapter_id))
|
||||||
|
|
||||||
nio = adapter.get_nio(0)
|
nio = adapter.get_nio(0)
|
||||||
@ -983,16 +896,14 @@ class VirtualBoxVM(BaseVM):
|
|||||||
raise VirtualBoxError("Packet capture is already activated on adapter {adapter_id}".format(adapter_id=adapter_id))
|
raise VirtualBoxError("Packet capture is already activated on adapter {adapter_id}".format(adapter_id=adapter_id))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
os.makedirs(os.path.dirname(output_file))
|
os.makedirs(os.path.dirname(output_file), exist_ok=True)
|
||||||
except FileExistsError:
|
|
||||||
pass
|
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
raise VirtualBoxError("Could not create captures directory {}".format(e))
|
raise VirtualBoxError("Could not create captures directory {}".format(e))
|
||||||
|
|
||||||
nio.startPacketCapture(output_file)
|
nio.startPacketCapture(output_file)
|
||||||
|
|
||||||
log.info("VirtualBox VM {name} [id={id}]: starting packet capture on adapter {adapter_id}".format(name=self._name,
|
log.info("VirtualBox VM '{name}' [{uuid}]: starting packet capture on adapter {adapter_id}".format(name=self.name,
|
||||||
id=self._id,
|
uuid=self.uuid,
|
||||||
adapter_id=adapter_id))
|
adapter_id=adapter_id))
|
||||||
|
|
||||||
def stop_capture(self, adapter_id):
|
def stop_capture(self, adapter_id):
|
||||||
@ -1005,12 +916,12 @@ class VirtualBoxVM(BaseVM):
|
|||||||
try:
|
try:
|
||||||
adapter = self._ethernet_adapters[adapter_id]
|
adapter = self._ethernet_adapters[adapter_id]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
raise VirtualBoxError("Adapter {adapter_id} doesn't exist on VirtualBox VM {name}".format(name=self._name,
|
raise VirtualBoxError("Adapter {adapter_id} doesn't exist on VirtualBox VM '{name}'".format(name=self.name,
|
||||||
adapter_id=adapter_id))
|
adapter_id=adapter_id))
|
||||||
|
|
||||||
nio = adapter.get_nio(0)
|
nio = adapter.get_nio(0)
|
||||||
nio.stopPacketCapture()
|
nio.stopPacketCapture()
|
||||||
|
|
||||||
log.info("VirtualBox VM {name} [id={id}]: stopping packet capture on adapter {adapter_id}".format(name=self._name,
|
log.info("VirtualBox VM '{name}' [{uuid}]: stopping packet capture on adapter {adapter_id}".format(name=self.name,
|
||||||
id=self._id,
|
uuid=self.uuid,
|
||||||
adapter_id=adapter_id))
|
adapter_id=adapter_id))
|
||||||
|
Loading…
Reference in New Issue
Block a user