]> git.proxmox.com Git - ceph.git/blob - ceph/src/ceph-volume/ceph_volume/util/prepare.py
update sources to 12.2.2
[ceph.git] / ceph / src / ceph-volume / ceph_volume / util / prepare.py
1 """
2 These utilities for prepare provide all the pieces needed to prepare a device
3 but also a compounded ("single call") helper to do them in order. Some plugins
4 may want to change some part of the process, while others might want to consume
5 the single-call helper
6 """
7 import os
8 import logging
9 from ceph_volume import process, conf
10 from ceph_volume.util import system, constants
11
12 logger = logging.getLogger(__name__)
13
14
15 def create_key():
16 stdout, stderr, returncode = process.call(['ceph-authtool', '--gen-print-key'])
17 if returncode != 0:
18 raise RuntimeError('Unable to generate a new auth key')
19 return ' '.join(stdout).strip()
20
21
22 def write_keyring(osd_id, secret):
23 # FIXME this only works for cephx, but there will be other types of secrets
24 # later
25 osd_keyring = '/var/lib/ceph/osd/%s-%s/keyring' % (conf.cluster, osd_id)
26 process.run(
27 [
28 'ceph-authtool', osd_keyring,
29 '--create-keyring',
30 '--name', 'osd.%s' % str(osd_id),
31 '--add-key', secret
32 ])
33 system.chown(osd_keyring)
34 # TODO: do the restorecon dance on the osd_keyring path
35
36
37 def create_id(fsid, json_secrets):
38 """
39 :param fsid: The osd fsid to create, always required
40 :param json_secrets: a json-ready object with whatever secrets are wanted
41 to be passed to the monitor
42 """
43 bootstrap_keyring = '/var/lib/ceph/bootstrap-osd/%s.keyring' % conf.cluster
44 stdout, stderr, returncode = process.call(
45 [
46 'ceph',
47 '--cluster', conf.cluster,
48 '--name', 'client.bootstrap-osd',
49 '--keyring', bootstrap_keyring,
50 '-i', '-',
51 'osd', 'new', fsid
52 ],
53 stdin=json_secrets
54 )
55 if returncode != 0:
56 raise RuntimeError('Unable to create a new OSD id')
57 return ' '.join(stdout).strip()
58
59
60 def mount_tmpfs(path):
61 process.run([
62 'sudo',
63 'mount',
64 '-t',
65 'tmpfs', 'tmpfs',
66 path
67 ])
68
69
70 def create_osd_path(osd_id, tmpfs=False):
71 path = '/var/lib/ceph/osd/%s-%s' % (conf.cluster, osd_id)
72 system.mkdir_p('/var/lib/ceph/osd/%s-%s' % (conf.cluster, osd_id))
73 if tmpfs:
74 mount_tmpfs(path)
75
76
77 def format_device(device):
78 # only supports xfs
79 command = ['sudo', 'mkfs', '-t', 'xfs']
80
81 # get the mkfs options if any for xfs,
82 # fallback to the default options defined in constants.mkfs
83 flags = conf.ceph.get_list(
84 'osd',
85 'osd_mkfs_options_xfs',
86 default=constants.mkfs.get('xfs'),
87 split=' ',
88 )
89
90 # always force
91 if '-f' not in flags:
92 flags.insert(0, '-f')
93
94 command.extend(flags)
95 command.append(device)
96 process.run(command)
97
98
99 def mount_osd(device, osd_id):
100 destination = '/var/lib/ceph/osd/%s-%s' % (conf.cluster, osd_id)
101 command = ['sudo', 'mount', '-t', 'xfs', '-o']
102 flags = conf.ceph.get_list(
103 'osd',
104 'osd_mount_options_xfs',
105 default=constants.mount.get('xfs'),
106 split=' ',
107 )
108 command.append(flags)
109 command.append(device)
110 command.append(destination)
111 process.run(command)
112
113
114 def _link_device(device, device_type, osd_id):
115 """
116 Allow linking any device type in an OSD directory. ``device`` must the be
117 source, with an absolute path and ``device_type`` will be the destination
118 name, like 'journal', or 'block'
119 """
120 device_path = '/var/lib/ceph/osd/%s-%s/%s' % (
121 conf.cluster,
122 osd_id,
123 device_type
124 )
125 command = ['sudo', 'ln', '-s', device, device_path]
126 system.chown(device)
127
128 process.run(command)
129
130
131 def link_journal(journal_device, osd_id):
132 _link_device(journal_device, 'journal', osd_id)
133
134
135 def link_block(block_device, osd_id):
136 _link_device(block_device, 'block', osd_id)
137
138
139 def link_wal(wal_device, osd_id):
140 _link_device(wal_device, 'block.wal', osd_id)
141
142
143 def link_db(db_device, osd_id):
144 _link_device(db_device, 'block.db', osd_id)
145
146
147 def get_monmap(osd_id):
148 """
149 Before creating the OSD files, a monmap needs to be retrieved so that it
150 can be used to tell the monitor(s) about the new OSD. A call will look like::
151
152 ceph --cluster ceph --name client.bootstrap-osd \
153 --keyring /var/lib/ceph/bootstrap-osd/ceph.keyring \
154 mon getmap -o /var/lib/ceph/osd/ceph-0/activate.monmap
155 """
156 path = '/var/lib/ceph/osd/%s-%s/' % (conf.cluster, osd_id)
157 bootstrap_keyring = '/var/lib/ceph/bootstrap-osd/%s.keyring' % conf.cluster
158 monmap_destination = os.path.join(path, 'activate.monmap')
159
160 process.run([
161 'sudo',
162 'ceph',
163 '--cluster', conf.cluster,
164 '--name', 'client.bootstrap-osd',
165 '--keyring', bootstrap_keyring,
166 'mon', 'getmap', '-o', monmap_destination
167 ])
168
169
170 def osd_mkfs_bluestore(osd_id, fsid, keyring=None, wal=False, db=False):
171 """
172 Create the files for the OSD to function. A normal call will look like:
173
174 ceph-osd --cluster ceph --mkfs --mkkey -i 0 \
175 --monmap /var/lib/ceph/osd/ceph-0/activate.monmap \
176 --osd-data /var/lib/ceph/osd/ceph-0 \
177 --osd-uuid 8d208665-89ae-4733-8888-5d3bfbeeec6c \
178 --keyring /var/lib/ceph/osd/ceph-0/keyring \
179 --setuser ceph --setgroup ceph
180
181 In some cases it is required to use the keyring, when it is passed in as
182 a keywork argument it is used as part of the ceph-osd command
183 """
184 path = '/var/lib/ceph/osd/%s-%s/' % (conf.cluster, osd_id)
185 monmap = os.path.join(path, 'activate.monmap')
186
187 system.chown(path)
188
189 base_command = [
190 'sudo',
191 'ceph-osd',
192 '--cluster', conf.cluster,
193 # undocumented flag, sets the `type` file to contain 'bluestore'
194 '--osd-objectstore', 'bluestore',
195 '--mkfs',
196 '-i', osd_id,
197 '--monmap', monmap,
198 ]
199
200 supplementary_command = [
201 '--osd-data', path,
202 '--osd-uuid', fsid,
203 '--setuser', 'ceph',
204 '--setgroup', 'ceph'
205 ]
206
207 if keyring is not None:
208 base_command.extend(['--key', keyring])
209
210 if wal:
211 base_command.extend(
212 ['--bluestore-block-wal-path', wal]
213 )
214 system.chown(wal)
215
216 if db:
217 base_command.extend(
218 ['--bluestore-block-db-path', db]
219 )
220 system.chown(db)
221
222 command = base_command + supplementary_command
223
224 process.run(command, obfuscate='--key')
225
226
227 def osd_mkfs_filestore(osd_id, fsid):
228 """
229 Create the files for the OSD to function. A normal call will look like:
230
231 ceph-osd --cluster ceph --mkfs --mkkey -i 0 \
232 --monmap /var/lib/ceph/osd/ceph-0/activate.monmap \
233 --osd-data /var/lib/ceph/osd/ceph-0 \
234 --osd-journal /var/lib/ceph/osd/ceph-0/journal \
235 --osd-uuid 8d208665-89ae-4733-8888-5d3bfbeeec6c \
236 --keyring /var/lib/ceph/osd/ceph-0/keyring \
237 --setuser ceph --setgroup ceph
238
239 """
240 path = '/var/lib/ceph/osd/%s-%s/' % (conf.cluster, osd_id)
241 monmap = os.path.join(path, 'activate.monmap')
242 journal = os.path.join(path, 'journal')
243
244 system.chown(journal)
245 system.chown(path)
246
247 process.run([
248 'sudo',
249 'ceph-osd',
250 '--cluster', conf.cluster,
251 # undocumented flag, sets the `type` file to contain 'filestore'
252 '--osd-objectstore', 'filestore',
253 '--mkfs',
254 '-i', osd_id,
255 '--monmap', monmap,
256 '--osd-data', path,
257 '--osd-journal', journal,
258 '--osd-uuid', fsid,
259 '--setuser', 'ceph',
260 '--setgroup', 'ceph'
261 ])