]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/storage/overlay.c
log: change WARN macro using strerror to SYSWARN
[mirror_lxc.git] / src / lxc / storage / overlay.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 #define _GNU_SOURCE
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "conf.h"
31 #include "confile.h"
32 #include "log.h"
33 #include "lxccontainer.h"
34 #include "overlay.h"
35 #include "rsync.h"
36 #include "storage.h"
37 #include "storage_utils.h"
38 #include "utils.h"
39
40 lxc_log_define(overlay, lxc);
41
42 static char *ovl_name;
43 static char *ovl_version[] = {"overlay", "overlayfs"};
44
45 static char *ovl_detect_name(void);
46 static int ovl_do_rsync(const char *src, const char *dest,
47 struct lxc_conf *conf);
48 static int ovl_remount_on_enodev(const char *lower, const char *target,
49 const char *name, unsigned long mountflags,
50 const void *options);
51
52 int ovl_clonepaths(struct lxc_storage *orig, struct lxc_storage *new, const char *oldname,
53 const char *cname, const char *oldpath, const char *lxcpath,
54 int snap, uint64_t newsize, struct lxc_conf *conf)
55 {
56 int ret;
57 const char *src;
58
59 if (!snap) {
60 ERROR("The overlay storage driver can only be used for "
61 "snapshots");
62 return -22;
63 }
64
65 if (!orig->src || !orig->dest)
66 return -1;
67
68 new->dest = must_make_path(lxcpath, cname, "rootfs", NULL);
69
70 ret = mkdir_p(new->dest, 0755);
71 if (ret < 0 && errno != EEXIST) {
72 SYSERROR("Failed to create directory \"%s\"", new->dest);
73 return -1;
74 }
75
76 if (am_guest_unpriv()) {
77 ret = chown_mapped_root(new->dest, conf);
78 if (ret < 0)
79 WARN("Failed to update ownership of %s", new->dest);
80 }
81
82 if (strcmp(orig->type, "dir") == 0) {
83 char *delta, *lastslash;
84 char *work;
85 int ret, len, lastslashidx;
86
87 /* If we have "/var/lib/lxc/c2/rootfs" then delta will be
88 * "/var/lib/lxc/c2/delta0".
89 */
90 lastslash = strrchr(new->dest, '/');
91 if (!lastslash) {
92 ERROR("Failed to detect \"/\" in string \"%s\"",
93 new->dest);
94 return -22;
95 }
96
97 if (strlen(lastslash) < (sizeof("/rootfs") - 1)) {
98 ERROR("Failed to detect \"/rootfs\" in string \"%s\"",
99 new->dest);
100 return -22;
101 }
102
103 lastslash++;
104 lastslashidx = lastslash - new->dest;
105
106 delta = malloc(lastslashidx + 7);
107 if (!delta) {
108 ERROR("Failed to allocate memory");
109 return -1;
110 }
111
112 memcpy(delta, new->dest, lastslashidx + 1);
113 memcpy(delta + lastslashidx, "delta0", sizeof("delta0") - 1);
114 delta[lastslashidx + sizeof("delta0") - 1] = '\0';
115
116 ret = mkdir(delta, 0755);
117 if (ret < 0 && errno != EEXIST) {
118 SYSERROR("Failed to create directory \"%s\"", delta);
119 free(delta);
120 return -1;
121 }
122
123 if (am_guest_unpriv()) {
124 ret = chown_mapped_root(delta, conf);
125 if (ret < 0)
126 WARN("Failed to update ownership of %s", delta);
127 }
128
129 /* Make workdir for overlayfs.v22 or higher:
130 * The workdir will be
131 * /var/lib/lxc/c2/olwork
132 * and is used to prepare files before they are atomically
133 * switched to the overlay destination. Workdirs need to be on
134 * the same filesystem as the upperdir so it's OK for it to be
135 * empty.
136 */
137 work = malloc(lastslashidx + 7);
138 if (!work) {
139 ERROR("Failed to allocate memory");
140 free(delta);
141 return -1;
142 }
143
144 memcpy(work, new->dest, lastslashidx + 1);
145 memcpy(work + lastslashidx, "olwork", sizeof("olwork") - 1);
146 work[lastslashidx + sizeof("olwork") - 1] = '\0';
147
148 ret = mkdir(work, 0755);
149 if (ret < 0) {
150 SYSERROR("Failed to create directory \"%s\"", work);
151 free(delta);
152 free(work);
153 return -1;
154 }
155
156 if (am_guest_unpriv()) {
157 ret = chown_mapped_root(work, conf);
158 if (ret < 0)
159 WARN("Failed to update ownership of %s", work);
160 }
161 free(work);
162
163 /* strlen("overlay:") = 8
164 * +
165 * strlen(delta)
166 * +
167 * :
168 * +
169 * strlen(src)
170 * +
171 * \0
172 */
173 src = lxc_storage_get_path(orig->src, orig->type);
174 len = 8 + strlen(delta) + 1 + strlen(src) + 1;
175 new->src = malloc(len);
176 if (!new->src) {
177 ERROR("Failed to allocate memory");
178 free(delta);
179 return -ENOMEM;
180 }
181
182 ret = snprintf(new->src, len, "overlay:%s:%s", src, delta);
183 free(delta);
184 if (ret < 0 || (size_t)ret >= len) {
185 ERROR("Failed to create string");
186 return -1;
187 }
188 } else if (!strcmp(orig->type, "overlayfs") ||
189 !strcmp(orig->type, "overlay")) {
190 char *clean_old_path, *clean_new_path;
191 char *lastslash, *ndelta, *nsrc, *odelta, *osrc, *s1, *s2, *s3,
192 *work;
193 int ret, lastslashidx;
194 size_t len, name_len;
195
196 osrc = strdup(orig->src);
197 if (!osrc) {
198 ERROR("Failed to duplicate string \"%s\"", orig->src);
199 return -22;
200 }
201
202 nsrc = osrc;
203 if (strncmp(osrc, "overlay:", 8) == 0)
204 nsrc += 8;
205 else if (strncmp(osrc, "overlayfs:", 10) == 0)
206 nsrc += 10;
207
208 odelta = strchr(nsrc, ':');
209 if (!odelta) {
210 ERROR("Failed to find \":\" in \"%s\"", nsrc);
211 free(osrc);
212 return -22;
213 }
214
215 *odelta = '\0';
216 odelta++;
217 ndelta = must_make_path(lxcpath, cname, "delta0", NULL);
218
219 ret = mkdir(ndelta, 0755);
220 if (ret < 0 && errno != EEXIST) {
221 SYSERROR("Failed to create directory \"%s\"", ndelta);
222 free(osrc);
223 free(ndelta);
224 return -1;
225 }
226
227 if (am_guest_unpriv()) {
228 ret = chown_mapped_root(ndelta, conf);
229 if (ret < 0)
230 WARN("Failed to update ownership of %s",
231 ndelta);
232 }
233
234 /* Make workdir for overlayfs.v22 or higher (See the comment
235 * further up.).
236 */
237 lastslash = strrchr(ndelta, '/');
238 if (!lastslash) {
239 ERROR("Failed to detect \"/\" in \"%s\"", ndelta);
240 free(osrc);
241 free(ndelta);
242 return -1;
243 }
244 lastslash++;
245 lastslashidx = lastslash - ndelta;
246
247 work = malloc(lastslashidx + 7);
248 if (!work) {
249 free(osrc);
250 free(ndelta);
251 ERROR("Failed to allocate memory");
252 return -1;
253 }
254
255 memcpy(work, ndelta, lastslashidx + 1);
256 memcpy(work + lastslashidx, "olwork", sizeof("olwork") - 1);
257 work[lastslashidx + sizeof("olwork") - 1] = '\0';
258
259 ret = mkdir(work, 0755);
260 if (ret < 0 && errno != EEXIST) {
261 SYSERROR("Failed to create directory \"%s\"", ndelta);
262 free(osrc);
263 free(ndelta);
264 free(work);
265 return -1;
266 }
267
268 if (am_guest_unpriv()) {
269 ret = chown_mapped_root(work, conf);
270 if (ret < 0)
271 WARN("Failed to update ownership of %s", work);
272 }
273 free(work);
274
275 /* strlen("overlay:") = 8
276 * +
277 * strlen(delta)
278 * +
279 * :
280 * +
281 * strlen(src)
282 * +
283 * \0
284 */
285 len = 8 + strlen(ndelta) + 1 + strlen(nsrc) + 1;
286 new->src = malloc(len);
287 if (!new->src) {
288 free(osrc);
289 free(ndelta);
290 ERROR("Failed to allocate memory");
291 return -ENOMEM;
292 }
293 ret = snprintf(new->src, len, "overlay:%s:%s", nsrc, ndelta);
294 if (ret < 0 || (size_t)ret >= len) {
295 ERROR("Failed to create string");
296 free(osrc);
297 free(ndelta);
298 return -1;
299 }
300
301 ret = ovl_do_rsync(odelta, ndelta, conf);
302 free(osrc);
303 free(ndelta);
304 if (ret < 0)
305 return -1;
306
307 /* When we create an overlay snapshot of an overlay container in
308 * the snapshot directory under "<lxcpath>/<name>/snaps/" we
309 * don't need to record a dependency. If we would restore would
310 * also fail.
311 */
312 clean_old_path = lxc_deslashify(oldpath);
313 if (!clean_old_path)
314 return -1;
315
316 clean_new_path = lxc_deslashify(lxcpath);
317 if (!clean_new_path) {
318 free(clean_old_path);
319 return -1;
320 }
321
322 s1 = strrchr(clean_old_path, '/');
323 if (!s1) {
324 ERROR("Failed to detect \"/\" in string \"%s\"", clean_old_path);
325 free(clean_old_path);
326 free(clean_new_path);
327 return -1;
328 }
329
330 s2 = strrchr(clean_new_path, '/');
331 if (!s2) {
332 ERROR("Failed to detect \"/\" in string \"%s\"", clean_new_path);
333 free(clean_old_path);
334 free(clean_new_path);
335 return -1;
336 }
337
338 if (!strncmp(s1, "/snaps", sizeof("/snaps") - 1)) {
339 s1 = clean_new_path;
340 s2 = clean_old_path;
341 s3 = (char *)cname;
342 } else if (!strncmp(s2, "/snaps", sizeof("/snaps") - 1)) {
343 s1 = clean_old_path;
344 s2 = clean_new_path;
345 s3 = (char *)oldname;
346 } else {
347 free(clean_old_path);
348 free(clean_new_path);
349 return 0;
350 }
351
352 len = strlen(s1);
353 if (!strncmp(s1, s2, len)) {
354 char *tmp;
355
356 tmp = (char *)(s2 + len + 1);
357 if (*tmp == '\0') {
358 free(clean_old_path);
359 free(clean_new_path);
360 return 0;
361 }
362
363 name_len = strlen(s3);
364 if (strncmp(s3, tmp, name_len)) {
365 free(clean_old_path);
366 free(clean_new_path);
367 return 0;
368 }
369
370 free(clean_old_path);
371 free(clean_new_path);
372 return LXC_CLONE_SNAPSHOT;
373 }
374
375 free(clean_old_path);
376 free(clean_new_path);
377 return 0;
378 } else {
379 ERROR("overlay clone of %s container is not yet supported",
380 orig->type);
381 /* Note, supporting this will require ovl_mount supporting
382 * mounting of the underlay. No big deal, just needs to be done.
383 */
384 return -1;
385 }
386
387 return 0;
388 }
389
390 /* To say "lxc-create -t ubuntu -n o1 -B overlay" means you want
391 * "<lxcpath>/<lxcname>/rootfs" to have the created container, while all changes
392 * after starting the container are written to "<lxcpath>/<lxcname>/delta0".
393 */
394 int ovl_create(struct lxc_storage *bdev, const char *dest, const char *n,
395 struct bdev_specs *specs)
396 {
397 char *delta;
398 int ret;
399 size_t len, newlen;
400
401 len = strlen(dest);
402 if (len < 8 || strcmp(dest + len - 7, "/rootfs")) {
403 ERROR("Failed to detect \"/rootfs\" in \"%s\"", dest);
404 return -1;
405 }
406
407 bdev->dest = strdup(dest);
408 if (!bdev->dest) {
409 ERROR("Failed to duplicate string \"%s\"", dest);
410 return -1;
411 }
412
413 delta = strdup(dest);
414 if (!delta) {
415 ERROR("Failed to allocate memory");
416 return -1;
417 }
418 memcpy(delta + len - 6, "delta0", sizeof("delta0") - 1);
419
420 ret = mkdir_p(delta, 0755);
421 if (ret < 0) {
422 SYSERROR("Failed to create directory \"%s\"", delta);
423 free(delta);
424 return -1;
425 }
426
427 /* overlay:lower:upper */
428 newlen = (2 * len) + strlen("overlay:") + 2;
429 bdev->src = malloc(newlen);
430 if (!bdev->src) {
431 ERROR("Failed to allocate memory");
432 free(delta);
433 return -1;
434 }
435
436 ret = snprintf(bdev->src, newlen, "overlay:%s:%s", dest, delta);
437 if (ret < 0 || (size_t)ret >= newlen) {
438 ERROR("Failed to create string");
439 free(delta);
440 return -1;
441 }
442
443 ret = mkdir_p(bdev->dest, 0755);
444 if (ret < 0) {
445 SYSERROR("Failed to create directory \"%s\"", bdev->dest);
446 free(delta);
447 return -1;
448 }
449
450 free(delta);
451 return 0;
452 }
453
454 int ovl_destroy(struct lxc_storage *orig)
455 {
456 char *upper = orig->src;
457
458 /* For an overlay container the rootfs is considered immutable
459 * and cannot be removed when restoring from a snapshot.
460 */
461 if (orig->flags & LXC_STORAGE_INTERNAL_OVERLAY_RESTORE)
462 return 0;
463
464 if (strncmp(upper, "overlay:", 8) == 0)
465 upper += 8;
466 else if (strncmp(upper, "overlayfs:", 10) == 0)
467 upper += 10;
468
469 upper = strchr(upper, ':');
470 if (!upper)
471 return -22;
472 upper++;
473
474 return lxc_rmdir_onedev(upper, NULL);
475 }
476
477 bool ovl_detect(const char *path)
478 {
479 if (!strncmp(path, "overlay:", 8))
480 return true;
481
482 if (!strncmp(path, "overlayfs:", 10))
483 return true;
484
485 return false;
486 }
487
488 int ovl_mount(struct lxc_storage *bdev)
489 {
490 char *tmp, *options, *dup, *lower, *upper;
491 char *options_work, *work, *lastslash;
492 int lastslashidx;
493 size_t len, len2;
494 unsigned long mntflags;
495 char *mntdata;
496 int ret, ret2;
497
498 if (strcmp(bdev->type, "overlay") && strcmp(bdev->type, "overlayfs"))
499 return -22;
500
501 if (!bdev->src || !bdev->dest)
502 return -22;
503
504 if (!ovl_name)
505 ovl_name = ovl_detect_name();
506
507 /* Separately mount it first:
508 * mount -t overlay * -o upperdir=${upper},lowerdir=${lower} lower dest
509 */
510 dup = strdup(bdev->src);
511 if (!dup) {
512 ERROR("Failed to allocate memory");
513 return -1;
514 }
515 upper = dup;
516 lower = dup;
517
518 if (strncmp(dup, "overlay:", 8) == 0)
519 lower += 8;
520 else if (strncmp(dup, "overlayfs:", 10) == 0)
521 lower += 10;
522 if (upper != lower)
523 upper = lower;
524
525 /* support multiple lower layers */
526 while ((tmp = strstr(upper, ":/"))) {
527 tmp++;
528 upper = tmp;
529 }
530
531 upper--;
532 if (upper == lower) {
533 free(dup);
534 return -22;
535 }
536 *upper = '\0';
537 upper++;
538
539 /* if delta doesn't yet exist, create it */
540 ret = mkdir_p(upper, 0755);
541 if (ret < 0 && errno != EEXIST) {
542 SYSERROR("Failed to create directory \"%s\"", upper);
543 free(dup);
544 return -22;
545 }
546
547 /* overlayfs.v22 or higher needs workdir option:
548 * if upper is
549 * /var/lib/lxc/c2/delta0
550 * then workdir is
551 * /var/lib/lxc/c2/olwork
552 */
553 lastslash = strrchr(upper, '/');
554 if (!lastslash) {
555 ERROR("Failed to detect \"/\" in string \"%s\"", upper);
556 free(dup);
557 return -22;
558 }
559
560 lastslash++;
561 lastslashidx = lastslash - upper;
562
563 work = malloc(lastslashidx + 7);
564 if (!work) {
565 ERROR("Failed to allocate memory");
566 free(dup);
567 return -22;
568 }
569
570 memcpy(work, upper, lastslashidx + 1);
571 memcpy(work + lastslashidx, "olwork", sizeof("olwork") - 1);
572 work[lastslashidx + sizeof("olwork") - 1] = '\0';
573
574 ret = parse_mntopts(bdev->mntopts, &mntflags, &mntdata);
575 if (ret < 0) {
576 ERROR("Failed to parse mount options");
577 free(mntdata);
578 free(dup);
579 free(work);
580 return -22;
581 }
582
583 ret = mkdir_p(work, 0755);
584 if (ret < 0 && errno != EEXIST) {
585 SYSERROR("Failed to create directory \"%s\"", work);
586 free(mntdata);
587 free(dup);
588 free(work);
589 return -22;
590 }
591
592 /*
593 * TODO:
594 * We should check whether bdev->src is a blockdev but for now only
595 * support overlays of a basic directory
596 */
597
598 if (mntdata) {
599 len = strlen(lower) + strlen(upper) +
600 strlen("upperdir=,lowerdir=,") + strlen(mntdata) + 1;
601 options = alloca(len);
602 ret = snprintf(options, len, "upperdir=%s,lowerdir=%s,%s",
603 upper, lower, mntdata);
604
605 len2 = strlen(lower) + strlen(upper) + strlen(work) +
606 strlen("upperdir=,lowerdir=,workdir=") +
607 strlen(mntdata) + 1;
608 options_work = alloca(len2);
609 ret2 = snprintf(options, len2,
610 "upperdir=%s,lowerdir=%s,workdir=%s,%s", upper,
611 lower, work, mntdata);
612 } else {
613 len = strlen(lower) + strlen(upper) +
614 strlen("upperdir=,lowerdir=") + 1;
615 options = alloca(len);
616 ret = snprintf(options, len, "upperdir=%s,lowerdir=%s", upper,
617 lower);
618
619 len2 = strlen(lower) + strlen(upper) + strlen(work) +
620 strlen("upperdir=,lowerdir=,workdir=") + 1;
621 options_work = alloca(len2);
622 ret2 = snprintf(options_work, len2,
623 "upperdir=%s,lowerdir=%s,workdir=%s", upper,
624 lower, work);
625 }
626
627 if (ret < 0 || ret >= len || ret2 < 0 || ret2 >= len2) {
628 ERROR("Failed to create string");
629 free(mntdata);
630 free(dup);
631 free(work);
632 return -1;
633 }
634
635 /* Assume we need a workdir as we are on a overlay version >= v22. */
636 ret = ovl_remount_on_enodev(lower, bdev->dest, ovl_name,
637 MS_MGC_VAL | mntflags, options_work);
638 if (ret < 0) {
639 INFO("Failed to mount \"%s\" on \"%s\" with options \"%s\". "
640 "Retrying without workdir: %s",
641 lower, bdev->dest, options_work, strerror(errno));
642
643 /* Assume we cannot use a workdir as we are on a version <= v21.
644 */
645 ret = ovl_remount_on_enodev(lower, bdev->dest, ovl_name,
646 MS_MGC_VAL | mntflags, options);
647 if (ret < 0)
648 SYSERROR("Failed to mount \"%s\" on \"%s\" with "
649 "options \"%s\": %s",
650 lower, bdev->dest, options, strerror(errno));
651 else
652 INFO("Mounted \"%s\" on \"%s\" with options \"%s\"",
653 lower, bdev->dest, options);
654 } else {
655 INFO("Mounted \"%s\" on \"%s\" with options \"%s\"", lower,
656 bdev->dest, options_work);
657 }
658
659 free(dup);
660 free(work);
661 return ret;
662 }
663
664 int ovl_umount(struct lxc_storage *bdev)
665 {
666 int ret;
667
668 if (strcmp(bdev->type, "overlay") && strcmp(bdev->type, "overlayfs"))
669 return -22;
670
671 if (!bdev->src || !bdev->dest)
672 return -22;
673
674 ret = umount(bdev->dest);
675 if (ret < 0)
676 SYSERROR("Failed to unmount \"%s\"", bdev->dest);
677 else
678 TRACE("Unmounted \"%s\"", bdev->dest);
679
680 return ret;
681 }
682
683 const char *ovl_get_lower(const char *rootfs_path)
684 {
685 const char *s1 = rootfs_path;
686
687 if (strncmp(rootfs_path, "overlay:", 8) == 0)
688 s1 += 8;
689 else if (strncmp(rootfs_path, "overlayfs:", 10) == 0)
690 s1 += 10;
691
692 s1 = strstr(s1, ":/");
693 if (!s1)
694 return NULL;
695 s1++;
696
697 return s1;
698 }
699
700 char *ovl_get_rootfs(const char *rootfs_path, size_t *rootfslen)
701 {
702 char *rootfsdir = NULL;
703 char *s1 = NULL;
704 char *s2 = NULL;
705 char *s3 = NULL;
706
707 if (!rootfs_path || !rootfslen)
708 return NULL;
709
710 s1 = strdup(rootfs_path);
711 if (!s1)
712 return NULL;
713
714 s2 = s1;
715 if (strncmp(rootfs_path, "overlay:", 8) == 0)
716 s2 += 8;
717 else if (strncmp(rootfs_path, "overlayfs:", 10) == 0)
718 s2 += 10;
719
720 s3 = strstr(s2, ":/");
721 if (s3)
722 *s3 = '\0';
723
724 rootfsdir = strdup(s2);
725 free(s1);
726 if (!rootfsdir)
727 return NULL;
728
729 *rootfslen = strlen(rootfsdir);
730
731 return rootfsdir;
732 }
733
734 int ovl_mkdir(const struct mntent *mntent, const struct lxc_rootfs *rootfs,
735 const char *lxc_name, const char *lxc_path)
736 {
737 char lxcpath[MAXPATHLEN];
738 char **opts;
739 int ret;
740 size_t arrlen, i, len, rootfslen;
741 int fret = -1;
742 size_t dirlen = 0;
743 char *rootfs_dir = NULL, *rootfs_path = NULL, *upperdir = NULL,
744 *workdir = NULL;
745
746 /* When rootfs == NULL we have a container without a rootfs. */
747 if (rootfs && rootfs->path)
748 rootfs_path = rootfs->path;
749
750 opts = lxc_string_split(mntent->mnt_opts, ',');
751 if (opts)
752 arrlen = lxc_array_len((void **)opts);
753 else
754 goto err;
755
756 for (i = 0; i < arrlen; i++) {
757 if (strstr(opts[i], "upperdir=") &&
758 (strlen(opts[i]) > (len = strlen("upperdir="))))
759 upperdir = opts[i] + len;
760 else if (strstr(opts[i], "workdir=") &&
761 (strlen(opts[i]) > (len = strlen("workdir="))))
762 workdir = opts[i] + len;
763 }
764
765 if (rootfs_path) {
766 ret = snprintf(lxcpath, MAXPATHLEN, "%s/%s", lxc_path, lxc_name);
767 if (ret < 0 || ret >= MAXPATHLEN)
768 goto err;
769
770 rootfs_dir = ovl_get_rootfs(rootfs_path, &rootfslen);
771 if (!rootfs_dir)
772 goto err;
773
774 dirlen = strlen(lxcpath);
775 }
776
777 /*
778 * We neither allow users to create upperdirs and workdirs outside the
779 * containerdir nor inside the rootfs. The latter might be debatable.
780 * When we have a container without a rootfs we skip the checks.
781 */
782 ret = 0;
783 if (upperdir) {
784 if (!rootfs_path)
785 ret = mkdir_p(upperdir, 0755);
786 else if (!strncmp(upperdir, lxcpath, dirlen) &&
787 strncmp(upperdir, rootfs_dir, rootfslen))
788 ret = mkdir_p(upperdir, 0755);
789
790 if (ret < 0)
791 SYSWARN("Failed to create directory \"%s\"", upperdir);
792 }
793
794 ret = 0;
795 if (workdir) {
796 if (!rootfs_path)
797 ret = mkdir_p(workdir, 0755);
798 else if (!strncmp(workdir, lxcpath, dirlen) &&
799 strncmp(workdir, rootfs_dir, rootfslen))
800 ret = mkdir_p(workdir, 0755);
801
802 if (ret < 0)
803 SYSWARN("Failed to create directory \"%s\"", workdir);
804 }
805
806 fret = 0;
807
808 err:
809 free(rootfs_dir);
810 lxc_free_array((void **)opts, free);
811 return fret;
812 }
813
814 /* To be called from lxcapi_clone() in lxccontainer.c: When we clone a container
815 * with overlay lxc.mount.entry entries we need to update absolute paths for
816 * upper- and workdir. This update is done in two locations:
817 * lxc_conf->unexpanded_config and lxc_conf->mount_list. Both updates are done
818 * independent of each other since lxc_conf->mountlist may contain more mount
819 * entries (e.g. from other included files) than lxc_conf->unexpanded_config.
820 */
821 int ovl_update_abs_paths(struct lxc_conf *lxc_conf, const char *lxc_path,
822 const char *lxc_name, const char *newpath,
823 const char *newname)
824 {
825 char new_upper[MAXPATHLEN], new_work[MAXPATHLEN], old_upper[MAXPATHLEN],
826 old_work[MAXPATHLEN];
827 size_t i;
828 struct lxc_list *iterator;
829 char *cleanpath = NULL;
830 int fret = -1;
831 int ret = 0;
832 const char *ovl_dirs[] = {"br", "upperdir", "workdir"};
833
834 cleanpath = strdup(newpath);
835 if (!cleanpath)
836 goto err;
837
838 remove_trailing_slashes(cleanpath);
839
840 /*
841 * We have to update lxc_conf->unexpanded_config separately from
842 * lxc_conf->mount_list.
843 */
844 for (i = 0; i < sizeof(ovl_dirs) / sizeof(ovl_dirs[0]); i++) {
845 if (!clone_update_unexp_ovl_paths(lxc_conf, lxc_path, newpath,
846 lxc_name, newname,
847 ovl_dirs[i]))
848 goto err;
849 }
850
851 ret =
852 snprintf(old_work, MAXPATHLEN, "workdir=%s/%s", lxc_path, lxc_name);
853 if (ret < 0 || ret >= MAXPATHLEN)
854 goto err;
855
856 ret =
857 snprintf(new_work, MAXPATHLEN, "workdir=%s/%s", cleanpath, newname);
858 if (ret < 0 || ret >= MAXPATHLEN)
859 goto err;
860
861 lxc_list_for_each(iterator, &lxc_conf->mount_list) {
862 char *mnt_entry = NULL, *new_mnt_entry = NULL, *tmp = NULL,
863 *tmp_mnt_entry = NULL;
864
865 mnt_entry = iterator->elem;
866
867 if (strstr(mnt_entry, "overlay"))
868 tmp = "upperdir";
869 if (!tmp)
870 continue;
871
872 ret = snprintf(old_upper, MAXPATHLEN, "%s=%s/%s", tmp, lxc_path,
873 lxc_name);
874 if (ret < 0 || ret >= MAXPATHLEN)
875 goto err;
876
877 ret = snprintf(new_upper, MAXPATHLEN, "%s=%s/%s", tmp,
878 cleanpath, newname);
879 if (ret < 0 || ret >= MAXPATHLEN)
880 goto err;
881
882 if (strstr(mnt_entry, old_upper)) {
883 tmp_mnt_entry =
884 lxc_string_replace(old_upper, new_upper, mnt_entry);
885 }
886
887 if (strstr(mnt_entry, old_work)) {
888 if (tmp_mnt_entry)
889 new_mnt_entry = lxc_string_replace(
890 old_work, new_work, tmp_mnt_entry);
891 else
892 new_mnt_entry = lxc_string_replace(
893 old_work, new_work, mnt_entry);
894 }
895
896 if (new_mnt_entry) {
897 free(iterator->elem);
898 iterator->elem = strdup(new_mnt_entry);
899 } else if (tmp_mnt_entry) {
900 free(iterator->elem);
901 iterator->elem = strdup(tmp_mnt_entry);
902 }
903
904 free(new_mnt_entry);
905 free(tmp_mnt_entry);
906 }
907
908 fret = 0;
909 err:
910 free(cleanpath);
911 return fret;
912 }
913
914 static int ovl_remount_on_enodev(const char *lower, const char *target,
915 const char *name, unsigned long mountflags,
916 const void *options)
917 {
918 int ret;
919 ret = mount(lower, target, ovl_name, MS_MGC_VAL | mountflags, options);
920 if (ret < 0 && errno == ENODEV) /* Try other module name. */
921 ret = mount(lower, target,
922 ovl_name == ovl_version[0] ? ovl_version[1]
923 : ovl_version[0],
924 MS_MGC_VAL | mountflags, options);
925 return ret;
926 }
927
928 static char *ovl_detect_name(void)
929 {
930 FILE *f;
931 char *v = ovl_version[0];
932 char *line = NULL;
933 size_t len = 0;
934
935 f = fopen("/proc/filesystems", "r");
936 if (!f)
937 return v;
938
939 while (getline(&line, &len, f) != -1) {
940 if (strcmp(line, "nodev\toverlayfs\n") == 0) {
941 v = ovl_version[1];
942 break;
943 }
944 }
945
946 fclose(f);
947 free(line);
948 return v;
949 }
950
951 static int ovl_do_rsync(const char *src, const char *dest,
952 struct lxc_conf *conf)
953 {
954 int ret = -1;
955 struct rsync_data_char rdata = {0};
956 char cmd_output[MAXPATHLEN] = {0};
957
958 rdata.src = (char *)src;
959 rdata.dest = (char *)dest;
960 if (am_guest_unpriv())
961 ret = userns_exec_full(conf, lxc_rsync_exec_wrapper, &rdata,
962 "lxc_rsync_exec_wrapper");
963 else
964 ret = run_command(cmd_output, sizeof(cmd_output),
965 lxc_rsync_exec_wrapper, (void *)&rdata);
966 if (ret < 0)
967 ERROR("Failed to rsync from \"%s\" into \"%s\"%s%s", src, dest,
968 cmd_output[0] != '\0' ? ": " : "",
969 cmd_output[0] != '\0' ? cmd_output : "");
970
971 return ret;
972 }