Revision 5282084b lib/backend.py

b/lib/backend.py
733 733
  return retdic
734 734

  
735 735

  
736
def _SymlinkBlockDev(instance_name, device_path, device_name):
736
def _GetBlockDevSymlinkPath(instance_name, idx):
737
  return os.path.join(constants.DISK_LINKS_DIR,
738
                      "%s:%d" % (instance_name, idx))
739

  
740

  
741
def _SymlinkBlockDev(instance_name, device_path, idx):
737 742
  """Set up symlinks to a instance's block device.
738 743

  
739 744
  This is an auxiliary function run when an instance is start (on the primary
740 745
  node) or when an instance is migrated (on the target node).
741 746

  
742
  Args:
743
    instance_name: the name of the target instance
744
    device_path: path of the physical block device, on the node
745
    device_name: 'virtual' name of the device
746 747

  
747
  Returns:
748
    absolute path to the disk's symlink
748
  @param instance_name: the name of the target instance
749
  @param device_path: path of the physical block device, on the node
750
  @param idx: the disk index
751
  @return: absolute path to the disk's symlink
749 752

  
750 753
  """
751
  link_basename = "%s-%s" % (instance_name, device_name)
752
  link_name = os.path.join(constants.DISK_LINKS_DIR, link_basename)
754
  link_name = _GetBlockDevSymlinkPath(instance_name, idx)
753 755
  try:
754 756
    os.symlink(device_path, link_name)
755
  except OSError, e:
756
    if e.errno == errno.EEXIST:
757
  except OSError, err:
758
    if err.errno == errno.EEXIST:
757 759
      if (not os.path.islink(link_name) or
758 760
          os.readlink(link_name) != device_path):
759 761
        os.remove(link_name)
......
764 766
  return link_name
765 767

  
766 768

  
767
def _RemoveBlockDevLinks(instance_name):
769
def _RemoveBlockDevLinks(instance_name, disks):
768 770
  """Remove the block device symlinks belonging to the given instance.
769 771

  
770 772
  """
771
  for short_name in os.listdir(constants.DISK_LINKS_DIR):
772
    link_name = os.path.join(constants.DISK_LINKS_DIR, short_name)
773
    if (os.path.islink(link_name) and
774
        short_name.startswith('%s-' % instance_name)):
773
  for idx, disk in enumerate(disks):
774
    link_name = _GetBlockDevSymlinkPath(instance_name, idx)
775
    if os.path.islink(link_name):
775 776
      try:
776 777
        os.remove(link_name)
777 778
      except OSError:
......
798 799
                                    str(disk))
799 800
    device.Open()
800 801
    try:
801
      link_name = _SymlinkBlockDev(instance.name, device.dev_path,
802
                                   "disk%d" % idx)
802
      link_name = _SymlinkBlockDev(instance.name, device.dev_path, idx)
803 803
    except OSError, e:
804 804
      raise errors.BlockDeviceError("Cannot create block device symlink: %s" %
805 805
                                    e.strerror)
......
832 832
    return False
833 833
  except errors.HypervisorError, err:
834 834
    logging.exception("Failed to start instance")
835
    _RemoveBlockDevLinks(instance.name)
835
    _RemoveBlockDevLinks(instance.name, instance.disks)
836 836
    return False
837 837

  
838 838
  return True
......
885 885
                    instance.name)
886 886
      return False
887 887

  
888
  _RemoveBlockDevLinks(instance.name)
888
  _RemoveBlockDevLinks(instance.name, instance.disks)
889 889

  
890 890
  return True
891 891

  
......
2107 2107
    return (False, "Can't make devices secondary: %s" % ",".join(msg))
2108 2108
  else:
2109 2109
    if instance_name:
2110
      _RemoveBlockDevLinks(instance_name)
2110
      _RemoveBlockDevLinks(instance_name, disks)
2111 2111
    return (True, "All devices secondary")
2112 2112

  
2113 2113

  

Also available in: Unified diff