Revision c0fc01e9

b/lib/cmdlib.py
10062 10062
    # creation job will fail.
10063 10063
    for nic in self.nics:
10064 10064
      if nic.mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
10065
        nic.mac = self.cfg.GenerateMAC(self.proc.GetECId())
10065
        nic.mac = self.cfg.GenerateMAC(nic.network, self.proc.GetECId())
10066 10066

  
10067 10067
    #### allocator run
10068 10068

  
......
12765 12765
      elif mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
12766 12766
        # otherwise generate the MAC address
12767 12767
        params[constants.INIC_MAC] = \
12768
          self.cfg.GenerateMAC(self.proc.GetECId())
12768
          self.cfg.GenerateMAC(new_net, self.proc.GetECId())
12769 12769
      else:
12770 12770
        # or validate/reserve the current one
12771 12771
        try:
......
12788 12788
      old_prefix = get_net_prefix(old_net)
12789 12789
      if old_prefix != new_prefix:
12790 12790
        params[constants.INIC_MAC] = \
12791
          self.cfg.GenerateMAC(self.proc.GetECId())
12791
          self.cfg.GenerateMAC(new_net, self.proc.GetECId())
12792 12792

  
12793 12793
    #if there is a change in nic-network configuration
12794 12794
    new_ip = params.get(constants.INIC_IP, old_ip)
......
15362 15362
      raise errors.OpPrereqError("Network '%s' already defined" %
15363 15363
                                 self.op.network, errors.ECODE_EXISTS)
15364 15364

  
15365
    if self.op.mac_prefix:
15366
      utils.NormalizeAndValidateMac(self.op.mac_prefix + ":00:00:00")
15367

  
15365 15368
  def BuildHooksEnv(self):
15366 15369
    """Build hooks env.
15367 15370

  
......
15548 15551
      if self.op.mac_prefix == constants.VALUE_NONE:
15549 15552
        self.mac_prefix = None
15550 15553
      else:
15554
        utils.NormalizeAndValidateMac(self.op.mac_prefix+":00:00:00")
15551 15555
        self.mac_prefix = self.op.mac_prefix
15552 15556

  
15553 15557
    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
......
223 224
    """
224 225
    return os.path.exists(pathutils.CLUSTER_CONF_FILE)
225 226

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

  
242
  def _GenerateMACSuffix(self):
227 243
    """Generate one mac address
228 244

  
229 245
    """
230
    prefix = self._config_data.cluster.mac_prefix
231 246
    byte1 = random.randrange(0, 256)
232 247
    byte2 = random.randrange(0, 256)
233 248
    byte3 = random.randrange(0, 256)
234
    mac = "%s:%02x:%02x:%02x" % (prefix, byte1, byte2, byte3)
235
    return mac
249
    suffix = "%02x:%02x:%02x" % (byte1, byte2, byte3)
250
    return suffix
236 251

  
237 252
  @locking.ssynchronized(_config_lock, shared=1)
238 253
  def GetNdParams(self, node):
......
281 296
    return self._config_data.cluster.SimpleFillDP(group.diskparams)
282 297

  
283 298
  @locking.ssynchronized(_config_lock, shared=1)
284
  def GenerateMAC(self, ec_id):
299
  def GenerateMAC(self, net, ec_id):
285 300
    """Generate a MAC for an instance.
286 301

  
287 302
    This should check the current instances for duplicates.
288 303

  
289 304
    """
290 305
    existing = self._AllMACs()
291
    return self._temporary_ids.Generate(existing, self._GenerateOneMAC, ec_id)
306
    gen_mac = self._GenerateMACPrefix(net)(self._GenerateMACSuffix)
307
    return self._temporary_ids.Generate(existing, gen_mac, ec_id)
292 308

  
293 309
  @locking.ssynchronized(_config_lock, shared=1)
294 310
  def ReserveMAC(self, mac, ec_id):

Also available in: Unified diff