Turn off Qemu graphics if no display is available

Fixes #66
This commit is contained in:
Julien Duponchelle 2015-02-23 20:21:00 +01:00
parent cad708f4ab
commit 4d1f08c96e
2 changed files with 49 additions and 1 deletions

View File

@ -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):

View File

@ -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