]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/lxccontainer.c
lxc_create: support 'lxc-create -t <template> -h'
[mirror_lxc.git] / src / lxc / lxccontainer.c
CommitLineData
72d0e1cb
SG
1/* liblxcapi
2 *
3 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2012 Canonical Ltd.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
9be53773 20#define _GNU_SOURCE
71454076 21#include <pthread.h>
9be53773
SH
22#include <unistd.h>
23#include <sys/types.h>
24#include <sys/wait.h>
25#include <errno.h>
26#include <sched.h>
27#include "config.h"
72d0e1cb
SG
28#include "lxc.h"
29#include "state.h"
30#include "lxccontainer.h"
31#include "conf.h"
72d0e1cb
SG
32#include "confile.h"
33#include "cgroup.h"
34#include "commands.h"
b6b918a1 35#include "version.h"
72d0e1cb 36#include "log.h"
9be53773 37#include "bdev.h"
6a44839f 38#include "utils.h"
2a59a681 39#include <lxc/utils.h>
e51d4895 40#include <lxc/monitor.h>
9c83a661
SG
41#include <sched.h>
42#include <fcntl.h>
43#include <arpa/inet.h>
44#include <ifaddrs.h>
72d0e1cb
SG
45
46lxc_log_define(lxc_container, lxc);
47
3e625e2d
SH
48static bool file_exists(char *f)
49{
50 struct stat statbuf;
51
52 return stat(f, &statbuf) == 0;
53}
54
55/*
56 * A few functions to help detect when a container creation failed.
57 * If a container creation was killed partway through, then trying
58 * to actually start that container could harm the host. We detect
59 * this by creating a 'partial' file under the container directory,
60 * and keeping an advisory lock. When container creation completes,
61 * we remove that file. When we load or try to start a container, if
62 * we find that file, without a flock, we remove the container.
63 */
64int ongoing_create(struct lxc_container *c)
65{
66 int len = strlen(c->config_path) + strlen(c->name) + 10;
67 char *path = alloca(len);
68 int fd, ret;
69 ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
70 if (ret < 0 || ret >= len) {
71 ERROR("Error writing partial pathname");
72 return -1;
73 }
74
75 if (!file_exists(path))
76 return 0;
77 if (process_lock())
78 return -1;
79 if ((fd = open(path, O_RDWR)) < 0) {
80 // give benefit of the doubt
81 SYSERROR("Error opening partial file");
82 process_unlock();
83 return 0;
84 }
85 if ((ret = flock(fd, LOCK_EX | LOCK_NB)) == -1 &&
86 errno == EWOULDBLOCK) {
87 // create is still ongoing
88 close(fd);
89 process_unlock();
90 return 1;
91 }
92 // create completed but partial is still there.
93 close(fd);
94 process_unlock();
95 return 2;
96}
97
98int create_partial(struct lxc_container *c)
99{
100 // $lxcpath + '/' + $name + '/partial' + \0
101 int len = strlen(c->config_path) + strlen(c->name) + 10;
102 char *path = alloca(len);
103 int fd, ret;
104 ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
105 if (ret < 0 || ret >= len) {
106 ERROR("Error writing partial pathname");
107 return -1;
108 }
1897e3bc 109 if (process_lock())
3e625e2d
SH
110 return -1;
111 if ((fd=open(path, O_CREAT | O_EXCL, 0755)) < 0) {
112 SYSERROR("Erorr creating partial file");
113 process_unlock();
114 return -1;
115 }
116 if (flock(fd, LOCK_EX) < 0) {
117 SYSERROR("Error locking partial file %s", path);
118 close(fd);
119 process_unlock();
120 return -1;
121 }
122 process_unlock();
123
124 return fd;
125}
126
127void remove_partial(struct lxc_container *c, int fd)
128{
129 // $lxcpath + '/' + $name + '/partial' + \0
130 int len = strlen(c->config_path) + strlen(c->name) + 10;
131 char *path = alloca(len);
132 int ret;
133
134 close(fd);
135 ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
136 if (ret < 0 || ret >= len) {
137 ERROR("Error writing partial pathname");
138 return;
139 }
140 if (process_lock())
141 return;
142 if (unlink(path) < 0)
143 SYSERROR("Error unlink partial file %s", path);
144 process_unlock();
145}
146
72d0e1cb 147/* LOCKING
3bc449ed
SH
148 * 1. container_mem_lock(c) protects the struct lxc_container from multiple threads.
149 * 2. container_disk_lock(c) protects the on-disk container data - in particular the
150 * container configuration file.
151 * The container_disk_lock also takes the container_mem_lock.
152 * 3. thread_mutex protects process data (ex: fd table) from multiple threads.
72d0e1cb
SG
153 * NOTHING mutexes two independent programs with their own struct
154 * lxc_container for the same c->name, between API calls. For instance,
155 * c->config_read(); c->start(); Between those calls, data on disk
156 * could change (which shouldn't bother the caller unless for instance
157 * the rootfs get moved). c->config_read(); update; c->config_write();
158 * Two such updaters could race. The callers should therefore check their
159 * results. Trying to prevent that would necessarily expose us to deadlocks
160 * due to hung callers. So I prefer to keep the locks only within our own
161 * functions, not across functions.
162 *
3bc449ed 163 * If you're going to clone while holding a lxccontainer, increment
72d0e1cb
SG
164 * c->numthreads (under privlock) before forking. When deleting,
165 * decrement numthreads under privlock, then if it hits 0 you can delete.
166 * Do not ever use a lxccontainer whose numthreads you did not bump.
167 */
168
169static void lxc_container_free(struct lxc_container *c)
170{
171 if (!c)
172 return;
173
174 if (c->configfile) {
175 free(c->configfile);
176 c->configfile = NULL;
177 }
178 if (c->error_string) {
179 free(c->error_string);
180 c->error_string = NULL;
181 }
d95db067 182 if (c->slock) {
df271a59 183 lxc_putlock(c->slock);
d95db067
DE
184 c->slock = NULL;
185 }
72d0e1cb 186 if (c->privlock) {
df271a59 187 lxc_putlock(c->privlock);
72d0e1cb
SG
188 c->privlock = NULL;
189 }
190 if (c->name) {
191 free(c->name);
192 c->name = NULL;
193 }
d95db067
DE
194 if (c->lxc_conf) {
195 lxc_conf_free(c->lxc_conf);
196 c->lxc_conf = NULL;
197 }
2a59a681
SH
198 if (c->config_path) {
199 free(c->config_path);
200 c->config_path = NULL;
201 }
72d0e1cb
SG
202 free(c);
203}
204
43d1aa34
SH
205/*
206 * Consider the following case:
207freer | racing get()er
208==================================================================
209lxc_container_put() | lxc_container_get()
210\ lxclock(c->privlock) | c->numthreads < 1? (no)
211\ c->numthreads = 0 | \ lxclock(c->privlock) -> waits
212\ lxcunlock() | \
213\ lxc_container_free() | \ lxclock() returns
214 | \ c->numthreads < 1 -> return 0
215\ \ (free stuff) |
216\ \ sem_destroy(privlock) |
217
218 * When the get()er checks numthreads the first time, one of the following
219 * is true:
220 * 1. freer has set numthreads = 0. get() returns 0
221 * 2. freer is between lxclock and setting numthreads to 0. get()er will
222 * sem_wait on privlock, get lxclock after freer() drops it, then see
223 * numthreads is 0 and exit without touching lxclock again..
224 * 3. freer has not yet locked privlock. If get()er runs first, then put()er
225 * will see --numthreads = 1 and not call lxc_container_free().
226*/
227
72d0e1cb
SG
228int lxc_container_get(struct lxc_container *c)
229{
230 if (!c)
231 return 0;
232
43d1aa34
SH
233 // if someone else has already started freeing the container, don't
234 // try to take the lock, which may be invalid
235 if (c->numthreads < 1)
236 return 0;
237
5cee8c50 238 if (container_mem_lock(c))
72d0e1cb
SG
239 return 0;
240 if (c->numthreads < 1) {
241 // bail without trying to unlock, bc the privlock is now probably
242 // in freed memory
243 return 0;
244 }
245 c->numthreads++;
5cee8c50 246 container_mem_unlock(c);
72d0e1cb
SG
247 return 1;
248}
249
250int lxc_container_put(struct lxc_container *c)
251{
252 if (!c)
253 return -1;
5cee8c50 254 if (container_mem_lock(c))
72d0e1cb
SG
255 return -1;
256 if (--c->numthreads < 1) {
5cee8c50 257 container_mem_unlock(c);
72d0e1cb
SG
258 lxc_container_free(c);
259 return 1;
260 }
5cee8c50 261 container_mem_unlock(c);
72d0e1cb
SG
262 return 0;
263}
264
72d0e1cb
SG
265static bool lxcapi_is_defined(struct lxc_container *c)
266{
267 struct stat statbuf;
268 bool ret = false;
269 int statret;
270
271 if (!c)
272 return false;
273
5cee8c50 274 if (container_mem_lock(c))
72d0e1cb
SG
275 return false;
276 if (!c->configfile)
277 goto out;
278 statret = stat(c->configfile, &statbuf);
279 if (statret != 0)
280 goto out;
281 ret = true;
282
283out:
5cee8c50 284 container_mem_unlock(c);
72d0e1cb
SG
285 return ret;
286}
287
288static const char *lxcapi_state(struct lxc_container *c)
289{
72d0e1cb
SG
290 lxc_state_t s;
291
292 if (!c)
293 return NULL;
13f5be62 294 s = lxc_getstate(c->name, c->config_path);
39dc698c 295 return lxc_state2str(s);
72d0e1cb
SG
296}
297
39dc698c 298static bool is_stopped(struct lxc_container *c)
794dd120
SH
299{
300 lxc_state_t s;
13f5be62 301 s = lxc_getstate(c->name, c->config_path);
794dd120
SH
302 return (s == STOPPED);
303}
304
72d0e1cb
SG
305static bool lxcapi_is_running(struct lxc_container *c)
306{
307 const char *s;
308
309 if (!c)
310 return false;
311 s = lxcapi_state(c);
312 if (!s || strcmp(s, "STOPPED") == 0)
313 return false;
314 return true;
315}
316
317static bool lxcapi_freeze(struct lxc_container *c)
318{
319 int ret;
320 if (!c)
321 return false;
322
9123e471 323 ret = lxc_freeze(c->name, c->config_path);
72d0e1cb
SG
324 if (ret)
325 return false;
326 return true;
327}
328
329static bool lxcapi_unfreeze(struct lxc_container *c)
330{
331 int ret;
332 if (!c)
333 return false;
334
9123e471 335 ret = lxc_unfreeze(c->name, c->config_path);
72d0e1cb
SG
336 if (ret)
337 return false;
338 return true;
339}
340
0115f8fd
DE
341static int lxcapi_console(struct lxc_container *c, int *ttynum, int *masterfd)
342{
343 int ttyfd;
344 if (!c)
345 return -1;
346
347 ttyfd = lxc_cmd_console(c->name, ttynum, masterfd, c->config_path);
348 return ttyfd;
349}
350
72d0e1cb
SG
351static pid_t lxcapi_init_pid(struct lxc_container *c)
352{
72d0e1cb
SG
353 if (!c)
354 return -1;
355
5cee8c50 356 return lxc_cmd_get_init_pid(c->name, c->config_path);
72d0e1cb
SG
357}
358
12a50cc6 359static bool load_config_locked(struct lxc_container *c, const char *fname)
8eb5694b
SH
360{
361 if (!c->lxc_conf)
362 c->lxc_conf = lxc_conf_init();
363 if (c->lxc_conf && !lxc_config_read(fname, c->lxc_conf))
364 return true;
365 return false;
366}
367
12a50cc6 368static bool lxcapi_load_config(struct lxc_container *c, const char *alt_file)
72d0e1cb 369{
39dc698c
SH
370 bool ret = false, need_disklock = false;
371 int lret;
12a50cc6 372 const char *fname;
72d0e1cb
SG
373 if (!c)
374 return false;
375
376 fname = c->configfile;
377 if (alt_file)
378 fname = alt_file;
379 if (!fname)
380 return false;
39dc698c
SH
381 /*
382 * If we're reading something other than the container's config,
383 * we only need to lock the in-memory container. If loading the
384 * container's config file, take the disk lock.
385 */
386 if (strcmp(fname, c->configfile) == 0)
387 need_disklock = true;
388
389 if (need_disklock)
390 lret = container_disk_lock(c);
391 else
392 lret = container_mem_lock(c);
393 if (lret)
72d0e1cb 394 return false;
39dc698c 395
8eb5694b 396 ret = load_config_locked(c, fname);
39dc698c
SH
397
398 if (need_disklock)
399 container_disk_unlock(c);
400 else
401 container_mem_unlock(c);
72d0e1cb
SG
402 return ret;
403}
404
405static void lxcapi_want_daemonize(struct lxc_container *c)
406{
407 if (!c)
408 return;
3bc449ed
SH
409 if (!container_mem_lock(c)) {
410 ERROR("Error getting mem lock");
411 return;
412 }
72d0e1cb 413 c->daemonize = 1;
3bc449ed 414 container_mem_unlock(c);
72d0e1cb
SG
415}
416
12a50cc6 417static bool lxcapi_wait(struct lxc_container *c, const char *state, int timeout)
7a44c8b4
SG
418{
419 int ret;
420
421 if (!c)
422 return false;
423
67e571de 424 ret = lxc_wait(c->name, state, timeout, c->config_path);
7a44c8b4
SG
425 return ret == 0;
426}
427
428
429static bool wait_on_daemonized_start(struct lxc_container *c)
430{
431 /* we'll probably want to make this timeout configurable? */
697fa639 432 int timeout = 5, ret, status;
7a44c8b4 433
697fa639
SH
434 /*
435 * our child is going to fork again, then exit. reap the
436 * child
437 */
438 ret = wait(&status);
439 if (ret == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
440 DEBUG("failed waiting for first dual-fork child");
7a44c8b4
SG
441 return lxcapi_wait(c, "RUNNING", timeout);
442}
443
72d0e1cb
SG
444/*
445 * I can't decide if it'd be more convenient for callers if we accept '...',
446 * or a null-terminated array (i.e. execl vs execv)
447 */
12a50cc6 448static bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv[])
72d0e1cb
SG
449{
450 int ret;
451 struct lxc_conf *conf;
452 int daemonize = 0;
453 char *default_args[] = {
454 "/sbin/init",
455 '\0',
456 };
457
458 /* container exists */
459 if (!c)
460 return false;
461 /* container has been setup */
462 if (!c->lxc_conf)
463 return false;
464
3e625e2d
SH
465 if ((ret = ongoing_create(c)) < 0) {
466 ERROR("Error checking for incomplete creation");
467 return false;
468 }
469 if (ret == 2) {
470 ERROR("Error: %s creation was not completed", c->name);
471 c->destroy(c);
472 return false;
473 } else if (ret == 1) {
474 ERROR("Error: creation of %s is ongoing", c->name);
475 return false;
476 }
477
72d0e1cb
SG
478 /* is this app meant to be run through lxcinit, as in lxc-execute? */
479 if (useinit && !argv)
480 return false;
481
5cee8c50 482 if (container_mem_lock(c))
72d0e1cb
SG
483 return false;
484 conf = c->lxc_conf;
485 daemonize = c->daemonize;
5cee8c50 486 container_mem_unlock(c);
72d0e1cb
SG
487
488 if (useinit) {
13f5be62 489 ret = lxc_execute(c->name, argv, 1, conf, c->config_path);
72d0e1cb
SG
490 return ret == 0 ? true : false;
491 }
492
493 if (!argv)
494 argv = default_args;
495
496 /*
497 * say, I'm not sure - what locks do we want here? Any?
498 * Is liblxc's locking enough here to protect the on disk
499 * container? We don't want to exclude things like lxc_info
500 * while container is running...
501 */
502 if (daemonize) {
503 if (!lxc_container_get(c))
504 return false;
e51d4895 505 lxc_monitord_spawn(c->config_path);
71454076 506
5cee8c50 507 if (process_lock())
71454076 508 return false;
72d0e1cb
SG
509 pid_t pid = fork();
510 if (pid < 0) {
511 lxc_container_put(c);
5cee8c50 512 process_unlock();
72d0e1cb
SG
513 return false;
514 }
71454076
DE
515 if (pid != 0) {
516 ret = wait_on_daemonized_start(c);
5cee8c50 517 process_unlock();
71454076
DE
518 return ret;
519 }
5cee8c50 520 process_unlock();
697fa639
SH
521 /* second fork to be reparented by init */
522 pid = fork();
523 if (pid < 0) {
524 SYSERROR("Error doing dual-fork");
525 return false;
526 }
527 if (pid != 0)
528 exit(0);
72d0e1cb 529 /* like daemon(), chdir to / and redirect 0,1,2 to /dev/null */
c278cef2
SH
530 if (chdir("/")) {
531 SYSERROR("Error chdir()ing to /.");
532 return false;
533 }
72d0e1cb
SG
534 close(0);
535 close(1);
536 close(2);
537 open("/dev/null", O_RDONLY);
538 open("/dev/null", O_RDWR);
539 open("/dev/null", O_RDWR);
540 setsid();
541 }
542
72d0e1cb
SG
543reboot:
544 conf->reboot = 0;
13f5be62 545 ret = lxc_start(c->name, argv, conf, c->config_path);
72d0e1cb
SG
546
547 if (conf->reboot) {
548 INFO("container requested reboot");
549 conf->reboot = 0;
72d0e1cb
SG
550 goto reboot;
551 }
552
553 if (daemonize) {
554 lxc_container_put(c);
555 exit (ret == 0 ? true : false);
556 } else {
557 return (ret == 0 ? true : false);
558 }
559}
560
561/*
562 * note there MUST be an ending NULL
563 */
564static bool lxcapi_startl(struct lxc_container *c, int useinit, ...)
565{
566 va_list ap;
567 char **inargs = NULL, **temp;
568 int n_inargs = 0;
569 bool bret = false;
570
571 /* container exists */
572 if (!c)
573 return false;
574
575 /* build array of arguments if any */
576 va_start(ap, useinit);
577 while (1) {
578 char *arg;
579 arg = va_arg(ap, char *);
580 if (!arg)
581 break;
582 n_inargs++;
583 temp = realloc(inargs, n_inargs * sizeof(*inargs));
586d4e9b
SH
584 if (!temp) {
585 va_end(ap);
72d0e1cb 586 goto out;
586d4e9b 587 }
72d0e1cb
SG
588 inargs = temp;
589 inargs[n_inargs - 1] = strdup(arg); // not sure if it's safe not to copy
590 }
591 va_end(ap);
592
593 /* add trailing NULL */
594 if (n_inargs) {
595 n_inargs++;
596 temp = realloc(inargs, n_inargs * sizeof(*inargs));
597 if (!temp)
598 goto out;
599 inargs = temp;
600 inargs[n_inargs - 1] = NULL;
601 }
602
603 bret = lxcapi_start(c, useinit, inargs);
604
605out:
606 if (inargs) {
607 int i;
608 for (i = 0; i < n_inargs; i++) {
609 if (inargs[i])
610 free(inargs[i]);
611 }
612 free(inargs);
613 }
614
615 return bret;
616}
617
618static bool lxcapi_stop(struct lxc_container *c)
619{
620 int ret;
621
622 if (!c)
623 return false;
624
ef6e34ee 625 ret = lxc_cmd_stop(c->name, c->config_path);
72d0e1cb
SG
626
627 return ret == 0;
72d0e1cb
SG
628}
629
630static bool valid_template(char *t)
631{
632 struct stat statbuf;
633 int statret;
634
635 statret = stat(t, &statbuf);
636 if (statret == 0)
637 return true;
638 return false;
639}
640
641/*
642 * create the standard expected container dir
643 */
644static bool create_container_dir(struct lxc_container *c)
645{
646 char *s;
647 int len, ret;
648
2a59a681 649 len = strlen(c->config_path) + strlen(c->name) + 2;
72d0e1cb
SG
650 s = malloc(len);
651 if (!s)
652 return false;
2a59a681 653 ret = snprintf(s, len, "%s/%s", c->config_path, c->name);
72d0e1cb
SG
654 if (ret < 0 || ret >= len) {
655 free(s);
656 return false;
657 }
658 ret = mkdir(s, 0755);
659 if (ret) {
660 if (errno == EEXIST)
661 ret = 0;
662 else
663 SYSERROR("failed to create container path for %s\n", c->name);
664 }
665 free(s);
666 return ret == 0;
667}
668
1897e3bc
SH
669static const char *lxcapi_get_config_path(struct lxc_container *c);
670static bool lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v);
671
72d0e1cb 672/*
1897e3bc
SH
673 * do_bdev_create: thin wrapper around bdev_create(). Like bdev_create(),
674 * it returns a mounted bdev on success, NULL on error.
72d0e1cb 675 */
1897e3bc
SH
676static struct bdev *do_bdev_create(struct lxc_container *c, const char *type,
677 struct bdev_specs *specs)
678{
679 char *dest;
680 const char *lxcpath = lxcapi_get_config_path(c);
681 size_t len;
682 struct bdev *bdev;
683 int ret;
684
685 /* lxcpath/lxcname/rootfs */
686 len = strlen(c->name) + strlen(lxcpath) + 9;
687 dest = alloca(len);
688 ret = snprintf(dest, len, "%s/%s/rootfs", lxcpath, c->name);
689 if (ret < 0 || ret >= len)
690 return NULL;
691
692 bdev = bdev_create(dest, type, c->name, specs);
693 if (!bdev)
694 return NULL;
695 lxcapi_set_config_item(c, "lxc.rootfs", bdev->src);
696 return bdev;
697}
698
699static bool lxcapi_destroy(struct lxc_container *c);
700/*
701 * lxcapi_create:
702 * create a container with the given parameters.
703 * @c: container to be created. It has the lxcpath, name, and a starting
704 * configuration already set
705 * @t: the template to execute to instantiate the root filesystem and
706 * adjust the configuration.
707 * @bdevtype: backing store type to use. If NULL, dir will be used.
708 * @specs: additional parameters for the backing store, i.e. LVM vg to
709 * use.
710 *
711 * @argv: the arguments to pass to the template, terminated by NULL. If no
712 * arguments, you can just pass NULL.
713 */
714static bool lxcapi_create(struct lxc_container *c, const char *t,
715 const char *bdevtype, struct bdev_specs *specs,
716 char *const argv[])
72d0e1cb
SG
717{
718 bool bret = false;
719 pid_t pid;
9be53773 720 char *tpath = NULL, **newargv;
3e625e2d 721 int partial_fd, ret, len, nargs = 0;
72d0e1cb
SG
722
723 if (!c)
724 return false;
725
726 len = strlen(LXCTEMPLATEDIR) + strlen(t) + strlen("/lxc-") + 1;
727 tpath = malloc(len);
728 if (!tpath)
729 return false;
730 ret = snprintf(tpath, len, "%s/lxc-%s", LXCTEMPLATEDIR, t);
731 if (ret < 0 || ret >= len)
732 goto out;
733 if (!valid_template(tpath)) {
734 ERROR("bad template: %s\n", t);
735 goto out;
736 }
737
72d0e1cb
SG
738 if (!c->save_config(c, NULL)) {
739 ERROR("failed to save starting configuration for %s\n", c->name);
740 goto out;
741 }
742
5a3d2e1e 743 /* container is already created if we have a config and rootfs.path is accessible */
e51d4895 744 if (lxcapi_is_defined(c) && c->lxc_conf && c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) == 0)
a741a85d 745 goto out;
5a3d2e1e 746
3e625e2d
SH
747 /* Mark that this container is being created */
748 if ((partial_fd = create_partial(c)) < 0)
749 goto out;
750
1897e3bc 751 /* no need to get disk lock bc we have the partial locked */
72d0e1cb 752
1897e3bc
SH
753 /*
754 * Create the backing store
755 * Note we can't do this in the same task as we use to execute the
756 * template because of the way zfs works.
757 * After you 'zfs create', zfs mounts the fs only in the initial
758 * namespace.
759 */
760 pid = fork();
761 if (pid < 0) {
762 SYSERROR("failed to fork task for container creation template\n");
763 goto out_unlock;
764 }
765
766 if (pid == 0) { // child
767 struct bdev *bdev = NULL;
768
769 if (!(bdev = do_bdev_create(c, bdevtype, specs))) {
770 ERROR("Error creating backing store type %s for %s",
771 bdevtype ? bdevtype : "(none)", c->name);
772 exit(1);
773 }
774
775 /* save config file again to store the new rootfs location */
776 if (!c->save_config(c, NULL)) {
777 ERROR("failed to save starting configuration for %s\n", c->name);
778 // parent task won't see bdev in config so we delete it
779 bdev->ops->umount(bdev);
780 bdev->ops->destroy(bdev);
781 exit(1);
782 }
783 exit(0);
784 }
785 if (wait_for_pid(pid) != 0)
786 goto out;
787
788 /* reload config to get the rootfs */
789 if (c->lxc_conf)
790 lxc_conf_free(c->lxc_conf);
791 c->lxc_conf = NULL;
792 if (!load_config_locked(c, c->configfile))
df271a59 793 goto out;
72d0e1cb 794
1897e3bc
SH
795 /*
796 * now execute the template
797 */
72d0e1cb
SG
798 pid = fork();
799 if (pid < 0) {
800 SYSERROR("failed to fork task for container creation template\n");
801 goto out_unlock;
802 }
803
804 if (pid == 0) { // child
1897e3bc
SH
805 char *patharg, *namearg, *rootfsarg, *src;
806 struct bdev *bdev = NULL;
72d0e1cb
SG
807 int i;
808
1897e3bc
SH
809 if (unshare(CLONE_NEWNS) < 0) {
810 ERROR("error unsharing mounts");
811 exit(1);
812 }
813
814 src = c->lxc_conf->rootfs.path;
815 /*
816 * for an overlayfs create, what the user wants is the template to fill
817 * in what will become the readonly lower layer. So don't mount for
818 * the template
819 */
820 if (strncmp(src, "overlayfs:", 10) == 0) {
821 src = overlayfs_getlower(src+10);
822 }
823 bdev = bdev_init(src, c->lxc_conf->rootfs.mount, NULL);
824 if (!bdev) {
825 ERROR("Error opening rootfs");
826 exit(1);
827 }
828
829 if (bdev->ops->mount(bdev) < 0) {
830 ERROR("Error mounting rootfs");
831 exit(1);
832 }
833
72d0e1cb
SG
834 /*
835 * create our new array, pre-pend the template name and
836 * base args
837 */
838 if (argv)
1897e3bc
SH
839 for (nargs = 0; argv[nargs]; nargs++) ;
840 nargs += 4; // template, path, rootfs and name args
72d0e1cb
SG
841 newargv = malloc(nargs * sizeof(*newargv));
842 if (!newargv)
843 exit(1);
2acf7795 844 newargv[0] = (char *)t;
72d0e1cb 845
2a59a681 846 len = strlen(c->config_path) + strlen(c->name) + strlen("--path=") + 2;
72d0e1cb
SG
847 patharg = malloc(len);
848 if (!patharg)
849 exit(1);
2a59a681 850 ret = snprintf(patharg, len, "--path=%s/%s", c->config_path, c->name);
72d0e1cb
SG
851 if (ret < 0 || ret >= len)
852 exit(1);
853 newargv[1] = patharg;
854 len = strlen("--name=") + strlen(c->name) + 1;
855 namearg = malloc(len);
856 if (!namearg)
857 exit(1);
858 ret = snprintf(namearg, len, "--name=%s", c->name);
859 if (ret < 0 || ret >= len)
860 exit(1);
861 newargv[2] = namearg;
862
1897e3bc
SH
863 len = strlen("--rootfs=") + 1 + strlen(bdev->dest);
864 rootfsarg = malloc(len);
865 if (!rootfsarg)
866 exit(1);
867 ret = snprintf(rootfsarg, len, "--rootfs=%s", bdev->dest);
868 if (ret < 0 || ret >= len)
869 exit(1);
870 newargv[3] = rootfsarg;
871
72d0e1cb
SG
872 /* add passed-in args */
873 if (argv)
1897e3bc
SH
874 for (i = 4; i < nargs; i++)
875 newargv[i] = argv[i-4];
72d0e1cb
SG
876
877 /* add trailing NULL */
878 nargs++;
879 newargv = realloc(newargv, nargs * sizeof(*newargv));
880 if (!newargv)
881 exit(1);
882 newargv[nargs - 1] = NULL;
883
884 /* execute */
e6a19d26 885 execv(tpath, newargv);
72d0e1cb
SG
886 SYSERROR("failed to execute template %s", tpath);
887 exit(1);
888 }
889
9be53773
SH
890 if (wait_for_pid(pid) != 0) {
891 ERROR("container creation template for %s failed\n", c->name);
8eb5694b
SH
892 goto out_unlock;
893 }
894
895 // now clear out the lxc_conf we have, reload from the created
896 // container
897 if (c->lxc_conf)
898 lxc_conf_free(c->lxc_conf);
899 c->lxc_conf = NULL;
900 bret = load_config_locked(c, c->configfile);
72d0e1cb
SG
901
902out_unlock:
3e625e2d
SH
903 if (partial_fd >= 0)
904 remove_partial(c, partial_fd);
72d0e1cb
SG
905out:
906 if (tpath)
907 free(tpath);
1897e3bc
SH
908 if (!bret && c)
909 lxcapi_destroy(c);
72d0e1cb
SG
910 return bret;
911}
912
3e625e2d
SH
913static bool lxcapi_reboot(struct lxc_container *c)
914{
915 pid_t pid;
916
917 if (!c)
918 return false;
919 if (!c->is_running(c))
920 return false;
921 pid = c->init_pid(c);
922 if (pid <= 0)
923 return false;
924 if (kill(pid, SIGINT) < 0)
925 return false;
926 return true;
927
928}
929
72d0e1cb
SG
930static bool lxcapi_shutdown(struct lxc_container *c, int timeout)
931{
932 bool retv;
933 pid_t pid;
934
935 if (!c)
936 return false;
937
938 if (!timeout)
939 timeout = -1;
940 if (!c->is_running(c))
941 return true;
942 pid = c->init_pid(c);
943 if (pid <= 0)
944 return true;
945 kill(pid, SIGPWR);
946 retv = c->wait(c, "STOPPED", timeout);
f6144ed4 947 if (!retv && timeout > 0) {
72d0e1cb
SG
948 c->stop(c);
949 retv = c->wait(c, "STOPPED", 0); // 0 means don't wait
950 }
951 return retv;
952}
953
1897e3bc
SH
954static bool lxcapi_createl(struct lxc_container *c, const char *t,
955 const char *bdevtype, struct bdev_specs *specs, ...)
72d0e1cb
SG
956{
957 bool bret = false;
958 char **args = NULL, **temp;
959 va_list ap;
960 int nargs = 0;
961
962 if (!c)
963 return false;
964
965 /*
966 * since we're going to wait for create to finish, I don't think we
967 * need to get a copy of the arguments.
968 */
1897e3bc 969 va_start(ap, specs);
72d0e1cb
SG
970 while (1) {
971 char *arg;
972 arg = va_arg(ap, char *);
973 if (!arg)
974 break;
975 nargs++;
83cab6e0 976 temp = realloc(args, (nargs+1) * sizeof(*args));
41670788
SH
977 if (!temp) {
978 va_end(ap);
72d0e1cb 979 goto out;
41670788 980 }
72d0e1cb
SG
981 args = temp;
982 args[nargs - 1] = arg;
983 }
984 va_end(ap);
63e414f8
SH
985 if (args)
986 args[nargs] = NULL;
72d0e1cb 987
1897e3bc 988 bret = c->create(c, t, bdevtype, specs, args);
72d0e1cb
SG
989
990out:
991 if (args)
992 free(args);
993 return bret;
994}
995
12a50cc6 996static bool lxcapi_clear_config_item(struct lxc_container *c, const char *key)
72d0e1cb
SG
997{
998 int ret;
999
1000 if (!c || !c->lxc_conf)
1001 return false;
5cee8c50 1002 if (container_mem_lock(c))
72d0e1cb 1003 return false;
72d0e1cb 1004 ret = lxc_clear_config_item(c->lxc_conf, key);
5cee8c50 1005 container_mem_unlock(c);
72d0e1cb
SG
1006 return ret == 0;
1007}
1008
9c83a661
SG
1009char** lxcapi_get_ips(struct lxc_container *c, char* interface, char* family, int scope)
1010{
1011 int count = 0;
1012 struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL;
1013 char addressOutputBuffer[INET6_ADDRSTRLEN];
1014 void *tempAddrPtr = NULL;
1015 char **addresses = NULL, **temp;
1016 char *address = NULL;
1017 char new_netns_path[MAXPATHLEN];
1018 int old_netns = -1, new_netns = -1, ret = 0;
1019
1020 if (!c->is_running(c))
1021 goto out;
1022
1023 /* Save reference to old netns */
1024 old_netns = open("/proc/self/ns/net", O_RDONLY);
1025 if (old_netns < 0) {
1026 SYSERROR("failed to open /proc/self/ns/net");
1027 goto out;
1028 }
1029
1030 /* Switch to new netns */
1031 ret = snprintf(new_netns_path, MAXPATHLEN, "/proc/%d/ns/net", c->init_pid(c));
1032 if (ret < 0 || ret >= MAXPATHLEN)
1033 goto out;
1034
1035 new_netns = open(new_netns_path, O_RDONLY);
1036 if (new_netns < 0) {
1037 SYSERROR("failed to open %s", new_netns_path);
1038 goto out;
1039 }
1040
1041 if (setns(new_netns, CLONE_NEWNET)) {
1042 SYSERROR("failed to setns");
1043 goto out;
1044 }
1045
1046 /* Grab the list of interfaces */
1047 if (getifaddrs(&interfaceArray)) {
1048 SYSERROR("failed to get interfaces list");
1049 goto out;
1050 }
1051
1052 /* Iterate through the interfaces */
1053 for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) {
1054 if(tempIfAddr->ifa_addr->sa_family == AF_INET) {
1055 if (family && strcmp(family, "inet"))
1056 continue;
1057 tempAddrPtr = &((struct sockaddr_in *)tempIfAddr->ifa_addr)->sin_addr;
1058 }
1059 else {
1060 if (family && strcmp(family, "inet6"))
1061 continue;
1062
1063 if (((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_scope_id != scope)
1064 continue;
1065
1066 tempAddrPtr = &((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_addr;
1067 }
1068
1069 if (interface && strcmp(interface, tempIfAddr->ifa_name))
1070 continue;
1071 else if (!interface && strcmp("lo", tempIfAddr->ifa_name) == 0)
1072 continue;
1073
1074 address = (char *)inet_ntop(tempIfAddr->ifa_addr->sa_family,
1075 tempAddrPtr,
1076 addressOutputBuffer,
1077 sizeof(addressOutputBuffer));
1078 if (!address)
1079 continue;
1080
1081 count += 1;
1082 temp = realloc(addresses, count * sizeof(*addresses));
1083 if (!temp) {
1084 count--;
1085 goto out;
1086 }
1087 addresses = temp;
1088 addresses[count - 1] = strdup(address);
1089 }
1090
1091out:
1092 if(interfaceArray)
1093 freeifaddrs(interfaceArray);
1094
1095 /* Switch back to original netns */
1096 if (old_netns >= 0 && setns(old_netns, CLONE_NEWNET))
1097 SYSERROR("failed to setns");
1098 if (new_netns >= 0)
1099 close(new_netns);
1100 if (old_netns >= 0)
1101 close(old_netns);
1102
1103 /* Append NULL to the array */
1104 if (count) {
1105 count++;
1106 temp = realloc(addresses, count * sizeof(*addresses));
1107 if (!temp) {
fca3080f
DE
1108 int i;
1109 for (i = 0; i < count-1; i++)
9c83a661
SG
1110 free(addresses[i]);
1111 free(addresses);
1112 return NULL;
1113 }
1114 addresses = temp;
1115 addresses[count - 1] = NULL;
1116 }
1117
1118 return addresses;
1119}
1120
12a50cc6 1121static int lxcapi_get_config_item(struct lxc_container *c, const char *key, char *retv, int inlen)
72d0e1cb
SG
1122{
1123 int ret;
1124
1125 if (!c || !c->lxc_conf)
1126 return -1;
5cee8c50 1127 if (container_mem_lock(c))
72d0e1cb 1128 return -1;
72d0e1cb 1129 ret = lxc_get_config_item(c->lxc_conf, key, retv, inlen);
5cee8c50 1130 container_mem_unlock(c);
72d0e1cb
SG
1131 return ret;
1132}
1133
12a50cc6 1134static int lxcapi_get_keys(struct lxc_container *c, const char *key, char *retv, int inlen)
72d0e1cb
SG
1135{
1136 if (!key)
1137 return lxc_listconfigs(retv, inlen);
1138 /*
1139 * Support 'lxc.network.<idx>', i.e. 'lxc.network.0'
1140 * This is an intelligent result to show which keys are valid given
1141 * the type of nic it is
1142 */
1143 if (!c || !c->lxc_conf)
1144 return -1;
5cee8c50 1145 if (container_mem_lock(c))
72d0e1cb
SG
1146 return -1;
1147 int ret = -1;
1148 if (strncmp(key, "lxc.network.", 12) == 0)
1149 ret = lxc_list_nicconfigs(c->lxc_conf, key, retv, inlen);
5cee8c50 1150 container_mem_unlock(c);
72d0e1cb
SG
1151 return ret;
1152}
1153
12a50cc6 1154static bool lxcapi_save_config(struct lxc_container *c, const char *alt_file)
72d0e1cb 1155{
39dc698c
SH
1156 FILE *fout;
1157 bool ret = false, need_disklock = false;
1158 int lret;
1159
72d0e1cb
SG
1160 if (!alt_file)
1161 alt_file = c->configfile;
1162 if (!alt_file)
1163 return false; // should we write to stdout if no file is specified?
39dc698c
SH
1164
1165 // If we haven't yet loaded a config, load the stock config
1166 if (!c->lxc_conf) {
72d0e1cb
SG
1167 if (!c->load_config(c, LXC_DEFAULT_CONFIG)) {
1168 ERROR("Error loading default configuration file %s while saving %s\n", LXC_DEFAULT_CONFIG, c->name);
1169 return false;
1170 }
39dc698c 1171 }
72d0e1cb 1172
5a3d2e1e
SG
1173 if (!create_container_dir(c))
1174 return false;
1175
39dc698c
SH
1176 /*
1177 * If we're writing to the container's config file, take the
1178 * disk lock. Otherwise just take the memlock to protect the
1179 * struct lxc_container while we're traversing it.
1180 */
1181 if (strcmp(c->configfile, alt_file) == 0)
1182 need_disklock = true;
1183
1184 if (need_disklock)
1185 lret = container_disk_lock(c);
1186 else
1187 lret = container_mem_lock(c);
1188
1189 if (lret)
72d0e1cb 1190 return false;
39dc698c
SH
1191
1192 fout = fopen(alt_file, "w");
1193 if (!fout)
1194 goto out;
72d0e1cb
SG
1195 write_config(fout, c->lxc_conf);
1196 fclose(fout);
39dc698c
SH
1197 ret = true;
1198
1199out:
1200 if (need_disklock)
1201 container_disk_unlock(c);
1202 else
1203 container_mem_unlock(c);
1204 return ret;
72d0e1cb
SG
1205}
1206
60bf62d4 1207// do we want the api to support --force, or leave that to the caller?
72d0e1cb
SG
1208static bool lxcapi_destroy(struct lxc_container *c)
1209{
1897e3bc 1210 struct bdev *r = NULL;
60bf62d4 1211 bool ret = false;
72d0e1cb 1212
1897e3bc 1213 if (!c || !lxcapi_is_defined(c))
5a3d2e1e
SG
1214 return false;
1215
3bc449ed 1216 if (container_disk_lock(c))
72d0e1cb 1217 return false;
72d0e1cb 1218
39dc698c 1219 if (!is_stopped(c)) {
60bf62d4
SH
1220 // we should queue some sort of error - in c->error_string?
1221 ERROR("container %s is not stopped", c->name);
1222 goto out;
72d0e1cb
SG
1223 }
1224
3bc449ed 1225 if (c->lxc_conf && c->lxc_conf->rootfs.path && c->lxc_conf->rootfs.mount)
1897e3bc 1226 r = bdev_init(c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL);
60bf62d4
SH
1227 if (r) {
1228 if (r->ops->destroy(r) < 0) {
1229 ERROR("Error destroying rootfs for %s", c->name);
1230 goto out;
1231 }
1232 }
1233
1234 const char *p1 = lxcapi_get_config_path(c);
1235 char *path = alloca(strlen(p1) + strlen(c->name) + 2);
1236 sprintf(path, "%s/%s", p1, c->name);
1237 if (lxc_rmdir_onedev(path) < 0) {
1238 ERROR("Error destroying container directory for %s", c->name);
1239 goto out;
1240 }
1241 ret = true;
1242
1243out:
3bc449ed 1244 container_disk_unlock(c);
60bf62d4 1245 return ret;
72d0e1cb
SG
1246}
1247
12a50cc6 1248static bool lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v)
72d0e1cb
SG
1249{
1250 int ret;
1251 bool b = false;
1252 struct lxc_config_t *config;
1253
1254 if (!c)
1255 return false;
1256
5cee8c50 1257 if (container_mem_lock(c))
72d0e1cb
SG
1258 return false;
1259
1260 if (!c->lxc_conf)
1261 c->lxc_conf = lxc_conf_init();
1262 if (!c->lxc_conf)
1263 goto err;
1264 config = lxc_getconfig(key);
1265 if (!config)
1266 goto err;
1267 ret = config->cb(key, v, c->lxc_conf);
1268 if (!ret)
1269 b = true;
1270
1271err:
5cee8c50 1272 container_mem_unlock(c);
72d0e1cb
SG
1273 return b;
1274}
1275
1276static char *lxcapi_config_file_name(struct lxc_container *c)
1277{
1278 if (!c || !c->configfile)
1279 return NULL;
1280 return strdup(c->configfile);
1281}
1282
2a59a681
SH
1283static const char *lxcapi_get_config_path(struct lxc_container *c)
1284{
1285 if (!c || !c->config_path)
1286 return NULL;
1287 return (const char *)(c->config_path);
1288}
1289
afeecbba
SH
1290/*
1291 * not for export
1292 * Just recalculate the c->configfile based on the
1293 * c->config_path, which must be set.
1294 * The lxc_container must be locked or not yet public.
1295 */
1296static bool set_config_filename(struct lxc_container *c)
1297{
1298 char *newpath;
1299 int len, ret;
1300
1301 if (!c->config_path)
1302 return false;
1303
1304 /* $lxc_path + "/" + c->name + "/" + "config" + '\0' */
1305 len = strlen(c->config_path) + strlen(c->name) + strlen("config") + 3;
1306 newpath = malloc(len);
1307 if (!newpath)
1308 return false;
1309
1310 ret = snprintf(newpath, len, "%s/%s/config", c->config_path, c->name);
1311 if (ret < 0 || ret >= len) {
1312 fprintf(stderr, "Error printing out config file name\n");
1313 free(newpath);
1314 return false;
1315 }
1316
1317 if (c->configfile)
1318 free(c->configfile);
1319 c->configfile = newpath;
1320
1321 return true;
1322}
1323
2a59a681
SH
1324static bool lxcapi_set_config_path(struct lxc_container *c, const char *path)
1325{
1326 char *p;
1327 bool b = false;
afeecbba 1328 char *oldpath = NULL;
2a59a681
SH
1329
1330 if (!c)
1331 return b;
1332
5cee8c50 1333 if (container_mem_lock(c))
2a59a681
SH
1334 return b;
1335
1336 p = strdup(path);
afeecbba
SH
1337 if (!p) {
1338 ERROR("Out of memory setting new lxc path");
2a59a681 1339 goto err;
afeecbba
SH
1340 }
1341
2a59a681
SH
1342 b = true;
1343 if (c->config_path)
afeecbba 1344 oldpath = c->config_path;
2a59a681 1345 c->config_path = p;
afeecbba
SH
1346
1347 /* Since we've changed the config path, we have to change the
1348 * config file name too */
1349 if (!set_config_filename(c)) {
1350 ERROR("Out of memory setting new config filename");
1351 b = false;
1352 free(c->config_path);
1353 c->config_path = oldpath;
1354 oldpath = NULL;
1355 }
2a59a681 1356err:
afeecbba
SH
1357 if (oldpath)
1358 free(oldpath);
5cee8c50 1359 container_mem_unlock(c);
2a59a681
SH
1360 return b;
1361}
1362
1363
794dd120
SH
1364static bool lxcapi_set_cgroup_item(struct lxc_container *c, const char *subsys, const char *value)
1365{
1366 int ret;
794dd120
SH
1367
1368 if (!c)
1369 return false;
1370
3bc449ed 1371 if (is_stopped(c))
794dd120
SH
1372 return false;
1373
3bc449ed
SH
1374 if (container_disk_lock(c))
1375 return false;
794dd120 1376
3bc449ed
SH
1377 ret = lxc_cgroup_set(c->name, subsys, value, c->config_path) == 0;
1378
1379 container_disk_unlock(c);
1380 return ret == 0;
794dd120
SH
1381}
1382
1383static int lxcapi_get_cgroup_item(struct lxc_container *c, const char *subsys, char *retv, int inlen)
1384{
3bc449ed 1385 int ret;
794dd120
SH
1386
1387 if (!c || !c->lxc_conf)
1388 return -1;
1389
3bc449ed 1390 if (is_stopped(c))
794dd120
SH
1391 return -1;
1392
3bc449ed
SH
1393 if (container_disk_lock(c))
1394 return -1;
794dd120 1395
ae5c8b8e 1396 ret = lxc_cgroup_get(c->name, subsys, retv, inlen, c->config_path);
794dd120 1397
3bc449ed 1398 container_disk_unlock(c);
794dd120
SH
1399 return ret;
1400}
1401
67e571de 1402const char *lxc_get_default_config_path(void)
83c98d82
DE
1403{
1404 return default_lxc_path();
1405}
794dd120 1406
a8428dfa
SH
1407const char *lxc_get_default_lvm_vg(void)
1408{
1409 return default_lvm_vg();
1410}
1411
1412const char *lxc_get_default_zfs_root(void)
1413{
1414 return default_zfs_root();
1415}
1416
b6b918a1
SG
1417const char *lxc_get_version(void)
1418{
1419 return lxc_version();
1420}
1421
9be53773
SH
1422static int copy_file(char *old, char *new)
1423{
1424 int in, out;
1425 ssize_t len, ret;
1426 char buf[8096];
1427 struct stat sbuf;
1428
1429 if (file_exists(new)) {
1430 ERROR("copy destination %s exists", new);
1431 return -1;
1432 }
1433 ret = stat(old, &sbuf);
1434 if (ret < 0) {
1435 SYSERROR("stat'ing %s", old);
1436 return -1;
1437 }
1438
1439 in = open(old, O_RDONLY);
1440 if (in < 0) {
1441 SYSERROR("opening original file %s", old);
1442 return -1;
1443 }
1444 out = open(new, O_CREAT | O_EXCL | O_WRONLY, 0644);
1445 if (out < 0) {
1446 SYSERROR("opening new file %s", new);
1447 close(in);
1448 return -1;
1449 }
1450
1451 while (1) {
1452 len = read(in, buf, 8096);
1453 if (len < 0) {
1454 SYSERROR("reading old file %s", old);
1455 goto err;
1456 }
1457 if (len == 0)
1458 break;
1459 ret = write(out, buf, len);
1460 if (ret < len) { // should we retry?
1461 SYSERROR("write to new file %s was interrupted", new);
1462 goto err;
1463 }
1464 }
1465 close(in);
1466 close(out);
1467
1468 // we set mode, but not owner/group
1469 ret = chmod(new, sbuf.st_mode);
1470 if (ret) {
1471 SYSERROR("setting mode on %s", new);
1472 return -1;
1473 }
1474
1475 return 0;
1476
1477err:
1478 close(in);
1479 close(out);
1480 return -1;
1481}
1482
1483/*
1484 * we're being passed result of two strstrs(x, y). We want to write
1485 * all data up to the first found string, or to end of the string if
1486 * neither string was found.
1487 * This function will return the earliest found string if any, or else
1488 * NULL
1489 */
1490static const char *lowest_nonnull(const char *p1, const char *p2)
1491{
1492 if (!p1)
1493 return p2;
1494 if (!p2)
1495 return p1;
1496 return p1 < p2 ? p1 : p2;
1497}
1498
1499static int is_word_sep(char c)
1500{
1501 switch(c) {
1502 case '\0':
1503 case '\n':
1504 case '\r':
1505 case '\t':
1506 case ' ':
1507 case '=':
1508 case '/':
1509 return 1;
1510 default: return 0;
1511 }
1512}
1513
1514static const char *find_first_wholeword(const char *p, const char *word)
1515{
1516 if (!p)
1517 return NULL;
1518
1519 while ((p = strstr(p, word)) != NULL) {
1520 if (is_word_sep(*(p-1)) && is_word_sep(p[strlen(word)]))
1521 return p;
1522 p++;
1523 }
1524 return NULL;
1525}
1526
1527static int update_name_and_paths(const char *path, struct lxc_container *oldc,
1528 const char *newname, const char *newpath)
1529{
1530 FILE *f;
a7478944 1531 long flen;
9be53773
SH
1532 char *contents;
1533 const char *p0, *p1, *p2, *end;
1534 const char *oldpath = oldc->get_config_path(oldc);
1535 const char *oldname = oldc->name;
1536
1537 f = fopen(path, "r");
1538 if (!f) {
1539 SYSERROR("opening old config");
1540 return -1;
1541 }
1542 if (fseek(f, 0, SEEK_END) < 0) {
1543 SYSERROR("seeking to end of old config");
1544 fclose(f);
1545 return -1;
1546 }
1547 flen = ftell(f);
1548 if (flen < 0) {
1549 fclose(f);
1550 SYSERROR("telling size of old config");
1551 return -1;
1552 }
1553 if (fseek(f, 0, SEEK_SET) < 0) {
1554 fclose(f);
1555 SYSERROR("rewinding old config");
1556 return -1;
1557 }
a2eea3c1 1558 contents = malloc(flen+1);
9be53773
SH
1559 if (!contents) {
1560 SYSERROR("out of memory");
1561 fclose(f);
a2eea3c1 1562 return -1;
9be53773
SH
1563 }
1564 if (fread(contents, 1, flen, f) != flen) {
1565 free(contents);
1566 fclose(f);
1567 SYSERROR("reading old config");
1568 return -1;
1569 }
a2eea3c1 1570 contents[flen] = '\0';
9be53773
SH
1571 if (fclose(f) < 0) {
1572 free(contents);
1573 SYSERROR("closing old config");
1574 return -1;
1575 }
1576
1577 f = fopen(path, "w");
1578 if (!f) {
1579 SYSERROR("reopening config");
1580 free(contents);
1581 return -1;
1582 }
1583
1584 p0 = contents;
1585 end = contents + flen;
1586 while (1) {
1587 p1 = find_first_wholeword(p0, oldpath);
1588 p2 = find_first_wholeword(p0, oldname);
1589 if (!p1 && !p2) {
1590 // write the rest and be done
1591 if (fwrite(p0, 1, (end-p0), f) != (end-p0)) {
1592 SYSERROR("writing new config");
1593 free(contents);
1594 fclose(f);
1595 return -1;
1596 }
1597 free(contents);
1598 fclose(f);
1599 // success
1600 return 0;
1601 } else {
1602 const char *p = lowest_nonnull(p1, p2);
1603 const char *new = (p == p2) ? newname : newpath;
1604 if (fwrite(p0, 1, (p-p0), f) != (p-p0)) {
1605 SYSERROR("writing new config");
1606 free(contents);
1607 fclose(f);
1608 return -1;
1609 }
1610 p0 = p;
1611 // now write the newpath or newname
1612 if (fwrite(new, 1, strlen(new), f) != strlen(new)) {
1613 SYSERROR("writing new name or path in new config");
1614 free(contents);
1615 fclose(f);
1616 return -1;
1617 }
1618 p0 += (p == p2) ? strlen(oldname) : strlen(oldpath);
1619 }
1620 }
1621}
1622
1623static int copyhooks(struct lxc_container *oldc, struct lxc_container *c)
1624{
1625 int i;
1626 int ret;
1627 struct lxc_list *it;
1628
1629 for (i=0; i<NUM_LXC_HOOKS; i++) {
1630 lxc_list_for_each(it, &c->lxc_conf->hooks[i]) {
1631 char *hookname = it->elem;
1632 char *fname = rindex(hookname, '/');
1633 char tmppath[MAXPATHLEN];
1634 if (!fname) // relative path - we don't support, but maybe we should
1635 return 0;
1636 // copy the script, and change the entry in confile
1637 ret = snprintf(tmppath, MAXPATHLEN, "%s/%s/%s",
1638 c->config_path, c->name, fname+1);
1639 if (ret < 0 || ret >= MAXPATHLEN)
1640 return -1;
1641 ret = copy_file(it->elem, tmppath);
1642 if (ret < 0)
1643 return -1;
1644 free(it->elem);
1645 it->elem = strdup(tmppath);
1646 if (!it->elem) {
1647 ERROR("out of memory copying hook path");
1648 return -1;
1649 }
1650 update_name_and_paths(it->elem, oldc, c->name, c->get_config_path(c));
1651 }
1652 }
1653
1654 c->save_config(c, NULL);
1655 return 0;
1656}
1657
1658static void new_hwaddr(char *hwaddr)
1659{
1660 FILE *f = fopen("/dev/urandom", "r");
1661 if (f) {
1662 unsigned int seed;
1663 int ret = fread(&seed, sizeof(seed), 1, f);
1664 if (ret != 1)
1665 seed = time(NULL);
1666 fclose(f);
1667 srand(seed);
1668 } else
1669 srand(time(NULL));
1670 snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x",
1671 rand() % 255, rand() % 255, rand() % 255);
1672}
1673
1674static void network_new_hwaddrs(struct lxc_container *c)
1675{
1676 struct lxc_list *it;
1677
1678 lxc_list_for_each(it, &c->lxc_conf->network) {
1679 struct lxc_netdev *n = it->elem;
1680 if (n->hwaddr)
1681 new_hwaddr(n->hwaddr);
1682 }
1683}
1684
1685static int copy_fstab(struct lxc_container *oldc, struct lxc_container *c)
1686{
1687 char newpath[MAXPATHLEN];
1688 char *oldpath = oldc->lxc_conf->fstab;
1689 int ret;
1690
1691 if (!oldpath)
1692 return 0;
1693
1694 char *p = rindex(oldpath, '/');
1695 if (!p)
1696 return -1;
1697 ret = snprintf(newpath, MAXPATHLEN, "%s/%s%s",
1698 c->config_path, c->name, p);
1699 if (ret < 0 || ret >= MAXPATHLEN) {
1700 ERROR("error printing new path for %s", oldpath);
1701 return -1;
1702 }
1703 if (file_exists(newpath)) {
1704 ERROR("error: fstab file %s exists", newpath);
1705 return -1;
1706 }
1707
1708 if (copy_file(oldpath, newpath) < 0) {
1709 ERROR("error: copying %s to %s", oldpath, newpath);
1710 return -1;
1711 }
1712 free(c->lxc_conf->fstab);
1713 c->lxc_conf->fstab = strdup(newpath);
1714 if (!c->lxc_conf->fstab) {
1715 ERROR("error: allocating pathname");
1716 return -1;
1717 }
1718
1719 return 0;
1720}
1721
1722static int copy_storage(struct lxc_container *c0, struct lxc_container *c,
1723 const char *newtype, int flags, const char *bdevdata, unsigned long newsize)
1724{
1725 struct bdev *bdev;
1726
1727 bdev = bdev_copy(c0->lxc_conf->rootfs.path, c0->name, c->name,
1728 c0->config_path, c->config_path, newtype, !!(flags & LXC_CLONE_SNAPSHOT),
1729 bdevdata, newsize);
1730 if (!bdev) {
1731 ERROR("error copying storage");
1732 return -1;
1733 }
1734 free(c->lxc_conf->rootfs.path);
1735 c->lxc_conf->rootfs.path = strdup(bdev->src);
1736 bdev_put(bdev);
1737 if (!c->lxc_conf->rootfs.path)
1738 return -1;
1739 // here we could also update all lxc.mount.entries or even
1740 // items in the lxc.mount fstab list. As discussed on m-l,
1741 // we could do either any source paths starting with the
1742 // lxcpath/oldname, or simply anythign which is not a virtual
1743 // fs or a bind mount.
1744 return 0;
1745}
1746
148e91f5 1747static int clone_update_rootfs(struct lxc_container *c, int flags, char **hookargs)
9be53773
SH
1748{
1749 int ret = -1;
1750 char path[MAXPATHLEN];
1751 struct bdev *bdev;
1752 FILE *fout;
1753 pid_t pid;
148e91f5 1754 struct lxc_conf *conf = c->lxc_conf;
9be53773
SH
1755
1756 /* update hostname in rootfs */
1757 /* we're going to mount, so run in a clean namespace to simplify cleanup */
1758
1759 pid = fork();
1760 if (pid < 0)
1761 return -1;
1762 if (pid > 0)
1763 return wait_for_pid(pid);
1764
1765 if (unshare(CLONE_NEWNS) < 0) {
1766 ERROR("error unsharing mounts");
1767 exit(1);
1768 }
1769 bdev = bdev_init(c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL);
1770 if (!bdev)
1771 exit(1);
1772 if (bdev->ops->mount(bdev) < 0)
1773 exit(1);
148e91f5
SH
1774
1775 if (!lxc_list_empty(&conf->hooks[LXCHOOK_CLONE])) {
1776 /* Start of environment variable setup for hooks */
1777 if (setenv("LXC_NAME", c->name, 1)) {
1778 SYSERROR("failed to set environment variable for container name");
1779 }
1780 if (setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) {
1781 SYSERROR("failed to set environment variable for config path");
1782 }
1783 if (setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) {
1784 SYSERROR("failed to set environment variable for rootfs mount");
1785 }
1786 if (setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) {
1787 SYSERROR("failed to set environment variable for rootfs mount");
1788 }
1789
1790 if (run_lxc_hooks(c->name, "clone", conf, hookargs)) {
1791 ERROR("Error executing clone hook for %s", c->name);
1792 exit(1);
1793 }
1794 }
1795
1796 if (!(flags & LXC_CLONE_KEEPNAME)) {
1797 ret = snprintf(path, MAXPATHLEN, "%s/etc/hostname", bdev->dest);
1798 if (ret < 0 || ret >= MAXPATHLEN)
1799 exit(1);
1800 if (!(fout = fopen(path, "w"))) {
1801 SYSERROR("unable to open %s: ignoring\n", path);
1802 exit(0);
1803 }
1804 if (fprintf(fout, "%s", c->name) < 0)
1805 exit(1);
1806 if (fclose(fout) < 0)
1807 exit(1);
9be53773 1808 }
9be53773
SH
1809 exit(0);
1810}
1811
1812/*
1813 * We want to support:
1814sudo lxc-clone -o o1 -n n1 -s -L|-fssize fssize -v|--vgname vgname \
1815 -p|--lvprefix lvprefix -t|--fstype fstype -B backingstore
1816
1817-s [ implies overlayfs]
1818-s -B overlayfs
1819-s -B aufs
1820
1821only rootfs gets converted (copied/snapshotted) on clone.
1822*/
1823
1824static int create_file_dirname(char *path)
1825{
1826 char *p = rindex(path, '/');
1827 int ret;
1828
1829 if (!p)
1830 return -1;
1831 *p = '\0';
1832 ret = mkdir(path, 0755);
1833 if (ret && errno != EEXIST)
1834 SYSERROR("creating container path %s\n", path);
1835 *p = '/';
1836 return ret;
1837}
1838
1839struct lxc_container *lxcapi_clone(struct lxc_container *c, const char *newname,
1840 const char *lxcpath, int flags,
148e91f5
SH
1841 const char *bdevtype, const char *bdevdata, unsigned long newsize,
1842 char **hookargs)
9be53773
SH
1843{
1844 struct lxc_container *c2 = NULL;
1845 char newpath[MAXPATHLEN];
1846 int ret;
1847 const char *n, *l;
1848 FILE *fout;
1849
1850 if (!c || !c->is_defined(c))
1851 return NULL;
1852
5cee8c50 1853 if (container_mem_lock(c))
9be53773
SH
1854 return NULL;
1855
39dc698c 1856 if (!is_stopped(c)) {
9be53773
SH
1857 ERROR("error: Original container (%s) is running", c->name);
1858 goto out;
1859 }
1860
1861 // Make sure the container doesn't yet exist.
1862 n = newname ? newname : c->name;
1863 l = lxcpath ? lxcpath : c->get_config_path(c);
1864 ret = snprintf(newpath, MAXPATHLEN, "%s/%s/config", l, n);
1865 if (ret < 0 || ret >= MAXPATHLEN) {
1866 SYSERROR("clone: failed making config pathname");
1867 goto out;
1868 }
1869 if (file_exists(newpath)) {
1870 ERROR("error: clone: %s exists", newpath);
1871 goto out;
1872 }
1873
1874 if (create_file_dirname(newpath) < 0) {
1875 ERROR("Error creating container dir for %s", newpath);
1876 goto out;
1877 }
1878
1879 // copy the configuration, tweak it as needed,
1880 fout = fopen(newpath, "w");
1881 if (!fout) {
1882 SYSERROR("open %s", newpath);
1883 goto out;
1884 }
1885 write_config(fout, c->lxc_conf);
1886 fclose(fout);
1887
1888 if (update_name_and_paths(newpath, c, n, l) < 0) {
1889 ERROR("Error updating name in cloned config");
1890 goto out;
1891 }
1892
1893 sprintf(newpath, "%s/%s/rootfs", l, n);
1894 if (mkdir(newpath, 0755) < 0) {
1895 SYSERROR("error creating %s", newpath);
1896 goto out;
1897 }
1898
1899 c2 = lxc_container_new(n, l);
375c2258 1900 if (!c2) {
9be53773
SH
1901 ERROR("clone: failed to create new container (%s %s)", n, l);
1902 goto out;
1903 }
1904
1905 // copy hooks if requested
1906 if (flags & LXC_CLONE_COPYHOOKS) {
1907 ret = copyhooks(c, c2);
1908 if (ret < 0) {
1909 ERROR("error copying hooks");
9be53773
SH
1910 goto out;
1911 }
1912 }
1913
1914 if (copy_fstab(c, c2) < 0) {
1915 ERROR("error copying fstab");
9be53773
SH
1916 goto out;
1917 }
1918
1919 // update macaddrs
1920 if (!(flags & LXC_CLONE_KEEPMACADDR))
1921 network_new_hwaddrs(c2);
1922
1923 // copy/snapshot rootfs's
1924 ret = copy_storage(c, c2, bdevtype, flags, bdevdata, newsize);
375c2258 1925 if (ret < 0)
9be53773 1926 goto out;
9be53773 1927
375c2258 1928 if (!c2->save_config(c2, NULL))
9be53773 1929 goto out;
9be53773 1930
148e91f5 1931 if (clone_update_rootfs(c2, flags, hookargs) < 0)
9be53773 1932 goto out;
9be53773
SH
1933
1934 // TODO: update c's lxc.snapshot = count
5cee8c50 1935 container_mem_unlock(c);
9be53773
SH
1936 return c2;
1937
1938out:
5cee8c50 1939 container_mem_unlock(c);
375c2258
SH
1940 if (c2) {
1941 c2->destroy(c2);
9be53773 1942 lxc_container_put(c2);
375c2258 1943 }
9be53773
SH
1944
1945 return NULL;
1946}
1947
afeecbba 1948struct lxc_container *lxc_container_new(const char *name, const char *configpath)
72d0e1cb
SG
1949{
1950 struct lxc_container *c;
72d0e1cb
SG
1951
1952 c = malloc(sizeof(*c));
1953 if (!c) {
1954 fprintf(stderr, "failed to malloc lxc_container\n");
1955 return NULL;
1956 }
1957 memset(c, 0, sizeof(*c));
1958
afeecbba
SH
1959 if (configpath)
1960 c->config_path = strdup(configpath);
1961 else
67e571de 1962 c->config_path = strdup(default_lxc_path());
afeecbba 1963
2a59a681
SH
1964 if (!c->config_path) {
1965 fprintf(stderr, "Out of memory");
1966 goto err;
1967 }
1968
72d0e1cb
SG
1969 c->name = malloc(strlen(name)+1);
1970 if (!c->name) {
1971 fprintf(stderr, "Error allocating lxc_container name\n");
1972 goto err;
1973 }
1974 strcpy(c->name, name);
1975
1976 c->numthreads = 1;
df271a59 1977 if (!(c->slock = lxc_newlock(c->config_path, name))) {
72d0e1cb
SG
1978 fprintf(stderr, "failed to create lock\n");
1979 goto err;
1980 }
1981
df271a59 1982 if (!(c->privlock = lxc_newlock(NULL, NULL))) {
72d0e1cb
SG
1983 fprintf(stderr, "failed to alloc privlock\n");
1984 goto err;
1985 }
1986
afeecbba 1987 if (!set_config_filename(c)) {
72d0e1cb
SG
1988 fprintf(stderr, "Error allocating config file pathname\n");
1989 goto err;
1990 }
72d0e1cb
SG
1991
1992 if (file_exists(c->configfile))
1993 lxcapi_load_config(c, NULL);
1994
3e625e2d
SH
1995 if (ongoing_create(c) == 2) {
1996 ERROR("Error: %s creation was not completed", c->name);
1897e3bc
SH
1997 lxcapi_destroy(c);
1998 lxc_conf_free(c->lxc_conf);
1999 c->lxc_conf = NULL;
3e625e2d
SH
2000 }
2001
72d0e1cb
SG
2002 // assign the member functions
2003 c->is_defined = lxcapi_is_defined;
2004 c->state = lxcapi_state;
2005 c->is_running = lxcapi_is_running;
2006 c->freeze = lxcapi_freeze;
2007 c->unfreeze = lxcapi_unfreeze;
0115f8fd 2008 c->console = lxcapi_console;
72d0e1cb
SG
2009 c->init_pid = lxcapi_init_pid;
2010 c->load_config = lxcapi_load_config;
2011 c->want_daemonize = lxcapi_want_daemonize;
2012 c->start = lxcapi_start;
2013 c->startl = lxcapi_startl;
2014 c->stop = lxcapi_stop;
2015 c->config_file_name = lxcapi_config_file_name;
2016 c->wait = lxcapi_wait;
2017 c->set_config_item = lxcapi_set_config_item;
2018 c->destroy = lxcapi_destroy;
2019 c->save_config = lxcapi_save_config;
2020 c->get_keys = lxcapi_get_keys;
2021 c->create = lxcapi_create;
2022 c->createl = lxcapi_createl;
2023 c->shutdown = lxcapi_shutdown;
3e625e2d 2024 c->reboot = lxcapi_reboot;
72d0e1cb
SG
2025 c->clear_config_item = lxcapi_clear_config_item;
2026 c->get_config_item = lxcapi_get_config_item;
794dd120
SH
2027 c->get_cgroup_item = lxcapi_get_cgroup_item;
2028 c->set_cgroup_item = lxcapi_set_cgroup_item;
2a59a681
SH
2029 c->get_config_path = lxcapi_get_config_path;
2030 c->set_config_path = lxcapi_set_config_path;
9be53773 2031 c->clone = lxcapi_clone;
9c83a661 2032 c->get_ips = lxcapi_get_ips;
72d0e1cb
SG
2033
2034 /* we'll allow the caller to update these later */
ab1bf971 2035 if (lxc_log_init(NULL, "none", NULL, "lxc_container", 0, c->config_path)) {
72d0e1cb
SG
2036 fprintf(stderr, "failed to open log\n");
2037 goto err;
2038 }
2039
72d0e1cb
SG
2040 return c;
2041
2042err:
2043 lxc_container_free(c);
2044 return NULL;
2045}
2046
4a7c7daa 2047int lxc_get_wait_states(const char **states)
72d0e1cb
SG
2048{
2049 int i;
2050
2051 if (states)
2052 for (i=0; i<MAX_STATE; i++)
2053 states[i] = lxc_state2str(i);
2054 return MAX_STATE;
2055}