Revision d0bb3f24 lib/hypervisor/hv_xen.py

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

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

  
29 30
from ganeti import constants
......
41 42
VIF_BRIDGE_SCRIPT = utils.PathJoin(pathutils.XEN_CONFIG_DIR,
42 43
                                   "scripts/vif-bridge")
43 44
_DOM0_NAME = "Domain-0"
45
_DISK_LETTERS = string.ascii_lowercase
46

  
47
_FILE_DRIVER_MAP = {
48
  constants.FD_LOOP: "file",
49
  constants.FD_BLKTAP: "tap:aio",
50
  }
44 51

  
45 52

  
46 53
def _CreateConfigCpus(cpu_mask):
......
254 261
  return _MergeInstanceInfo(_ParseNodeInfo(info), fn)
255 262

  
256 263

  
264
def _GetConfigFileDiskData(block_devices, blockdev_prefix,
265
                           _letters=_DISK_LETTERS):
266
  """Get disk directives for Xen config file.
267

  
268
  This method builds the xen config disk directive according to the
269
  given disk_template and block_devices.
270

  
271
  @param block_devices: list of tuples (cfdev, rldev):
272
      - cfdev: dict containing ganeti config disk part
273
      - rldev: ganeti.bdev.BlockDev object
274
  @param blockdev_prefix: a string containing blockdevice prefix,
275
                          e.g. "sd" for /dev/sda
276

  
277
  @return: string containing disk directive for xen instance config file
278

  
279
  """
280
  if len(block_devices) > len(_letters):
281
    raise errors.HypervisorError("Too many disks")
282

  
283
  disk_data = []
284

  
285
  for sd_suffix, (cfdev, dev_path) in zip(_letters, block_devices):
286
    sd_name = blockdev_prefix + sd_suffix
287

  
288
    if cfdev.mode == constants.DISK_RDWR:
289
      mode = "w"
290
    else:
291
      mode = "r"
292

  
293
    if cfdev.dev_type == constants.LD_FILE:
294
      driver = _FILE_DRIVER_MAP[cfdev.physical_id[0]]
295
    else:
296
      driver = "phy"
297

  
298
    disk_data.append("'%s:%s,%s,%s'" % (driver, dev_path, sd_name, mode))
299

  
300
  return disk_data
301

  
302

  
257 303
class XenHypervisor(hv_base.BaseHypervisor):
258 304
  """Xen generic hypervisor interface
259 305

  
......
506 552

  
507 553
    return None
508 554

  
509
  @staticmethod
510
  def _GetConfigFileDiskData(block_devices, blockdev_prefix):
511
    """Get disk directive for xen config file.
512

  
513
    This method builds the xen config disk directive according to the
514
    given disk_template and block_devices.
515

  
516
    @param block_devices: list of tuples (cfdev, rldev):
517
        - cfdev: dict containing ganeti config disk part
518
        - rldev: ganeti.bdev.BlockDev object
519
    @param blockdev_prefix: a string containing blockdevice prefix,
520
                            e.g. "sd" for /dev/sda
521

  
522
    @return: string containing disk directive for xen instance config file
523

  
524
    """
525
    FILE_DRIVER_MAP = {
526
      constants.FD_LOOP: "file",
527
      constants.FD_BLKTAP: "tap:aio",
528
      }
529
    disk_data = []
530
    if len(block_devices) > 24:
531
      # 'z' - 'a' = 24
532
      raise errors.HypervisorError("Too many disks")
533
    namespace = [blockdev_prefix + chr(i + ord("a")) for i in range(24)]
534
    for sd_name, (cfdev, dev_path) in zip(namespace, block_devices):
535
      if cfdev.mode == constants.DISK_RDWR:
536
        mode = "w"
537
      else:
538
        mode = "r"
539
      if cfdev.dev_type == constants.LD_FILE:
540
        line = "'%s:%s,%s,%s'" % (FILE_DRIVER_MAP[cfdev.physical_id[0]],
541
                                  dev_path, sd_name, mode)
542
      else:
543
        line = "'phy:%s,%s,%s'" % (dev_path, sd_name, mode)
544
      disk_data.append(line)
545

  
546
    return disk_data
547

  
548 555
  def MigrationInfo(self, instance):
549 556
    """Get instance information to perform a migration.
550 557

  
......
765 772
        nic_str += ", bridge=%s" % nic.nicparams[constants.NIC_LINK]
766 773
      vif_data.append("'%s'" % nic_str)
767 774

  
768
    disk_data = cls._GetConfigFileDiskData(block_devices,
769
                                           hvp[constants.HV_BLOCKDEV_PREFIX])
775
    disk_data = \
776
      _GetConfigFileDiskData(block_devices, hvp[constants.HV_BLOCKDEV_PREFIX])
770 777

  
771 778
    config.write("vif = [%s]\n" % ",".join(vif_data))
772 779
    config.write("disk = [%s]\n" % ",".join(disk_data))
......
918 925

  
919 926
    config.write("vif = [%s]\n" % ",".join(vif_data))
920 927

  
921
    disk_data = cls._GetConfigFileDiskData(block_devices,
922
                                           hvp[constants.HV_BLOCKDEV_PREFIX])
928
    disk_data = \
929
      _GetConfigFileDiskData(block_devices, hvp[constants.HV_BLOCKDEV_PREFIX])
923 930

  
924 931
    iso_path = hvp[constants.HV_CDROM_IMAGE_PATH]
925 932
    if iso_path:

Also available in: Unified diff