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
24 #include <arpa/inet.h>
38 #include <sys/mount.h>
40 #include <sys/syscall.h>
41 #include <sys/sysmacros.h>
42 #include <sys/types.h>
46 #include "../include/netns_ifaddrs.h"
48 #include "api_extensions.h"
52 #include "commands_utils.h"
56 #include "confile_utils.h"
59 #include "initutils.h"
62 #include "lxccontainer.h"
64 #include "memory_utils.h"
66 #include "namespace.h"
69 #include "raw_syscalls.h"
73 #include "storage/btrfs.h"
74 #include "storage/overlay.h"
75 #include "storage_utils.h"
77 #include "syscall_wrappers.h"
83 #include <openssl/evp.h>
88 #include <sys/mkdev.h>
92 #include <../include/lxcmntent.h>
98 #include "include/strlcpy.h"
101 /* Define faccessat() if missing from the C library */
102 #ifndef HAVE_FACCESSAT
103 static int faccessat(int __fd
, const char *__file
, int __type
, int __flag
)
105 #ifdef __NR_faccessat
106 return syscall(__NR_faccessat
, __fd
, __file
, __type
, __flag
);
114 lxc_log_define(lxccontainer
, lxc
);
116 static bool do_lxcapi_destroy(struct lxc_container
*c
);
117 static const char *lxcapi_get_config_path(struct lxc_container
*c
);
118 #define do_lxcapi_get_config_path(c) lxcapi_get_config_path(c)
119 static bool do_lxcapi_set_config_item(struct lxc_container
*c
, const char *key
, const char *v
);
120 static bool container_destroy(struct lxc_container
*c
,
121 struct lxc_storage
*storage
);
122 static bool get_snappath_dir(struct lxc_container
*c
, char *snappath
);
123 static bool lxcapi_snapshot_destroy_all(struct lxc_container
*c
);
124 static bool do_lxcapi_save_config(struct lxc_container
*c
, const char *alt_file
);
126 static bool config_file_exists(const char *lxcpath
, const char *cname
)
128 __do_free
char *fname
= NULL
;
132 /* $lxcpath + '/' + $cname + '/config' + \0 */
133 len
= strlen(lxcpath
) + strlen(cname
) + 9;
134 fname
= must_realloc(NULL
, len
);
135 ret
= snprintf(fname
, len
, "%s/%s/config", lxcpath
, cname
);
136 if (ret
< 0 || (size_t)ret
>= len
)
139 return file_exists(fname
);
143 * A few functions to help detect when a container creation failed. If a
144 * container creation was killed partway through, then trying to actually start
145 * that container could harm the host. We detect this by creating a 'partial'
146 * file under the container directory, and keeping an advisory lock. When
147 * container creation completes, we remove that file. When we load or try to
148 * start a container, if we find that file, without a flock, we remove the
152 LXC_CREATE_FAILED
= -1,
153 LXC_CREATE_SUCCESS
= 0,
154 LXC_CREATE_ONGOING
= 1,
155 LXC_CREATE_INCOMPLETE
= 2,
158 static int ongoing_create(struct lxc_container
*c
)
160 __do_close_prot_errno
int fd
= -EBADF
;
161 __do_free
char *path
= NULL
;
162 struct flock lk
= {0};
166 len
= strlen(c
->config_path
) + strlen(c
->name
) + 10;
167 path
= must_realloc(NULL
, len
);
168 ret
= snprintf(path
, len
, "%s/%s/partial", c
->config_path
, c
->name
);
169 if (ret
< 0 || (size_t)ret
>= len
)
170 return LXC_CREATE_FAILED
;
172 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
175 return LXC_CREATE_FAILED
;
177 return LXC_CREATE_SUCCESS
;
181 lk
.l_whence
= SEEK_SET
;
183 * F_OFD_GETLK requires that l_pid be set to 0 otherwise the kernel
188 ret
= fcntl(fd
, F_OFD_GETLK
, &lk
);
189 if (ret
< 0 && errno
== EINVAL
) {
190 ret
= flock(fd
, LOCK_EX
| LOCK_NB
);
191 if (ret
< 0 && errno
== EWOULDBLOCK
)
195 /* F_OFD_GETLK will not send us back a pid so don't check it. */
197 /* Create is still ongoing. */
198 return LXC_CREATE_ONGOING
;
200 /* Create completed but partial is still there. */
201 return LXC_CREATE_INCOMPLETE
;
204 static int create_partial(struct lxc_container
*c
)
206 __do_free
char *path
= NULL
;
209 struct flock lk
= {0};
211 /* $lxcpath + '/' + $name + '/partial' + \0 */
212 len
= strlen(c
->config_path
) + strlen(c
->name
) + 10;
213 path
= must_realloc(NULL
, len
);
214 ret
= snprintf(path
, len
, "%s/%s/partial", c
->config_path
, c
->name
);
215 if (ret
< 0 || (size_t)ret
>= len
)
218 fd
= open(path
, O_RDWR
| O_CREAT
| O_EXCL
| O_CLOEXEC
, 0000);
223 lk
.l_whence
= SEEK_SET
;
225 ret
= fcntl(fd
, F_OFD_SETLKW
, &lk
);
227 if (errno
== EINVAL
) {
228 ret
= flock(fd
, LOCK_EX
);
233 SYSERROR("Failed to lock partial file %s", path
);
241 static void remove_partial(struct lxc_container
*c
, int fd
)
243 __do_free
char *path
= NULL
;
249 /* $lxcpath + '/' + $name + '/partial' + \0 */
250 len
= strlen(c
->config_path
) + strlen(c
->name
) + 10;
251 path
= must_realloc(NULL
, len
);
252 ret
= snprintf(path
, len
, "%s/%s/partial", c
->config_path
, c
->name
);
253 if (ret
< 0 || (size_t)ret
>= len
)
258 SYSERROR("Failed to remove partial file %s", path
);
262 * 1. container_mem_lock(c) protects the struct lxc_container from multiple threads.
263 * 2. container_disk_lock(c) protects the on-disk container data - in particular the
264 * container configuration file.
265 * The container_disk_lock also takes the container_mem_lock.
266 * 3. thread_mutex protects process data (ex: fd table) from multiple threads.
267 * NOTHING mutexes two independent programs with their own struct
268 * lxc_container for the same c->name, between API calls. For instance,
269 * c->config_read(); c->start(); Between those calls, data on disk
270 * could change (which shouldn't bother the caller unless for instance
271 * the rootfs get moved). c->config_read(); update; c->config_write();
272 * Two such updaters could race. The callers should therefore check their
273 * results. Trying to prevent that would necessarily expose us to deadlocks
274 * due to hung callers. So I prefer to keep the locks only within our own
275 * functions, not across functions.
277 * If you're going to clone while holding a lxccontainer, increment
278 * c->numthreads (under privlock) before forking. When deleting,
279 * decrement numthreads under privlock, then if it hits 0 you can delete.
280 * Do not ever use a lxccontainer whose numthreads you did not bump.
282 static void lxc_container_free(struct lxc_container
*c
)
288 c
->configfile
= NULL
;
290 free(c
->error_string
);
291 c
->error_string
= NULL
;
294 lxc_putlock(c
->slock
);
299 lxc_putlock(c
->privlock
);
307 lxc_conf_free(c
->lxc_conf
);
311 free(c
->config_path
);
312 c
->config_path
= NULL
;
317 /* Consider the following case:
319 * |====================================================================|
320 * | freer | racing get()er |
321 * |====================================================================|
322 * | lxc_container_put() | lxc_container_get() |
323 * | \ lxclock(c->privlock) | c->numthreads < 1? (no) |
324 * | \ c->numthreads = 0 | \ lxclock(c->privlock) -> waits |
325 * | \ lxcunlock() | \ |
326 * | \ lxc_container_free() | \ lxclock() returns |
327 * | | \ c->numthreads < 1 -> return 0 |
328 * | \ \ (free stuff) | |
329 * | \ \ sem_destroy(privlock) | |
330 * |_______________________________|____________________________________|
332 * When the get()er checks numthreads the first time, one of the following
334 * 1. freer has set numthreads = 0. get() returns 0
335 * 2. freer is between lxclock and setting numthreads to 0. get()er will
336 * sem_wait on privlock, get lxclock after freer() drops it, then see
337 * numthreads is 0 and exit without touching lxclock again..
338 * 3. freer has not yet locked privlock. If get()er runs first, then put()er
339 * will see --numthreads = 1 and not call lxc_container_free().
342 int lxc_container_get(struct lxc_container
*c
)
347 /* If someone else has already started freeing the container, don't try
348 * to take the lock, which may be invalid.
350 if (c
->numthreads
< 1)
353 if (container_mem_lock(c
))
356 /* Bail without trying to unlock, bc the privlock is now probably in
359 if (c
->numthreads
< 1)
363 container_mem_unlock(c
);
368 int lxc_container_put(struct lxc_container
*c
)
373 if (container_mem_lock(c
))
378 if (c
->numthreads
< 1) {
379 container_mem_unlock(c
);
380 lxc_container_free(c
);
384 container_mem_unlock(c
);
388 static bool do_lxcapi_is_defined(struct lxc_container
*c
)
397 if (container_mem_lock(c
))
403 statret
= stat(c
->configfile
, &statbuf
);
410 container_mem_unlock(c
);
414 #define WRAP_API(rettype, fnname) \
415 static rettype fnname(struct lxc_container *c) \
418 bool reset_config = false; \
420 if (!current_config && c && c->lxc_conf) { \
421 current_config = c->lxc_conf; \
422 reset_config = true; \
425 ret = do_##fnname(c); \
427 current_config = NULL; \
432 #define WRAP_API_1(rettype, fnname, t1) \
433 static rettype fnname(struct lxc_container *c, t1 a1) \
436 bool reset_config = false; \
438 if (!current_config && c && c->lxc_conf) { \
439 current_config = c->lxc_conf; \
440 reset_config = true; \
443 ret = do_##fnname(c, a1); \
445 current_config = NULL; \
450 #define WRAP_API_2(rettype, fnname, t1, t2) \
451 static rettype fnname(struct lxc_container *c, t1 a1, t2 a2) \
454 bool reset_config = false; \
456 if (!current_config && c && c->lxc_conf) { \
457 current_config = c->lxc_conf; \
458 reset_config = true; \
461 ret = do_##fnname(c, a1, a2); \
463 current_config = NULL; \
468 #define WRAP_API_3(rettype, fnname, t1, t2, t3) \
469 static rettype fnname(struct lxc_container *c, t1 a1, t2 a2, t3 a3) \
472 bool reset_config = false; \
474 if (!current_config && c && c->lxc_conf) { \
475 current_config = c->lxc_conf; \
476 reset_config = true; \
479 ret = do_##fnname(c, a1, a2, a3); \
481 current_config = NULL; \
486 #define WRAP_API_6(rettype, fnname, t1, t2, t3, t4, t5, t6) \
487 static rettype fnname(struct lxc_container *c, t1 a1, t2 a2, t3 a3, \
488 t4 a4, t5 a5, t6 a6) \
491 bool reset_config = false; \
493 if (!current_config && c && c->lxc_conf) { \
494 current_config = c->lxc_conf; \
495 reset_config = true; \
498 ret = do_##fnname(c, a1, a2, a3, a4, a5, a6); \
500 current_config = NULL; \
505 WRAP_API(bool, lxcapi_is_defined
)
507 static const char *do_lxcapi_state(struct lxc_container
*c
)
514 s
= lxc_getstate(c
->name
, c
->config_path
);
515 return lxc_state2str(s
);
518 WRAP_API(const char *, lxcapi_state
)
520 static bool is_stopped(struct lxc_container
*c
)
524 s
= lxc_getstate(c
->name
, c
->config_path
);
525 return (s
== STOPPED
);
528 static bool do_lxcapi_is_running(struct lxc_container
*c
)
533 return !is_stopped(c
);
536 WRAP_API(bool, lxcapi_is_running
)
538 static bool do_lxcapi_freeze(struct lxc_container
*c
)
542 if (!c
|| !c
->lxc_conf
)
545 s
= lxc_getstate(c
->name
, c
->config_path
);
547 return lxc_freeze(c
->lxc_conf
, c
->name
, c
->config_path
) == 0;
552 WRAP_API(bool, lxcapi_freeze
)
554 static bool do_lxcapi_unfreeze(struct lxc_container
*c
)
558 if (!c
|| !c
->lxc_conf
)
561 s
= lxc_getstate(c
->name
, c
->config_path
);
563 return lxc_unfreeze(c
->lxc_conf
, c
->name
, c
->config_path
) == 0;
568 WRAP_API(bool, lxcapi_unfreeze
)
570 static int do_lxcapi_console_getfd(struct lxc_container
*c
, int *ttynum
, int *masterfd
)
575 return lxc_terminal_getfd(c
, ttynum
, masterfd
);
578 WRAP_API_2(int, lxcapi_console_getfd
, int *, int *)
580 static int lxcapi_console(struct lxc_container
*c
, int ttynum
, int stdinfd
,
581 int stdoutfd
, int stderrfd
, int escape
)
588 current_config
= c
->lxc_conf
;
589 ret
= lxc_console(c
, ttynum
, stdinfd
, stdoutfd
, stderrfd
, escape
);
590 current_config
= NULL
;
595 static int do_lxcapi_console_log(struct lxc_container
*c
, struct lxc_console_log
*log
)
602 ret
= lxc_cmd_console_log(c
->name
, do_lxcapi_get_config_path(c
), log
);
605 NOTICE("The console log is empty");
606 else if (ret
== -EFAULT
)
607 NOTICE("The container does not keep a console log");
608 else if (ret
== -ENOENT
)
609 NOTICE("The container does not keep a console log file");
610 else if (ret
== -EIO
)
611 NOTICE("Failed to write console log to log file");
613 ERROR("Failed to retrieve console log");
619 WRAP_API_1(int, lxcapi_console_log
, struct lxc_console_log
*)
621 static pid_t
do_lxcapi_init_pid(struct lxc_container
*c
)
626 return lxc_cmd_get_init_pid(c
->name
, c
->config_path
);
629 WRAP_API(pid_t
, lxcapi_init_pid
)
631 static bool load_config_locked(struct lxc_container
*c
, const char *fname
)
634 c
->lxc_conf
= lxc_conf_init();
639 if (lxc_config_read(fname
, c
->lxc_conf
, false) != 0)
642 c
->lxc_conf
->name
= c
->name
;
646 static bool do_lxcapi_load_config(struct lxc_container
*c
, const char *alt_file
)
650 bool need_disklock
= false, ret
= false;
655 fname
= c
->configfile
;
663 /* If we're reading something other than the container's config, we only
664 * need to lock the in-memory container. If loading the container's
665 * config file, take the disk lock.
667 if (strcmp(fname
, c
->configfile
) == 0)
668 need_disklock
= true;
671 lret
= container_disk_lock(c
);
673 lret
= container_mem_lock(c
);
677 ret
= load_config_locked(c
, fname
);
680 container_disk_unlock(c
);
682 container_mem_unlock(c
);
687 WRAP_API_1(bool, lxcapi_load_config
, const char *)
689 static bool do_lxcapi_want_daemonize(struct lxc_container
*c
, bool state
)
691 if (!c
|| !c
->lxc_conf
)
694 if (container_mem_lock(c
))
697 c
->daemonize
= state
;
699 container_mem_unlock(c
);
704 WRAP_API_1(bool, lxcapi_want_daemonize
, bool)
706 static bool do_lxcapi_want_close_all_fds(struct lxc_container
*c
, bool state
)
708 if (!c
|| !c
->lxc_conf
)
711 if (container_mem_lock(c
))
714 c
->lxc_conf
->close_all_fds
= state
;
716 container_mem_unlock(c
);
721 WRAP_API_1(bool, lxcapi_want_close_all_fds
, bool)
723 static bool do_lxcapi_wait(struct lxc_container
*c
, const char *state
,
731 ret
= lxc_wait(c
->name
, state
, timeout
, c
->config_path
);
735 WRAP_API_2(bool, lxcapi_wait
, const char *, int)
737 static bool am_single_threaded(void)
740 struct dirent
*direntp
;
743 dir
= opendir("/proc/self/task");
747 while ((direntp
= readdir(dir
))) {
748 if (strcmp(direntp
->d_name
, ".") == 0)
751 if (strcmp(direntp
->d_name
, "..") == 0)
763 static void push_arg(char ***argp
, char *arg
, int *nargs
)
768 copy
= must_copy_string(arg
);
771 argv
= realloc(*argp
, (*nargs
+ 2) * sizeof(char *));
780 static char **split_init_cmd(const char *incmd
)
782 __do_free
char *copy
= NULL
;
790 copy
= must_copy_string(incmd
);
793 argv
= malloc(sizeof(char *));
797 lxc_iterate_parts (p
, copy
, " ")
798 push_arg(&argv
, p
, &nargs
);
808 static void free_init_cmd(char **argv
)
821 static int lxc_rcv_status(int state_socket
)
827 /* Receive container state. */
828 ret
= lxc_abstract_unix_rcv_credential(state_socket
, &state
, sizeof(int));
833 TRACE("Caught EINTR; retrying");
840 static bool wait_on_daemonized_start(struct lxc_handler
*handler
, int pid
)
844 /* Close write end of the socket pair. */
845 close(handler
->state_socket_pair
[1]);
846 handler
->state_socket_pair
[1] = -1;
848 state
= lxc_rcv_status(handler
->state_socket_pair
[0]);
850 /* Close read end of the socket pair. */
851 close(handler
->state_socket_pair
[0]);
852 handler
->state_socket_pair
[0] = -1;
854 /* The first child is going to fork() again and then exits. So we reap
855 * the first child here.
857 ret
= wait_for_pid(pid
);
859 DEBUG("Failed waiting on first child %d", pid
);
861 DEBUG("First child %d exited", pid
);
864 SYSERROR("Failed to receive the container state");
868 /* If we receive anything else then running we know that the container
871 if (state
!= RUNNING
) {
872 ERROR("Received container state \"%s\" instead of \"RUNNING\"",
873 lxc_state2str(state
));
877 TRACE("Container is in \"RUNNING\" state");
881 static bool do_lxcapi_start(struct lxc_container
*c
, int useinit
, char * const argv
[])
884 struct lxc_handler
*handler
;
885 struct lxc_conf
*conf
;
886 char *default_args
[] = {
890 char **init_cmd
= NULL
;
891 int keepfds
[3] = {-1, -1, -1};
893 /* container does exist */
897 /* If anything fails before we set error_num, we want an error in there.
901 /* Container has not been setup. */
905 ret
= ongoing_create(c
);
907 case LXC_CREATE_FAILED
:
908 ERROR("Failed checking for incomplete container creation");
910 case LXC_CREATE_ONGOING
:
911 ERROR("Ongoing container creation detected");
913 case LXC_CREATE_INCOMPLETE
:
914 ERROR("Failed to create container");
915 do_lxcapi_destroy(c
);
919 if (container_mem_lock(c
))
924 /* initialize handler */
925 handler
= lxc_init_handler(c
->name
, conf
, c
->config_path
, c
->daemonize
);
927 container_mem_unlock(c
);
932 if (useinit
&& conf
->execute_cmd
)
933 argv
= init_cmd
= split_init_cmd(conf
->execute_cmd
);
935 argv
= init_cmd
= split_init_cmd(conf
->init_cmd
);
938 /* ... otherwise use default_args. */
941 ERROR("No valid init detected");
942 lxc_free_handler(handler
);
948 /* I'm not sure what locks we want here.Any? Is liblxc's locking enough
949 * here to protect the on disk container? We don't want to exclude
950 * things like lxc_info while the container is running.
959 free_init_cmd(init_cmd
);
960 lxc_free_handler(handler
);
966 /* Set to NULL because we don't want father unlink
967 * the PID file, child will do the free and unlink.
971 /* Wait for container to tell us whether it started
974 started
= wait_on_daemonized_start(handler
, pid
);
976 free_init_cmd(init_cmd
);
977 lxc_free_handler(handler
);
983 /* We don't really care if this doesn't print all the
984 * characters. All that it means is that the proctitle will be
985 * ugly. Similarly, we also don't care if setproctitle() fails.
987 ret
= snprintf(title
, sizeof(title
), "[lxc monitor] %s %s", c
->config_path
, c
->name
);
989 ret
= setproctitle(title
);
991 INFO("Failed to set process title to %s", title
);
993 INFO("Set process title to %s", title
);
996 /* We fork() a second time to be reparented to init. Like
997 * POSIX's daemon() function we change to "/" and redirect
998 * std{in,out,err} to /dev/null.
1002 SYSERROR("Failed to fork first child process");
1003 _exit(EXIT_FAILURE
);
1008 free_init_cmd(init_cmd
);
1009 lxc_free_handler(handler
);
1010 _exit(EXIT_SUCCESS
);
1015 /* change to / directory */
1018 SYSERROR("Failed to change to \"/\" directory");
1019 _exit(EXIT_FAILURE
);
1022 keepfds
[0] = handler
->conf
->maincmd_fd
;
1023 keepfds
[1] = handler
->state_socket_pair
[0];
1024 keepfds
[2] = handler
->state_socket_pair
[1];
1025 ret
= lxc_check_inherited(conf
, true, keepfds
,
1026 sizeof(keepfds
) / sizeof(keepfds
[0]));
1028 _exit(EXIT_FAILURE
);
1030 /* redirect std{in,out,err} to /dev/null */
1031 ret
= null_stdfds();
1033 ERROR("Failed to redirect std{in,out,err} to /dev/null");
1034 _exit(EXIT_FAILURE
);
1037 /* become session leader */
1040 TRACE("Process %d is already process group leader", lxc_raw_getpid());
1041 } else if (!am_single_threaded()) {
1042 ERROR("Cannot start non-daemonized container when threaded");
1043 free_init_cmd(init_cmd
);
1044 lxc_free_handler(handler
);
1048 /* We need to write PID file after daemonize, so we always write the
1053 char pidstr
[INTTYPE_TO_STRLEN(pid_t
)];
1055 w
= snprintf(pidstr
, sizeof(pidstr
), "%d", lxc_raw_getpid());
1056 if (w
< 0 || (size_t)w
>= sizeof(pidstr
)) {
1057 free_init_cmd(init_cmd
);
1058 lxc_free_handler(handler
);
1060 SYSERROR("Failed to write monitor pid to \"%s\"", c
->pidfile
);
1063 _exit(EXIT_FAILURE
);
1068 ret
= lxc_write_to_file(c
->pidfile
, pidstr
, w
, false, 0600);
1070 free_init_cmd(init_cmd
);
1071 lxc_free_handler(handler
);
1073 SYSERROR("Failed to write monitor pid to \"%s\"", c
->pidfile
);
1076 _exit(EXIT_FAILURE
);
1082 conf
->reboot
= REBOOT_NONE
;
1084 /* Unshare the mount namespace if requested */
1085 if (conf
->monitor_unshare
) {
1086 ret
= unshare(CLONE_NEWNS
);
1088 SYSERROR("Failed to unshare mount namespace");
1089 lxc_free_handler(handler
);
1094 ret
= mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
);
1096 SYSERROR("Failed to make / rslave at startup");
1097 lxc_free_handler(handler
);
1104 if (conf
->reboot
== REBOOT_INIT
) {
1105 /* initialize handler */
1106 handler
= lxc_init_handler(c
->name
, conf
, c
->config_path
, c
->daemonize
);
1113 keepfds
[0] = handler
->conf
->maincmd_fd
;
1114 keepfds
[1] = handler
->state_socket_pair
[0];
1115 keepfds
[2] = handler
->state_socket_pair
[1];
1116 ret
= lxc_check_inherited(conf
, c
->daemonize
, keepfds
,
1117 sizeof(keepfds
) / sizeof(keepfds
[0]));
1119 lxc_free_handler(handler
);
1125 ret
= lxc_execute(c
->name
, argv
, 1, handler
, c
->config_path
,
1126 c
->daemonize
, &c
->error_num
);
1128 ret
= lxc_start(c
->name
, argv
, handler
, c
->config_path
,
1129 c
->daemonize
, &c
->error_num
);
1131 if (conf
->reboot
== REBOOT_REQ
) {
1132 INFO("Container requested reboot");
1133 conf
->reboot
= REBOOT_INIT
;
1143 free_init_cmd(init_cmd
);
1145 if (c
->daemonize
&& ret
!= 0)
1146 _exit(EXIT_FAILURE
);
1147 else if (c
->daemonize
)
1148 _exit(EXIT_SUCCESS
);
1156 static bool lxcapi_start(struct lxc_container
*c
, int useinit
,
1161 current_config
= c
? c
->lxc_conf
: NULL
;
1162 ret
= do_lxcapi_start(c
, useinit
, argv
);
1163 current_config
= NULL
;
1168 /* Note, there MUST be an ending NULL. */
1169 static bool lxcapi_startl(struct lxc_container
*c
, int useinit
, ...)
1172 char **inargs
= NULL
;
1175 /* container exists */
1179 current_config
= c
->lxc_conf
;
1181 va_start(ap
, useinit
);
1182 inargs
= lxc_va_arg_list_to_argv(ap
, 0, 1);
1187 /* pass NULL if no arguments were supplied */
1188 bret
= do_lxcapi_start(c
, useinit
, *inargs
? inargs
: NULL
);
1194 for (arg
= inargs
; *arg
; arg
++)
1199 current_config
= NULL
;
1204 static bool do_lxcapi_stop(struct lxc_container
*c
)
1211 ret
= lxc_cmd_stop(c
->name
, c
->config_path
);
1216 WRAP_API(bool, lxcapi_stop
)
1218 static int do_create_container_dir(const char *path
, struct lxc_conf
*conf
)
1220 __do_free
char *p
= NULL
;
1224 mode_t mask
= umask(0002);
1225 ret
= mkdir(path
, 0770);
1230 if (errno
!= EEXIST
)
1236 p
= must_copy_string(path
);
1238 if (!lxc_list_empty(&conf
->id_map
)) {
1239 ret
= chown_mapped_root(p
, conf
);
1247 /* Create the standard expected container dir. */
1248 static bool create_container_dir(struct lxc_container
*c
)
1254 len
= strlen(c
->config_path
) + strlen(c
->name
) + 2;
1259 ret
= snprintf(s
, len
, "%s/%s", c
->config_path
, c
->name
);
1260 if (ret
< 0 || (size_t)ret
>= len
) {
1265 ret
= do_create_container_dir(s
, c
->lxc_conf
);
1271 /* do_storage_create: thin wrapper around storage_create(). Like
1272 * storage_create(), it returns a mounted bdev on success, NULL on error.
1274 static struct lxc_storage
*do_storage_create(struct lxc_container
*c
,
1276 struct bdev_specs
*specs
)
1278 __do_free
char *dest
= NULL
;
1281 struct lxc_storage
*bdev
;
1283 /* rootfs.path or lxcpath/lxcname/rootfs */
1284 if (c
->lxc_conf
->rootfs
.path
&&
1285 (access(c
->lxc_conf
->rootfs
.path
, F_OK
) == 0)) {
1286 const char *rpath
= c
->lxc_conf
->rootfs
.path
;
1287 len
= strlen(rpath
) + 1;
1288 dest
= must_realloc(NULL
, len
);
1289 ret
= snprintf(dest
, len
, "%s", rpath
);
1291 const char *lxcpath
= do_lxcapi_get_config_path(c
);
1292 len
= strlen(c
->name
) + strlen(lxcpath
) + 9;
1293 dest
= must_realloc(NULL
, len
);
1294 ret
= snprintf(dest
, len
, "%s/%s/rootfs", lxcpath
, c
->name
);
1296 if (ret
< 0 || (size_t)ret
>= len
)
1299 bdev
= storage_create(dest
, type
, c
->name
, specs
);
1301 ERROR("Failed to create \"%s\" storage", type
);
1305 if (!c
->set_config_item(c
, "lxc.rootfs.path", bdev
->src
)) {
1306 ERROR("Failed to set \"lxc.rootfs.path = %s\"", bdev
->src
);
1311 /* If we are not root, chown the rootfs dir to root in the target user
1315 if (ret
!= 0 || (c
->lxc_conf
&& !lxc_list_empty(&c
->lxc_conf
->id_map
))) {
1316 ret
= chown_mapped_root(bdev
->dest
, c
->lxc_conf
);
1318 ERROR("Error chowning \"%s\" to container root", bdev
->dest
);
1319 suggest_default_idmap();
1328 /* Strip path and return name of file for argv[0] passed to execvp */
1329 static char *lxctemplatefilename(char *tpath
)
1333 p
= tpath
+ strlen(tpath
) - 1;
1334 while ( (p
-1) >= tpath
&& *(p
-1) != '/')
1340 static bool create_run_template(struct lxc_container
*c
, char *tpath
,
1341 bool need_null_stdfds
, char *const argv
[])
1351 SYSERROR("Failed to fork task for container creation template");
1355 if (pid
== 0) { /* child */
1357 char *namearg
, *patharg
, *rootfsarg
;
1360 struct lxc_storage
*bdev
= NULL
;
1361 struct lxc_conf
*conf
= c
->lxc_conf
;
1364 if (need_null_stdfds
) {
1365 ret
= null_stdfds();
1367 _exit(EXIT_FAILURE
);
1370 bdev
= storage_init(c
->lxc_conf
);
1372 ERROR("Failed to initialize storage");
1373 _exit(EXIT_FAILURE
);
1378 ret
= unshare(CLONE_NEWNS
);
1380 ERROR("Failed to unshare CLONE_NEWNS");
1381 _exit(EXIT_FAILURE
);
1384 ret
= detect_shared_rootfs();
1386 ret
= mount(NULL
, "/", NULL
, MS_SLAVE
| MS_REC
, NULL
);
1388 SYSERROR("Failed to make \"/\" rslave");
1389 ERROR("Continuing...");
1394 if (strcmp(bdev
->type
, "dir") != 0 && strcmp(bdev
->type
, "btrfs") != 0) {
1396 ERROR("Unprivileged users can only create "
1397 "btrfs and directory-backed containers");
1398 _exit(EXIT_FAILURE
);
1401 if (strcmp(bdev
->type
, "overlay") == 0 ||
1402 strcmp(bdev
->type
, "overlayfs") == 0) {
1403 /* If we create an overlay container we need to
1404 * rsync the contents into
1405 * <container-path>/<container-name>/rootfs.
1406 * However, the overlay mount function will
1408 * <container-path>/<container-name>/delta0
1410 * <container-path>/<container-name>/rootfs
1411 * which means we would rsync the rootfs into
1412 * the delta directory. That doesn't make sense
1413 * since the delta directory only exists to
1414 * record the differences to
1415 * <container-path>/<container-name>/rootfs. So
1416 * let's simply bind-mount here and then rsync
1418 * <container-path>/<container-name>/rootfs.
1422 src
= ovl_get_rootfs(bdev
->src
, &(size_t){0});
1424 ERROR("Failed to get rootfs");
1425 _exit(EXIT_FAILURE
);
1428 ret
= mount(src
, bdev
->dest
, "bind", MS_BIND
| MS_REC
, NULL
);
1430 ERROR("Failed to mount rootfs");
1431 _exit(EXIT_FAILURE
);
1434 ret
= bdev
->ops
->mount(bdev
);
1436 ERROR("Failed to mount rootfs");
1437 _exit(EXIT_FAILURE
);
1440 } else { /* TODO come up with a better way here! */
1443 src
= lxc_storage_get_path(bdev
->src
, bdev
->type
);
1444 bdev
->dest
= strdup(src
);
1447 /* Create our new array, pre-pend the template name and base
1451 for (nargs
= 0; argv
[nargs
]; nargs
++)
1454 /* template, path, rootfs and name args */
1457 newargv
= malloc(nargs
* sizeof(*newargv
));
1459 _exit(EXIT_FAILURE
);
1460 newargv
[0] = lxctemplatefilename(tpath
);
1463 len
= strlen(c
->config_path
) + strlen(c
->name
) + strlen("--path=") + 2;
1464 patharg
= malloc(len
);
1466 _exit(EXIT_FAILURE
);
1468 ret
= snprintf(patharg
, len
, "--path=%s/%s", c
->config_path
, c
->name
);
1469 if (ret
< 0 || ret
>= len
)
1470 _exit(EXIT_FAILURE
);
1471 newargv
[1] = patharg
;
1474 len
= strlen("--name=") + strlen(c
->name
) + 1;
1475 namearg
= malloc(len
);
1477 _exit(EXIT_FAILURE
);
1479 ret
= snprintf(namearg
, len
, "--name=%s", c
->name
);
1480 if (ret
< 0 || ret
>= len
)
1481 _exit(EXIT_FAILURE
);
1482 newargv
[2] = namearg
;
1485 len
= strlen("--rootfs=") + 1 + strlen(bdev
->dest
);
1486 rootfsarg
= malloc(len
);
1488 _exit(EXIT_FAILURE
);
1490 ret
= snprintf(rootfsarg
, len
, "--rootfs=%s", bdev
->dest
);
1491 if (ret
< 0 || ret
>= len
)
1492 _exit(EXIT_FAILURE
);
1493 newargv
[3] = rootfsarg
;
1495 /* add passed-in args */
1497 for (i
= 4; i
< nargs
; i
++)
1498 newargv
[i
] = argv
[i
- 4];
1500 /* add trailing NULL */
1502 newargv
= realloc(newargv
, nargs
* sizeof(*newargv
));
1504 _exit(EXIT_FAILURE
);
1505 newargv
[nargs
- 1] = NULL
;
1507 /* If we're running the template in a mapped userns, then we
1508 * prepend the template command with: lxc-usernsexec <-m map1>
1509 * ... <-m mapn> -- and we append "--mapped-uid x", where x is
1510 * the mapped uid for our geteuid()
1512 if (!lxc_list_empty(&conf
->id_map
)) {
1513 int extraargs
, hostuid_mapped
, hostgid_mapped
;
1515 char txtuid
[20], txtgid
[20];
1516 struct lxc_list
*it
;
1520 n2
= malloc(n2args
* sizeof(*n2
));
1522 _exit(EXIT_FAILURE
);
1525 tpath
= "lxc-usernsexec";
1526 n2
[0] = "lxc-usernsexec";
1528 lxc_list_for_each(it
, &conf
->id_map
) {
1531 n2
= realloc(n2
, n2args
* sizeof(char *));
1533 _exit(EXIT_FAILURE
);
1535 n2
[n2args
- 2] = "-m";
1536 n2
[n2args
- 1] = malloc(200);
1537 if (!n2
[n2args
- 1])
1538 _exit(EXIT_FAILURE
);
1540 ret
= snprintf(n2
[n2args
- 1], 200, "%c:%lu:%lu:%lu",
1541 map
->idtype
== ID_TYPE_UID
? 'u' : 'g',
1542 map
->nsid
, map
->hostid
, map
->range
);
1543 if (ret
< 0 || ret
>= 200)
1544 _exit(EXIT_FAILURE
);
1547 hostuid_mapped
= mapped_hostid(geteuid(), conf
, ID_TYPE_UID
);
1548 extraargs
= hostuid_mapped
>= 0 ? 1 : 3;
1550 n2
= realloc(n2
, (nargs
+ n2args
+ extraargs
) * sizeof(char *));
1552 _exit(EXIT_FAILURE
);
1554 if (hostuid_mapped
< 0) {
1555 hostuid_mapped
= find_unmapped_nsid(conf
, ID_TYPE_UID
);
1556 n2
[n2args
++] = "-m";
1557 if (hostuid_mapped
< 0) {
1558 ERROR("Failed to find free uid to map");
1559 _exit(EXIT_FAILURE
);
1562 n2
[n2args
++] = malloc(200);
1563 if (!n2
[n2args
- 1]) {
1564 SYSERROR("out of memory");
1565 _exit(EXIT_FAILURE
);
1568 ret
= snprintf(n2
[n2args
- 1], 200, "u:%d:%d:1",
1569 hostuid_mapped
, geteuid());
1570 if (ret
< 0 || ret
>= 200)
1571 _exit(EXIT_FAILURE
);
1574 hostgid_mapped
= mapped_hostid(getegid(), conf
, ID_TYPE_GID
);
1575 extraargs
= hostgid_mapped
>= 0 ? 1 : 3;
1577 n2
= realloc(n2
, (nargs
+ n2args
+ extraargs
) * sizeof(char *));
1579 _exit(EXIT_FAILURE
);
1581 if (hostgid_mapped
< 0) {
1582 hostgid_mapped
= find_unmapped_nsid(conf
, ID_TYPE_GID
);
1583 n2
[n2args
++] = "-m";
1584 if (hostgid_mapped
< 0) {
1585 ERROR("Failed to find free gid to map");
1586 _exit(EXIT_FAILURE
);
1589 n2
[n2args
++] = malloc(200);
1590 if (!n2
[n2args
- 1]) {
1591 SYSERROR("out of memory");
1592 _exit(EXIT_FAILURE
);
1595 ret
= snprintf(n2
[n2args
- 1], 200, "g:%d:%d:1",
1596 hostgid_mapped
, getegid());
1597 if (ret
< 0 || ret
>= 200)
1598 _exit(EXIT_FAILURE
);
1601 n2
[n2args
++] = "--";
1603 for (i
= 0; i
< nargs
; i
++)
1604 n2
[i
+ n2args
] = newargv
[i
];
1607 /* Finally add "--mapped-uid $uid" to tell template what
1608 * to chown cached images to.
1611 n2
= realloc(n2
, n2args
* sizeof(char *));
1613 _exit(EXIT_FAILURE
);
1615 /* note n2[n2args-1] is NULL */
1616 n2
[n2args
- 5] = "--mapped-uid";
1618 ret
= snprintf(txtuid
, 20, "%d", hostuid_mapped
);
1619 if (ret
< 0 || ret
>= 20) {
1622 _exit(EXIT_FAILURE
);
1625 n2
[n2args
- 4] = txtuid
;
1626 n2
[n2args
- 3] = "--mapped-gid";
1628 ret
= snprintf(txtgid
, 20, "%d", hostgid_mapped
);
1629 if (ret
< 0 || ret
>= 20) {
1632 _exit(EXIT_FAILURE
);
1635 n2
[n2args
- 2] = txtgid
;
1636 n2
[n2args
- 1] = NULL
;
1641 execvp(tpath
, newargv
);
1642 SYSERROR("Failed to execute template %s", tpath
);
1643 _exit(EXIT_FAILURE
);
1646 ret
= wait_for_pid(pid
);
1648 ERROR("Failed to create container from template");
1655 static bool prepend_lxc_header(char *path
, const char *t
, char *const argv
[])
1664 unsigned char md_value
[EVP_MAX_MD_SIZE
];
1668 f
= fopen(path
, "r");
1672 ret
= fseek(f
, 0, SEEK_END
);
1681 ret
= fseek(f
, 0, SEEK_SET
);
1685 ret
= fseek(f
, 0, SEEK_SET
);
1690 contents
= malloc(flen
+ 1);
1694 len
= fread(contents
, 1, flen
, f
);
1696 goto out_free_contents
;
1698 contents
[flen
] = '\0';
1703 goto out_free_contents
;
1706 tpath
= get_template_path(t
);
1708 ERROR("Invalid template \"%s\" specified", t
);
1709 goto out_free_contents
;
1712 ret
= sha1sum_file(tpath
, md_value
, &md_len
);
1714 ERROR("Failed to get sha1sum of %s", tpath
);
1716 goto out_free_contents
;
1721 f
= fopen(path
, "w");
1723 SYSERROR("Reopening config for writing");
1728 fprintf(f
, "# Template used to create this container: %s\n", t
);
1730 fprintf(f
, "# Parameters passed to the template:");
1732 fprintf(f
, " %s", *argv
);
1739 fprintf(f
, "# Template script checksum (SHA-1): ");
1740 for (i
=0; i
<md_len
; i
++)
1741 fprintf(f
, "%02x", md_value
[i
]);
1744 fprintf(f
, "# For additional config options, please look at lxc.container.conf(5)\n");
1745 fprintf(f
, "\n# Uncomment the following line to support nesting containers:\n");
1746 fprintf(f
, "#lxc.include = " LXCTEMPLATECONFIG
"/nesting.conf\n");
1747 fprintf(f
, "# (Be aware this has security implications)\n\n");
1748 if (fwrite(contents
, 1, flen
, f
) != flen
) {
1749 SYSERROR("Writing original contents");
1769 SYSERROR("Error prepending header");
1776 static void lxcapi_clear_config(struct lxc_container
*c
)
1778 if (!c
|| !c
->lxc_conf
)
1781 lxc_conf_free(c
->lxc_conf
);
1785 #define do_lxcapi_clear_config(c) lxcapi_clear_config(c)
1789 * create a container with the given parameters.
1790 * @c: container to be created. It has the lxcpath, name, and a starting
1791 * configuration already set
1792 * @t: the template to execute to instantiate the root filesystem and
1793 * adjust the configuration.
1794 * @bdevtype: backing store type to use. If NULL, dir will be used.
1795 * @specs: additional parameters for the backing store, i.e. LVM vg to
1798 * @argv: the arguments to pass to the template, terminated by NULL. If no
1799 * arguments, you can just pass NULL.
1801 static bool do_lxcapi_create(struct lxc_container
*c
, const char *t
,
1802 const char *bdevtype
, struct bdev_specs
*specs
,
1803 int flags
, char *const argv
[])
1808 bool ret
= false, rootfs_managed
= true;
1815 tpath
= get_template_path(t
);
1817 ERROR("Unknown template \"%s\"", t
);
1822 /* If a template is passed in, and the rootfs already is defined in the
1823 * container config and exists, then the caller is trying to create an
1824 * existing container. Return an error, but do NOT delete the container.
1826 if (do_lxcapi_is_defined(c
) && c
->lxc_conf
&& c
->lxc_conf
->rootfs
.path
&&
1827 access(c
->lxc_conf
->rootfs
.path
, F_OK
) == 0 && tpath
) {
1828 ERROR("Container \"%s\" already exists in \"%s\"", c
->name
,
1834 if (!do_lxcapi_load_config(c
, lxc_global_config_value("lxc.default_config"))) {
1835 ERROR("Error loading default configuration file %s",
1836 lxc_global_config_value("lxc.default_config"));
1841 if (!create_container_dir(c
))
1844 if (c
->lxc_conf
->rootfs
.path
)
1845 rootfs_managed
= false;
1847 /* If both template and rootfs.path are set, template is setup as
1848 * rootfs.path. The container is already created if we have a config and
1849 * rootfs.path is accessible
1851 if (!c
->lxc_conf
->rootfs
.path
&& !tpath
) {
1852 /* No template passed in and rootfs does not exist. */
1853 if (!c
->save_config(c
, NULL
)) {
1854 ERROR("Failed to save initial config for \"%s\"", c
->name
);
1861 /* Rootfs passed into configuration, but does not exist. */
1862 if (c
->lxc_conf
->rootfs
.path
&& access(c
->lxc_conf
->rootfs
.path
, F_OK
) != 0)
1865 if (do_lxcapi_is_defined(c
) && c
->lxc_conf
->rootfs
.path
&& !tpath
) {
1866 /* Rootfs already existed, user just wanted to save the loaded
1869 if (!c
->save_config(c
, NULL
))
1870 ERROR("Failed to save initial config for \"%s\"", c
->name
);
1876 /* Mark that this container is being created */
1877 partial_fd
= create_partial(c
);
1881 /* No need to get disk lock bc we have the partial lock. */
1885 /* Create the storage.
1886 * Note we can't do this in the same task as we use to execute the
1887 * template because of the way zfs works.
1888 * After you 'zfs create', zfs mounts the fs only in the initial
1893 SYSERROR("Failed to fork task for container creation template");
1897 if (pid
== 0) { /* child */
1898 struct lxc_storage
*bdev
= NULL
;
1900 bdev
= do_storage_create(c
, bdevtype
, specs
);
1902 ERROR("Failed to create %s storage for %s",
1903 bdevtype
? bdevtype
: "(none)", c
->name
);
1904 _exit(EXIT_FAILURE
);
1907 /* Save config file again to store the new rootfs location. */
1908 if (!do_lxcapi_save_config(c
, NULL
)) {
1909 ERROR("Failed to save initial config for %s", c
->name
);
1910 /* Parent task won't see the storage driver in the
1911 * config so we delete it.
1913 bdev
->ops
->umount(bdev
);
1914 bdev
->ops
->destroy(bdev
);
1915 _exit(EXIT_FAILURE
);
1918 _exit(EXIT_SUCCESS
);
1921 if (wait_for_pid(pid
) != 0)
1924 /* Reload config to get the rootfs. */
1925 lxc_conf_free(c
->lxc_conf
);
1928 if (!load_config_locked(c
, c
->configfile
))
1931 if (!create_run_template(c
, tpath
, !!(flags
& LXC_CREATE_QUIET
), argv
))
1934 /* Now clear out the lxc_conf we have, reload from the created
1937 do_lxcapi_clear_config(c
);
1940 if (!prepend_lxc_header(c
->configfile
, tpath
, argv
)) {
1941 ERROR("Failed to prepend header to config file");
1946 ret
= load_config_locked(c
, c
->configfile
);
1950 remove_partial(c
, partial_fd
);
1954 bool reset_managed
= c
->lxc_conf
->rootfs
.managed
;
1957 * Ensure that we don't destroy storage we didn't create
1960 if (!rootfs_managed
)
1961 c
->lxc_conf
->rootfs
.managed
= false;
1962 container_destroy(c
, NULL
);
1963 c
->lxc_conf
->rootfs
.managed
= reset_managed
;
1971 static bool lxcapi_create(struct lxc_container
*c
, const char *t
,
1972 const char *bdevtype
, struct bdev_specs
*specs
,
1973 int flags
, char *const argv
[])
1977 current_config
= c
? c
->lxc_conf
: NULL
;
1979 ret
= do_lxcapi_create(c
, t
, bdevtype
, specs
, flags
, argv
);
1980 current_config
= NULL
;
1984 static bool do_lxcapi_reboot(struct lxc_container
*c
)
1988 int rebootsignal
= SIGINT
;
1993 if (!do_lxcapi_is_running(c
))
1996 pid
= do_lxcapi_init_pid(c
);
2000 if (c
->lxc_conf
&& c
->lxc_conf
->rebootsignal
)
2001 rebootsignal
= c
->lxc_conf
->rebootsignal
;
2003 ret
= kill(pid
, rebootsignal
);
2005 WARN("Failed to send signal %d to pid %d", rebootsignal
, pid
);
2012 WRAP_API(bool, lxcapi_reboot
)
2014 static bool do_lxcapi_reboot2(struct lxc_container
*c
, int timeout
)
2018 int rebootsignal
= SIGINT
, state_client_fd
= -1;
2019 lxc_state_t states
[MAX_STATE
] = {0};
2024 if (!do_lxcapi_is_running(c
))
2027 pid
= do_lxcapi_init_pid(c
);
2031 if (c
->lxc_conf
&& c
->lxc_conf
->rebootsignal
)
2032 rebootsignal
= c
->lxc_conf
->rebootsignal
;
2034 /* Add a new state client before sending the shutdown signal so that we
2035 * don't miss a state.
2038 states
[RUNNING
] = 2;
2039 ret
= lxc_cmd_add_state_client(c
->name
, c
->config_path
, states
,
2044 if (state_client_fd
< 0)
2050 if (ret
< MAX_STATE
)
2054 /* Send reboot signal to container. */
2055 killret
= kill(pid
, rebootsignal
);
2057 if (state_client_fd
>= 0)
2058 close(state_client_fd
);
2060 WARN("Failed to send signal %d to pid %d", rebootsignal
, pid
);
2063 TRACE("Sent signal %d to pid %d", rebootsignal
, pid
);
2068 ret
= lxc_cmd_sock_rcv_state(state_client_fd
, timeout
);
2069 close(state_client_fd
);
2073 TRACE("Received state \"%s\"", lxc_state2str(ret
));
2080 WRAP_API_1(bool, lxcapi_reboot2
, int)
2082 static bool do_lxcapi_shutdown(struct lxc_container
*c
, int timeout
)
2086 int haltsignal
= SIGPWR
, state_client_fd
= -EBADF
;
2087 lxc_state_t states
[MAX_STATE
] = {0};
2092 if (!do_lxcapi_is_running(c
))
2095 pid
= do_lxcapi_init_pid(c
);
2099 /* Detect whether we should send SIGRTMIN + 3 (e.g. systemd). */
2100 if (c
->lxc_conf
&& c
->lxc_conf
->haltsignal
)
2101 haltsignal
= c
->lxc_conf
->haltsignal
;
2102 else if (task_blocks_signal(pid
, (SIGRTMIN
+ 3)))
2103 haltsignal
= (SIGRTMIN
+ 3);
2105 /* Add a new state client before sending the shutdown signal so that we
2106 * don't miss a state.
2109 states
[STOPPED
] = 1;
2110 ret
= lxc_cmd_add_state_client(c
->name
, c
->config_path
, states
,
2115 if (state_client_fd
< 0)
2121 if (ret
< MAX_STATE
)
2125 /* Send shutdown signal to container. */
2126 killret
= kill(pid
, haltsignal
);
2128 if (state_client_fd
>= 0)
2129 close(state_client_fd
);
2131 WARN("Failed to send signal %d to pid %d", haltsignal
, pid
);
2134 TRACE("Sent signal %d to pid %d", haltsignal
, pid
);
2139 ret
= lxc_cmd_sock_rcv_state(state_client_fd
, timeout
);
2140 close(state_client_fd
);
2144 TRACE("Received state \"%s\"", lxc_state2str(ret
));
2151 WRAP_API_1(bool, lxcapi_shutdown
, int)
2153 static bool lxcapi_createl(struct lxc_container
*c
, const char *t
,
2154 const char *bdevtype
, struct bdev_specs
*specs
, int flags
, ...)
2163 current_config
= c
->lxc_conf
;
2166 * since we're going to wait for create to finish, I don't think we
2167 * need to get a copy of the arguments.
2169 va_start(ap
, flags
);
2170 args
= lxc_va_arg_list_to_argv(ap
, 0, 0);
2173 ERROR("Failed to allocate memory");
2177 bret
= do_lxcapi_create(c
, t
, bdevtype
, specs
, flags
, args
);
2181 current_config
= NULL
;
2185 static void do_clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
)
2187 if (!strcmp(key
, "lxc.cgroup"))
2188 return clear_unexp_config_line(conf
, key
, true);
2190 if (!strcmp(key
, "lxc.network"))
2191 return clear_unexp_config_line(conf
, key
, true);
2193 if (!strcmp(key
, "lxc.net"))
2194 return clear_unexp_config_line(conf
, key
, true);
2196 /* Clear a network with a specific index. */
2197 if (!strncmp(key
, "lxc.net.", 8)) {
2202 ret
= lxc_safe_uint(idx
, &(unsigned int){0});
2204 return clear_unexp_config_line(conf
, key
, true);
2207 if (!strcmp(key
, "lxc.hook"))
2208 return clear_unexp_config_line(conf
, key
, true);
2210 return clear_unexp_config_line(conf
, key
, false);
2213 static bool do_lxcapi_clear_config_item(struct lxc_container
*c
,
2217 struct lxc_config_t
*config
;
2219 if (!c
|| !c
->lxc_conf
)
2222 if (container_mem_lock(c
))
2225 config
= lxc_get_config(key
);
2226 /* Verify that the config key exists and that it has a callback
2229 if (config
&& config
->clr
)
2230 ret
= config
->clr(key
, c
->lxc_conf
, NULL
);
2233 do_clear_unexp_config_line(c
->lxc_conf
, key
);
2235 container_mem_unlock(c
);
2239 WRAP_API_1(bool, lxcapi_clear_config_item
, const char *)
2241 static inline bool enter_net_ns(struct lxc_container
*c
)
2243 pid_t pid
= do_lxcapi_init_pid(c
);
2248 if ((geteuid() != 0 || (c
->lxc_conf
&& !lxc_list_empty(&c
->lxc_conf
->id_map
))) &&
2249 (access("/proc/self/ns/user", F_OK
) == 0))
2250 if (!switch_to_ns(pid
, "user"))
2253 return switch_to_ns(pid
, "net");
2256 /* Used by qsort and bsearch functions for comparing names. */
2257 static inline int string_cmp(char **first
, char **second
)
2259 return strcmp(*first
, *second
);
2262 /* Used by qsort and bsearch functions for comparing container names. */
2263 static inline int container_cmp(struct lxc_container
**first
,
2264 struct lxc_container
**second
)
2266 return strcmp((*first
)->name
, (*second
)->name
);
2269 static bool add_to_array(char ***names
, char *cname
, int pos
)
2271 char **newnames
= realloc(*names
, (pos
+1) * sizeof(char *));
2273 ERROR("Out of memory");
2278 newnames
[pos
] = strdup(cname
);
2282 /* Sort the array as we will use binary search on it. */
2283 qsort(newnames
, pos
+ 1, sizeof(char *),
2284 (int (*)(const void *, const void *))string_cmp
);
2289 static bool add_to_clist(struct lxc_container
***list
, struct lxc_container
*c
,
2292 struct lxc_container
**newlist
= realloc(*list
, (pos
+ 1) * sizeof(struct lxc_container
*));
2294 ERROR("Out of memory");
2301 /* Sort the array as we will use binary search on it. */
2303 qsort(newlist
, pos
+ 1, sizeof(struct lxc_container
*),
2304 (int (*)(const void *, const void *))container_cmp
);
2309 static char** get_from_array(char ***names
, char *cname
, int size
)
2311 return (char **)bsearch(&cname
, *names
, size
, sizeof(char *), (int (*)(const void *, const void *))string_cmp
);
2314 static bool array_contains(char ***names
, char *cname
, int size
)
2316 if(get_from_array(names
, cname
, size
) != NULL
)
2322 static bool remove_from_array(char ***names
, char *cname
, int size
)
2324 char **result
= get_from_array(names
, cname
, size
);
2325 if (result
!= NULL
) {
2333 static char **do_lxcapi_get_interfaces(struct lxc_container
*c
)
2336 int i
, count
= 0, pipefd
[2];
2337 char **interfaces
= NULL
;
2338 char interface
[IFNAMSIZ
];
2340 if (pipe2(pipefd
, O_CLOEXEC
) < 0)
2345 SYSERROR("Failed to fork task to get interfaces information");
2351 if (pid
== 0) { /* child */
2352 int ret
= 1, nbytes
;
2353 struct netns_ifaddrs
*interfaceArray
= NULL
, *tempIfAddr
= NULL
;
2355 /* close the read-end of the pipe */
2358 if (!enter_net_ns(c
)) {
2359 SYSERROR("Failed to enter network namespace");
2363 /* Grab the list of interfaces */
2364 if (netns_getifaddrs(&interfaceArray
, -1, &(bool){false})) {
2365 SYSERROR("Failed to get interfaces list");
2369 /* Iterate through the interfaces */
2370 for (tempIfAddr
= interfaceArray
; tempIfAddr
!= NULL
;
2371 tempIfAddr
= tempIfAddr
->ifa_next
) {
2372 nbytes
= lxc_write_nointr(pipefd
[1], tempIfAddr
->ifa_name
, IFNAMSIZ
);
2383 netns_freeifaddrs(interfaceArray
);
2385 /* close the write-end of the pipe, thus sending EOF to the reader */
2390 /* close the write-end of the pipe */
2393 while (lxc_read_nointr(pipefd
[0], &interface
, IFNAMSIZ
) == IFNAMSIZ
) {
2394 interface
[IFNAMSIZ
- 1] = '\0';
2396 if (array_contains(&interfaces
, interface
, count
))
2399 if (!add_to_array(&interfaces
, interface
, count
))
2400 ERROR("Failed to add \"%s\" to array", interface
);
2405 if (wait_for_pid(pid
) != 0) {
2406 for (i
= 0; i
< count
; i
++)
2407 free(interfaces
[i
]);
2413 /* close the read-end of the pipe */
2416 /* Append NULL to the array */
2418 interfaces
= (char **)lxc_append_null_to_array((void **)interfaces
, count
);
2423 WRAP_API(char **, lxcapi_get_interfaces
)
2425 static char **do_lxcapi_get_ips(struct lxc_container
*c
, const char *interface
,
2426 const char *family
, int scope
)
2431 char address
[INET6_ADDRSTRLEN
];
2433 char **addresses
= NULL
;
2435 ret
= pipe2(pipefd
, O_CLOEXEC
);
2437 SYSERROR("Failed to create pipe");
2443 SYSERROR("Failed to create new process");
2451 char addressOutputBuffer
[INET6_ADDRSTRLEN
];
2452 char *address_ptr
= NULL
;
2453 void *tempAddrPtr
= NULL
;
2454 struct netns_ifaddrs
*interfaceArray
= NULL
, *tempIfAddr
= NULL
;
2456 /* close the read-end of the pipe */
2459 if (!enter_net_ns(c
)) {
2460 SYSERROR("Failed to attach to network namespace");
2464 /* Grab the list of interfaces */
2465 if (netns_getifaddrs(&interfaceArray
, -1, &(bool){false})) {
2466 SYSERROR("Failed to get interfaces list");
2470 /* Iterate through the interfaces */
2471 for (tempIfAddr
= interfaceArray
; tempIfAddr
;
2472 tempIfAddr
= tempIfAddr
->ifa_next
) {
2473 if (tempIfAddr
->ifa_addr
== NULL
)
2476 #pragma GCC diagnostic push
2477 #pragma GCC diagnostic ignored "-Wcast-align"
2479 if (tempIfAddr
->ifa_addr
->sa_family
== AF_INET
) {
2480 if (family
&& strcmp(family
, "inet"))
2483 tempAddrPtr
= &((struct sockaddr_in
*)tempIfAddr
->ifa_addr
)->sin_addr
;
2485 if (family
&& strcmp(family
, "inet6"))
2488 if (((struct sockaddr_in6
*)tempIfAddr
->ifa_addr
)->sin6_scope_id
!= scope
)
2491 tempAddrPtr
= &((struct sockaddr_in6
*)tempIfAddr
->ifa_addr
)->sin6_addr
;
2494 #pragma GCC diagnostic pop
2496 if (interface
&& strcmp(interface
, tempIfAddr
->ifa_name
))
2498 else if (!interface
&& strcmp("lo", tempIfAddr
->ifa_name
) == 0)
2501 address_ptr
= (char *)inet_ntop(tempIfAddr
->ifa_addr
->sa_family
,
2502 tempAddrPtr
, addressOutputBuffer
,
2503 sizeof(addressOutputBuffer
));
2507 nbytes
= lxc_write_nointr(pipefd
[1], address_ptr
, INET6_ADDRSTRLEN
);
2508 if (nbytes
!= INET6_ADDRSTRLEN
) {
2509 SYSERROR("Failed to send ipv6 address \"%s\"",
2521 netns_freeifaddrs(interfaceArray
);
2523 /* close the write-end of the pipe, thus sending EOF to the reader */
2528 /* close the write-end of the pipe */
2531 while (lxc_read_nointr(pipefd
[0], &address
, INET6_ADDRSTRLEN
) == INET6_ADDRSTRLEN
) {
2532 address
[INET6_ADDRSTRLEN
- 1] = '\0';
2534 if (!add_to_array(&addresses
, address
, count
))
2535 ERROR("PARENT: add_to_array failed");
2540 if (wait_for_pid(pid
) != 0) {
2541 for (i
= 0; i
< count
; i
++)
2548 /* close the read-end of the pipe */
2551 /* Append NULL to the array */
2553 addresses
= (char **)lxc_append_null_to_array((void **)addresses
, count
);
2558 WRAP_API_3(char **, lxcapi_get_ips
, const char *, const char *, int)
2560 static int do_lxcapi_get_config_item(struct lxc_container
*c
, const char *key
, char *retv
, int inlen
)
2563 struct lxc_config_t
*config
;
2565 if (!c
|| !c
->lxc_conf
)
2568 if (container_mem_lock(c
))
2571 config
= lxc_get_config(key
);
2572 /* Verify that the config key exists and that it has a callback
2575 if (config
&& config
->get
)
2576 ret
= config
->get(key
, retv
, inlen
, c
->lxc_conf
, NULL
);
2578 container_mem_unlock(c
);
2582 WRAP_API_3(int, lxcapi_get_config_item
, const char *, char *, int)
2584 static char* do_lxcapi_get_running_config_item(struct lxc_container
*c
, const char *key
)
2588 if (!c
|| !c
->lxc_conf
)
2591 if (container_mem_lock(c
))
2594 ret
= lxc_cmd_get_config_item(c
->name
, key
, do_lxcapi_get_config_path(c
));
2595 container_mem_unlock(c
);
2599 WRAP_API_1(char *, lxcapi_get_running_config_item
, const char *)
2601 static int do_lxcapi_get_keys(struct lxc_container
*c
, const char *key
, char *retv
, int inlen
)
2605 /* List all config items. */
2607 return lxc_list_config_items(retv
, inlen
);
2609 if (!c
|| !c
->lxc_conf
)
2612 if (container_mem_lock(c
))
2615 /* Support 'lxc.net.<idx>', i.e. 'lxc.net.0'
2616 * This is an intelligent result to show which keys are valid given the
2617 * type of nic it is.
2619 if (strncmp(key
, "lxc.net.", 8) == 0)
2620 ret
= lxc_list_net(c
->lxc_conf
, key
, retv
, inlen
);
2622 ret
= lxc_list_subkeys(c
->lxc_conf
, key
, retv
, inlen
);
2624 container_mem_unlock(c
);
2628 WRAP_API_3(int, lxcapi_get_keys
, const char *, char *, int)
2630 static bool do_lxcapi_save_config(struct lxc_container
*c
, const char *alt_file
)
2633 bool ret
= false, need_disklock
= false;
2636 alt_file
= c
->configfile
;
2641 /* If we haven't yet loaded a config, load the stock config. */
2643 if (!do_lxcapi_load_config(c
, lxc_global_config_value("lxc.default_config"))) {
2644 ERROR("Error loading default configuration file %s "
2646 lxc_global_config_value("lxc.default_config"),
2652 if (!create_container_dir(c
))
2655 /* If we're writing to the container's config file, take the disk lock.
2656 * Otherwise just take the memlock to protect the struct lxc_container
2657 * while we're traversing it.
2659 if (strcmp(c
->configfile
, alt_file
) == 0)
2660 need_disklock
= true;
2663 lret
= container_disk_lock(c
);
2665 lret
= container_mem_lock(c
);
2669 fd
= open(alt_file
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_CLOEXEC
,
2670 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2674 lret
= write_config(fd
, c
->lxc_conf
);
2683 container_disk_unlock(c
);
2685 container_mem_unlock(c
);
2690 WRAP_API_1(bool, lxcapi_save_config
, const char *)
2693 static bool mod_rdep(struct lxc_container
*c0
, struct lxc_container
*c
, bool inc
)
2699 char path
[PATH_MAX
];
2700 char newpath
[PATH_MAX
];
2701 int fd
, ret
, n
= 0, v
= 0;
2703 size_t len
= 0, bytes
= 0;
2705 if (container_disk_lock(c0
))
2708 ret
= snprintf(path
, PATH_MAX
, "%s/%s/lxc_snapshots", c0
->config_path
, c0
->name
);
2709 if (ret
< 0 || ret
> PATH_MAX
)
2712 ret
= snprintf(newpath
, PATH_MAX
, "%s\n%s\n", c
->config_path
, c
->name
);
2713 if (ret
< 0 || ret
> PATH_MAX
)
2716 /* If we find an lxc-snapshot file using the old format only listing the
2717 * number of snapshots we will keep using it. */
2718 f1
= fopen(path
, "r");
2720 n
= fscanf(f1
, "%d", &v
);
2722 if (n
== 1 && v
== 0) {
2725 SYSERROR("Failed to remove \"%s\"", path
);
2733 f1
= fopen(path
, "w");
2737 if (fprintf(f1
, "%d\n", v
) < 0) {
2738 ERROR("Error writing new snapshots value");
2745 SYSERROR("Error writing to or closing snapshots file");
2749 /* Here we know that we have or can use an lxc-snapshot file
2750 * using the new format. */
2752 f1
= fopen(path
, "a");
2756 if (fprintf(f1
, "%s", newpath
) < 0) {
2757 ERROR("Error writing new snapshots entry");
2760 SYSERROR("Error writing to or closing snapshots file");
2766 SYSERROR("Error writing to or closing snapshots file");
2770 if ((fd
= open(path
, O_RDWR
| O_CLOEXEC
)) < 0)
2773 if (fstat(fd
, &fbuf
) < 0) {
2778 if (fbuf
.st_size
!= 0) {
2779 buf
= lxc_strmmap(NULL
, fbuf
.st_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
2780 if (buf
== MAP_FAILED
) {
2781 SYSERROR("Failed to create mapping %s", path
);
2786 len
= strlen(newpath
);
2787 while ((del
= strstr((char *)buf
, newpath
))) {
2788 memmove(del
, del
+ len
, strlen(del
) - len
+ 1);
2792 lxc_strmunmap(buf
, fbuf
.st_size
);
2793 if (ftruncate(fd
, fbuf
.st_size
- bytes
) < 0) {
2794 SYSERROR("Failed to truncate file %s", path
);
2803 /* If the lxc-snapshot file is empty, remove it. */
2804 if (stat(path
, &fbuf
) < 0)
2807 if (!fbuf
.st_size
) {
2810 SYSERROR("Failed to remove \"%s\"", path
);
2817 container_disk_unlock(c0
);
2821 void mod_all_rdeps(struct lxc_container
*c
, bool inc
)
2823 struct lxc_container
*p
;
2824 char *lxcpath
= NULL
, *lxcname
= NULL
, path
[PATH_MAX
];
2825 size_t pathlen
= 0, namelen
= 0;
2829 ret
= snprintf(path
, PATH_MAX
, "%s/%s/lxc_rdepends",
2830 c
->config_path
, c
->name
);
2831 if (ret
< 0 || ret
>= PATH_MAX
) {
2832 ERROR("Path name too long");
2836 f
= fopen(path
, "r");
2840 while (getline(&lxcpath
, &pathlen
, f
) != -1) {
2841 if (getline(&lxcname
, &namelen
, f
) == -1) {
2842 ERROR("badly formatted file %s", path
);
2846 remove_trailing_newlines(lxcpath
);
2847 remove_trailing_newlines(lxcname
);
2849 if ((p
= lxc_container_new(lxcname
, lxcpath
)) == NULL
) {
2850 ERROR("Unable to find dependent container %s:%s",
2855 if (!mod_rdep(p
, c
, inc
))
2856 ERROR("Failed to update snapshots file for %s:%s",
2859 lxc_container_put(p
);
2868 static bool has_fs_snapshots(struct lxc_container
*c
)
2871 char path
[PATH_MAX
];
2876 ret
= snprintf(path
, PATH_MAX
, "%s/%s/lxc_snapshots", c
->config_path
,
2878 if (ret
< 0 || ret
> PATH_MAX
)
2881 /* If the file doesn't exist there are no snapshots. */
2882 if (stat(path
, &fbuf
) < 0)
2887 f
= fopen(path
, "r");
2891 ret
= fscanf(f
, "%d", &v
);
2893 /* TODO: Figure out what to do with the return value of fscanf. */
2895 INFO("Container uses new lxc-snapshots format %s", path
);
2904 static bool has_snapshots(struct lxc_container
*c
)
2906 char path
[PATH_MAX
];
2907 struct dirent
*direntp
;
2911 if (!get_snappath_dir(c
, path
))
2914 dir
= opendir(path
);
2918 while ((direntp
= readdir(dir
))) {
2919 if (!strcmp(direntp
->d_name
, "."))
2922 if (!strcmp(direntp
->d_name
, ".."))
2932 static bool do_destroy_container(struct lxc_conf
*conf
) {
2935 if (am_guest_unpriv()) {
2936 ret
= userns_exec_full(conf
, storage_destroy_wrapper
, conf
,
2937 "storage_destroy_wrapper");
2944 return storage_destroy(conf
);
2947 static int lxc_rmdir_onedev_wrapper(void *data
)
2949 char *arg
= (char *) data
;
2950 return lxc_rmdir_onedev(arg
, "snaps");
2953 static int lxc_unlink_exec_wrapper(void *data
)
2959 static bool container_destroy(struct lxc_container
*c
,
2960 struct lxc_storage
*storage
)
2964 struct lxc_conf
*conf
;
2969 if (!c
|| !do_lxcapi_is_defined(c
))
2973 if (container_disk_lock(c
))
2976 if (!is_stopped(c
)) {
2977 /* We should queue some sort of error - in c->error_string? */
2978 ERROR("container %s is not stopped", c
->name
);
2982 if (conf
&& !lxc_list_empty(&conf
->hooks
[LXCHOOK_DESTROY
])) {
2983 /* Start of environment variable setup for hooks */
2984 if (setenv("LXC_NAME", c
->name
, 1))
2985 SYSERROR("Failed to set environment variable for container name");
2987 if (conf
->rcfile
&& setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1))
2988 SYSERROR("Failed to set environment variable for config path");
2990 if (conf
->rootfs
.mount
&& setenv("LXC_ROOTFS_MOUNT", conf
->rootfs
.mount
, 1))
2991 SYSERROR("Failed to set environment variable for rootfs mount");
2993 if (conf
->rootfs
.path
&& setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1))
2994 SYSERROR("Failed to set environment variable for rootfs mount");
2996 if (conf
->console
.path
&& setenv("LXC_CONSOLE", conf
->console
.path
, 1))
2997 SYSERROR("Failed to set environment variable for console path");
2999 if (conf
->console
.log_path
&& setenv("LXC_CONSOLE_LOGPATH", conf
->console
.log_path
, 1))
3000 SYSERROR("Failed to set environment variable for console log");
3001 /* End of environment variable setup for hooks */
3003 if (run_lxc_hooks(c
->name
, "destroy", conf
, NULL
)) {
3004 ERROR("Failed to execute clone hook for \"%s\"", c
->name
);
3009 if (current_config
&& conf
== current_config
) {
3010 current_config
= NULL
;
3012 if (conf
->logfd
!= -1) {
3018 /* LXC is not managing the storage of the container. */
3019 if (conf
&& !conf
->rootfs
.managed
)
3022 if (conf
&& conf
->rootfs
.path
&& conf
->rootfs
.mount
) {
3023 if (!do_destroy_container(conf
)) {
3024 ERROR("Error destroying rootfs for %s", c
->name
);
3027 INFO("Destroyed rootfs for %s", c
->name
);
3030 mod_all_rdeps(c
, false);
3032 p1
= do_lxcapi_get_config_path(c
);
3041 * strlen("config") = 6
3045 len
= strlen(p1
) + 1 + strlen(c
->name
) + 1 + 6 + 1;
3048 ERROR("Failed to allocate memory");
3052 /* For an overlay container the rootfs is considered immutable and
3053 * cannot be removed when restoring from a snapshot.
3055 if (storage
&& (!strcmp(storage
->type
, "overlay") ||
3056 !strcmp(storage
->type
, "overlayfs")) &&
3057 (storage
->flags
& LXC_STORAGE_INTERNAL_OVERLAY_RESTORE
)) {
3058 ret
= snprintf(path
, len
, "%s/%s/config", p1
, c
->name
);
3059 if (ret
< 0 || (size_t)ret
>= len
)
3062 if (am_guest_unpriv())
3063 ret
= userns_exec_1(conf
, lxc_unlink_exec_wrapper
, path
,
3064 "lxc_unlink_exec_wrapper");
3068 SYSERROR("Failed to destroy config file \"%s\" for \"%s\"",
3072 INFO("Destroyed config file \"%s\" for \"%s\"", path
, c
->name
);
3078 ret
= snprintf(path
, len
, "%s/%s", p1
, c
->name
);
3079 if (ret
< 0 || (size_t)ret
>= len
)
3082 if (am_guest_unpriv())
3083 ret
= userns_exec_full(conf
, lxc_rmdir_onedev_wrapper
, path
,
3084 "lxc_rmdir_onedev_wrapper");
3086 ret
= lxc_rmdir_onedev(path
, "snaps");
3088 ERROR("Failed to destroy directory \"%s\" for \"%s\"", path
,
3092 INFO("Destroyed directory \"%s\" for \"%s\"", path
, c
->name
);
3101 container_disk_unlock(c
);
3105 static bool do_lxcapi_destroy(struct lxc_container
*c
)
3107 if (!c
|| !lxcapi_is_defined(c
))
3110 if (c
->lxc_conf
&& c
->lxc_conf
->rootfs
.managed
) {
3111 if (has_snapshots(c
)) {
3112 ERROR("Container %s has snapshots; not removing", c
->name
);
3116 if (has_fs_snapshots(c
)) {
3117 ERROR("container %s has snapshots on its rootfs", c
->name
);
3122 return container_destroy(c
, NULL
);
3125 WRAP_API(bool, lxcapi_destroy
)
3127 static bool do_lxcapi_destroy_with_snapshots(struct lxc_container
*c
)
3129 if (!c
|| !lxcapi_is_defined(c
))
3132 if (!lxcapi_snapshot_destroy_all(c
)) {
3133 ERROR("Error deleting all snapshots");
3137 return lxcapi_destroy(c
);
3140 WRAP_API(bool, lxcapi_destroy_with_snapshots
)
3142 int lxc_set_config_item_locked(struct lxc_conf
*conf
, const char *key
,
3146 struct lxc_config_t
*config
;
3149 config
= lxc_get_config(key
);
3153 ret
= config
->set(key
, v
, conf
, NULL
);
3157 if (lxc_config_value_empty(v
))
3158 do_clear_unexp_config_line(conf
, key
);
3160 bret
= do_append_unexp_config_line(conf
, key
, v
);
3167 static bool do_set_config_item_locked(struct lxc_container
*c
, const char *key
,
3173 c
->lxc_conf
= lxc_conf_init();
3178 ret
= lxc_set_config_item_locked(c
->lxc_conf
, key
, v
);
3185 static bool do_lxcapi_set_config_item(struct lxc_container
*c
, const char *key
, const char *v
)
3192 if (container_mem_lock(c
))
3195 b
= do_set_config_item_locked(c
, key
, v
);
3197 container_mem_unlock(c
);
3201 WRAP_API_2(bool, lxcapi_set_config_item
, const char *, const char *)
3203 static char *lxcapi_config_file_name(struct lxc_container
*c
)
3205 if (!c
|| !c
->configfile
)
3208 return strdup(c
->configfile
);
3211 static const char *lxcapi_get_config_path(struct lxc_container
*c
)
3213 if (!c
|| !c
->config_path
)
3216 return (const char *)(c
->config_path
);
3221 * Just recalculate the c->configfile based on the
3222 * c->config_path, which must be set.
3223 * The lxc_container must be locked or not yet public.
3225 static bool set_config_filename(struct lxc_container
*c
)
3230 if (!c
->config_path
)
3233 /* $lxc_path + "/" + c->name + "/" + "config" + '\0' */
3234 len
= strlen(c
->config_path
) + strlen(c
->name
) + strlen("config") + 3;
3235 newpath
= malloc(len
);
3239 ret
= snprintf(newpath
, len
, "%s/%s/config", c
->config_path
, c
->name
);
3240 if (ret
< 0 || ret
>= len
) {
3241 fprintf(stderr
, "Error printing out config file name\n");
3246 free(c
->configfile
);
3247 c
->configfile
= newpath
;
3252 static bool do_lxcapi_set_config_path(struct lxc_container
*c
, const char *path
)
3256 char *oldpath
= NULL
;
3261 if (container_mem_lock(c
))
3266 ERROR("Out of memory setting new lxc path");
3272 oldpath
= c
->config_path
;
3275 /* Since we've changed the config path, we have to change the
3276 * config file name too */
3277 if (!set_config_filename(c
)) {
3278 ERROR("Out of memory setting new config filename");
3280 free(c
->config_path
);
3281 c
->config_path
= oldpath
;
3287 container_mem_unlock(c
);
3291 WRAP_API_1(bool, lxcapi_set_config_path
, const char *)
3293 static bool do_lxcapi_set_cgroup_item(struct lxc_container
*c
, const char *subsys
, const char *value
)
3296 struct cgroup_ops
*cgroup_ops
;
3304 cgroup_ops
= cgroup_init(c
->lxc_conf
);
3308 ret
= cgroup_ops
->set(cgroup_ops
, subsys
, value
, c
->name
, c
->config_path
);
3310 cgroup_exit(cgroup_ops
);
3315 WRAP_API_2(bool, lxcapi_set_cgroup_item
, const char *, const char *)
3317 static int do_lxcapi_get_cgroup_item(struct lxc_container
*c
, const char *subsys
, char *retv
, int inlen
)
3320 struct cgroup_ops
*cgroup_ops
;
3328 cgroup_ops
= cgroup_init(c
->lxc_conf
);
3332 ret
= cgroup_ops
->get(cgroup_ops
, subsys
, retv
, inlen
, c
->name
,
3335 cgroup_exit(cgroup_ops
);
3340 WRAP_API_3(int, lxcapi_get_cgroup_item
, const char *, char *, int)
3342 const char *lxc_get_global_config_item(const char *key
)
3344 return lxc_global_config_value(key
);
3347 const char *lxc_get_version(void)
3352 static int copy_file(const char *old
, const char *new)
3359 if (file_exists(new)) {
3360 ERROR("copy destination %s exists", new);
3364 ret
= stat(old
, &sbuf
);
3366 INFO("Error stat'ing %s", old
);
3370 in
= open(old
, O_RDONLY
);
3372 SYSERROR("Error opening original file %s", old
);
3376 out
= open(new, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
3378 SYSERROR("Error opening new file %s", new);
3384 len
= lxc_read_nointr(in
, buf
, 8096);
3386 SYSERROR("Error reading old file %s", old
);
3393 ret
= lxc_write_nointr(out
, buf
, len
);
3394 if (ret
< len
) { /* should we retry? */
3395 SYSERROR("Error: write to new file %s was interrupted", new);
3403 /* We set mode, but not owner/group. */
3404 ret
= chmod(new, sbuf
.st_mode
);
3406 SYSERROR("Error setting mode on %s", new);
3418 static int copyhooks(struct lxc_container
*oldc
, struct lxc_container
*c
)
3420 __do_free
char *cpath
= NULL
;
3422 struct lxc_list
*it
;
3424 len
= strlen(oldc
->config_path
) + strlen(oldc
->name
) + 3;
3425 cpath
= must_realloc(NULL
, len
);
3426 ret
= snprintf(cpath
, len
, "%s/%s/", oldc
->config_path
, oldc
->name
);
3427 if (ret
< 0 || ret
>= len
)
3430 for (i
=0; i
<NUM_LXC_HOOKS
; i
++) {
3431 lxc_list_for_each(it
, &c
->lxc_conf
->hooks
[i
]) {
3432 char *hookname
= it
->elem
;
3433 char *fname
= strrchr(hookname
, '/');
3434 char tmppath
[PATH_MAX
];
3435 if (!fname
) /* relative path - we don't support, but maybe we should */
3438 if (strncmp(hookname
, cpath
, len
- 1) != 0) {
3439 /* this hook is public - ignore */
3443 /* copy the script, and change the entry in confile */
3444 ret
= snprintf(tmppath
, PATH_MAX
, "%s/%s/%s",
3445 c
->config_path
, c
->name
, fname
+1);
3446 if (ret
< 0 || ret
>= PATH_MAX
)
3449 ret
= copy_file(it
->elem
, tmppath
);
3455 it
->elem
= strdup(tmppath
);
3457 ERROR("out of memory copying hook path");
3463 if (!clone_update_unexp_hooks(c
->lxc_conf
, oldc
->config_path
,
3464 c
->config_path
, oldc
->name
, c
->name
)) {
3465 ERROR("Error saving new hooks in clone");
3469 do_lxcapi_save_config(c
, NULL
);
3474 static int copy_fstab(struct lxc_container
*oldc
, struct lxc_container
*c
)
3476 char newpath
[PATH_MAX
];
3477 char *oldpath
= oldc
->lxc_conf
->fstab
;
3483 clear_unexp_config_line(c
->lxc_conf
, "lxc.mount.fstab", false);
3485 char *p
= strrchr(oldpath
, '/');
3489 ret
= snprintf(newpath
, PATH_MAX
, "%s/%s%s",
3490 c
->config_path
, c
->name
, p
);
3491 if (ret
< 0 || ret
>= PATH_MAX
) {
3492 ERROR("error printing new path for %s", oldpath
);
3496 if (file_exists(newpath
)) {
3497 ERROR("error: fstab file %s exists", newpath
);
3501 if (copy_file(oldpath
, newpath
) < 0) {
3502 ERROR("error: copying %s to %s", oldpath
, newpath
);
3506 free(c
->lxc_conf
->fstab
);
3508 c
->lxc_conf
->fstab
= strdup(newpath
);
3509 if (!c
->lxc_conf
->fstab
) {
3510 ERROR("error: allocating pathname");
3514 if (!do_append_unexp_config_line(c
->lxc_conf
, "lxc.mount.fstab", newpath
)) {
3515 ERROR("error saving new lxctab");
3522 static void copy_rdepends(struct lxc_container
*c
, struct lxc_container
*c0
)
3524 char path0
[PATH_MAX
], path1
[PATH_MAX
];
3527 ret
= snprintf(path0
, PATH_MAX
, "%s/%s/lxc_rdepends", c0
->config_path
,
3529 if (ret
< 0 || ret
>= PATH_MAX
) {
3530 WARN("Error copying reverse dependencies");
3534 ret
= snprintf(path1
, PATH_MAX
, "%s/%s/lxc_rdepends", c
->config_path
,
3536 if (ret
< 0 || ret
>= PATH_MAX
) {
3537 WARN("Error copying reverse dependencies");
3541 if (copy_file(path0
, path1
) < 0) {
3542 INFO("Error copying reverse dependencies");
3547 static bool add_rdepends(struct lxc_container
*c
, struct lxc_container
*c0
)
3550 char path
[PATH_MAX
];
3554 ret
= snprintf(path
, PATH_MAX
, "%s/%s/lxc_rdepends", c
->config_path
,
3556 if (ret
< 0 || ret
>= PATH_MAX
)
3559 f
= fopen(path
, "a");
3565 /* If anything goes wrong, just return an error. */
3566 if (fprintf(f
, "%s\n%s\n", c0
->config_path
, c0
->name
) < 0)
3576 * If the fs natively supports snapshot clones with no penalty,
3577 * then default to those even if not requested.
3578 * Currently we only do this for btrfs.
3580 bool should_default_to_snapshot(struct lxc_container
*c0
,
3581 struct lxc_container
*c1
)
3583 __do_free
char *p0
= NULL
, *p1
= NULL
;
3585 size_t l0
= strlen(c0
->config_path
) + strlen(c0
->name
) + 2;
3586 size_t l1
= strlen(c1
->config_path
) + strlen(c1
->name
) + 2;
3587 char *rootfs
= c0
->lxc_conf
->rootfs
.path
;
3589 p0
= must_realloc(NULL
, l0
+ 1);
3590 p1
= must_realloc(NULL
, l1
+ 1);
3591 ret
= snprintf(p0
, l0
, "%s/%s", c0
->config_path
, c0
->name
);
3592 if (ret
< 0 || ret
>= l0
)
3595 ret
= snprintf(p1
, l1
, "%s/%s", c1
->config_path
, c1
->name
);
3596 if (ret
< 0 || ret
>= l1
)
3599 if (!is_btrfs_fs(p0
) || !is_btrfs_fs(p1
))
3602 if (is_btrfs_subvol(rootfs
) <= 0)
3605 return btrfs_same_fs(p0
, p1
) == 0;
3608 static int copy_storage(struct lxc_container
*c0
, struct lxc_container
*c
,
3609 const char *newtype
, int flags
, const char *bdevdata
,
3612 struct lxc_storage
*bdev
;
3615 if (should_default_to_snapshot(c0
, c
))
3616 flags
|= LXC_CLONE_SNAPSHOT
;
3618 bdev
= storage_copy(c0
, c
->name
, c
->config_path
, newtype
, flags
,
3619 bdevdata
, newsize
, &need_rdep
);
3621 ERROR("Error copying storage.");
3625 /* Set new rootfs. */
3626 free(c
->lxc_conf
->rootfs
.path
);
3627 c
->lxc_conf
->rootfs
.path
= strdup(bdev
->src
);
3630 if (!c
->lxc_conf
->rootfs
.path
) {
3631 ERROR("Out of memory while setting storage path.");
3635 /* Append a new lxc.rootfs.path entry to the unexpanded config. */
3636 clear_unexp_config_line(c
->lxc_conf
, "lxc.rootfs.path", false);
3637 if (!do_append_unexp_config_line(c
->lxc_conf
, "lxc.rootfs.path",
3638 c
->lxc_conf
->rootfs
.path
)) {
3639 ERROR("Error saving new rootfs to cloned config.");
3643 if (flags
& LXC_CLONE_SNAPSHOT
)
3644 copy_rdepends(c
, c0
);
3647 if (!add_rdepends(c
, c0
))
3648 WARN("Error adding reverse dependency from %s to %s",
3652 mod_all_rdeps(c
, true);
3657 struct clone_update_data
{
3658 struct lxc_container
*c0
;
3659 struct lxc_container
*c1
;
3664 static int clone_update_rootfs(struct clone_update_data
*data
)
3666 struct lxc_container
*c0
= data
->c0
;
3667 struct lxc_container
*c
= data
->c1
;
3668 int flags
= data
->flags
;
3669 char **hookargs
= data
->hookargs
;
3671 char path
[PATH_MAX
];
3672 struct lxc_storage
*bdev
;
3674 struct lxc_conf
*conf
= c
->lxc_conf
;
3676 /* update hostname in rootfs */
3677 /* we're going to mount, so run in a clean namespace to simplify cleanup */
3679 if (setgid(0) < 0) {
3680 ERROR("Failed to setgid to 0");
3684 if (setuid(0) < 0) {
3685 ERROR("Failed to setuid to 0");
3689 if (setgroups(0, NULL
) < 0)
3690 WARN("Failed to clear groups");
3692 if (unshare(CLONE_NEWNS
) < 0)
3695 bdev
= storage_init(c
->lxc_conf
);
3699 if (strcmp(bdev
->type
, "dir") != 0) {
3700 if (unshare(CLONE_NEWNS
) < 0) {
3701 ERROR("error unsharing mounts");
3706 if (detect_shared_rootfs()) {
3707 if (mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
)) {
3708 SYSERROR("Failed to make / rslave");
3709 ERROR("Continuing...");
3713 if (bdev
->ops
->mount(bdev
) < 0) {
3717 } else { /* TODO come up with a better way */
3719 bdev
->dest
= strdup(lxc_storage_get_path(bdev
->src
, bdev
->type
));
3722 if (!lxc_list_empty(&conf
->hooks
[LXCHOOK_CLONE
])) {
3723 /* Start of environment variable setup for hooks */
3724 if (c0
->name
&& setenv("LXC_SRC_NAME", c0
->name
, 1))
3725 SYSERROR("failed to set environment variable for source container name");
3727 if (setenv("LXC_NAME", c
->name
, 1))
3728 SYSERROR("failed to set environment variable for container name");
3730 if (conf
->rcfile
&& setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1))
3731 SYSERROR("failed to set environment variable for config path");
3733 if (bdev
->dest
&& setenv("LXC_ROOTFS_MOUNT", bdev
->dest
, 1))
3734 SYSERROR("failed to set environment variable for rootfs mount");
3736 if (conf
->rootfs
.path
&& setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1))
3737 SYSERROR("failed to set environment variable for rootfs mount");
3739 if (run_lxc_hooks(c
->name
, "clone", conf
, hookargs
)) {
3740 ERROR("Error executing clone hook for %s", c
->name
);
3746 if (!(flags
& LXC_CLONE_KEEPNAME
)) {
3747 ret
= snprintf(path
, PATH_MAX
, "%s/etc/hostname", bdev
->dest
);
3750 if (ret
< 0 || ret
>= PATH_MAX
)
3753 if (!file_exists(path
))
3756 if (!(fout
= fopen(path
, "w"))) {
3757 SYSERROR("unable to open %s: ignoring", path
);
3761 if (fprintf(fout
, "%s", c
->name
) < 0) {
3766 if (fclose(fout
) < 0)
3775 static int clone_update_rootfs_wrapper(void *data
)
3777 struct clone_update_data
*arg
= (struct clone_update_data
*) data
;
3778 return clone_update_rootfs(arg
);
3782 * We want to support:
3783 sudo lxc-clone -o o1 -n n1 -s -L|-fssize fssize -v|--vgname vgname \
3784 -p|--lvprefix lvprefix -t|--fstype fstype -B backingstore
3786 -s [ implies overlay]
3789 only rootfs gets converted (copied/snapshotted) on clone.
3792 static int create_file_dirname(char *path
, struct lxc_conf
*conf
)
3794 char *p
= strrchr(path
, '/');
3801 ret
= do_create_container_dir(path
, conf
);
3807 static struct lxc_container
*do_lxcapi_clone(struct lxc_container
*c
, const char *newname
,
3808 const char *lxcpath
, int flags
,
3809 const char *bdevtype
, const char *bdevdata
, uint64_t newsize
,
3812 char newpath
[PATH_MAX
];
3814 struct clone_update_data data
;
3815 size_t saved_unexp_len
;
3817 int storage_copied
= 0;
3818 char *origroot
= NULL
, *saved_unexp_conf
= NULL
;
3819 struct lxc_container
*c2
= NULL
;
3821 if (!c
|| !do_lxcapi_is_defined(c
))
3824 if (container_mem_lock(c
))
3826 if (!is_stopped(c
) && !(flags
& LXC_CLONE_ALLOW_RUNNING
)) {
3827 ERROR("error: Original container (%s) is running. Use --allowrunning if you want to force a snapshot of the running container.", c
->name
);
3831 /* Make sure the container doesn't yet exist. */
3836 lxcpath
= do_lxcapi_get_config_path(c
);
3838 ret
= snprintf(newpath
, PATH_MAX
, "%s/%s/config", lxcpath
, newname
);
3839 if (ret
< 0 || ret
>= PATH_MAX
) {
3840 SYSERROR("clone: failed making config pathname");
3844 if (file_exists(newpath
)) {
3845 ERROR("error: clone: %s exists", newpath
);
3849 ret
= create_file_dirname(newpath
, c
->lxc_conf
);
3850 if (ret
< 0 && errno
!= EEXIST
) {
3851 ERROR("Error creating container dir for %s", newpath
);
3855 /* Copy the configuration. Tweak it as needed. */
3856 if (c
->lxc_conf
->rootfs
.path
) {
3857 origroot
= c
->lxc_conf
->rootfs
.path
;
3858 c
->lxc_conf
->rootfs
.path
= NULL
;
3861 fd
= open(newpath
, O_WRONLY
| O_CREAT
| O_CLOEXEC
,
3862 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3864 SYSERROR("Failed to open \"%s\"", newpath
);
3868 saved_unexp_conf
= c
->lxc_conf
->unexpanded_config
;
3869 saved_unexp_len
= c
->lxc_conf
->unexpanded_len
;
3870 c
->lxc_conf
->unexpanded_config
= strdup(saved_unexp_conf
);
3871 if (!c
->lxc_conf
->unexpanded_config
) {
3876 clear_unexp_config_line(c
->lxc_conf
, "lxc.rootfs.path", false);
3877 write_config(fd
, c
->lxc_conf
);
3880 c
->lxc_conf
->rootfs
.path
= origroot
;
3882 free(c
->lxc_conf
->unexpanded_config
);
3883 c
->lxc_conf
->unexpanded_config
= saved_unexp_conf
;
3884 saved_unexp_conf
= NULL
;
3885 c
->lxc_conf
->unexpanded_len
= saved_unexp_len
;
3887 ret
= snprintf(newpath
, PATH_MAX
, "%s/%s/rootfs", lxcpath
, newname
);
3888 if (ret
< 0 || ret
>= PATH_MAX
) {
3889 SYSERROR("clone: failed making rootfs pathname");
3893 ret
= mkdir(newpath
, 0755);
3895 /* For an overlay container the rootfs is considered immutable
3896 * and will not have been removed when restoring from a
3899 if (errno
!= ENOENT
&&
3900 !(flags
& LXC_STORAGE_INTERNAL_OVERLAY_RESTORE
)) {
3901 SYSERROR("Failed to create directory \"%s\"", newpath
);
3906 if (am_guest_unpriv()) {
3907 if (chown_mapped_root(newpath
, c
->lxc_conf
) < 0) {
3908 ERROR("Error chowning %s to container root", newpath
);
3913 c2
= lxc_container_new(newname
, lxcpath
);
3915 ERROR("clone: failed to create new container (%s %s)", newname
,
3920 /* copy/snapshot rootfs's */
3921 ret
= copy_storage(c
, c2
, bdevtype
, flags
, bdevdata
, newsize
);
3925 /* update utsname */
3926 if (!(flags
& LXC_CLONE_KEEPNAME
)) {
3927 clear_unexp_config_line(c2
->lxc_conf
, "lxc.utsname", false);
3928 clear_unexp_config_line(c2
->lxc_conf
, "lxc.uts.name", false);
3930 if (!do_set_config_item_locked(c2
, "lxc.uts.name", newname
)) {
3931 ERROR("Error setting new hostname");
3937 ret
= copyhooks(c
, c2
);
3939 ERROR("error copying hooks");
3943 if (copy_fstab(c
, c2
) < 0) {
3944 ERROR("error copying fstab");
3948 /* update macaddrs */
3949 if (!(flags
& LXC_CLONE_KEEPMACADDR
)) {
3950 if (!network_new_hwaddrs(c2
->lxc_conf
)) {
3951 ERROR("Error updating mac addresses");
3956 /* Update absolute paths for overlay mount directories. */
3957 if (ovl_update_abs_paths(c2
->lxc_conf
, c
->config_path
, c
->name
, lxcpath
, newname
) < 0)
3960 /* We've now successfully created c2's storage, so clear it out if we
3965 if (!c2
->save_config(c2
, NULL
))
3968 if ((pid
= fork()) < 0) {
3974 ret
= wait_for_pid(pid
);
3978 container_mem_unlock(c
);
3985 data
.hookargs
= hookargs
;
3987 if (am_guest_unpriv())
3988 ret
= userns_exec_full(c
->lxc_conf
, clone_update_rootfs_wrapper
,
3989 &data
, "clone_update_rootfs_wrapper");
3991 ret
= clone_update_rootfs(&data
);
3993 _exit(EXIT_FAILURE
);
3995 container_mem_unlock(c
);
3996 _exit(EXIT_SUCCESS
);
3999 container_mem_unlock(c
);
4001 if (!storage_copied
)
4002 c2
->lxc_conf
->rootfs
.path
= NULL
;
4005 lxc_container_put(c2
);
4011 static struct lxc_container
*lxcapi_clone(struct lxc_container
*c
, const char *newname
,
4012 const char *lxcpath
, int flags
,
4013 const char *bdevtype
, const char *bdevdata
, uint64_t newsize
,
4016 struct lxc_container
* ret
;
4018 current_config
= c
? c
->lxc_conf
: NULL
;
4019 ret
= do_lxcapi_clone(c
, newname
, lxcpath
, flags
, bdevtype
, bdevdata
, newsize
, hookargs
);
4020 current_config
= NULL
;
4025 static bool do_lxcapi_rename(struct lxc_container
*c
, const char *newname
)
4027 struct lxc_storage
*bdev
;
4028 struct lxc_container
*newc
;
4030 if (!c
|| !c
->name
|| !c
->config_path
|| !c
->lxc_conf
)
4033 if (has_fs_snapshots(c
) || has_snapshots(c
)) {
4034 ERROR("Renaming a container with snapshots is not supported");
4038 bdev
= storage_init(c
->lxc_conf
);
4040 ERROR("Failed to find original backing store type");
4044 newc
= lxcapi_clone(c
, newname
, c
->config_path
, LXC_CLONE_KEEPMACADDR
, NULL
, bdev
->type
, 0, NULL
);
4047 lxc_container_put(newc
);
4051 if (newc
&& lxcapi_is_defined(newc
))
4052 lxc_container_put(newc
);
4054 if (!container_destroy(c
, NULL
)) {
4055 ERROR("Could not destroy existing container %s", c
->name
);
4062 WRAP_API_1(bool, lxcapi_rename
, const char *)
4064 static int lxcapi_attach(struct lxc_container
*c
,
4065 lxc_attach_exec_t exec_function
, void *exec_payload
,
4066 lxc_attach_options_t
*options
, pid_t
*attached_process
)
4073 current_config
= c
->lxc_conf
;
4075 ret
= lxc_attach(c
, exec_function
, exec_payload
, options
,
4077 current_config
= NULL
;
4081 static int do_lxcapi_attach_run_wait(struct lxc_container
*c
,
4082 lxc_attach_options_t
*options
,
4083 const char *program
,
4084 const char *const argv
[])
4086 lxc_attach_command_t command
;
4093 command
.program
= (char *)program
;
4094 command
.argv
= (char **)argv
;
4096 ret
= lxc_attach(c
, lxc_attach_run_command
, &command
, options
, &pid
);
4100 return lxc_wait_for_pid_status(pid
);
4103 static int lxcapi_attach_run_wait(struct lxc_container
*c
,
4104 lxc_attach_options_t
*options
,
4105 const char *program
, const char *const argv
[])
4109 current_config
= c
? c
->lxc_conf
: NULL
;
4110 ret
= do_lxcapi_attach_run_wait(c
, options
, program
, argv
);
4111 current_config
= NULL
;
4116 static int get_next_index(const char *lxcpath
, char *cname
)
4118 __do_free
char *fname
= NULL
;
4122 fname
= must_realloc(NULL
, strlen(lxcpath
) + 20);
4125 sprintf(fname
, "%s/snap%d", lxcpath
, i
);
4127 ret
= stat(fname
, &sb
);
4135 static bool get_snappath_dir(struct lxc_container
*c
, char *snappath
)
4140 * If the old style snapshot path exists, use it
4141 * /var/lib/lxc -> /var/lib/lxcsnaps
4143 ret
= snprintf(snappath
, PATH_MAX
, "%ssnaps", c
->config_path
);
4144 if (ret
< 0 || ret
>= PATH_MAX
)
4147 if (dir_exists(snappath
)) {
4148 ret
= snprintf(snappath
, PATH_MAX
, "%ssnaps/%s", c
->config_path
, c
->name
);
4149 if (ret
< 0 || ret
>= PATH_MAX
)
4156 * Use the new style path
4157 * /var/lib/lxc -> /var/lib/lxc + c->name + /snaps + \0
4159 ret
= snprintf(snappath
, PATH_MAX
, "%s/%s/snaps", c
->config_path
, c
->name
);
4160 if (ret
< 0 || ret
>= PATH_MAX
)
4166 static int do_lxcapi_snapshot(struct lxc_container
*c
, const char *commentfile
)
4168 __do_free
char *dfnam
= NULL
;
4172 struct lxc_container
*c2
;
4173 char snappath
[PATH_MAX
], newname
[20];
4177 if (!c
|| !lxcapi_is_defined(c
))
4180 if (!storage_can_backup(c
->lxc_conf
)) {
4181 ERROR("%s's backing store cannot be backed up", c
->name
);
4182 ERROR("Your container must use another backing store type");
4186 if (!get_snappath_dir(c
, snappath
))
4189 i
= get_next_index(snappath
, c
->name
);
4191 if (mkdir_p(snappath
, 0755) < 0) {
4192 ERROR("Failed to create snapshot directory %s", snappath
);
4196 ret
= snprintf(newname
, 20, "snap%d", i
);
4197 if (ret
< 0 || ret
>= 20)
4201 * We pass LXC_CLONE_SNAPSHOT to make sure that a rdepends file entry is
4202 * created in the original container
4204 flags
= LXC_CLONE_SNAPSHOT
| LXC_CLONE_KEEPMACADDR
| LXC_CLONE_KEEPNAME
|
4205 LXC_CLONE_KEEPBDEVTYPE
| LXC_CLONE_MAYBE_SNAPSHOT
;
4206 if (storage_is_dir(c
->lxc_conf
)) {
4207 ERROR("Snapshot of directory-backed container requested");
4208 ERROR("Making a copy-clone. If you do want snapshots, then");
4209 ERROR("please create overlay clone first, snapshot that");
4210 ERROR("and keep the original container pristine");
4211 flags
&= ~LXC_CLONE_SNAPSHOT
| LXC_CLONE_MAYBE_SNAPSHOT
;
4214 c2
= do_lxcapi_clone(c
, newname
, snappath
, flags
, NULL
, NULL
, 0, NULL
);
4216 ERROR("Failed to clone of %s:%s", c
->config_path
, c
->name
);
4220 lxc_container_put(c2
);
4222 /* Now write down the creation time. */
4225 if (!localtime_r(&timer
, &tm_info
)) {
4226 ERROR("Failed to get localtime");
4230 strftime(buffer
, 25, "%Y:%m:%d %H:%M:%S", &tm_info
);
4232 dfnam
= must_realloc(NULL
, strlen(snappath
) + strlen(newname
) + 5);
4233 sprintf(dfnam
, "%s/%s/ts", snappath
, newname
);
4234 f
= fopen(dfnam
, "w");
4236 ERROR("Failed to open %s", dfnam
);
4240 if (fprintf(f
, "%s", buffer
) < 0) {
4241 SYSERROR("Writing timestamp");
4248 SYSERROR("Writing timestamp");
4253 __do_free
char *path
= NULL
;
4254 /* $p / $name / comment \0 */
4255 int len
= strlen(snappath
) + strlen(newname
) + 10;
4257 path
= must_realloc(NULL
, len
);
4258 sprintf(path
, "%s/%s/comment", snappath
, newname
);
4259 return copy_file(commentfile
, path
) < 0 ? -1 : i
;
4265 WRAP_API_1(int, lxcapi_snapshot
, const char *)
4267 static void lxcsnap_free(struct lxc_snapshot
*s
)
4270 free(s
->comment_pathname
);
4275 static char *get_snapcomment_path(char* snappath
, char *name
)
4277 /* $snappath/$name/comment */
4278 int ret
, len
= strlen(snappath
) + strlen(name
) + 10;
4279 char *s
= malloc(len
);
4282 ret
= snprintf(s
, len
, "%s/%s/comment", snappath
, name
);
4283 if (ret
< 0 || ret
>= len
) {
4292 static char *get_timestamp(char* snappath
, char *name
)
4294 char path
[PATH_MAX
], *s
= NULL
;
4298 ret
= snprintf(path
, PATH_MAX
, "%s/%s/ts", snappath
, name
);
4299 if (ret
< 0 || ret
>= PATH_MAX
)
4302 fin
= fopen(path
, "r");
4306 (void) fseek(fin
, 0, SEEK_END
);
4308 (void) fseek(fin
, 0, SEEK_SET
);
4313 if (fread(s
, 1, len
, fin
) != len
) {
4314 SYSERROR("reading timestamp");
4325 static int do_lxcapi_snapshot_list(struct lxc_container
*c
, struct lxc_snapshot
**ret_snaps
)
4327 char snappath
[PATH_MAX
], path2
[PATH_MAX
];
4329 struct dirent
*direntp
;
4330 struct lxc_snapshot
*snaps
=NULL
, *nsnaps
;
4333 if (!c
|| !lxcapi_is_defined(c
))
4336 if (!get_snappath_dir(c
, snappath
)) {
4337 ERROR("path name too long");
4341 dir
= opendir(snappath
);
4343 INFO("Failed to open %s - assuming no snapshots", snappath
);
4347 while ((direntp
= readdir(dir
))) {
4348 if (!strcmp(direntp
->d_name
, "."))
4351 if (!strcmp(direntp
->d_name
, ".."))
4354 ret
= snprintf(path2
, PATH_MAX
, "%s/%s/config", snappath
, direntp
->d_name
);
4355 if (ret
< 0 || ret
>= PATH_MAX
) {
4356 ERROR("pathname too long");
4360 if (!file_exists(path2
))
4363 nsnaps
= realloc(snaps
, (count
+ 1)*sizeof(*snaps
));
4365 SYSERROR("Out of memory");
4370 snaps
[count
].free
= lxcsnap_free
;
4371 snaps
[count
].name
= strdup(direntp
->d_name
);
4372 if (!snaps
[count
].name
)
4375 snaps
[count
].lxcpath
= strdup(snappath
);
4376 if (!snaps
[count
].lxcpath
) {
4377 free(snaps
[count
].name
);
4381 snaps
[count
].comment_pathname
= get_snapcomment_path(snappath
, direntp
->d_name
);
4382 snaps
[count
].timestamp
= get_timestamp(snappath
, direntp
->d_name
);
4387 WARN("Failed to close directory");
4396 for (i
=0; i
<count
; i
++)
4397 lxcsnap_free(&snaps
[i
]);
4403 WARN("Failed to close directory");
4408 WRAP_API_1(int, lxcapi_snapshot_list
, struct lxc_snapshot
**)
4410 static bool do_lxcapi_snapshot_restore(struct lxc_container
*c
, const char *snapname
, const char *newname
)
4412 char clonelxcpath
[PATH_MAX
];
4414 struct lxc_container
*snap
, *rest
;
4415 struct lxc_storage
*bdev
;
4418 if (!c
|| !c
->name
|| !c
->config_path
)
4421 if (has_fs_snapshots(c
)) {
4422 ERROR("container rootfs has dependent snapshots");
4426 bdev
= storage_init(c
->lxc_conf
);
4428 ERROR("Failed to find original backing store type");
4432 /* For an overlay container the rootfs is considered immutable
4433 * and cannot be removed when restoring from a snapshot. We pass this
4434 * internal flag along to communicate this to various parts of the
4437 if (!strcmp(bdev
->type
, "overlay") || !strcmp(bdev
->type
, "overlayfs"))
4438 bdev
->flags
|= LXC_STORAGE_INTERNAL_OVERLAY_RESTORE
;
4443 if (!get_snappath_dir(c
, clonelxcpath
)) {
4447 /* how should we lock this? */
4449 snap
= lxc_container_new(snapname
, clonelxcpath
);
4450 if (!snap
|| !lxcapi_is_defined(snap
)) {
4451 ERROR("Could not open snapshot %s", snapname
);
4454 lxc_container_put(snap
);
4460 if (!strcmp(c
->name
, newname
)) {
4461 if (!container_destroy(c
, bdev
)) {
4462 ERROR("Could not destroy existing container %s", newname
);
4463 lxc_container_put(snap
);
4469 if (strcmp(bdev
->type
, "dir") != 0 && strcmp(bdev
->type
, "loop") != 0)
4470 flags
= LXC_CLONE_SNAPSHOT
| LXC_CLONE_MAYBE_SNAPSHOT
;
4472 if (!strcmp(bdev
->type
, "overlay") || !strcmp(bdev
->type
, "overlayfs"))
4473 flags
|= LXC_STORAGE_INTERNAL_OVERLAY_RESTORE
;
4475 rest
= lxcapi_clone(snap
, newname
, c
->config_path
, flags
, bdev
->type
,
4478 if (rest
&& lxcapi_is_defined(rest
))
4482 lxc_container_put(rest
);
4484 lxc_container_put(snap
);
4488 WRAP_API_2(bool, lxcapi_snapshot_restore
, const char *, const char *)
4490 static bool do_snapshot_destroy(const char *snapname
, const char *clonelxcpath
)
4492 struct lxc_container
*snap
= NULL
;
4495 snap
= lxc_container_new(snapname
, clonelxcpath
);
4497 ERROR("Could not find snapshot %s", snapname
);
4501 if (!do_lxcapi_destroy(snap
)) {
4502 ERROR("Could not destroy snapshot %s", snapname
);
4510 lxc_container_put(snap
);
4515 static bool remove_all_snapshots(const char *path
)
4518 struct dirent
*direntp
;
4521 dir
= opendir(path
);
4523 SYSERROR("opendir on snapshot path %s", path
);
4527 while ((direntp
= readdir(dir
))) {
4528 if (!strcmp(direntp
->d_name
, "."))
4531 if (!strcmp(direntp
->d_name
, ".."))
4534 if (!do_snapshot_destroy(direntp
->d_name
, path
)) {
4543 SYSERROR("Error removing directory %s", path
);
4548 static bool do_lxcapi_snapshot_destroy(struct lxc_container
*c
, const char *snapname
)
4550 char clonelxcpath
[PATH_MAX
];
4552 if (!c
|| !c
->name
|| !c
->config_path
|| !snapname
)
4555 if (!get_snappath_dir(c
, clonelxcpath
))
4558 return do_snapshot_destroy(snapname
, clonelxcpath
);
4561 WRAP_API_1(bool, lxcapi_snapshot_destroy
, const char *)
4563 static bool do_lxcapi_snapshot_destroy_all(struct lxc_container
*c
)
4565 char clonelxcpath
[PATH_MAX
];
4567 if (!c
|| !c
->name
|| !c
->config_path
)
4570 if (!get_snappath_dir(c
, clonelxcpath
))
4573 return remove_all_snapshots(clonelxcpath
);
4576 WRAP_API(bool, lxcapi_snapshot_destroy_all
)
4578 static bool do_lxcapi_may_control(struct lxc_container
*c
)
4583 return lxc_try_cmd(c
->name
, c
->config_path
) == 0;
4586 WRAP_API(bool, lxcapi_may_control
)
4588 static bool do_add_remove_node(pid_t init_pid
, const char *path
, bool add
,
4594 char chrootpath
[PATH_MAX
];
4595 char *directory_path
= NULL
;
4599 SYSERROR("Failed to fork()");
4604 ret
= wait_for_pid(pid
);
4606 ERROR("Failed to create device node");
4613 /* prepare the path */
4614 ret
= snprintf(chrootpath
, PATH_MAX
, "/proc/%d/root", init_pid
);
4615 if (ret
< 0 || ret
>= PATH_MAX
)
4618 ret
= chroot(chrootpath
);
4620 _exit(EXIT_FAILURE
);
4624 _exit(EXIT_FAILURE
);
4626 /* remove path if it exists */
4627 ret
= faccessat(AT_FDCWD
, path
, F_OK
, AT_SYMLINK_NOFOLLOW
);
4631 SYSERROR("Failed to remove \"%s\"", path
);
4632 _exit(EXIT_FAILURE
);
4637 _exit(EXIT_SUCCESS
);
4639 /* create any missing directories */
4642 _exit(EXIT_FAILURE
);
4644 directory_path
= dirname(tmp
);
4645 ret
= mkdir_p(directory_path
, 0755);
4646 if (ret
< 0 && errno
!= EEXIST
) {
4647 SYSERROR("Failed to create path \"%s\"", directory_path
);
4649 _exit(EXIT_FAILURE
);
4652 /* create the device node */
4653 ret
= mknod(path
, st
->st_mode
, st
->st_rdev
);
4656 SYSERROR("Failed to create device node at \"%s\"", path
);
4657 _exit(EXIT_FAILURE
);
4660 _exit(EXIT_SUCCESS
);
4663 static bool add_remove_device_node(struct lxc_container
*c
, const char *src_path
, const char *dest_path
, bool add
)
4667 char value
[LXC_MAX_BUFFER
];
4671 /* make sure container is running */
4672 if (!do_lxcapi_is_running(c
)) {
4673 ERROR("container is not running");
4677 /* use src_path if dest_path is NULL otherwise use dest_path */
4678 p
= dest_path
? dest_path
: src_path
;
4680 /* make sure we can access p */
4681 if(access(p
, F_OK
) < 0 || stat(p
, &st
) < 0)
4684 /* continue if path is character device or block device */
4685 if (S_ISCHR(st
.st_mode
))
4686 ret
= snprintf(value
, LXC_MAX_BUFFER
, "c %d:%d rwm", major(st
.st_rdev
), minor(st
.st_rdev
));
4687 else if (S_ISBLK(st
.st_mode
))
4688 ret
= snprintf(value
, LXC_MAX_BUFFER
, "b %d:%d rwm", major(st
.st_rdev
), minor(st
.st_rdev
));
4692 /* check snprintf return code */
4693 if (ret
< 0 || ret
>= LXC_MAX_BUFFER
)
4696 init_pid
= do_lxcapi_init_pid(c
);
4698 ERROR("Failed to get init pid");
4702 if (!do_add_remove_node(init_pid
, p
, add
, &st
))
4705 /* add or remove device to/from cgroup access list */
4707 if (!do_lxcapi_set_cgroup_item(c
, "devices.allow", value
)) {
4708 ERROR("set_cgroup_item failed while adding the device node");
4712 if (!do_lxcapi_set_cgroup_item(c
, "devices.deny", value
)) {
4713 ERROR("set_cgroup_item failed while removing the device node");
4721 static bool do_lxcapi_add_device_node(struct lxc_container
*c
, const char *src_path
, const char *dest_path
)
4723 // cannot mknod if we're not privileged wrt init_user_ns
4724 if (am_host_unpriv()) {
4725 ERROR(LXC_UNPRIV_EOPNOTSUPP
, __FUNCTION__
);
4729 return add_remove_device_node(c
, src_path
, dest_path
, true);
4732 WRAP_API_2(bool, lxcapi_add_device_node
, const char *, const char *)
4734 static bool do_lxcapi_remove_device_node(struct lxc_container
*c
, const char *src_path
, const char *dest_path
)
4736 if (am_guest_unpriv()) {
4737 ERROR(LXC_UNPRIV_EOPNOTSUPP
, __FUNCTION__
);
4741 return add_remove_device_node(c
, src_path
, dest_path
, false);
4744 WRAP_API_2(bool, lxcapi_remove_device_node
, const char *, const char *)
4746 static bool do_lxcapi_attach_interface(struct lxc_container
*c
,
4748 const char *dst_ifname
)
4753 if (am_guest_unpriv()) {
4754 ERROR(LXC_UNPRIV_EOPNOTSUPP
, __FUNCTION__
);
4759 ERROR("No source interface name given");
4763 ret
= lxc_netdev_isup(ifname
);
4765 /* netdev of ifname is up. */
4766 ret
= lxc_netdev_down(ifname
);
4771 init_pid
= do_lxcapi_init_pid(c
);
4773 ERROR("Failed to get init pid");
4777 ret
= lxc_netdev_move_by_name(ifname
, init_pid
, dst_ifname
);
4781 INFO("Moved network device \"%s\" to network namespace of %d", ifname
, init_pid
);
4788 WRAP_API_2(bool, lxcapi_attach_interface
, const char *, const char *)
4790 static bool do_lxcapi_detach_interface(struct lxc_container
*c
,
4792 const char *dst_ifname
)
4795 pid_t pid
, pid_outside
;
4798 * TODO - if this is a physical device, then we need am_host_unpriv.
4799 * But for other types guest privilege suffices.
4801 if (am_guest_unpriv()) {
4802 ERROR(LXC_UNPRIV_EOPNOTSUPP
, __FUNCTION__
);
4807 ERROR("No source interface name given");
4811 pid_outside
= lxc_raw_getpid();
4814 ERROR("Failed to fork");
4818 if (pid
== 0) { /* child */
4821 init_pid
= do_lxcapi_init_pid(c
);
4823 ERROR("Failed to get init pid");
4824 _exit(EXIT_FAILURE
);
4826 if (!switch_to_ns(init_pid
, "net")) {
4827 ERROR("Failed to enter network namespace");
4828 _exit(EXIT_FAILURE
);
4831 ret
= lxc_netdev_isup(ifname
);
4833 ERROR("Failed to determine whether network device \"%s\" is up", ifname
);
4834 _exit(EXIT_FAILURE
);
4837 /* netdev of ifname is up. */
4839 ret
= lxc_netdev_down(ifname
);
4841 ERROR("Failed to set network device \"%s\" down", ifname
);
4842 _exit(EXIT_FAILURE
);
4846 ret
= lxc_netdev_move_by_name(ifname
, pid_outside
, dst_ifname
);
4847 /* -EINVAL means there is no netdev named as ifname. */
4850 ERROR("Network device \"%s\" not found", ifname
);
4852 ERROR("Failed to remove network device \"%s\"", ifname
);
4854 _exit(EXIT_FAILURE
);
4857 _exit(EXIT_SUCCESS
);
4860 ret
= wait_for_pid(pid
);
4864 INFO("Moved network device \"%s\" to network namespace of %d", ifname
, pid_outside
);
4868 WRAP_API_2(bool, lxcapi_detach_interface
, const char *, const char *)
4870 static int do_lxcapi_migrate(struct lxc_container
*c
, unsigned int cmd
,
4871 struct migrate_opts
*opts
, unsigned int size
)
4874 struct migrate_opts
*valid_opts
= opts
;
4875 uint64_t features_to_check
= 0;
4877 /* If the caller has a bigger (newer) struct migrate_opts, let's make
4878 * sure that the stuff on the end is zero, i.e. that they didn't ask us
4879 * to do anything special.
4881 if (size
> sizeof(*opts
)) {
4882 unsigned char *addr
;
4885 addr
= (void *)opts
+ sizeof(*opts
);
4886 end
= (void *)opts
+ size
;
4888 for (; addr
< end
; addr
++)
4893 /* If the caller has a smaller struct, let's zero out the end for them
4894 * so we don't accidentally use bits of it that they didn't know about
4897 if (size
< sizeof(*opts
)) {
4898 valid_opts
= malloc(sizeof(*opts
));
4902 memset(valid_opts
, 0, sizeof(*opts
));
4903 memcpy(valid_opts
, opts
, size
);
4907 case MIGRATE_PRE_DUMP
:
4908 if (!do_lxcapi_is_running(c
)) {
4909 ERROR("container is not running");
4913 ret
= !__criu_pre_dump(c
, valid_opts
);
4916 if (!do_lxcapi_is_running(c
)) {
4917 ERROR("container is not running");
4921 ret
= !__criu_dump(c
, valid_opts
);
4923 case MIGRATE_RESTORE
:
4924 if (do_lxcapi_is_running(c
)) {
4925 ERROR("container is already running");
4929 ret
= !__criu_restore(c
, valid_opts
);
4931 case MIGRATE_FEATURE_CHECK
:
4932 features_to_check
= valid_opts
->features_to_check
;
4933 ret
= !__criu_check_feature(&features_to_check
);
4935 /* Something went wrong. Let's let the caller
4936 * know which feature checks failed. */
4937 valid_opts
->features_to_check
= features_to_check
;
4941 ERROR("invalid migrate command %u", cmd
);
4946 if (size
< sizeof(*opts
))
4952 WRAP_API_3(int, lxcapi_migrate
, unsigned int, struct migrate_opts
*, unsigned int)
4954 static bool do_lxcapi_checkpoint(struct lxc_container
*c
, char *directory
, bool stop
, bool verbose
)
4956 struct migrate_opts opts
;
4958 memset(&opts
, 0, sizeof(opts
));
4960 opts
.directory
= directory
;
4962 opts
.verbose
= verbose
;
4964 return !do_lxcapi_migrate(c
, MIGRATE_DUMP
, &opts
, sizeof(opts
));
4967 WRAP_API_3(bool, lxcapi_checkpoint
, char *, bool, bool)
4969 static bool do_lxcapi_restore(struct lxc_container
*c
, char *directory
, bool verbose
)
4971 struct migrate_opts opts
;
4973 memset(&opts
, 0, sizeof(opts
));
4975 opts
.directory
= directory
;
4976 opts
.verbose
= verbose
;
4978 return !do_lxcapi_migrate(c
, MIGRATE_RESTORE
, &opts
, sizeof(opts
));
4981 WRAP_API_2(bool, lxcapi_restore
, char *, bool)
4983 /* @st_mode is the st_mode field of the stat(source) return struct */
4984 static int create_mount_target(const char *dest
, mode_t st_mode
)
4986 char *dirdup
, *destdirname
;
4989 dirdup
= strdup(dest
);
4991 SYSERROR("Failed to duplicate target name \"%s\"", dest
);
4994 destdirname
= dirname(dirdup
);
4996 ret
= mkdir_p(destdirname
, 0755);
4998 SYSERROR("Failed to create \"%s\"", destdirname
);
5006 if (S_ISDIR(st_mode
))
5007 ret
= mkdir(dest
, 0000);
5009 ret
= mknod(dest
, S_IFREG
| 0000, 0);
5012 TRACE("Created mount target \"%s\"", dest
);
5013 else if (ret
< 0 && errno
!= EEXIST
) {
5014 SYSERROR("Failed to create mount target \"%s\"", dest
);
5021 static int do_lxcapi_mount(struct lxc_container
*c
, const char *source
,
5022 const char *target
, const char *filesystemtype
,
5023 unsigned long mountflags
, const void *data
,
5024 struct lxc_mount
*mnt
)
5027 char template[PATH_MAX
], path
[PATH_MAX
];
5028 pid_t pid
, init_pid
;
5031 int ret
= -1, fd
= -EBADF
;
5033 if (!c
|| !c
->lxc_conf
) {
5034 ERROR("Container or configuration is NULL");
5038 if (!c
->lxc_conf
->shmount
.path_host
) {
5039 ERROR("Host path to shared mountpoint must be specified in the config\n");
5043 ret
= snprintf(template, sizeof(template), "%s/.lxcmount_XXXXXX", c
->lxc_conf
->shmount
.path_host
);
5044 if (ret
< 0 || (size_t)ret
>= sizeof(template)) {
5045 SYSERROR("Error writing shmounts tempdir name");
5049 /* Create a temporary file / dir under the shared mountpoint */
5050 if (!source
|| strcmp(source
, "") == 0) {
5051 /* If source is not specified, maybe we want to mount a filesystem? */
5052 sb
.st_mode
= S_IFDIR
;
5054 ret
= stat(source
, &sb
);
5056 SYSERROR("Error getting stat info about the source \"%s\"", source
);
5061 is_dir
= (S_ISDIR(sb
.st_mode
) != 0);
5063 sret
= mkdtemp(template);
5065 SYSERROR("Could not create shmounts temporary dir");
5069 fd
= lxc_make_tmpfile(template, false);
5071 SYSERROR("Could not create shmounts temporary file");
5079 SYSERROR("Could not fork");
5085 ret
= mount(source
, template, filesystemtype
, mountflags
, data
);
5087 SYSERROR("Failed to mount onto \"%s\"", template);
5088 _exit(EXIT_FAILURE
);
5090 TRACE("Mounted \"%s\" onto \"%s\"", source
, template);
5092 init_pid
= do_lxcapi_init_pid(c
);
5094 ERROR("Failed to obtain container's init pid");
5095 _exit(EXIT_FAILURE
);
5098 /* Enter the container namespaces */
5099 if (!lxc_list_empty(&c
->lxc_conf
->id_map
)) {
5100 if (!switch_to_ns(init_pid
, "user")) {
5101 ERROR("Failed to enter user namespace");
5102 _exit(EXIT_FAILURE
);
5105 if (!lxc_switch_uid_gid(0, 0))
5106 _exit(EXIT_FAILURE
);
5109 if (!switch_to_ns(init_pid
, "mnt")) {
5110 ERROR("Failed to enter mount namespace");
5111 _exit(EXIT_FAILURE
);
5114 ret
= create_mount_target(target
, sb
.st_mode
);
5116 _exit(EXIT_FAILURE
);
5118 suff
= strrchr(template, '/');
5120 goto cleanup_target_in_child
;
5122 ret
= snprintf(path
, sizeof(path
), "%s%s", c
->lxc_conf
->shmount
.path_cont
, suff
);
5123 if (ret
< 0 || (size_t)ret
>= sizeof(path
)) {
5124 SYSERROR("Error writing container mountpoint name");
5125 goto cleanup_target_in_child
;
5128 ret
= mount(path
, target
, NULL
, MS_MOVE
| MS_REC
, NULL
);
5130 SYSERROR("Failed to move the mount from \"%s\" to \"%s\"", path
, target
);
5131 goto cleanup_target_in_child
;
5133 TRACE("Moved mount from \"%s\" to \"%s\"", path
, target
);
5135 _exit(EXIT_SUCCESS
);
5137 cleanup_target_in_child
:
5138 (void)remove(target
);
5139 _exit(EXIT_FAILURE
);
5142 ret
= wait_for_pid(pid
);
5144 SYSERROR("Wait for the child with pid %ld failed", (long)pid
);
5148 if (umount2(template, MNT_DETACH
))
5149 SYSWARN("Failed to remove temporary mount \"%s\"", template);
5152 (void)rmdir(template);
5154 (void)unlink(template);
5163 WRAP_API_6(int, lxcapi_mount
, const char *, const char *, const char *,
5164 unsigned long, const void *, struct lxc_mount
*)
5166 static int do_lxcapi_umount(struct lxc_container
*c
, const char *target
,
5167 unsigned long flags
, struct lxc_mount
*mnt
)
5169 pid_t pid
, init_pid
;
5172 if (!c
|| !c
->lxc_conf
) {
5173 ERROR("Container or configuration is NULL");
5180 SYSERROR("Could not fork");
5185 init_pid
= do_lxcapi_init_pid(c
);
5187 ERROR("Failed to obtain container's init pid");
5188 _exit(EXIT_FAILURE
);
5191 /* Enter the container namespaces */
5192 if (!lxc_list_empty(&c
->lxc_conf
->id_map
)) {
5193 if (!switch_to_ns(init_pid
, "user")) {
5194 ERROR("Failed to enter user namespace");
5195 _exit(EXIT_FAILURE
);
5199 if (!switch_to_ns(init_pid
, "mnt")) {
5200 ERROR("Failed to enter mount namespace");
5201 _exit(EXIT_FAILURE
);
5204 /* Do the unmount */
5205 ret
= umount2(target
, flags
);
5207 SYSERROR("Failed to umount \"%s\"", target
);
5208 _exit(EXIT_FAILURE
);
5211 _exit(EXIT_SUCCESS
);
5214 ret
= wait_for_pid(pid
);
5216 SYSERROR("Wait for the child with pid %ld failed", (long)pid
);
5223 WRAP_API_3(int, lxcapi_umount
, const char *, unsigned long, struct lxc_mount
*)
5225 static int lxcapi_attach_run_waitl(struct lxc_container
*c
, lxc_attach_options_t
*options
, const char *program
, const char *arg
, ...)
5234 current_config
= c
->lxc_conf
;
5237 argv
= lxc_va_arg_list_to_argv_const(ap
, 1);
5241 ERROR("Memory allocation error.");
5247 ret
= do_lxcapi_attach_run_wait(c
, options
, program
, (const char * const *)argv
);
5251 current_config
= NULL
;
5255 static int do_lxcapi_seccomp_notify_fd(struct lxc_container
*c
)
5257 if (!c
|| !c
->lxc_conf
)
5258 return minus_one_set_errno(-EINVAL
);
5260 return lxc_seccomp_get_notify_fd(&c
->lxc_conf
->seccomp
);
5263 WRAP_API(int, lxcapi_seccomp_notify_fd
)
5265 struct lxc_container
*lxc_container_new(const char *name
, const char *configpath
)
5267 struct lxc_container
*c
;
5274 c
= malloc(sizeof(*c
));
5276 fprintf(stderr
, "Failed to allocate memory for %s\n", name
);
5279 memset(c
, 0, sizeof(*c
));
5282 c
->config_path
= strdup(configpath
);
5284 c
->config_path
= strdup(lxc_global_config_value("lxc.lxcpath"));
5285 if (!c
->config_path
) {
5286 fprintf(stderr
, "Failed to allocate memory for %s\n", name
);
5290 remove_trailing_slashes(c
->config_path
);
5293 c
->name
= malloc(len
+ 1);
5295 fprintf(stderr
, "Failed to allocate memory for %s\n", name
);
5298 (void)strlcpy(c
->name
, name
, len
+ 1);
5301 c
->slock
= lxc_newlock(c
->config_path
, name
);
5303 fprintf(stderr
, "Failed to create lock for %s\n", name
);
5307 c
->privlock
= lxc_newlock(NULL
, NULL
);
5309 fprintf(stderr
, "Failed to create private lock for %s\n", name
);
5313 if (!set_config_filename(c
)) {
5314 fprintf(stderr
, "Failed to create config file name for %s\n", name
);
5318 if (file_exists(c
->configfile
) && !lxcapi_load_config(c
, NULL
)) {
5319 fprintf(stderr
, "Failed to load config for %s\n", name
);
5323 rc
= ongoing_create(c
);
5325 case LXC_CREATE_INCOMPLETE
:
5326 SYSERROR("Failed to complete container creation for %s", c
->name
);
5327 container_destroy(c
, NULL
);
5328 lxcapi_clear_config(c
);
5330 case LXC_CREATE_ONGOING
:
5331 /* container creation going on */
5333 case LXC_CREATE_FAILED
:
5334 /* container creation failed */
5335 if (errno
!= EACCES
&& errno
!= EPERM
) {
5336 /* insufficient privileges */
5337 SYSERROR("Failed checking for incomplete container %s creation", c
->name
);
5343 c
->daemonize
= true;
5346 /* Assign the member functions. */
5347 c
->is_defined
= lxcapi_is_defined
;
5348 c
->state
= lxcapi_state
;
5349 c
->is_running
= lxcapi_is_running
;
5350 c
->freeze
= lxcapi_freeze
;
5351 c
->unfreeze
= lxcapi_unfreeze
;
5352 c
->console
= lxcapi_console
;
5353 c
->console_getfd
= lxcapi_console_getfd
;
5354 c
->init_pid
= lxcapi_init_pid
;
5355 c
->load_config
= lxcapi_load_config
;
5356 c
->want_daemonize
= lxcapi_want_daemonize
;
5357 c
->want_close_all_fds
= lxcapi_want_close_all_fds
;
5358 c
->start
= lxcapi_start
;
5359 c
->startl
= lxcapi_startl
;
5360 c
->stop
= lxcapi_stop
;
5361 c
->config_file_name
= lxcapi_config_file_name
;
5362 c
->wait
= lxcapi_wait
;
5363 c
->set_config_item
= lxcapi_set_config_item
;
5364 c
->destroy
= lxcapi_destroy
;
5365 c
->destroy_with_snapshots
= lxcapi_destroy_with_snapshots
;
5366 c
->rename
= lxcapi_rename
;
5367 c
->save_config
= lxcapi_save_config
;
5368 c
->get_keys
= lxcapi_get_keys
;
5369 c
->create
= lxcapi_create
;
5370 c
->createl
= lxcapi_createl
;
5371 c
->shutdown
= lxcapi_shutdown
;
5372 c
->reboot
= lxcapi_reboot
;
5373 c
->reboot2
= lxcapi_reboot2
;
5374 c
->clear_config
= lxcapi_clear_config
;
5375 c
->clear_config_item
= lxcapi_clear_config_item
;
5376 c
->get_config_item
= lxcapi_get_config_item
;
5377 c
->get_running_config_item
= lxcapi_get_running_config_item
;
5378 c
->get_cgroup_item
= lxcapi_get_cgroup_item
;
5379 c
->set_cgroup_item
= lxcapi_set_cgroup_item
;
5380 c
->get_config_path
= lxcapi_get_config_path
;
5381 c
->set_config_path
= lxcapi_set_config_path
;
5382 c
->clone
= lxcapi_clone
;
5383 c
->get_interfaces
= lxcapi_get_interfaces
;
5384 c
->get_ips
= lxcapi_get_ips
;
5385 c
->attach
= lxcapi_attach
;
5386 c
->attach_run_wait
= lxcapi_attach_run_wait
;
5387 c
->attach_run_waitl
= lxcapi_attach_run_waitl
;
5388 c
->snapshot
= lxcapi_snapshot
;
5389 c
->snapshot_list
= lxcapi_snapshot_list
;
5390 c
->snapshot_restore
= lxcapi_snapshot_restore
;
5391 c
->snapshot_destroy
= lxcapi_snapshot_destroy
;
5392 c
->snapshot_destroy_all
= lxcapi_snapshot_destroy_all
;
5393 c
->may_control
= lxcapi_may_control
;
5394 c
->add_device_node
= lxcapi_add_device_node
;
5395 c
->remove_device_node
= lxcapi_remove_device_node
;
5396 c
->attach_interface
= lxcapi_attach_interface
;
5397 c
->detach_interface
= lxcapi_detach_interface
;
5398 c
->checkpoint
= lxcapi_checkpoint
;
5399 c
->restore
= lxcapi_restore
;
5400 c
->migrate
= lxcapi_migrate
;
5401 c
->console_log
= lxcapi_console_log
;
5402 c
->mount
= lxcapi_mount
;
5403 c
->umount
= lxcapi_umount
;
5404 c
->seccomp_notify_fd
= lxcapi_seccomp_notify_fd
;
5409 lxc_container_free(c
);
5413 int lxc_get_wait_states(const char **states
)
5418 for (i
=0; i
<MAX_STATE
; i
++)
5419 states
[i
] = lxc_state2str(i
);
5425 * These next two could probably be done smarter with reusing a common function
5426 * with different iterators and tests...
5428 int list_defined_containers(const char *lxcpath
, char ***names
, struct lxc_container
***cret
)
5431 int i
, cfound
= 0, nfound
= 0;
5432 struct dirent
*direntp
;
5433 struct lxc_container
*c
;
5436 lxcpath
= lxc_global_config_value("lxc.lxcpath");
5438 dir
= opendir(lxcpath
);
5440 SYSERROR("opendir on lxcpath");
5450 while ((direntp
= readdir(dir
))) {
5451 /* Ignore '.', '..' and any hidden directory. */
5452 if (!strncmp(direntp
->d_name
, ".", 1))
5455 if (!config_file_exists(lxcpath
, direntp
->d_name
))
5459 if (!add_to_array(names
, direntp
->d_name
, cfound
))
5469 c
= lxc_container_new(direntp
->d_name
, lxcpath
);
5471 INFO("Container %s:%s has a config but could not be loaded",
5472 lxcpath
, direntp
->d_name
);
5475 if(!remove_from_array(names
, direntp
->d_name
, cfound
--))
5481 if (!do_lxcapi_is_defined(c
)) {
5482 INFO("Container %s:%s has a config but is not defined",
5483 lxcpath
, direntp
->d_name
);
5486 if(!remove_from_array(names
, direntp
->d_name
, cfound
--))
5489 lxc_container_put(c
);
5493 if (!add_to_clist(cret
, c
, nfound
, true)) {
5494 lxc_container_put(c
);
5505 if (names
&& *names
) {
5506 for (i
=0; i
<cfound
; i
++)
5511 if (cret
&& *cret
) {
5512 for (i
=0; i
<nfound
; i
++)
5513 lxc_container_put((*cret
)[i
]);
5521 int list_active_containers(const char *lxcpath
, char ***nret
,
5522 struct lxc_container
***cret
)
5524 int i
, ret
= -1, cret_cnt
= 0, ct_name_cnt
= 0;
5527 char **ct_name
= NULL
;
5529 struct lxc_container
*c
= NULL
;
5533 lxcpath
= lxc_global_config_value("lxc.lxcpath");
5534 lxcpath_len
= strlen(lxcpath
);
5542 FILE *f
= fopen("/proc/net/unix", "r");
5546 while (getline(&line
, &len
, f
) != -1) {
5547 char *p
= strrchr(line
, ' '), *p2
;
5558 if (strncmp(p
, lxcpath
, lxcpath_len
) == 0) {
5560 } else if (strncmp(p
, "lxc/", 4) == 0) {
5570 /* Now p is the start of lxc_name. */
5571 p2
= strchr(p
, '/');
5572 if (!p2
|| strncmp(p2
, "/command", 8) != 0)
5577 char *recvpath
= lxc_cmd_get_lxcpath(p
);
5581 if (strncmp(lxcpath
, recvpath
, lxcpath_len
) != 0) {
5587 p
= lxc_cmd_get_name(p
);
5592 if (array_contains(&ct_name
, p
, ct_name_cnt
)) {
5598 if (!add_to_array(&ct_name
, p
, ct_name_cnt
)) {
5601 goto free_cret_list
;
5612 c
= lxc_container_new(p
, lxcpath
);
5614 INFO("Container %s:%s is running but could not be loaded",
5617 remove_from_array(&ct_name
, p
, ct_name_cnt
--);
5628 * If this is an anonymous container, then is_defined *can*
5629 * return false. So we don't do that check. Count on the
5630 * fact that the command socket exists.
5633 if (!add_to_clist(cret
, c
, cret_cnt
, true)) {
5634 lxc_container_put(c
);
5635 goto free_cret_list
;
5641 if (nret
&& cret
&& cret_cnt
!= ct_name_cnt
) {
5643 lxc_container_put(c
);
5644 goto free_cret_list
;
5656 if (cret
&& *cret
) {
5657 for (i
= 0; i
< cret_cnt
; i
++)
5658 lxc_container_put((*cret
)[i
]);
5664 for (i
= 0; i
< ct_name_cnt
; i
++)
5675 int list_all_containers(const char *lxcpath
, char ***nret
,
5676 struct lxc_container
***cret
)
5678 int i
, ret
, active_cnt
, ct_cnt
, ct_list_cnt
;
5681 struct lxc_container
**ct_list
= NULL
;
5683 ct_cnt
= list_defined_containers(lxcpath
, &ct_name
, NULL
);
5687 active_cnt
= list_active_containers(lxcpath
, &active_name
, NULL
);
5688 if (active_cnt
< 0) {
5693 for (i
= 0; i
< active_cnt
; i
++) {
5694 if (!array_contains(&ct_name
, active_name
[i
], ct_cnt
)) {
5695 if (!add_to_array(&ct_name
, active_name
[i
], ct_cnt
)) {
5697 goto free_active_name
;
5703 free(active_name
[i
]);
5704 active_name
[i
] = NULL
;
5711 for (i
= 0, ct_list_cnt
= 0; i
< ct_cnt
&& cret
; i
++) {
5712 struct lxc_container
*c
;
5714 c
= lxc_container_new(ct_name
[i
], lxcpath
);
5716 WARN("Container %s:%s could not be loaded", lxcpath
, ct_name
[i
]);
5717 remove_from_array(&ct_name
, ct_name
[i
], ct_cnt
--);
5721 if (!add_to_clist(&ct_list
, c
, ct_list_cnt
, false)) {
5722 lxc_container_put(c
);
5743 for (i
= 0; i
< ct_list_cnt
; i
++) {
5744 lxc_container_put(ct_list
[i
]);
5749 for (i
= 0; i
< active_cnt
; i
++) {
5750 free(active_name
[i
]);
5755 for (i
= 0; i
< ct_cnt
; i
++) {
5762 bool lxc_config_item_is_supported(const char *key
)
5764 return !!lxc_get_config(key
);
5767 bool lxc_has_api_extension(const char *extension
)
5769 /* The NULL API extension is always present. :) */
5773 for (size_t i
= 0; i
< nr_api_extensions
; i
++)
5774 if (strcmp(api_extensions
[i
], extension
) == 0)