/
lxclvm.c
473 lines (387 loc) · 10.4 KB
/
lxclvm.c
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
* lxc: linux Container library
*
* (C) Copyright IBM Corp. 2007, 2008
*
* Authors:
* Daniel Lezcano <daniel.lezcano at free.fr>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE
#define __STDC_FORMAT_MACROS /* Required for PRIu64 to work. */
#include <inttypes.h> /* Required for PRIu64 to work. */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/sysmacros.h>
#include <sys/wait.h>
#include "bdev.h"
#include "config.h"
#include "log.h"
#include "lxclvm.h"
#include "storage_utils.h"
#include "utils.h"
/* major()/minor() */
#ifdef MAJOR_IN_MKDEV
# include <sys/mkdev.h>
#endif
lxc_log_define(lxclvm, lxc);
extern char *dir_new_path(char *src, const char *oldname, const char *name,
const char *oldpath, const char *lxcpath);
/*
* LVM ops
*/
/*
* path must be '/dev/$vg/$lv', $vg must be an existing VG, and $lv must not
* yet exist. This function will attempt to create /dev/$vg/$lv of size
* $size. If thinpool is specified, we'll check for it's existence and if
* it's
* a valid thin pool, and if so, we'll create the requested lv from that
* thin
* pool.
*/
static int do_lvm_create(const char *path, uint64_t size,
const char *thinpool)
{
int ret, pid, len;
char sz[24], *pathdup, *vg, *lv, *tp = NULL;
if ((pid = fork()) < 0) {
SYSERROR("failed fork");
return -1;
}
if (pid > 0)
return wait_for_pid(pid);
// specify bytes to lvcreate
ret = snprintf(sz, 24, "%"PRIu64"b", size);
if (ret < 0 || ret >= 24)
exit(EXIT_FAILURE);
pathdup = strdup(path);
if (!pathdup)
exit(EXIT_FAILURE);
lv = strrchr(pathdup, '/');
if (!lv)
exit(EXIT_FAILURE);
*lv = '\0';
lv++;
vg = strrchr(pathdup, '/');
if (!vg)
exit(EXIT_FAILURE);
vg++;
if (thinpool) {
len = strlen(pathdup) + strlen(thinpool) + 2;
tp = alloca(len);
ret = snprintf(tp, len, "%s/%s", pathdup, thinpool);
if (ret < 0 || ret >= len)
exit(EXIT_FAILURE);
ret = lvm_is_thin_pool(tp);
INFO("got %d for thin pool at path: %s", ret, tp);
if (ret < 0)
exit(EXIT_FAILURE);
if (!ret)
tp = NULL;
}
(void)setenv("LVM_SUPPRESS_FD_WARNINGS", "1", 1);
if (!tp)
execlp("lvcreate", "lvcreate", "-L", sz, vg, "-n", lv, (char *)NULL);
else
execlp("lvcreate", "lvcreate", "--thinpool", tp, "-V", sz, vg, "-n", lv, (char *)NULL);
SYSERROR("execlp");
exit(EXIT_FAILURE);
}
/*
* Look at /sys/dev/block/maj:min/dm/uuid. If it contains the hardcoded LVM
* prefix "LVM-", then this is an lvm2 LV
*/
int lvm_detect(const char *path)
{
char devp[MAXPATHLEN], buf[4];
FILE *fout;
int ret;
struct stat statbuf;
if (!strncmp(path, "lvm:", 4))
return 1;
ret = stat(path, &statbuf);
if (ret != 0)
return 0;
if (!S_ISBLK(statbuf.st_mode))
return 0;
ret = snprintf(devp, MAXPATHLEN, "/sys/dev/block/%d:%d/dm/uuid",
major(statbuf.st_rdev), minor(statbuf.st_rdev));
if (ret < 0 || ret >= MAXPATHLEN) {
ERROR("lvm uuid pathname too long");
return 0;
}
fout = fopen(devp, "r");
if (!fout)
return 0;
ret = fread(buf, 1, 4, fout);
fclose(fout);
if (ret != 4 || strncmp(buf, "LVM-", 4) != 0)
return 0;
return 1;
}
int lvm_mount(struct bdev *bdev)
{
char *src;
if (strcmp(bdev->type, "lvm"))
return -22;
if (!bdev->src || !bdev->dest)
return -22;
src = lxc_storage_get_path(bdev->src, bdev->type);
/* If we might pass in data sometime, then we'll have to enrich
* mount_unknown_fs().
*/
return mount_unknown_fs(src, bdev->dest, bdev->mntopts);
}
int lvm_umount(struct bdev *bdev)
{
if (strcmp(bdev->type, "lvm"))
return -22;
if (!bdev->src || !bdev->dest)
return -22;
return umount(bdev->dest);
}
int lvm_compare_lv_attr(const char *path, int pos, const char expected)
{
struct lxc_popen_FILE *f;
int ret, len, status, start=0;
char *cmd, output[12];
const char *lvscmd = "lvs --unbuffered --noheadings -o lv_attr %s 2>/dev/null";
len = strlen(lvscmd) + strlen(path) - 1;
cmd = alloca(len);
ret = snprintf(cmd, len, lvscmd, path);
if (ret < 0 || ret >= len)
return -1;
f = lxc_popen(cmd);
if (f == NULL) {
SYSERROR("popen failed");
return -1;
}
ret = fgets(output, 12, f->f) == NULL;
status = lxc_pclose(f);
if (ret || WEXITSTATUS(status))
// Assume either vg or lvs do not exist, default
// comparison to false.
return 0;
len = strlen(output);
while(start < len && output[start] == ' ') start++;
if (start + pos < len && output[start + pos] == expected)
return 1;
return 0;
}
int lvm_is_thin_volume(const char *path)
{
return lvm_compare_lv_attr(path, 6, 't');
}
int lvm_is_thin_pool(const char *path)
{
return lvm_compare_lv_attr(path, 0, 't');
}
int lvm_snapshot(const char *orig, const char *path, uint64_t size)
{
int ret, pid;
char sz[24], *pathdup, *lv;
if ((pid = fork()) < 0) {
SYSERROR("failed fork");
return -1;
}
if (pid > 0)
return wait_for_pid(pid);
// specify bytes to lvcreate
ret = snprintf(sz, 24, "%"PRIu64"b", size);
if (ret < 0 || ret >= 24)
exit(EXIT_FAILURE);
pathdup = strdup(path);
if (!pathdup)
exit(EXIT_FAILURE);
lv = strrchr(pathdup, '/');
if (!lv) {
free(pathdup);
exit(EXIT_FAILURE);
}
*lv = '\0';
lv++;
// check if the original lv is backed by a thin pool, in which case we
// cannot specify a size that's different from the original size.
ret = lvm_is_thin_volume(orig);
if (ret == -1) {
free(pathdup);
return -1;
}
(void)setenv("LVM_SUPPRESS_FD_WARNINGS", "1", 1);
if (!ret) {
ret = execlp("lvcreate", "lvcreate", "-s", "-L", sz, "-n", lv, orig, (char *)NULL);
} else {
ret = execlp("lvcreate", "lvcreate", "-s", "-n", lv, orig, (char *)NULL);
}
free(pathdup);
exit(EXIT_FAILURE);
}
int lvm_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname,
const char *cname, const char *oldpath, const char *lxcpath, int snap,
uint64_t newsize, struct lxc_conf *conf)
{
char fstype[100];
uint64_t size = newsize;
int len, ret;
const char *cmd_args[2];
char cmd_output[MAXPATHLEN];
if (!orig->src || !orig->dest)
return -1;
if (strcmp(orig->type, "lvm")) {
const char *vg;
if (snap) {
ERROR("LVM snapshot from %s backing store is not supported",
orig->type);
return -1;
}
vg = lxc_global_config_value("lxc.bdev.lvm.vg");
if (!vg) {
ERROR("The \"lxc.bdev.lvm.vg\" key is not set");
return -1;
}
len = strlen("/dev/") + strlen(vg) + strlen(cname) + 4 + 2;
new->src = malloc(len);
if (new->src)
return -1;
ret = snprintf(new->src, len, "lvm:/dev/%s/%s", vg, cname);
if (ret < 0 || ret >= len)
return -1;
} else {
new->src = dir_new_path(orig->src, oldname, cname, oldpath, lxcpath);
if (!new->src)
return -1;
}
if (orig->mntopts) {
new->mntopts = strdup(orig->mntopts);
if (!new->mntopts)
return -1;
}
len = strlen(lxcpath) + strlen(cname) + strlen("rootfs") + 3;
new->dest = malloc(len);
if (!new->dest)
return -1;
ret = snprintf(new->dest, len, "%s/%s/rootfs", lxcpath, cname);
if (ret < 0 || ret >= len)
return -1;
if (mkdir_p(new->dest, 0755) < 0)
return -1;
if (is_blktype(orig)) {
if (!newsize && blk_getsize(orig, &size) < 0) {
ERROR("Error getting size of %s", orig->src);
return -1;
}
if (detect_fs(orig, fstype, 100) < 0) {
INFO("could not find fstype for %s, using ext3", orig->src);
return -1;
}
} else {
sprintf(fstype, "ext3");
if (!newsize)
size = DEFAULT_FS_SIZE;
}
if (snap) {
char *newsrc, *origsrc;
origsrc = lxc_storage_get_path(orig->src, "lvm");
newsrc = lxc_storage_get_path(new->src, "lvm");
if (lvm_snapshot(origsrc, newsrc, size) < 0) {
ERROR("could not create %s snapshot of %s", new->src, orig->src);
return -1;
}
} else {
char *src;
src = lxc_storage_get_path(new->src, "lvm");
if (do_lvm_create(src, size, lxc_global_config_value("lxc.bdev.lvm.thin_pool")) < 0) {
ERROR("Error creating new lvm blockdev");
return -1;
}
cmd_args[0] = fstype;
cmd_args[1] = src;
// create an fs in the loopback file
ret = run_command(cmd_output, sizeof(cmd_output),
do_mkfs_exec_wrapper, (void *)cmd_args);
if (ret < 0)
return -1;
}
return 0;
}
int lvm_destroy(struct bdev *orig)
{
char *src;
pid_t pid;
if ((pid = fork()) < 0)
return -1;
if (!pid) {
(void)setenv("LVM_SUPPRESS_FD_WARNINGS", "1", 1);
src = lxc_storage_get_path(orig->src, "lvm");
execlp("lvremove", "lvremove", "-f", src, (char *)NULL);
exit(EXIT_FAILURE);
}
return wait_for_pid(pid);
}
int lvm_create(struct bdev *bdev, const char *dest, const char *n,
struct bdev_specs *specs)
{
const char *vg, *thinpool, *fstype, *lv = n;
uint64_t sz;
int ret, len;
const char *cmd_args[2];
char cmd_output[MAXPATHLEN];
if (!specs)
return -1;
vg = specs->lvm.vg;
if (!vg)
vg = lxc_global_config_value("lxc.bdev.lvm.vg");
thinpool = specs->lvm.thinpool;
if (!thinpool)
thinpool = lxc_global_config_value("lxc.bdev.lvm.thin_pool");
/* /dev/$vg/$lv */
if (specs->lvm.lv)
lv = specs->lvm.lv;
len = strlen(vg) + strlen(lv) + 4 + 7;
bdev->src = malloc(len);
if (!bdev->src)
return -1;
ret = snprintf(bdev->src, len, "lvm:/dev/%s/%s", vg, lv);
if (ret < 0 || ret >= len)
return -1;
// fssize is in bytes.
sz = specs->fssize;
if (!sz)
sz = DEFAULT_FS_SIZE;
if (do_lvm_create(bdev->src + 4, sz, thinpool) < 0) {
ERROR("Error creating new lvm blockdev %s size %"PRIu64" bytes", bdev->src, sz);
return -1;
}
fstype = specs->fstype;
if (!fstype)
fstype = DEFAULT_FSTYPE;
cmd_args[0] = fstype;
cmd_args[1] = bdev->src + 4;
ret = run_command(cmd_output, sizeof(cmd_output), do_mkfs_exec_wrapper,
(void *)cmd_args);
if (ret < 0)
return -1;
if (!(bdev->dest = strdup(dest)))
return -1;
if (mkdir_p(bdev->dest, 0755) < 0) {
ERROR("Error creating %s", bdev->dest);
return -1;
}
return 0;
}