From 37ddff9f07ad999fa3b756ea3787f25258917947 Mon Sep 17 00:00:00 2001 From: grossmj Date: Wed, 3 Jun 2015 14:52:49 -0600 Subject: [PATCH] Support for base MAC address for Qemu VMs. --- gns3server/modules/qemu/qemu_vm.py | 29 +++++++++++++++++++++++++++-- gns3server/schemas/qemu.py | 20 +++++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/gns3server/modules/qemu/qemu_vm.py b/gns3server/modules/qemu/qemu_vm.py index d7d2bf12..7d0a164d 100644 --- a/gns3server/modules/qemu/qemu_vm.py +++ b/gns3server/modules/qemu/qemu_vm.py @@ -75,6 +75,7 @@ class QemuVM(BaseVM): self._hdb_disk_image = "" self._hdc_disk_image = "" self._hdd_disk_image = "" + self._mac_address = "00:00:ab:%s:%s:00" % (self.id[-4:-2], self.id[-2:]) self._options = "" self._ram = 256 self._ethernet_adapters = [] @@ -276,6 +277,31 @@ class QemuVM(BaseVM): id=self._id, adapter_type=adapter_type)) + @property + def mac_address(self): + """ + Returns the MAC address for this QEMU VM. + + :returns: adapter type (string) + """ + + return self._mac_address + + @mac_address.setter + def mac_address(self, mac_address): + """ + Sets the MAC address for this QEMU VM. + + :param mac_address: MAC address + """ + + self._mac_address = mac_address + + log.info('QEMU VM "{name}" [{id}]: MAC address changed to {mac_addr}'.format(name=self._name, + id=self._id, + mac_addr=mac_address)) + + @property def legacy_networking(self): """ @@ -1033,8 +1059,7 @@ class QemuVM(BaseVM): network_options = [] network_options.extend(["-net", "none"]) # we do not want any user networking back-end if no adapter is connected. for adapter_number, adapter in enumerate(self._ethernet_adapters): - # TODO: let users specify a base mac address - mac = "00:00:ab:%s:%s:%02x" % (self.id[-4:-2], self.id[-2:], adapter_number) + mac = "%s%02x" % (self._mac_address[:-2], (int(self._mac_address[-2:]) + adapter_number) % 255) nio = adapter.get_nio(0) if self._legacy_networking: # legacy QEMU networking syntax (-net) diff --git a/gns3server/schemas/qemu.py b/gns3server/schemas/qemu.py index 7f597ca6..f4f97685 100644 --- a/gns3server/schemas/qemu.py +++ b/gns3server/schemas/qemu.py @@ -78,6 +78,12 @@ QEMU_CREATE_SCHEMA = { "type": ["string", "null"], "minLength": 1, }, + "mac_address": { + "description": "QEMU MAC address", + "type": ["string", "null"], + "minLength": 1, + "pattern": "^([0-9a-fA-F]{2}[:]){5}([0-9a-fA-F]{2})$" + }, "initrd": { "description": "QEMU initrd path", "type": ["string", "null"], @@ -175,6 +181,12 @@ QEMU_UPDATE_SCHEMA = { "type": ["string", "null"], "minLength": 1, }, + "mac_address": { + "description": "QEMU MAC address", + "type": ["string", "null"], + "minLength": 1, + "pattern": "^([0-9a-fA-F]{2}[:]){5}([0-9a-fA-F]{2})$" + }, "initrd": { "description": "QEMU initrd path", "type": ["string", "null"], @@ -275,6 +287,12 @@ QEMU_OBJECT_SCHEMA = { "type": "string", "minLength": 1, }, + "mac_address": { + "description": "QEMU MAC address", + "type": "string", + "minLength": 1, + "pattern": "^([0-9a-fA-F]{2}[:]){5}([0-9a-fA-F]{2})$" + }, "console": { "description": "console TCP port", "minimum": 1, @@ -323,7 +341,7 @@ QEMU_OBJECT_SCHEMA = { }, "additionalProperties": False, "required": ["vm_id", "project_id", "name", "qemu_path", "hda_disk_image", "hdb_disk_image", - "hdc_disk_image", "hdd_disk_image", "ram", "adapters", "adapter_type", "console", + "hdc_disk_image", "hdd_disk_image", "ram", "adapters", "adapter_type", "mac_address", "console", "initrd", "kernel_image", "kernel_command_line", "legacy_networking", "acpi_shutdown", "cpu_throttling", "process_priority", "options"] }