3 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2012 Canonical Ltd.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <sys/types.h>
28 #include <sys/mount.h>
34 #include <arpa/inet.h>
39 #include <sys/syscall.h>
41 #include <lxc/lxccontainer.h>
42 #include <lxc/version.h>
43 #include <lxc/network.h>
58 #include "namespace.h"
66 #include <../include/ifaddrs.h>
70 #include <../include/lxcmntent.h>
75 #define MAX_BUFFER 4096
77 #define NOT_SUPPORTED_ERROR "the requested function %s is not currently supported with unprivileged containers"
79 /* Define faccessat() if missing from the C library */
80 #ifndef HAVE_FACCESSAT
81 static int faccessat(int __fd
, const char *__file
, int __type
, int __flag
)
84 return syscall(__NR_faccessat
, __fd
, __file
, __type
, __flag
);
92 lxc_log_define(lxc_container
, lxc
);
94 static bool do_lxcapi_destroy(struct lxc_container
*c
);
95 static const char *lxcapi_get_config_path(struct lxc_container
*c
);
96 #define do_lxcapi_get_config_path(c) lxcapi_get_config_path(c)
97 static bool do_lxcapi_set_config_item(struct lxc_container
*c
, const char *key
, const char *v
);
98 static bool container_destroy(struct lxc_container
*c
);
99 static bool get_snappath_dir(struct lxc_container
*c
, char *snappath
);
100 static bool lxcapi_snapshot_destroy_all(struct lxc_container
*c
);
101 static bool do_lxcapi_save_config(struct lxc_container
*c
, const char *alt_file
);
103 static bool config_file_exists(const char *lxcpath
, const char *cname
)
105 /* $lxcpath + '/' + $cname + '/config' + \0 */
106 int ret
, len
= strlen(lxcpath
) + strlen(cname
) + 9;
107 char *fname
= alloca(len
);
109 ret
= snprintf(fname
, len
, "%s/%s/config", lxcpath
, cname
);
110 if (ret
< 0 || ret
>= len
)
113 return file_exists(fname
);
117 * A few functions to help detect when a container creation failed.
118 * If a container creation was killed partway through, then trying
119 * to actually start that container could harm the host. We detect
120 * this by creating a 'partial' file under the container directory,
121 * and keeping an advisory lock. When container creation completes,
122 * we remove that file. When we load or try to start a container, if
123 * we find that file, without a flock, we remove the container.
125 static int ongoing_create(struct lxc_container
*c
)
127 int len
= strlen(c
->config_path
) + strlen(c
->name
) + 10;
128 char *path
= alloca(len
);
132 ret
= snprintf(path
, len
, "%s/%s/partial", c
->config_path
, c
->name
);
133 if (ret
< 0 || ret
>= len
) {
134 ERROR("Error writing partial pathname");
138 if (!file_exists(path
))
140 fd
= open(path
, O_RDWR
);
142 // give benefit of the doubt
143 SYSERROR("Error opening partial file");
147 lk
.l_whence
= SEEK_SET
;
151 if (fcntl(fd
, F_GETLK
, &lk
) == 0 && lk
.l_pid
!= -1) {
152 // create is still ongoing
156 // create completed but partial is still there.
161 static int create_partial(struct lxc_container
*c
)
163 // $lxcpath + '/' + $name + '/partial' + \0
164 int len
= strlen(c
->config_path
) + strlen(c
->name
) + 10;
165 char *path
= alloca(len
);
169 ret
= snprintf(path
, len
, "%s/%s/partial", c
->config_path
, c
->name
);
170 if (ret
< 0 || ret
>= len
) {
171 ERROR("Error writing partial pathname");
174 if ((fd
=open(path
, O_RDWR
| O_CREAT
| O_EXCL
, 0755)) < 0) {
175 SYSERROR("Erorr creating partial file");
179 lk
.l_whence
= SEEK_SET
;
182 if (fcntl(fd
, F_SETLKW
, &lk
) < 0) {
183 SYSERROR("Error locking partial file %s", path
);
191 static void remove_partial(struct lxc_container
*c
, int fd
)
193 // $lxcpath + '/' + $name + '/partial' + \0
194 int len
= strlen(c
->config_path
) + strlen(c
->name
) + 10;
195 char *path
= alloca(len
);
199 ret
= snprintf(path
, len
, "%s/%s/partial", c
->config_path
, c
->name
);
200 if (ret
< 0 || ret
>= len
) {
201 ERROR("Error writing partial pathname");
204 if (unlink(path
) < 0)
205 SYSERROR("Error unlink partial file %s", path
);
209 * 1. container_mem_lock(c) protects the struct lxc_container from multiple threads.
210 * 2. container_disk_lock(c) protects the on-disk container data - in particular the
211 * container configuration file.
212 * The container_disk_lock also takes the container_mem_lock.
213 * 3. thread_mutex protects process data (ex: fd table) from multiple threads.
214 * NOTHING mutexes two independent programs with their own struct
215 * lxc_container for the same c->name, between API calls. For instance,
216 * c->config_read(); c->start(); Between those calls, data on disk
217 * could change (which shouldn't bother the caller unless for instance
218 * the rootfs get moved). c->config_read(); update; c->config_write();
219 * Two such updaters could race. The callers should therefore check their
220 * results. Trying to prevent that would necessarily expose us to deadlocks
221 * due to hung callers. So I prefer to keep the locks only within our own
222 * functions, not across functions.
224 * If you're going to clone while holding a lxccontainer, increment
225 * c->numthreads (under privlock) before forking. When deleting,
226 * decrement numthreads under privlock, then if it hits 0 you can delete.
227 * Do not ever use a lxccontainer whose numthreads you did not bump.
230 static void lxc_container_free(struct lxc_container
*c
)
236 c
->configfile
= NULL
;
237 free(c
->error_string
);
238 c
->error_string
= NULL
;
240 lxc_putlock(c
->slock
);
244 lxc_putlock(c
->privlock
);
250 lxc_conf_free(c
->lxc_conf
);
253 free(c
->config_path
);
254 c
->config_path
= NULL
;
260 * Consider the following case:
261 freer | racing get()er
262 ==================================================================
263 lxc_container_put() | lxc_container_get()
264 \ lxclock(c->privlock) | c->numthreads < 1? (no)
265 \ c->numthreads = 0 | \ lxclock(c->privlock) -> waits
267 \ lxc_container_free() | \ lxclock() returns
268 | \ c->numthreads < 1 -> return 0
270 \ \ sem_destroy(privlock) |
272 * When the get()er checks numthreads the first time, one of the following
274 * 1. freer has set numthreads = 0. get() returns 0
275 * 2. freer is between lxclock and setting numthreads to 0. get()er will
276 * sem_wait on privlock, get lxclock after freer() drops it, then see
277 * numthreads is 0 and exit without touching lxclock again..
278 * 3. freer has not yet locked privlock. If get()er runs first, then put()er
279 * will see --numthreads = 1 and not call lxc_container_free().
282 int lxc_container_get(struct lxc_container
*c
)
287 // if someone else has already started freeing the container, don't
288 // try to take the lock, which may be invalid
289 if (c
->numthreads
< 1)
292 if (container_mem_lock(c
))
294 if (c
->numthreads
< 1) {
295 // bail without trying to unlock, bc the privlock is now probably
300 container_mem_unlock(c
);
304 int lxc_container_put(struct lxc_container
*c
)
308 if (container_mem_lock(c
))
310 if (--c
->numthreads
< 1) {
311 container_mem_unlock(c
);
312 lxc_container_free(c
);
315 container_mem_unlock(c
);
319 static bool do_lxcapi_is_defined(struct lxc_container
*c
)
328 if (container_mem_lock(c
))
332 statret
= stat(c
->configfile
, &statbuf
);
338 container_mem_unlock(c
);
342 #define WRAP_API(rettype, fnname) \
343 static rettype fnname(struct lxc_container *c) \
346 struct lxc_conf *old = current_config; \
347 current_config = c ? c->lxc_conf : NULL; \
348 ret = do_##fnname(c); \
349 current_config = old; \
353 #define WRAP_API_1(rettype, fnname, t1) \
354 static rettype fnname(struct lxc_container *c, t1 a1) \
357 struct lxc_conf *old = current_config; \
358 current_config = c ? c->lxc_conf : NULL; \
359 ret = do_##fnname(c, a1); \
360 current_config = old; \
364 #define WRAP_API_2(rettype, fnname, t1, t2) \
365 static rettype fnname(struct lxc_container *c, t1 a1, t2 a2) \
368 struct lxc_conf *old = current_config; \
369 current_config = c ? c->lxc_conf : NULL; \
370 ret = do_##fnname(c, a1, a2); \
371 current_config = old; \
375 #define WRAP_API_3(rettype, fnname, t1, t2, t3) \
376 static rettype fnname(struct lxc_container *c, t1 a1, t2 a2, t3 a3) \
379 struct lxc_conf *old = current_config; \
380 current_config = c ? c->lxc_conf : NULL; \
381 ret = do_##fnname(c, a1, a2, a3); \
382 current_config = old; \
386 WRAP_API(bool, lxcapi_is_defined
)
388 static const char *do_lxcapi_state(struct lxc_container
*c
)
394 s
= lxc_getstate(c
->name
, c
->config_path
);
395 return lxc_state2str(s
);
398 WRAP_API(const char *, lxcapi_state
)
400 static bool is_stopped(struct lxc_container
*c
)
403 s
= lxc_getstate(c
->name
, c
->config_path
);
404 return (s
== STOPPED
);
407 static bool do_lxcapi_is_running(struct lxc_container
*c
)
413 s
= do_lxcapi_state(c
);
414 if (!s
|| strcmp(s
, "STOPPED") == 0)
419 WRAP_API(bool, lxcapi_is_running
)
421 static bool do_lxcapi_freeze(struct lxc_container
*c
)
427 ret
= lxc_freeze(c
->name
, c
->config_path
);
433 WRAP_API(bool, lxcapi_freeze
)
435 static bool do_lxcapi_unfreeze(struct lxc_container
*c
)
441 ret
= lxc_unfreeze(c
->name
, c
->config_path
);
447 WRAP_API(bool, lxcapi_unfreeze
)
449 static int do_lxcapi_console_getfd(struct lxc_container
*c
, int *ttynum
, int *masterfd
)
455 ttyfd
= lxc_console_getfd(c
, ttynum
, masterfd
);
459 WRAP_API_2(int, lxcapi_console_getfd
, int *, int *)
461 static int lxcapi_console(struct lxc_container
*c
, int ttynum
, int stdinfd
,
462 int stdoutfd
, int stderrfd
, int escape
)
465 struct lxc_conf
*old
= current_config
;
466 current_config
= c
? c
->lxc_conf
: NULL
;
467 ret
= lxc_console(c
, ttynum
, stdinfd
, stdoutfd
, stderrfd
, escape
);
468 current_config
= old
;
472 static pid_t
do_lxcapi_init_pid(struct lxc_container
*c
)
477 return lxc_cmd_get_init_pid(c
->name
, c
->config_path
);
480 WRAP_API(pid_t
, lxcapi_init_pid
)
482 static bool load_config_locked(struct lxc_container
*c
, const char *fname
)
485 c
->lxc_conf
= lxc_conf_init();
488 if (lxc_config_read(fname
, c
->lxc_conf
, false) != 0)
493 static bool do_lxcapi_load_config(struct lxc_container
*c
, const char *alt_file
)
495 bool ret
= false, need_disklock
= false;
501 fname
= c
->configfile
;
507 * If we're reading something other than the container's config,
508 * we only need to lock the in-memory container. If loading the
509 * container's config file, take the disk lock.
511 if (strcmp(fname
, c
->configfile
) == 0)
512 need_disklock
= true;
515 lret
= container_disk_lock(c
);
517 lret
= container_mem_lock(c
);
521 ret
= load_config_locked(c
, fname
);
524 container_disk_unlock(c
);
526 container_mem_unlock(c
);
530 WRAP_API_1(bool, lxcapi_load_config
, const char *)
532 static bool do_lxcapi_want_daemonize(struct lxc_container
*c
, bool state
)
534 if (!c
|| !c
->lxc_conf
)
536 if (container_mem_lock(c
)) {
537 ERROR("Error getting mem lock");
540 c
->daemonize
= state
;
541 container_mem_unlock(c
);
545 WRAP_API_1(bool, lxcapi_want_daemonize
, bool)
547 static bool do_lxcapi_want_close_all_fds(struct lxc_container
*c
, bool state
)
549 if (!c
|| !c
->lxc_conf
)
551 if (container_mem_lock(c
)) {
552 ERROR("Error getting mem lock");
555 c
->lxc_conf
->close_all_fds
= state
;
556 container_mem_unlock(c
);
560 WRAP_API_1(bool, lxcapi_want_close_all_fds
, bool)
562 static bool do_lxcapi_wait(struct lxc_container
*c
, const char *state
, int timeout
)
569 ret
= lxc_wait(c
->name
, state
, timeout
, c
->config_path
);
573 WRAP_API_2(bool, lxcapi_wait
, const char *, int)
575 static bool do_wait_on_daemonized_start(struct lxc_container
*c
, int pid
)
577 /* we'll probably want to make this timeout configurable? */
578 int timeout
= 5, ret
, status
;
581 * our child is going to fork again, then exit. reap the
584 ret
= waitpid(pid
, &status
, 0);
585 if (ret
== -1 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
586 DEBUG("failed waiting for first dual-fork child");
587 return do_lxcapi_wait(c
, "RUNNING", timeout
);
590 WRAP_API_1(bool, wait_on_daemonized_start
, int)
592 static bool am_single_threaded(void)
594 struct dirent dirent
, *direntp
;
598 dir
= opendir("/proc/self/task");
600 INFO("failed to open /proc/self/task");
604 while (!readdir_r(dir
, &dirent
, &direntp
)) {
608 if (!strcmp(direntp
->d_name
, "."))
611 if (!strcmp(direntp
->d_name
, ".."))
621 * I can't decide if it'd be more convenient for callers if we accept '...',
622 * or a null-terminated array (i.e. execl vs execv)
624 static bool do_lxcapi_start(struct lxc_container
*c
, int useinit
, char * const argv
[])
627 struct lxc_conf
*conf
;
628 bool daemonize
= false;
630 char *default_args
[] = {
636 /* container exists */
639 /* container has been setup */
643 if ((ret
= ongoing_create(c
)) < 0) {
644 ERROR("Error checking for incomplete creation");
648 ERROR("Error: %s creation was not completed", c
->name
);
649 do_lxcapi_destroy(c
);
651 } else if (ret
== 1) {
652 ERROR("Error: creation of %s is ongoing", c
->name
);
656 /* is this app meant to be run through lxcinit, as in lxc-execute? */
657 if (useinit
&& !argv
)
660 if (container_mem_lock(c
))
663 daemonize
= c
->daemonize
;
664 container_mem_unlock(c
);
667 ret
= lxc_execute(c
->name
, argv
, 1, conf
, c
->config_path
);
668 return ret
== 0 ? true : false;
672 if (conf
->init_cmd
) {
673 init_cmd
[0] = conf
->init_cmd
;
682 * say, I'm not sure - what locks do we want here? Any?
683 * Is liblxc's locking enough here to protect the on disk
684 * container? We don't want to exclude things like lxc_info
685 * while container is running...
689 lxc_monitord_spawn(c
->config_path
);
696 /* Set to NULL because we don't want father unlink
697 * the PID file, child will do the free and unlink.
700 return wait_on_daemonized_start(c
, pid
);
703 /* We don't really care if this doesn't print all the
704 * characters; all that it means is that the proctitle will be
705 * ugly. Similarly, we also don't care if setproctitle()
707 snprintf(title
, sizeof(title
), "[lxc monitor] %s %s", c
->config_path
, c
->name
);
708 INFO("Attempting to set proc title to %s", title
);
711 /* second fork to be reparented by init */
714 SYSERROR("Error doing dual-fork");
719 /* like daemon(), chdir to / and redirect 0,1,2 to /dev/null */
721 SYSERROR("Error chdir()ing to /.");
724 lxc_check_inherited(conf
, true, -1);
728 open("/dev/zero", O_RDONLY
);
729 open("/dev/null", O_RDWR
);
730 open("/dev/null", O_RDWR
);
733 if (!am_single_threaded()) {
734 ERROR("Cannot start non-daemonized container when threaded");
739 /* We need to write PID file after daeminize, so we always
740 * write the right PID.
743 pid_fp
= fopen(c
->pidfile
, "w");
744 if (pid_fp
== NULL
) {
745 SYSERROR("Failed to create pidfile '%s' for '%s'",
746 c
->pidfile
, c
->name
);
750 if (fprintf(pid_fp
, "%d\n", getpid()) < 0) {
751 SYSERROR("Failed to write '%s'", c
->pidfile
);
764 if (lxc_check_inherited(conf
, daemonize
, -1)) {
765 ERROR("Inherited fds found");
770 ret
= lxc_start(c
->name
, argv
, conf
, c
->config_path
);
774 INFO("container requested reboot");
787 exit (ret
== 0 ? true : false);
789 return (ret
== 0 ? true : false);
792 static bool lxcapi_start(struct lxc_container
*c
, int useinit
, char * const argv
[])
795 struct lxc_conf
*old
= current_config
;
796 current_config
= c
? c
->lxc_conf
: NULL
;
797 ret
= do_lxcapi_start(c
, useinit
, argv
);
798 current_config
= old
;
803 * note there MUST be an ending NULL
805 static bool lxcapi_startl(struct lxc_container
*c
, int useinit
, ...)
808 char **inargs
= NULL
;
811 /* container exists */
815 struct lxc_conf
*old
= current_config
;
816 current_config
= c
->lxc_conf
;
818 va_start(ap
, useinit
);
819 inargs
= lxc_va_arg_list_to_argv(ap
, 0, 1);
823 ERROR("Memory allocation error.");
827 /* pass NULL if no arguments were supplied */
828 bret
= do_lxcapi_start(c
, useinit
, *inargs
? inargs
: NULL
);
833 for (arg
= inargs
; *arg
; arg
++)
838 current_config
= old
;
842 static bool do_lxcapi_stop(struct lxc_container
*c
)
849 ret
= lxc_cmd_stop(c
->name
, c
->config_path
);
854 WRAP_API(bool, lxcapi_stop
)
856 static int do_create_container_dir(const char *path
, struct lxc_conf
*conf
)
858 int ret
= -1, lasterr
;
859 char *p
= alloca(strlen(path
)+1);
860 mode_t mask
= umask(0002);
861 ret
= mkdir(path
, 0770);
869 SYSERROR("failed to create container path %s", path
);
874 if (!lxc_list_empty(&conf
->id_map
) && chown_mapped_root(p
, conf
) != 0) {
875 ERROR("Failed to chown container dir");
882 * create the standard expected container dir
884 static bool create_container_dir(struct lxc_container
*c
)
889 len
= strlen(c
->config_path
) + strlen(c
->name
) + 2;
893 ret
= snprintf(s
, len
, "%s/%s", c
->config_path
, c
->name
);
894 if (ret
< 0 || ret
>= len
) {
898 ret
= do_create_container_dir(s
, c
->lxc_conf
);
904 * do_bdev_create: thin wrapper around bdev_create(). Like bdev_create(),
905 * it returns a mounted bdev on success, NULL on error.
907 static struct bdev
*do_bdev_create(struct lxc_container
*c
, const char *type
,
908 struct bdev_specs
*specs
)
915 /* rootfs.path or lxcpath/lxcname/rootfs */
916 if (c
->lxc_conf
->rootfs
.path
&& access(c
->lxc_conf
->rootfs
.path
, F_OK
) == 0) {
917 const char *rpath
= c
->lxc_conf
->rootfs
.path
;
918 len
= strlen(rpath
) + 1;
920 ret
= snprintf(dest
, len
, "%s", rpath
);
922 const char *lxcpath
= do_lxcapi_get_config_path(c
);
923 len
= strlen(c
->name
) + strlen(lxcpath
) + 9;
925 ret
= snprintf(dest
, len
, "%s/%s/rootfs", lxcpath
, c
->name
);
927 if (ret
< 0 || ret
>= len
)
930 bdev
= bdev_create(dest
, type
, c
->name
, specs
);
932 ERROR("Failed to create backing store type %s", type
);
936 do_lxcapi_set_config_item(c
, "lxc.rootfs", bdev
->src
);
938 /* if we are not root, chown the rootfs dir to root in the
941 if (geteuid() != 0 || (c
->lxc_conf
&& !lxc_list_empty(&c
->lxc_conf
->id_map
))) {
942 if (chown_mapped_root(bdev
->dest
, c
->lxc_conf
) < 0) {
943 ERROR("Error chowning %s to container root", bdev
->dest
);
944 suggest_default_idmap();
953 static char *lxcbasename(char *path
)
955 char *p
= path
+ strlen(path
) - 1;
956 while (*p
!= '/' && p
> path
)
961 static bool create_run_template(struct lxc_container
*c
, char *tpath
, bool quiet
,
971 SYSERROR("failed to fork task for container creation template");
975 if (pid
== 0) { // child
976 char *patharg
, *namearg
, *rootfsarg
, *src
;
977 struct bdev
*bdev
= NULL
;
979 int ret
, len
, nargs
= 0;
981 struct lxc_conf
*conf
= c
->lxc_conf
;
987 open("/dev/zero", O_RDONLY
);
988 open("/dev/null", O_RDWR
);
989 open("/dev/null", O_RDWR
);
992 src
= c
->lxc_conf
->rootfs
.path
;
994 * for an overlay create, what the user wants is the template to fill
995 * in what will become the readonly lower layer. So don't mount for
998 if (strncmp(src
, "overlayfs:", 10) == 0)
999 src
= overlay_getlower(src
+10);
1000 if (strncmp(src
, "aufs:", 5) == 0)
1001 src
= overlay_getlower(src
+5);
1003 bdev
= bdev_init(c
->lxc_conf
, src
, c
->lxc_conf
->rootfs
.mount
, NULL
);
1005 ERROR("Error opening rootfs");
1009 if (geteuid() == 0) {
1010 if (unshare(CLONE_NEWNS
) < 0) {
1011 ERROR("error unsharing mounts");
1014 if (detect_shared_rootfs()) {
1015 if (mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
)) {
1016 SYSERROR("Failed to make / rslave to run template");
1017 ERROR("Continuing...");
1021 if (strcmp(bdev
->type
, "dir") && strcmp(bdev
->type
, "btrfs")) {
1022 if (geteuid() != 0) {
1023 ERROR("non-root users can only create btrfs and directory-backed containers");
1026 if (bdev
->ops
->mount(bdev
) < 0) {
1027 ERROR("Error mounting rootfs");
1030 } else { // TODO come up with a better way here!
1032 bdev
->dest
= strdup(bdev
->src
);
1036 * create our new array, pre-pend the template name and
1040 for (nargs
= 0; argv
[nargs
]; nargs
++) ;
1041 nargs
+= 4; // template, path, rootfs and name args
1043 newargv
= malloc(nargs
* sizeof(*newargv
));
1046 newargv
[0] = lxcbasename(tpath
);
1048 len
= strlen(c
->config_path
) + strlen(c
->name
) + strlen("--path=") + 2;
1049 patharg
= malloc(len
);
1052 ret
= snprintf(patharg
, len
, "--path=%s/%s", c
->config_path
, c
->name
);
1053 if (ret
< 0 || ret
>= len
)
1055 newargv
[1] = patharg
;
1056 len
= strlen("--name=") + strlen(c
->name
) + 1;
1057 namearg
= malloc(len
);
1060 ret
= snprintf(namearg
, len
, "--name=%s", c
->name
);
1061 if (ret
< 0 || ret
>= len
)
1063 newargv
[2] = namearg
;
1065 len
= strlen("--rootfs=") + 1 + strlen(bdev
->dest
);
1066 rootfsarg
= malloc(len
);
1069 ret
= snprintf(rootfsarg
, len
, "--rootfs=%s", bdev
->dest
);
1070 if (ret
< 0 || ret
>= len
)
1072 newargv
[3] = rootfsarg
;
1074 /* add passed-in args */
1076 for (i
= 4; i
< nargs
; i
++)
1077 newargv
[i
] = argv
[i
-4];
1079 /* add trailing NULL */
1081 newargv
= realloc(newargv
, nargs
* sizeof(*newargv
));
1084 newargv
[nargs
- 1] = NULL
;
1087 * If we're running the template in a mapped userns, then
1088 * we prepend the template command with:
1089 * lxc-usernsexec <-m map1> ... <-m mapn> --
1090 * and we append "--mapped-uid x", where x is the mapped uid
1093 if (!lxc_list_empty(&conf
->id_map
)) {
1097 char **n2
= malloc(n2args
* sizeof(*n2
));
1098 struct lxc_list
*it
;
1102 SYSERROR("out of memory");
1106 tpath
= "lxc-usernsexec";
1107 n2
[0] = "lxc-usernsexec";
1108 lxc_list_for_each(it
, &conf
->id_map
) {
1111 n2
= realloc(n2
, n2args
* sizeof(char *));
1114 n2
[n2args
-2] = "-m";
1115 n2
[n2args
-1] = malloc(200);
1118 ret
= snprintf(n2
[n2args
-1], 200, "%c:%lu:%lu:%lu",
1119 map
->idtype
== ID_TYPE_UID
? 'u' : 'g',
1120 map
->nsid
, map
->hostid
, map
->range
);
1121 if (ret
< 0 || ret
>= 200)
1124 int hostid_mapped
= mapped_hostid(geteuid(), conf
, ID_TYPE_UID
);
1125 int extraargs
= hostid_mapped
>= 0 ? 1 : 3;
1126 n2
= realloc(n2
, (nargs
+ n2args
+ extraargs
) * sizeof(char *));
1129 if (hostid_mapped
< 0) {
1130 hostid_mapped
= find_unmapped_nsuid(conf
, ID_TYPE_UID
);
1131 n2
[n2args
++] = "-m";
1132 if (hostid_mapped
< 0) {
1133 ERROR("Could not find free uid to map");
1136 n2
[n2args
++] = malloc(200);
1137 if (!n2
[n2args
-1]) {
1138 SYSERROR("out of memory");
1141 ret
= snprintf(n2
[n2args
-1], 200, "u:%d:%d:1",
1142 hostid_mapped
, geteuid());
1143 if (ret
< 0 || ret
>= 200) {
1144 ERROR("string too long");
1148 int hostgid_mapped
= mapped_hostid(getegid(), conf
, ID_TYPE_GID
);
1149 extraargs
= hostgid_mapped
>= 0 ? 1 : 3;
1150 n2
= realloc(n2
, (nargs
+ n2args
+ extraargs
) * sizeof(char *));
1153 if (hostgid_mapped
< 0) {
1154 hostgid_mapped
= find_unmapped_nsuid(conf
, ID_TYPE_GID
);
1155 n2
[n2args
++] = "-m";
1156 if (hostgid_mapped
< 0) {
1157 ERROR("Could not find free uid to map");
1160 n2
[n2args
++] = malloc(200);
1161 if (!n2
[n2args
-1]) {
1162 SYSERROR("out of memory");
1165 ret
= snprintf(n2
[n2args
-1], 200, "g:%d:%d:1",
1166 hostgid_mapped
, getegid());
1167 if (ret
< 0 || ret
>= 200) {
1168 ERROR("string too long");
1172 n2
[n2args
++] = "--";
1173 for (i
= 0; i
< nargs
; i
++)
1174 n2
[i
+ n2args
] = newargv
[i
];
1176 // Finally add "--mapped-uid $uid" to tell template what to chown
1179 n2
= realloc(n2
, n2args
* sizeof(char *));
1181 SYSERROR("out of memory");
1184 // note n2[n2args-1] is NULL
1185 n2
[n2args
-5] = "--mapped-uid";
1186 snprintf(txtuid
, 20, "%d", hostid_mapped
);
1187 n2
[n2args
-4] = txtuid
;
1188 n2
[n2args
-3] = "--mapped-gid";
1189 snprintf(txtgid
, 20, "%d", hostgid_mapped
);
1190 n2
[n2args
-2] = txtgid
;
1191 n2
[n2args
-1] = NULL
;
1196 execvp(tpath
, newargv
);
1197 SYSERROR("failed to execute template %s", tpath
);
1201 if (wait_for_pid(pid
) != 0) {
1202 ERROR("container creation template for %s failed", c
->name
);
1209 static bool prepend_lxc_header(char *path
, const char *t
, char *const argv
[])
1217 unsigned char md_value
[SHA_DIGEST_LENGTH
];
1221 f
= fopen(path
, "r");
1225 if (fseek(f
, 0, SEEK_END
) < 0)
1227 if ((flen
= ftell(f
)) < 0)
1229 if (fseek(f
, 0, SEEK_SET
) < 0)
1231 if ((contents
= malloc(flen
+ 1)) == NULL
)
1233 if (fread(contents
, 1, flen
, f
) != flen
)
1234 goto out_free_contents
;
1236 contents
[flen
] = '\0';
1240 goto out_free_contents
;
1243 tpath
= get_template_path(t
);
1245 ERROR("bad template: %s", t
);
1246 goto out_free_contents
;
1249 ret
= sha1sum_file(tpath
, md_value
);
1251 ERROR("Error getting sha1sum of %s", tpath
);
1253 goto out_free_contents
;
1258 f
= fopen(path
, "w");
1260 SYSERROR("reopening config for writing");
1264 fprintf(f
, "# Template used to create this container: %s\n", t
);
1266 fprintf(f
, "# Parameters passed to the template:");
1268 fprintf(f
, " %s", *argv
);
1274 fprintf(f
, "# Template script checksum (SHA-1): ");
1275 for (i
=0; i
<SHA_DIGEST_LENGTH
; i
++)
1276 fprintf(f
, "%02x", md_value
[i
]);
1279 fprintf(f
, "# For additional config options, please look at lxc.container.conf(5)\n");
1280 if (fwrite(contents
, 1, flen
, f
) != flen
) {
1281 SYSERROR("Writing original contents");
1297 SYSERROR("Error prepending header");
1303 static void lxcapi_clear_config(struct lxc_container
*c
)
1307 lxc_conf_free(c
->lxc_conf
);
1313 #define do_lxcapi_clear_config(c) lxcapi_clear_config(c)
1317 * create a container with the given parameters.
1318 * @c: container to be created. It has the lxcpath, name, and a starting
1319 * configuration already set
1320 * @t: the template to execute to instantiate the root filesystem and
1321 * adjust the configuration.
1322 * @bdevtype: backing store type to use. If NULL, dir will be used.
1323 * @specs: additional parameters for the backing store, i.e. LVM vg to
1326 * @argv: the arguments to pass to the template, terminated by NULL. If no
1327 * arguments, you can just pass NULL.
1329 static bool do_lxcapi_create(struct lxc_container
*c
, const char *t
,
1330 const char *bdevtype
, struct bdev_specs
*specs
, int flags
,
1342 tpath
= get_template_path(t
);
1344 ERROR("bad template: %s", t
);
1350 * If a template is passed in, and the rootfs already is defined in
1351 * the container config and exists, then * caller is trying to create
1352 * an existing container. Return an error, but do NOT delete the
1355 if (do_lxcapi_is_defined(c
) && c
->lxc_conf
&& c
->lxc_conf
->rootfs
.path
&&
1356 access(c
->lxc_conf
->rootfs
.path
, F_OK
) == 0 && tpath
) {
1357 ERROR("Container %s:%s already exists", c
->config_path
, c
->name
);
1362 if (!do_lxcapi_load_config(c
, lxc_global_config_value("lxc.default_config"))) {
1363 ERROR("Error loading default configuration file %s", lxc_global_config_value("lxc.default_config"));
1368 if (!create_container_dir(c
))
1372 * either template or rootfs.path should be set.
1373 * if both template and rootfs.path are set, template is setup as rootfs.path.
1374 * container is already created if we have a config and rootfs.path is accessible
1376 if (!c
->lxc_conf
->rootfs
.path
&& !tpath
)
1377 /* no template passed in and rootfs does not exist: error */
1379 if (c
->lxc_conf
->rootfs
.path
&& access(c
->lxc_conf
->rootfs
.path
, F_OK
) != 0)
1380 /* rootfs passed into configuration, but does not exist: error */
1382 if (do_lxcapi_is_defined(c
) && c
->lxc_conf
->rootfs
.path
&& !tpath
) {
1383 /* Rootfs already existed, user just wanted to save the
1384 * loaded configuration */
1389 /* Mark that this container is being created */
1390 if ((partial_fd
= create_partial(c
)) < 0)
1393 /* no need to get disk lock bc we have the partial locked */
1396 * Create the backing store
1397 * Note we can't do this in the same task as we use to execute the
1398 * template because of the way zfs works.
1399 * After you 'zfs create', zfs mounts the fs only in the initial
1404 SYSERROR("failed to fork task for container creation template");
1408 if (pid
== 0) { // child
1409 struct bdev
*bdev
= NULL
;
1411 if (!(bdev
= do_bdev_create(c
, bdevtype
, specs
))) {
1412 ERROR("Error creating backing store type %s for %s",
1413 bdevtype
? bdevtype
: "(none)", c
->name
);
1417 /* save config file again to store the new rootfs location */
1418 if (!do_lxcapi_save_config(c
, NULL
)) {
1419 ERROR("failed to save starting configuration for %s", c
->name
);
1420 // parent task won't see bdev in config so we delete it
1421 bdev
->ops
->umount(bdev
);
1422 bdev
->ops
->destroy(bdev
);
1427 if (wait_for_pid(pid
) != 0)
1430 /* reload config to get the rootfs */
1431 lxc_conf_free(c
->lxc_conf
);
1433 if (!load_config_locked(c
, c
->configfile
))
1436 if (!create_run_template(c
, tpath
, !!(flags
& LXC_CREATE_QUIET
), argv
))
1439 // now clear out the lxc_conf we have, reload from the created
1441 do_lxcapi_clear_config(c
);
1444 if (!prepend_lxc_header(c
->configfile
, tpath
, argv
)) {
1445 ERROR("Error prepending header to configuration file");
1449 ret
= load_config_locked(c
, c
->configfile
);
1452 if (partial_fd
>= 0)
1453 remove_partial(c
, partial_fd
);
1456 container_destroy(c
);
1462 static bool lxcapi_create(struct lxc_container
*c
, const char *t
,
1463 const char *bdevtype
, struct bdev_specs
*specs
, int flags
,
1467 struct lxc_conf
*old
= current_config
;
1468 current_config
= c
? c
->lxc_conf
: NULL
;
1469 ret
= do_lxcapi_create(c
, t
, bdevtype
, specs
, flags
, argv
);
1470 current_config
= old
;
1474 static bool do_lxcapi_reboot(struct lxc_container
*c
)
1477 int rebootsignal
= SIGINT
;
1481 if (!do_lxcapi_is_running(c
))
1483 pid
= do_lxcapi_init_pid(c
);
1486 if (c
->lxc_conf
&& c
->lxc_conf
->rebootsignal
)
1487 rebootsignal
= c
->lxc_conf
->rebootsignal
;
1488 if (kill(pid
, rebootsignal
) < 0)
1494 WRAP_API(bool, lxcapi_reboot
)
1496 static bool do_lxcapi_shutdown(struct lxc_container
*c
, int timeout
)
1500 int haltsignal
= SIGPWR
;
1505 if (!do_lxcapi_is_running(c
))
1507 pid
= do_lxcapi_init_pid(c
);
1510 if (c
->lxc_conf
&& c
->lxc_conf
->haltsignal
)
1511 haltsignal
= c
->lxc_conf
->haltsignal
;
1512 kill(pid
, haltsignal
);
1513 retv
= do_lxcapi_wait(c
, "STOPPED", timeout
);
1517 WRAP_API_1(bool, lxcapi_shutdown
, int)
1519 static bool lxcapi_createl(struct lxc_container
*c
, const char *t
,
1520 const char *bdevtype
, struct bdev_specs
*specs
, int flags
, ...)
1529 struct lxc_conf
*old
= current_config
;
1530 current_config
= c
? c
->lxc_conf
: NULL
;
1533 * since we're going to wait for create to finish, I don't think we
1534 * need to get a copy of the arguments.
1536 va_start(ap
, flags
);
1537 args
= lxc_va_arg_list_to_argv(ap
, 0, 0);
1540 ERROR("Memory allocation error.");
1544 bret
= do_lxcapi_create(c
, t
, bdevtype
, specs
, flags
, args
);
1548 current_config
= old
;
1552 static void do_clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
)
1554 if (strcmp(key
, "lxc.cgroup") == 0)
1555 clear_unexp_config_line(conf
, key
, true);
1556 else if (strcmp(key
, "lxc.network") == 0)
1557 clear_unexp_config_line(conf
, key
, true);
1558 else if (strcmp(key
, "lxc.hook") == 0)
1559 clear_unexp_config_line(conf
, key
, true);
1561 clear_unexp_config_line(conf
, key
, false);
1562 if (!do_append_unexp_config_line(conf
, key
, ""))
1563 WARN("Error clearing configuration for %s", key
);
1566 static bool do_lxcapi_clear_config_item(struct lxc_container
*c
, const char *key
)
1570 if (!c
|| !c
->lxc_conf
)
1572 if (container_mem_lock(c
))
1574 ret
= lxc_clear_config_item(c
->lxc_conf
, key
);
1576 do_clear_unexp_config_line(c
->lxc_conf
, key
);
1577 container_mem_unlock(c
);
1581 WRAP_API_1(bool, lxcapi_clear_config_item
, const char *)
1583 static inline bool enter_net_ns(struct lxc_container
*c
)
1585 pid_t pid
= do_lxcapi_init_pid(c
);
1587 if ((geteuid() != 0 || (c
->lxc_conf
&& !lxc_list_empty(&c
->lxc_conf
->id_map
))) && access("/proc/self/ns/user", F_OK
) == 0) {
1588 if (!switch_to_ns(pid
, "user"))
1591 return switch_to_ns(pid
, "net");
1594 // used by qsort and bsearch functions for comparing names
1595 static inline int string_cmp(char **first
, char **second
)
1597 return strcmp(*first
, *second
);
1600 // used by qsort and bsearch functions for comparing container names
1601 static inline int container_cmp(struct lxc_container
**first
, struct lxc_container
**second
)
1603 return strcmp((*first
)->name
, (*second
)->name
);
1606 static bool add_to_array(char ***names
, char *cname
, int pos
)
1608 char **newnames
= realloc(*names
, (pos
+1) * sizeof(char *));
1610 ERROR("Out of memory");
1615 newnames
[pos
] = strdup(cname
);
1619 // sort the arrray as we will use binary search on it
1620 qsort(newnames
, pos
+ 1, sizeof(char *), (int (*)(const void *,const void *))string_cmp
);
1625 static bool add_to_clist(struct lxc_container
***list
, struct lxc_container
*c
, int pos
, bool sort
)
1627 struct lxc_container
**newlist
= realloc(*list
, (pos
+1) * sizeof(struct lxc_container
*));
1629 ERROR("Out of memory");
1636 // sort the arrray as we will use binary search on it
1638 qsort(newlist
, pos
+ 1, sizeof(struct lxc_container
*), (int (*)(const void *,const void *))container_cmp
);
1643 static char** get_from_array(char ***names
, char *cname
, int size
)
1645 return (char **)bsearch(&cname
, *names
, size
, sizeof(char *), (int (*)(const void *, const void *))string_cmp
);
1649 static bool array_contains(char ***names
, char *cname
, int size
) {
1650 if(get_from_array(names
, cname
, size
) != NULL
)
1655 static bool remove_from_array(char ***names
, char *cname
, int size
)
1657 char **result
= get_from_array(names
, cname
, size
);
1658 if (result
!= NULL
) {
1665 static char ** do_lxcapi_get_interfaces(struct lxc_container
*c
)
1668 int i
, count
= 0, pipefd
[2];
1669 char **interfaces
= NULL
;
1670 char interface
[IFNAMSIZ
];
1672 if(pipe(pipefd
) < 0) {
1673 SYSERROR("pipe failed");
1679 SYSERROR("failed to fork task to get interfaces information");
1685 if (pid
== 0) { // child
1686 int ret
= 1, nbytes
;
1687 struct ifaddrs
*interfaceArray
= NULL
, *tempIfAddr
= NULL
;
1689 /* close the read-end of the pipe */
1692 if (!enter_net_ns(c
)) {
1693 SYSERROR("failed to enter namespace");
1697 /* Grab the list of interfaces */
1698 if (getifaddrs(&interfaceArray
)) {
1699 SYSERROR("failed to get interfaces list");
1703 /* Iterate through the interfaces */
1704 for (tempIfAddr
= interfaceArray
; tempIfAddr
!= NULL
; tempIfAddr
= tempIfAddr
->ifa_next
) {
1705 nbytes
= write(pipefd
[1], tempIfAddr
->ifa_name
, IFNAMSIZ
);
1707 ERROR("write failed");
1716 freeifaddrs(interfaceArray
);
1718 /* close the write-end of the pipe, thus sending EOF to the reader */
1723 /* close the write-end of the pipe */
1726 while (read(pipefd
[0], &interface
, IFNAMSIZ
) == IFNAMSIZ
) {
1727 if (array_contains(&interfaces
, interface
, count
))
1730 if(!add_to_array(&interfaces
, interface
, count
))
1731 ERROR("PARENT: add_to_array failed");
1735 if (wait_for_pid(pid
) != 0) {
1736 for(i
=0;i
<count
;i
++)
1737 free(interfaces
[i
]);
1742 /* close the read-end of the pipe */
1745 /* Append NULL to the array */
1747 interfaces
= (char **)lxc_append_null_to_array((void **)interfaces
, count
);
1752 WRAP_API(char **, lxcapi_get_interfaces
)
1754 static char** do_lxcapi_get_ips(struct lxc_container
*c
, const char* interface
, const char* family
, int scope
)
1757 int i
, count
= 0, pipefd
[2];
1758 char **addresses
= NULL
;
1759 char address
[INET6_ADDRSTRLEN
];
1761 if(pipe(pipefd
) < 0) {
1762 SYSERROR("pipe failed");
1768 SYSERROR("failed to fork task to get container ips");
1774 if (pid
== 0) { // child
1775 int ret
= 1, nbytes
;
1776 struct ifaddrs
*interfaceArray
= NULL
, *tempIfAddr
= NULL
;
1777 char addressOutputBuffer
[INET6_ADDRSTRLEN
];
1778 void *tempAddrPtr
= NULL
;
1779 char *address
= NULL
;
1781 /* close the read-end of the pipe */
1784 if (!enter_net_ns(c
)) {
1785 SYSERROR("failed to enter namespace");
1789 /* Grab the list of interfaces */
1790 if (getifaddrs(&interfaceArray
)) {
1791 SYSERROR("failed to get interfaces list");
1795 /* Iterate through the interfaces */
1796 for (tempIfAddr
= interfaceArray
; tempIfAddr
!= NULL
; tempIfAddr
= tempIfAddr
->ifa_next
) {
1797 if (tempIfAddr
->ifa_addr
== NULL
)
1800 if(tempIfAddr
->ifa_addr
->sa_family
== AF_INET
) {
1801 if (family
&& strcmp(family
, "inet"))
1803 tempAddrPtr
= &((struct sockaddr_in
*)tempIfAddr
->ifa_addr
)->sin_addr
;
1806 if (family
&& strcmp(family
, "inet6"))
1809 if (((struct sockaddr_in6
*)tempIfAddr
->ifa_addr
)->sin6_scope_id
!= scope
)
1812 tempAddrPtr
= &((struct sockaddr_in6
*)tempIfAddr
->ifa_addr
)->sin6_addr
;
1815 if (interface
&& strcmp(interface
, tempIfAddr
->ifa_name
))
1817 else if (!interface
&& strcmp("lo", tempIfAddr
->ifa_name
) == 0)
1820 address
= (char *)inet_ntop(tempIfAddr
->ifa_addr
->sa_family
,
1822 addressOutputBuffer
,
1823 sizeof(addressOutputBuffer
));
1827 nbytes
= write(pipefd
[1], address
, INET6_ADDRSTRLEN
);
1829 ERROR("write failed");
1838 freeifaddrs(interfaceArray
);
1840 /* close the write-end of the pipe, thus sending EOF to the reader */
1845 /* close the write-end of the pipe */
1848 while (read(pipefd
[0], &address
, INET6_ADDRSTRLEN
) == INET6_ADDRSTRLEN
) {
1849 if(!add_to_array(&addresses
, address
, count
))
1850 ERROR("PARENT: add_to_array failed");
1854 if (wait_for_pid(pid
) != 0) {
1855 for(i
=0;i
<count
;i
++)
1861 /* close the read-end of the pipe */
1864 /* Append NULL to the array */
1866 addresses
= (char **)lxc_append_null_to_array((void **)addresses
, count
);
1871 WRAP_API_3(char **, lxcapi_get_ips
, const char *, const char *, int)
1873 static int do_lxcapi_get_config_item(struct lxc_container
*c
, const char *key
, char *retv
, int inlen
)
1877 if (!c
|| !c
->lxc_conf
)
1879 if (container_mem_lock(c
))
1881 ret
= lxc_get_config_item(c
->lxc_conf
, key
, retv
, inlen
);
1882 container_mem_unlock(c
);
1886 WRAP_API_3(int, lxcapi_get_config_item
, const char *, char *, int)
1888 static char* do_lxcapi_get_running_config_item(struct lxc_container
*c
, const char *key
)
1892 if (!c
|| !c
->lxc_conf
)
1894 if (container_mem_lock(c
))
1896 ret
= lxc_cmd_get_config_item(c
->name
, key
, do_lxcapi_get_config_path(c
));
1897 container_mem_unlock(c
);
1901 WRAP_API_1(char *, lxcapi_get_running_config_item
, const char *)
1903 static int do_lxcapi_get_keys(struct lxc_container
*c
, const char *key
, char *retv
, int inlen
)
1906 return lxc_listconfigs(retv
, inlen
);
1908 * Support 'lxc.network.<idx>', i.e. 'lxc.network.0'
1909 * This is an intelligent result to show which keys are valid given
1910 * the type of nic it is
1912 if (!c
|| !c
->lxc_conf
)
1914 if (container_mem_lock(c
))
1917 if (strncmp(key
, "lxc.network.", 12) == 0)
1918 ret
= lxc_list_nicconfigs(c
->lxc_conf
, key
, retv
, inlen
);
1919 container_mem_unlock(c
);
1923 WRAP_API_3(int, lxcapi_get_keys
, const char *, char *, int)
1925 static bool do_lxcapi_save_config(struct lxc_container
*c
, const char *alt_file
)
1928 bool ret
= false, need_disklock
= false;
1932 alt_file
= c
->configfile
;
1934 return false; // should we write to stdout if no file is specified?
1936 // If we haven't yet loaded a config, load the stock config
1938 if (!do_lxcapi_load_config(c
, lxc_global_config_value("lxc.default_config"))) {
1939 ERROR("Error loading default configuration file %s while saving %s", lxc_global_config_value("lxc.default_config"), c
->name
);
1944 if (!create_container_dir(c
))
1948 * If we're writing to the container's config file, take the
1949 * disk lock. Otherwise just take the memlock to protect the
1950 * struct lxc_container while we're traversing it.
1952 if (strcmp(c
->configfile
, alt_file
) == 0)
1953 need_disklock
= true;
1956 lret
= container_disk_lock(c
);
1958 lret
= container_mem_lock(c
);
1963 fout
= fopen(alt_file
, "w");
1966 write_config(fout
, c
->lxc_conf
);
1972 container_disk_unlock(c
);
1974 container_mem_unlock(c
);
1978 WRAP_API_1(bool, lxcapi_save_config
, const char *)
1980 static bool mod_rdep(struct lxc_container
*c
, bool inc
)
1982 char path
[MAXPATHLEN
];
1987 if (container_disk_lock(c
))
1989 ret
= snprintf(path
, MAXPATHLEN
, "%s/%s/lxc_snapshots", c
->config_path
,
1991 if (ret
< 0 || ret
> MAXPATHLEN
)
1993 f
= fopen(path
, "r");
1995 ret
= fscanf(f
, "%d", &v
);
1998 ERROR("Corrupted file %s", path
);
2003 f
= fopen(path
, "w");
2006 if (fprintf(f
, "%d\n", v
) < 0) {
2007 ERROR("Error writing new snapshots value");
2013 SYSERROR("Error writing to or closing snapshots file");
2020 container_disk_unlock(c
);
2024 static void strip_newline(char *p
)
2026 size_t len
= strlen(p
);
2029 if (p
[len
-1] == '\n')
2033 static void mod_all_rdeps(struct lxc_container
*c
, bool inc
)
2035 struct lxc_container
*p
;
2036 char *lxcpath
= NULL
, *lxcname
= NULL
, path
[MAXPATHLEN
];
2037 size_t pathlen
= 0, namelen
= 0;
2041 ret
= snprintf(path
, MAXPATHLEN
, "%s/%s/lxc_rdepends",
2042 c
->config_path
, c
->name
);
2043 if (ret
< 0 || ret
>= MAXPATHLEN
) {
2044 ERROR("Path name too long");
2047 f
= fopen(path
, "r");
2050 while (getline(&lxcpath
, &pathlen
, f
) != -1) {
2051 if (getline(&lxcname
, &namelen
, f
) == -1) {
2052 ERROR("badly formatted file %s", path
);
2055 strip_newline(lxcpath
);
2056 strip_newline(lxcname
);
2057 if ((p
= lxc_container_new(lxcname
, lxcpath
)) == NULL
) {
2058 ERROR("Unable to find dependent container %s:%s",
2062 if (!mod_rdep(p
, inc
))
2063 ERROR("Failed to increase numsnapshots for %s:%s",
2065 lxc_container_put(p
);
2073 static bool has_fs_snapshots(struct lxc_container
*c
)
2075 char path
[MAXPATHLEN
];
2080 ret
= snprintf(path
, MAXPATHLEN
, "%s/%s/lxc_snapshots", c
->config_path
,
2082 if (ret
< 0 || ret
> MAXPATHLEN
)
2084 f
= fopen(path
, "r");
2087 ret
= fscanf(f
, "%d", &v
);
2097 static bool has_snapshots(struct lxc_container
*c
)
2099 char path
[MAXPATHLEN
];
2100 struct dirent dirent
, *direntp
;
2104 if (!get_snappath_dir(c
, path
))
2106 dir
= opendir(path
);
2109 while (!readdir_r(dir
, &dirent
, &direntp
)) {
2113 if (!strcmp(direntp
->d_name
, "."))
2116 if (!strcmp(direntp
->d_name
, ".."))
2125 static int lxc_rmdir_onedev_wrapper(void *data
)
2127 char *arg
= (char *) data
;
2128 return lxc_rmdir_onedev(arg
, "snaps");
2131 static int do_bdev_destroy(struct lxc_conf
*conf
)
2136 r
= bdev_init(conf
, conf
->rootfs
.path
, conf
->rootfs
.mount
, NULL
);
2140 if (r
->ops
->destroy(r
) < 0)
2146 static int bdev_destroy_wrapper(void *data
)
2148 struct lxc_conf
*conf
= data
;
2150 if (setgid(0) < 0) {
2151 ERROR("Failed to setgid to 0");
2154 if (setgroups(0, NULL
) < 0)
2155 WARN("Failed to clear groups");
2156 if (setuid(0) < 0) {
2157 ERROR("Failed to setuid to 0");
2160 return do_bdev_destroy(conf
);
2163 static bool container_destroy(struct lxc_container
*c
)
2168 if (!c
|| !do_lxcapi_is_defined(c
))
2171 if (container_disk_lock(c
))
2174 if (!is_stopped(c
)) {
2175 // we should queue some sort of error - in c->error_string?
2176 ERROR("container %s is not stopped", c
->name
);
2180 if (c
->lxc_conf
&& c
->lxc_conf
->rootfs
.path
&& c
->lxc_conf
->rootfs
.mount
) {
2182 ret
= userns_exec_1(c
->lxc_conf
, bdev_destroy_wrapper
, c
->lxc_conf
);
2184 ret
= do_bdev_destroy(c
->lxc_conf
);
2186 ERROR("Error destroying rootfs for %s", c
->name
);
2191 mod_all_rdeps(c
, false);
2193 const char *p1
= do_lxcapi_get_config_path(c
);
2194 char *path
= alloca(strlen(p1
) + strlen(c
->name
) + 2);
2195 sprintf(path
, "%s/%s", p1
, c
->name
);
2197 ret
= userns_exec_1(c
->lxc_conf
, lxc_rmdir_onedev_wrapper
, path
);
2199 ret
= lxc_rmdir_onedev(path
, "snaps");
2201 ERROR("Error destroying container directory for %s", c
->name
);
2207 container_disk_unlock(c
);
2211 static bool do_lxcapi_destroy(struct lxc_container
*c
)
2213 if (!c
|| !lxcapi_is_defined(c
))
2215 if (has_snapshots(c
)) {
2216 ERROR("Container %s has snapshots; not removing", c
->name
);
2220 if (has_fs_snapshots(c
)) {
2221 ERROR("container %s has snapshots on its rootfs", c
->name
);
2225 return container_destroy(c
);
2228 WRAP_API(bool, lxcapi_destroy
)
2230 static bool do_lxcapi_destroy_with_snapshots(struct lxc_container
*c
)
2232 if (!c
|| !lxcapi_is_defined(c
))
2234 if (!lxcapi_snapshot_destroy_all(c
)) {
2235 ERROR("Error deleting all snapshots");
2238 return lxcapi_destroy(c
);
2241 WRAP_API(bool, lxcapi_destroy_with_snapshots
)
2243 static bool set_config_item_locked(struct lxc_container
*c
, const char *key
, const char *v
)
2245 struct lxc_config_t
*config
;
2248 c
->lxc_conf
= lxc_conf_init();
2251 config
= lxc_getconfig(key
);
2254 if (config
->cb(key
, v
, c
->lxc_conf
) != 0)
2256 return do_append_unexp_config_line(c
->lxc_conf
, key
, v
);
2259 static bool do_lxcapi_set_config_item(struct lxc_container
*c
, const char *key
, const char *v
)
2266 if (container_mem_lock(c
))
2269 b
= set_config_item_locked(c
, key
, v
);
2271 container_mem_unlock(c
);
2275 WRAP_API_2(bool, lxcapi_set_config_item
, const char *, const char *)
2277 static char *lxcapi_config_file_name(struct lxc_container
*c
)
2279 if (!c
|| !c
->configfile
)
2281 return strdup(c
->configfile
);
2284 static const char *lxcapi_get_config_path(struct lxc_container
*c
)
2286 if (!c
|| !c
->config_path
)
2288 return (const char *)(c
->config_path
);
2293 * Just recalculate the c->configfile based on the
2294 * c->config_path, which must be set.
2295 * The lxc_container must be locked or not yet public.
2297 static bool set_config_filename(struct lxc_container
*c
)
2302 if (!c
->config_path
)
2305 /* $lxc_path + "/" + c->name + "/" + "config" + '\0' */
2306 len
= strlen(c
->config_path
) + strlen(c
->name
) + strlen("config") + 3;
2307 newpath
= malloc(len
);
2311 ret
= snprintf(newpath
, len
, "%s/%s/config", c
->config_path
, c
->name
);
2312 if (ret
< 0 || ret
>= len
) {
2313 fprintf(stderr
, "Error printing out config file name\n");
2318 free(c
->configfile
);
2319 c
->configfile
= newpath
;
2324 static bool do_lxcapi_set_config_path(struct lxc_container
*c
, const char *path
)
2328 char *oldpath
= NULL
;
2333 if (container_mem_lock(c
))
2338 ERROR("Out of memory setting new lxc path");
2344 oldpath
= c
->config_path
;
2347 /* Since we've changed the config path, we have to change the
2348 * config file name too */
2349 if (!set_config_filename(c
)) {
2350 ERROR("Out of memory setting new config filename");
2352 free(c
->config_path
);
2353 c
->config_path
= oldpath
;
2358 container_mem_unlock(c
);
2362 WRAP_API_1(bool, lxcapi_set_config_path
, const char *)
2364 static bool do_lxcapi_set_cgroup_item(struct lxc_container
*c
, const char *subsys
, const char *value
)
2374 if (container_disk_lock(c
))
2377 ret
= lxc_cgroup_set(subsys
, value
, c
->name
, c
->config_path
);
2379 container_disk_unlock(c
);
2383 WRAP_API_2(bool, lxcapi_set_cgroup_item
, const char *, const char *)
2385 static int do_lxcapi_get_cgroup_item(struct lxc_container
*c
, const char *subsys
, char *retv
, int inlen
)
2395 if (container_disk_lock(c
))
2398 ret
= lxc_cgroup_get(subsys
, retv
, inlen
, c
->name
, c
->config_path
);
2400 container_disk_unlock(c
);
2404 WRAP_API_3(int, lxcapi_get_cgroup_item
, const char *, char *, int)
2406 const char *lxc_get_global_config_item(const char *key
)
2408 return lxc_global_config_value(key
);
2411 const char *lxc_get_version(void)
2416 static int copy_file(const char *old
, const char *new)
2423 if (file_exists(new)) {
2424 ERROR("copy destination %s exists", new);
2427 ret
= stat(old
, &sbuf
);
2429 INFO("Error stat'ing %s", old
);
2433 in
= open(old
, O_RDONLY
);
2435 SYSERROR("Error opening original file %s", old
);
2438 out
= open(new, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
2440 SYSERROR("Error opening new file %s", new);
2446 len
= read(in
, buf
, 8096);
2448 SYSERROR("Error reading old file %s", old
);
2453 ret
= write(out
, buf
, len
);
2454 if (ret
< len
) { // should we retry?
2455 SYSERROR("Error: write to new file %s was interrupted", new);
2462 // we set mode, but not owner/group
2463 ret
= chmod(new, sbuf
.st_mode
);
2465 SYSERROR("Error setting mode on %s", new);
2477 static int copyhooks(struct lxc_container
*oldc
, struct lxc_container
*c
)
2480 struct lxc_list
*it
;
2483 len
= strlen(oldc
->config_path
) + strlen(oldc
->name
) + 3;
2484 cpath
= alloca(len
);
2485 ret
= snprintf(cpath
, len
, "%s/%s/", oldc
->config_path
, oldc
->name
);
2486 if (ret
< 0 || ret
>= len
)
2489 for (i
=0; i
<NUM_LXC_HOOKS
; i
++) {
2490 lxc_list_for_each(it
, &c
->lxc_conf
->hooks
[i
]) {
2491 char *hookname
= it
->elem
;
2492 char *fname
= strrchr(hookname
, '/');
2493 char tmppath
[MAXPATHLEN
];
2494 if (!fname
) // relative path - we don't support, but maybe we should
2496 if (strncmp(hookname
, cpath
, len
- 1) != 0) {
2497 // this hook is public - ignore
2500 // copy the script, and change the entry in confile
2501 ret
= snprintf(tmppath
, MAXPATHLEN
, "%s/%s/%s",
2502 c
->config_path
, c
->name
, fname
+1);
2503 if (ret
< 0 || ret
>= MAXPATHLEN
)
2505 ret
= copy_file(it
->elem
, tmppath
);
2509 it
->elem
= strdup(tmppath
);
2511 ERROR("out of memory copying hook path");
2517 if (!clone_update_unexp_hooks(c
->lxc_conf
, oldc
->config_path
,
2518 c
->config_path
, oldc
->name
, c
->name
)) {
2519 ERROR("Error saving new hooks in clone");
2522 do_lxcapi_save_config(c
, NULL
);
2527 static int copy_fstab(struct lxc_container
*oldc
, struct lxc_container
*c
)
2529 char newpath
[MAXPATHLEN
];
2530 char *oldpath
= oldc
->lxc_conf
->fstab
;
2536 clear_unexp_config_line(c
->lxc_conf
, "lxc.mount", false);
2538 char *p
= strrchr(oldpath
, '/');
2541 ret
= snprintf(newpath
, MAXPATHLEN
, "%s/%s%s",
2542 c
->config_path
, c
->name
, p
);
2543 if (ret
< 0 || ret
>= MAXPATHLEN
) {
2544 ERROR("error printing new path for %s", oldpath
);
2547 if (file_exists(newpath
)) {
2548 ERROR("error: fstab file %s exists", newpath
);
2552 if (copy_file(oldpath
, newpath
) < 0) {
2553 ERROR("error: copying %s to %s", oldpath
, newpath
);
2556 free(c
->lxc_conf
->fstab
);
2557 c
->lxc_conf
->fstab
= strdup(newpath
);
2558 if (!c
->lxc_conf
->fstab
) {
2559 ERROR("error: allocating pathname");
2562 if (!do_append_unexp_config_line(c
->lxc_conf
, "lxc.mount", newpath
)) {
2563 ERROR("error saving new lxctab");
2570 static void copy_rdepends(struct lxc_container
*c
, struct lxc_container
*c0
)
2572 char path0
[MAXPATHLEN
], path1
[MAXPATHLEN
];
2575 ret
= snprintf(path0
, MAXPATHLEN
, "%s/%s/lxc_rdepends", c0
->config_path
,
2577 if (ret
< 0 || ret
>= MAXPATHLEN
) {
2578 WARN("Error copying reverse dependencies");
2581 ret
= snprintf(path1
, MAXPATHLEN
, "%s/%s/lxc_rdepends", c
->config_path
,
2583 if (ret
< 0 || ret
>= MAXPATHLEN
) {
2584 WARN("Error copying reverse dependencies");
2587 if (copy_file(path0
, path1
) < 0) {
2588 INFO("Error copying reverse dependencies");
2593 static bool add_rdepends(struct lxc_container
*c
, struct lxc_container
*c0
)
2596 char path
[MAXPATHLEN
];
2600 ret
= snprintf(path
, MAXPATHLEN
, "%s/%s/lxc_rdepends", c
->config_path
,
2602 if (ret
< 0 || ret
>= MAXPATHLEN
)
2604 f
= fopen(path
, "a");
2608 // if anything goes wrong, just return an error
2609 if (fprintf(f
, "%s\n%s\n", c0
->config_path
, c0
->name
) < 0)
2616 static int copy_storage(struct lxc_container
*c0
, struct lxc_container
*c
,
2617 const char *newtype
, int flags
, const char *bdevdata
, uint64_t newsize
)
2622 bdev
= bdev_copy(c0
, c
->name
, c
->config_path
, newtype
, flags
,
2623 bdevdata
, newsize
, &need_rdep
);
2625 ERROR("Error copying storage");
2628 free(c
->lxc_conf
->rootfs
.path
);
2629 c
->lxc_conf
->rootfs
.path
= strdup(bdev
->src
);
2631 if (!c
->lxc_conf
->rootfs
.path
) {
2632 ERROR("Out of memory while setting storage path");
2635 // We will simply append a new lxc.rootfs entry to the unexpanded config
2636 clear_unexp_config_line(c
->lxc_conf
, "lxc.rootfs", false);
2637 if (!do_append_unexp_config_line(c
->lxc_conf
, "lxc.rootfs", c
->lxc_conf
->rootfs
.path
)) {
2638 ERROR("Error saving new rootfs to cloend config");
2641 if (flags
& LXC_CLONE_SNAPSHOT
)
2642 copy_rdepends(c
, c0
);
2644 if (!add_rdepends(c
, c0
))
2645 WARN("Error adding reverse dependency from %s to %s",
2649 mod_all_rdeps(c
, true);
2654 struct clone_update_data
{
2655 struct lxc_container
*c0
;
2656 struct lxc_container
*c1
;
2661 static int clone_update_rootfs(struct clone_update_data
*data
)
2663 struct lxc_container
*c0
= data
->c0
;
2664 struct lxc_container
*c
= data
->c1
;
2665 int flags
= data
->flags
;
2666 char **hookargs
= data
->hookargs
;
2668 char path
[MAXPATHLEN
];
2671 struct lxc_conf
*conf
= c
->lxc_conf
;
2673 /* update hostname in rootfs */
2674 /* we're going to mount, so run in a clean namespace to simplify cleanup */
2676 if (setgid(0) < 0) {
2677 ERROR("Failed to setgid to 0");
2680 if (setuid(0) < 0) {
2681 ERROR("Failed to setuid to 0");
2684 if (setgroups(0, NULL
) < 0)
2685 WARN("Failed to clear groups");
2687 if (unshare(CLONE_NEWNS
) < 0)
2689 bdev
= bdev_init(c
->lxc_conf
, c
->lxc_conf
->rootfs
.path
, c
->lxc_conf
->rootfs
.mount
, NULL
);
2692 if (strcmp(bdev
->type
, "dir") != 0) {
2693 if (unshare(CLONE_NEWNS
) < 0) {
2694 ERROR("error unsharing mounts");
2698 if (detect_shared_rootfs()) {
2699 if (mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
)) {
2700 SYSERROR("Failed to make / rslave");
2701 ERROR("Continuing...");
2704 if (bdev
->ops
->mount(bdev
) < 0) {
2708 } else { // TODO come up with a better way
2710 bdev
->dest
= strdup(bdev
->src
);
2713 if (!lxc_list_empty(&conf
->hooks
[LXCHOOK_CLONE
])) {
2714 /* Start of environment variable setup for hooks */
2715 if (setenv("LXC_SRC_NAME", c0
->name
, 1)) {
2716 SYSERROR("failed to set environment variable for source container name");
2718 if (setenv("LXC_NAME", c
->name
, 1)) {
2719 SYSERROR("failed to set environment variable for container name");
2721 if (setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1)) {
2722 SYSERROR("failed to set environment variable for config path");
2724 if (setenv("LXC_ROOTFS_MOUNT", bdev
->dest
, 1)) {
2725 SYSERROR("failed to set environment variable for rootfs mount");
2727 if (setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1)) {
2728 SYSERROR("failed to set environment variable for rootfs mount");
2731 if (run_lxc_hooks(c
->name
, "clone", conf
, c
->get_config_path(c
), hookargs
)) {
2732 ERROR("Error executing clone hook for %s", c
->name
);
2738 if (!(flags
& LXC_CLONE_KEEPNAME
)) {
2739 ret
= snprintf(path
, MAXPATHLEN
, "%s/etc/hostname", bdev
->dest
);
2742 if (ret
< 0 || ret
>= MAXPATHLEN
)
2744 if (!file_exists(path
))
2746 if (!(fout
= fopen(path
, "w"))) {
2747 SYSERROR("unable to open %s: ignoring", path
);
2750 if (fprintf(fout
, "%s", c
->name
) < 0) {
2754 if (fclose(fout
) < 0)
2763 static int clone_update_rootfs_wrapper(void *data
)
2765 struct clone_update_data
*arg
= (struct clone_update_data
*) data
;
2766 return clone_update_rootfs(arg
);
2770 * We want to support:
2771 sudo lxc-clone -o o1 -n n1 -s -L|-fssize fssize -v|--vgname vgname \
2772 -p|--lvprefix lvprefix -t|--fstype fstype -B backingstore
2774 -s [ implies overlayfs]
2778 only rootfs gets converted (copied/snapshotted) on clone.
2781 static int create_file_dirname(char *path
, struct lxc_conf
*conf
)
2783 char *p
= strrchr(path
, '/');
2789 ret
= do_create_container_dir(path
, conf
);
2794 static struct lxc_container
*do_lxcapi_clone(struct lxc_container
*c
, const char *newname
,
2795 const char *lxcpath
, int flags
,
2796 const char *bdevtype
, const char *bdevdata
, uint64_t newsize
,
2799 struct lxc_container
*c2
= NULL
;
2800 char newpath
[MAXPATHLEN
];
2801 int ret
, storage_copied
= 0;
2802 char *origroot
= NULL
;
2803 struct clone_update_data data
;
2807 if (!c
|| !do_lxcapi_is_defined(c
))
2810 if (container_mem_lock(c
))
2813 if (!is_stopped(c
)) {
2814 ERROR("error: Original container (%s) is running", c
->name
);
2818 // Make sure the container doesn't yet exist.
2822 lxcpath
= do_lxcapi_get_config_path(c
);
2823 ret
= snprintf(newpath
, MAXPATHLEN
, "%s/%s/config", lxcpath
, newname
);
2824 if (ret
< 0 || ret
>= MAXPATHLEN
) {
2825 SYSERROR("clone: failed making config pathname");
2828 if (file_exists(newpath
)) {
2829 ERROR("error: clone: %s exists", newpath
);
2833 ret
= create_file_dirname(newpath
, c
->lxc_conf
);
2834 if (ret
< 0 && errno
!= EEXIST
) {
2835 ERROR("Error creating container dir for %s", newpath
);
2839 // copy the configuration, tweak it as needed,
2840 if (c
->lxc_conf
->rootfs
.path
) {
2841 origroot
= c
->lxc_conf
->rootfs
.path
;
2842 c
->lxc_conf
->rootfs
.path
= NULL
;
2844 fout
= fopen(newpath
, "w");
2846 SYSERROR("open %s", newpath
);
2849 write_config(fout
, c
->lxc_conf
);
2851 c
->lxc_conf
->rootfs
.path
= origroot
;
2853 sprintf(newpath
, "%s/%s/rootfs", lxcpath
, newname
);
2854 if (mkdir(newpath
, 0755) < 0) {
2855 SYSERROR("error creating %s", newpath
);
2860 if (chown_mapped_root(newpath
, c
->lxc_conf
) < 0) {
2861 ERROR("Error chowning %s to container root", newpath
);
2866 c2
= lxc_container_new(newname
, lxcpath
);
2868 ERROR("clone: failed to create new container (%s %s)", newname
,
2873 // copy/snapshot rootfs's
2874 ret
= copy_storage(c
, c2
, bdevtype
, flags
, bdevdata
, newsize
);
2878 clear_unexp_config_line(c2
->lxc_conf
, "lxc.utsname", false);
2881 if (!set_config_item_locked(c2
, "lxc.utsname", newname
)) {
2882 ERROR("Error setting new hostname");
2887 ret
= copyhooks(c
, c2
);
2889 ERROR("error copying hooks");
2893 if (copy_fstab(c
, c2
) < 0) {
2894 ERROR("error copying fstab");
2899 if (!(flags
& LXC_CLONE_KEEPMACADDR
)) {
2900 if (!network_new_hwaddrs(c2
->lxc_conf
)) {
2901 ERROR("Error updating mac addresses");
2906 // We've now successfully created c2's storage, so clear it out if we
2910 if (!c2
->save_config(c2
, NULL
))
2913 if ((pid
= fork()) < 0) {
2918 ret
= wait_for_pid(pid
);
2921 container_mem_unlock(c
);
2927 data
.hookargs
= hookargs
;
2929 ret
= userns_exec_1(c
->lxc_conf
, clone_update_rootfs_wrapper
,
2932 ret
= clone_update_rootfs(&data
);
2936 container_mem_unlock(c
);
2940 container_mem_unlock(c
);
2942 if (!storage_copied
)
2943 c2
->lxc_conf
->rootfs
.path
= NULL
;
2945 lxc_container_put(c2
);
2951 static struct lxc_container
*lxcapi_clone(struct lxc_container
*c
, const char *newname
,
2952 const char *lxcpath
, int flags
,
2953 const char *bdevtype
, const char *bdevdata
, uint64_t newsize
,
2956 struct lxc_container
* ret
;
2957 struct lxc_conf
*old
= current_config
;
2958 current_config
= c
? c
->lxc_conf
: NULL
;
2959 ret
= do_lxcapi_clone(c
, newname
, lxcpath
, flags
, bdevtype
, bdevdata
, newsize
, hookargs
);
2960 current_config
= old
;
2964 static bool do_lxcapi_rename(struct lxc_container
*c
, const char *newname
)
2967 struct lxc_container
*newc
;
2969 if (!c
|| !c
->name
|| !c
->config_path
|| !c
->lxc_conf
)
2972 if (has_fs_snapshots(c
) || has_snapshots(c
)) {
2973 ERROR("Renaming a container with snapshots is not supported");
2976 bdev
= bdev_init(c
->lxc_conf
, c
->lxc_conf
->rootfs
.path
, c
->lxc_conf
->rootfs
.mount
, NULL
);
2978 ERROR("Failed to find original backing store type");
2982 newc
= lxcapi_clone(c
, newname
, c
->config_path
, LXC_CLONE_KEEPMACADDR
, NULL
, bdev
->type
, 0, NULL
);
2985 lxc_container_put(newc
);
2989 if (newc
&& lxcapi_is_defined(newc
))
2990 lxc_container_put(newc
);
2992 if (!container_destroy(c
)) {
2993 ERROR("Could not destroy existing container %s", c
->name
);
2999 WRAP_API_1(bool, lxcapi_rename
, const char *)
3001 static int lxcapi_attach(struct lxc_container
*c
, lxc_attach_exec_t exec_function
, void *exec_payload
, lxc_attach_options_t
*options
, pid_t
*attached_process
)
3003 struct lxc_conf
*old
= current_config
;
3009 current_config
= c
->lxc_conf
;
3011 ret
= lxc_attach(c
->name
, c
->config_path
, exec_function
, exec_payload
, options
, attached_process
);
3012 current_config
= old
;
3016 static int do_lxcapi_attach_run_wait(struct lxc_container
*c
, lxc_attach_options_t
*options
, const char *program
, const char * const argv
[])
3018 lxc_attach_command_t command
;
3025 command
.program
= (char*)program
;
3026 command
.argv
= (char**)argv
;
3027 r
= lxc_attach(c
->name
, c
->config_path
, lxc_attach_run_command
, &command
, options
, &pid
);
3032 return lxc_wait_for_pid_status(pid
);
3035 static int lxcapi_attach_run_wait(struct lxc_container
*c
, lxc_attach_options_t
*options
, const char *program
, const char * const argv
[])
3038 struct lxc_conf
*old
= current_config
;
3039 current_config
= c
? c
->lxc_conf
: NULL
;
3040 ret
= do_lxcapi_attach_run_wait(c
, options
, program
, argv
);
3041 current_config
= old
;
3045 static int get_next_index(const char *lxcpath
, char *cname
)
3051 fname
= alloca(strlen(lxcpath
) + 20);
3053 sprintf(fname
, "%s/snap%d", lxcpath
, i
);
3054 ret
= stat(fname
, &sb
);
3061 static bool get_snappath_dir(struct lxc_container
*c
, char *snappath
)
3065 * If the old style snapshot path exists, use it
3066 * /var/lib/lxc -> /var/lib/lxcsnaps
3068 ret
= snprintf(snappath
, MAXPATHLEN
, "%ssnaps", c
->config_path
);
3069 if (ret
< 0 || ret
>= MAXPATHLEN
)
3071 if (dir_exists(snappath
)) {
3072 ret
= snprintf(snappath
, MAXPATHLEN
, "%ssnaps/%s", c
->config_path
, c
->name
);
3073 if (ret
< 0 || ret
>= MAXPATHLEN
)
3079 * Use the new style path
3080 * /var/lib/lxc -> /var/lib/lxc + c->name + /snaps + \0
3082 ret
= snprintf(snappath
, MAXPATHLEN
, "%s/%s/snaps", c
->config_path
, c
->name
);
3083 if (ret
< 0 || ret
>= MAXPATHLEN
)
3088 static int do_lxcapi_snapshot(struct lxc_container
*c
, const char *commentfile
)
3091 struct lxc_container
*c2
;
3092 char snappath
[MAXPATHLEN
], newname
[20];
3094 if (!c
|| !lxcapi_is_defined(c
))
3097 if (!bdev_can_backup(c
->lxc_conf
)) {
3098 ERROR("%s's backing store cannot be backed up.", c
->name
);
3099 ERROR("Your container must use another backing store type.");
3103 if (!get_snappath_dir(c
, snappath
))
3106 i
= get_next_index(snappath
, c
->name
);
3108 if (mkdir_p(snappath
, 0755) < 0) {
3109 ERROR("Failed to create snapshot directory %s", snappath
);
3113 ret
= snprintf(newname
, 20, "snap%d", i
);
3114 if (ret
< 0 || ret
>= 20)
3118 * We pass LXC_CLONE_SNAPSHOT to make sure that a rdepends file entry is
3119 * created in the original container
3121 flags
= LXC_CLONE_SNAPSHOT
| LXC_CLONE_KEEPMACADDR
| LXC_CLONE_KEEPNAME
|
3122 LXC_CLONE_KEEPBDEVTYPE
| LXC_CLONE_MAYBE_SNAPSHOT
;
3123 if (bdev_is_dir(c
->lxc_conf
, c
->lxc_conf
->rootfs
.path
)) {
3124 ERROR("Snapshot of directory-backed container requested.");
3125 ERROR("Making a copy-clone. If you do want snapshots, then");
3126 ERROR("please create an aufs or overlayfs clone first, snapshot that");
3127 ERROR("and keep the original container pristine.");
3128 flags
&= ~LXC_CLONE_SNAPSHOT
| LXC_CLONE_MAYBE_SNAPSHOT
;
3130 c2
= do_lxcapi_clone(c
, newname
, snappath
, flags
, NULL
, NULL
, 0, NULL
);
3132 ERROR("clone of %s:%s failed", c
->config_path
, c
->name
);
3136 lxc_container_put(c2
);
3138 // Now write down the creation time
3145 tm_info
= localtime(&timer
);
3147 strftime(buffer
, 25, "%Y:%m:%d %H:%M:%S", tm_info
);
3149 char *dfnam
= alloca(strlen(snappath
) + strlen(newname
) + 5);
3150 sprintf(dfnam
, "%s/%s/ts", snappath
, newname
);
3151 f
= fopen(dfnam
, "w");
3153 ERROR("Failed to open %s", dfnam
);
3156 if (fprintf(f
, "%s", buffer
) < 0) {
3157 SYSERROR("Writing timestamp");
3163 SYSERROR("Writing timestamp");
3168 // $p / $name / comment \0
3169 int len
= strlen(snappath
) + strlen(newname
) + 10;
3170 char *path
= alloca(len
);
3171 sprintf(path
, "%s/%s/comment", snappath
, newname
);
3172 return copy_file(commentfile
, path
) < 0 ? -1 : i
;
3178 WRAP_API_1(int, lxcapi_snapshot
, const char *)
3180 static void lxcsnap_free(struct lxc_snapshot
*s
)
3183 free(s
->comment_pathname
);
3188 static char *get_snapcomment_path(char* snappath
, char *name
)
3190 // $snappath/$name/comment
3191 int ret
, len
= strlen(snappath
) + strlen(name
) + 10;
3192 char *s
= malloc(len
);
3195 ret
= snprintf(s
, len
, "%s/%s/comment", snappath
, name
);
3196 if (ret
< 0 || ret
>= len
) {
3204 static char *get_timestamp(char* snappath
, char *name
)
3206 char path
[MAXPATHLEN
], *s
= NULL
;
3210 ret
= snprintf(path
, MAXPATHLEN
, "%s/%s/ts", snappath
, name
);
3211 if (ret
< 0 || ret
>= MAXPATHLEN
)
3213 fin
= fopen(path
, "r");
3216 (void) fseek(fin
, 0, SEEK_END
);
3218 (void) fseek(fin
, 0, SEEK_SET
);
3223 if (fread(s
, 1, len
, fin
) != len
) {
3224 SYSERROR("reading timestamp");
3234 static int do_lxcapi_snapshot_list(struct lxc_container
*c
, struct lxc_snapshot
**ret_snaps
)
3236 char snappath
[MAXPATHLEN
], path2
[MAXPATHLEN
];
3238 struct dirent dirent
, *direntp
;
3239 struct lxc_snapshot
*snaps
=NULL
, *nsnaps
;
3242 if (!c
|| !lxcapi_is_defined(c
))
3245 if (!get_snappath_dir(c
, snappath
)) {
3246 ERROR("path name too long");
3249 dir
= opendir(snappath
);
3251 INFO("failed to open %s - assuming no snapshots", snappath
);
3255 while (!readdir_r(dir
, &dirent
, &direntp
)) {
3259 if (!strcmp(direntp
->d_name
, "."))
3262 if (!strcmp(direntp
->d_name
, ".."))
3265 ret
= snprintf(path2
, MAXPATHLEN
, "%s/%s/config", snappath
, direntp
->d_name
);
3266 if (ret
< 0 || ret
>= MAXPATHLEN
) {
3267 ERROR("pathname too long");
3270 if (!file_exists(path2
))
3272 nsnaps
= realloc(snaps
, (count
+ 1)*sizeof(*snaps
));
3274 SYSERROR("Out of memory");
3278 snaps
[count
].free
= lxcsnap_free
;
3279 snaps
[count
].name
= strdup(direntp
->d_name
);
3280 if (!snaps
[count
].name
)
3282 snaps
[count
].lxcpath
= strdup(snappath
);
3283 if (!snaps
[count
].lxcpath
) {
3284 free(snaps
[count
].name
);
3287 snaps
[count
].comment_pathname
= get_snapcomment_path(snappath
, direntp
->d_name
);
3288 snaps
[count
].timestamp
= get_timestamp(snappath
, direntp
->d_name
);
3293 WARN("failed to close directory");
3301 for (i
=0; i
<count
; i
++)
3302 lxcsnap_free(&snaps
[i
]);
3306 WARN("failed to close directory");
3310 WRAP_API_1(int, lxcapi_snapshot_list
, struct lxc_snapshot
**)
3312 static bool do_lxcapi_snapshot_restore(struct lxc_container
*c
, const char *snapname
, const char *newname
)
3314 char clonelxcpath
[MAXPATHLEN
];
3316 struct lxc_container
*snap
, *rest
;
3320 if (!c
|| !c
->name
|| !c
->config_path
)
3323 if (has_fs_snapshots(c
)) {
3324 ERROR("container rootfs has dependent snapshots");
3328 bdev
= bdev_init(c
->lxc_conf
, c
->lxc_conf
->rootfs
.path
, c
->lxc_conf
->rootfs
.mount
, NULL
);
3330 ERROR("Failed to find original backing store type");
3337 if (!get_snappath_dir(c
, clonelxcpath
)) {
3341 // how should we lock this?
3343 snap
= lxc_container_new(snapname
, clonelxcpath
);
3344 if (!snap
|| !lxcapi_is_defined(snap
)) {
3345 ERROR("Could not open snapshot %s", snapname
);
3346 if (snap
) lxc_container_put(snap
);
3351 if (strcmp(c
->name
, newname
) == 0) {
3352 if (!container_destroy(c
)) {
3353 ERROR("Could not destroy existing container %s", newname
);
3354 lxc_container_put(snap
);
3360 if (strcmp(bdev
->type
, "dir") != 0 && strcmp(bdev
->type
, "loop") != 0)
3361 flags
= LXC_CLONE_SNAPSHOT
| LXC_CLONE_MAYBE_SNAPSHOT
;
3362 rest
= lxcapi_clone(snap
, newname
, c
->config_path
, flags
,
3363 bdev
->type
, NULL
, 0, NULL
);
3365 if (rest
&& lxcapi_is_defined(rest
))
3368 lxc_container_put(rest
);
3369 lxc_container_put(snap
);
3373 WRAP_API_2(bool, lxcapi_snapshot_restore
, const char *, const char *)
3375 static bool do_snapshot_destroy(const char *snapname
, const char *clonelxcpath
)
3377 struct lxc_container
*snap
= NULL
;
3380 snap
= lxc_container_new(snapname
, clonelxcpath
);
3382 ERROR("Could not find snapshot %s", snapname
);
3386 if (!do_lxcapi_destroy(snap
)) {
3387 ERROR("Could not destroy snapshot %s", snapname
);
3394 lxc_container_put(snap
);
3398 static bool remove_all_snapshots(const char *path
)
3401 struct dirent dirent
, *direntp
;
3404 dir
= opendir(path
);
3406 SYSERROR("opendir on snapshot path %s", path
);
3409 while (!readdir_r(dir
, &dirent
, &direntp
)) {
3412 if (!strcmp(direntp
->d_name
, "."))
3414 if (!strcmp(direntp
->d_name
, ".."))
3416 if (!do_snapshot_destroy(direntp
->d_name
, path
)) {
3425 SYSERROR("Error removing directory %s", path
);
3430 static bool do_lxcapi_snapshot_destroy(struct lxc_container
*c
, const char *snapname
)
3432 char clonelxcpath
[MAXPATHLEN
];
3434 if (!c
|| !c
->name
|| !c
->config_path
|| !snapname
)
3437 if (!get_snappath_dir(c
, clonelxcpath
))
3440 return do_snapshot_destroy(snapname
, clonelxcpath
);
3443 WRAP_API_1(bool, lxcapi_snapshot_destroy
, const char *)
3445 static bool do_lxcapi_snapshot_destroy_all(struct lxc_container
*c
)
3447 char clonelxcpath
[MAXPATHLEN
];
3449 if (!c
|| !c
->name
|| !c
->config_path
)
3452 if (!get_snappath_dir(c
, clonelxcpath
))
3455 return remove_all_snapshots(clonelxcpath
);
3458 WRAP_API(bool, lxcapi_snapshot_destroy_all
)
3460 static bool do_lxcapi_may_control(struct lxc_container
*c
)
3462 return lxc_try_cmd(c
->name
, c
->config_path
) == 0;
3465 WRAP_API(bool, lxcapi_may_control
)
3467 static bool do_add_remove_node(pid_t init_pid
, const char *path
, bool add
,
3470 char chrootpath
[MAXPATHLEN
];
3471 char *directory_path
= NULL
;
3475 if ((pid
= fork()) < 0) {
3476 SYSERROR("failed to fork a child helper");
3480 if (wait_for_pid(pid
) != 0) {
3481 ERROR("Failed to create note in guest");
3487 /* prepare the path */
3488 ret
= snprintf(chrootpath
, MAXPATHLEN
, "/proc/%d/root", init_pid
);
3489 if (ret
< 0 || ret
>= MAXPATHLEN
)
3492 if (chroot(chrootpath
) < 0)
3496 /* remove path if it exists */
3497 if(faccessat(AT_FDCWD
, path
, F_OK
, AT_SYMLINK_NOFOLLOW
) == 0) {
3498 if (unlink(path
) < 0) {
3499 ERROR("unlink failed");
3506 /* create any missing directories */
3507 directory_path
= dirname(strdup(path
));
3508 if (mkdir_p(directory_path
, 0755) < 0 && errno
!= EEXIST
) {
3509 ERROR("failed to create directory");
3513 /* create the device node */
3514 if (mknod(path
, st
->st_mode
, st
->st_rdev
) < 0) {
3515 ERROR("mknod failed");
3522 static bool add_remove_device_node(struct lxc_container
*c
, const char *src_path
, const char *dest_path
, bool add
)
3526 char value
[MAX_BUFFER
];
3529 /* make sure container is running */
3530 if (!do_lxcapi_is_running(c
)) {
3531 ERROR("container is not running");
3535 /* use src_path if dest_path is NULL otherwise use dest_path */
3536 p
= dest_path
? dest_path
: src_path
;
3538 /* make sure we can access p */
3539 if(access(p
, F_OK
) < 0 || stat(p
, &st
) < 0)
3542 /* continue if path is character device or block device */
3543 if (S_ISCHR(st
.st_mode
))
3544 ret
= snprintf(value
, MAX_BUFFER
, "c %d:%d rwm", major(st
.st_rdev
), minor(st
.st_rdev
));
3545 else if (S_ISBLK(st
.st_mode
))
3546 ret
= snprintf(value
, MAX_BUFFER
, "b %d:%d rwm", major(st
.st_rdev
), minor(st
.st_rdev
));
3550 /* check snprintf return code */
3551 if (ret
< 0 || ret
>= MAX_BUFFER
)
3554 if (!do_add_remove_node(do_lxcapi_init_pid(c
), p
, add
, &st
))
3557 /* add or remove device to/from cgroup access list */
3559 if (!do_lxcapi_set_cgroup_item(c
, "devices.allow", value
)) {
3560 ERROR("set_cgroup_item failed while adding the device node");
3564 if (!do_lxcapi_set_cgroup_item(c
, "devices.deny", value
)) {
3565 ERROR("set_cgroup_item failed while removing the device node");
3573 static bool do_lxcapi_add_device_node(struct lxc_container
*c
, const char *src_path
, const char *dest_path
)
3576 ERROR(NOT_SUPPORTED_ERROR
, __FUNCTION__
);
3579 return add_remove_device_node(c
, src_path
, dest_path
, true);
3582 WRAP_API_2(bool, lxcapi_add_device_node
, const char *, const char *)
3584 static bool do_lxcapi_remove_device_node(struct lxc_container
*c
, const char *src_path
, const char *dest_path
)
3587 ERROR(NOT_SUPPORTED_ERROR
, __FUNCTION__
);
3590 return add_remove_device_node(c
, src_path
, dest_path
, false);
3593 WRAP_API_2(bool, lxcapi_remove_device_node
, const char *, const char *)
3595 static bool do_lxcapi_attach_interface(struct lxc_container
*c
, const char *ifname
,
3596 const char *dst_ifname
)
3600 ERROR(NOT_SUPPORTED_ERROR
, __FUNCTION__
);
3605 ERROR("No source interface name given");
3609 ret
= lxc_netdev_isup(ifname
);
3612 /* netdev of ifname is up. */
3613 ret
= lxc_netdev_down(ifname
);
3618 ret
= lxc_netdev_move_by_name(ifname
, do_lxcapi_init_pid(c
), dst_ifname
);
3628 WRAP_API_2(bool, lxcapi_attach_interface
, const char *, const char *)
3630 static bool do_lxcapi_detach_interface(struct lxc_container
*c
, const char *ifname
,
3631 const char *dst_ifname
)
3633 pid_t pid
, pid_outside
;
3636 ERROR(NOT_SUPPORTED_ERROR
, __FUNCTION__
);
3641 ERROR("No source interface name given");
3645 pid_outside
= getpid();
3648 ERROR("failed to fork task to get interfaces information");
3652 if (pid
== 0) { // child
3654 if (!enter_net_ns(c
)) {
3655 ERROR("failed to enter namespace");
3659 ret
= lxc_netdev_isup(ifname
);
3663 /* netdev of ifname is up. */
3665 ret
= lxc_netdev_down(ifname
);
3670 ret
= lxc_netdev_move_by_name(ifname
, pid_outside
, dst_ifname
);
3672 /* -EINVAL means there is no netdev named as ifanme. */
3673 if (ret
== -EINVAL
) {
3674 ERROR("No network device named as %s.", ifname
);
3679 if (wait_for_pid(pid
) != 0)
3685 WRAP_API_2(bool, lxcapi_detach_interface
, const char *, const char *)
3688 /* The type of criu invocation, one of "dump" or "restore" */
3691 /* The directory to pass to criu */
3694 /* The container to dump */
3695 struct lxc_container
*c
;
3697 /* Enable criu verbose mode? */
3700 /* dump: stop the container or not after dumping? */
3703 /* restore: the file to write the init process' pid into */
3705 const char *cgroup_path
;
3708 static void exec_criu(struct criu_opts
*opts
)
3710 char **argv
, log
[PATH_MAX
];
3711 int static_args
= 14, argc
= 0, i
, ret
;
3713 struct lxc_list
*it
;
3715 struct mntent mntent
;
3719 /* The command line always looks like:
3720 * criu $(action) --tcp-established --file-locks --link-remap --force-irmap \
3721 * --manage-cgroups action-script foo.sh -D $(directory) \
3722 * -o $(directory)/$(action).log
3723 * +1 for final NULL */
3725 if (strcmp(opts
->action
, "dump") == 0) {
3729 /* --leave-running */
3732 } else if (strcmp(opts
->action
, "restore") == 0) {
3733 /* --root $(lxc_mount_point) --restore-detached
3734 * --restore-sibling --pidfile $foo --cgroup-root $foo */
3743 ret
= snprintf(log
, PATH_MAX
, "%s/%s.log", opts
->directory
, opts
->action
);
3744 if (ret
< 0 || ret
>= PATH_MAX
) {
3745 ERROR("logfile name too long\n");
3749 // We need to tell criu where cgmanager's socket is bind mounted from
3750 // if it exists since it's external.
3751 if (cgroup_driver() == CGMANAGER
)
3754 argv
= malloc(static_args
* sizeof(*argv
));
3758 memset(argv
, 0, static_args
* sizeof(*argv
));
3760 #define DECLARE_ARG(arg) \
3762 if (arg == NULL) { \
3763 ERROR("Got NULL argument for criu"); \
3766 argv[argc++] = strdup(arg); \
3767 if (!argv[argc-1]) \
3771 argv
[argc
++] = on_path("criu", NULL
);
3772 if (!argv
[argc
-1]) {
3773 ERROR("Couldn't find criu binary\n");
3777 DECLARE_ARG(opts
->action
);
3778 DECLARE_ARG("--tcp-established");
3779 DECLARE_ARG("--file-locks");
3780 DECLARE_ARG("--link-remap");
3781 DECLARE_ARG("--force-irmap");
3782 DECLARE_ARG("--manage-cgroups");
3783 DECLARE_ARG("--action-script");
3784 DECLARE_ARG(DATADIR
"/lxc/lxc-restore-net");
3786 DECLARE_ARG(opts
->directory
);
3791 DECLARE_ARG("-vvvvvv");
3794 * Note: this macro is not intended to be called unless argc is equal
3795 * to the length of the array; there is nothing that keeps track of the
3796 * length of the array besides the location in the code that this is
3797 * called. (Yes this is bad, and we should fix it.)
3799 #define RESIZE_ARGS(additional) \
3802 if (additional < 0) { \
3803 ERROR("resizing by negative amount"); \
3805 } else if (additional == 0) \
3808 m = realloc(argv, (argc + additional + 1) * sizeof(*argv)); \
3814 if (strcmp(opts
->action
, "dump") == 0) {
3817 if (cgroup_driver() == CGMANAGER
) {
3818 DECLARE_ARG("--ext-mount-map");
3819 DECLARE_ARG("/sys/fs/cgroup/cgmanager:cgmanager");
3822 if (sprintf(pid
, "%d", lxcapi_init_pid(opts
->c
)) < 0)
3828 DECLARE_ARG("--leave-running");
3829 } else if (strcmp(opts
->action
, "restore") == 0) {
3831 if (cgroup_driver() == CGMANAGER
) {
3832 DECLARE_ARG("--ext-mount-map");
3833 DECLARE_ARG("cgmanager:/sys/fs/cgroup/cgmanager");
3836 DECLARE_ARG("--root");
3837 DECLARE_ARG(opts
->c
->lxc_conf
->rootfs
.mount
);
3838 DECLARE_ARG("--restore-detached");
3839 DECLARE_ARG("--restore-sibling");
3840 DECLARE_ARG("--pidfile");
3841 DECLARE_ARG(opts
->pidfile
);
3842 DECLARE_ARG("--cgroup-root");
3843 DECLARE_ARG(opts
->cgroup_path
);
3845 RESIZE_ARGS(lxc_list_len(&opts
->c
->lxc_conf
->network
) * 2);
3847 lxc_list_for_each(it
, &opts
->c
->lxc_conf
->network
) {
3848 char eth
[128], *veth
;
3849 struct lxc_netdev
*n
= it
->elem
;
3852 if (strlen(n
->name
) >= sizeof(eth
))
3854 strncpy(eth
, n
->name
, sizeof(eth
));
3856 sprintf(eth
, "eth%d", netnr
);
3858 veth
= n
->priv
.veth_attr
.pair
;
3860 ret
= snprintf(buf
, sizeof(buf
), "%s=%s", eth
, veth
);
3861 if (ret
< 0 || ret
>= sizeof(buf
))
3864 DECLARE_ARG("--veth-pair");
3869 // CRIU wants to know about any external bind mounts the
3871 mnts
= write_mount_file(&opts
->c
->lxc_conf
->mount_list
);
3875 RESIZE_ARGS(lxc_list_len(&opts
->c
->lxc_conf
->mount_list
) * 2);
3877 while (getmntent_r(mnts
, &mntent
, buf
, sizeof(buf
))) {
3878 char arg
[2048], *key
, *val
;
3881 if (strcmp(opts
->action
, "dump") == 0) {
3882 key
= mntent
.mnt_fsname
;
3883 val
= mntent
.mnt_dir
;
3885 key
= mntent
.mnt_dir
;
3886 val
= mntent
.mnt_fsname
;
3889 ret
= snprintf(arg
, sizeof(arg
), "%s:%s", key
, val
);
3890 if (ret
< 0 || ret
>= sizeof(arg
)) {
3894 DECLARE_ARG("--ext-mount-map");
3903 lxc_list_for_each(it
, &opts
->c
->lxc_conf
->network
) {
3904 struct lxc_netdev
*n
= it
->elem
;
3908 * Here, we set some parameters that lxc-restore-net
3909 * will examine to figure out the right network to
3912 snprintf(buf
, sizeof(buf
), "LXC_CRIU_BRIDGE%d", netnr
);
3913 if (setenv(buf
, n
->link
, 1))
3916 if (strcmp("restore", opts
->action
) == 0)
3917 strncpy(veth
, n
->priv
.veth_attr
.pair
, sizeof(veth
));
3920 ret
= snprintf(buf
, sizeof(buf
), "lxc.network.%d.veth.pair", netnr
);
3921 if (ret
< 0 || ret
>= sizeof(buf
))
3923 tmp
= lxcapi_get_running_config_item(opts
->c
, buf
);
3924 strncpy(veth
, tmp
, sizeof(veth
));
3928 snprintf(buf
, sizeof(buf
), "LXC_CRIU_VETH%d", netnr
);
3929 if (setenv(buf
, veth
, 1))
3937 execv(argv
[0], argv
);
3941 for (i
= 0; argv
[i
]; i
++)
3946 /* Check and make sure the container has a configuration that we know CRIU can
3948 static bool criu_ok(struct lxc_container
*c
)
3950 struct lxc_list
*it
;
3951 bool found_deny_rule
= false;
3954 ERROR("Must be root to checkpoint\n");
3958 /* We only know how to restore containers with veth networks. */
3959 lxc_list_for_each(it
, &c
->lxc_conf
->network
) {
3960 struct lxc_netdev
*n
= it
->elem
;
3961 if (n
->type
!= LXC_NET_VETH
&& n
->type
!= LXC_NET_NONE
) {
3962 ERROR("Found network that is not VETH or NONE\n");
3967 // These requirements come from http://criu.org/LXC
3968 if (c
->lxc_conf
->console
.path
&&
3969 strcmp(c
->lxc_conf
->console
.path
, "none") != 0) {
3970 ERROR("lxc.console must be none\n");
3974 if (c
->lxc_conf
->tty
!= 0) {
3975 ERROR("lxc.tty must be 0\n");
3979 lxc_list_for_each(it
, &c
->lxc_conf
->cgroup
) {
3980 struct lxc_cgroup
*cg
= it
->elem
;
3981 if (strcmp(cg
->subsystem
, "devices.deny") == 0 &&
3982 strcmp(cg
->value
, "c 5:1 rwm") == 0) {
3984 found_deny_rule
= true;
3989 if (!found_deny_rule
) {
3990 ERROR("couldn't find devices.deny = c 5:1 rwm");
3997 static bool dump_net_info(struct lxc_container
*c
, char *directory
)
4000 struct lxc_list
*it
;
4003 lxc_list_for_each(it
, &c
->lxc_conf
->network
) {
4004 char *veth
= NULL
, *bridge
= NULL
, veth_path
[PATH_MAX
], eth
[128];
4005 struct lxc_netdev
*n
= it
->elem
;
4006 bool has_error
= true;
4009 pret
= snprintf(veth_path
, PATH_MAX
, "lxc.network.%d.veth.pair", netnr
);
4010 if (pret
< 0 || pret
>= PATH_MAX
)
4013 veth
= lxcapi_get_running_config_item(c
, veth_path
);
4015 /* criu_ok() checks that all interfaces are
4016 * LXC_NET{VETH,NONE}, and VETHs should have this
4018 assert(n
->type
== LXC_NET_NONE
);
4022 bridge
= lxcapi_get_running_config_item(c
, veth_path
);
4026 pret
= snprintf(veth_path
, PATH_MAX
, "%s/veth%d", directory
, netnr
);
4027 if (pret
< 0 || pret
>= PATH_MAX
|| print_to_file(veth_path
, veth
) < 0)
4031 if (strlen(n
->name
) >= 128)
4033 strncpy(eth
, n
->name
, 128);
4035 sprintf(eth
, "eth%d", netnr
);
4048 static bool do_lxcapi_checkpoint(struct lxc_container
*c
, char *directory
, bool stop
, bool verbose
)
4056 if (mkdir(directory
, 0700) < 0 && errno
!= EEXIST
)
4059 if (!dump_net_info(c
, directory
))
4067 struct criu_opts os
;
4070 os
.directory
= directory
;
4073 os
.verbose
= verbose
;
4075 /* exec_criu() returning is an error */
4079 pid_t w
= waitpid(pid
, &status
, 0);
4081 SYSERROR("waitpid");
4085 if (WIFEXITED(status
)) {
4086 return !WEXITSTATUS(status
);
4093 WRAP_API_3(bool, lxcapi_checkpoint
, char *, bool, bool)
4095 static bool restore_net_info(struct lxc_container
*c
)
4097 struct lxc_list
*it
;
4098 bool has_error
= true;
4100 if (container_mem_lock(c
))
4103 lxc_list_for_each(it
, &c
->lxc_conf
->network
) {
4104 struct lxc_netdev
*netdev
= it
->elem
;
4105 char template[IFNAMSIZ
];
4106 snprintf(template, sizeof(template), "vethXXXXXX");
4108 if (!netdev
->priv
.veth_attr
.pair
)
4109 netdev
->priv
.veth_attr
.pair
= lxc_mkifname(template);
4111 if (!netdev
->priv
.veth_attr
.pair
)
4118 container_mem_unlock(c
);
4122 // do_restore never returns, the calling process is used as the
4123 // monitor process. do_restore calls exit() if it fails.
4124 static void do_restore(struct lxc_container
*c
, int pipe
, char *directory
, bool verbose
)
4127 char pidfile
[L_tmpnam
];
4128 struct lxc_handler
*handler
;
4131 if (!tmpnam(pidfile
))
4134 handler
= lxc_init(c
->name
, c
->lxc_conf
, c
->config_path
);
4138 if (!cgroup_init(handler
)) {
4139 ERROR("failed initing cgroups");
4140 goto out_fini_handler
;
4143 if (!cgroup_create(handler
)) {
4144 ERROR("failed creating groups");
4145 goto out_fini_handler
;
4148 if (!restore_net_info(c
)) {
4149 ERROR("failed restoring network info");
4150 goto out_fini_handler
;
4153 resolve_clone_flags(handler
);
4157 goto out_fini_handler
;
4160 struct criu_opts os
;
4161 struct lxc_rootfs
*rootfs
;
4166 if (unshare(CLONE_NEWNS
))
4167 goto out_fini_handler
;
4169 /* CRIU needs the lxc root bind mounted so that it is the root of some
4171 rootfs
= &c
->lxc_conf
->rootfs
;
4173 if (rootfs_is_blockdev(c
->lxc_conf
)) {
4174 if (do_rootfs_setup(c
->lxc_conf
, c
->name
, c
->config_path
) < 0)
4175 goto out_fini_handler
;
4177 if (mkdir(rootfs
->mount
, 0755) < 0 && errno
!= EEXIST
)
4178 goto out_fini_handler
;
4180 if (mount(rootfs
->path
, rootfs
->mount
, NULL
, MS_BIND
, NULL
) < 0) {
4181 rmdir(rootfs
->mount
);
4182 goto out_fini_handler
;
4186 os
.action
= "restore";
4187 os
.directory
= directory
;
4189 os
.pidfile
= pidfile
;
4190 os
.verbose
= verbose
;
4191 os
.cgroup_path
= cgroup_canonical_path(handler
);
4193 /* exec_criu() returning is an error */
4195 umount(rootfs
->mount
);
4196 rmdir(rootfs
->mount
);
4197 goto out_fini_handler
;
4202 pid_t w
= waitpid(pid
, &status
, 0);
4204 SYSERROR("waitpid");
4205 goto out_fini_handler
;
4208 ret
= write(pipe
, &status
, sizeof(status
));
4212 if (sizeof(status
) != ret
) {
4213 SYSERROR("failed to write all of status");
4214 goto out_fini_handler
;
4217 if (WIFEXITED(status
)) {
4218 if (WEXITSTATUS(status
)) {
4219 goto out_fini_handler
;
4222 FILE *f
= fopen(pidfile
, "r");
4224 SYSERROR("couldn't read restore's init pidfile %s\n", pidfile
);
4225 goto out_fini_handler
;
4228 ret
= fscanf(f
, "%d", (int*) &handler
->pid
);
4231 ERROR("reading restore pid failed");
4232 goto out_fini_handler
;
4235 if (lxc_set_state(c
->name
, handler
, RUNNING
))
4236 goto out_fini_handler
;
4239 ERROR("CRIU was killed with signal %d\n", WTERMSIG(status
));
4240 goto out_fini_handler
;
4244 * See comment in lxcapi_start; we don't care if these
4245 * fail because it's just a beauty thing. We just
4246 * assign the return here to silence potential.
4248 ret
= snprintf(title
, sizeof(title
), "[lxc monitor] %s %s", c
->config_path
, c
->name
);
4249 ret
= setproctitle(title
);
4251 ret
= lxc_poll(c
->name
, handler
);
4253 lxc_abort(c
->name
, handler
);
4254 lxc_fini(c
->name
, handler
);
4259 lxc_fini(c
->name
, handler
);
4264 if (write(pipe
, &status
, sizeof(status
)) != sizeof(status
)) {
4265 SYSERROR("writing status failed");
4273 static bool do_lxcapi_restore(struct lxc_container
*c
, char *directory
, bool verbose
)
4283 ERROR("Must be root to restore\n");
4288 ERROR("failed to create pipe");
4301 // this never returns
4302 do_restore(c
, pipefd
[1], directory
, verbose
);
4307 nread
= read(pipefd
[0], &status
, sizeof(status
));
4309 if (sizeof(status
) != nread
) {
4310 ERROR("reading status from pipe failed");
4314 // If the criu process was killed or exited nonzero, wait() for the
4315 // handler, since the restore process died. Otherwise, we don't need to
4316 // wait, since the child becomes the monitor process.
4317 if (!WIFEXITED(status
) || WEXITSTATUS(status
))
4322 if (wait_for_pid(pid
))
4323 ERROR("restore process died");
4327 WRAP_API_2(bool, lxcapi_restore
, char *, bool)
4329 static int lxcapi_attach_run_waitl(struct lxc_container
*c
, lxc_attach_options_t
*options
, const char *program
, const char *arg
, ...)
4338 struct lxc_conf
*old
= current_config
;
4339 current_config
= c
->lxc_conf
;
4342 argv
= lxc_va_arg_list_to_argv_const(ap
, 1);
4346 ERROR("Memory allocation error.");
4352 ret
= do_lxcapi_attach_run_wait(c
, options
, program
, (const char * const *)argv
);
4355 current_config
= old
;
4359 struct lxc_container
*lxc_container_new(const char *name
, const char *configpath
)
4361 struct lxc_container
*c
;
4366 c
= malloc(sizeof(*c
));
4368 fprintf(stderr
, "failed to malloc lxc_container\n");
4371 memset(c
, 0, sizeof(*c
));
4374 c
->config_path
= strdup(configpath
);
4376 c
->config_path
= strdup(lxc_global_config_value("lxc.lxcpath"));
4378 if (!c
->config_path
) {
4379 fprintf(stderr
, "Out of memory\n");
4383 remove_trailing_slashes(c
->config_path
);
4384 c
->name
= malloc(strlen(name
)+1);
4386 fprintf(stderr
, "Error allocating lxc_container name\n");
4389 strcpy(c
->name
, name
);
4392 if (!(c
->slock
= lxc_newlock(c
->config_path
, name
))) {
4393 fprintf(stderr
, "failed to create lock\n");
4397 if (!(c
->privlock
= lxc_newlock(NULL
, NULL
))) {
4398 fprintf(stderr
, "failed to alloc privlock\n");
4402 if (!set_config_filename(c
)) {
4403 fprintf(stderr
, "Error allocating config file pathname\n");
4407 if (file_exists(c
->configfile
) && !lxcapi_load_config(c
, NULL
))
4410 if (ongoing_create(c
) == 2) {
4411 ERROR("Error: %s creation was not completed", c
->name
);
4412 container_destroy(c
);
4413 lxcapi_clear_config(c
);
4415 c
->daemonize
= true;
4418 // assign the member functions
4419 c
->is_defined
= lxcapi_is_defined
;
4420 c
->state
= lxcapi_state
;
4421 c
->is_running
= lxcapi_is_running
;
4422 c
->freeze
= lxcapi_freeze
;
4423 c
->unfreeze
= lxcapi_unfreeze
;
4424 c
->console
= lxcapi_console
;
4425 c
->console_getfd
= lxcapi_console_getfd
;
4426 c
->init_pid
= lxcapi_init_pid
;
4427 c
->load_config
= lxcapi_load_config
;
4428 c
->want_daemonize
= lxcapi_want_daemonize
;
4429 c
->want_close_all_fds
= lxcapi_want_close_all_fds
;
4430 c
->start
= lxcapi_start
;
4431 c
->startl
= lxcapi_startl
;
4432 c
->stop
= lxcapi_stop
;
4433 c
->config_file_name
= lxcapi_config_file_name
;
4434 c
->wait
= lxcapi_wait
;
4435 c
->set_config_item
= lxcapi_set_config_item
;
4436 c
->destroy
= lxcapi_destroy
;
4437 c
->destroy_with_snapshots
= lxcapi_destroy_with_snapshots
;
4438 c
->rename
= lxcapi_rename
;
4439 c
->save_config
= lxcapi_save_config
;
4440 c
->get_keys
= lxcapi_get_keys
;
4441 c
->create
= lxcapi_create
;
4442 c
->createl
= lxcapi_createl
;
4443 c
->shutdown
= lxcapi_shutdown
;
4444 c
->reboot
= lxcapi_reboot
;
4445 c
->clear_config
= lxcapi_clear_config
;
4446 c
->clear_config_item
= lxcapi_clear_config_item
;
4447 c
->get_config_item
= lxcapi_get_config_item
;
4448 c
->get_running_config_item
= lxcapi_get_running_config_item
;
4449 c
->get_cgroup_item
= lxcapi_get_cgroup_item
;
4450 c
->set_cgroup_item
= lxcapi_set_cgroup_item
;
4451 c
->get_config_path
= lxcapi_get_config_path
;
4452 c
->set_config_path
= lxcapi_set_config_path
;
4453 c
->clone
= lxcapi_clone
;
4454 c
->get_interfaces
= lxcapi_get_interfaces
;
4455 c
->get_ips
= lxcapi_get_ips
;
4456 c
->attach
= lxcapi_attach
;
4457 c
->attach_run_wait
= lxcapi_attach_run_wait
;
4458 c
->attach_run_waitl
= lxcapi_attach_run_waitl
;
4459 c
->snapshot
= lxcapi_snapshot
;
4460 c
->snapshot_list
= lxcapi_snapshot_list
;
4461 c
->snapshot_restore
= lxcapi_snapshot_restore
;
4462 c
->snapshot_destroy
= lxcapi_snapshot_destroy
;
4463 c
->snapshot_destroy_all
= lxcapi_snapshot_destroy_all
;
4464 c
->may_control
= lxcapi_may_control
;
4465 c
->add_device_node
= lxcapi_add_device_node
;
4466 c
->remove_device_node
= lxcapi_remove_device_node
;
4467 c
->attach_interface
= lxcapi_attach_interface
;
4468 c
->detach_interface
= lxcapi_detach_interface
;
4469 c
->checkpoint
= lxcapi_checkpoint
;
4470 c
->restore
= lxcapi_restore
;
4475 lxc_container_free(c
);
4479 int lxc_get_wait_states(const char **states
)
4484 for (i
=0; i
<MAX_STATE
; i
++)
4485 states
[i
] = lxc_state2str(i
);
4490 * These next two could probably be done smarter with reusing a common function
4491 * with different iterators and tests...
4493 int list_defined_containers(const char *lxcpath
, char ***names
, struct lxc_container
***cret
)
4496 int i
, cfound
= 0, nfound
= 0;
4497 struct dirent dirent
, *direntp
;
4498 struct lxc_container
*c
;
4501 lxcpath
= lxc_global_config_value("lxc.lxcpath");
4503 dir
= opendir(lxcpath
);
4505 SYSERROR("opendir on lxcpath");
4514 while (!readdir_r(dir
, &dirent
, &direntp
)) {
4517 if (!strcmp(direntp
->d_name
, "."))
4519 if (!strcmp(direntp
->d_name
, ".."))
4522 if (!config_file_exists(lxcpath
, direntp
->d_name
))
4526 if (!add_to_array(names
, direntp
->d_name
, cfound
))
4536 c
= lxc_container_new(direntp
->d_name
, lxcpath
);
4538 INFO("Container %s:%s has a config but could not be loaded",
4539 lxcpath
, direntp
->d_name
);
4541 if(!remove_from_array(names
, direntp
->d_name
, cfound
--))
4545 if (!do_lxcapi_is_defined(c
)) {
4546 INFO("Container %s:%s has a config but is not defined",
4547 lxcpath
, direntp
->d_name
);
4549 if(!remove_from_array(names
, direntp
->d_name
, cfound
--))
4551 lxc_container_put(c
);
4555 if (!add_to_clist(cret
, c
, nfound
, true)) {
4556 lxc_container_put(c
);
4566 if (names
&& *names
) {
4567 for (i
=0; i
<cfound
; i
++)
4571 if (cret
&& *cret
) {
4572 for (i
=0; i
<nfound
; i
++)
4573 lxc_container_put((*cret
)[i
]);
4580 int list_active_containers(const char *lxcpath
, char ***nret
,
4581 struct lxc_container
***cret
)
4583 int i
, ret
= -1, cret_cnt
= 0, ct_name_cnt
= 0;
4586 char **ct_name
= NULL
;
4588 struct lxc_container
*c
;
4592 lxcpath
= lxc_global_config_value("lxc.lxcpath");
4593 lxcpath_len
= strlen(lxcpath
);
4600 FILE *f
= fopen("/proc/net/unix", "r");
4604 while (getline(&line
, &len
, f
) != -1) {
4606 char *p
= strrchr(line
, ' '), *p2
;
4615 if (strncmp(p
, lxcpath
, lxcpath_len
) == 0) {
4617 } else if (strncmp(p
, "lxc/", 4) == 0) {
4627 // Now p is the start of lxc_name
4628 p2
= strchr(p
, '/');
4629 if (!p2
|| strncmp(p2
, "/command", 8) != 0)
4634 if (strncmp(lxcpath
, lxc_cmd_get_lxcpath(p
), lxcpath_len
) != 0)
4636 p
= lxc_cmd_get_name(p
);
4639 if (array_contains(&ct_name
, p
, ct_name_cnt
))
4642 if (!add_to_array(&ct_name
, p
, ct_name_cnt
))
4643 goto free_cret_list
;
4650 c
= lxc_container_new(p
, lxcpath
);
4652 INFO("Container %s:%s is running but could not be loaded",
4654 remove_from_array(&ct_name
, p
, ct_name_cnt
--);
4659 * If this is an anonymous container, then is_defined *can*
4660 * return false. So we don't do that check. Count on the
4661 * fact that the command socket exists.
4664 if (!add_to_clist(cret
, c
, cret_cnt
, true)) {
4665 lxc_container_put(c
);
4666 goto free_cret_list
;
4671 assert(!nret
|| !cret
|| cret_cnt
== ct_name_cnt
);
4680 if (cret
&& *cret
) {
4681 for (i
= 0; i
< cret_cnt
; i
++)
4682 lxc_container_put((*cret
)[i
]);
4688 for (i
= 0; i
< ct_name_cnt
; i
++)
4700 int list_all_containers(const char *lxcpath
, char ***nret
,
4701 struct lxc_container
***cret
)
4703 int i
, ret
, active_cnt
, ct_cnt
, ct_list_cnt
;
4706 struct lxc_container
**ct_list
= NULL
;
4708 ct_cnt
= list_defined_containers(lxcpath
, &ct_name
, NULL
);
4712 active_cnt
= list_active_containers(lxcpath
, &active_name
, NULL
);
4713 if (active_cnt
< 0) {
4718 for (i
= 0; i
< active_cnt
; i
++) {
4719 if (!array_contains(&ct_name
, active_name
[i
], ct_cnt
)) {
4720 if (!add_to_array(&ct_name
, active_name
[i
], ct_cnt
)) {
4722 goto free_active_name
;
4726 free(active_name
[i
]);
4727 active_name
[i
] = NULL
;
4733 for (i
= 0, ct_list_cnt
= 0; i
< ct_cnt
&& cret
; i
++) {
4734 struct lxc_container
*c
;
4736 c
= lxc_container_new(ct_name
[i
], lxcpath
);
4738 WARN("Container %s:%s could not be loaded", lxcpath
, ct_name
[i
]);
4739 remove_from_array(&ct_name
, ct_name
[i
], ct_cnt
--);
4743 if (!add_to_clist(&ct_list
, c
, ct_list_cnt
, false)) {
4744 lxc_container_put(c
);
4763 for (i
= 0; i
< ct_list_cnt
; i
++) {
4764 lxc_container_put(ct_list
[i
]);
4769 for (i
= 0; i
< active_cnt
; i
++) {
4770 free(active_name
[i
]);
4775 for (i
= 0; i
< ct_cnt
; i
++) {