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