From 9cce4de190cd1267fb806e734f03d4ad3ec39fbd Mon Sep 17 00:00:00 2001 From: grossmj Date: Tue, 4 Jun 2019 18:00:44 +0200 Subject: [PATCH] %guest-cid% variable implementation for Qemu VMs. Fixes https://github.com/GNS3/gns3-gui/issues/2804 --- gns3server/compute/qemu/__init__.py | 21 +++++++++++ gns3server/compute/qemu/qemu_vm.py | 18 ++++++++-- gns3server/compute/qemu/utils/__init__.py | 0 gns3server/compute/qemu/utils/guest_cid.py | 38 ++++++++++++++++++++ gns3server/compute/qemu/{ => utils}/qcow2.py | 0 tests/compute/qemu/test_qcow2.py | 2 +- 6 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 gns3server/compute/qemu/utils/__init__.py create mode 100644 gns3server/compute/qemu/utils/guest_cid.py rename gns3server/compute/qemu/{ => utils}/qcow2.py (100%) diff --git a/gns3server/compute/qemu/__init__.py b/gns3server/compute/qemu/__init__.py index 922064e8..10d8187f 100644 --- a/gns3server/compute/qemu/__init__.py +++ b/gns3server/compute/qemu/__init__.py @@ -30,6 +30,7 @@ from ...utils.asyncio import subprocess_check_output from ..base_manager import BaseManager from .qemu_error import QemuError from .qemu_vm import QemuVM +from .utils.guest_cid import get_next_guest_cid import logging log = logging.getLogger(__name__) @@ -40,6 +41,26 @@ class Qemu(BaseManager): _NODE_CLASS = QemuVM _NODE_TYPE = "qemu" + def __init__(self): + + super().__init__() + self._guest_cid_lock = asyncio.Lock() + + async def create_node(self, *args, **kwargs): + """ + Creates a new Qemu VM. + + :returns: QemuVM instance + """ + + async with self._guest_cid_lock: + # wait for a node to be completely created before adding a new one + # this is important otherwise we allocate the same application ID + # when creating multiple Qemu VMs at the same time + guest_cid = get_next_guest_cid(self.nodes) + node = await super().create_node(*args, guest_cid=guest_cid, **kwargs) + return node + @staticmethod async def get_kvm_archs(): """ diff --git a/gns3server/compute/qemu/qemu_vm.py b/gns3server/compute/qemu/qemu_vm.py index d715d873..301eaa20 100644 --- a/gns3server/compute/qemu/qemu_vm.py +++ b/gns3server/compute/qemu/qemu_vm.py @@ -36,6 +36,7 @@ import json from gns3server.utils import parse_version from gns3server.utils.asyncio import subprocess_check_output, cancellable_wait_run_in_executor from .qemu_error import QemuError +from .utils.qcow2 import Qcow2, Qcow2Error from ..adapters.ethernet_adapter import EthernetAdapter from ..nios.nio_udp import NIOUDP from ..nios.nio_tap import NIOTAP @@ -43,7 +44,6 @@ from ..base_node import BaseNode from ...schemas.qemu import QEMU_OBJECT_SCHEMA, QEMU_PLATFORMS from ...utils.asyncio import monitor_process from ...utils.images import md5sum -from .qcow2 import Qcow2, Qcow2Error from ...utils import macaddress_to_int, int_to_macaddress @@ -67,7 +67,7 @@ class QemuVM(BaseNode): :param platform: Platform to emulate """ - def __init__(self, name, node_id, project, manager, linked_clone=True, qemu_path=None, console=None, console_type="telnet", platform=None): + def __init__(self, name, node_id, project, manager, guest_cid=None, linked_clone=True, qemu_path=None, console=None, console_type="telnet", platform=None): super().__init__(name, node_id, project, manager, console=console, console_type=console_type, linked_clone=linked_clone, wrap_console=True) server_config = manager.config.get_section_config("Server") @@ -80,6 +80,7 @@ class QemuVM(BaseNode): self._qemu_img_stdout_file = "" self._execute_lock = asyncio.Lock() self._local_udp_tunnels = {} + self._guest_cid = guest_cid # QEMU VM settings if qemu_path: @@ -124,6 +125,16 @@ class QemuVM(BaseNode): self.adapters = 1 # creates 1 adapter by default log.info('QEMU VM "{name}" [{id}] has been created'.format(name=self._name, id=self._id)) + @property + def guest_cid(self): + """ + Returns guest_cid (console ID) which unique identifier between 3 and 65535 + + :returns: integer between 3 and 65535 + """ + + return self._guest_cid + @property def monitor(self): """ @@ -1917,7 +1928,8 @@ class QemuVM(BaseNode): additional_options = additional_options.replace("%vm-id%", self._id) additional_options = additional_options.replace("%project-id%", self.project.id) additional_options = additional_options.replace("%project-path%", '"' + self.project.path.replace('"', '\\"') + '"') - if self._console: + additional_options = additional_options.replace("%guest-cid%", str(self._guest_cid)) + if self._console_type != "none" and self._console: additional_options = additional_options.replace("%console-port%", str(self._console)) command = [self.qemu_path] command.extend(["-name", self._name]) diff --git a/gns3server/compute/qemu/utils/__init__.py b/gns3server/compute/qemu/utils/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/gns3server/compute/qemu/utils/guest_cid.py b/gns3server/compute/qemu/utils/guest_cid.py new file mode 100644 index 00000000..22218b31 --- /dev/null +++ b/gns3server/compute/qemu/utils/guest_cid.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2017 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 . + +from ..qemu_error import QemuError + +import logging +log = logging.getLogger(__name__) + + +def get_next_guest_cid(nodes): + """ + Calculates free guest_id from given nodes + + :param nodes: + :raises QemuError when exceeds number + :return: integer first free cid + """ + + used = set([n.guest_cid for n in nodes]) + pool = set(range(3, 65535)) + try: + return (pool - used).pop() + except KeyError: + raise QemuError("Cannot create a new Qemu VM (limit of 65535 guest ID on one host reached)") diff --git a/gns3server/compute/qemu/qcow2.py b/gns3server/compute/qemu/utils/qcow2.py similarity index 100% rename from gns3server/compute/qemu/qcow2.py rename to gns3server/compute/qemu/utils/qcow2.py diff --git a/tests/compute/qemu/test_qcow2.py b/tests/compute/qemu/test_qcow2.py index bf09e584..d8532940 100644 --- a/tests/compute/qemu/test_qcow2.py +++ b/tests/compute/qemu/test_qcow2.py @@ -20,7 +20,7 @@ import pytest import shutil import asyncio -from gns3server.compute.qemu.qcow2 import Qcow2, Qcow2Error +from gns3server.compute.qemu.utils.qcow2 import Qcow2, Qcow2Error def qemu_img():