Skip to content

Commit 51ecc79

Browse files
committed
net: option to force MAC address prefix
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 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) <matttbe@kernel.org>
1 parent 87e4a43 commit 51ecc79

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

virtme/commands/run.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ def make_parser() -> argparse.ArgumentParser:
123123
nargs="?",
124124
help="Enable basic network access: user, bridge(=<br>), loop.",
125125
)
126+
g.add_argument(
127+
"--mac-address-prefix",
128+
action="store",
129+
default=None,
130+
help="The MAC address, excluding the last octet, to assign to the NIC interface, e.g. 52:54:00:12:34",
131+
)
126132
g.add_argument(
127133
"--balloon",
128134
action="store_true",
@@ -1329,11 +1335,17 @@ def do_script(shellcmd: str, ret_path=None, show_boot_console=False) -> None:
13291335
if video_args:
13301336
qemuargs.extend(video_args)
13311337

1338+
def get_mac(index):
1339+
if args.mac_address_prefix is None:
1340+
return ""
1341+
return ",mac=%s:%02d" % (args.mac_address_prefix, index)
1342+
13321343
if args.net:
13331344
extend_dhcp = False
13341345
index = 0
13351346
for net in args.net:
1336-
qemuargs.extend(["-device", "%s,netdev=n%d" % (arch.virtio_dev_type("net"), index)])
1347+
qemuargs.extend(["-device", "%s,netdev=n%d%s" %
1348+
(arch.virtio_dev_type("net"), index, get_mac(index))])
13371349
if net == "user":
13381350
qemuargs.extend(["-netdev", "user,id=n%d" % index])
13391351
extend_dhcp = True
@@ -1348,7 +1360,8 @@ def do_script(shellcmd: str, ret_path=None, show_boot_console=False) -> None:
13481360
hubid = index
13491361
qemuargs.extend(["-netdev", "hubport,id=n%d,hubid=%d" % (index, hubid)])
13501362
index += 1
1351-
qemuargs.extend(["-device", "%s,netdev=n%d" % (arch.virtio_dev_type("net"), index)])
1363+
qemuargs.extend(["-device", "%s,netdev=n%d%s" %
1364+
(arch.virtio_dev_type("net"), index, get_mac(index))])
13521365
qemuargs.extend(["-netdev", "hubport,id=n%d,hubid=%d" % (index, hubid)])
13531366
else:
13541367
arg_fail("--net: invalid choice: '%s' (choose from user, bridge(=<br>), loop)" % net)

virtme_ng/run.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,13 @@ def make_parser():
352352
help="Enable network access: user, bridge(=<br>), loop",
353353
)
354354

355+
parser.add_argument(
356+
"--mac-address-prefix",
357+
"-M",
358+
action="store",
359+
help="The MAC address, excluding the last octet, to assign to the NIC interface, e.g. 52:54:00:12:34",
360+
)
361+
355362
parser.add_argument(
356363
"--disk",
357364
"-D",
@@ -934,6 +941,12 @@ def _get_virtme_network(self, args):
934941
else:
935942
self.virtme_param["network"] = ""
936943

944+
def _get_virtme_mac_address_prefix(self, args):
945+
if args.mac_address_prefix is not None:
946+
self.virtme_param["mac_address_prefix"] = "--mac-address-prefix " + args.mac_address_prefix
947+
else:
948+
self.virtme_param["mac_address_prefix"] = ""
949+
937950
def _get_virtme_disk(self, args):
938951
if args.disk is not None:
939952
disk_str = ""
@@ -1088,6 +1101,7 @@ def run(self, args):
10881101
self._get_virtme_no_virtme_ng_init(args)
10891102
self._get_virtme_mods(args)
10901103
self._get_virtme_network(args)
1104+
self._get_virtme_mac_address_prefix(args)
10911105
self._get_virtme_disk(args)
10921106
self._get_virtme_sound(args)
10931107
self._get_virtme_disable_microvm(args)
@@ -1126,6 +1140,7 @@ def run(self, args):
11261140
+ f'{self.virtme_param["no_virtme_ng_init"]} '
11271141
+ f'{self.virtme_param["mods"]} '
11281142
+ f'{self.virtme_param["network"]} '
1143+
+ f'{self.virtme_param["mac_address_prefix"]} '
11291144
+ f'{self.virtme_param["disk"]} '
11301145
+ f'{self.virtme_param["sound"]} '
11311146
+ f'{self.virtme_param["disable_microvm"]} '

0 commit comments

Comments
 (0)