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