2015-02-19 15:46:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 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 13:39:13 +00:00
|
|
|
import os
|
2016-01-01 00:40:12 +00:00
|
|
|
import platform
|
2015-02-20 13:39:13 +00:00
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
import subprocess
|
2015-02-19 15:46:57 +00:00
|
|
|
|
2015-02-20 13:39:13 +00:00
|
|
|
from ...utils.asyncio import subprocess_check_output
|
2015-02-19 15:46:57 +00:00
|
|
|
from ..base_manager import BaseManager
|
|
|
|
from .qemu_error import QemuError
|
|
|
|
from .qemu_vm import QemuVM
|
|
|
|
|
2015-04-23 03:42:36 +00:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
|
|
class Qemu(BaseManager):
|
2015-04-08 17:17:34 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
_NODE_CLASS = QemuVM
|
2015-02-19 15:46:57 +00:00
|
|
|
|
2016-01-01 00:40:12 +00:00
|
|
|
@staticmethod
|
2016-01-04 15:30:06 +00:00
|
|
|
@asyncio.coroutine
|
2016-01-01 00:40:12 +00:00
|
|
|
def get_kvm_archs():
|
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
kvm = []
|
2016-01-04 15:30:06 +00:00
|
|
|
|
2016-01-26 12:57:55 +00:00
|
|
|
if not os.path.exists("/dev/kvm"):
|
2016-01-04 15:30:06 +00:00
|
|
|
return kvm
|
|
|
|
|
2016-01-26 12:57:55 +00: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 00:40:12 +00:00
|
|
|
return kvm
|
|
|
|
|
2015-02-20 13:39:13 +00:00
|
|
|
@staticmethod
|
2015-05-10 17:46:51 +00:00
|
|
|
def paths_list():
|
2015-02-20 13:39:13 +00:00
|
|
|
"""
|
2015-05-10 17:46:51 +00:00
|
|
|
Gets a folder list of possibly available QEMU binaries on the host.
|
2015-02-20 13:39:13 +00:00
|
|
|
|
2015-05-10 17:46:51 +00:00
|
|
|
:returns: List of folders where Qemu binaries MAY reside.
|
2015-02-20 13:39:13 +00:00
|
|
|
"""
|
|
|
|
|
2015-06-02 13:35:14 +00:00
|
|
|
paths = set()
|
2015-04-26 18:49:29 +00:00
|
|
|
try:
|
2015-06-02 13:35:14 +00:00
|
|
|
paths.add(os.getcwd())
|
2015-04-26 18:49:29 +00:00
|
|
|
except FileNotFoundError:
|
|
|
|
log.warning("The current working directory doesn't exist")
|
2015-04-23 03:42:36 +00:00
|
|
|
if "PATH" in os.environ:
|
2015-06-02 13:35:14 +00:00
|
|
|
paths.update(os.environ["PATH"].split(os.pathsep))
|
2015-04-23 03:42:36 +00:00
|
|
|
else:
|
|
|
|
log.warning("The PATH environment variable doesn't exist")
|
2015-02-20 13:39:13 +00:00
|
|
|
# look for Qemu binaries in the current working directory and $PATH
|
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
# add specific Windows paths
|
|
|
|
if hasattr(sys, "frozen"):
|
|
|
|
# add any qemu dir in the same location as gns3server.exe to the list of paths
|
|
|
|
exec_dir = os.path.dirname(os.path.abspath(sys.executable))
|
|
|
|
for f in os.listdir(exec_dir):
|
|
|
|
if f.lower().startswith("qemu"):
|
2015-06-02 13:35:14 +00:00
|
|
|
paths.add(os.path.join(exec_dir, f))
|
2015-02-20 13:39:13 +00:00
|
|
|
|
|
|
|
if "PROGRAMFILES(X86)" in os.environ and os.path.exists(os.environ["PROGRAMFILES(X86)"]):
|
2015-06-02 13:35:14 +00:00
|
|
|
paths.add(os.path.join(os.environ["PROGRAMFILES(X86)"], "qemu"))
|
2015-02-20 13:39:13 +00:00
|
|
|
if "PROGRAMFILES" in os.environ and os.path.exists(os.environ["PROGRAMFILES"]):
|
2015-06-02 13:35:14 +00:00
|
|
|
paths.add(os.path.join(os.environ["PROGRAMFILES"], "qemu"))
|
2015-02-20 13:39:13 +00:00
|
|
|
elif sys.platform.startswith("darwin"):
|
|
|
|
if hasattr(sys, "frozen"):
|
2015-08-27 16:27:17 +00:00
|
|
|
# add specific locations on Mac OS X regardless of what's in $PATH
|
|
|
|
paths.update(["/usr/bin", "/usr/local/bin", "/opt/local/bin"])
|
2015-05-18 09:58:56 +00:00
|
|
|
try:
|
2015-07-05 19:53:47 +00:00
|
|
|
exec_dir = os.path.dirname(os.path.abspath(sys.executable))
|
|
|
|
paths.add(os.path.abspath(os.path.join(exec_dir, "../Resources/qemu/bin/")))
|
2015-05-18 09:58:56 +00:00
|
|
|
# If the user run the server by hand from outside
|
|
|
|
except FileNotFoundError:
|
2015-06-03 16:58:17 +00:00
|
|
|
paths.add("/Applications/GNS3.app/Contents/Resources/qemu/bin")
|
2015-05-10 17:46:51 +00:00
|
|
|
return paths
|
|
|
|
|
|
|
|
@staticmethod
|
2016-01-01 00:40:12 +00:00
|
|
|
def binary_list(archs=None):
|
2015-05-10 17:46:51 +00: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():
|
2015-02-20 13:39:13 +00:00
|
|
|
try:
|
|
|
|
for f in os.listdir(path):
|
2016-02-03 15:38:46 +00:00
|
|
|
if f.endswith("-spice"):
|
|
|
|
continue
|
2015-04-20 08:12:17 +00:00
|
|
|
if (f.startswith("qemu-system") or f.startswith("qemu-kvm") or f == "qemu" or f == "qemu.exe") and \
|
2015-02-20 13:39:13 +00:00
|
|
|
os.access(os.path.join(path, f), os.X_OK) and \
|
|
|
|
os.path.isfile(os.path.join(path, f)):
|
2016-01-01 00:40:12 +00:00
|
|
|
if archs is not None:
|
|
|
|
for arch in archs:
|
|
|
|
if f.endswith(arch) or f.endswith("{}.exe".format(arch)) or f.endswith("{}w.exe".format(arch)):
|
|
|
|
qemu_path = os.path.join(path, f)
|
|
|
|
version = yield from Qemu.get_qemu_version(qemu_path)
|
|
|
|
qemus.append({"path": qemu_path, "version": version})
|
|
|
|
else:
|
|
|
|
qemu_path = os.path.join(path, f)
|
|
|
|
version = yield from Qemu.get_qemu_version(qemu_path)
|
|
|
|
qemus.append({"path": qemu_path, "version": version})
|
|
|
|
|
2015-02-20 13:39:13 +00:00
|
|
|
except OSError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
return qemus
|
|
|
|
|
2015-05-10 17:46:51 +00:00
|
|
|
@staticmethod
|
|
|
|
def img_binary_list():
|
|
|
|
"""
|
|
|
|
Gets QEMU-img binaries list available on the host.
|
|
|
|
|
|
|
|
:returns: Array of dictionary {"path": Qemu-img binary path, "version": version of Qemu-img}
|
|
|
|
"""
|
|
|
|
qemu_imgs = []
|
|
|
|
for path in Qemu.paths_list():
|
|
|
|
try:
|
|
|
|
for f in os.listdir(path):
|
|
|
|
if (f == "qemu-img" or f == "qemu-img.exe") and \
|
|
|
|
os.access(os.path.join(path, f), os.X_OK) and \
|
|
|
|
os.path.isfile(os.path.join(path, f)):
|
|
|
|
qemu_path = os.path.join(path, f)
|
|
|
|
version = yield from Qemu._get_qemu_img_version(qemu_path)
|
|
|
|
qemu_imgs.append({"path": qemu_path, "version": version})
|
|
|
|
except OSError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
return qemu_imgs
|
|
|
|
|
2015-02-20 13:39:13 +00:00
|
|
|
@staticmethod
|
|
|
|
@asyncio.coroutine
|
2015-10-04 12:41:39 +00:00
|
|
|
def get_qemu_version(qemu_path):
|
2015-02-20 13:39:13 +00:00
|
|
|
"""
|
|
|
|
Gets the Qemu version.
|
2015-04-08 17:17:34 +00:00
|
|
|
|
|
|
|
:param qemu_path: path to Qemu executable.
|
2015-02-20 13:39:13 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
if sys.platform.startswith("win"):
|
2015-10-04 12:41:39 +00:00
|
|
|
# Qemu on Windows doesn't return anything with parameter -version
|
|
|
|
# look for a version number in version.txt file in the same directory instead
|
|
|
|
version_file = os.path.join(os.path.dirname(qemu_path), "version.txt")
|
|
|
|
if os.path.isfile(version_file):
|
|
|
|
try:
|
|
|
|
with open(version_file, "rb") as file:
|
|
|
|
version = file.read().decode("utf-8").strip()
|
|
|
|
match = re.search("[0-9\.]+", version)
|
|
|
|
if match:
|
|
|
|
return version
|
|
|
|
except (UnicodeDecodeError, OSError) as e:
|
|
|
|
log.warn("could not read {}: {}".format(version_file, e))
|
2015-02-20 13:39:13 +00:00
|
|
|
return ""
|
2015-10-04 12:41:39 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
output = yield from subprocess_check_output(qemu_path, "-version")
|
|
|
|
match = re.search("version\s+([0-9a-z\-\.]+)", output)
|
|
|
|
if match:
|
|
|
|
version = match.group(1)
|
|
|
|
return version
|
|
|
|
else:
|
|
|
|
raise QemuError("Could not determine the Qemu version for {}".format(qemu_path))
|
|
|
|
except subprocess.SubprocessError as e:
|
|
|
|
raise QemuError("Error while looking for the Qemu version: {}".format(e))
|
2015-02-26 01:55:35 +00:00
|
|
|
|
2015-05-10 17:46:51 +00:00
|
|
|
@staticmethod
|
|
|
|
@asyncio.coroutine
|
|
|
|
def _get_qemu_img_version(qemu_img_path):
|
|
|
|
"""
|
|
|
|
Gets the Qemu-img version.
|
|
|
|
|
|
|
|
:param qemu_img_path: path to Qemu-img executable.
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
output = yield from subprocess_check_output(qemu_img_path, "--version")
|
|
|
|
match = re.search("version\s+([0-9a-z\-\.]+)", output)
|
|
|
|
if match:
|
|
|
|
version = match.group(1)
|
|
|
|
return version
|
|
|
|
else:
|
|
|
|
raise QemuError("Could not determine the Qemu-img version for {}".format(qemu_img_path))
|
|
|
|
except subprocess.SubprocessError as e:
|
|
|
|
raise QemuError("Error while looking for the Qemu-img version: {}".format(e))
|
|
|
|
|
2015-02-26 01:55:35 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_legacy_vm_workdir(legacy_vm_id, name):
|
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Returns the name of the legacy working directory name for a node.
|
2015-02-26 01:55:35 +00:00
|
|
|
|
|
|
|
:param legacy_vm_id: legacy VM identifier (integer)
|
2016-05-11 17:35:36 +00:00
|
|
|
:param: node name (not used)
|
2015-02-26 01:55:35 +00:00
|
|
|
|
|
|
|
:returns: working directory name
|
|
|
|
"""
|
|
|
|
|
|
|
|
return os.path.join("qemu", "vm-{}".format(legacy_vm_id))
|
2015-04-14 16:46:55 +00:00
|
|
|
|
|
|
|
def get_images_directory(self):
|
|
|
|
"""
|
|
|
|
Return the full path of the images directory on disk
|
|
|
|
"""
|
|
|
|
return os.path.join(os.path.expanduser(self.config.get_section_config("Server").get("images_path", "~/GNS3/images")), "QEMU")
|
2015-07-27 14:19:15 +00:00
|
|
|
|
2015-07-27 17:18:36 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def create_disk(self, qemu_img, path, options):
|
|
|
|
"""
|
|
|
|
Create a qemu disk with qemu-img
|
|
|
|
|
|
|
|
:param qemu_img: qemu-img binary path
|
|
|
|
:param path: Image path
|
|
|
|
:param options: Disk image creation options
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
img_format = options.pop("format")
|
|
|
|
img_size = options.pop("size")
|
|
|
|
|
|
|
|
if not os.path.isabs(path):
|
|
|
|
directory = self.get_images_directory()
|
|
|
|
os.makedirs(directory, exist_ok=True)
|
|
|
|
path = os.path.join(directory, os.path.basename(path))
|
2015-07-28 14:15:01 +00:00
|
|
|
if os.path.exists(path):
|
|
|
|
raise QemuError("Could not create disk image {} already exist".format(path))
|
2015-07-27 17:18:36 +00:00
|
|
|
|
|
|
|
command = [qemu_img, "create", "-f", img_format]
|
|
|
|
for option in sorted(options.keys()):
|
|
|
|
command.extend(["-o", "{}={}".format(option, options[option])])
|
|
|
|
command.append(path)
|
|
|
|
command.append("{}M".format(img_size))
|
|
|
|
|
|
|
|
process = yield from asyncio.create_subprocess_exec(*command)
|
|
|
|
yield from process.wait()
|
|
|
|
except (OSError, subprocess.SubprocessError) as e:
|
2015-07-28 14:15:01 +00:00
|
|
|
raise QemuError("Could not create disk image {}:{}".format(path, e))
|