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