3 # Copyright (C) 2012 Greek Research and Technology Network
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 """ vlmc provider wrapper-script for ganeti extstorage disk template
22 The script takes it's input from environment variables. Specifically the
23 following variables should be present:
25 - VOL_NAME: The name of the new Image file
26 - VOL_SIZE: The size of the new Image (in megabytes)
28 The following variables are optional:
30 - EXTP_ORIGIN: The name of the Image file to snapshot
32 The code branches to the correct function, depending on the name (sys.argv[0])
33 of the executed script (attach, create, etc).
35 Returns O after successfull completion, 1 on failure
42 from ganeti import utils
46 """Read the enviromental variables
49 name = os.getenv("VOL_NAME")
51 sys.stderr.write('The environment variable VOL_NAME is missing.\n')
54 size = os.getenv("VOL_SIZE")
56 # sys.stderr.write('The environment variable VOL_SIZE is missing.\n')
59 origin = os.getenv("EXTP_ORIGIN")
61 return (name, size, origin)
64 """Create a new vlmc Image
66 sys.stderr.write('Creation started...\n')
68 name, size, origin = env
70 cmd = ["vlmc", "create", "%s" % name, "--size", "%s" % size]
72 cmd.extend(["--snap", origin])
74 sys.stderr.write('Before RunCmd')
75 result = utils.RunCmd(cmd)
76 sys.stderr.write('After RunCmd')
79 sys.stderr.write('vlmc creation failed (%s): %s\n' %
80 (result.fail_reason, result.output))
85 def _ParseVlmcShowmappedOutput(output, volume_name):
86 """Parse the output of `vlmc showmapped'.
88 This method parses the output of `vlmc showmapped' and returns
89 the vlmc block device path (e.g. /dev/xsegbd0) that matches the
99 lines = output.splitlines()
100 splitted_lines = map(lambda l: l.split(field_sep), lines)
102 # Check empty output.
103 if not splitted_lines:
104 sys.stderr.write("vlmc showmapped returned empty output")
107 # Check showmapped header line, to determine number of fields.
108 field_cnt = len(splitted_lines[0])
109 if field_cnt != allfields:
110 sys.stderr.write("Cannot parse vlmc showmapped output because its format"
111 " seems to have changed; expected %s fields, found %s" %
112 (allfields, field_cnt)
116 filter(lambda l: len(l) == allfields and l[volumefield] == volume_name,
119 if len(matched_lines) > 1:
120 sys.stderr.write("The vlmc volume %s is mapped more than once."
121 " This shouldn't happen, try to unmap the extra"
122 " devices manually." % volume_name)
126 # vlmc block device found. Return it.
127 dev = matched_lines[0][devicefield]
130 # The given volume is not mapped.
134 """Map an existing vlmc Image to a block device
136 This function maps an existing vlmc Image to a block device
137 e.g. /dev/xsegbd{X} and returns the device path. If the mapping
138 already exists, it returns the corresponding device path.
143 # Check if the mapping already exists
144 cmd = ["vlmc", "showmapped"]
145 result = utils.RunCmd(cmd)
147 sys.stderr.write("vlmc showmapped failed (%s): %s" %
148 (result.fail_reason, result.output))
151 dev = _ParseVlmcShowmappedOutput(result.output, name)
153 # The mapping exists. Return it.
154 sys.stdout.write("%s" % str(dev))
157 # The mapping doesn't exist. Create it.
158 map_cmd = ["vlmc", "map", name]
159 result = utils.RunCmd(map_cmd)
161 sys.stderr.write("vlmc map failed (%s): %s" %
162 (result.fail_reason, result.output))
165 # Find the corresponding vlmc device.
166 showmap_cmd = ["vlmc", "showmapped"]
167 result = utils.RunCmd(showmap_cmd)
169 sys.stderr.write("vlmc map succeeded, but showmapped failed (%s): %s" %
170 (result.fail_reason, result.output))
173 dev = _ParseVlmcShowmappedOutput(result.output, name)
176 sys.stderr.write("vlmc map succeeded, but could not find the vlmc block"
177 " device in output of showmapped, for volume: %s" % name)
179 # The device was successfully mapped. Return it.
180 sys.stdout.write("%s" % str(dev))
184 """Unmap a vlmc device from the Image it is mapped to
186 This function unmaps an vlmc device from the Image it is mapped to.
187 It is idempotent if the mapping doesn't exist at all.
191 # Check if the mapping already exists
192 cmd = ["vlmc", "showmapped"]
193 result = utils.RunCmd(cmd)
195 sys.stderr.write("vlmc showmapped failed (%s): %s" %
196 (result.fail_reason, result.output))
199 dev = _ParseVlmcShowmappedOutput(result.output, name)
202 # The mapping exists. Unmap the vlmc device.
203 unmap_cmd = ["vlmc", "unmap", "%s" % dev]
204 result = utils.RunCmd(unmap_cmd)
206 sys.stderr.write("vlmc unmap failed (%s): %s",
207 result.fail_reason, result.output)
212 """Grow an existing vlmc Image
216 cmd = ["vlmc", "resize", "%s" % name, "--size", "%s" % size]
218 result = utils.RunCmd(cmd)
220 sys.stderr.write('vlmc resize failed (%s): %s\n' %
221 (result.fail_reason, result.output))
227 """ Delete a vlmc Image
232 cmd = ["vlmc", "rm", "%s" % name]
234 result = utils.RunCmd(cmd)
236 sys.stderr.write("Can't remove Image %s from cluster with vlmc rm: "
238 (name, result.fail_reason, result.output))
249 sys.stderr.write("Wrong environment. Aborting...\n")
260 }[os.path.basename(sys.argv[0])](env)
262 sys.stderr.write("Op not supported")
265 if __name__ == "__main__":