/
main.go
412 lines (318 loc) · 12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* SPDX-License-Identifier: MPL-2.0
*
* Copyright (c) 2024 Damian Peckett <damian@pecke.tt>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package main
import (
"bytes"
"context"
"errors"
"fmt"
"log/slog"
"os"
"os/exec"
"os/signal"
"os/user"
"strconv"
"strings"
"syscall"
"time"
"github.com/bucket-sailor/picoceph/internal/nbd"
"github.com/bucket-sailor/picoceph/internal/tmpl"
"github.com/bucket-sailor/picoceph/internal/util"
"github.com/google/uuid"
"golang.org/x/sync/errgroup"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{}))
logger.Info("Creating ceph directories")
cephUserUid, cephGroupGid, err := cephUser()
if err != nil {
logger.Error("Could not get ceph user", "error", err)
os.Exit(1)
}
for _, dir := range []string{"/etc/ceph", "/var/lib/ceph", "/var/log/ceph"} {
if err := os.MkdirAll(dir, 0o755); err != nil {
logger.Error("Could not create directory", "error", err)
os.Exit(1)
}
if err := os.Chown(dir, cephUserUid, cephGroupGid); err != nil {
logger.Error("Could not change owner", "error", err)
os.Exit(1)
}
}
logger.Info("Writing ceph.conf")
fsid := uuid.New().String()
if err := writeCephConf(fsid); err != nil {
logger.Error("Could not write ceph.conf", "error", err)
os.Exit(1)
}
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
logger.Info("Configuring monitor")
if err := configureMonitor(ctx, fsid); err != nil {
return fmt.Errorf("could not configure monitor: %w", err)
}
logger.Info("Starting monitor")
cmd := exec.CommandContext(ctx, "ceph-mon", "-f", "-i", "a")
if out, err := cmd.CombinedOutput(); err != nil {
if strings.Contains(err.Error(), "signal: killed") {
return nil
}
return fmt.Errorf("could not start monitor: %w: %s", err, out)
}
return nil
})
g.Go(func() error {
logger.Info("Configuring manager")
if err := configureManager(ctx); err != nil {
return fmt.Errorf("could not configure manager: %w", err)
}
logger.Info("Starting manager")
cmd := exec.CommandContext(ctx, "ceph-mgr", "-f", "-i", "a")
if out, err := cmd.CombinedOutput(); err != nil {
if strings.Contains(err.Error(), "signal: killed") {
return nil
}
return fmt.Errorf("could not start manager: %w: %s", err, out)
}
return nil
})
g.Go(func() error {
logger.Info("Creating OSD device")
if err := createOSDDevice(ctx); err != nil {
return fmt.Errorf("could not create OSD device: %w", err)
}
logger.Info("Preparing OSD")
cmd := exec.CommandContext(ctx, "ceph-volume", "lvm", "create", "--no-systemd", "--data", "ceph-vg/osd-0", "--osd-id", "0")
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("could not prepare OSD device: %w: %s", err, out)
}
logger.Info("Starting OSD")
cmd = exec.CommandContext(ctx, "ceph-osd", "-f", "--id", "0")
if out, err := cmd.CombinedOutput(); err != nil {
if strings.Contains(err.Error(), "signal: killed") {
return nil
}
return fmt.Errorf("could not start OSD: %w: %s", err, out)
}
return nil
})
g.Go(func() error {
logger.Info("Configuring RADOS Gateway")
if err := configureRADOSGateway(ctx); err != nil {
return fmt.Errorf("could not configure RADOS Gateway: %w", err)
}
logger.Info("Starting RADOS Gateway")
cmd := exec.CommandContext(ctx, "radosgw", "-f", "-n", "client.radosgw.gateway")
if out, err := cmd.CombinedOutput(); err != nil {
if strings.Contains(err.Error(), "signal: killed") {
return nil
}
return fmt.Errorf("could not start RADOS Gateway: %w: %s", err, out)
}
return nil
})
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-sigCh
cancel()
}()
if err := g.Wait(); err != nil && !errors.Is(err, context.Canceled) {
logger.Error("Could not run picoceph", "error", err)
os.Exit(1)
}
}
func configureRADOSGateway(ctx context.Context) error {
if err := os.MkdirAll("/var/lib/ceph/radosgw/ceph-radosgw.gateway", 0o755); err != nil {
return fmt.Errorf("could not create directory: %w", err)
}
radosgwKeyring, err := os.Create("/var/lib/ceph/radosgw/ceph-radosgw.gateway/keyring")
if err != nil {
return fmt.Errorf("could not create keyring: %w", err)
}
defer radosgwKeyring.Close()
cephCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
cmd := exec.CommandContext(cephCtx, "ceph", "auth", "get-or-create", "client.radosgw.gateway", "osd", "allow rwx", "mon", "allow rw")
cmd.Stdout = radosgwKeyring
var out bytes.Buffer
cmd.Stderr = &out
if err := cmd.Run(); err != nil {
return fmt.Errorf("could not create keyring: %w: %s", err, out.String())
}
cephUserUid, cephGroupGid, err := cephUser()
if err != nil {
return fmt.Errorf("could not get ceph user: %w", err)
}
if err := util.ChownRecursive("/var/lib/ceph/radosgw/ceph-radosgw.gateway", cephUserUid, cephGroupGid); err != nil {
return fmt.Errorf("could not change owner: %w", err)
}
return nil
}
func configureManager(ctx context.Context) error {
if err := os.MkdirAll("/var/lib/ceph/mgr/ceph-a", 0o755); err != nil {
return fmt.Errorf("could not create directory: %w", err)
}
mgrKeyring, err := os.Create("/var/lib/ceph/mgr/ceph-a/keyring")
if err != nil {
return fmt.Errorf("could not create keyring: %w", err)
}
defer mgrKeyring.Close()
cephCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
cmd := exec.CommandContext(cephCtx, "ceph", "auth", "get-or-create", "mgr.a", "mon", "allow profile mgr", "osd", "allow *", "mds", "allow *")
cmd.Stdout = mgrKeyring
var out bytes.Buffer
cmd.Stderr = &out
if err := cmd.Run(); err != nil {
return fmt.Errorf("could not create keyring: %w: %s", err, out.String())
}
cephUserUid, cephGroupGid, err := cephUser()
if err != nil {
return fmt.Errorf("could not get ceph user: %w", err)
}
if err := util.ChownRecursive("/var/lib/ceph/mgr/ceph-a", cephUserUid, cephGroupGid); err != nil {
return fmt.Errorf("could not change owner: %w", err)
}
return nil
}
func configureMonitor(ctx context.Context, fsid string) error {
if err := os.MkdirAll("/var/lib/ceph/mon/ceph-a", 0o755); err != nil {
return fmt.Errorf("could not create directory: %w", err)
}
cmd := exec.CommandContext(ctx, "ceph-authtool", "--create-keyring", "/tmp/ceph.mon.keyring", "--gen-key", "-n", "mon.", "--cap", "mon", "allow *")
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("could not create keyring: %w: %s", err, out)
}
cmd = exec.CommandContext(ctx, "ceph-authtool", "--create-keyring", "/etc/ceph/ceph.client.admin.keyring", "--gen-key", "-n", "client.admin", "--cap", "mon", "allow *", "--cap", "osd", "allow *", "--cap", "mds", "allow *", "--cap", "mgr", "allow *")
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("could not create keyring: %w: %s", err, out)
}
cmd = exec.CommandContext(ctx, "ceph-authtool", "--create-keyring", "/var/lib/ceph/bootstrap-osd/ceph.keyring", "--gen-key", "-n", "client.bootstrap-osd", "--cap", "mon", "profile bootstrap-osd", "--cap", "mgr", "allow r")
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("could not create keyring: %w: %s", err, out)
}
cmd = exec.CommandContext(ctx, "ceph-authtool", "/tmp/ceph.mon.keyring", "--import-keyring", "/etc/ceph/ceph.client.admin.keyring")
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("could not import keyring: %w: %s", err, out)
}
cmd = exec.CommandContext(ctx, "ceph-authtool", "/tmp/ceph.mon.keyring", "--import-keyring", "/var/lib/ceph/bootstrap-osd/ceph.keyring")
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("could not import keyring: %w: %s", err, out)
}
cmd = exec.CommandContext(ctx, "monmaptool", "--create", "--addv", "a", "[v2:127.0.0.1:3300,v1:127.0.0.1:6789]", "--fsid", fsid, "/tmp/monmap")
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("could not create monmap: %w: %s", err, out)
}
cmd = exec.CommandContext(ctx, "ceph-mon", "--mkfs", "-i", "a", "--monmap", "/tmp/monmap", "--keyring", "/tmp/ceph.mon.keyring")
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("could not create monitor: %w: %s", err, out)
}
cephUserUid, cephGroupGid, err := cephUser()
if err != nil {
return fmt.Errorf("could not get ceph user: %w", err)
}
if err := util.ChownRecursive("/etc/ceph", cephUserUid, cephGroupGid); err != nil {
return fmt.Errorf("could not change owner: %w", err)
}
if err := os.Chown("/var/lib/ceph/mon/ceph-a", cephUserUid, cephGroupGid); err != nil {
return fmt.Errorf("could not change owner: %w", err)
}
return nil
}
func writeCephConf(fsid string) error {
cephConf, err := os.Create("/etc/ceph/ceph.conf")
if err != nil {
return fmt.Errorf("could not create ceph.conf: %w", err)
}
defer cephConf.Close()
tmpl := tmpl.GetCephConf()
if err := tmpl.Execute(cephConf, struct {
FSID string
}{
FSID: fsid,
}); err != nil {
return fmt.Errorf("could not execute ceph.conf template: %w", err)
}
cephUserUid, cephGroupGid, err := cephUser()
if err != nil {
return fmt.Errorf("could not get ceph user: %w", err)
}
if err := os.Chown("/etc/ceph/ceph.conf", cephUserUid, cephGroupGid); err != nil {
return fmt.Errorf("could not change owner: %w", err)
}
return nil
}
func createOSDDevice(ctx context.Context) error {
cmd := exec.CommandContext(ctx, "/usr/sbin/dmsetup", "remove", "-v", "ceph--vg-osd--0")
_ = cmd.Run()
if err := os.RemoveAll("/dev/ceph-vg"); err != nil {
return fmt.Errorf("could not remove directory: %w", err)
}
if err := os.MkdirAll("/var/lib/ceph/disk", 0o755); err != nil {
return fmt.Errorf("could not create directory: %w", err)
}
// Create a qemu image.
cmd = exec.CommandContext(ctx, "qemu-img", "create", "-f", "qcow2", "/var/lib/ceph/disk/osd-0.qcow2", "10G")
if output, err := cmd.Output(); err != nil {
return fmt.Errorf("could not create qemu image: %w: %s", err, output)
}
// Load the nbd kernel module (if not already loaded or built-in).
if err := nbd.Setup(ctx); err != nil {
// TODO: maybe we can fall back to using a loop device?
return fmt.Errorf("could not setup nbd: %w", err)
}
// Find the next free nbd device.
nbdDevicePath, err := nbd.NextFreeDevice()
if err != nil {
return fmt.Errorf("could not find free nbd device: %w", err)
}
// Mount the image using nbd.
cmd = exec.CommandContext(ctx, "qemu-nbd", "--connect="+nbdDevicePath, "/var/lib/ceph/disk/osd-0.qcow2")
if output, err := cmd.Output(); err != nil {
return fmt.Errorf("could not mount qemu image: %w: %s", err, output)
}
cmd = exec.CommandContext(ctx, "pvcreate", nbdDevicePath)
cmd.Env = append(os.Environ(), "DM_DISABLE_UDEV=1")
if output, err := cmd.Output(); err != nil {
return fmt.Errorf("could not create physical volume: %w: %s", err, output)
}
cmd = exec.CommandContext(ctx, "vgcreate", "ceph-vg", nbdDevicePath)
cmd.Env = append(os.Environ(), "DM_DISABLE_UDEV=1")
if output, err := cmd.Output(); err != nil {
return fmt.Errorf("could not create volume group: %w: %s", err, output)
}
cmd = exec.CommandContext(ctx, "lvcreate", "-l", "100%FREE", "-n", "osd-0", "ceph-vg")
cmd.Env = append(os.Environ(), "DM_DISABLE_UDEV=1")
if output, err := cmd.Output(); err != nil {
return fmt.Errorf("could not create logical volume: %w: %s", err, output)
}
return nil
}
func cephUser() (int, int, error) {
cephUser, err := user.Lookup("ceph")
if err != nil {
return -1, -1, fmt.Errorf("could not get ceph user: %w", err)
}
cephUserUid, err := strconv.Atoi(cephUser.Uid)
if err != nil {
return -1, -1, fmt.Errorf("could not convert ceph user uid: %w", err)
}
cephGroup, err := user.LookupGroup("ceph")
if err != nil {
return -1, -1, fmt.Errorf("could not get ceph group: %w", err)
}
cephGroupGid, err := strconv.Atoi(cephGroup.Gid)
if err != nil {
return -1, -1, fmt.Errorf("could not convert ceph group gid: %w", err)
}
return cephUserUid, cephGroupGid, nil
}