1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-28 11:18:11 +00:00

Prevent corruption of VM in VirtualBox when using linked clone

Fix https://github.com/GNS3/gns3-gui/issues/1821
This commit is contained in:
Julien Duponchelle 2017-01-31 18:58:43 +01:00
parent 27a1089806
commit f0ff035c0b
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
4 changed files with 37 additions and 10 deletions

View File

@ -161,7 +161,7 @@ class VirtualBox(BaseManager):
continue continue
@asyncio.coroutine @asyncio.coroutine
def list_vms(self): def list_vms(self, allow_clone=False):
""" """
Gets VirtualBox VM list. Gets VirtualBox VM list.
""" """
@ -176,7 +176,7 @@ class VirtualBox(BaseManager):
if vmname == "<inaccessible>": if vmname == "<inaccessible>":
continue # ignore inaccessible VMs continue # ignore inaccessible VMs
extra_data = yield from self.execute("getextradata", [vmname, "GNS3/Clone"]) extra_data = yield from self.execute("getextradata", [vmname, "GNS3/Clone"])
if len(extra_data) == 0 or not extra_data[0].strip() == "Value: yes": if allow_clone or len(extra_data) == 0 or not extra_data[0].strip() == "Value: yes":
# get the amount of RAM # get the amount of RAM
info_results = yield from self.execute("showvminfo", [vmname, "--machinereadable"]) info_results = yield from self.execute("showvminfo", [vmname, "--machinereadable"])
ram = 0 ram = 0

View File

@ -603,9 +603,16 @@ class VirtualBoxVM(BaseNode):
:param vmname: VirtualBox VM name :param vmname: VirtualBox VM name
""" """
if vmname == self._vmname:
return
if self.linked_clone: if self.linked_clone:
if self.status == "started": if self.status == "started":
raise VirtualBoxError("You can't change the name of running VM {}".format(self._name)) raise VirtualBoxError("You can't change the name of running VM {}".format(self._name))
# We can't rename a VM to name that already exists
vms = yield from self.manager.list_vms(allow_clone=True)
if vmname in [vm["vmname"] for vm in vms]:
raise VirtualBoxError("You can't change the name to {} it's already use in VirtualBox".format(vmname))
yield from self._modify_vm('--name "{}"'.format(vmname)) yield from self._modify_vm('--name "{}"'.format(vmname))
log.info("VirtualBox VM '{name}' [{id}] has set the VM name to '{vmname}'".format(name=self.name, id=self.id, vmname=vmname)) log.info("VirtualBox VM '{name}' [{id}] has set the VM name to '{vmname}'".format(name=self.name, id=self.id, vmname=vmname))

View File

@ -112,10 +112,13 @@ class VirtualBoxHandler:
vbox_manager = VirtualBox.instance() vbox_manager = VirtualBox.instance()
vm = vbox_manager.get_node(request.match_info["node_id"], project_id=request.match_info["project_id"]) vm = vbox_manager.get_node(request.match_info["node_id"], project_id=request.match_info["project_id"])
if "vmname" in request.json: if "name" in request.json:
vmname = request.json.pop("vmname") name = request.json.pop("name")
if vmname != vm.vmname: vmname = request.json.pop("vmname", None)
yield from vm.set_vmname(vmname) if name != vm.name:
vm.name = name
if vm.linked_clone:
yield from vm.set_vmname(vm.name)
if "adapters" in request.json: if "adapters" in request.json:
adapters = int(request.json.pop("adapters")) adapters = int(request.json.pop("adapters"))

View File

@ -18,7 +18,7 @@
import os import os
import pytest import pytest
import asyncio import asyncio
from tests.utils import asyncio_patch from tests.utils import asyncio_patch, AsyncioMagicMock
from gns3server.compute.virtualbox.virtualbox_vm import VirtualBoxVM from gns3server.compute.virtualbox.virtualbox_vm import VirtualBoxVM
from gns3server.compute.virtualbox.virtualbox_error import VirtualBoxError from gns3server.compute.virtualbox.virtualbox_error import VirtualBoxError
@ -46,13 +46,30 @@ def test_vm(project, manager):
def test_rename_vmname(project, manager, async_run): def test_rename_vmname(project, manager, async_run):
""" """
Rename a VM is not allowed when using linked clone Rename a VM is not allowed when using a running linked clone
or if the vm already exists in Vbox
""" """
vm = VirtualBoxVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager, "test", False) vm = VirtualBoxVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager, "test", False)
vm._node_status = "started" vm.manager.list_vms = AsyncioMagicMock(return_value=[{"vmname": "Debian"}])
vm._linked_clone = True vm._linked_clone = True
vm._modify_vm = AsyncioMagicMock()
# Vm is running
vm._node_status = "started"
with pytest.raises(VirtualBoxError): with pytest.raises(VirtualBoxError):
async_run(vm.set_vmname("toto")) async_run(vm.set_vmname("Arch"))
assert not vm._modify_vm.called
vm._node_status = "stopped"
# Name already use
with pytest.raises(VirtualBoxError):
async_run(vm.set_vmname("Debian"))
assert not vm._modify_vm.called
# Work
async_run(vm.set_vmname("Arch"))
assert vm._modify_vm.called
def test_vm_valid_virtualbox_api_version(loop, project, manager): def test_vm_valid_virtualbox_api_version(loop, project, manager):