Revision 397b7844

b/lib/constants.py
948 948
HV_KVM_MACHINE_VERSION = "machine_version"
949 949
HV_KVM_PATH = "kvm_path"
950 950
HV_VIF_TYPE = "vif_type"
951
HV_VIF_SCRIPT = "vif_script"
951 952
HV_XEN_CMD = "xen_cmd"
952 953

  
953 954

  
......
1019 1020
  HV_KVM_EXTRA: VTYPE_STRING,
1020 1021
  HV_KVM_MACHINE_VERSION: VTYPE_STRING,
1021 1022
  HV_VIF_TYPE: VTYPE_STRING,
1023
  HV_VIF_SCRIPT: VTYPE_STRING,
1022 1024
  HV_XEN_CMD: VTYPE_STRING,
1023 1025
  }
1024 1026

  
......
2052 2054
    HV_CPU_MASK: CPU_PINNING_ALL,
2053 2055
    HV_CPU_CAP: 0,
2054 2056
    HV_CPU_WEIGHT: 256,
2057
    HV_VIF_SCRIPT: "",
2055 2058
    HV_XEN_CMD: XEN_CMD_XM,
2056 2059
    },
2057 2060
  HT_XEN_HVM: {
......
2075 2078
    HV_CPU_CAP: 0,
2076 2079
    HV_CPU_WEIGHT: 256,
2077 2080
    HV_VIF_TYPE: HT_HVM_VIF_IOEMU,
2081
    HV_VIF_SCRIPT: "",
2078 2082
    HV_XEN_CMD: XEN_CMD_XM,
2079 2083
    },
2080 2084
  HT_KVM: {
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
......
313 315
  CAN_MIGRATE = True
314 316
  REBOOT_RETRY_COUNT = 60
315 317
  REBOOT_RETRY_INTERVAL = 10
318
  _ROOT_DIR = pathutils.RUN_DIR + "/xen-hypervisor"
319
  _NICS_DIR = _ROOT_DIR + "/nic" # contains NICs' info
320
  _DIRS = [_ROOT_DIR, _NICS_DIR]
316 321

  
317 322
  ANCILLARY_FILES = [
318 323
    XEND_CONFIG_FILE,
......
383 388
    return utils.PathJoin(self._cfgdir, instance_name)
384 389

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

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

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

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

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

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

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

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

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

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

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

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

  
......
423 483

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

  
427 492
  def _StashConfigFile(self, instance_name):
428 493
    """Move the Xen config file to the log directory and return its new path.
......
892 957
    constants.HV_CPU_CAP: hv_base.OPT_NONNEGATIVE_INT_CHECK,
893 958
    constants.HV_CPU_WEIGHT:
894 959
      (False, lambda x: 0 < x < 65536, "invalid weight", None, None),
960
    constants.HV_VIF_SCRIPT: hv_base.OPT_FILE_CHECK,
895 961
    constants.HV_XEN_CMD:
896 962
      hv_base.ParamInSet(True, constants.KNOWN_XEN_COMMANDS),
897 963
    }
......
945 1011
    config.write("name = '%s'\n" % instance.name)
946 1012

  
947 1013
    vif_data = []
948
    for nic in instance.nics:
1014
    for idx, nic in enumerate(instance.nics):
949 1015
      nic_str = "mac=%s" % (nic.mac)
950 1016
      ip = getattr(nic, "ip", None)
951 1017
      if ip is not None:
952 1018
        nic_str += ", ip=%s" % ip
953 1019
      if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
954 1020
        nic_str += ", bridge=%s" % nic.nicparams[constants.NIC_LINK]
1021
      if hvp[constants.HV_VIF_SCRIPT]:
1022
        nic_str += ", script=%s" % hvp[constants.HV_VIF_SCRIPT]
955 1023
      vif_data.append("'%s'" % nic_str)
1024
      self._WriteNICInfoFile(instance.name, idx, nic)
956 1025

  
957 1026
    disk_data = \
958 1027
      _GetConfigFileDiskData(block_devices, hvp[constants.HV_BLOCKDEV_PREFIX])
......
1016 1085
      (False, lambda x: 0 < x < 65535, "invalid weight", None, None),
1017 1086
    constants.HV_VIF_TYPE:
1018 1087
      hv_base.ParamInSet(False, constants.HT_HVM_VALID_VIF_TYPES),
1088
    constants.HV_VIF_SCRIPT: hv_base.OPT_FILE_CHECK,
1019 1089
    constants.HV_XEN_CMD:
1020 1090
      hv_base.ParamInSet(True, constants.KNOWN_XEN_COMMANDS),
1021 1091
    }
......
1106 1176
      # parameter 'model' is only valid with type 'ioemu'
1107 1177
      nic_type_str = ", model=%s, type=%s" % \
1108 1178
        (nic_type, constants.HT_HVM_VIF_IOEMU)
1109
    for nic in instance.nics:
1179
    for idx, nic in enumerate(instance.nics):
1110 1180
      nic_str = "mac=%s%s" % (nic.mac, nic_type_str)
1111 1181
      ip = getattr(nic, "ip", None)
1112 1182
      if ip is not None:
1113 1183
        nic_str += ", ip=%s" % ip
1114 1184
      if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
1115 1185
        nic_str += ", bridge=%s" % nic.nicparams[constants.NIC_LINK]
1186
      if hvp[constants.HV_VIF_SCRIPT]:
1187
        nic_str += ", script=%s" % hvp[constants.HV_VIF_SCRIPT]
1116 1188
      vif_data.append("'%s'" % nic_str)
1189
      self._WriteNICInfoFile(instance.name, idx, nic)
1117 1190

  
1118 1191
    config.write("vif = [%s]\n" % ",".join(vif_data))
1119 1192

  

Also available in: Unified diff