Package ganeti :: Package utils :: Module storage
[hide private]
[frames] | no frames]

Source Code for Module ganeti.utils.storage

  1  # 
  2  # 
  3   
  4  # Copyright (C) 2013 Google Inc. 
  5  # All rights reserved. 
  6  # 
  7  # Redistribution and use in source and binary forms, with or without 
  8  # modification, are permitted provided that the following conditions are 
  9  # met: 
 10  # 
 11  # 1. Redistributions of source code must retain the above copyright notice, 
 12  # this list of conditions and the following disclaimer. 
 13  # 
 14  # 2. Redistributions in binary form must reproduce the above copyright 
 15  # notice, this list of conditions and the following disclaimer in the 
 16  # documentation and/or other materials provided with the distribution. 
 17  # 
 18  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
 19  # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
 20  # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 21  # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
 22  # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 23  # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 24  # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 25  # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 26  # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 27  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 28  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 29   
 30  """Utility functions for storage. 
 31   
 32  """ 
 33   
 34  import logging 
 35   
 36  from ganeti import constants 
 37   
 38   
39 -def GetDiskTemplatesOfStorageTypes(*storage_types):
40 """Given the storage type, returns a list of disk templates based on that 41 storage type.""" 42 return [dt for dt in constants.DISK_TEMPLATES 43 if constants.MAP_DISK_TEMPLATE_STORAGE_TYPE[dt] in storage_types]
44 45
46 -def IsDiskTemplateEnabled(disk_template, enabled_disk_templates):
47 """Checks if a particular disk template is enabled. 48 49 """ 50 return disk_template in enabled_disk_templates
51 52
53 -def IsFileStorageEnabled(enabled_disk_templates):
54 """Checks if file storage is enabled. 55 56 """ 57 return IsDiskTemplateEnabled(constants.DT_FILE, enabled_disk_templates)
58 59
60 -def IsSharedFileStorageEnabled(enabled_disk_templates):
61 """Checks if shared file storage is enabled. 62 63 """ 64 return IsDiskTemplateEnabled(constants.DT_SHARED_FILE, enabled_disk_templates)
65 66
67 -def IsLvmEnabled(enabled_disk_templates):
68 """Check whether or not any lvm-based disk templates are enabled.""" 69 return len(constants.DTS_LVM & set(enabled_disk_templates)) != 0
70 71
72 -def LvmGetsEnabled(enabled_disk_templates, new_enabled_disk_templates):
73 """Checks whether lvm was not enabled before, but will be enabled after 74 the operation. 75 76 """ 77 if IsLvmEnabled(enabled_disk_templates): 78 return False 79 return len(constants.DTS_LVM & set(new_enabled_disk_templates)) != 0
80 81
82 -def _GetDefaultStorageUnitForDiskTemplate(cfg, disk_template):
83 """Retrieves the identifier of the default storage entity for the given 84 storage type. 85 86 @type cfg: C{objects.ConfigData} 87 @param cfg: the configuration data 88 @type disk_template: string 89 @param disk_template: a disk template, for example 'drbd' 90 @rtype: string 91 @return: identifier for a storage unit, for example the vg_name for lvm 92 storage 93 94 """ 95 storage_type = constants.MAP_DISK_TEMPLATE_STORAGE_TYPE[disk_template] 96 cluster = cfg.GetClusterInfo() 97 if disk_template in constants.DTS_LVM: 98 return (storage_type, cfg.GetVGName()) 99 elif disk_template == constants.DT_FILE: 100 return (storage_type, cluster.file_storage_dir) 101 elif disk_template == constants.DT_SHARED_FILE: 102 return (storage_type, cluster.shared_file_storage_dir) 103 elif disk_template == constants.DT_GLUSTER: 104 return (storage_type, cluster.gluster_storage_dir) 105 else: 106 return (storage_type, None)
107 108
109 -def DiskTemplateSupportsSpaceReporting(disk_template):
110 """Check whether the disk template supports storage space reporting.""" 111 return (constants.MAP_DISK_TEMPLATE_STORAGE_TYPE[disk_template] 112 in constants.STS_REPORT)
113 114
115 -def GetStorageUnits(cfg, disk_templates):
116 """Get the cluster's storage units for the given disk templates. 117 118 If any lvm-based disk template is requested, spindle information 119 is added to the request. 120 121 @type cfg: L{config.ConfigWriter} 122 @param cfg: Cluster configuration 123 @type disk_templates: list of string 124 @param disk_templates: list of disk templates for which the storage 125 units will be computed 126 @rtype: list of tuples (string, string) 127 @return: list of storage units, each storage unit being a tuple of 128 (storage_type, storage_key); storage_type is in 129 C{constants.STORAGE_TYPES} and the storage_key a string to 130 identify an entity of that storage type, for example a volume group 131 name for LVM storage or a file for file storage. 132 133 """ 134 storage_units = [] 135 for disk_template in disk_templates: 136 if DiskTemplateSupportsSpaceReporting(disk_template): 137 storage_units.append( 138 _GetDefaultStorageUnitForDiskTemplate(cfg, disk_template)) 139 return storage_units
140 141
142 -def LookupSpaceInfoByDiskTemplate(storage_space_info, disk_template):
143 """Looks up the storage space info for a given disk template. 144 145 @type storage_space_info: list of dicts 146 @param storage_space_info: result of C{GetNodeInfo} 147 @type disk_template: string 148 @param disk_template: disk template to get storage space info 149 @rtype: tuple 150 @return: returns the element of storage_space_info that matches the given 151 disk template 152 153 """ 154 storage_type = constants.MAP_DISK_TEMPLATE_STORAGE_TYPE[disk_template] 155 return LookupSpaceInfoByStorageType(storage_space_info, storage_type)
156 157
158 -def LookupSpaceInfoByStorageType(storage_space_info, storage_type):
159 """Looks up the storage space info for a given storage type. 160 161 Note that this lookup can be ambiguous if storage space reporting for several 162 units of the same storage type was requested. This function is only supposed 163 to be used for legacy code in situations where it actually is unambiguous. 164 165 @type storage_space_info: list of dicts 166 @param storage_space_info: result of C{GetNodeInfo} 167 @type storage_type: string 168 @param storage_type: a storage type, which is included in the storage_units 169 list 170 @rtype: tuple 171 @return: returns the element of storage_space_info that matches the given 172 storage type 173 174 """ 175 result = None 176 for unit_info in storage_space_info: 177 if unit_info["type"] == storage_type: 178 if result is None: 179 result = unit_info 180 else: 181 # There is more than one storage type in the query, log a warning 182 logging.warning("Storage space information requested for" 183 " ambiguous storage type '%s'.", storage_type) 184 return result
185 186
187 -def GetDiskLabels(prefix, num_disks, start=0):
188 """Generate disk labels for a number of disks 189 190 Note that disk labels are generated in the range [start..num_disks[ 191 (e.g., as in range(start, num_disks)) 192 193 @type prefix: string 194 @param prefix: disk label prefix (e.g., "/dev/sd") 195 196 @type num_disks: int 197 @param num_disks: number of disks (i.e., disk labels) 198 199 @type start: int 200 @param start: optional start index 201 202 @rtype: generator 203 @return: generator for the disk labels 204 205 """ 206 def _GetDiskSuffix(i): 207 n = ord('z') - ord('a') + 1 208 if i < n: 209 return chr(ord('a') + i) 210 else: 211 mod = int(i % n) 212 pref = _GetDiskSuffix((i - mod) / (n + 1)) 213 suf = _GetDiskSuffix(mod) 214 return pref + suf
215 216 for i in range(start, num_disks): 217 yield prefix + _GetDiskSuffix(i) 218 219
220 -def osminor(dev):
221 """Return the device minor number from a raw device number. 222 223 This is a replacement for os.minor working around the issue that 224 Python's os.minor still has the old definition. See Ganeti issue 225 1058 for more details. 226 """ 227 return (dev & 0xff) | ((dev >> 12) & ~0xff)
228