2015-02-19 17:46:57 +02:00
|
|
|
#
|
|
|
|
# Copyright (C) 2015 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/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Qemu server module.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import asyncio
|
2015-02-20 15:39:13 +02:00
|
|
|
import os
|
2016-01-01 02:40:12 +02:00
|
|
|
import platform
|
2015-02-20 15:39:13 +02:00
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
import subprocess
|
2015-02-19 17:46:57 +02:00
|
|
|
|
2015-02-20 15:39:13 +02:00
|
|
|
from ...utils.asyncio import subprocess_check_output
|
2020-06-28 17:35:39 +03:00
|
|
|
from ...utils.get_resource import get_resource
|
2015-02-19 17:46:57 +02:00
|
|
|
from ..base_manager import BaseManager
|
2020-06-28 17:35:39 +03:00
|
|
|
from ..error import NodeError, ImageMissingError
|
2015-02-19 17:46:57 +02:00
|
|
|
from .qemu_error import QemuError
|
|
|
|
from .qemu_vm import QemuVM
|
2019-06-04 19:00:44 +03:00
|
|
|
from .utils.guest_cid import get_next_guest_cid
|
2020-06-28 17:35:39 +03:00
|
|
|
from .utils.ziputils import unpack_zip
|
2015-02-19 17:46:57 +02:00
|
|
|
|
2015-04-23 06:42:36 +03:00
|
|
|
import logging
|
2021-04-13 12:16:50 +03:00
|
|
|
|
2015-04-23 06:42:36 +03:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2015-02-19 17:46:57 +02:00
|
|
|
|
|
|
|
class Qemu(BaseManager):
|
2015-04-08 20:17:34 +03:00
|
|
|
|
2016-05-11 20:35:36 +03:00
|
|
|
_NODE_CLASS = QemuVM
|
2016-06-07 20:38:01 +03:00
|
|
|
_NODE_TYPE = "qemu"
|
2015-02-19 17:46:57 +02:00
|
|
|
|
2019-06-04 19:00:44 +03:00
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
super().__init__()
|
|
|
|
self._guest_cid_lock = asyncio.Lock()
|
2020-08-13 10:40:31 +03:00
|
|
|
self.config_disk = "config.img"
|
2020-06-28 17:35:39 +03:00
|
|
|
self._init_config_disk()
|
2019-06-04 19:00:44 +03:00
|
|
|
|
|
|
|
async def create_node(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Creates a new Qemu VM.
|
|
|
|
|
|
|
|
:returns: QemuVM instance
|
|
|
|
"""
|
|
|
|
|
2019-06-05 12:25:35 +03:00
|
|
|
node = await super().create_node(*args, **kwargs)
|
|
|
|
|
|
|
|
# allocate a guest console ID (CID)
|
|
|
|
if node.console_type != "none" and node.console:
|
|
|
|
# by default, the guest CID is equal to the console port
|
|
|
|
node.guest_cid = node.console
|
|
|
|
else:
|
|
|
|
# otherwise pick a guest CID if no console port is configured
|
|
|
|
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 guest ID
|
|
|
|
# when creating multiple Qemu VMs at the same time
|
|
|
|
node.guest_cid = get_next_guest_cid(self.nodes)
|
2019-06-04 19:00:44 +03:00
|
|
|
return node
|
|
|
|
|
2016-01-01 02:40:12 +02:00
|
|
|
@staticmethod
|
2018-10-15 13:05:49 +03:00
|
|
|
async def get_kvm_archs():
|
2016-01-01 02:40:12 +02:00
|
|
|
"""
|
|
|
|
Gets a list of architectures for which KVM is available on this server.
|
|
|
|
|
|
|
|
:returns: List of architectures for which KVM is available on this server.
|
|
|
|
"""
|
2018-03-15 09:17:39 +02:00
|
|
|
|
2016-01-01 02:40:12 +02:00
|
|
|
kvm = []
|
2016-01-04 17:30:06 +02:00
|
|
|
|
2016-01-26 14:57:55 +02:00
|
|
|
if not os.path.exists("/dev/kvm"):
|
2016-01-04 17:30:06 +02:00
|
|
|
return kvm
|
|
|
|
|
2016-01-26 14:57:55 +02:00
|
|
|
arch = platform.machine()
|
|
|
|
if arch == "x86_64":
|
|
|
|
kvm.append("x86_64")
|
|
|
|
kvm.append("i386")
|
|
|
|
elif arch == "i386":
|
|
|
|
kvm.append("i386")
|
|
|
|
else:
|
|
|
|
kvm.append(platform.machine())
|
2016-01-01 02:40:12 +02:00
|
|
|
return kvm
|
|
|
|
|
2015-02-20 15:39:13 +02:00
|
|
|
@staticmethod
|
2015-05-10 20:46:51 +03:00
|
|
|
def paths_list():
|
2015-02-20 15:39:13 +02:00
|
|
|
"""
|
2015-05-10 20:46:51 +03:00
|
|
|
Gets a folder list of possibly available QEMU binaries on the host.
|
2015-02-20 15:39:13 +02:00
|
|
|
|
2015-05-10 20:46:51 +03:00
|
|
|
:returns: List of folders where Qemu binaries MAY reside.
|
2015-02-20 15:39:13 +02:00
|
|
|
"""
|
|
|
|
|
2015-06-02 16:35:14 +03:00
|
|
|
paths = set()
|
2015-04-26 21:49:29 +03:00
|
|
|
try:
|
2015-06-02 16:35:14 +03:00
|
|
|
paths.add(os.getcwd())
|
2015-04-26 21:49:29 +03:00
|
|
|
except FileNotFoundError:
|
|
|
|
log.warning("The current working directory doesn't exist")
|
2015-04-23 06:42:36 +03:00
|
|
|
if "PATH" in os.environ:
|
2015-06-02 16:35:14 +03:00
|
|
|
paths.update(os.environ["PATH"].split(os.pathsep))
|
2015-04-23 06:42:36 +03:00
|
|
|
else:
|
|
|
|
log.warning("The PATH environment variable doesn't exist")
|
2015-02-20 15:39:13 +02:00
|
|
|
# look for Qemu binaries in the current working directory and $PATH
|
2022-01-19 13:58:36 +02:00
|
|
|
if sys.platform.startswith("darwin") and hasattr(sys, "frozen"):
|
|
|
|
# add specific locations on Mac OS X regardless of what's in $PATH
|
|
|
|
paths.update(["/usr/bin", "/usr/local/bin", "/opt/local/bin"])
|
|
|
|
try:
|
2015-02-20 15:39:13 +02:00
|
|
|
exec_dir = os.path.dirname(os.path.abspath(sys.executable))
|
2022-01-19 13:58:36 +02:00
|
|
|
paths.add(os.path.abspath(os.path.join(exec_dir, "qemu/bin")))
|
|
|
|
# If the user run the server by hand from outside
|
|
|
|
except FileNotFoundError:
|
|
|
|
paths.add("/Applications/GNS3.app/Contents/MacOS/qemu/bin")
|
2015-05-10 20:46:51 +03:00
|
|
|
return paths
|
|
|
|
|
|
|
|
@staticmethod
|
2018-10-15 13:05:49 +03:00
|
|
|
async def binary_list(archs=None):
|
2015-05-10 20:46:51 +03:00
|
|
|
"""
|
|
|
|
Gets QEMU binaries list available on the host.
|
|
|
|
|
|
|
|
:returns: Array of dictionary {"path": Qemu binary path, "version": version of Qemu}
|
|
|
|
"""
|
|
|
|
|
|
|
|
qemus = []
|
|
|
|
for path in Qemu.paths_list():
|
2021-04-13 12:07:58 +03:00
|
|
|
log.debug(f"Searching for Qemu binaries in '{path}'")
|
2015-02-20 15:39:13 +02:00
|
|
|
try:
|
|
|
|
for f in os.listdir(path):
|
2021-04-13 12:16:50 +03:00
|
|
|
if (
|
|
|
|
(f.startswith("qemu-system") or f.startswith("qemu-kvm") or f == "qemu" or f == "qemu.exe")
|
|
|
|
and os.access(os.path.join(path, f), os.X_OK)
|
|
|
|
and os.path.isfile(os.path.join(path, f))
|
|
|
|
):
|
2016-01-01 02:40:12 +02:00
|
|
|
if archs is not None:
|
|
|
|
for arch in archs:
|
2021-04-13 12:07:58 +03:00
|
|
|
if f.endswith(arch) or f.endswith(f"{arch}.exe") or f.endswith(f"{arch}w.exe"):
|
2016-01-01 02:40:12 +02:00
|
|
|
qemu_path = os.path.join(path, f)
|
2018-10-15 13:05:49 +03:00
|
|
|
version = await Qemu.get_qemu_version(qemu_path)
|
2016-01-01 02:40:12 +02:00
|
|
|
qemus.append({"path": qemu_path, "version": version})
|
|
|
|
else:
|
|
|
|
qemu_path = os.path.join(path, f)
|
2018-10-15 13:05:49 +03:00
|
|
|
version = await Qemu.get_qemu_version(qemu_path)
|
2016-01-01 02:40:12 +02:00
|
|
|
qemus.append({"path": qemu_path, "version": version})
|
|
|
|
|
2015-02-20 15:39:13 +02:00
|
|
|
except OSError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
return qemus
|
|
|
|
|
|
|
|
@staticmethod
|
2018-10-15 13:05:49 +03:00
|
|
|
async def get_qemu_version(qemu_path):
|
2015-02-20 15:39:13 +02:00
|
|
|
"""
|
|
|
|
Gets the Qemu version.
|
2015-04-08 20:17:34 +03:00
|
|
|
|
|
|
|
:param qemu_path: path to Qemu executable.
|
2015-02-20 15:39:13 +02:00
|
|
|
"""
|
|
|
|
|
2022-01-19 13:58:36 +02:00
|
|
|
try:
|
|
|
|
output = await subprocess_check_output(qemu_path, "-version", "-nographic")
|
|
|
|
match = re.search(r"version\s+([0-9a-z\-\.]+)", output)
|
|
|
|
if match:
|
|
|
|
version = match.group(1)
|
|
|
|
return version
|
|
|
|
else:
|
|
|
|
raise QemuError(f"Could not determine the Qemu version for {qemu_path}")
|
|
|
|
except (OSError, subprocess.SubprocessError) as e:
|
|
|
|
raise QemuError(f"Error while looking for the Qemu version: {e}")
|
2015-02-26 03:55:35 +02:00
|
|
|
|
2015-05-10 20:46:51 +03:00
|
|
|
@staticmethod
|
2018-10-15 13:05:49 +03:00
|
|
|
async def _get_qemu_img_version(qemu_img_path):
|
2015-05-10 20:46:51 +03:00
|
|
|
"""
|
|
|
|
Gets the Qemu-img version.
|
|
|
|
|
|
|
|
:param qemu_img_path: path to Qemu-img executable.
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
2018-10-15 13:05:49 +03:00
|
|
|
output = await subprocess_check_output(qemu_img_path, "--version")
|
2019-01-17 13:01:58 +02:00
|
|
|
match = re.search(r"version\s+([0-9a-z\-\.]+)", output)
|
2015-05-10 20:46:51 +03:00
|
|
|
if match:
|
|
|
|
version = match.group(1)
|
|
|
|
return version
|
|
|
|
else:
|
2023-02-26 12:51:24 +02:00
|
|
|
raise QemuError("Could not determine the Qemu-img version for '{}'".format(qemu_img_path))
|
2018-09-11 16:06:01 +03:00
|
|
|
except (OSError, subprocess.SubprocessError) as e:
|
2015-05-10 20:46:51 +03:00
|
|
|
raise QemuError("Error while looking for the Qemu-img version: {}".format(e))
|
|
|
|
|
2023-02-26 12:51:24 +02:00
|
|
|
@staticmethod
|
|
|
|
async def get_swtpm_version(swtpm_path):
|
|
|
|
"""
|
|
|
|
Gets the swtpm version.
|
|
|
|
|
|
|
|
:param swtpm_path: path to swtpm executable.
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
output = await subprocess_check_output(swtpm_path, "--version")
|
|
|
|
match = re.search(r"version\s+([\d.]+)", output)
|
|
|
|
if match:
|
|
|
|
version = match.group(1)
|
|
|
|
return version
|
|
|
|
else:
|
|
|
|
raise QemuError("Could not determine the swtpm version for '{}'".format(swtpm_path))
|
|
|
|
except (OSError, subprocess.SubprocessError) as e:
|
|
|
|
raise QemuError("Error while looking for the swtpm version: {}".format(e))
|
|
|
|
|
2018-03-21 11:41:25 +02:00
|
|
|
@staticmethod
|
|
|
|
def get_haxm_windows_version():
|
|
|
|
"""
|
|
|
|
Gets the HAXM version number (Windows).
|
|
|
|
|
|
|
|
:returns: HAXM version number. Returns None if HAXM is not installed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
assert(sys.platform.startswith("win"))
|
|
|
|
import winreg
|
|
|
|
|
|
|
|
hkey = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products")
|
|
|
|
version = None
|
|
|
|
for index in range(winreg.QueryInfoKey(hkey)[0]):
|
|
|
|
product_id = winreg.EnumKey(hkey, index)
|
|
|
|
try:
|
|
|
|
product_key = winreg.OpenKey(hkey, r"{}\InstallProperties".format(product_id))
|
|
|
|
try:
|
|
|
|
if winreg.QueryValueEx(product_key, "DisplayName")[0].endswith("Hardware Accelerated Execution Manager"):
|
|
|
|
version = winreg.QueryValueEx(product_key, "DisplayVersion")[0]
|
|
|
|
break
|
|
|
|
finally:
|
|
|
|
winreg.CloseKey(product_key)
|
|
|
|
except OSError:
|
|
|
|
continue
|
|
|
|
winreg.CloseKey(hkey)
|
|
|
|
return version
|
2015-05-10 20:46:51 +03:00
|
|
|
|
2015-02-26 03:55:35 +02:00
|
|
|
@staticmethod
|
|
|
|
def get_legacy_vm_workdir(legacy_vm_id, name):
|
|
|
|
"""
|
2016-05-11 20:35:36 +03:00
|
|
|
Returns the name of the legacy working directory name for a node.
|
2015-02-26 03:55:35 +02:00
|
|
|
|
|
|
|
:param legacy_vm_id: legacy VM identifier (integer)
|
2016-05-11 20:35:36 +03:00
|
|
|
:param: node name (not used)
|
2015-02-26 03:55:35 +02:00
|
|
|
|
|
|
|
:returns: working directory name
|
|
|
|
"""
|
|
|
|
|
2021-04-13 12:07:58 +03:00
|
|
|
return os.path.join("qemu", f"vm-{legacy_vm_id}")
|
2015-04-14 19:46:55 +03:00
|
|
|
|
2020-06-28 17:35:39 +03:00
|
|
|
def _init_config_disk(self):
|
|
|
|
"""
|
|
|
|
Initialize the default config disk
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.get_abs_image_path(self.config_disk)
|
2020-08-13 10:40:31 +03:00
|
|
|
except (NodeError, ImageMissingError):
|
2021-04-13 12:07:58 +03:00
|
|
|
config_disk_zip = get_resource(f"compute/qemu/resources/{self.config_disk}.zip")
|
2020-06-28 17:35:39 +03:00
|
|
|
if config_disk_zip and os.path.exists(config_disk_zip):
|
|
|
|
directory = self.get_images_directory()
|
|
|
|
try:
|
|
|
|
unpack_zip(config_disk_zip, directory)
|
|
|
|
except OSError as e:
|
2021-04-13 12:07:58 +03:00
|
|
|
log.warning(f"Config disk creation: {e}")
|
2020-06-28 17:35:39 +03:00
|
|
|
else:
|
2021-04-13 12:07:58 +03:00
|
|
|
log.warning(f"Config disk: image '{self.config_disk}' missing")
|