Skip to content

Commit

Permalink
net: option to force MAC address prefix
Browse files Browse the repository at this point in the history
By default, QEMU set the first NIC's MAC address to 52:54:00:12:34:56,
and increment the last octet for the next ones. When different VMs need
to discuss between each others via the same bridge, that can cause
conflicts.

A new option has been added to set a different prefix, e.g.

  --mac-address-prefix 52:54:00:12:34

Which will assign 52:54:00:12:34:00 to the fist NIC, and increment the
last octet for the next one, etc.

Signed-off-by: Matthieu Baerts (NGI0) <[email protected]>
  • Loading branch information
matttbe committed Nov 27, 2024
1 parent 87e4a43 commit ada419b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
17 changes: 15 additions & 2 deletions virtme/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ def make_parser() -> argparse.ArgumentParser:
nargs="?",
help="Enable basic network access: user, bridge(=<br>), loop.",
)
g.add_argument(
"--mac-address-prefix",
action="store",
default=None,
help="The MAC address, excluding the last octet, to assign to the NIC interface, e.g. 52:54:00:12:34",
)
g.add_argument(
"--balloon",
action="store_true",
Expand Down Expand Up @@ -1329,11 +1335,17 @@ def do_script(shellcmd: str, ret_path=None, show_boot_console=False) -> None:
if video_args:
qemuargs.extend(video_args)

def get_mac(index):
if args.mac_address_prefix is None:
return ""
return ",mac=%s:%02d" % (args.mac_address_prefix, index)

if args.net:
extend_dhcp = False
index = 0
for net in args.net:
qemuargs.extend(["-device", "%s,netdev=n%d" % (arch.virtio_dev_type("net"), index)])
qemuargs.extend(["-device", "%s,netdev=n%d%s" %
(arch.virtio_dev_type("net"), index, get_mac(index))])
if net == "user":
qemuargs.extend(["-netdev", "user,id=n%d" % index])
extend_dhcp = True
Expand All @@ -1348,7 +1360,8 @@ def do_script(shellcmd: str, ret_path=None, show_boot_console=False) -> None:
hubid = index
qemuargs.extend(["-netdev", "hubport,id=n%d,hubid=%d" % (index, hubid)])
index += 1
qemuargs.extend(["-device", "%s,netdev=n%d" % (arch.virtio_dev_type("net"), index)])
qemuargs.extend(["-device", "%s,netdev=n%d%s" %
(arch.virtio_dev_type("net"), index, get_mac(index))])
qemuargs.extend(["-netdev", "hubport,id=n%d,hubid=%d" % (index, hubid)])
else:
arg_fail("--net: invalid choice: '%s' (choose from user, bridge(=<br>), loop)" % net)
Expand Down
15 changes: 15 additions & 0 deletions virtme_ng/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,13 @@ def make_parser():
help="Enable network access: user, bridge(=<br>), loop",
)

parser.add_argument(
"--mac-address-prefix",
"-M",
action="store",
help="The MAC address, excluding the last octet, to assign to the NIC interface, e.g. 52:54:00:12:34",
)

parser.add_argument(
"--disk",
"-D",
Expand Down Expand Up @@ -934,6 +941,12 @@ def _get_virtme_network(self, args):
else:
self.virtme_param["network"] = ""

def _get_virtme_mac_address_prefix(self, args):
if args.mac_address_prefix is not None:
self.virtme_param["mac_address_prefix"] = "--mac-address-prefix " + args.mac_address_prefix
else:
self.virtme_param["mac_address_prefix"] = ""

def _get_virtme_disk(self, args):
if args.disk is not None:
disk_str = ""
Expand Down Expand Up @@ -1088,6 +1101,7 @@ def run(self, args):
self._get_virtme_no_virtme_ng_init(args)
self._get_virtme_mods(args)
self._get_virtme_network(args)
self._get_virtme_mac_address_prefix(args)
self._get_virtme_disk(args)
self._get_virtme_sound(args)
self._get_virtme_disable_microvm(args)
Expand Down Expand Up @@ -1126,6 +1140,7 @@ def run(self, args):
+ f'{self.virtme_param["no_virtme_ng_init"]} '
+ f'{self.virtme_param["mods"]} '
+ f'{self.virtme_param["network"]} '
+ f'{self.virtme_param["mac_address_prefix"]} '
+ f'{self.virtme_param["disk"]} '
+ f'{self.virtme_param["sound"]} '
+ f'{self.virtme_param["disable_microvm"]} '
Expand Down

0 comments on commit ada419b

Please sign in to comment.