mirror of
https://github.com/GNS3/gns3-server
synced 2025-01-27 00:11:07 +00:00
Support a platform parameter for Qemu
I'm sure we will discover a distribution with a different naming conventions... Fix #220
This commit is contained in:
parent
fb79543342
commit
2da0b36ee7
@ -51,9 +51,10 @@ class QEMUHandler:
|
|||||||
qemu = Qemu.instance()
|
qemu = Qemu.instance()
|
||||||
vm = yield from qemu.create_vm(request.json.pop("name"),
|
vm = yield from qemu.create_vm(request.json.pop("name"),
|
||||||
request.match_info["project_id"],
|
request.match_info["project_id"],
|
||||||
request.json.get("vm_id"),
|
request.json.pop("vm_id", None),
|
||||||
qemu_path=request.json.get("qemu_path"),
|
qemu_path=request.json.pop("qemu_path", None),
|
||||||
console=request.json.get("console"))
|
platform=request.json.pop("platform", None),
|
||||||
|
console=request.json.pop("console", None))
|
||||||
|
|
||||||
for name, value in request.json.items():
|
for name, value in request.json.items():
|
||||||
if hasattr(vm, name) and getattr(vm, name) != value:
|
if hasattr(vm, name) and getattr(vm, name) != value:
|
||||||
|
@ -22,6 +22,7 @@ order to run a QEMU VM.
|
|||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import shlex
|
import shlex
|
||||||
@ -35,13 +36,16 @@ from ..nios.nio_udp import NIOUDP
|
|||||||
from ..nios.nio_tap import NIOTAP
|
from ..nios.nio_tap import NIOTAP
|
||||||
from ..nios.nio_nat import NIONAT
|
from ..nios.nio_nat import NIONAT
|
||||||
from ..base_vm import BaseVM
|
from ..base_vm import BaseVM
|
||||||
from ...schemas.qemu import QEMU_OBJECT_SCHEMA
|
from ...schemas.qemu import QEMU_OBJECT_SCHEMA, QEMU_PLATFORMS
|
||||||
from ...utils.asyncio import monitor_process
|
from ...utils.asyncio import monitor_process
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class QemuVM(BaseVM):
|
class QemuVM(BaseVM):
|
||||||
module_name = 'qemu'
|
module_name = 'qemu'
|
||||||
|
|
||||||
@ -54,10 +58,11 @@ class QemuVM(BaseVM):
|
|||||||
:param manager: Manager instance
|
:param manager: Manager instance
|
||||||
:param console: TCP console port
|
:param console: TCP console port
|
||||||
:param qemu_path: path to the QEMU binary
|
:param qemu_path: path to the QEMU binary
|
||||||
|
:param platform: Platform to emulate
|
||||||
:param console: TCP console port
|
:param console: TCP console port
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, name, vm_id, project, manager, qemu_path=None, console=None):
|
def __init__(self, name, vm_id, project, manager, qemu_path=None, console=None, platform=None):
|
||||||
|
|
||||||
super().__init__(name, vm_id, project, manager, console=console)
|
super().__init__(name, vm_id, project, manager, console=console)
|
||||||
server_config = manager.config.get_section_config("Server")
|
server_config = manager.config.get_section_config("Server")
|
||||||
@ -70,7 +75,18 @@ class QemuVM(BaseVM):
|
|||||||
self._stdout_file = ""
|
self._stdout_file = ""
|
||||||
|
|
||||||
# QEMU VM settings
|
# QEMU VM settings
|
||||||
|
|
||||||
|
if qemu_path:
|
||||||
|
try:
|
||||||
self.qemu_path = qemu_path
|
self.qemu_path = qemu_path
|
||||||
|
except QemuError as e:
|
||||||
|
if platform:
|
||||||
|
self.platform = platform
|
||||||
|
else:
|
||||||
|
raise e
|
||||||
|
else:
|
||||||
|
self.platform = platform
|
||||||
|
|
||||||
self._hda_disk_image = ""
|
self._hda_disk_image = ""
|
||||||
self._hdb_disk_image = ""
|
self._hdb_disk_image = ""
|
||||||
self._hdc_disk_image = ""
|
self._hdc_disk_image = ""
|
||||||
@ -124,6 +140,20 @@ class QemuVM(BaseVM):
|
|||||||
if qemu_path and os.pathsep not in qemu_path:
|
if qemu_path and os.pathsep not in qemu_path:
|
||||||
qemu_path = shutil.which(qemu_path)
|
qemu_path = shutil.which(qemu_path)
|
||||||
|
|
||||||
|
self._check_qemu_path(qemu_path)
|
||||||
|
self._qemu_path = qemu_path
|
||||||
|
self._platform = os.path.basename(qemu_path)
|
||||||
|
if self._platform == "qemu-kvm":
|
||||||
|
self._platform = "x86_64"
|
||||||
|
else:
|
||||||
|
self._platform = re.sub(r'^qemu-system-(.*)(w.exe)?$', r'\1', os.path.basename(qemu_path), re.IGNORECASE)
|
||||||
|
if self._platform not in QEMU_PLATFORMS:
|
||||||
|
raise QemuError("Platform {} is unknown".format(self._platform))
|
||||||
|
log.info('QEMU VM "{name}" [{id}] has set the QEMU path to {qemu_path}'.format(name=self._name,
|
||||||
|
id=self._id,
|
||||||
|
qemu_path=qemu_path))
|
||||||
|
|
||||||
|
def _check_qemu_path(self, qemu_path):
|
||||||
if qemu_path is None:
|
if qemu_path is None:
|
||||||
raise QemuError("QEMU binary path is not set or not found in the path")
|
raise QemuError("QEMU binary path is not set or not found in the path")
|
||||||
if not os.path.exists(qemu_path):
|
if not os.path.exists(qemu_path):
|
||||||
@ -131,10 +161,20 @@ class QemuVM(BaseVM):
|
|||||||
if not os.access(qemu_path, os.X_OK):
|
if not os.access(qemu_path, os.X_OK):
|
||||||
raise QemuError("QEMU binary '{}' is not executable".format(qemu_path))
|
raise QemuError("QEMU binary '{}' is not executable".format(qemu_path))
|
||||||
|
|
||||||
self._qemu_path = qemu_path
|
@property
|
||||||
log.info('QEMU VM "{name}" [{id}] has set the QEMU path to {qemu_path}'.format(name=self._name,
|
def platform(self):
|
||||||
id=self._id,
|
"""
|
||||||
qemu_path=qemu_path))
|
Return the current platform
|
||||||
|
"""
|
||||||
|
return self._platform
|
||||||
|
|
||||||
|
@platform.setter
|
||||||
|
def platform(self, platform):
|
||||||
|
self._platform = platform
|
||||||
|
if sys.platform.startswith("win"):
|
||||||
|
self.qemu_path = "qemu-system-{}w.exe".format(platform)
|
||||||
|
else:
|
||||||
|
self.qemu_path = "qemu-system-{}".format(platform)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def hda_disk_image(self):
|
def hda_disk_image(self):
|
||||||
|
@ -15,6 +15,8 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
QEMU_PLATFORMS = ["aarch64", "alpha", "arm", "cris", "i386", "lm32", "m68k", "microblaze", "microblazeel", "mips", "mips64", "mips64el", "mipsel", "moxie", "or32", "ppc", "ppc64", "ppcemb", "s390x", "sh4", "sh4eb", "sparc", "sparc64", "tricore", "unicore32", "x86_64", "xtensa", "xtensaeb"]
|
||||||
|
|
||||||
|
|
||||||
QEMU_CREATE_SCHEMA = {
|
QEMU_CREATE_SCHEMA = {
|
||||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
@ -38,9 +40,13 @@ QEMU_CREATE_SCHEMA = {
|
|||||||
},
|
},
|
||||||
"qemu_path": {
|
"qemu_path": {
|
||||||
"description": "Path to QEMU",
|
"description": "Path to QEMU",
|
||||||
"type": "string",
|
"type": ["string", "null"],
|
||||||
"minLength": 1,
|
"minLength": 1,
|
||||||
},
|
},
|
||||||
|
"platform": {
|
||||||
|
"description": "Platform to emulate",
|
||||||
|
"enum": QEMU_PLATFORMS + ["null"]
|
||||||
|
},
|
||||||
"console": {
|
"console": {
|
||||||
"description": "console TCP port",
|
"description": "console TCP port",
|
||||||
"minimum": 1,
|
"minimum": 1,
|
||||||
@ -130,7 +136,7 @@ QEMU_CREATE_SCHEMA = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
"additionalProperties": False,
|
"additionalProperties": False,
|
||||||
"required": ["name", "qemu_path"],
|
"required": ["name"],
|
||||||
}
|
}
|
||||||
|
|
||||||
QEMU_UPDATE_SCHEMA = {
|
QEMU_UPDATE_SCHEMA = {
|
||||||
@ -148,6 +154,10 @@ QEMU_UPDATE_SCHEMA = {
|
|||||||
"type": ["string", "null"],
|
"type": ["string", "null"],
|
||||||
"minLength": 1,
|
"minLength": 1,
|
||||||
},
|
},
|
||||||
|
"platform": {
|
||||||
|
"description": "Platform to emulate",
|
||||||
|
"enum": QEMU_PLATFORMS + ["null"]
|
||||||
|
},
|
||||||
"console": {
|
"console": {
|
||||||
"description": "console TCP port",
|
"description": "console TCP port",
|
||||||
"minimum": 1,
|
"minimum": 1,
|
||||||
@ -264,6 +274,10 @@ QEMU_OBJECT_SCHEMA = {
|
|||||||
"type": "string",
|
"type": "string",
|
||||||
"minLength": 1,
|
"minLength": 1,
|
||||||
},
|
},
|
||||||
|
"platform": {
|
||||||
|
"description": "Platform to emulate",
|
||||||
|
"enum": QEMU_PLATFORMS
|
||||||
|
},
|
||||||
"hda_disk_image": {
|
"hda_disk_image": {
|
||||||
"description": "QEMU hda disk image path",
|
"description": "QEMU hda disk image path",
|
||||||
"type": "string",
|
"type": "string",
|
||||||
@ -352,7 +366,7 @@ QEMU_OBJECT_SCHEMA = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
"additionalProperties": False,
|
"additionalProperties": False,
|
||||||
"required": ["vm_id", "project_id", "name", "qemu_path", "hda_disk_image", "hdb_disk_image",
|
"required": ["vm_id", "project_id", "name", "qemu_path", "platform", "hda_disk_image", "hdb_disk_image",
|
||||||
"hdc_disk_image", "hdd_disk_image", "ram", "adapters", "adapter_type", "mac_address", "console",
|
"hdc_disk_image", "hdd_disk_image", "ram", "adapters", "adapter_type", "mac_address", "console",
|
||||||
"initrd", "kernel_image", "kernel_command_line", "legacy_networking", "acpi_shutdown", "kvm",
|
"initrd", "kernel_image", "kernel_command_line", "legacy_networking", "acpi_shutdown", "kvm",
|
||||||
"cpu_throttling", "process_priority", "options"]
|
"cpu_throttling", "process_priority", "options"]
|
||||||
|
@ -25,7 +25,7 @@ from unittest.mock import patch
|
|||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def fake_qemu_bin():
|
def fake_qemu_bin():
|
||||||
|
|
||||||
bin_path = os.path.join(os.environ["PATH"], "qemu_x42")
|
bin_path = os.path.join(os.environ["PATH"], "qemu_x86_64")
|
||||||
with open(bin_path, "w+") as f:
|
with open(bin_path, "w+") as f:
|
||||||
f.write("1")
|
f.write("1")
|
||||||
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
||||||
@ -51,7 +51,7 @@ def base_params(tmpdir, fake_qemu_bin):
|
|||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def fake_qemu_bin():
|
def fake_qemu_bin():
|
||||||
|
|
||||||
bin_path = os.path.join(os.environ["PATH"], "qemu_x42")
|
bin_path = os.path.join(os.environ["PATH"], "qemu-system-x86_64")
|
||||||
with open(bin_path, "w+") as f:
|
with open(bin_path, "w+") as f:
|
||||||
f.write("1")
|
f.write("1")
|
||||||
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
||||||
@ -65,12 +65,27 @@ def vm(server, project, base_params):
|
|||||||
return response.json
|
return response.json
|
||||||
|
|
||||||
|
|
||||||
def test_qemu_create(server, project, base_params):
|
def test_qemu_create(server, project, base_params, fake_qemu_bin):
|
||||||
response = server.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), base_params)
|
response = server.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), base_params)
|
||||||
assert response.status == 201
|
assert response.status == 201
|
||||||
assert response.route == "/projects/{project_id}/qemu/vms"
|
assert response.route == "/projects/{project_id}/qemu/vms"
|
||||||
assert response.json["name"] == "PC TEST 1"
|
assert response.json["name"] == "PC TEST 1"
|
||||||
assert response.json["project_id"] == project.id
|
assert response.json["project_id"] == project.id
|
||||||
|
assert response.json["qemu_path"] == fake_qemu_bin
|
||||||
|
assert response.json["platform"] == "x86_64"
|
||||||
|
|
||||||
|
|
||||||
|
def test_qemu_create_platform(server, project, base_params, fake_qemu_bin):
|
||||||
|
base_params["qemu_path"] = None
|
||||||
|
base_params["platform"] = "x86_64"
|
||||||
|
|
||||||
|
response = server.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), base_params)
|
||||||
|
assert response.status == 201
|
||||||
|
assert response.route == "/projects/{project_id}/qemu/vms"
|
||||||
|
assert response.json["name"] == "PC TEST 1"
|
||||||
|
assert response.json["project_id"] == project.id
|
||||||
|
assert response.json["qemu_path"] == fake_qemu_bin
|
||||||
|
assert response.json["platform"] == "x86_64"
|
||||||
|
|
||||||
|
|
||||||
def test_qemu_create_with_params(server, project, base_params):
|
def test_qemu_create_with_params(server, project, base_params):
|
||||||
|
@ -52,9 +52,9 @@ def fake_qemu_img_binary():
|
|||||||
def fake_qemu_binary():
|
def fake_qemu_binary():
|
||||||
|
|
||||||
if sys.platform.startswith("win"):
|
if sys.platform.startswith("win"):
|
||||||
bin_path = os.path.join(os.environ["PATH"], "qemu_x42.EXE")
|
bin_path = os.path.join(os.environ["PATH"], "qemu-system-x86_64.EXE")
|
||||||
else:
|
else:
|
||||||
bin_path = os.path.join(os.environ["PATH"], "qemu_x42")
|
bin_path = os.path.join(os.environ["PATH"], "qemu-system-x86_64")
|
||||||
with open(bin_path, "w+") as f:
|
with open(bin_path, "w+") as f:
|
||||||
f.write("1")
|
f.write("1")
|
||||||
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
||||||
@ -171,7 +171,9 @@ def test_set_qemu_path(vm, tmpdir, fake_qemu_binary):
|
|||||||
vm.qemu_path = None
|
vm.qemu_path = None
|
||||||
|
|
||||||
# Should not crash with unicode characters
|
# Should not crash with unicode characters
|
||||||
path = str(tmpdir / "bla\u62FF")
|
path = str(tmpdir / "\u62FF" / "qemu-system-mips")
|
||||||
|
|
||||||
|
os.makedirs( str(tmpdir / "\u62FF") )
|
||||||
|
|
||||||
# Raise because file doesn't exists
|
# Raise because file doesn't exists
|
||||||
with pytest.raises(QemuError):
|
with pytest.raises(QemuError):
|
||||||
@ -189,14 +191,45 @@ def test_set_qemu_path(vm, tmpdir, fake_qemu_binary):
|
|||||||
|
|
||||||
vm.qemu_path = path
|
vm.qemu_path = path
|
||||||
assert vm.qemu_path == path
|
assert vm.qemu_path == path
|
||||||
|
assert vm.platform == "mips"
|
||||||
|
|
||||||
|
|
||||||
def test_set_qemu_path_environ(vm, tmpdir, fake_qemu_binary):
|
def test_set_qemu_path_environ(vm, tmpdir, fake_qemu_binary):
|
||||||
|
|
||||||
# It should find the binary in the path
|
# It should find the binary in the path
|
||||||
vm.qemu_path = "qemu_x42"
|
vm.qemu_path = "qemu-system-x86_64"
|
||||||
|
|
||||||
assert vm.qemu_path == fake_qemu_binary
|
assert vm.qemu_path == fake_qemu_binary
|
||||||
|
assert vm.platform == "x86_64"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(sys.platform.startswith("linux") is False, reason="Supported only on linux")
|
||||||
|
def test_set_qemu_path_kvm_binary(vm, tmpdir, fake_qemu_binary):
|
||||||
|
|
||||||
|
bin_path = os.path.join(os.environ["PATH"], "qemu-kvm")
|
||||||
|
with open(bin_path, "w+") as f:
|
||||||
|
f.write("1")
|
||||||
|
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
||||||
|
return bin_path
|
||||||
|
|
||||||
|
# It should find the binary in the path
|
||||||
|
vm.qemu_path = "qemu-kvm"
|
||||||
|
|
||||||
|
assert vm.qemu_path == fake_qemu_binary
|
||||||
|
assert vm.platform == "x86_64"
|
||||||
|
|
||||||
|
|
||||||
|
def test_set_platform(project, manager):
|
||||||
|
|
||||||
|
with patch("shutil.which", return_value="/bin/qemu-system-x86_64") as which_mock:
|
||||||
|
with patch("gns3server.modules.qemu.QemuVM._check_qemu_path"):
|
||||||
|
vm = QemuVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager, platform="x86_64")
|
||||||
|
if sys.platform.startswith("win"):
|
||||||
|
which_mock.assert_called_with("qemu-system-x86_64w.exe")
|
||||||
|
else:
|
||||||
|
which_mock.assert_called_with("qemu-system-x86_64")
|
||||||
|
assert vm.platform == "x86_64"
|
||||||
|
assert vm.qemu_path == "/bin/qemu-system-x86_64"
|
||||||
|
|
||||||
|
|
||||||
def test_disk_options(vm, loop, fake_qemu_img_binary):
|
def test_disk_options(vm, loop, fake_qemu_img_binary):
|
||||||
|
Loading…
Reference in New Issue
Block a user