Revision 8235cd88 lib/hypervisor/hv_kvm.py

b/lib/hypervisor/hv_kvm.py
79 79
  constants.HV_KVM_SPICE_USE_TLS,
80 80
  ])
81 81

  
82
# below constants show the format of runtime file
83
# the nics are in second possition, while the disks in 4th (last)
84
# moreover disk entries are stored in tupples of L{objects.Disk}, dev_path
85
_KVM_NICS_RUNTIME_INDEX = 1
86
_KVM_DISKS_RUNTIME_INDEX = 3
87
_DEVICE_RUNTIME_INDEX = {
88
  constants.HOTPLUG_TARGET_DISK: _KVM_DISKS_RUNTIME_INDEX,
89
  constants.HOTPLUG_TARGET_NIC: _KVM_NICS_RUNTIME_INDEX
90
  }
91
_FIND_RUNTIME_ENTRY = {
92
  constants.HOTPLUG_TARGET_NIC: \
93
    lambda nic, kvm_nics: [n for n in kvm_nics if n.uuid == nic.uuid],
94
  constants.HOTPLUG_TARGET_DISK: \
95
    lambda disk, kvm_disks: [(d, l) for (d, l) in kvm_disks
96
                             if d.uuid == disk.uuid]
97
  }
98
_RUNTIME_DEVICE = {
99
  constants.HOTPLUG_TARGET_NIC: lambda d: d,
100
  constants.HOTPLUG_TARGET_DISK: lambda (d, e): d
101
  }
102
_RUNTIME_ENTRY = {
103
  constants.HOTPLUG_TARGET_NIC: lambda d, e: d,
104
  constants.HOTPLUG_TARGET_DISK: lambda d, e: (d, e)
105
  }
106

  
107
def _GetExistingDeviceInfo(dev_type, device, runtime):
108
  """Helper function to get an existing device inside the runtime file
109

  
110
  Used when an instance is running. Load kvm runtime file and search
111
  for a device based on its type and uuid.
112

  
113
  @type dev_type: sting
114
  @param dev_type: device type of param dev
115
  @type device: L{objects.Disk} or L{objects.NIC}
116
  @param device: the device object for which we generate a kvm name
117
  @type runtime: tuple (cmd, nics, hvparams, disks)
118
  @param runtime: the runtime data to search for the device
119
  @raise errors.HotplugError: in case the requested device does not
120
    exist (e.g. device has been added without --hotplug option) or
121
    device info has not pci slot (e.g. old devices in the cluster)
122

  
123
  """
124
  index = _DEVICE_RUNTIME_INDEX[dev_type]
125
  found = _FIND_RUNTIME_ENTRY[dev_type](device, runtime[index])
126
  if not found:
127
    raise errors.HotplugError("Cannot find runtime info for %s with UUID %s" %
128
                              (dev_type, device.uuid))
129

  
130
  return found[0]
131

  
82 132

  
83 133
def _GetTunFeatures(fd, _ioctl=fcntl.ioctl):
84 134
  """Retrieves supported TUN features from file descriptor.
......
1430 1480
    if hvp[constants.HV_KVM_EXTRA]:
1431 1481
      kvm_cmd.extend(hvp[constants.HV_KVM_EXTRA].split(" "))
1432 1482

  
1433
    # Save the current instance nics, but defer their expansion as parameters,
1434
    # as we'll need to generate executable temp files for them.
1435
    kvm_nics = instance.nics
1483
    kvm_disks = []
1484
    for disk, dev_path in block_devices:
1485
      kvm_disks.append((disk, dev_path))
1486

  
1487
    kvm_nics = []
1488
    for nic in instance.nics:
1489
      kvm_nics.append(nic)
1490

  
1436 1491
    hvparams = hvp
1437 1492

  
1438
    return (kvm_cmd, kvm_nics, hvparams)
1493
    return (kvm_cmd, kvm_nics, hvparams, kvm_disks)
1439 1494

  
1440 1495
  def _WriteKVMRuntime(self, instance_name, data):
1441 1496
    """Write an instance's KVM runtime
......
1461 1516
    """Save an instance's KVM runtime
1462 1517

  
1463 1518
    """
1464
    kvm_cmd, kvm_nics, hvparams = kvm_runtime
1519
    kvm_cmd, kvm_nics, hvparams, block_devices = kvm_runtime
1520

  
1465 1521
    serialized_nics = [nic.ToDict() for nic in kvm_nics]
1466
    serialized_form = serializer.Dump((kvm_cmd, serialized_nics, hvparams))
1522
    serialized_blockdevs = [(blk.ToDict(), link) for blk, link in block_devices]
1523
    serialized_form = serializer.Dump((kvm_cmd, serialized_nics, hvparams,
1524
                                      serialized_blockdevs))
1525

  
1467 1526
    self._WriteKVMRuntime(instance.name, serialized_form)
1468 1527

  
1469 1528
  def _LoadKVMRuntime(self, instance, serialized_runtime=None):
......
1472 1531
    """
1473 1532
    if not serialized_runtime:
1474 1533
      serialized_runtime = self._ReadKVMRuntime(instance.name)
1534

  
1475 1535
    loaded_runtime = serializer.Load(serialized_runtime)
1476
    kvm_cmd, serialized_nics, hvparams = loaded_runtime
1536
    if len(loaded_runtime)==3:
1537
      serialized_blockdevs = []
1538
      kvm_cmd, serialized_nics, hvparams = loaded_runtime
1539
    else:
1540
      kvm_cmd, serialized_nics, hvparams, serialized_blockdevs = loaded_runtime
1541

  
1477 1542
    kvm_nics = [objects.NIC.FromDict(snic) for snic in serialized_nics]
1478
    return (kvm_cmd, kvm_nics, hvparams)
1543
    block_devices = [(objects.Disk.FromDict(sdisk), link)
1544
                     for sdisk, link in serialized_blockdevs]
1545

  
1546
    return (kvm_cmd, kvm_nics, hvparams, block_devices)
1479 1547

  
1480 1548
  def _RunKVMCmd(self, name, kvm_cmd, tap_fds=None):
1481 1549
    """Run the KVM cmd and check for errors
......
1523 1591

  
1524 1592
    temp_files = []
1525 1593

  
1526
    kvm_cmd, kvm_nics, up_hvp = kvm_runtime
1594
    kvm_cmd, kvm_nics, up_hvp, block_devices = kvm_runtime
1527 1595
    # the first element of kvm_cmd is always the path to the kvm binary
1528 1596
    kvm_path = kvm_cmd[0]
1529 1597
    up_hvp = objects.FillDict(conf_hvp, up_hvp)

Also available in: Unified diff