Revision 82187135

b/lib/cmdlib.py
5770 5770
      # MAC address verification
5771 5771
      mac = nic.get("mac", constants.VALUE_AUTO)
5772 5772
      if mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
5773
        if not utils.IsValidMac(mac.lower()):
5774
          raise errors.OpPrereqError("Invalid MAC address specified: %s" %
5775
                                     mac, errors.ECODE_INVAL)
5776
        else:
5777
          try:
5778
            self.cfg.ReserveMAC(mac, self.proc.GetECId())
5779
          except errors.ReservationError:
5780
            raise errors.OpPrereqError("MAC address %s already in use"
5781
                                       " in cluster" % mac,
5782
                                       errors.ECODE_NOTUNIQUE)
5773
        mac = utils.NormalizeAndValidateMac(mac)
5774

  
5775
        try:
5776
          self.cfg.ReserveMAC(mac, self.proc.GetECId())
5777
        except errors.ReservationError:
5778
          raise errors.OpPrereqError("MAC address %s already in use"
5779
                                     " in cluster" % mac,
5780
                                     errors.ECODE_NOTUNIQUE)
5783 5781

  
5784 5782
      # bridge verification
5785 5783
      bridge = nic.get("bridge", None)
......
7536 7534
      if 'mac' in nic_dict:
7537 7535
        nic_mac = nic_dict['mac']
7538 7536
        if nic_mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
7539
          if not utils.IsValidMac(nic_mac):
7540
            raise errors.OpPrereqError("Invalid MAC address %s" % nic_mac,
7541
                                       errors.ECODE_INVAL)
7537
          nic_mac = utils.NormalizeAndValidateMac(nic_mac)
7538

  
7542 7539
        if nic_op != constants.DDM_ADD and nic_mac == constants.VALUE_AUTO:
7543 7540
          raise errors.OpPrereqError("'auto' is not a valid MAC address when"
7544 7541
                                     " modifying an existing nic",
b/lib/utils.py
1398 1398
  return [i for i in seq if i not in seen and not seen.add(i)]
1399 1399

  
1400 1400

  
1401
def IsValidMac(mac):
1402
  """Predicate to check if a MAC address is valid.
1401
def NormalizeAndValidateMac(mac):
1402
  """Normalizes and check if a MAC address is valid.
1403 1403

  
1404 1404
  Checks whether the supplied MAC address is formally correct, only
1405
  accepts colon separated format.
1405
  accepts colon separated format. Normalize it to all lower.
1406 1406

  
1407 1407
  @type mac: str
1408 1408
  @param mac: the MAC to be validated
1409
  @rtype: boolean
1410
  @return: True is the MAC seems valid
1409
  @rtype: str
1410
  @return: returns the normalized and validated MAC.
1411

  
1412
  @raise errors.OpPrereqError: If the MAC isn't valid
1411 1413

  
1412 1414
  """
1413
  mac_check = re.compile("^([0-9a-f]{2}(:|$)){6}$")
1414
  return mac_check.match(mac) is not None
1415
  mac_check = re.compile("^([0-9a-f]{2}(:|$)){6}$", re.I)
1416
  if not mac_check.match(mac):
1417
    raise errors.OpPrereqError("Invalid MAC address specified: %s" %
1418
                               mac, errors.ECODE_INVAL)
1419

  
1420
  return mac.lower()
1415 1421

  
1416 1422

  
1417 1423
def TestDelay(duration):

Also available in: Unified diff