]> git.proxmox.com Git - ceph.git/blob - ceph/src/ceph-volume/ceph_volume/devices/lvm/activate.py
update sources to 12.2.10
[ceph.git] / ceph / src / ceph-volume / ceph_volume / devices / lvm / activate.py
1 from __future__ import print_function
2 import argparse
3 import logging
4 import os
5 from textwrap import dedent
6 from ceph_volume import process, conf, decorators, terminal, __release__, configuration
7 from ceph_volume.util import system, disk
8 from ceph_volume.util import prepare as prepare_utils
9 from ceph_volume.util import encryption as encryption_utils
10 from ceph_volume.systemd import systemctl
11 from ceph_volume.api import lvm as api
12 from .listing import direct_report
13
14
15 logger = logging.getLogger(__name__)
16
17
18 def activate_filestore(lvs, no_systemd=False):
19 # find the osd
20 osd_lv = lvs.get(lv_tags={'ceph.type': 'data'})
21 if not osd_lv:
22 raise RuntimeError('Unable to find a data LV for filestore activation')
23 is_encrypted = osd_lv.tags.get('ceph.encrypted', '0') == '1'
24 is_vdo = osd_lv.tags.get('ceph.vdo', '0')
25
26 osd_id = osd_lv.tags['ceph.osd_id']
27 configuration.load_ceph_conf_path(osd_lv.tags['ceph.cluster_name'])
28 configuration.load()
29 # it may have a volume with a journal
30 osd_journal_lv = lvs.get(lv_tags={'ceph.type': 'journal'})
31 # TODO: add sensible error reporting if this is ever the case
32 # blow up with a KeyError if this doesn't exist
33 osd_fsid = osd_lv.tags['ceph.osd_fsid']
34 if not osd_journal_lv:
35 # must be a disk partition, by quering blkid by the uuid we are ensuring that the
36 # device path is always correct
37 journal_uuid = osd_lv.tags['ceph.journal_uuid']
38 osd_journal = disk.get_device_from_partuuid(journal_uuid)
39 else:
40 journal_uuid = osd_journal_lv.lv_uuid
41 osd_journal = osd_lv.tags['ceph.journal_device']
42
43 if not osd_journal:
44 raise RuntimeError('unable to detect an lv or device journal for OSD %s' % osd_id)
45
46 # this is done here, so that previous checks that ensure path availability
47 # and correctness can still be enforced, and report if any issues are found
48 if is_encrypted:
49 lockbox_secret = osd_lv.tags['ceph.cephx_lockbox_secret']
50 # this keyring writing is idempotent
51 encryption_utils.write_lockbox_keyring(osd_id, osd_fsid, lockbox_secret)
52 dmcrypt_secret = encryption_utils.get_dmcrypt_key(osd_id, osd_fsid)
53 encryption_utils.luks_open(dmcrypt_secret, osd_lv.lv_path, osd_lv.lv_uuid)
54 encryption_utils.luks_open(dmcrypt_secret, osd_journal, journal_uuid)
55
56 osd_journal = '/dev/mapper/%s' % journal_uuid
57 source = '/dev/mapper/%s' % osd_lv.lv_uuid
58 else:
59 source = osd_lv.lv_path
60
61 # mount the osd
62 destination = '/var/lib/ceph/osd/%s-%s' % (conf.cluster, osd_id)
63 if not system.device_is_mounted(source, destination=destination):
64 prepare_utils.mount_osd(source, osd_id, is_vdo=is_vdo)
65
66 # always re-do the symlink regardless if it exists, so that the journal
67 # device path that may have changed can be mapped correctly every time
68 destination = '/var/lib/ceph/osd/%s-%s/journal' % (conf.cluster, osd_id)
69 process.run(['ln', '-snf', osd_journal, destination])
70
71 # make sure that the journal has proper permissions
72 system.chown(osd_journal)
73
74 if no_systemd is False:
75 # enable the ceph-volume unit for this OSD
76 systemctl.enable_volume(osd_id, osd_fsid, 'lvm')
77
78 # enable the OSD
79 systemctl.enable_osd(osd_id)
80
81 # start the OSD
82 systemctl.start_osd(osd_id)
83 terminal.success("ceph-volume lvm activate successful for osd ID: %s" % osd_id)
84
85
86 def get_osd_device_path(osd_lv, lvs, device_type, dmcrypt_secret=None):
87 """
88 ``device_type`` can be one of ``db``, ``wal`` or ``block`` so that
89 we can query ``lvs`` (a ``Volumes`` object) and fallback to querying the uuid
90 if that is not present.
91
92 Return a path if possible, failing to do that a ``None``, since some of these devices
93 are optional
94 """
95 osd_lv = lvs.get(lv_tags={'ceph.type': 'block'})
96 is_encrypted = osd_lv.tags.get('ceph.encrypted', '0') == '1'
97 logger.debug('Found block device (%s) with encryption: %s', osd_lv.name, is_encrypted)
98 uuid_tag = 'ceph.%s_uuid' % device_type
99 device_uuid = osd_lv.tags.get(uuid_tag)
100 if not device_uuid:
101 return None
102
103 device_lv = lvs.get(lv_uuid=device_uuid)
104 if device_lv:
105 if is_encrypted:
106 encryption_utils.luks_open(dmcrypt_secret, device_lv.lv_path, device_uuid)
107 return '/dev/mapper/%s' % device_uuid
108 return device_lv.lv_path
109 else:
110 # this could be a regular device, so query it with blkid
111 physical_device = disk.get_device_from_partuuid(device_uuid)
112 if physical_device and is_encrypted:
113 encryption_utils.luks_open(dmcrypt_secret, physical_device, device_uuid)
114 return '/dev/mapper/%s' % device_uuid
115 return physical_device or None
116 return None
117
118
119 def activate_bluestore(lvs, no_systemd=False):
120 # find the osd
121 osd_lv = lvs.get(lv_tags={'ceph.type': 'block'})
122 if not osd_lv:
123 raise RuntimeError('could not find a bluestore OSD to activate')
124 is_encrypted = osd_lv.tags.get('ceph.encrypted', '0') == '1'
125 dmcrypt_secret = None
126 osd_id = osd_lv.tags['ceph.osd_id']
127 conf.cluster = osd_lv.tags['ceph.cluster_name']
128 osd_fsid = osd_lv.tags['ceph.osd_fsid']
129
130 # mount on tmpfs the osd directory
131 osd_path = '/var/lib/ceph/osd/%s-%s' % (conf.cluster, osd_id)
132 if not system.path_is_mounted(osd_path):
133 # mkdir -p and mount as tmpfs
134 prepare_utils.create_osd_path(osd_id, tmpfs=True)
135 # XXX This needs to be removed once ceph-bluestore-tool can deal with
136 # symlinks that exist in the osd dir
137 for link_name in ['block', 'block.db', 'block.wal']:
138 link_path = os.path.join(osd_path, link_name)
139 if os.path.exists(link_path):
140 os.unlink(os.path.join(osd_path, link_name))
141 # encryption is handled here, before priming the OSD dir
142 if is_encrypted:
143 osd_lv_path = '/dev/mapper/%s' % osd_lv.lv_uuid
144 lockbox_secret = osd_lv.tags['ceph.cephx_lockbox_secret']
145 encryption_utils.write_lockbox_keyring(osd_id, osd_fsid, lockbox_secret)
146 dmcrypt_secret = encryption_utils.get_dmcrypt_key(osd_id, osd_fsid)
147 encryption_utils.luks_open(dmcrypt_secret, osd_lv.lv_path, osd_lv.lv_uuid)
148 else:
149 osd_lv_path = osd_lv.lv_path
150
151 db_device_path = get_osd_device_path(osd_lv, lvs, 'db', dmcrypt_secret=dmcrypt_secret)
152 wal_device_path = get_osd_device_path(osd_lv, lvs, 'wal', dmcrypt_secret=dmcrypt_secret)
153
154 # Once symlinks are removed, the osd dir can be 'primed again.
155 prime_command = [
156 'ceph-bluestore-tool', '--cluster=%s' % conf.cluster,
157 'prime-osd-dir', '--dev', osd_lv_path,
158 '--path', osd_path]
159
160 if __release__ != "luminous":
161 # mon-config changes are not available in Luminous
162 prime_command.append('--no-mon-config')
163
164 process.run(prime_command)
165 # always re-do the symlink regardless if it exists, so that the block,
166 # block.wal, and block.db devices that may have changed can be mapped
167 # correctly every time
168 process.run(['ln', '-snf', osd_lv_path, os.path.join(osd_path, 'block')])
169 system.chown(os.path.join(osd_path, 'block'))
170 system.chown(osd_path)
171 if db_device_path:
172 destination = os.path.join(osd_path, 'block.db')
173 process.run(['ln', '-snf', db_device_path, destination])
174 system.chown(db_device_path)
175 system.chown(destination)
176 if wal_device_path:
177 destination = os.path.join(osd_path, 'block.wal')
178 process.run(['ln', '-snf', wal_device_path, destination])
179 system.chown(wal_device_path)
180 system.chown(destination)
181
182 if no_systemd is False:
183 # enable the ceph-volume unit for this OSD
184 systemctl.enable_volume(osd_id, osd_fsid, 'lvm')
185
186 # enable the OSD
187 systemctl.enable_osd(osd_id)
188
189 # start the OSD
190 systemctl.start_osd(osd_id)
191 terminal.success("ceph-volume lvm activate successful for osd ID: %s" % osd_id)
192
193
194 class Activate(object):
195
196 help = 'Discover and mount the LVM device associated with an OSD ID and start the Ceph OSD'
197
198 def __init__(self, argv):
199 self.argv = argv
200
201 @decorators.needs_root
202 def activate_all(self, args):
203 listed_osds = direct_report()
204 osds = {}
205 for osd_id, devices in listed_osds.items():
206 # the metadata for all devices in each OSD will contain
207 # the FSID which is required for activation
208 for device in devices:
209 fsid = device.get('tags', {}).get('ceph.osd_fsid')
210 if fsid:
211 osds[fsid] = osd_id
212 break
213 if not osds:
214 terminal.warning('Was unable to find any OSDs to activate')
215 terminal.warning('Verify OSDs are present with "ceph-volume lvm list"')
216 return
217 for osd_fsid, osd_id in osds.items():
218 if systemctl.osd_is_active(osd_id):
219 terminal.warning(
220 'OSD ID %s FSID %s process is active. Skipping activation' % (osd_id, osd_fsid)
221 )
222 else:
223 terminal.info('Activating OSD ID %s FSID %s' % (osd_id, osd_fsid))
224 self.activate(args, osd_id=osd_id, osd_fsid=osd_fsid)
225
226 @decorators.needs_root
227 def activate(self, args, osd_id=None, osd_fsid=None):
228 """
229 :param args: The parsed arguments coming from the CLI
230 :param osd_id: When activating all, this gets populated with an existing OSD ID
231 :param osd_fsid: When activating all, this gets populated with an existing OSD FSID
232 """
233 osd_id = osd_id if osd_id is not None else args.osd_id
234 osd_fsid = osd_fsid if osd_fsid is not None else args.osd_fsid
235
236 lvs = api.Volumes()
237 # filter them down for the OSD ID and FSID we need to activate
238 if osd_id and osd_fsid:
239 lvs.filter(lv_tags={'ceph.osd_id': osd_id, 'ceph.osd_fsid': osd_fsid})
240 elif osd_fsid and not osd_id:
241 lvs.filter(lv_tags={'ceph.osd_fsid': osd_fsid})
242 if not lvs:
243 raise RuntimeError('could not find osd.%s with fsid %s' % (osd_id, osd_fsid))
244 # This argument is only available when passed in directly or via
245 # systemd, not when ``create`` is being used
246 if getattr(args, 'auto_detect_objectstore', False):
247 logger.info('auto detecting objectstore')
248 # may get multiple lvs, so can't do lvs.get() calls here
249 for lv in lvs:
250 has_journal = lv.tags.get('ceph.journal_uuid')
251 if has_journal:
252 logger.info('found a journal associated with the OSD, assuming filestore')
253 return activate_filestore(lvs, no_systemd=args.no_systemd)
254 logger.info('unable to find a journal associated with the OSD, assuming bluestore')
255 return activate_bluestore(lvs, no_systemd=args.no_systemd)
256 if args.bluestore:
257 activate_bluestore(lvs, no_systemd=args.no_systemd)
258 elif args.filestore:
259 activate_filestore(lvs, no_systemd=args.no_systemd)
260
261 def main(self):
262 sub_command_help = dedent("""
263 Activate OSDs by discovering them with LVM and mounting them in their
264 appropriate destination:
265
266 ceph-volume lvm activate {ID} {FSID}
267
268 The lvs associated with the OSD need to have been prepared previously,
269 so that all needed tags and metadata exist.
270
271 When migrating OSDs, or a multiple-osd activation is needed, the
272 ``--all`` flag can be used instead of the individual ID and FSID:
273
274 ceph-volume lvm activate --all
275
276 """)
277 parser = argparse.ArgumentParser(
278 prog='ceph-volume lvm activate',
279 formatter_class=argparse.RawDescriptionHelpFormatter,
280 description=sub_command_help,
281 )
282
283 parser.add_argument(
284 'osd_id',
285 metavar='ID',
286 nargs='?',
287 help='The ID of the OSD, usually an integer, like 0'
288 )
289 parser.add_argument(
290 'osd_fsid',
291 metavar='FSID',
292 nargs='?',
293 help='The FSID of the OSD, similar to a SHA1'
294 )
295 parser.add_argument(
296 '--auto-detect-objectstore',
297 action='store_true',
298 help='Autodetect the objectstore by inspecting the OSD',
299 )
300 parser.add_argument(
301 '--bluestore',
302 action='store_true',
303 help='bluestore objectstore (default)',
304 )
305 parser.add_argument(
306 '--filestore',
307 action='store_true',
308 help='filestore objectstore',
309 )
310 parser.add_argument(
311 '--all',
312 dest='activate_all',
313 action='store_true',
314 help='Activate all OSDs found in the system',
315 )
316 parser.add_argument(
317 '--no-systemd',
318 dest='no_systemd',
319 action='store_true',
320 help='Skip creating and enabling systemd units and starting OSD services',
321 )
322 if len(self.argv) == 0:
323 print(sub_command_help)
324 return
325 args = parser.parse_args(self.argv)
326 # Default to bluestore here since defaulting it in add_argument may
327 # cause both to be True
328 if not args.bluestore and not args.filestore:
329 args.bluestore = True
330 if args.activate_all:
331 self.activate_all(args)
332 else:
333 self.activate(args)