]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/tools/lxc_copy.c
caps: fix includes
[mirror_lxc.git] / src / lxc / tools / lxc_copy.c
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #ifndef _GNU_SOURCE
4 #define _GNU_SOURCE 1
5 #endif
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <getopt.h>
9 #include <signal.h>
10 #include <stdbool.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
18 #include <time.h>
19 #include <unistd.h>
20
21 #include <lxc/lxccontainer.h>
22
23 #include "arguments.h"
24 #include "config.h"
25 #include "log.h"
26 #include "storage_utils.h"
27 #include "utils.h"
28
29 #ifndef HAVE_GETSUBOPT
30 #include "getsubopt.h"
31 #endif
32
33 lxc_log_define(lxc_copy, lxc);
34
35 enum mnttype {
36 LXC_MNT_BIND,
37 LXC_MNT_OVL,
38 };
39
40 struct mnts {
41 enum mnttype mnt_type;
42 char *src;
43 char *dest;
44 char *options;
45 char *upper;
46 char *workdir;
47 char *lower;
48 };
49
50 static unsigned int mnt_table_size = 0;
51 static struct mnts *mnt_table = NULL;
52
53 static int my_parser(struct lxc_arguments *args, int c, char *arg);
54
55 static const struct option my_longopts[] = {
56 { "newname", required_argument, 0, 'N'},
57 { "newpath", required_argument, 0, 'p'},
58 { "rename", no_argument, 0, 'R'},
59 { "snapshot", no_argument, 0, 's'},
60 { "allowrunning", no_argument, 0, 'a'},
61 { "foreground", no_argument, 0, 'F'},
62 { "daemon", no_argument, 0, 'd'},
63 { "ephemeral", no_argument, 0, 'e'},
64 { "mount", required_argument, 0, 'm'},
65 { "backingstorage", required_argument, 0, 'B'},
66 { "fssize", required_argument, 0, 'L'},
67 { "keepdata", no_argument, 0, 'D'},
68 { "keepname", no_argument, 0, 'K'},
69 { "keepmac", no_argument, 0, 'M'},
70 { "tmpfs", no_argument, 0, 't'},
71 LXC_COMMON_OPTIONS
72 };
73
74 /* mount keys */
75 static char *const mount_keys[] = {
76 [LXC_MNT_BIND] = "bind",
77 [LXC_MNT_OVL] = "overlay",
78 NULL
79 };
80
81 static struct lxc_arguments my_args = {
82 .progname = "lxc-copy",
83 .help = "\n\
84 --name=NAME [-P lxcpath] -N newname [-p newpath] [-B backingstorage] [-s] [-K] [-M] [-L size [unit]] -- hook options\n\
85 --name=NAME [-P lxcpath] [-N newname] [-p newpath] [-B backingstorage] -e [-d] [-D] [-K] [-M] [-m {bind,overlay}=/src:/dest] -- hook options\n\
86 --name=NAME [-P lxcpath] -N newname -R\n\
87 \n\
88 lxc-copy clone a container\n\
89 \n\
90 Options :\n\
91 -n, --name=NAME NAME of the container\n\
92 -N, --newname=NEWNAME NEWNAME for the restored container\n\
93 -p, --newpath=NEWPATH NEWPATH for the container to be stored\n\
94 -R, --rename rename container\n\
95 -s, --snapshot create snapshot instead of clone\n\
96 -a, --allowrunning allow snapshot creation even if source container is running\n\
97 -F, --foreground start with current tty attached to /dev/console\n\
98 -d, --daemon daemonize the container (default)\n\
99 -e, --ephemeral start ephemeral container\n\
100 -m, --mount directory to mount into container, either \n\
101 {bind,overlay}=/src-path or {bind,overlay}=/src-path:/dst-path\n\
102 -B, --backingstorage=TYPE backingstorage type for the container\n\
103 -t, --tmpfs place ephemeral container on a tmpfs\n\
104 (WARNING: On reboot all changes made to the container will be lost.)\n\
105 -L, --fssize size of the new block device for block device containers\n\
106 -D, --keepdata pass together with -e start a persistent snapshot \n\
107 -K, --keepname keep the hostname of the original container\n\
108 -- hook options arguments passed to the hook program\n\
109 -M, --keepmac keep the MAC address of the original container\n\
110 --rcfile=FILE Load configuration file FILE\n",
111 .options = my_longopts,
112 .parser = my_parser,
113 .task = CLONE,
114 .daemonize = 1,
115 .quiet = false,
116 .tmpfs = false,
117 };
118
119 static struct mnts *add_mnt(struct mnts **mnts, unsigned int *num,
120 enum mnttype type);
121 static int mk_rand_ovl_dirs(struct mnts *mnts, unsigned int num,
122 struct lxc_arguments *arg);
123 static char *construct_path(char *path, bool as_prefix);
124 static char *set_mnt_entry(struct mnts *m);
125 static int do_clone(struct lxc_container *c, char *newname, char *newpath,
126 int flags, char *bdevtype, uint64_t fssize, enum task task,
127 char **args);
128 static int do_clone_ephemeral(struct lxc_container *c,
129 struct lxc_arguments *arg, char **args,
130 int flags);
131 static int do_clone_rename(struct lxc_container *c, char *newname);
132 static int do_clone_task(struct lxc_container *c, enum task task, int flags,
133 char **args);
134 static void free_mnts(void);
135
136 /* Place an ephemeral container started with -e flag on a tmpfs. Restrictions
137 * are that you cannot request the data to be kept while placing the container
138 * on a tmpfs and that either overlay storage driver must be used.
139 */
140 static char *mount_tmpfs(const char *oldname, const char *newname,
141 const char *path, struct lxc_arguments *arg);
142 static int parse_mntsubopts(char *subopts, char *const *keys,
143 char *mntparameters);
144 static int parse_bind_mnt(char *mntstring, enum mnttype type);
145 static int parse_ovl_mnt(char *mntstring, enum mnttype type);
146
147 int main(int argc, char *argv[])
148 {
149 struct lxc_container *c;
150 struct lxc_log log;
151 int flags = 0;
152 int ret = EXIT_FAILURE;
153
154 if (lxc_arguments_parse(&my_args, argc, argv))
155 exit(ret);
156
157 /* Only create log if explicitly instructed */
158 if (my_args.log_file || my_args.log_priority) {
159 log.name = my_args.name;
160 log.file = my_args.log_file;
161 log.level = my_args.log_priority;
162 log.prefix = my_args.progname;
163 log.quiet = my_args.quiet;
164 log.lxcpath = my_args.lxcpath[0];
165
166 if (lxc_log_init(&log))
167 exit(ret);
168 }
169
170 if (geteuid()) {
171 if (access(my_args.lxcpath[0], O_RDONLY) < 0) {
172 ERROR("You lack access to %s", my_args.lxcpath[0]);
173 exit(ret);
174 }
175 }
176
177 if (!my_args.newname && !(my_args.task == DESTROY)) {
178 ERROR("You must provide a NEWNAME for the clone");
179 exit(ret);
180 }
181
182 if (my_args.task == SNAP || my_args.task == DESTROY)
183 flags |= LXC_CLONE_SNAPSHOT;
184 if (my_args.allowrunning)
185 flags |= LXC_CLONE_ALLOW_RUNNING;
186 if (my_args.keepname)
187 flags |= LXC_CLONE_KEEPNAME;
188
189 if (my_args.keepmac)
190 flags |= LXC_CLONE_KEEPMACADDR;
191
192 if (!my_args.newpath)
193 my_args.newpath = (char *)my_args.lxcpath[0];
194
195 c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
196 if (!c)
197 exit(ret);
198
199 if (my_args.rcfile) {
200 c->clear_config(c);
201
202 if (!c->load_config(c, my_args.rcfile)) {
203 ERROR("Failed to load rcfile");
204 goto out;
205 }
206
207 c->configfile = strdup(my_args.rcfile);
208 if (!c->configfile) {
209 ERROR("Out of memory setting new config filename");
210 goto out;
211 }
212 }
213
214 if (!c->may_control(c)) {
215 ERROR("Insufficent privileges to control %s", c->name);
216 goto out;
217 }
218
219 if (!c->is_defined(c)) {
220 ERROR("Container %s is not defined", c->name);
221 goto out;
222 }
223
224 ret = do_clone_task(c, my_args.task, flags, &argv[optind]);
225
226 out:
227 lxc_container_put(c);
228
229 if (ret == 0)
230 exit(EXIT_SUCCESS);
231 exit(EXIT_FAILURE);
232 }
233
234 static struct mnts *add_mnt(struct mnts **mnts, unsigned int *num, enum mnttype type)
235 {
236 struct mnts *m, *n;
237
238 n = realloc(*mnts, (*num + 1) * sizeof(struct mnts));
239 if (!n)
240 return NULL;
241
242 *mnts = n;
243 m = *mnts + *num;
244 (*num)++;
245
246 *m = (struct mnts) {.mnt_type = type};
247
248 return m;
249 }
250
251 static int mk_rand_ovl_dirs(struct mnts *mnts, unsigned int num, struct lxc_arguments *arg)
252 {
253 char upperdir[PATH_MAX];
254 char workdir[PATH_MAX];
255 unsigned int i;
256 int ret;
257 struct mnts *m = NULL;
258
259 for (i = 0, m = mnts; i < num; i++, m++) {
260 if (m->mnt_type == LXC_MNT_OVL) {
261 ret = snprintf(upperdir, PATH_MAX, "%s/%s/delta#XXXXXX",
262 arg->newpath, arg->newname);
263 if (ret < 0 || ret >= PATH_MAX)
264 return -1;
265
266 if (!mkdtemp(upperdir))
267 return -1;
268
269 m->upper = strdup(upperdir);
270 if (!m->upper)
271 return -1;
272 }
273
274 if (m->mnt_type == LXC_MNT_OVL) {
275 ret = snprintf(workdir, PATH_MAX, "%s/%s/work#XXXXXX",
276 arg->newpath, arg->newname);
277 if (ret < 0 || ret >= PATH_MAX)
278 return -1;
279
280 if (!mkdtemp(workdir))
281 return -1;
282
283 m->workdir = strdup(workdir);
284 if (!m->workdir)
285 return -1;
286 }
287 }
288
289 return 0;
290 }
291
292 static char **lxc_normalize_path(const char *path)
293 {
294 char **components;
295 size_t components_len = 0;
296 size_t pos = 0;
297
298 components = lxc_string_split(path, '/');
299 if (!components)
300 return NULL;
301
302 /* resolve '.' and '..' */
303 for (pos = 0; pos < components_len;) {
304 if (strequal(components[pos], ".") ||
305 (strequal(components[pos], "..") && pos == 0)) {
306 /* eat this element */
307 free(components[pos]);
308 memmove(&components[pos], &components[pos + 1],
309 sizeof(char *) * (components_len - pos));
310 components_len--;
311 } else if (strequal(components[pos], "..")) {
312 /* eat this and the previous element */
313 free(components[pos - 1]);
314 free(components[pos]);
315 memmove(&components[pos - 1], &components[pos + 1],
316 sizeof(char *) * (components_len - pos));
317 components_len -= 2;
318 pos--;
319 } else {
320 pos++;
321 }
322 }
323
324 return components;
325 }
326
327 static char *construct_path(char *path, bool as_prefix)
328 {
329 char **components = NULL;
330 char *cleanpath = NULL;
331
332 components = lxc_normalize_path(path);
333 if (!components)
334 return NULL;
335
336 cleanpath = lxc_string_join("/", (const char **)components, as_prefix);
337 lxc_free_array((void **)components, free);
338
339 return cleanpath;
340 }
341
342 static char *set_mnt_entry(struct mnts *m)
343 {
344 char *mntentry = NULL;
345 int ret = 0;
346 size_t len = 0;
347
348 if (m->mnt_type == LXC_MNT_OVL) {
349 len = strlen(" overlay lowerdir=,upperdir=,workdir=,create=dir") +
350 2 * strlen(m->src) + strlen(m->dest) + strlen(m->upper) +
351 strlen(m->workdir) + 1;
352
353 mntentry = malloc(len);
354 if (!mntentry)
355 goto err;
356
357 ret = snprintf(mntentry, len, "%s %s overlay lowerdir=%s,upperdir=%s,workdir=%s,create=dir",
358 m->src, m->dest, m->src, m->upper, m->workdir);
359 if (ret < 0 || (size_t)ret >= len)
360 goto err;
361 } else if (m->mnt_type == LXC_MNT_BIND) {
362 len = strlen(" none bind,optional,, 0 0") +
363 strlen(is_dir(m->src) ? "create=dir" : "create=file") +
364 strlen(m->src) + strlen(m->dest) + strlen(m->options) + 1;
365
366 mntentry = malloc(len);
367 if (!mntentry)
368 goto err;
369
370 ret = snprintf(mntentry, len, "%s %s none bind,optional,%s,%s 0 0",
371 m->src, m->dest, m->options,
372 is_dir(m->src) ? "create=dir" : "create=file");
373 if (ret < 0 || (size_t)ret >= len)
374 goto err;
375 }
376
377 return mntentry;
378
379 err:
380 free(mntentry);
381 return NULL;
382 }
383
384 static int do_clone(struct lxc_container *c, char *newname, char *newpath,
385 int flags, char *bdevtype, uint64_t fssize, enum task task,
386 char **args)
387 {
388 struct lxc_container *clone;
389
390 clone = c->clone(c, newname, newpath, flags, bdevtype, NULL, fssize,
391 args);
392 if (!clone) {
393 ERROR("Failed to clone");
394 return -1;
395 }
396
397 lxc_container_put(clone);
398
399 return 0;
400 }
401
402 static int do_clone_ephemeral(struct lxc_container *c,
403 struct lxc_arguments *arg, char **args, int flags)
404 {
405 char *premount;
406 char randname[PATH_MAX];
407 unsigned int i;
408 int ret = 0;
409 bool bret = true, started = false;
410 char *tmp_buf = randname;
411 struct lxc_container *clone;
412 lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT;
413 attach_options.env_policy = LXC_ATTACH_CLEAR_ENV;
414
415 if (!arg->newname) {
416 ret = snprintf(randname, PATH_MAX, "%s/%s_XXXXXX", arg->newpath, arg->name);
417 if (ret < 0 || ret >= PATH_MAX)
418 return -1;
419
420 if (!mkdtemp(randname))
421 return -1;
422
423 if (chmod(randname, 0770) < 0) {
424 (void)remove(randname);
425 return -1;
426 }
427
428 arg->newname = randname + strlen(arg->newpath) + 1;
429 }
430
431 clone = c->clone(c, arg->newname, arg->newpath, flags,
432 arg->bdevtype, NULL, arg->fssize, args);
433 if (!clone)
434 return -1;
435
436 if (arg->tmpfs) {
437 premount = mount_tmpfs(arg->name, arg->newname, arg->newpath, arg);
438 if (!premount)
439 goto destroy_and_put;
440
441 bret = clone->set_config_item(clone, "lxc.hook.pre-mount", premount);
442 free(premount);
443 if (!bret)
444 goto destroy_and_put;
445 }
446
447 if (!arg->keepdata)
448 if (!clone->set_config_item(clone, "lxc.ephemeral", "1"))
449 goto destroy_and_put;
450
451 /* allocate and create random upper- and workdirs for overlay mounts */
452 if (mk_rand_ovl_dirs(mnt_table, mnt_table_size, arg) < 0)
453 goto destroy_and_put;
454
455 /* allocate and set mount entries */
456 struct mnts *n = NULL;
457 for (i = 0, n = mnt_table; i < mnt_table_size; i++, n++) {
458 char *mntentry = NULL;
459
460 mntentry = set_mnt_entry(n);
461 if (!mntentry)
462 goto destroy_and_put;
463
464 bret = clone->set_config_item(clone, "lxc.mount.entry", mntentry);
465 free(mntentry);
466 if (!bret)
467 goto destroy_and_put;
468 }
469
470 if (!clone->save_config(clone, NULL))
471 goto destroy_and_put;
472
473 if (!my_args.quiet)
474 printf("Created %s as clone of %s\n", arg->newname, arg->name);
475
476 if (arg->tmpfs && !my_args.quiet)
477 printf("Container is placed on tmpfs.\nRebooting will cause "
478 "all changes made to it to be lost!\n");
479
480 if (!arg->daemonize && arg->argc) {
481 clone->want_daemonize(clone, true);
482 arg->daemonize = 1;
483 } else if (!arg->daemonize) {
484 clone->want_daemonize(clone, false);
485 }
486
487 started = clone->start(clone, 0, NULL);
488 if (!started)
489 goto destroy_and_put;
490
491 if (arg->daemonize && arg->argc) {
492 ret = clone->attach_run_wait(clone, &attach_options, arg->argv[0], (const char *const *)arg->argv);
493 if (ret < 0)
494 goto destroy_and_put;
495 clone->shutdown(clone, -1);
496 }
497
498 free_mnts();
499 lxc_container_put(clone);
500 return 0;
501
502 destroy_and_put:
503 if (started)
504 clone->shutdown(clone, -1);
505
506 ret = clone->get_config_item(clone, "lxc.ephemeral", tmp_buf, PATH_MAX);
507 if (ret > 0 && strcmp(tmp_buf, "0"))
508 clone->destroy(clone);
509
510 free_mnts();
511 lxc_container_put(clone);
512 return -1;
513 }
514
515 static int do_clone_rename(struct lxc_container *c, char *newname)
516 {
517 if (!c->rename(c, newname)) {
518 ERROR("Renaming container %s to %s failed", c->name, newname);
519 return -1;
520 }
521
522 return 0;
523 }
524
525 static int do_clone_task(struct lxc_container *c, enum task task, int flags,
526 char **args)
527 {
528 int ret = 0;
529
530 switch (task) {
531 case DESTROY:
532 ret = do_clone_ephemeral(c, &my_args, args, flags);
533 break;
534 case RENAME:
535 ret = do_clone_rename(c, my_args.newname);
536 break;
537 default:
538 ret = do_clone(c, my_args.newname, my_args.newpath, flags,
539 my_args.bdevtype, my_args.fssize, my_args.task,
540 args);
541 break;
542 }
543
544 return ret;
545 }
546
547 static void free_mnts(void)
548 {
549 unsigned int i;
550 struct mnts *n = NULL;
551
552 for (i = 0, n = mnt_table; i < mnt_table_size; i++, n++) {
553 free(n->src);
554 free(n->dest);
555 free(n->options);
556 free(n->upper);
557 free(n->workdir);
558 }
559
560 free(mnt_table);
561 mnt_table = NULL;
562 mnt_table_size = 0;
563 }
564
565 static int my_parser(struct lxc_arguments *args, int c, char *arg)
566 {
567 char *subopts = NULL;
568 char *mntparameters = NULL;
569 switch (c) {
570 case 'N':
571 args->newname = arg;
572 break;
573 case 'p':
574 args->newpath = arg;
575 break;
576 case 'R':
577 args->task = RENAME;
578 break;
579 case 's':
580 args->task = SNAP;
581 break;
582 case 'a':
583 args->allowrunning = 1;
584 break;
585 case 'F':
586 args->daemonize = 0;
587 break;
588 case 'd':
589 args->daemonize = 1;
590 break;
591 case 'e':
592 args->task = DESTROY;
593 break;
594 case 'm':
595 subopts = optarg;
596 if (parse_mntsubopts(subopts, mount_keys, mntparameters) < 0)
597 return -1;
598 break;
599 case 'B':
600 if (strncmp(arg, "overlay", strlen(arg)) == 0)
601 arg = "overlayfs";
602 args->bdevtype = arg;
603 break;
604 case 't':
605 args->tmpfs = true;
606 break;
607 case 'L':
608 args->fssize = get_fssize(optarg);
609 break;
610 case 'D':
611 args->keepdata = 1;
612 break;
613 case 'K':
614 args->keepname = 1;
615 break;
616 case 'M':
617 args->keepmac = 1;
618 break;
619 }
620
621 return 0;
622 }
623
624 static int parse_bind_mnt(char *mntstring, enum mnttype type)
625 {
626 int len = 0;
627 char **mntarray = NULL;
628 struct mnts *m = NULL;
629
630 m = add_mnt(&mnt_table, &mnt_table_size, type);
631 if (!m)
632 goto err;
633
634 mntarray = lxc_string_split(mntstring, ':');
635 if (!mntarray)
636 goto err;
637
638 m->src = construct_path(mntarray[0], true);
639 if (!m->src)
640 goto err;
641
642 len = lxc_array_len((void **)mntarray);
643 if (len == 1) { /* bind=src */
644 m->dest = construct_path(mntarray[0], false);
645 } else if (len == 2) { /* bind=src:option or bind=src:dest */
646 if (strncmp(mntarray[1], "rw", strlen(mntarray[1])) == 0)
647 m->options = strdup("rw");
648
649 if (strncmp(mntarray[1], "ro", strlen(mntarray[1])) == 0)
650 m->options = strdup("ro");
651
652 if (m->options)
653 m->dest = construct_path(mntarray[0], false);
654 else
655 m->dest = construct_path(mntarray[1], false);
656 } else if (len == 3) { /* bind=src:dest:option */
657 m->dest = construct_path(mntarray[1], false);
658 m->options = strdup(mntarray[2]);
659 } else {
660 printf("Excess elements in mount specification\n");
661 }
662
663 if (!m->dest)
664 goto err;
665
666 if (!m->options)
667 m->options = strdup("rw");
668
669 if (!m->options || (strncmp(m->options, "rw", strlen(m->options)) != 0 &&
670 strncmp(m->options, "ro", strlen(m->options)) != 0))
671 goto err;
672
673 lxc_free_array((void **)mntarray, free);
674 return 0;
675
676 err:
677 free_mnts();
678 lxc_free_array((void **)mntarray, free);
679 return -1;
680 }
681
682 static int parse_mntsubopts(char *subopts, char *const *keys, char *mntparameters)
683 {
684 while (*subopts != '\0') {
685 switch (getsubopt(&subopts, keys, &mntparameters)) {
686 case LXC_MNT_BIND:
687 if (parse_bind_mnt(mntparameters, LXC_MNT_BIND) < 0)
688 return -1;
689 break;
690 case LXC_MNT_OVL:
691 if (parse_ovl_mnt(mntparameters, LXC_MNT_OVL) < 0)
692 return -1;
693 break;
694 default:
695 break;
696 }
697 }
698
699 return 0;
700 }
701
702 static int parse_ovl_mnt(char *mntstring, enum mnttype type)
703 {
704 int len = 0;
705 char **mntarray = NULL;
706 struct mnts *m;
707
708 m = add_mnt(&mnt_table, &mnt_table_size, type);
709 if (!m)
710 goto err;
711
712 mntarray = lxc_string_split(mntstring, ':');
713 if (!mntarray)
714 goto err;
715
716 m->src = construct_path(mntarray[0], true);
717 if (!m->src)
718 goto err;
719
720 len = lxc_array_len((void **)mntarray);
721 if (len == 1) /* overlay=src */
722 m->dest = construct_path(mntarray[0], false);
723 else if (len == 2) /* overlay=src:dest */
724 m->dest = construct_path(mntarray[1], false);
725 else
726 printf("Excess elements in mount specification\n");
727
728 if (!m->dest)
729 goto err;
730
731 lxc_free_array((void **)mntarray, free);
732 return 0;
733
734 err:
735 free_mnts();
736 lxc_free_array((void **)mntarray, free);
737 return -1;
738 }
739
740 /* For ephemeral snapshots backed by the overlay filesystem, this function
741 * mounts a fresh tmpfs over the containers directory if the user requests it.
742 * Because we mount a fresh tmpfs over the directory of the container the
743 * updated /etc/hostname file created during the clone residing in the upperdir
744 * (currently named "delta0" by default) will be hidden. Hence, if the user
745 * requests that the old name is not to be kept for the clone, we recreate this
746 * file on the tmpfs. This should be all that is required to restore the exact
747 * behaviour we would get with a normal clone.
748 */
749 static char *mount_tmpfs(const char *oldname, const char *newname,
750 const char *path, struct lxc_arguments *arg)
751 {
752 int ret, fd;
753 size_t len;
754 mode_t msk;
755 char *premount = NULL;
756 FILE *fp = NULL;
757
758 if (arg->tmpfs && arg->keepdata) {
759 ERROR("%s",
760 "A container can only be placed on a tmpfs when the "
761 "overlay storage driver is used");
762 goto err_free;
763 }
764
765 if (arg->tmpfs && !arg->bdevtype) {
766 arg->bdevtype = "overlayfs";
767 } else if (arg->tmpfs && arg->bdevtype &&
768 strncmp(arg->bdevtype, "overlayfs", strlen(arg->bdevtype)) != 0) {
769 ERROR("%s",
770 "A container can only be placed on a tmpfs when the "
771 "overlay storage driver is used");
772 goto err_free;
773 }
774
775 len = strlen(path) + strlen(newname) + strlen("pre-start-XXXXXX") + /* //\0 */ 3;
776 premount = malloc(len);
777 if (!premount)
778 goto err_free;
779
780 ret = snprintf(premount, len, "%s/%s/pre-start-XXXXXX", path, newname);
781 if (ret < 0 || (size_t)ret >= len)
782 goto err_free;
783
784 msk = umask(0022);
785 fd = mkstemp(premount);
786 umask(msk);
787 if (fd < 0)
788 goto err_free;
789
790 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
791 ERROR("Failed to set close-on-exec on file descriptor");
792 goto err_close;
793 }
794
795 if (chmod(premount, 0755) < 0)
796 goto err_close;
797
798 fp = fdopen(fd, "r+");
799 if (!fp)
800 goto err_close;
801 fd = -1;
802
803 ret = fprintf(fp, "#! /bin/sh\n"
804 "mount -n -t tmpfs -o mode=0755 none %s/%s/overlay\n",
805 path, newname);
806 if (ret < 0)
807 goto err_close;
808
809 if (!arg->keepname) {
810 ret = fprintf(fp,
811 "mkdir -p %s/%s/%s/etc\n"
812 "echo %s > %s/%s/%s/etc/hostname\n",
813 path, newname, LXC_OVERLAY_DELTA_PATH, newname,
814 path, newname, LXC_OVERLAY_DELTA_PATH);
815 if (ret < 0)
816 goto err_close;
817 }
818
819 fclose(fp);
820 return premount;
821
822 err_close:
823 if (fd > 0)
824 close(fd);
825 else if (fp)
826 fclose(fp);
827
828 err_free:
829 free(premount);
830 return NULL;
831 }