Commit 82187135 authored by René Nussbaumer's avatar René Nussbaumer Committed by Iustin Pop
Browse files

Normalize MAC addresses to all lower.

This change will normalize the MAC to all lower after validation.
Signed-off-by: default avatarRené Nussbaumer <>
Signed-off-by: default avatarIustin Pop <>
Reviewed-by: default avatarIustin Pop <>
parent 7699c3af
......@@ -5770,10 +5770,8 @@ class LUCreateInstance(LogicalUnit):
# MAC address verification
mac = nic.get("mac", constants.VALUE_AUTO)
if mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
if not utils.IsValidMac(mac.lower()):
raise errors.OpPrereqError("Invalid MAC address specified: %s" %
mac, errors.ECODE_INVAL)
mac = utils.NormalizeAndValidateMac(mac)
self.cfg.ReserveMAC(mac, self.proc.GetECId())
except errors.ReservationError:
......@@ -7536,9 +7534,8 @@ class LUSetInstanceParams(LogicalUnit):
if 'mac' in nic_dict:
nic_mac = nic_dict['mac']
if nic_mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
if not utils.IsValidMac(nic_mac):
raise errors.OpPrereqError("Invalid MAC address %s" % nic_mac,
nic_mac = utils.NormalizeAndValidateMac(nic_mac)
if nic_op != constants.DDM_ADD and nic_mac == constants.VALUE_AUTO:
raise errors.OpPrereqError("'auto' is not a valid MAC address when"
" modifying an existing nic",
......@@ -1398,20 +1398,26 @@ def UniqueSequence(seq):
return [i for i in seq if i not in seen and not seen.add(i)]
def IsValidMac(mac):
"""Predicate to check if a MAC address is valid.
def NormalizeAndValidateMac(mac):
"""Normalizes and check if a MAC address is valid.
Checks whether the supplied MAC address is formally correct, only
accepts colon separated format.
accepts colon separated format. Normalize it to all lower.
@type mac: str
@param mac: the MAC to be validated
@rtype: boolean
@return: True is the MAC seems valid
@rtype: str
@return: returns the normalized and validated MAC.
@raise errors.OpPrereqError: If the MAC isn't valid
mac_check = re.compile("^([0-9a-f]{2}(:|$)){6}$")
return mac_check.match(mac) is not None
mac_check = re.compile("^([0-9a-f]{2}(:|$)){6}$", re.I)
if not mac_check.match(mac):
raise errors.OpPrereqError("Invalid MAC address specified: %s" %
mac, errors.ECODE_INVAL)
return mac.lower()
def TestDelay(duration):
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment