Skip to content

Commit

Permalink
net: option to force net MAC address
Browse files Browse the repository at this point in the history
By default, QEMU sets the first NIC's MAC address to 52:54:00:12:34:56,
and increments 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 MAC address, e.g.

  --net-mac-address 52:54:00:12:34:56

Which will assign 52:54:00:12:34:56 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 151e559
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
26 changes: 24 additions & 2 deletions virtme/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ def make_parser() -> argparse.ArgumentParser:
nargs="?",
help="Enable basic network access: user, bridge(=<br>), loop.",
)
g.add_argument(
"--net-mac-address",
action="store",
default=None,
help="The MAC address to assign to the NIC interface, e.g. 52:54:00:12:34:56. "
+ "The last octet will be incremented for the next network devices.",
)
g.add_argument(
"--balloon",
action="store_true",
Expand Down Expand Up @@ -1329,11 +1336,25 @@ def do_script(shellcmd: str, ret_path=None, show_boot_console=False) -> None:
if video_args:
qemuargs.extend(video_args)

def get_net_mac(index):
if args.net_mac_address is None:
return ""

mac = args.net_mac_address.split(':')
try:
if len(mac) != 6:
assert(ValueError)
mac[5] = "%02x" % ((int(mac[5], 16) + index) % 256)
except:
arg_fail("--net-mac-address: invalid MAC address: '%s'" % args.net_mac_address)
return ",mac=" + ":".join(mac)

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_net_mac(index))])
if net == "user":
qemuargs.extend(["-netdev", "user,id=n%d" % index])
extend_dhcp = True
Expand All @@ -1348,7 +1369,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_net_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
16 changes: 16 additions & 0 deletions virtme_ng/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,14 @@ def make_parser():
help="Enable network access: user, bridge(=<br>), loop",
)

parser.add_argument(
"--net-mac-address",
"-N",
action="store",
help="The MAC address to assign to the NIC interface, e.g. 52:54:00:12:34:56. "
+ "The last octet will be incremented for the next network devices.",
)

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

def _get_virtme_net_mac_address(self, args):
if args.net_mac_address is not None:
self.virtme_param["net_mac_address"] = "--net-mac-address " + args.net_mac_address
else:
self.virtme_param["net_mac_address"] = ""

def _get_virtme_disk(self, args):
if args.disk is not None:
disk_str = ""
Expand Down Expand Up @@ -1088,6 +1102,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_net_mac_address(args)
self._get_virtme_disk(args)
self._get_virtme_sound(args)
self._get_virtme_disable_microvm(args)
Expand Down Expand Up @@ -1126,6 +1141,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["net_mac_address"]} '
+ f'{self.virtme_param["disk"]} '
+ f'{self.virtme_param["sound"]} '
+ f'{self.virtme_param["disable_microvm"]} '
Expand Down

0 comments on commit 151e559

Please sign in to comment.