]> git.proxmox.com Git - ceph.git/blob - ceph/src/ceph-volume/ceph_volume/util/prepare.py
update sources to v12.1.3
[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 create_path(osd_id):
61 system.mkdir_p('/var/lib/ceph/osd/%s-%s' % (conf.cluster, osd_id))
62
63
64 def format_device(device):
65 # only supports xfs
66 command = ['sudo', 'mkfs', '-t', 'xfs']
67
68 # get the mkfs options if any for xfs,
69 # fallback to the default options defined in constants.mkfs
70 flags = conf.ceph.get_list(
71 'osd',
72 'osd_mkfs_options_xfs',
73 default=constants.mkfs.get('xfs'),
74 split=' ',
75 )
76
77 # always force
78 if '-f' not in flags:
79 flags.insert(0, '-f')
80
81 command.extend(flags)
82 command.append(device)
83 process.run(command)
84
85
86 def mount_osd(device, osd_id):
87 destination = '/var/lib/ceph/osd/%s-%s' % (conf.cluster, osd_id)
88 command = ['sudo', 'mount', '-t', 'xfs', '-o']
89 flags = conf.ceph.get_list(
90 'osd',
91 'osd_mount_options_xfs',
92 default=constants.mount.get('xfs'),
93 split=' ',
94 )
95 command.append(flags)
96 command.append(device)
97 command.append(destination)
98 process.run(command)
99
100
101 def link_journal(journal_device, osd_id):
102 journal_path = '/var/lib/ceph/osd/%s-%s/journal' % (
103 conf.cluster,
104 osd_id
105 )
106 command = ['sudo', 'ln', '-s', journal_device, journal_path]
107 process.run(command)
108
109
110 def get_monmap(osd_id):
111 """
112 Before creating the OSD files, a monmap needs to be retrieved so that it
113 can be used to tell the monitor(s) about the new OSD. A call will look like::
114
115 ceph --cluster ceph --name client.bootstrap-osd \
116 --keyring /var/lib/ceph/bootstrap-osd/ceph.keyring \
117 mon getmap -o /var/lib/ceph/osd/ceph-0/activate.monmap
118 """
119 path = '/var/lib/ceph/osd/%s-%s/' % (conf.cluster, osd_id)
120 bootstrap_keyring = '/var/lib/ceph/bootstrap-osd/%s.keyring' % conf.cluster
121 monmap_destination = os.path.join(path, 'activate.monmap')
122
123 process.run([
124 'sudo',
125 'ceph',
126 '--cluster', conf.cluster,
127 '--name', 'client.bootstrap-osd',
128 '--keyring', bootstrap_keyring,
129 'mon', 'getmap', '-o', monmap_destination
130 ])
131
132
133 def osd_mkfs(osd_id, fsid):
134 """
135 Create the files for the OSD to function. A normal call will look like:
136
137 ceph-osd --cluster ceph --mkfs --mkkey -i 0 \
138 --monmap /var/lib/ceph/osd/ceph-0/activate.monmap \
139 --osd-data /var/lib/ceph/osd/ceph-0 \
140 --osd-journal /var/lib/ceph/osd/ceph-0/journal \
141 --osd-uuid 8d208665-89ae-4733-8888-5d3bfbeeec6c \
142 --keyring /var/lib/ceph/osd/ceph-0/keyring \
143 --setuser ceph --setgroup ceph
144
145 """
146 path = '/var/lib/ceph/osd/%s-%s/' % (conf.cluster, osd_id)
147 monmap = os.path.join(path, 'activate.monmap')
148 journal = os.path.join(path, 'journal')
149
150 system.chown(journal)
151 system.chown(path)
152
153 process.run([
154 'sudo',
155 'ceph-osd',
156 '--cluster', conf.cluster,
157 '--mkfs',
158 '-i', osd_id,
159 '--monmap', monmap,
160 '--osd-data', path,
161 '--osd-journal', journal,
162 '--osd-uuid', fsid,
163 '--setuser', 'ceph',
164 '--setgroup', 'ceph'
165 ])