mirror of
https://github.com/GNS3/gns3-server
synced 2024-11-28 11:18:11 +00:00
Fix VirtualBox VM close.
This commit is contained in:
parent
33d5882a4a
commit
df72369b0e
@ -24,6 +24,9 @@ import sys
|
|||||||
import shutil
|
import shutil
|
||||||
import asyncio
|
import asyncio
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import logging
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
from ..base_manager import BaseManager
|
from ..base_manager import BaseManager
|
||||||
from .virtualbox_vm import VirtualBoxVM
|
from .virtualbox_vm import VirtualBoxVM
|
||||||
@ -82,6 +85,7 @@ class VirtualBox(BaseManager):
|
|||||||
vboxmanage_path = self.find_vboxmanage()
|
vboxmanage_path = self.find_vboxmanage()
|
||||||
command = [vboxmanage_path, "--nologo", subcommand]
|
command = [vboxmanage_path, "--nologo", subcommand]
|
||||||
command.extend(args)
|
command.extend(args)
|
||||||
|
log.debug("Executing VBoxManage with command: {}".format(command))
|
||||||
try:
|
try:
|
||||||
vbox_user = self.config.get_section_config("VirtualBox").get("vbox_user")
|
vbox_user = self.config.get_section_config("VirtualBox").get("vbox_user")
|
||||||
if vbox_user:
|
if vbox_user:
|
||||||
@ -100,8 +104,9 @@ class VirtualBox(BaseManager):
|
|||||||
|
|
||||||
if process.returncode:
|
if process.returncode:
|
||||||
# only the first line of the output is useful
|
# only the first line of the output is useful
|
||||||
vboxmanage_error = stderr_data.decode("utf-8", errors="ignore").splitlines()[0]
|
vboxmanage_error = stderr_data.decode("utf-8", errors="ignore")
|
||||||
raise VirtualBoxError(vboxmanage_error)
|
log.warn("VBoxManage has returned an error: {}".format(vboxmanage_error))
|
||||||
|
raise VirtualBoxError(vboxmanage_error.splitlines()[0])
|
||||||
|
|
||||||
return stdout_data.decode("utf-8", errors="ignore").splitlines()
|
return stdout_data.decode("utf-8", errors="ignore").splitlines()
|
||||||
|
|
||||||
|
@ -305,7 +305,12 @@ class VirtualBoxVM(BaseVM):
|
|||||||
for hdd_info in hdd_table:
|
for hdd_info in hdd_table:
|
||||||
hdd_file = os.path.join(self.working_dir, self._vmname, "Snapshots", hdd_info["hdd"])
|
hdd_file = os.path.join(self.working_dir, self._vmname, "Snapshots", hdd_info["hdd"])
|
||||||
if os.path.exists(hdd_file):
|
if os.path.exists(hdd_file):
|
||||||
log.debug("reattaching hdd {}".format(hdd_file))
|
log.info("VirtualBox VM '{name}' [{uuid}] attaching HDD {controller} {port} {device} {medium}".format(name=self.name,
|
||||||
|
uuid=self.uuid,
|
||||||
|
controller=hdd_info["controller"],
|
||||||
|
port=hdd_info["port"],
|
||||||
|
device=hdd_info["device"],
|
||||||
|
medium=hdd_file))
|
||||||
yield from self._storage_attach('--storagectl "{}" --port {} --device {} --type hdd --medium "{}"'.format(hdd_info["controller"],
|
yield from self._storage_attach('--storagectl "{}" --port {} --device {} --type hdd --medium "{}"'.format(hdd_info["controller"],
|
||||||
hdd_info["port"],
|
hdd_info["port"],
|
||||||
hdd_info["device"],
|
hdd_info["device"],
|
||||||
@ -327,7 +332,7 @@ class VirtualBoxVM(BaseVM):
|
|||||||
hdd_table = []
|
hdd_table = []
|
||||||
if os.path.exists(self.working_dir):
|
if os.path.exists(self.working_dir):
|
||||||
hdd_files = yield from self._get_all_hdd_files()
|
hdd_files = yield from self._get_all_hdd_files()
|
||||||
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():
|
||||||
match = re.search("^([\s\w]+)\-(\d)\-(\d)$", entry)
|
match = re.search("^([\s\w]+)\-(\d)\-(\d)$", entry)
|
||||||
if match:
|
if match:
|
||||||
@ -335,6 +340,11 @@ class VirtualBoxVM(BaseVM):
|
|||||||
port = match.group(2)
|
port = match.group(2)
|
||||||
device = match.group(3)
|
device = match.group(3)
|
||||||
if value in hdd_files:
|
if value in hdd_files:
|
||||||
|
log.info("VirtualBox VM '{name}' [{uuid}] detaching HDD {controller} {port} {device}".format(name=self.name,
|
||||||
|
uuid=self.uuid,
|
||||||
|
controller=controller,
|
||||||
|
port=port,
|
||||||
|
device=device))
|
||||||
yield from self._storage_attach('--storagectl "{}" --port {} --device {} --type hdd --medium none'.format(controller, port, device))
|
yield from self._storage_attach('--storagectl "{}" --port {} --device {} --type hdd --medium none'.format(controller, port, device))
|
||||||
hdd_table.append(
|
hdd_table.append(
|
||||||
{
|
{
|
||||||
@ -345,16 +355,18 @@ class VirtualBoxVM(BaseVM):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
yield from self.manager.execute("unregistervm", [self._vmname])
|
log.info("VirtualBox VM '{name}' [{uuid}] unregistering".format(name=self.name, uuid=self.uuid))
|
||||||
|
yield from self.manager.execute("unregistervm", [self._name])
|
||||||
|
|
||||||
if hdd_table:
|
if hdd_table:
|
||||||
try:
|
try:
|
||||||
hdd_info_file = os.path.join(self.working_dir, self._vmname, "hdd_info.json")
|
hdd_info_file = os.path.join(self.working_dir, self._vmname, "hdd_info.json")
|
||||||
with open(hdd_info_file, "w") as f:
|
with open(hdd_info_file, "w") as f:
|
||||||
# log.info("saving project: {}".format(path))
|
|
||||||
json.dump(hdd_table, f, indent=4)
|
json.dump(hdd_table, f, indent=4)
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
raise VirtualBoxError("Could not write HDD info file: {}".format(e))
|
log.warning("VirtualBox VM '{name}' [{uuid}] could not write HHD info file: {error}".format(name=self.name,
|
||||||
|
uuid=self.uuid,
|
||||||
|
error=e.strerror))
|
||||||
|
|
||||||
log.info("VirtualBox VM '{name}' [{uuid}] closed".format(name=self.name,
|
log.info("VirtualBox VM '{name}' [{uuid}] closed".format(name=self.name,
|
||||||
uuid=self.uuid))
|
uuid=self.uuid))
|
||||||
@ -403,10 +415,10 @@ class VirtualBoxVM(BaseVM):
|
|||||||
|
|
||||||
if enable_remote_console:
|
if enable_remote_console:
|
||||||
log.info("VirtualBox VM '{name}' [{uuid}] has enabled the console".format(name=self.name, uuid=self.uuid))
|
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}' [{uuid}] has disabled the console".format(name=self.name, uuid=self.uuid))
|
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
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -454,9 +466,9 @@ class VirtualBoxVM(BaseVM):
|
|||||||
self._ethernet_adapters.append(EthernetAdapter())
|
self._ethernet_adapters.append(EthernetAdapter())
|
||||||
|
|
||||||
self._adapters = len(self._ethernet_adapters)
|
self._adapters = len(self._ethernet_adapters)
|
||||||
log.info("VirtualBox VM '{name}' [{uuid}]: number of Ethernet adapters changed to {adapters}".format(name=self.name,
|
log.info("VirtualBox VM '{name}' [{uuid}] has changed the number of Ethernet adapters to {adapters}".format(name=self.name,
|
||||||
uuid=self.uuid,
|
uuid=self.uuid,
|
||||||
adapters=adapters))
|
adapters=adapters))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def adapter_start_index(self):
|
def adapter_start_index(self):
|
||||||
@ -690,7 +702,7 @@ class VirtualBoxVM(BaseVM):
|
|||||||
self._vmname = self._name
|
self._vmname = self._name
|
||||||
yield from self.manager.execute("setextradata", [self._vmname, "GNS3/Clone", "yes"])
|
yield from self.manager.execute("setextradata", [self._vmname, "GNS3/Clone", "yes"])
|
||||||
|
|
||||||
args = [self._name, "take", "reset"]
|
args = [self._vmname, "take", "reset"]
|
||||||
result = yield from self.manager.execute("snapshot", args)
|
result = yield from self.manager.execute("snapshot", args)
|
||||||
log.debug("Snapshot reset created: {}".format(result))
|
log.debug("Snapshot reset created: {}".format(result))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user