]>
Commit | Line | Data |
---|---|---|
d2e6a577 FG |
1 | from __future__ import print_function |
2 | import json | |
b32b8144 | 3 | import logging |
d2e6a577 FG |
4 | from textwrap import dedent |
5 | from ceph_volume.util import prepare as prepare_utils | |
b32b8144 | 6 | from ceph_volume.util import encryption as encryption_utils |
181888fb | 7 | from ceph_volume.util import system, disk |
3a9019d9 | 8 | from ceph_volume.util.arg_validators import exclude_group_options |
181888fb | 9 | from ceph_volume import conf, decorators, terminal |
3efd9988 | 10 | from ceph_volume.api import lvm as api |
b32b8144 | 11 | from .common import prepare_parser, rollback_osd |
d2e6a577 FG |
12 | |
13 | ||
b32b8144 FG |
14 | logger = logging.getLogger(__name__) |
15 | ||
16 | ||
17 | def prepare_dmcrypt(key, device, device_type, tags): | |
18 | """ | |
19 | Helper for devices that are encrypted. The operations needed for | |
20 | block, db, wal, or data/journal devices are all the same | |
21 | """ | |
22 | if not device: | |
23 | return '' | |
24 | tag_name = 'ceph.%s_uuid' % device_type | |
25 | uuid = tags[tag_name] | |
26 | # format data device | |
27 | encryption_utils.luks_format( | |
28 | key, | |
29 | device | |
30 | ) | |
31 | encryption_utils.luks_open( | |
32 | key, | |
33 | device, | |
34 | uuid | |
35 | ) | |
36 | ||
37 | return '/dev/mapper/%s' % uuid | |
38 | ||
39 | ||
40 | def prepare_filestore(device, journal, secrets, tags, osd_id, fsid): | |
d2e6a577 | 41 | """ |
3efd9988 | 42 | :param device: The name of the logical volume to work with |
d2e6a577 FG |
43 | :param journal: similar to device but can also be a regular/plain disk |
44 | :param secrets: A dict with the secrets needed to create the osd (e.g. cephx) | |
45 | :param id_: The OSD id | |
46 | :param fsid: The OSD fsid, also known as the OSD UUID | |
47 | """ | |
48 | cephx_secret = secrets.get('cephx_secret', prepare_utils.create_key()) | |
d2e6a577 | 49 | |
b32b8144 FG |
50 | # encryption-only operations |
51 | if secrets.get('dmcrypt_key'): | |
52 | # format and open ('decrypt' devices) and re-assign the device and journal | |
53 | # variables so that the rest of the process can use the mapper paths | |
54 | key = secrets['dmcrypt_key'] | |
55 | device = prepare_dmcrypt(key, device, 'data', tags) | |
56 | journal = prepare_dmcrypt(key, journal, 'journal', tags) | |
57 | ||
94b18763 FG |
58 | # vdo detection |
59 | is_vdo = api.is_vdo(device) | |
d2e6a577 | 60 | # create the directory |
3efd9988 | 61 | prepare_utils.create_osd_path(osd_id) |
d2e6a577 FG |
62 | # format the device |
63 | prepare_utils.format_device(device) | |
64 | # mount the data device | |
94b18763 | 65 | prepare_utils.mount_osd(device, osd_id, is_vdo=is_vdo) |
d2e6a577 FG |
66 | # symlink the journal |
67 | prepare_utils.link_journal(journal, osd_id) | |
68 | # get the latest monmap | |
69 | prepare_utils.get_monmap(osd_id) | |
70 | # prepare the osd filesystem | |
1adf2230 | 71 | prepare_utils.osd_mkfs_filestore(osd_id, fsid, cephx_secret) |
d2e6a577 FG |
72 | # write the OSD keyring if it doesn't exist already |
73 | prepare_utils.write_keyring(osd_id, cephx_secret) | |
b32b8144 FG |
74 | if secrets.get('dmcrypt_key'): |
75 | # if the device is going to get activated right away, this can be done | |
76 | # here, otherwise it will be recreated | |
77 | encryption_utils.write_lockbox_keyring( | |
78 | osd_id, | |
79 | fsid, | |
80 | tags['ceph.cephx_lockbox_secret'] | |
81 | ) | |
d2e6a577 FG |
82 | |
83 | ||
b32b8144 | 84 | def prepare_bluestore(block, wal, db, secrets, tags, osd_id, fsid): |
3efd9988 FG |
85 | """ |
86 | :param block: The name of the logical volume for the bluestore data | |
87 | :param wal: a regular/plain disk or logical volume, to be used for block.wal | |
88 | :param db: a regular/plain disk or logical volume, to be used for block.db | |
89 | :param secrets: A dict with the secrets needed to create the osd (e.g. cephx) | |
90 | :param id_: The OSD id | |
91 | :param fsid: The OSD fsid, also known as the OSD UUID | |
92 | """ | |
93 | cephx_secret = secrets.get('cephx_secret', prepare_utils.create_key()) | |
b32b8144 FG |
94 | # encryption-only operations |
95 | if secrets.get('dmcrypt_key'): | |
96 | # If encrypted, there is no need to create the lockbox keyring file because | |
97 | # bluestore re-creates the files and does not have support for other files | |
98 | # like the custom lockbox one. This will need to be done on activation. | |
99 | # format and open ('decrypt' devices) and re-assign the device and journal | |
100 | # variables so that the rest of the process can use the mapper paths | |
101 | key = secrets['dmcrypt_key'] | |
102 | block = prepare_dmcrypt(key, block, 'block', tags) | |
103 | wal = prepare_dmcrypt(key, wal, 'wal', tags) | |
104 | db = prepare_dmcrypt(key, db, 'db', tags) | |
3efd9988 | 105 | |
3efd9988 FG |
106 | # create the directory |
107 | prepare_utils.create_osd_path(osd_id, tmpfs=True) | |
108 | # symlink the block | |
109 | prepare_utils.link_block(block, osd_id) | |
110 | # get the latest monmap | |
111 | prepare_utils.get_monmap(osd_id) | |
112 | # write the OSD keyring if it doesn't exist already | |
113 | prepare_utils.write_keyring(osd_id, cephx_secret) | |
114 | # prepare the osd filesystem | |
115 | prepare_utils.osd_mkfs_bluestore( | |
116 | osd_id, fsid, | |
117 | keyring=cephx_secret, | |
118 | wal=wal, | |
119 | db=db | |
120 | ) | |
d2e6a577 FG |
121 | |
122 | ||
123 | class Prepare(object): | |
124 | ||
125 | help = 'Format an LVM device and associate it with an OSD' | |
126 | ||
127 | def __init__(self, argv): | |
128 | self.argv = argv | |
b32b8144 | 129 | self.osd_id = None |
d2e6a577 | 130 | |
3efd9988 | 131 | def get_ptuuid(self, argument): |
181888fb FG |
132 | uuid = disk.get_partuuid(argument) |
133 | if not uuid: | |
134 | terminal.error('blkid could not detect a PARTUUID for device: %s' % argument) | |
3efd9988 | 135 | raise RuntimeError('unable to use device') |
181888fb FG |
136 | return uuid |
137 | ||
3efd9988 | 138 | def get_lv(self, argument): |
b5b8bbf5 | 139 | """ |
3efd9988 FG |
140 | Perform some parsing of the command-line value so that the process |
141 | can determine correctly if it got a device path or an lv. | |
142 | ||
143 | :param argument: The command-line value that will need to be split to | |
144 | retrieve the actual lv | |
b5b8bbf5 FG |
145 | """ |
146 | try: | |
147 | vg_name, lv_name = argument.split('/') | |
148 | except (ValueError, AttributeError): | |
149 | return None | |
150 | return api.get_lv(lv_name=lv_name, vg_name=vg_name) | |
151 | ||
3efd9988 FG |
152 | def setup_device(self, device_type, device_name, tags): |
153 | """ | |
154 | Check if ``device`` is an lv, if so, set the tags, making sure to | |
155 | update the tags with the lv_uuid and lv_path which the incoming tags | |
156 | will not have. | |
157 | ||
158 | If the device is not a logical volume, then retrieve the partition UUID | |
159 | by querying ``blkid`` | |
160 | """ | |
161 | if device_name is None: | |
162 | return '', '', tags | |
163 | tags['ceph.type'] = device_type | |
94b18763 | 164 | tags['ceph.vdo'] = api.is_vdo(device_name) |
3efd9988 FG |
165 | lv = self.get_lv(device_name) |
166 | if lv: | |
167 | uuid = lv.lv_uuid | |
168 | path = lv.lv_path | |
169 | tags['ceph.%s_uuid' % device_type] = uuid | |
170 | tags['ceph.%s_device' % device_type] = path | |
171 | lv.set_tags(tags) | |
172 | else: | |
173 | # otherwise assume this is a regular disk partition | |
174 | uuid = self.get_ptuuid(device_name) | |
175 | path = device_name | |
176 | tags['ceph.%s_uuid' % device_type] = uuid | |
177 | tags['ceph.%s_device' % device_type] = path | |
178 | return path, uuid, tags | |
179 | ||
180 | def prepare_device(self, arg, device_type, cluster_fsid, osd_fsid): | |
181 | """ | |
182 | Check if ``arg`` is a device or partition to create an LV out of it | |
183 | with a distinct volume group name, assigning LV tags on it and | |
184 | ultimately, returning the logical volume object. Failing to detect | |
185 | a device or partition will result in error. | |
186 | ||
187 | :param arg: The value of ``--data`` when parsing args | |
188 | :param device_type: Usually, either ``data`` or ``block`` (filestore vs. bluestore) | |
189 | :param cluster_fsid: The cluster fsid/uuid | |
190 | :param osd_fsid: The OSD fsid/uuid | |
191 | """ | |
192 | if disk.is_partition(arg) or disk.is_device(arg): | |
193 | # we must create a vg, and then a single lv | |
1adf2230 | 194 | vg = api.create_vg(arg) |
3efd9988 FG |
195 | lv_name = "osd-%s-%s" % (device_type, osd_fsid) |
196 | return api.create_lv( | |
197 | lv_name, | |
1adf2230 | 198 | vg.name, # the volume group |
3efd9988 FG |
199 | tags={'ceph.type': device_type}) |
200 | else: | |
201 | error = [ | |
b32b8144 FG |
202 | 'Cannot use device (%s).' % arg, |
203 | 'A vg/lv path or an existing device is needed'] | |
3efd9988 FG |
204 | raise RuntimeError(' '.join(error)) |
205 | ||
206 | raise RuntimeError('no data logical volume found with: %s' % arg) | |
207 | ||
b32b8144 FG |
208 | def safe_prepare(self, args): |
209 | """ | |
210 | An intermediate step between `main()` and `prepare()` so that we can | |
211 | capture the `self.osd_id` in case we need to rollback | |
212 | """ | |
213 | try: | |
214 | self.prepare(args) | |
215 | except Exception: | |
28e407b8 | 216 | logger.exception('lvm prepare was unable to complete') |
b32b8144 FG |
217 | logger.info('will rollback OSD ID creation') |
218 | rollback_osd(args, self.osd_id) | |
219 | raise | |
220 | terminal.success("ceph-volume lvm prepare successful for: %s" % args.data) | |
221 | ||
d2e6a577 FG |
222 | @decorators.needs_root |
223 | def prepare(self, args): | |
224 | # FIXME we don't allow re-using a keyring, we always generate one for the | |
225 | # OSD, this needs to be fixed. This could either be a file (!) or a string | |
226 | # (!!) or some flags that we would need to compound into a dict so that we | |
227 | # can convert to JSON (!!!) | |
228 | secrets = {'cephx_secret': prepare_utils.create_key()} | |
b32b8144 FG |
229 | cephx_lockbox_secret = '' |
230 | encrypted = 1 if args.dmcrypt else 0 | |
231 | cephx_lockbox_secret = '' if not encrypted else prepare_utils.create_key() | |
232 | ||
233 | if encrypted: | |
234 | secrets['dmcrypt_key'] = encryption_utils.create_dmcrypt_key() | |
235 | secrets['cephx_lockbox_secret'] = cephx_lockbox_secret | |
d2e6a577 FG |
236 | |
237 | cluster_fsid = conf.ceph.get('global', 'fsid') | |
3efd9988 | 238 | osd_fsid = args.osd_fsid or system.generate_uuid() |
b32b8144 FG |
239 | crush_device_class = args.crush_device_class |
240 | if crush_device_class: | |
241 | secrets['crush_device_class'] = crush_device_class | |
242 | # reuse a given ID if it exists, otherwise create a new ID | |
243 | self.osd_id = prepare_utils.create_id(osd_fsid, json.dumps(secrets), osd_id=args.osd_id) | |
244 | tags = { | |
245 | 'ceph.osd_fsid': osd_fsid, | |
246 | 'ceph.osd_id': self.osd_id, | |
247 | 'ceph.cluster_fsid': cluster_fsid, | |
248 | 'ceph.cluster_name': conf.cluster, | |
249 | 'ceph.crush_device_class': crush_device_class, | |
250 | } | |
d2e6a577 | 251 | if args.filestore: |
b5b8bbf5 FG |
252 | if not args.journal: |
253 | raise RuntimeError('--journal is required when using --filestore') | |
b5b8bbf5 | 254 | |
3efd9988 FG |
255 | data_lv = self.get_lv(args.data) |
256 | if not data_lv: | |
257 | data_lv = self.prepare_device(args.data, 'data', cluster_fsid, osd_fsid) | |
181888fb | 258 | |
b32b8144 FG |
259 | tags['ceph.data_device'] = data_lv.lv_path |
260 | tags['ceph.data_uuid'] = data_lv.lv_uuid | |
261 | tags['ceph.cephx_lockbox_secret'] = cephx_lockbox_secret | |
262 | tags['ceph.encrypted'] = encrypted | |
94b18763 | 263 | tags['ceph.vdo'] = api.is_vdo(data_lv.lv_path) |
3efd9988 FG |
264 | |
265 | journal_device, journal_uuid, tags = self.setup_device('journal', args.journal, tags) | |
266 | ||
267 | tags['ceph.type'] = 'data' | |
268 | data_lv.set_tags(tags) | |
d2e6a577 FG |
269 | |
270 | prepare_filestore( | |
271 | data_lv.lv_path, | |
272 | journal_device, | |
273 | secrets, | |
b32b8144 FG |
274 | tags, |
275 | self.osd_id, | |
276 | osd_fsid, | |
d2e6a577 FG |
277 | ) |
278 | elif args.bluestore: | |
3efd9988 FG |
279 | block_lv = self.get_lv(args.data) |
280 | if not block_lv: | |
281 | block_lv = self.prepare_device(args.data, 'block', cluster_fsid, osd_fsid) | |
282 | ||
b32b8144 FG |
283 | tags['ceph.block_device'] = block_lv.lv_path |
284 | tags['ceph.block_uuid'] = block_lv.lv_uuid | |
285 | tags['ceph.cephx_lockbox_secret'] = cephx_lockbox_secret | |
286 | tags['ceph.encrypted'] = encrypted | |
94b18763 | 287 | tags['ceph.vdo'] = api.is_vdo(block_lv.lv_path) |
3efd9988 FG |
288 | |
289 | wal_device, wal_uuid, tags = self.setup_device('wal', args.block_wal, tags) | |
290 | db_device, db_uuid, tags = self.setup_device('db', args.block_db, tags) | |
291 | ||
292 | tags['ceph.type'] = 'block' | |
293 | block_lv.set_tags(tags) | |
294 | ||
295 | prepare_bluestore( | |
296 | block_lv.lv_path, | |
297 | wal_device, | |
298 | db_device, | |
299 | secrets, | |
b32b8144 FG |
300 | tags, |
301 | self.osd_id, | |
302 | osd_fsid, | |
3efd9988 | 303 | ) |
d2e6a577 FG |
304 | |
305 | def main(self): | |
306 | sub_command_help = dedent(""" | |
307 | Prepare an OSD by assigning an ID and FSID, registering them with the | |
308 | cluster with an ID and FSID, formatting and mounting the volume, and | |
309 | finally by adding all the metadata to the logical volumes using LVM | |
310 | tags, so that it can later be discovered. | |
311 | ||
312 | Once the OSD is ready, an ad-hoc systemd unit will be enabled so that | |
313 | it can later get activated and the OSD daemon can get started. | |
314 | ||
315 | Most basic Usage looks like (journal will be collocated from the same volume group): | |
316 | ||
317 | ceph-volume lvm prepare --data {volume group name} | |
318 | ||
b32b8144 | 319 | Encryption is supported via dmcrypt and the --dmcrypt flag. |
d2e6a577 FG |
320 | |
321 | Example calls for supported scenarios: | |
322 | ||
323 | Dedicated volume group for Journal(s) | |
324 | ------------------------------------- | |
325 | ||
326 | Existing logical volume (lv) or device: | |
327 | ||
3efd9988 | 328 | ceph-volume lvm prepare --filestore --data {vg/lv} --journal /path/to/device |
d2e6a577 FG |
329 | |
330 | Or: | |
331 | ||
3efd9988 FG |
332 | ceph-volume lvm prepare --filestore --data {vg/lv} --journal {vg/lv} |
333 | ||
334 | Existing block device, that will be made a group and logical volume: | |
335 | ||
336 | ceph-volume lvm prepare --filestore --data /path/to/device --journal {vg/lv} | |
337 | ||
338 | Bluestore | |
339 | --------- | |
340 | ||
341 | Existing logical volume (lv): | |
342 | ||
343 | ceph-volume lvm prepare --bluestore --data {vg/lv} | |
344 | ||
345 | Existing block device, that will be made a group and logical volume: | |
d2e6a577 | 346 | |
3efd9988 | 347 | ceph-volume lvm prepare --bluestore --data /path/to/device |
d2e6a577 | 348 | |
3efd9988 | 349 | Optionally, can consume db and wal devices or logical volumes: |
d2e6a577 | 350 | |
3efd9988 | 351 | ceph-volume lvm prepare --bluestore --data {vg/lv} --block.wal {device} --block-db {vg/lv} |
d2e6a577 FG |
352 | """) |
353 | parser = prepare_parser( | |
354 | prog='ceph-volume lvm prepare', | |
355 | description=sub_command_help, | |
356 | ) | |
357 | if len(self.argv) == 0: | |
358 | print(sub_command_help) | |
359 | return | |
3a9019d9 | 360 | exclude_group_options(parser, argv=self.argv, groups=['filestore', 'bluestore']) |
d2e6a577 | 361 | args = parser.parse_args(self.argv) |
3efd9988 FG |
362 | # Default to bluestore here since defaulting it in add_argument may |
363 | # cause both to be True | |
b32b8144 | 364 | if not args.bluestore and not args.filestore: |
3efd9988 | 365 | args.bluestore = True |
b32b8144 | 366 | self.safe_prepare(args) |