Revision 71fe51f8 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
......
319 321
  CAN_MIGRATE = True
320 322
  REBOOT_RETRY_COUNT = 60
321 323
  REBOOT_RETRY_INTERVAL = 10
324
  _ROOT_DIR = pathutils.RUN_DIR + "/xen-hypervisor"
325
  _NICS_DIR = _ROOT_DIR + "/nic" # contains NICs' info
326
  _DIRS = [_ROOT_DIR, _NICS_DIR]
322 327

  
323 328
  ANCILLARY_FILES = [
324 329
    XEND_CONFIG_FILE,
......
382 387
    return utils.PathJoin(self._cfgdir, instance_name)
383 388

  
384 389
  @classmethod
390
  def _WriteNICInfoFile(cls, instance_name, idx, nic):
391
    """Write the Xen config file for the instance.
392

  
393
    This version of the function just writes the config file from static data.
394

  
395
    """
396
    dirs = [(dname, constants.RUN_DIRS_MODE)
397
            for dname in cls._DIRS + [cls._InstanceNICDir(instance_name)]]
398
    utils.EnsureDirs(dirs)
399

  
400
    cfg_file = cls._InstanceNICFile(instance_name, idx)
401
    data = StringIO()
402

  
403
    if nic.netinfo:
404
      netinfo = objects.Network.FromDict(nic.netinfo)
405
      data.write("NETWORK_NAME=%s\n" % netinfo.name)
406
      if netinfo.network:
407
        data.write("NETWORK_SUBNET=%s\n" % netinfo.network)
408
      if netinfo.gateway:
409
        data.write("NETWORK_GATEWAY=%s\n" % netinfo.gateway)
410
      if netinfo.network6:
411
        data.write("NETWORK_SUBNET6=%s\n" % netinfo.network6)
412
      if netinfo.gateway6:
413
        data.write("NETWORK_GATEWAY6=%s\n" % netinfo.gateway6)
414
      if netinfo.mac_prefix:
415
        data.write("NETWORK_MAC_PREFIX=%s\n" % netinfo.mac_prefix)
416
      if netinfo.tags:
417
        data.write("NETWORK_TAGS=%s\n" % "\ ".join(netinfo.tags))
418

  
419
    data.write("MAC=%s\n" % nic.mac)
420
    data.write("IP=%s\n" % nic.ip)
421
    data.write("MODE=%s\n" % nic.nicparams[constants.NIC_MODE])
422
    data.write("LINK=%s\n" % nic.nicparams[constants.NIC_LINK])
423

  
424
    try:
425
      utils.WriteFile(cfg_file, data=data.getvalue())
426
    except EnvironmentError, err:
427
      raise errors.HypervisorError("Cannot write Xen instance configuration"
428
                                   " file %s: %s" % (cfg_file, err))
429

  
430
  @classmethod
431
  def _InstanceNICDir(cls, instance_name):
432
    """Returns the directory holding the tap device files for a given instance.
433

  
434
    """
435
    return utils.PathJoin(cls._NICS_DIR, instance_name)
436

  
437
  @classmethod
438
  def _InstanceNICFile(cls, instance_name, seq):
439
    """Returns the name of the file containing the tap device for a given NIC
440

  
441
    """
442
    return utils.PathJoin(cls._InstanceNICDir(instance_name), str(seq))
443

  
444
  @classmethod
385 445
  def _GetConfig(cls, instance, startup_memory, block_devices):
386 446
    """Build Xen configuration for an instance.
387 447

  
......
422 482

  
423 483
    """
424 484
    utils.RemoveFile(self._ConfigFileName(instance_name))
485
    try:
486
      shutil.rmtree(self._InstanceNICDir(instance_name))
487
    except OSError, err:
488
      if err.errno != errno.ENOENT:
489
        raise
425 490

  
426 491
  def _StashConfigFile(self, instance_name):
427 492
    """Move the Xen config file to the log directory and return its new path.
......
854 919
    constants.HV_CPU_CAP: hv_base.OPT_NONNEGATIVE_INT_CHECK,
855 920
    constants.HV_CPU_WEIGHT:
856 921
      (False, lambda x: 0 < x < 65536, "invalid weight", None, None),
922
    constants.HV_VIF_SCRIPT: hv_base.OPT_FILE_CHECK,
857 923
    }
858 924

  
859 925
  def _GetConfig(self, instance, startup_memory, block_devices):
......
905 971
    config.write("name = '%s'\n" % instance.name)
906 972

  
907 973
    vif_data = []
908
    for nic in instance.nics:
974
    for idx, nic in enumerate(instance.nics):
909 975
      nic_str = "mac=%s" % (nic.mac)
910 976
      ip = getattr(nic, "ip", None)
911 977
      if ip is not None:
912 978
        nic_str += ", ip=%s" % ip
913 979
      if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
914 980
        nic_str += ", bridge=%s" % nic.nicparams[constants.NIC_LINK]
981
      if hvp[constants.HV_VIF_SCRIPT]:
982
        nic_str += ", script=%s" % hvp[constants.HV_VIF_SCRIPT]
915 983
      vif_data.append("'%s'" % nic_str)
984
      self._WriteNICInfoFile(instance.name, idx, nic)
916 985

  
917 986
    disk_data = \
918 987
      _GetConfigFileDiskData(block_devices, hvp[constants.HV_BLOCKDEV_PREFIX])
......
977 1046
    constants.HV_VIF_TYPE:
978 1047
      hv_base.ParamInSet(False, constants.HT_HVM_VALID_VIF_TYPES),
979 1048
    constants.HV_VIRIDIAN: hv_base.NO_CHECK,
1049
    constants.HV_VIF_SCRIPT: hv_base.OPT_FILE_CHECK,
980 1050
    }
981 1051

  
982 1052
  def _GetConfig(self, instance, startup_memory, block_devices):
......
1070 1140
      # parameter 'model' is only valid with type 'ioemu'
1071 1141
      nic_type_str = ", model=%s, type=%s" % \
1072 1142
        (nic_type, constants.HT_HVM_VIF_IOEMU)
1073
    for nic in instance.nics:
1143
    for idx, nic in enumerate(instance.nics):
1074 1144
      nic_str = "mac=%s%s" % (nic.mac, nic_type_str)
1075 1145
      ip = getattr(nic, "ip", None)
1076 1146
      if ip is not None:
1077 1147
        nic_str += ", ip=%s" % ip
1078 1148
      if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
1079 1149
        nic_str += ", bridge=%s" % nic.nicparams[constants.NIC_LINK]
1150
      if hvp[constants.HV_VIF_SCRIPT]:
1151
        nic_str += ", script=%s" % hvp[constants.HV_VIF_SCRIPT]
1080 1152
      vif_data.append("'%s'" % nic_str)
1153
      self._WriteNICInfoFile(instance.name, idx, nic)
1081 1154

  
1082 1155
    config.write("vif = [%s]\n" % ",".join(vif_data))
1083 1156

  

Also available in: Unified diff