Skip to content

Commit

Permalink
vsphere esx: fix forcing mac
Browse files Browse the repository at this point in the history
  • Loading branch information
karmab committed Aug 9, 2024
1 parent 3341178 commit a0dd58f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
39 changes: 23 additions & 16 deletions kvirt/providers/vsphere/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,17 +394,13 @@ def create(self, name, virttype=None, profile='kvirt', flavor=None, plan='kvirt'
# NICSPEC
if not self.networks:
self.set_networks()
macs = {}
for index, net in enumerate(nets):
netname = net['name'] if isinstance(net, dict) else net
mac = net['mac'] if isinstance(net, dict) and 'mac' in net else None
if isinstance(net, dict) and 'mac' in net:
macs[index] = net['mac']
if netname == 'default':
if image is not None:
if mac is not None and len(currentnics) >= index:
currentnic = currentnics[index]
currentnic.macAddress = mac
currentnic.addressType = vim.vm.device.VirtualEthernetCardOption.MacTypes.manual
nicspec = vim.vm.device.VirtualDeviceSpec(device=currentnic, operation="edit")
devconfspec.append(nicspec)
continue
else:
netname = 'VM Network'
Expand All @@ -420,11 +416,9 @@ def create(self, name, virttype=None, profile='kvirt', flavor=None, plan='kvirt'
if self.portgs[dvsnet][0] == currentswitchuuid and\
self.portgs[dvsnet][1] == currentportgroupkey:
currentnetwork = dvsnet
modified = False
if currentnetwork is None:
warning(f"Couldn't figure out network associated to nic {index}")
elif currentnetwork != netname:
modified = True
if netname in self.portgs:
switchuuid = self.portgs[netname][0]
portgroupkey = self.portgs[netname][1]
Expand All @@ -435,11 +429,6 @@ def create(self, name, virttype=None, profile='kvirt', flavor=None, plan='kvirt'
currentnic.backing.deviceName = netname
else:
return {'result': 'failure', 'reason': f"Invalid network {netname}"}
if mac is not None:
modified = True
currentnic.addressType = vim.vm.device.VirtualEthernetCardOption.MacTypes.manual
currentnic.macAddress = mac
if modified:
nicspec = vim.vm.device.VirtualDeviceSpec(device=currentnic, operation="edit")
devconfspec.append(nicspec)
continue
Expand All @@ -448,9 +437,9 @@ def create(self, name, virttype=None, profile='kvirt', flavor=None, plan='kvirt'
if netname in self.portgs:
switchuuid = self.portgs[netname][0]
portgroupkey = self.portgs[netname][1]
nicspec = createdvsnicspec(nicname, netname, switchuuid, portgroupkey, nictype=nictype, mac=mac)
nicspec = createdvsnicspec(nicname, netname, switchuuid, portgroupkey, nictype=nictype)
elif netname in self.networks:
nicspec = createnicspec(nicname, netname, nictype=nictype, mac=mac)
nicspec = createnicspec(nicname, netname, nictype=nictype)
else:
return {'result': 'failure', 'reason': f"Invalid network {netname}"}
devconfspec.append(nicspec)
Expand Down Expand Up @@ -479,6 +468,8 @@ def create(self, name, virttype=None, profile='kvirt', flavor=None, plan='kvirt'
confspec.nestedHVEnabled = True
t = vm.Reconfigure(confspec)
waitForMe(t)
if macs:
self.set_macs(name, macs)
if 'vmgroup' in overrides:
vmgroup = overrides['vmgroup']
vmgroups = {}
Expand Down Expand Up @@ -1759,3 +1750,19 @@ def update_subnet(self, name, overrides={}):
def list_dns_zones(self):
print("not implemented")
return []

def set_macs(self, name, macs):
vm = find(self.si, self.dc.vmFolder, vim.VirtualMachine, name)
currentdevices = vm.config.hardware.device
currentnics = [d for d in currentdevices if isinstance(d, vim.vm.device.VirtualEthernetCard)]
confspec = vim.vm.ConfigSpec()
devconfspec = []
for index in macs:
currentnic = currentnics[index]
currentnic.macAddress = macs[index]
currentnic.addressType = vim.vm.device.VirtualEthernetCardOption.MacTypes.manual
nicspec = vim.vm.device.VirtualDeviceSpec(device=currentnic, operation="edit")
devconfspec.append(nicspec)
confspec.deviceChange = devconfspec
t = vm.Reconfigure(confspec)
waitForMe(t)
11 changes: 2 additions & 9 deletions kvirt/providers/vsphere/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def makecuspec(name, nets=[], gateway=None, dns=None, domain=None):
return customspec


def createnicspec(nicname, netname, nictype=None, mac=None):
def createnicspec(nicname, netname, nictype=None):
nicspec = vim.vm.device.VirtualDeviceSpec()
nicspec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
if nictype == 'pcnet32':
Expand All @@ -176,17 +176,14 @@ def createnicspec(nicname, netname, nictype=None, mac=None):
desc.summary = netname
nicbacking.deviceName = netname
nic.backing = nicbacking
if mac is not None:
nic.addressType = vim.vm.device.VirtualEthernetCardOption.MacTypes.manual
nic.macAddress = mac
# nic.key = 0
nic.deviceInfo = desc
nic.addressType = 'generated'
nicspec.device = nic
return nicspec


def createdvsnicspec(nicname, netname, switchuuid, portgroupkey, nictype=None, mac=None):
def createdvsnicspec(nicname, netname, switchuuid, portgroupkey, nictype=None):
nicspec = vim.vm.device.VirtualDeviceSpec()
nicspec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
if nictype == 'pcnet32':
Expand All @@ -203,10 +200,6 @@ def createdvsnicspec(nicname, netname, switchuuid, portgroupkey, nictype=None, m
dvconnection.portgroupKey = portgroupkey
dnicbacking.port = dvconnection
nic.backing = dnicbacking
if mac is not None:
nic.addressType = vim.vm.device.VirtualEthernetCardOption.MacTypes.manual
nic.madAddress = mac
nicspec.device = nic
return nicspec


Expand Down

0 comments on commit a0dd58f

Please sign in to comment.