Revision 2e6f0cdf lib/hypervisor/hv_xen.py

b/lib/hypervisor/hv_xen.py
24 24
"""
25 25

  
26 26
import logging
27
import errno
27 28
import string # pylint: disable=W0402
29
import shutil
28 30
from cStringIO import StringIO
29 31

  
30 32
from ganeti import constants
......
310 312
  CAN_MIGRATE = True
311 313
  REBOOT_RETRY_COUNT = 60
312 314
  REBOOT_RETRY_INTERVAL = 10
315
  _ROOT_DIR = pathutils.RUN_DIR + "/xen-hypervisor"
316
  _NICS_DIR = _ROOT_DIR + "/nic" # contains NICs' info
317
  _DIRS = [_ROOT_DIR, _NICS_DIR]
313 318

  
314 319
  ANCILLARY_FILES = [
315 320
    XEND_CONFIG_FILE,
......
373 378
    return utils.PathJoin(self._cfgdir, instance_name)
374 379

  
375 380
  @classmethod
381
  def _WriteNICInfoFile(cls, instance_name, idx, nic):
382
    """Write the Xen config file for the instance.
383

  
384
    This version of the function just writes the config file from static data.
385

  
386
    """
387
    dirs = [(dname, constants.RUN_DIRS_MODE)
388
            for dname in cls._DIRS + [cls._InstanceNICDir(instance_name)]]
389
    utils.EnsureDirs(dirs)
390

  
391
    cfg_file = cls._InstanceNICFile(instance_name, idx)
392
    data = StringIO()
393

  
394
    if nic.netinfo:
395
      netinfo = objects.Network.FromDict(nic.netinfo)
396
      data.write("NETWORK_NAME=%s\n" % netinfo.name)
397
      if netinfo.network:
398
        data.write("NETWORK_SUBNET=%s\n" % netinfo.network)
399
      if netinfo.gateway:
400
        data.write("NETWORK_GATEWAY=%s\n" % netinfo.gateway)
401
      if netinfo.network6:
402
        data.write("NETWORK_SUBNET6=%s\n" % netinfo.network6)
403
      if netinfo.gateway6:
404
        data.write("NETWORK_GATEWAY6=%s\n" % netinfo.gateway6)
405
      if netinfo.mac_prefix:
406
        data.write("NETWORK_MAC_PREFIX=%s\n" % netinfo.mac_prefix)
407
      if netinfo.tags:
408
        data.write("NETWORK_TAGS=%s\n" % "\ ".join(netinfo.tags))
409

  
410
    data.write("MAC=%s\n" % nic.mac)
411
    data.write("IP=%s\n" % nic.ip)
412
    data.write("MODE=%s\n" % nic.nicparams[constants.NIC_MODE])
413
    data.write("LINK=%s\n" % nic.nicparams[constants.NIC_LINK])
414

  
415
    try:
416
      utils.WriteFile(cfg_file, data=data.getvalue())
417
    except EnvironmentError, err:
418
      raise errors.HypervisorError("Cannot write Xen instance configuration"
419
                                   " file %s: %s" % (cfg_file, err))
420

  
421
  @classmethod
422
  def _InstanceNICDir(cls, instance_name):
423
    """Returns the directory holding the tap device files for a given instance.
424

  
425
    """
426
    return utils.PathJoin(cls._NICS_DIR, instance_name)
427

  
428
  @classmethod
429
  def _InstanceNICFile(cls, instance_name, seq):
430
    """Returns the name of the file containing the tap device for a given NIC
431

  
432
    """
433
    return utils.PathJoin(cls._InstanceNICDir(instance_name), str(seq))
434

  
435
  @classmethod
376 436
  def _GetConfig(cls, instance, startup_memory, block_devices):
377 437
    """Build Xen configuration for an instance.
378 438

  
......
413 473

  
414 474
    """
415 475
    utils.RemoveFile(self._ConfigFileName(instance_name))
476
    try:
477
      shutil.rmtree(self._InstanceNICDir(instance_name))
478
    except OSError, err:
479
      if err.errno != errno.ENOENT:
480
        raise
416 481

  
417 482
  def _StashConfigFile(self, instance_name):
418 483
    """Move the Xen config file to the log directory and return its new path.
......
804 869
    constants.HV_CPU_CAP: hv_base.OPT_NONNEGATIVE_INT_CHECK,
805 870
    constants.HV_CPU_WEIGHT:
806 871
      (False, lambda x: 0 < x < 65536, "invalid weight", None, None),
872
    constants.HV_VIF_SCRIPT: hv_base.OPT_FILE_CHECK,
807 873
    }
808 874

  
809 875
  def _GetConfig(self, instance, startup_memory, block_devices):
......
855 921
    config.write("name = '%s'\n" % instance.name)
856 922

  
857 923
    vif_data = []
858
    for nic in instance.nics:
924
    for idx, nic in enumerate(instance.nics):
859 925
      nic_str = "mac=%s" % (nic.mac)
860 926
      ip = getattr(nic, "ip", None)
861 927
      if ip is not None:
862 928
        nic_str += ", ip=%s" % ip
863 929
      if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
864 930
        nic_str += ", bridge=%s" % nic.nicparams[constants.NIC_LINK]
931
      if hvp[constants.HV_VIF_SCRIPT]:
932
        nic_str += ", script=%s" % hvp[constants.HV_VIF_SCRIPT]
865 933
      vif_data.append("'%s'" % nic_str)
934
      self._WriteNICInfoFile(instance.name, idx, nic)
866 935

  
867 936
    disk_data = \
868 937
      _GetConfigFileDiskData(block_devices, hvp[constants.HV_BLOCKDEV_PREFIX])
......
927 996
    constants.HV_VIF_TYPE:
928 997
      hv_base.ParamInSet(False, constants.HT_HVM_VALID_VIF_TYPES),
929 998
    constants.HV_VIRIDIAN: hv_base.NO_CHECK,
999
    constants.HV_VIF_SCRIPT: hv_base.OPT_FILE_CHECK,
930 1000
    }
931 1001

  
932 1002
  def _GetConfig(self, instance, startup_memory, block_devices):
......
1020 1090
      # parameter 'model' is only valid with type 'ioemu'
1021 1091
      nic_type_str = ", model=%s, type=%s" % \
1022 1092
        (nic_type, constants.HT_HVM_VIF_IOEMU)
1023
    for nic in instance.nics:
1093
    for idx, nic in enumerate(instance.nics):
1024 1094
      nic_str = "mac=%s%s" % (nic.mac, nic_type_str)
1025 1095
      ip = getattr(nic, "ip", None)
1026 1096
      if ip is not None:
1027 1097
        nic_str += ", ip=%s" % ip
1028 1098
      if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
1029 1099
        nic_str += ", bridge=%s" % nic.nicparams[constants.NIC_LINK]
1100
      if hvp[constants.HV_VIF_SCRIPT]:
1101
        nic_str += ", script=%s" % hvp[constants.HV_VIF_SCRIPT]
1030 1102
      vif_data.append("'%s'" % nic_str)
1103
      self._WriteNICInfoFile(instance.name, idx, nic)
1031 1104

  
1032 1105
    config.write("vif = [%s]\n" % ",".join(vif_data))
1033 1106

  

Also available in: Unified diff