From 4d1f08c96e9e7d8b30519fa449c470ff827a7b4d Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Mon, 23 Feb 2015 20:21:00 +0100 Subject: [PATCH] Turn off Qemu graphics if no display is available Fixes #66 --- gns3server/modules/qemu/qemu_vm.py | 17 ++++++++++++++- tests/modules/qemu/test_qemu_vm.py | 33 ++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/gns3server/modules/qemu/qemu_vm.py b/gns3server/modules/qemu/qemu_vm.py index eb4ab118..829bbfe7 100644 --- a/gns3server/modules/qemu/qemu_vm.py +++ b/gns3server/modules/qemu/qemu_vm.py @@ -915,13 +915,17 @@ class QemuVM(BaseVM): return options + def _get_random_mac(self, adapter_id): + # TODO: let users specify a base mac address + return "00:00:ab:%02x:%02x:%02d" % (random.randint(0x00, 0xff), random.randint(0x00, 0xff), adapter_id) + def _network_options(self): network_options = [] adapter_id = 0 for adapter in self._ethernet_adapters: # TODO: let users specify a base mac address - mac = "00:00:ab:%02x:%02x:%02d" % (random.randint(0x00, 0xff), random.randint(0x00, 0xff), adapter_id) + mac = self._get_random_mac(adapter_id) network_options.extend(["-net", "nic,vlan={},macaddr={},model={}".format(adapter_id, mac, self._adapter_type)]) nio = adapter.get_nio(0) if nio and isinstance(nio, NIO_UDP): @@ -944,6 +948,16 @@ class QemuVM(BaseVM): return network_options + def _graphic(self): + """ + Add the correct graphic options depending of the OS + """ + if sys.platform.startswith("win"): + return [] + if len(os.environ.get("DISPLAY", "")) > 0: + return [] + return ["-nographic"] + @asyncio.coroutine def _build_command(self): """ @@ -963,6 +977,7 @@ class QemuVM(BaseVM): if additional_options: command.extend(shlex.split(additional_options)) command.extend(self._network_options()) + command.extend(self._graphic()) return command def __json__(self): diff --git a/tests/modules/qemu/test_qemu_vm.py b/tests/modules/qemu/test_qemu_vm.py index a174f7fd..fe0f8cd4 100644 --- a/tests/modules/qemu/test_qemu_vm.py +++ b/tests/modules/qemu/test_qemu_vm.py @@ -233,3 +233,36 @@ def test_control_vm_expect_text(vm, loop): assert writer.write.called_with("test") assert res == "epic product" + + +def test_build_command(vm, loop, fake_qemu_binary): + + os.environ["DISPLAY"] = "0:0" + with patch("gns3server.modules.qemu.qemu_vm.QemuVM._get_random_mac", return_value="00:00:ab:7e:b5:00"): + with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: + cmd = loop.run_until_complete(asyncio.async(vm._build_command())) + assert cmd == [ + fake_qemu_binary, + "-name", + "test", + "-m", + "256", + "-hda", + os.path.join(vm.working_dir, "flash.qcow2"), + "-serial", + "telnet:0.0.0.0:{},server,nowait".format(vm.console), + "-monitor", + "telnet:0.0.0.0:{},server,nowait".format(vm.monitor), + "-net", + "nic,vlan=0,macaddr=00:00:ab:7e:b5:00,model=e1000", + "-net", + "user,vlan=0,name=gns3-0" + ] + + +def test_build_command_without_display(vm, loop, fake_qemu_binary): + + os.environ["DISPLAY"] = "" + with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process: + cmd = loop.run_until_complete(asyncio.async(vm._build_command())) + assert "-nographic" in cmd