Revision 24ea7a54

b/lib/cmdlib.py
9877 9877
    # creation job will fail.
9878 9878
    for nic in self.nics:
9879 9879
      if nic.mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
9880
        nic.mac = self.cfg.GenerateMAC(self.proc.GetECId())
9880
        nic.mac = self.cfg.GenerateMAC(nic.network, self.proc.GetECId())
9881 9881

  
9882 9882
    #### allocator run
9883 9883

  
......
12412 12412
      elif mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
12413 12413
        # otherwise generate the MAC address
12414 12414
        params[constants.INIC_MAC] = \
12415
          self.cfg.GenerateMAC(self.proc.GetECId())
12415
          self.cfg.GenerateMAC(new_net, self.proc.GetECId())
12416 12416
      else:
12417 12417
        # or validate/reserve the current one
12418 12418
        try:
......
12433 12433
      old_prefix = get_net_prefix(old_net)
12434 12434
      if old_prefix != new_prefix:
12435 12435
        params[constants.INIC_MAC] = \
12436
          self.cfg.GenerateMAC(self.proc.GetECId())
12436
          self.cfg.GenerateMAC(new_net, self.proc.GetECId())
12437 12437

  
12438 12438
    #if there is a change in nic-network configuration
12439 12439
    new_ip = params.get(constants.INIC_IP, old_ip)
......
15507 15507
      raise errors.OpPrereqError("Network '%s' already defined" %
15508 15508
                                 self.op.network, errors.ECODE_EXISTS)
15509 15509

  
15510
    if self.op.mac_prefix:
15511
      utils.NormalizeAndValidateMac(self.op.mac_prefix+":00:00:00")
15510 15512

  
15511 15513
  def BuildHooksEnv(self):
15512 15514
    """Build hooks env.
......
15695 15697
      if self.op.mac_prefix == constants.VALUE_NONE:
15696 15698
        self.mac_prefix = None
15697 15699
      else:
15700
        utils.NormalizeAndValidateMac(self.op.mac_prefix+":00:00:00")
15698 15701
        self.mac_prefix = self.op.mac_prefix
15699 15702

  
15700 15703
    if self.op.gateway6:
b/lib/config.py
39 39
import logging
40 40
import time
41 41
import itertools
42
from functools import wraps
42 43

  
43 44
from ganeti import errors
44 45
from ganeti import locking
......
218 219
    """
219 220
    return os.path.exists(constants.CLUSTER_CONF_FILE)
220 221

  
221
  def _GenerateOneMAC(self):
222
  def _GenerateMACPrefix(self, net=None):
223
    def _get_mac_prefix(view_func):
224
      def _decorator(*args, **kwargs):
225
        prefix = self._config_data.cluster.mac_prefix
226
        if net:
227
          net_uuid = self._UnlockedLookupNetwork(net)
228
          if net_uuid:
229
            nobj = self._UnlockedGetNetwork(net_uuid)
230
            if nobj.mac_prefix:
231
              prefix = nobj.mac_prefix
232
        suffix = view_func(*args, **kwargs)
233
        return prefix+':'+suffix
234
      return wraps(view_func)(_decorator)
235
    return _get_mac_prefix
236

  
237
  def _GenerateMACSuffix(self):
222 238
    """Generate one mac address
223 239

  
224 240
    """
225
    prefix = self._config_data.cluster.mac_prefix
226 241
    byte1 = random.randrange(0, 256)
227 242
    byte2 = random.randrange(0, 256)
228 243
    byte3 = random.randrange(0, 256)
229
    mac = "%s:%02x:%02x:%02x" % (prefix, byte1, byte2, byte3)
230
    return mac
244
    suffix = "%02x:%02x:%02x" % (byte1, byte2, byte3)
245
    return suffix
231 246

  
232 247
  @locking.ssynchronized(_config_lock, shared=1)
233 248
  def GetNdParams(self, node):
......
276 291
    return self._config_data.cluster.SimpleFillDP(group.diskparams)
277 292

  
278 293
  @locking.ssynchronized(_config_lock, shared=1)
279
  def GenerateMAC(self, ec_id):
294
  def GenerateMAC(self, net, ec_id):
280 295
    """Generate a MAC for an instance.
281 296

  
282 297
    This should check the current instances for duplicates.
283 298

  
284 299
    """
285 300
    existing = self._AllMACs()
286
    return self._temporary_ids.Generate(existing, self._GenerateOneMAC, ec_id)
301
    gen_mac = self._GenerateMACPrefix(net)(self._GenerateMACSuffix)
302
    return self._temporary_ids.Generate(existing, gen_mac, ec_id)
287 303

  
288 304
  @locking.ssynchronized(_config_lock, shared=1)
289 305
  def ReserveMAC(self, mac, ec_id):

Also available in: Unified diff