root / lib / utils / storage.py @ 1cc324f0
History | View | Annotate | Download (6.2 kB)
1 | 59ef0f15 | Helga Velroyen | #
|
---|---|---|---|
2 | 59ef0f15 | Helga Velroyen | #
|
3 | 59ef0f15 | Helga Velroyen | |
4 | 59ef0f15 | Helga Velroyen | # Copyright (C) 2013 Google Inc.
|
5 | 59ef0f15 | Helga Velroyen | #
|
6 | 59ef0f15 | Helga Velroyen | # This program is free software; you can redistribute it and/or modify
|
7 | 59ef0f15 | Helga Velroyen | # it under the terms of the GNU General Public License as published by
|
8 | 59ef0f15 | Helga Velroyen | # the Free Software Foundation; either version 2 of the License, or
|
9 | 59ef0f15 | Helga Velroyen | # (at your option) any later version.
|
10 | 59ef0f15 | Helga Velroyen | #
|
11 | 59ef0f15 | Helga Velroyen | # This program is distributed in the hope that it will be useful, but
|
12 | 59ef0f15 | Helga Velroyen | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 59ef0f15 | Helga Velroyen | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 59ef0f15 | Helga Velroyen | # General Public License for more details.
|
15 | 59ef0f15 | Helga Velroyen | #
|
16 | 59ef0f15 | Helga Velroyen | # You should have received a copy of the GNU General Public License
|
17 | 59ef0f15 | Helga Velroyen | # along with this program; if not, write to the Free Software
|
18 | 59ef0f15 | Helga Velroyen | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 59ef0f15 | Helga Velroyen | # 02110-1301, USA.
|
20 | 59ef0f15 | Helga Velroyen | |
21 | 59ef0f15 | Helga Velroyen | """Utility functions for storage.
|
22 | 59ef0f15 | Helga Velroyen |
|
23 | 59ef0f15 | Helga Velroyen | """
|
24 | 59ef0f15 | Helga Velroyen | |
25 | 683335b3 | Helga Velroyen | import logging |
26 | 683335b3 | Helga Velroyen | |
27 | 59ef0f15 | Helga Velroyen | from ganeti import constants |
28 | 683335b3 | Helga Velroyen | from ganeti import pathutils |
29 | 59ef0f15 | Helga Velroyen | |
30 | 59ef0f15 | Helga Velroyen | |
31 | 59ef0f15 | Helga Velroyen | def GetDiskTemplatesOfStorageType(storage_type): |
32 | 59ef0f15 | Helga Velroyen | """Given the storage type, returns a list of disk templates based on that
|
33 | 59ef0f15 | Helga Velroyen | storage type."""
|
34 | 59ef0f15 | Helga Velroyen | return [dt for dt in constants.DISK_TEMPLATES |
35 | 59ef0f15 | Helga Velroyen | if constants.DISK_TEMPLATES_STORAGE_TYPE[dt] == storage_type]
|
36 | 59ef0f15 | Helga Velroyen | |
37 | 59ef0f15 | Helga Velroyen | |
38 | 59ef0f15 | Helga Velroyen | def GetLvmDiskTemplates(): |
39 | 59ef0f15 | Helga Velroyen | """Returns all disk templates that use LVM."""
|
40 | 59ef0f15 | Helga Velroyen | return GetDiskTemplatesOfStorageType(constants.ST_LVM_VG)
|
41 | 59ef0f15 | Helga Velroyen | |
42 | 59ef0f15 | Helga Velroyen | |
43 | ebe93784 | Helga Velroyen | def IsDiskTemplateEnabled(disk_template, enabled_disk_templates): |
44 | ebe93784 | Helga Velroyen | """Checks if a particular disk template is enabled.
|
45 | ebe93784 | Helga Velroyen |
|
46 | ebe93784 | Helga Velroyen | """
|
47 | ebe93784 | Helga Velroyen | return disk_template in enabled_disk_templates |
48 | ebe93784 | Helga Velroyen | |
49 | ebe93784 | Helga Velroyen | |
50 | ebe93784 | Helga Velroyen | def IsFileStorageEnabled(enabled_disk_templates): |
51 | ebe93784 | Helga Velroyen | """Checks if file storage is enabled.
|
52 | ebe93784 | Helga Velroyen |
|
53 | ebe93784 | Helga Velroyen | """
|
54 | ebe93784 | Helga Velroyen | return IsDiskTemplateEnabled(constants.DT_FILE, enabled_disk_templates)
|
55 | ebe93784 | Helga Velroyen | |
56 | ebe93784 | Helga Velroyen | |
57 | ebe93784 | Helga Velroyen | def IsSharedFileStorageEnabled(enabled_disk_templates): |
58 | ebe93784 | Helga Velroyen | """Checks if shared file storage is enabled.
|
59 | ebe93784 | Helga Velroyen |
|
60 | ebe93784 | Helga Velroyen | """
|
61 | ebe93784 | Helga Velroyen | return IsDiskTemplateEnabled(constants.DT_SHARED_FILE, enabled_disk_templates)
|
62 | ebe93784 | Helga Velroyen | |
63 | ebe93784 | Helga Velroyen | |
64 | 59ef0f15 | Helga Velroyen | def IsLvmEnabled(enabled_disk_templates): |
65 | 59ef0f15 | Helga Velroyen | """Check whether or not any lvm-based disk templates are enabled."""
|
66 | 59ef0f15 | Helga Velroyen | return len(set(GetLvmDiskTemplates()) |
67 | 59ef0f15 | Helga Velroyen | .intersection(set(enabled_disk_templates))) != 0 |
68 | 59ef0f15 | Helga Velroyen | |
69 | 59ef0f15 | Helga Velroyen | |
70 | 59ef0f15 | Helga Velroyen | def LvmGetsEnabled(enabled_disk_templates, new_enabled_disk_templates): |
71 | 59ef0f15 | Helga Velroyen | """Checks whether lvm was not enabled before, but will be enabled after
|
72 | 59ef0f15 | Helga Velroyen | the operation.
|
73 | 59ef0f15 | Helga Velroyen |
|
74 | 59ef0f15 | Helga Velroyen | """
|
75 | 59ef0f15 | Helga Velroyen | if IsLvmEnabled(enabled_disk_templates):
|
76 | 59ef0f15 | Helga Velroyen | return False |
77 | 59ef0f15 | Helga Velroyen | return set(GetLvmDiskTemplates()).intersection( |
78 | 59ef0f15 | Helga Velroyen | set(new_enabled_disk_templates))
|
79 | 683335b3 | Helga Velroyen | |
80 | 683335b3 | Helga Velroyen | |
81 | 683335b3 | Helga Velroyen | def _GetDefaultStorageUnitForDiskTemplate(cfg, disk_template): |
82 | 683335b3 | Helga Velroyen | """Retrieves the identifier of the default storage entity for the given
|
83 | 683335b3 | Helga Velroyen | storage type.
|
84 | 683335b3 | Helga Velroyen |
|
85 | d721894a | Helga Velroyen | @type cfg: C{objects.ConfigData}
|
86 | d721894a | Helga Velroyen | @param cfg: the configuration data
|
87 | 683335b3 | Helga Velroyen | @type disk_template: string
|
88 | 683335b3 | Helga Velroyen | @param disk_template: a disk template, for example 'drbd'
|
89 | 683335b3 | Helga Velroyen | @rtype: string
|
90 | 683335b3 | Helga Velroyen | @return: identifier for a storage unit, for example the vg_name for lvm
|
91 | 683335b3 | Helga Velroyen | storage
|
92 | 683335b3 | Helga Velroyen |
|
93 | 683335b3 | Helga Velroyen | """
|
94 | 683335b3 | Helga Velroyen | storage_type = constants.DISK_TEMPLATES_STORAGE_TYPE[disk_template] |
95 | d721894a | Helga Velroyen | cluster = cfg.GetClusterInfo() |
96 | 683335b3 | Helga Velroyen | if disk_template in GetLvmDiskTemplates(): |
97 | 683335b3 | Helga Velroyen | return (storage_type, cfg.GetVGName())
|
98 | 683335b3 | Helga Velroyen | elif disk_template == constants.DT_FILE:
|
99 | d721894a | Helga Velroyen | return (storage_type, cluster.file_storage_dir)
|
100 | d721894a | Helga Velroyen | # FIXME: Adjust this, once SHARED_FILE_STORAGE_DIR
|
101 | d721894a | Helga Velroyen | # is not in autoconf anymore.
|
102 | 683335b3 | Helga Velroyen | elif disk_template == constants.DT_SHARED_FILE:
|
103 | 683335b3 | Helga Velroyen | return (storage_type, pathutils.DEFAULT_SHARED_FILE_STORAGE_DIR)
|
104 | 683335b3 | Helga Velroyen | else:
|
105 | 683335b3 | Helga Velroyen | return (storage_type, None) |
106 | 683335b3 | Helga Velroyen | |
107 | 683335b3 | Helga Velroyen | |
108 | 683335b3 | Helga Velroyen | def _GetDefaultStorageUnitForSpindles(cfg): |
109 | 683335b3 | Helga Velroyen | """Creates a 'spindle' storage unit, by retrieving the volume group
|
110 | 683335b3 | Helga Velroyen | name and associating it to the lvm-pv storage type.
|
111 | 683335b3 | Helga Velroyen |
|
112 | 683335b3 | Helga Velroyen | @rtype: (string, string)
|
113 | 683335b3 | Helga Velroyen | @return: tuple (storage_type, storage_key), where storage type is
|
114 | 683335b3 | Helga Velroyen | 'lvm-pv' and storage_key the name of the default volume group
|
115 | 683335b3 | Helga Velroyen |
|
116 | 683335b3 | Helga Velroyen | """
|
117 | 683335b3 | Helga Velroyen | return (constants.ST_LVM_PV, cfg.GetVGName())
|
118 | 683335b3 | Helga Velroyen | |
119 | 683335b3 | Helga Velroyen | |
120 | 683335b3 | Helga Velroyen | # List of storage type for which space reporting is implemented.
|
121 | 683335b3 | Helga Velroyen | # FIXME: Remove this, once the backend is capable to do this for all
|
122 | 683335b3 | Helga Velroyen | # storage types.
|
123 | 683335b3 | Helga Velroyen | _DISK_TEMPLATES_SPACE_QUERYABLE = GetLvmDiskTemplates() \ |
124 | 683335b3 | Helga Velroyen | + GetDiskTemplatesOfStorageType(constants.ST_FILE) |
125 | 683335b3 | Helga Velroyen | |
126 | 683335b3 | Helga Velroyen | |
127 | 683335b3 | Helga Velroyen | def GetStorageUnitsOfCluster(cfg, include_spindles=False): |
128 | 683335b3 | Helga Velroyen | """Examines the cluster's configuration and returns a list of storage
|
129 | 683335b3 | Helga Velroyen | units and their storage keys, ordered by the order in which they
|
130 | 683335b3 | Helga Velroyen | are enabled.
|
131 | 683335b3 | Helga Velroyen |
|
132 | 683335b3 | Helga Velroyen | @type cfg: L{config.ConfigWriter}
|
133 | 683335b3 | Helga Velroyen | @param cfg: Cluster configuration
|
134 | 683335b3 | Helga Velroyen | @type include_spindles: boolean
|
135 | 683335b3 | Helga Velroyen | @param include_spindles: flag to include an extra storage unit for physical
|
136 | 683335b3 | Helga Velroyen | volumes
|
137 | 683335b3 | Helga Velroyen | @rtype: list of tuples (string, string)
|
138 | 683335b3 | Helga Velroyen | @return: list of storage units, each storage unit being a tuple of
|
139 | 683335b3 | Helga Velroyen | (storage_type, storage_key); storage_type is in
|
140 | 683335b3 | Helga Velroyen | C{constants.VALID_STORAGE_TYPES} and the storage_key a string to
|
141 | 683335b3 | Helga Velroyen | identify an entity of that storage type, for example a volume group
|
142 | 683335b3 | Helga Velroyen | name for LVM storage or a file for file storage.
|
143 | 683335b3 | Helga Velroyen |
|
144 | 683335b3 | Helga Velroyen | """
|
145 | 683335b3 | Helga Velroyen | cluster_config = cfg.GetClusterInfo() |
146 | 683335b3 | Helga Velroyen | storage_units = [] |
147 | 683335b3 | Helga Velroyen | for disk_template in cluster_config.enabled_disk_templates: |
148 | 683335b3 | Helga Velroyen | if disk_template in _DISK_TEMPLATES_SPACE_QUERYABLE: |
149 | 683335b3 | Helga Velroyen | storage_units.append( |
150 | 683335b3 | Helga Velroyen | _GetDefaultStorageUnitForDiskTemplate(cfg, disk_template)) |
151 | 683335b3 | Helga Velroyen | if include_spindles:
|
152 | 683335b3 | Helga Velroyen | included_storage_types = set([st for (st, _) in storage_units]) |
153 | 683335b3 | Helga Velroyen | if not constants.ST_LVM_PV in included_storage_types: |
154 | 683335b3 | Helga Velroyen | storage_units.append( |
155 | 683335b3 | Helga Velroyen | _GetDefaultStorageUnitForSpindles(cfg)) |
156 | 683335b3 | Helga Velroyen | |
157 | 683335b3 | Helga Velroyen | return storage_units
|
158 | 683335b3 | Helga Velroyen | |
159 | 683335b3 | Helga Velroyen | |
160 | 683335b3 | Helga Velroyen | def LookupSpaceInfoByStorageType(storage_space_info, storage_type): |
161 | 683335b3 | Helga Velroyen | """Looks up the storage space info for a given storage type.
|
162 | 683335b3 | Helga Velroyen |
|
163 | 683335b3 | Helga Velroyen | Note that this lookup can be ambiguous if storage space reporting for several
|
164 | 683335b3 | Helga Velroyen | units of the same storage type was requested. This function is only supposed
|
165 | 683335b3 | Helga Velroyen | to be used for legacy code in situations where it actually is unambiguous.
|
166 | 683335b3 | Helga Velroyen |
|
167 | 683335b3 | Helga Velroyen | @type storage_space_info: list of dicts
|
168 | 683335b3 | Helga Velroyen | @param storage_space_info: result of C{GetNodeInfo}
|
169 | 683335b3 | Helga Velroyen | @type storage_type: string
|
170 | 683335b3 | Helga Velroyen | @param storage_type: a storage type, which is included in the storage_units
|
171 | 683335b3 | Helga Velroyen | list
|
172 | 683335b3 | Helga Velroyen | @rtype: tuple
|
173 | 683335b3 | Helga Velroyen | @return: returns the element of storage_space_info that matches the given
|
174 | 683335b3 | Helga Velroyen | storage type
|
175 | 683335b3 | Helga Velroyen |
|
176 | 683335b3 | Helga Velroyen | """
|
177 | 683335b3 | Helga Velroyen | result = None
|
178 | 683335b3 | Helga Velroyen | for unit_info in storage_space_info: |
179 | 683335b3 | Helga Velroyen | if unit_info["type"] == storage_type: |
180 | 683335b3 | Helga Velroyen | if result is None: |
181 | 683335b3 | Helga Velroyen | result = unit_info |
182 | 683335b3 | Helga Velroyen | else:
|
183 | 683335b3 | Helga Velroyen | # There is more than one storage type in the query, log a warning
|
184 | 683335b3 | Helga Velroyen | logging.warning("Storage space information requested for"
|
185 | 683335b3 | Helga Velroyen | " ambiguous storage type '%s'.", storage_type)
|
186 | 683335b3 | Helga Velroyen | return result |