Revision a0af6c80

b/lib/cmdlib.py
10120 10120
    # creation job will fail.
10121 10121
    for nic in self.nics:
10122 10122
      if nic.mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
10123
        nic.mac = self.cfg.GenerateMAC(self.proc.GetECId())
10123
        nic.mac = self.cfg.GenerateMAC(nic.network, self.proc.GetECId())
10124 10124

  
10125 10125
    #### allocator run
10126 10126

  
......
12846 12846
      elif mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
12847 12847
        # otherwise generate the MAC address
12848 12848
        params[constants.INIC_MAC] = \
12849
          self.cfg.GenerateMAC(self.proc.GetECId())
12849
          self.cfg.GenerateMAC(new_net, self.proc.GetECId())
12850 12850
      else:
12851 12851
        # or validate/reserve the current one
12852 12852
        try:
......
12867 12867
      old_prefix = get_net_prefix(old_net)
12868 12868
      if old_prefix != new_prefix:
12869 12869
        params[constants.INIC_MAC] = \
12870
          self.cfg.GenerateMAC(self.proc.GetECId())
12870
          self.cfg.GenerateMAC(new_net, self.proc.GetECId())
12871 12871

  
12872 12872
    #if there is a change in nic-network configuration
12873 12873
    new_ip = params.get(constants.INIC_IP, old_ip)
......
15482 15482
      raise errors.OpPrereqError("Network '%s' already defined" %
15483 15483
                                 self.op.network, errors.ECODE_EXISTS)
15484 15484

  
15485
    if self.op.mac_prefix:
15486
      utils.NormalizeAndValidateMac(self.op.mac_prefix+":00:00:00")
15485 15487

  
15486 15488
  def BuildHooksEnv(self):
15487 15489
    """Build hooks env.
......
15670 15672
      if self.op.mac_prefix == constants.VALUE_NONE:
15671 15673
        self.mac_prefix = None
15672 15674
      else:
15675
        utils.NormalizeAndValidateMac(self.op.mac_prefix+":00:00:00")
15673 15676
        self.mac_prefix = self.op.mac_prefix
15674 15677

  
15675 15678
    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
......
219 220
    """
220 221
    return os.path.exists(pathutils.CLUSTER_CONF_FILE)
221 222

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

  
238
  def _GenerateMACSuffix(self):
223 239
    """Generate one mac address
224 240

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

  
233 248
  @locking.ssynchronized(_config_lock, shared=1)
234 249
  def GetNdParams(self, node):
......
277 292
    return self._config_data.cluster.SimpleFillDP(group.diskparams)
278 293

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

  
283 298
    This should check the current instances for duplicates.
284 299

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

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

Also available in: Unified diff