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 GetDiskTemplatesOfStorageType(storage_type):
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] == storage_type]
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 else: 104 return (storage_type, None)
105 106
107 -def DiskTemplateSupportsSpaceReporting(disk_template):
108 """Check whether the disk template supports storage space reporting.""" 109 return (constants.MAP_DISK_TEMPLATE_STORAGE_TYPE[disk_template] 110 in constants.STS_REPORT)
111 112
113 -def GetStorageUnits(cfg, disk_templates):
114 """Get the cluster's storage units for the given disk templates. 115 116 If any lvm-based disk template is requested, spindle information 117 is added to the request. 118 119 @type cfg: L{config.ConfigWriter} 120 @param cfg: Cluster configuration 121 @type disk_templates: list of string 122 @param disk_templates: list of disk templates for which the storage 123 units will be computed 124 @rtype: list of tuples (string, string) 125 @return: list of storage units, each storage unit being a tuple of 126 (storage_type, storage_key); storage_type is in 127 C{constants.STORAGE_TYPES} and the storage_key a string to 128 identify an entity of that storage type, for example a volume group 129 name for LVM storage or a file for file storage. 130 131 """ 132 storage_units = [] 133 for disk_template in disk_templates: 134 if DiskTemplateSupportsSpaceReporting(disk_template): 135 storage_units.append( 136 _GetDefaultStorageUnitForDiskTemplate(cfg, disk_template)) 137 return storage_units
138 139
140 -def LookupSpaceInfoByDiskTemplate(storage_space_info, disk_template):
141 """Looks up the storage space info for a given disk template. 142 143 @type storage_space_info: list of dicts 144 @param storage_space_info: result of C{GetNodeInfo} 145 @type disk_template: string 146 @param disk_template: disk template to get storage space info 147 @rtype: tuple 148 @return: returns the element of storage_space_info that matches the given 149 disk template 150 151 """ 152 storage_type = constants.MAP_DISK_TEMPLATE_STORAGE_TYPE[disk_template] 153 return LookupSpaceInfoByStorageType(storage_space_info, storage_type)
154 155
156 -def LookupSpaceInfoByStorageType(storage_space_info, storage_type):
157 """Looks up the storage space info for a given storage type. 158 159 Note that this lookup can be ambiguous if storage space reporting for several 160 units of the same storage type was requested. This function is only supposed 161 to be used for legacy code in situations where it actually is unambiguous. 162 163 @type storage_space_info: list of dicts 164 @param storage_space_info: result of C{GetNodeInfo} 165 @type storage_type: string 166 @param storage_type: a storage type, which is included in the storage_units 167 list 168 @rtype: tuple 169 @return: returns the element of storage_space_info that matches the given 170 storage type 171 172 """ 173 result = None 174 for unit_info in storage_space_info: 175 if unit_info["type"] == storage_type: 176 if result is None: 177 result = unit_info 178 else: 179 # There is more than one storage type in the query, log a warning 180 logging.warning("Storage space information requested for" 181 " ambiguous storage type '%s'.", storage_type) 182 return result
183 184
185 -def osminor(dev):
186 """Return the device minor number from a raw device number. 187 188 This is a replacement for os.minor working around the issue that 189 Python's os.minor still has the old definition. See Ganeti issue 190 1058 for more details. 191 """ 192 return (dev & 0xff) | ((dev >> 12) & ~0xff)
193