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