]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/storage/lvm.c
6908e9066c3dbecc914686132403735b85de2065
[mirror_lxc.git] / src / lxc / storage / lvm.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE 1
26 #endif
27 #define __STDC_FORMAT_MACROS
28 #include <inttypes.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/sysmacros.h>
34 #include <sys/wait.h>
35 #include <unistd.h>
36
37 #include "config.h"
38 #include "log.h"
39 #include "lvm.h"
40 #include "rsync.h"
41 #include "storage.h"
42 #include "storage_utils.h"
43 #include "utils.h"
44
45 #ifdef MAJOR_IN_MKDEV
46 #include <sys/mkdev.h>
47 #endif
48
49 lxc_log_define(lvm, lxc);
50
51 struct lvcreate_args {
52 const char *size;
53 const char *vg;
54 const char *lv;
55 const char *thinpool;
56 const char *fstype;
57
58 /* snapshot specific arguments */
59 const char *source_lv;
60 };
61
62 static int lvm_destroy_exec_wrapper(void *data)
63 {
64 struct lvcreate_args *args = data;
65
66 (void)setenv("LVM_SUPPRESS_FD_WARNINGS", "1", 1);
67 execlp("lvremove", "lvremove", "-f", args->lv, (char *)NULL);
68
69 return -1;
70 }
71
72 static int lvm_create_exec_wrapper(void *data)
73 {
74 struct lvcreate_args *args = data;
75
76 (void)setenv("LVM_SUPPRESS_FD_WARNINGS", "1", 1);
77 if (args->thinpool)
78 execlp("lvcreate", "lvcreate", "--thinpool", args->thinpool,
79 "-V", args->size, args->vg, "-n", args->lv,
80 (char *)NULL);
81 else
82 execlp("lvcreate", "lvcreate", "-L", args->size, args->vg, "-n",
83 args->lv, (char *)NULL);
84
85 return -1;
86 }
87
88 static int lvm_snapshot_exec_wrapper(void *data)
89 {
90 struct lvcreate_args *args = data;
91
92 (void)setenv("LVM_SUPPRESS_FD_WARNINGS", "1", 1);
93 if (args->thinpool)
94 execlp("lvcreate", "lvcreate", "-s", "-n", args->lv,
95 args->source_lv, (char *)NULL);
96 else
97 execlp("lvcreate", "lvcreate", "-s", "-L", args->size, "-n",
98 args->lv, args->source_lv, (char *)NULL);
99
100 return -1;
101 }
102
103 /* The path must be "/dev/<vg>/<lv>". The volume group <vg> must be an existing
104 * volume group, and the logical volume <lv> must not yet exist.
105 * This function will attempt to create "/dev/<vg>/<lv> of size <size>. If
106 * thinpool is specified, we'll check for it's existence and if it's a valid
107 * thin pool, and if so, we'll create the requested logical volume from that
108 * thin pool.
109 */
110 static int do_lvm_create(const char *path, uint64_t size, const char *thinpool)
111 {
112 int len, ret;
113 char *pathdup, *vg, *lv;
114 char cmd_output[PATH_MAX];
115 char sz[24];
116 char *tp = NULL;
117 struct lvcreate_args cmd_args = {0};
118
119 ret = snprintf(sz, 24, "%" PRIu64 "b", size);
120 if (ret < 0 || ret >= 24) {
121 ERROR("Failed to create string: %d", ret);
122 return -1;
123 }
124
125 pathdup = strdup(path);
126 if (!pathdup) {
127 ERROR("Failed to duplicate string \"%s\"", path);
128 return -1;
129 }
130
131 lv = strrchr(pathdup, '/');
132 if (!lv) {
133 ERROR("Failed to detect \"/\" in string \"%s\"", pathdup);
134 free(pathdup);
135 return -1;
136 }
137 *lv = '\0';
138 lv++;
139 TRACE("Parsed logical volume \"%s\"", lv);
140
141 vg = strrchr(pathdup, '/');
142 if (!vg) {
143 ERROR("Failed to detect \"/\" in string \"%s\"", pathdup);
144 free(pathdup);
145 return -1;
146 }
147 vg++;
148 TRACE("Parsed volume group \"%s\"", vg);
149
150 if (thinpool) {
151 len = strlen(pathdup) + strlen(thinpool) + 2;
152 tp = alloca(len);
153
154 ret = snprintf(tp, len, "%s/%s", pathdup, thinpool);
155 if (ret < 0 || ret >= len) {
156 ERROR("Failed to create string: %d", ret);
157 free(pathdup);
158 return -1;
159 }
160
161 ret = lvm_is_thin_pool(tp);
162 TRACE("got %d for thin pool at path: %s", ret, tp);
163 if (ret < 0) {
164 ERROR("Failed to detect whether \"%s\" is a thinpool", tp);
165 free(pathdup);
166 return -1;
167 } else if (!ret) {
168 TRACE("Detected that \"%s\" is not a thinpool", tp);
169 tp = NULL;
170 } else {
171 TRACE("Detected \"%s\" is a thinpool", tp);
172 }
173 }
174
175 cmd_args.thinpool = tp;
176 cmd_args.vg = vg;
177 cmd_args.lv = lv;
178 cmd_args.size = sz;
179 TRACE("Creating new lvm storage volume \"%s\" on volume group \"%s\" "
180 "of size \"%s\"", lv, vg, sz);
181 ret = run_command(cmd_output, sizeof(cmd_output),
182 lvm_create_exec_wrapper, (void *)&cmd_args);
183 if (ret < 0) {
184 ERROR("Failed to create logical volume \"%s\": %s", lv,
185 cmd_output);
186 free(pathdup);
187 return -1;
188 }
189 TRACE("Created new lvm storage volume \"%s\" on volume group \"%s\" "
190 "of size \"%s\"", lv, vg, sz);
191
192 free(pathdup);
193 return ret;
194 }
195
196 /* Look at "/sys/dev/block/maj:min/dm/uuid". If it contains the hardcoded LVM
197 * prefix "LVM-" then this is an lvm2 LV.
198 */
199 bool lvm_detect(const char *path)
200 {
201 int fd;
202 ssize_t ret;
203 struct stat statbuf;
204 char devp[PATH_MAX], buf[4];
205
206 if (!strncmp(path, "lvm:", 4))
207 return true;
208
209 ret = stat(path, &statbuf);
210 if (ret < 0)
211 return false;
212
213 if (!S_ISBLK(statbuf.st_mode))
214 return false;
215
216 ret = snprintf(devp, PATH_MAX, "/sys/dev/block/%d:%d/dm/uuid",
217 major(statbuf.st_rdev), minor(statbuf.st_rdev));
218 if (ret < 0 || ret >= PATH_MAX) {
219 ERROR("Failed to create string");
220 return false;
221 }
222
223 fd = open(devp, O_RDONLY);
224 if (fd < 0)
225 return false;
226
227 ret = read(fd, buf, sizeof(buf));
228 close(fd);
229 if (ret != sizeof(buf))
230 return false;
231
232 if (strncmp(buf, "LVM-", 4))
233 return false;
234
235 return true;
236 }
237
238 int lvm_mount(struct lxc_storage *bdev)
239 {
240 const char *src;
241
242 if (strcmp(bdev->type, "lvm"))
243 return -22;
244
245 if (!bdev->src || !bdev->dest)
246 return -22;
247
248 src = lxc_storage_get_path(bdev->src, bdev->type);
249
250 /* If we might pass in data sometime, then we'll have to enrich
251 * mount_unknown_fs().
252 */
253 return mount_unknown_fs(src, bdev->dest, bdev->mntopts);
254 }
255
256 int lvm_umount(struct lxc_storage *bdev)
257 {
258 if (strcmp(bdev->type, "lvm"))
259 return -22;
260
261 if (!bdev->src || !bdev->dest)
262 return -22;
263
264 return umount(bdev->dest);
265 }
266
267 int lvm_compare_lv_attr(const char *path, int pos, const char expected)
268 {
269 struct lxc_popen_FILE *f;
270 int ret, status;
271 size_t len;
272 char *cmd;
273 char output[12];
274 int start = 0;
275 const char *lvscmd = "lvs --unbuffered --noheadings -o lv_attr %s 2>/dev/null";
276
277 len = strlen(lvscmd) + strlen(path) + 1;
278 cmd = alloca(len);
279
280 ret = snprintf(cmd, len, lvscmd, path);
281 if (ret < 0 || (size_t)ret >= len)
282 return -1;
283
284 f = lxc_popen(cmd);
285 if (!f) {
286 SYSERROR("popen failed");
287 return -1;
288 }
289
290 ret = 0;
291 if (!fgets(output, 12, f->f))
292 ret = 1;
293
294 status = lxc_pclose(f);
295 /* Assume either vg or lvs do not exist, default comparison to false. */
296 if (ret || WEXITSTATUS(status))
297 return 0;
298
299 len = strlen(output);
300 while (start < len && output[start] == ' ')
301 start++;
302
303 if (start + pos < len && output[start + pos] == expected)
304 return 1;
305
306 return 0;
307 }
308
309 int lvm_is_thin_volume(const char *path)
310 {
311 return lvm_compare_lv_attr(path, 6, 't');
312 }
313
314 int lvm_is_thin_pool(const char *path)
315 {
316 return lvm_compare_lv_attr(path, 0, 't');
317 }
318
319 static int lvm_snapshot_create_new_uuid_wrapper(void *data)
320 {
321 struct lvcreate_args *args = data;
322
323 if (strcmp(args->fstype, "xfs") == 0)
324 execlp("xfs_admin", "xfs_admin", "-U", "generate", args->lv, (char *)NULL);
325
326 if (strcmp(args->fstype, "btrfs") == 0)
327 execlp("btrfstune", "btrfstune", "-f", "-u", args->lv, (char *)NULL);
328
329 return 0;
330 }
331
332 static int lvm_snapshot(struct lxc_storage *orig, const char *path, uint64_t size)
333 {
334 int ret;
335 char *lv, *pathdup;
336 char sz[24];
337 char fstype[100];
338 char cmd_output[PATH_MAX];
339 char repairchar;
340 const char *origsrc;
341 struct lvcreate_args cmd_args = {0};
342
343 ret = snprintf(sz, 24, "%" PRIu64 "b", size);
344 if (ret < 0 || ret >= 24) {
345 ERROR("Failed to create string");
346 return -1;
347 }
348
349 pathdup = strdup(path);
350 if (!pathdup) {
351 ERROR("Failed to duplicate string \"%s\"", path);
352 return -1;
353 }
354
355 lv = strrchr(pathdup, '/');
356 if (!lv) {
357 ERROR("Failed to detect \"/\" in string \"%s\"", pathdup);
358 free(pathdup);
359 return -1;
360 }
361 repairchar = *lv;
362 *lv = '\0';
363 lv++;
364 TRACE("Parsed logical volume \"%s\"", lv);
365
366 /* Check if the original logical volume is backed by a thinpool, in
367 * which case we cannot specify a size that's different from the
368 * original size.
369 */
370 origsrc = lxc_storage_get_path(orig->src, "lvm");
371 ret = lvm_is_thin_volume(origsrc);
372 if (ret < 0) {
373 free(pathdup);
374 return -1;
375 } else if (ret) {
376 cmd_args.thinpool = origsrc;
377 }
378
379 cmd_args.lv = lv;
380 cmd_args.source_lv = origsrc;
381 cmd_args.size = sz;
382 TRACE("Creating new lvm snapshot \"%s\" of \"%s\" with size \"%s\"", lv,
383 origsrc, sz);
384 ret = run_command(cmd_output, sizeof(cmd_output),
385 lvm_snapshot_exec_wrapper, (void *)&cmd_args);
386 if (ret < 0) {
387 ERROR("Failed to create logical volume \"%s\": %s", lv, cmd_output);
388 free(pathdup);
389 return -1;
390 }
391
392 if (detect_fs(orig, fstype, 100) < 0) {
393 INFO("Failed to detect filesystem type for \"%s\"", origsrc);
394 free(pathdup);
395 return -1;
396 }
397
398 /* repair path */
399 lv--;
400 *lv = repairchar;
401 cmd_args.lv = pathdup;
402 cmd_args.fstype = fstype;
403 ret = run_command(cmd_output, sizeof(cmd_output),
404 lvm_snapshot_create_new_uuid_wrapper, (void *)&cmd_args);
405 if (ret < 0) {
406 ERROR("Failed to create new uuid for volume \"%s\": %s", pathdup, cmd_output);
407 free(pathdup);
408 return -1;
409 }
410
411 free(pathdup);
412 return 0;
413 }
414
415 int lvm_clonepaths(struct lxc_storage *orig, struct lxc_storage *new,
416 const char *oldname, const char *cname, const char *oldpath,
417 const char *lxcpath, int snap, uint64_t newsize,
418 struct lxc_conf *conf)
419 {
420 int len, ret;
421 const char *vg;
422
423 if (!orig->src || !orig->dest)
424 return -1;
425
426 if (strcmp(orig->type, "lvm") && snap) {
427 ERROR("LVM snapshot from \"%s\" storage driver is not supported",
428 orig->type);
429 return -1;
430 }
431
432 if (strcmp(orig->type, "lvm")) {
433 vg = lxc_global_config_value("lxc.bdev.lvm.vg");
434 new->src = lxc_string_join(
435 "/",
436 (const char *[]){"lvm:", "dev", vg, cname, NULL},
437 false);
438 } else {
439 const char *src;
440 char *dup, *slider;
441
442 src = lxc_storage_get_path(orig->src, orig->type);
443
444 dup = strdup(src);
445 if (!dup) {
446 ERROR("Failed to duplicate string \"%s\"", src);
447 return -1;
448 }
449
450 slider = strrchr(dup, '/');
451 if (!slider) {
452 ERROR("Failed to detect \"/\" in string \"%s\"", dup);
453 free(dup);
454 return -1;
455 }
456 *slider = '\0';
457 slider = dup;
458
459 new->src = lxc_string_join(
460 "/",
461 (const char *[]){"lvm:", *slider == '/' ? ++slider : slider,
462 cname, NULL},
463 false);
464 free(dup);
465 }
466 if (!new->src) {
467 ERROR("Failed to create string");
468 return -1;
469 }
470
471 if (orig->mntopts) {
472 new->mntopts = strdup(orig->mntopts);
473 if (!new->mntopts) {
474 ERROR("Failed to duplicate string \"%s\"", orig->mntopts);
475 return -1;
476 }
477 }
478
479 len = strlen(lxcpath) + strlen(cname) + strlen("rootfs") + 3;
480 new->dest = malloc(len);
481 if (!new->dest) {
482 ERROR("Failed to allocate memory");
483 return -1;
484 }
485
486 ret = snprintf(new->dest, len, "%s/%s/rootfs", lxcpath, cname);
487 if (ret < 0 || ret >= len) {
488 ERROR("Failed to create string");
489 return -1;
490 }
491
492 ret = mkdir_p(new->dest, 0755);
493 if (ret < 0) {
494 SYSERROR("Failed to create directory \"%s\"", new->dest);
495 return -1;
496 }
497
498 return 0;
499 }
500
501 bool lvm_create_clone(struct lxc_conf *conf, struct lxc_storage *orig,
502 struct lxc_storage *new, uint64_t newsize)
503 {
504 int ret;
505 const char *src;
506 const char *thinpool;
507 struct rsync_data data;
508 const char *cmd_args[2];
509 char cmd_output[PATH_MAX] = {0};
510 char fstype[100] = "ext4";
511 uint64_t size = newsize;
512
513 if (is_blktype(orig)) {
514 /* detect size */
515 if (!newsize && blk_getsize(orig, &size) < 0) {
516 ERROR("Failed to detect size of logical volume \"%s\"",
517 orig->src);
518 return -1;
519 }
520
521 /* detect filesystem */
522 if (detect_fs(orig, fstype, 100) < 0) {
523 INFO("Failed to detect filesystem type for \"%s\"", orig->src);
524 return -1;
525 }
526 } else if (!newsize) {
527 size = DEFAULT_FS_SIZE;
528 }
529
530 src = lxc_storage_get_path(new->src, "lvm");
531 thinpool = lxc_global_config_value("lxc.bdev.lvm.thin_pool");
532
533 ret = do_lvm_create(src, size, thinpool);
534 if (ret < 0) {
535 ERROR("Failed to create lvm storage volume \"%s\"", src);
536 return -1;
537 }
538
539 cmd_args[0] = fstype;
540 cmd_args[1] = src;
541 ret = run_command(cmd_output, sizeof(cmd_output),
542 do_mkfs_exec_wrapper, (void *)cmd_args);
543 if (ret < 0) {
544 ERROR("Failed to create new filesystem \"%s\" for lvm storage "
545 "volume \"%s\": %s", fstype, src, cmd_output);
546 return -1;
547 }
548
549 data.orig = orig;
550 data.new = new;
551 ret = run_command(cmd_output, sizeof(cmd_output),
552 lxc_storage_rsync_exec_wrapper, (void *)&data);
553 if (ret < 0) {
554 ERROR("Failed to rsync from \"%s\" to \"%s\"", orig->dest,
555 new->dest);
556 return false;
557 }
558
559 TRACE("Created lvm storage volume \"%s\"", new->dest);
560 return true;
561 }
562
563 bool lvm_create_snapshot(struct lxc_conf *conf, struct lxc_storage *orig,
564 struct lxc_storage *new, uint64_t newsize)
565 {
566 int ret;
567 const char *newsrc;
568 uint64_t size = newsize;
569
570 if (is_blktype(orig)) {
571 if (!newsize && blk_getsize(orig, &size) < 0) {
572 ERROR("Failed to detect size of logical volume \"%s\"",
573 orig->src);
574 return -1;
575 }
576 } else if (!newsize) {
577 size = DEFAULT_FS_SIZE;
578 }
579
580 newsrc = lxc_storage_get_path(new->src, "lvm");
581
582 ret = lvm_snapshot(orig, newsrc, size);
583 if (ret < 0) {
584 ERROR("Failed to create lvm \"%s\" snapshot of \"%s\"",
585 new->src, orig->src);
586 return false;
587 }
588
589 TRACE("Created lvm snapshot \"%s\" from \"%s\"", new->dest, orig->dest);
590 return true;
591 }
592
593 int lvm_destroy(struct lxc_storage *orig)
594 {
595 int ret;
596 char cmd_output[PATH_MAX];
597 struct lvcreate_args cmd_args = {0};
598
599 cmd_args.lv = lxc_storage_get_path(orig->src, "lvm");
600 ret = run_command(cmd_output, sizeof(cmd_output),
601 lvm_destroy_exec_wrapper, (void *)&cmd_args);
602 if (ret < 0) {
603 ERROR("Failed to destroy logical volume \"%s\": %s", orig->src,
604 cmd_output);
605 return -1;
606 }
607
608 TRACE("Destroyed logical volume \"%s\"", orig->src);
609 return 0;
610 }
611
612 int lvm_create(struct lxc_storage *bdev, const char *dest, const char *n,
613 struct bdev_specs *specs)
614 {
615 const char *vg, *thinpool, *fstype, *lv = n;
616 uint64_t sz;
617 int ret, len;
618 const char *cmd_args[2];
619 char cmd_output[PATH_MAX];
620
621 if (!specs)
622 return -1;
623
624 vg = specs->lvm.vg;
625 if (!vg)
626 vg = lxc_global_config_value("lxc.bdev.lvm.vg");
627
628 thinpool = specs->lvm.thinpool;
629 if (!thinpool)
630 thinpool = lxc_global_config_value("lxc.bdev.lvm.thin_pool");
631
632 /* /dev/$vg/$lv */
633 if (specs->lvm.lv)
634 lv = specs->lvm.lv;
635
636 len = strlen(vg) + strlen(lv) + 4 + 7;
637 bdev->src = malloc(len);
638 if (!bdev->src) {
639 ERROR("Failed to allocate memory");
640 return -1;
641 }
642
643 ret = snprintf(bdev->src, len, "lvm:/dev/%s/%s", vg, lv);
644 if (ret < 0 || ret >= len) {
645 ERROR("Failed to create string");
646 return -1;
647 }
648
649 /* size is in bytes */
650 sz = specs->fssize;
651 if (!sz)
652 sz = DEFAULT_FS_SIZE;
653
654 ret = do_lvm_create(bdev->src + 4, sz, thinpool);
655 if (ret < 0) {
656 ERROR("Error creating new logical volume \"%s\" of size "
657 "\"%" PRIu64 " bytes\"", bdev->src, sz);
658 return -1;
659 }
660
661 fstype = specs->fstype;
662 if (!fstype)
663 fstype = DEFAULT_FSTYPE;
664
665 cmd_args[0] = fstype;
666 cmd_args[1] = lxc_storage_get_path(bdev->src, bdev->type);
667 ret = run_command(cmd_output, sizeof(cmd_output), do_mkfs_exec_wrapper,
668 (void *)cmd_args);
669 if (ret < 0) {
670 ERROR("Failed to create new logical volume \"%s\": %s",
671 bdev->src, cmd_output);
672 return -1;
673 }
674
675 bdev->dest = strdup(dest);
676 if (!bdev->dest) {
677 ERROR("Failed to duplicate string \"%s\"", dest);
678 return -1;
679 }
680
681 ret = mkdir_p(bdev->dest, 0755);
682 if (ret < 0) {
683 SYSERROR("Failed to create directory \"%s\"", bdev->dest);
684 return -1;
685 }
686
687 TRACE("Created new logical volume \"%s\"", bdev->dest);
688 return 0;
689 }