1 /* SPDX-License-Identifier: LGPL-2.1+ */
19 #include <sys/mount.h>
21 #include <sys/syscall.h>
22 #include <sys/sysmacros.h>
23 #include <sys/types.h>
29 #include "netns_ifaddrs.h"
31 #include "api_extensions.h"
36 #include "commands_utils.h"
39 #include "confile_utils.h"
42 #include "initutils.h"
46 #include "memory_utils.h"
48 #include "namespace.h"
50 #include "open_utils.h"
52 #include "process_utils.h"
56 #include "storage/btrfs.h"
57 #include "storage/overlay.h"
58 #include "storage_utils.h"
60 #include "syscall_wrappers.h"
66 #include <openssl/evp.h>
71 #include <sys/mkdev.h>
75 #include <../include/lxcmntent.h>
81 #include "include/strlcpy.h"
84 lxc_log_define(lxccontainer
, lxc
);
86 static bool do_lxcapi_destroy(struct lxc_container
*c
);
87 static const char *lxcapi_get_config_path(struct lxc_container
*c
);
88 #define do_lxcapi_get_config_path(c) lxcapi_get_config_path(c)
89 static bool do_lxcapi_set_config_item(struct lxc_container
*c
, const char *key
, const char *v
);
90 static bool container_destroy(struct lxc_container
*c
,
91 struct lxc_storage
*storage
);
92 static bool get_snappath_dir(struct lxc_container
*c
, char *snappath
);
93 static bool lxcapi_snapshot_destroy_all(struct lxc_container
*c
);
94 static bool do_lxcapi_save_config(struct lxc_container
*c
, const char *alt_file
);
96 static bool config_file_exists(const char *lxcpath
, const char *cname
)
98 __do_free
char *fname
= NULL
;
102 /* $lxcpath + '/' + $cname + '/config' + \0 */
103 len
= strlen(lxcpath
) + 1 + strlen(cname
) + 1 + strlen(LXC_CONFIG_FNAME
) + 1;
104 fname
= must_realloc(NULL
, len
);
105 ret
= strnprintf(fname
, len
, "%s/%s/%s", lxcpath
, cname
, LXC_CONFIG_FNAME
);
109 return file_exists(fname
);
113 * A few functions to help detect when a container creation failed. If a
114 * container creation was killed partway through, then trying to actually start
115 * that container could harm the host. We detect this by creating a 'partial'
116 * file under the container directory, and keeping an advisory lock. When
117 * container creation completes, we remove that file. When we load or try to
118 * start a container, if we find that file, without a flock, we remove the
122 LXC_CREATE_FAILED
= -1,
123 LXC_CREATE_SUCCESS
= 0,
124 LXC_CREATE_ONGOING
= 1,
125 LXC_CREATE_INCOMPLETE
= 2,
128 static int ongoing_create(struct lxc_container
*c
)
130 __do_close
int fd
= -EBADF
;
131 __do_free
char *path
= NULL
;
132 struct flock lk
= {0};
136 len
= strlen(c
->config_path
) + 1 + strlen(c
->name
) + 1 + strlen(LXC_PARTIAL_FNAME
) + 1;
137 path
= must_realloc(NULL
, len
);
138 ret
= strnprintf(path
, len
, "%s/%s/%s", c
->config_path
, c
->name
, LXC_PARTIAL_FNAME
);
140 return LXC_CREATE_FAILED
;
142 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
145 return LXC_CREATE_FAILED
;
147 return LXC_CREATE_SUCCESS
;
151 lk
.l_whence
= SEEK_SET
;
153 * F_OFD_GETLK requires that l_pid be set to 0 otherwise the kernel
158 ret
= fcntl(fd
, F_OFD_GETLK
, &lk
);
159 if (ret
< 0 && errno
== EINVAL
) {
160 ret
= flock(fd
, LOCK_EX
| LOCK_NB
);
161 if (ret
< 0 && errno
== EWOULDBLOCK
)
165 /* F_OFD_GETLK will not send us back a pid so don't check it. */
167 /* Create is still ongoing. */
168 return LXC_CREATE_ONGOING
;
170 /* Create completed but partial is still there. */
171 return LXC_CREATE_INCOMPLETE
;
174 static int create_partial(int fd_rootfs
, struct lxc_container
*c
)
176 __do_close
int fd_partial
= -EBADF
;
178 struct flock lk
= {0};
180 fd_partial
= openat(fd_rootfs
, LXC_PARTIAL_FNAME
, O_RDWR
| O_CREAT
| O_EXCL
| O_CLOEXEC
, 0000);
182 return syserror("errno(%d) - Failed to create \"%d/" LXC_PARTIAL_FNAME
"\" to mark container as partially created", errno
, fd_rootfs
);
185 lk
.l_whence
= SEEK_SET
;
187 ret
= fcntl(fd_partial
, F_OFD_SETLKW
, &lk
);
189 if (errno
== EINVAL
) {
190 ret
= flock(fd_partial
, LOCK_EX
);
192 return move_fd(fd_partial
);
195 return syserror("Failed to lock partial file \"%d/" LXC_PARTIAL_FNAME
"\"", fd_rootfs
);
198 TRACE("Created \"%d/" LXC_PARTIAL_FNAME
"\" to mark container as partially created", fd_rootfs
);
199 return move_fd(fd_partial
);
202 static void remove_partial(struct lxc_container
*c
, int fd
)
204 __do_free
char *path
= NULL
;
210 /* $lxcpath + '/' + $name + '/partial' + \0 */
211 len
= strlen(c
->config_path
) + 1 + strlen(c
->name
) + 1 + strlen(LXC_PARTIAL_FNAME
) + 1;
212 path
= must_realloc(NULL
, len
);
213 ret
= strnprintf(path
, len
, "%s/%s/%s", c
->config_path
, c
->name
, LXC_PARTIAL_FNAME
);
219 SYSERROR("Failed to remove partial file %s", path
);
223 * 1. container_mem_lock(c) protects the struct lxc_container from multiple threads.
224 * 2. container_disk_lock(c) protects the on-disk container data - in particular the
225 * container configuration file.
226 * The container_disk_lock also takes the container_mem_lock.
227 * 3. thread_mutex protects process data (ex: fd table) from multiple threads.
228 * NOTHING mutexes two independent programs with their own struct
229 * lxc_container for the same c->name, between API calls. For instance,
230 * c->config_read(); c->start(); Between those calls, data on disk
231 * could change (which shouldn't bother the caller unless for instance
232 * the rootfs get moved). c->config_read(); update; c->config_write();
233 * Two such updaters could race. The callers should therefore check their
234 * results. Trying to prevent that would necessarily expose us to deadlocks
235 * due to hung callers. So I prefer to keep the locks only within our own
236 * functions, not across functions.
238 * If you're going to clone while holding a lxccontainer, increment
239 * c->numthreads (under privlock) before forking. When deleting,
240 * decrement numthreads under privlock, then if it hits 0 you can delete.
241 * Do not ever use a lxccontainer whose numthreads you did not bump.
243 static void lxc_container_free(struct lxc_container
*c
)
249 c
->configfile
= NULL
;
251 free(c
->error_string
);
252 c
->error_string
= NULL
;
255 lxc_putlock(c
->slock
);
260 lxc_putlock(c
->privlock
);
268 lxc_conf_free(c
->lxc_conf
);
272 free(c
->config_path
);
273 c
->config_path
= NULL
;
278 /* Consider the following case:
280 * |====================================================================|
281 * | freer | racing get()er |
282 * |====================================================================|
283 * | lxc_container_put() | lxc_container_get() |
284 * | \ lxclock(c->privlock) | c->numthreads < 1? (no) |
285 * | \ c->numthreads = 0 | \ lxclock(c->privlock) -> waits |
286 * | \ lxcunlock() | \ |
287 * | \ lxc_container_free() | \ lxclock() returns |
288 * | | \ c->numthreads < 1 -> return 0 |
289 * | \ \ (free stuff) | |
290 * | \ \ sem_destroy(privlock) | |
291 * |_______________________________|____________________________________|
293 * When the get()er checks numthreads the first time, one of the following
295 * 1. freer has set numthreads = 0. get() returns 0
296 * 2. freer is between lxclock and setting numthreads to 0. get()er will
297 * sem_wait on privlock, get lxclock after freer() drops it, then see
298 * numthreads is 0 and exit without touching lxclock again..
299 * 3. freer has not yet locked privlock. If get()er runs first, then put()er
300 * will see --numthreads = 1 and not call lxc_container_free().
303 int lxc_container_get(struct lxc_container
*c
)
308 /* If someone else has already started freeing the container, don't try
309 * to take the lock, which may be invalid.
311 if (c
->numthreads
< 1)
314 if (container_mem_lock(c
))
317 /* Bail without trying to unlock, bc the privlock is now probably in
320 if (c
->numthreads
< 1)
324 container_mem_unlock(c
);
329 int lxc_container_put(struct lxc_container
*c
)
334 if (container_mem_lock(c
))
339 if (c
->numthreads
< 1) {
340 container_mem_unlock(c
);
341 lxc_container_free(c
);
345 container_mem_unlock(c
);
349 static bool do_lxcapi_is_defined(struct lxc_container
*c
)
358 if (container_mem_lock(c
))
364 statret
= stat(c
->configfile
, &statbuf
);
371 container_mem_unlock(c
);
375 #define WRAP_API(rettype, fnname) \
376 static rettype fnname(struct lxc_container *c) \
379 bool reset_config = false; \
381 if (!current_config && c && c->lxc_conf) { \
382 current_config = c->lxc_conf; \
383 reset_config = true; \
386 ret = do_##fnname(c); \
388 current_config = NULL; \
393 #define WRAP_API_1(rettype, fnname, t1) \
394 static rettype fnname(struct lxc_container *c, t1 a1) \
397 bool reset_config = false; \
399 if (!current_config && c && c->lxc_conf) { \
400 current_config = c->lxc_conf; \
401 reset_config = true; \
404 ret = do_##fnname(c, a1); \
406 current_config = NULL; \
411 #define WRAP_API_2(rettype, fnname, t1, t2) \
412 static rettype fnname(struct lxc_container *c, t1 a1, t2 a2) \
415 bool reset_config = false; \
417 if (!current_config && c && c->lxc_conf) { \
418 current_config = c->lxc_conf; \
419 reset_config = true; \
422 ret = do_##fnname(c, a1, a2); \
424 current_config = NULL; \
429 #define WRAP_API_3(rettype, fnname, t1, t2, t3) \
430 static rettype fnname(struct lxc_container *c, t1 a1, t2 a2, t3 a3) \
433 bool reset_config = false; \
435 if (!current_config && c && c->lxc_conf) { \
436 current_config = c->lxc_conf; \
437 reset_config = true; \
440 ret = do_##fnname(c, a1, a2, a3); \
442 current_config = NULL; \
447 #define WRAP_API_5(rettype, fnname, t1, t2, t3, t4, t5) \
448 static rettype fnname(struct lxc_container *c, t1 a1, t2 a2, t3 a3, \
452 bool reset_config = false; \
454 if (!current_config && c && c->lxc_conf) { \
455 current_config = c->lxc_conf; \
456 reset_config = true; \
459 ret = do_##fnname(c, a1, a2, a3, a4, a5); \
461 current_config = NULL; \
466 #define WRAP_API_6(rettype, fnname, t1, t2, t3, t4, t5, t6) \
467 static rettype fnname(struct lxc_container *c, t1 a1, t2 a2, t3 a3, \
468 t4 a4, t5 a5, t6 a6) \
471 bool reset_config = false; \
473 if (!current_config && c && c->lxc_conf) { \
474 current_config = c->lxc_conf; \
475 reset_config = true; \
478 ret = do_##fnname(c, a1, a2, a3, a4, a5, a6); \
480 current_config = NULL; \
485 WRAP_API(bool, lxcapi_is_defined
)
487 static const char *do_lxcapi_state(struct lxc_container
*c
)
494 s
= lxc_getstate(c
->name
, c
->config_path
);
495 return lxc_state2str(s
);
498 WRAP_API(const char *, lxcapi_state
)
500 static bool is_stopped(struct lxc_container
*c
)
504 s
= lxc_getstate(c
->name
, c
->config_path
);
505 return (s
== STOPPED
);
508 static bool do_lxcapi_is_running(struct lxc_container
*c
)
513 return !is_stopped(c
);
516 WRAP_API(bool, lxcapi_is_running
)
518 static bool do_lxcapi_freeze(struct lxc_container
*c
)
523 if (!c
|| !c
->lxc_conf
)
526 s
= lxc_getstate(c
->name
, c
->config_path
);
528 ret
= cgroup_freeze(c
->name
, c
->config_path
, -1);
529 if (ret
== -ENOCGROUP2
)
530 ret
= lxc_freeze(c
->lxc_conf
, c
->name
, c
->config_path
);
536 WRAP_API(bool, lxcapi_freeze
)
538 static bool do_lxcapi_unfreeze(struct lxc_container
*c
)
543 if (!c
|| !c
->lxc_conf
)
546 s
= lxc_getstate(c
->name
, c
->config_path
);
548 ret
= cgroup_unfreeze(c
->name
, c
->config_path
, -1);
549 if (ret
== -ENOCGROUP2
)
550 ret
= lxc_unfreeze(c
->lxc_conf
, c
->name
, c
->config_path
);
557 WRAP_API(bool, lxcapi_unfreeze
)
559 static int do_lxcapi_console_getfd(struct lxc_container
*c
, int *ttynum
, int *ptxfd
)
564 return lxc_terminal_getfd(c
, ttynum
, ptxfd
);
567 WRAP_API_2(int, lxcapi_console_getfd
, int *, int *)
569 static int lxcapi_console(struct lxc_container
*c
, int ttynum
, int stdinfd
,
570 int stdoutfd
, int stderrfd
, int escape
)
577 current_config
= c
->lxc_conf
;
578 ret
= lxc_console(c
, ttynum
, stdinfd
, stdoutfd
, stderrfd
, escape
);
579 current_config
= NULL
;
584 static int do_lxcapi_console_log(struct lxc_container
*c
, struct lxc_console_log
*log
)
591 ret
= lxc_cmd_console_log(c
->name
, do_lxcapi_get_config_path(c
), log
);
594 NOTICE("The console log is empty");
595 else if (ret
== -EFAULT
)
596 NOTICE("The container does not keep a console log");
597 else if (ret
== -ENOENT
)
598 NOTICE("The container does not keep a console log file");
599 else if (ret
== -EIO
)
600 NOTICE("Failed to write console log to log file");
602 ERROR("Failed to retrieve console log");
608 WRAP_API_1(int, lxcapi_console_log
, struct lxc_console_log
*)
610 static pid_t
do_lxcapi_init_pid(struct lxc_container
*c
)
615 return lxc_cmd_get_init_pid(c
->name
, c
->config_path
);
618 WRAP_API(pid_t
, lxcapi_init_pid
)
620 static int do_lxcapi_init_pidfd(struct lxc_container
*c
)
623 return ret_errno(EBADF
);
625 return lxc_cmd_get_init_pidfd(c
->name
, c
->config_path
);
628 WRAP_API(int, lxcapi_init_pidfd
)
630 static int do_lxcapi_devpts_fd(struct lxc_container
*c
)
633 return ret_errno(EBADF
);
635 return lxc_cmd_get_devpts_fd(c
->name
, c
->config_path
);
638 WRAP_API(int, lxcapi_devpts_fd
)
640 static bool load_config_locked(struct lxc_container
*c
, const char *fname
)
643 c
->lxc_conf
= lxc_conf_init();
648 if (lxc_config_read(fname
, c
->lxc_conf
, false) != 0)
651 c
->lxc_conf
->name
= c
->name
;
655 static bool do_lxcapi_load_config(struct lxc_container
*c
, const char *alt_file
)
659 bool need_disklock
= false, ret
= false;
664 fname
= c
->configfile
;
672 /* If we're reading something other than the container's config, we only
673 * need to lock the in-memory container. If loading the container's
674 * config file, take the disk lock.
676 if (strequal(fname
, c
->configfile
))
677 need_disklock
= true;
680 lret
= container_disk_lock(c
);
682 lret
= container_mem_lock(c
);
686 ret
= load_config_locked(c
, fname
);
689 container_disk_unlock(c
);
691 container_mem_unlock(c
);
696 WRAP_API_1(bool, lxcapi_load_config
, const char *)
698 static bool do_lxcapi_want_daemonize(struct lxc_container
*c
, bool state
)
700 if (!c
|| !c
->lxc_conf
)
703 if (container_mem_lock(c
))
706 c
->daemonize
= state
;
708 container_mem_unlock(c
);
713 WRAP_API_1(bool, lxcapi_want_daemonize
, bool)
715 static bool do_lxcapi_want_close_all_fds(struct lxc_container
*c
, bool state
)
717 if (!c
|| !c
->lxc_conf
)
720 if (container_mem_lock(c
))
723 c
->lxc_conf
->close_all_fds
= state
;
725 container_mem_unlock(c
);
730 WRAP_API_1(bool, lxcapi_want_close_all_fds
, bool)
732 static bool do_lxcapi_wait(struct lxc_container
*c
, const char *state
,
740 ret
= lxc_wait(c
->name
, state
, timeout
, c
->config_path
);
744 WRAP_API_2(bool, lxcapi_wait
, const char *, int)
746 static bool am_single_threaded(void)
748 __do_closedir
DIR *dir
= NULL
;
749 struct dirent
*direntp
;
752 dir
= opendir("/proc/self/task");
756 while ((direntp
= readdir(dir
))) {
757 if (strequal(direntp
->d_name
, "."))
760 if (strequal(direntp
->d_name
, ".."))
771 static void push_arg(char ***argp
, char *arg
, int *nargs
)
776 copy
= must_copy_string(arg
);
779 argv
= realloc(*argp
, (*nargs
+ 2) * sizeof(char *));
788 static char **split_init_cmd(const char *incmd
)
790 __do_free
char *copy
= NULL
;
798 copy
= must_copy_string(incmd
);
801 argv
= malloc(sizeof(char *));
805 lxc_iterate_parts (p
, copy
, " ")
806 push_arg(&argv
, p
, &nargs
);
816 static void free_init_cmd(char **argv
)
829 static int lxc_rcv_status(int state_socket
)
835 /* Receive container state. */
836 ret
= lxc_abstract_unix_rcv_credential(state_socket
, &state
, sizeof(int));
841 TRACE("Caught EINTR; retrying");
848 static bool wait_on_daemonized_start(struct lxc_handler
*handler
, int pid
)
852 /* The first child is going to fork() again and then exits. So we reap
853 * the first child here.
855 ret
= wait_for_pid(pid
);
857 DEBUG("Failed waiting on first child %d", pid
);
859 DEBUG("First child %d exited", pid
);
861 /* Close write end of the socket pair. */
862 close_prot_errno_disarm(handler
->state_socket_pair
[1]);
864 state
= lxc_rcv_status(handler
->state_socket_pair
[0]);
866 /* Close read end of the socket pair. */
867 close_prot_errno_disarm(handler
->state_socket_pair
[0]);
870 SYSERROR("Failed to receive the container state");
874 /* If we receive anything else then running we know that the container
877 if (state
!= RUNNING
) {
878 ERROR("Received container state \"%s\" instead of \"RUNNING\"",
879 lxc_state2str(state
));
883 TRACE("Container is in \"RUNNING\" state");
887 static bool do_lxcapi_start(struct lxc_container
*c
, int useinit
, char * const argv
[])
890 struct lxc_handler
*handler
;
891 struct lxc_conf
*conf
;
892 char *default_args
[] = {
896 char **init_cmd
= NULL
;
898 /* container does exist */
902 /* If anything fails before we set error_num, we want an error in there.
906 /* Container has not been setup. */
910 ret
= ongoing_create(c
);
912 case LXC_CREATE_FAILED
:
913 ERROR("Failed checking for incomplete container creation");
915 case LXC_CREATE_ONGOING
:
916 ERROR("Ongoing container creation detected");
918 case LXC_CREATE_INCOMPLETE
:
919 ERROR("Failed to create container");
920 do_lxcapi_destroy(c
);
924 if (container_mem_lock(c
))
929 /* initialize handler */
930 handler
= lxc_init_handler(NULL
, c
->name
, conf
, c
->config_path
, c
->daemonize
);
932 container_mem_unlock(c
);
937 if (useinit
&& conf
->execute_cmd
)
938 argv
= init_cmd
= split_init_cmd(conf
->execute_cmd
);
940 argv
= init_cmd
= split_init_cmd(conf
->init_cmd
);
943 /* ... otherwise use default_args. */
946 ERROR("No valid init detected");
947 lxc_put_handler(handler
);
953 /* I'm not sure what locks we want here.Any? Is liblxc's locking enough
954 * here to protect the on disk container? We don't want to exclude
955 * things like lxc_info while the container is running.
960 pid_t pid_first
, pid_second
;
964 free_init_cmd(init_cmd
);
965 lxc_put_handler(handler
);
970 if (pid_first
!= 0) {
971 /* Set to NULL because we don't want father unlink
972 * the PID file, child will do the free and unlink.
976 /* Wait for container to tell us whether it started
979 started
= wait_on_daemonized_start(handler
, pid_first
);
981 free_init_cmd(init_cmd
);
982 lxc_put_handler(handler
);
988 /* We don't really care if this doesn't print all the
989 * characters. All that it means is that the proctitle will be
990 * ugly. Similarly, we also don't care if setproctitle() fails.
992 ret
= strnprintf(title
, sizeof(title
), "[lxc monitor] %s %s", c
->config_path
, c
->name
);
994 ret
= setproctitle(title
);
996 INFO("Failed to set process title to %s", title
);
998 INFO("Set process title to %s", title
);
1001 /* We fork() a second time to be reparented to init. Like
1002 * POSIX's daemon() function we change to "/" and redirect
1003 * std{in,out,err} to /dev/null.
1005 pid_second
= fork();
1006 if (pid_second
< 0) {
1007 SYSERROR("Failed to fork first child process");
1008 _exit(EXIT_FAILURE
);
1012 if (pid_second
!= 0) {
1013 free_init_cmd(init_cmd
);
1014 lxc_put_handler(handler
);
1015 _exit(EXIT_SUCCESS
);
1020 /* change to / directory */
1023 SYSERROR("Failed to change to \"/\" directory");
1024 _exit(EXIT_FAILURE
);
1027 ret
= inherit_fds(handler
, true);
1029 _exit(EXIT_FAILURE
);
1031 /* redirect std{in,out,err} to /dev/null */
1032 ret
= null_stdfds();
1034 ERROR("Failed to redirect std{in,out,err} to /dev/null");
1035 _exit(EXIT_FAILURE
);
1038 /* become session leader */
1041 TRACE("Process %d is already process group leader", lxc_raw_getpid());
1042 } else if (!am_single_threaded()) {
1043 ERROR("Cannot start non-daemonized container when threaded");
1044 free_init_cmd(init_cmd
);
1045 lxc_put_handler(handler
);
1049 /* We need to write PID file after daemonize, so we always write the
1054 char pidstr
[INTTYPE_TO_STRLEN(pid_t
)];
1056 w
= strnprintf(pidstr
, sizeof(pidstr
), "%d", lxc_raw_getpid());
1058 free_init_cmd(init_cmd
);
1059 lxc_put_handler(handler
);
1061 SYSERROR("Failed to write monitor pid to \"%s\"", c
->pidfile
);
1064 _exit(EXIT_FAILURE
);
1069 ret
= lxc_write_to_file(c
->pidfile
, pidstr
, w
, false, 0600);
1071 free_init_cmd(init_cmd
);
1072 lxc_put_handler(handler
);
1074 SYSERROR("Failed to write monitor pid to \"%s\"", c
->pidfile
);
1077 _exit(EXIT_FAILURE
);
1083 conf
->reboot
= REBOOT_NONE
;
1085 /* Unshare the mount namespace if requested */
1086 if (conf
->monitor_unshare
) {
1087 ret
= unshare(CLONE_NEWNS
);
1089 SYSERROR("Failed to unshare mount namespace");
1090 lxc_put_handler(handler
);
1095 ret
= mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
);
1097 SYSERROR("Failed to recursively turn root mount tree into dependent mount. Continuing...");
1098 lxc_put_handler(handler
);
1105 if (conf
->reboot
== REBOOT_INIT
) {
1106 /* initialize handler */
1107 handler
= lxc_init_handler(handler
, c
->name
, conf
, c
->config_path
, c
->daemonize
);
1114 ret
= inherit_fds(handler
, c
->daemonize
);
1116 lxc_put_handler(handler
);
1122 ret
= lxc_execute(c
->name
, argv
, 1, handler
, c
->config_path
,
1123 c
->daemonize
, &c
->error_num
);
1125 ret
= lxc_start(argv
, handler
, c
->config_path
, c
->daemonize
,
1128 if (conf
->reboot
== REBOOT_REQ
) {
1129 INFO("Container requested reboot");
1130 conf
->reboot
= REBOOT_INIT
;
1140 free_init_cmd(init_cmd
);
1142 if (c
->daemonize
&& ret
!= 0)
1143 _exit(EXIT_FAILURE
);
1144 else if (c
->daemonize
)
1145 _exit(EXIT_SUCCESS
);
1153 static bool lxcapi_start(struct lxc_container
*c
, int useinit
,
1158 current_config
= c
? c
->lxc_conf
: NULL
;
1159 ret
= do_lxcapi_start(c
, useinit
, argv
);
1160 current_config
= NULL
;
1165 /* Note, there MUST be an ending NULL. */
1166 static bool lxcapi_startl(struct lxc_container
*c
, int useinit
, ...)
1169 char **inargs
= NULL
;
1172 /* container exists */
1176 current_config
= c
->lxc_conf
;
1178 va_start(ap
, useinit
);
1179 inargs
= lxc_va_arg_list_to_argv(ap
, 0, 1);
1184 /* pass NULL if no arguments were supplied */
1185 bret
= do_lxcapi_start(c
, useinit
, *inargs
? inargs
: NULL
);
1191 for (arg
= inargs
; *arg
; arg
++)
1196 current_config
= NULL
;
1201 static bool do_lxcapi_stop(struct lxc_container
*c
)
1208 ret
= lxc_cmd_stop(c
->name
, c
->config_path
);
1213 WRAP_API(bool, lxcapi_stop
)
1215 static int do_create_container_dir(const char *path
, struct lxc_conf
*conf
)
1217 __do_close
int fd_rootfs
= -EBADF
;
1220 mode_t mask
= umask(0002);
1221 ret
= mkdir(path
, 0770);
1223 if (ret
< 0 && errno
!= EEXIST
)
1226 fd_rootfs
= open_at(-EBADF
, path
, O_DIRECTORY
| O_CLOEXEC
, PROTECT_LOOKUP_ABSOLUTE_XDEV_SYMLINKS
, 0);
1228 return syserror("Failed to open container directory \"%d(%s)\"", fd_rootfs
, path
);
1230 if (list_empty(&conf
->id_map
))
1231 return move_fd(fd_rootfs
);
1233 ret
= userns_exec_mapped_root(NULL
, fd_rootfs
, conf
);
1235 return syserror_ret(-1, "Failed to chown rootfs \"%s\"", path
);
1237 return move_fd(fd_rootfs
);
1240 /* Create the standard expected container dir. */
1241 static int create_container_dir(struct lxc_container
*c
)
1243 __do_free
char *s
= NULL
;
1247 len
= strlen(c
->config_path
) + strlen(c
->name
) + 2;
1250 return ret_errno(ENOMEM
);
1252 ret
= strnprintf(s
, len
, "%s/%s", c
->config_path
, c
->name
);
1256 return do_create_container_dir(s
, c
->lxc_conf
);
1259 /* do_storage_create: thin wrapper around storage_create(). Like
1260 * storage_create(), it returns a mounted bdev on success, NULL on error.
1262 static struct lxc_storage
*do_storage_create(struct lxc_container
*c
,
1264 struct bdev_specs
*specs
)
1266 __do_free
char *dest
= NULL
;
1269 struct lxc_storage
*bdev
;
1271 /* rootfs.path or lxcpath/lxcname/rootfs */
1272 if (c
->lxc_conf
->rootfs
.path
&&
1273 (access(c
->lxc_conf
->rootfs
.path
, F_OK
) == 0)) {
1274 const char *rpath
= c
->lxc_conf
->rootfs
.path
;
1275 len
= strlen(rpath
) + 1;
1276 dest
= must_realloc(NULL
, len
);
1277 ret
= strnprintf(dest
, len
, "%s", rpath
);
1279 const char *lxcpath
= do_lxcapi_get_config_path(c
);
1280 len
= strlen(c
->name
) + 1 + strlen(lxcpath
) + 1 + strlen(LXC_ROOTFS_DNAME
) + 1;
1281 dest
= must_realloc(NULL
, len
);
1282 ret
= strnprintf(dest
, len
, "%s/%s/%s", lxcpath
, c
->name
, LXC_ROOTFS_DNAME
);
1287 bdev
= storage_create(dest
, type
, c
->name
, specs
, c
->lxc_conf
);
1289 ERROR("Failed to create \"%s\" storage", type
);
1293 if (!c
->set_config_item(c
, "lxc.rootfs.path", bdev
->src
)) {
1294 ERROR("Failed to set \"lxc.rootfs.path = %s\"", bdev
->src
);
1299 /* If we are not root, chown the rootfs dir to root in the target user
1302 if (am_guest_unpriv() || !list_empty(&c
->lxc_conf
->id_map
)) {
1303 ret
= chown_mapped_root(bdev
->dest
, c
->lxc_conf
);
1305 ERROR("Error chowning \"%s\" to container root", bdev
->dest
);
1306 suggest_default_idmap();
1315 /* Strip path and return name of file for argv[0] passed to execvp */
1316 static char *lxctemplatefilename(char *tpath
)
1320 p
= tpath
+ strlen(tpath
) - 1;
1321 while ( (p
-1) >= tpath
&& *(p
-1) != '/')
1327 static bool create_run_template(struct lxc_container
*c
, char *tpath
,
1328 bool need_null_stdfds
, char *const argv
[])
1338 SYSERROR("Failed to fork task for container creation template");
1342 if (pid
== 0) { /* child */
1344 char *namearg
, *patharg
, *rootfsarg
;
1347 struct lxc_storage
*bdev
= NULL
;
1348 struct lxc_conf
*conf
= c
->lxc_conf
;
1351 if (need_null_stdfds
) {
1352 ret
= null_stdfds();
1354 _exit(EXIT_FAILURE
);
1357 ret
= lxc_storage_prepare(conf
);
1359 ERROR("Failed to initialize storage");
1360 _exit(EXIT_FAILURE
);
1362 bdev
= conf
->rootfs
.storage
;
1366 ret
= unshare(CLONE_NEWNS
);
1368 ERROR("Failed to unshare CLONE_NEWNS");
1369 _exit(EXIT_FAILURE
);
1372 if (detect_shared_rootfs() && mount(NULL
, "/", NULL
, MS_SLAVE
| MS_REC
, NULL
))
1373 SYSERROR("Failed to recursively turn root mount tree into dependent mount. Continuing...");
1376 if (!strequal(bdev
->type
, "dir") && !strequal(bdev
->type
, "btrfs")) {
1378 ERROR("Unprivileged users can only create "
1379 "btrfs and directory-backed containers");
1380 _exit(EXIT_FAILURE
);
1383 if (strequal(bdev
->type
, "overlay") ||
1384 strequal(bdev
->type
, "overlayfs")) {
1385 /* If we create an overlay container we need to
1386 * rsync the contents into
1387 * <container-path>/<container-name>/rootfs.
1388 * However, the overlay mount function will
1390 * <container-path>/<container-name>/delta0
1392 * <container-path>/<container-name>/rootfs
1393 * which means we would rsync the rootfs into
1394 * the delta directory. That doesn't make sense
1395 * since the delta directory only exists to
1396 * record the differences to
1397 * <container-path>/<container-name>/rootfs. So
1398 * let's simply bind-mount here and then rsync
1400 * <container-path>/<container-name>/rootfs.
1404 src
= ovl_get_rootfs(bdev
->src
, &(size_t){0});
1406 ERROR("Failed to get rootfs");
1407 _exit(EXIT_FAILURE
);
1410 ret
= mount(src
, bdev
->dest
, "bind", MS_BIND
| MS_REC
, NULL
);
1412 ERROR("Failed to mount rootfs");
1413 _exit(EXIT_FAILURE
);
1416 ret
= bdev
->ops
->mount(bdev
);
1418 ERROR("Failed to mount rootfs");
1419 _exit(EXIT_FAILURE
);
1422 } else { /* TODO come up with a better way here! */
1425 src
= lxc_storage_get_path(bdev
->src
, bdev
->type
);
1426 bdev
->dest
= strdup(src
);
1429 /* Create our new array, pre-pend the template name and base
1433 for (nargs
= 0; argv
[nargs
]; nargs
++)
1436 /* template, path, rootfs and name args */
1439 newargv
= malloc(nargs
* sizeof(*newargv
));
1441 _exit(EXIT_FAILURE
);
1442 newargv
[0] = lxctemplatefilename(tpath
);
1445 len
= strlen(c
->config_path
) + strlen(c
->name
) + strlen("--path=") + 2;
1446 patharg
= malloc(len
);
1448 _exit(EXIT_FAILURE
);
1450 ret
= strnprintf(patharg
, len
, "--path=%s/%s", c
->config_path
, c
->name
);
1452 _exit(EXIT_FAILURE
);
1453 newargv
[1] = patharg
;
1456 len
= strlen("--name=") + strlen(c
->name
) + 1;
1457 namearg
= malloc(len
);
1459 _exit(EXIT_FAILURE
);
1461 ret
= strnprintf(namearg
, len
, "--name=%s", c
->name
);
1463 _exit(EXIT_FAILURE
);
1464 newargv
[2] = namearg
;
1467 len
= strlen("--rootfs=") + 1 + strlen(bdev
->dest
);
1468 rootfsarg
= malloc(len
);
1470 _exit(EXIT_FAILURE
);
1472 ret
= strnprintf(rootfsarg
, len
, "--rootfs=%s", bdev
->dest
);
1474 _exit(EXIT_FAILURE
);
1475 newargv
[3] = rootfsarg
;
1477 /* add passed-in args */
1479 for (i
= 4; i
< nargs
; i
++)
1480 newargv
[i
] = argv
[i
- 4];
1482 /* add trailing NULL */
1484 newargv
= realloc(newargv
, nargs
* sizeof(*newargv
));
1486 _exit(EXIT_FAILURE
);
1487 newargv
[nargs
- 1] = NULL
;
1489 /* If we're running the template in a mapped userns, then we
1490 * prepend the template command with: lxc-usernsexec <-m map1>
1491 * ... <-m mapn> -- and we append "--mapped-uid x", where x is
1492 * the mapped uid for our geteuid()
1494 if (!list_empty(&conf
->id_map
)) {
1495 int extraargs
, hostuid_mapped
, hostgid_mapped
;
1497 char txtuid
[20], txtgid
[20];
1501 n2
= malloc(n2args
* sizeof(*n2
));
1503 _exit(EXIT_FAILURE
);
1506 tpath
= "lxc-usernsexec";
1507 n2
[0] = "lxc-usernsexec";
1509 list_for_each_entry(map
, &conf
->id_map
, head
) {
1511 n2
= realloc(n2
, n2args
* sizeof(char *));
1513 _exit(EXIT_FAILURE
);
1515 n2
[n2args
- 2] = "-m";
1516 n2
[n2args
- 1] = malloc(200);
1517 if (!n2
[n2args
- 1])
1518 _exit(EXIT_FAILURE
);
1520 ret
= strnprintf(n2
[n2args
- 1], 200, "%c:%lu:%lu:%lu",
1521 map
->idtype
== ID_TYPE_UID
? 'u' : 'g',
1522 map
->nsid
, map
->hostid
, map
->range
);
1524 _exit(EXIT_FAILURE
);
1527 hostuid_mapped
= mapped_hostid(geteuid(), conf
, ID_TYPE_UID
);
1528 extraargs
= hostuid_mapped
>= 0 ? 1 : 3;
1530 n2
= realloc(n2
, (nargs
+ n2args
+ extraargs
) * sizeof(char *));
1532 _exit(EXIT_FAILURE
);
1534 if (hostuid_mapped
< 0) {
1535 hostuid_mapped
= find_unmapped_nsid(conf
, ID_TYPE_UID
);
1536 n2
[n2args
++] = "-m";
1537 if (hostuid_mapped
< 0) {
1538 ERROR("Failed to find free uid to map");
1539 _exit(EXIT_FAILURE
);
1542 n2
[n2args
++] = malloc(200);
1543 if (!n2
[n2args
- 1]) {
1544 SYSERROR("out of memory");
1545 _exit(EXIT_FAILURE
);
1548 ret
= strnprintf(n2
[n2args
- 1], 200, "u:%d:%d:1",
1549 hostuid_mapped
, geteuid());
1551 _exit(EXIT_FAILURE
);
1554 hostgid_mapped
= mapped_hostid(getegid(), conf
, ID_TYPE_GID
);
1555 extraargs
= hostgid_mapped
>= 0 ? 1 : 3;
1557 n2
= realloc(n2
, (nargs
+ n2args
+ extraargs
) * sizeof(char *));
1559 _exit(EXIT_FAILURE
);
1561 if (hostgid_mapped
< 0) {
1562 hostgid_mapped
= find_unmapped_nsid(conf
, ID_TYPE_GID
);
1563 n2
[n2args
++] = "-m";
1564 if (hostgid_mapped
< 0) {
1565 ERROR("Failed to find free gid to map");
1566 _exit(EXIT_FAILURE
);
1569 n2
[n2args
++] = malloc(200);
1570 if (!n2
[n2args
- 1]) {
1571 SYSERROR("out of memory");
1572 _exit(EXIT_FAILURE
);
1575 ret
= strnprintf(n2
[n2args
- 1], 200, "g:%d:%d:1",
1576 hostgid_mapped
, getegid());
1578 _exit(EXIT_FAILURE
);
1581 n2
[n2args
++] = "--";
1583 for (i
= 0; i
< nargs
; i
++)
1584 n2
[i
+ n2args
] = newargv
[i
];
1587 /* Finally add "--mapped-uid $uid" to tell template what
1588 * to chown cached images to.
1591 n2
= realloc(n2
, n2args
* sizeof(char *));
1593 _exit(EXIT_FAILURE
);
1595 /* note n2[n2args-1] is NULL */
1596 n2
[n2args
- 5] = "--mapped-uid";
1598 ret
= strnprintf(txtuid
, 20, "%d", hostuid_mapped
);
1602 _exit(EXIT_FAILURE
);
1605 n2
[n2args
- 4] = txtuid
;
1606 n2
[n2args
- 3] = "--mapped-gid";
1608 ret
= strnprintf(txtgid
, 20, "%d", hostgid_mapped
);
1612 _exit(EXIT_FAILURE
);
1615 n2
[n2args
- 2] = txtgid
;
1616 n2
[n2args
- 1] = NULL
;
1621 execvp(tpath
, newargv
);
1622 SYSERROR("Failed to execute template %s", tpath
);
1623 _exit(EXIT_FAILURE
);
1626 ret
= wait_for_pid(pid
);
1628 ERROR("Failed to create container from template");
1635 static bool prepend_lxc_header(char *path
, const char *t
, char *const argv
[])
1643 unsigned int md_len
= 0;
1644 unsigned char md_value
[EVP_MAX_MD_SIZE
];
1648 f
= fopen(path
, "re");
1652 ret
= fseek(f
, 0, SEEK_END
);
1661 ret
= fseek(f
, 0, SEEK_SET
);
1665 ret
= fseek(f
, 0, SEEK_SET
);
1670 contents
= malloc(flen
+ 1);
1674 len
= fread(contents
, 1, flen
, f
);
1676 goto out_free_contents
;
1678 contents
[flen
] = '\0';
1683 goto out_free_contents
;
1686 tpath
= get_template_path(t
);
1688 ERROR("Invalid template \"%s\" specified", t
);
1689 goto out_free_contents
;
1692 ret
= sha1sum_file(tpath
, md_value
, &md_len
);
1694 ERROR("Failed to get sha1sum of %s", tpath
);
1696 goto out_free_contents
;
1701 f
= fopen(path
, "we");
1703 SYSERROR("Reopening config for writing");
1708 fprintf(f
, "# Template used to create this container: %s\n", t
);
1710 fprintf(f
, "# Parameters passed to the template:");
1712 fprintf(f
, " %s", *argv
);
1719 fprintf(f
, "# Template script checksum (SHA-1): ");
1720 for (size_t i
= 0; i
< md_len
; i
++)
1721 fprintf(f
, "%02x", md_value
[i
]);
1724 fprintf(f
, "# For additional config options, please look at lxc.container.conf(5)\n");
1725 fprintf(f
, "\n# Uncomment the following line to support nesting containers:\n");
1726 fprintf(f
, "#lxc.include = " LXCTEMPLATECONFIG
"/nesting.conf\n");
1727 fprintf(f
, "# (Be aware this has security implications)\n\n");
1728 nbytes
= fwrite(contents
, 1, flen
, f
);
1729 if (nbytes
< 0 || nbytes
!= flen
) {
1730 SYSERROR("Writing original contents");
1750 SYSERROR("Error prepending header");
1757 static void lxcapi_clear_config(struct lxc_container
*c
)
1759 if (!c
|| !c
->lxc_conf
)
1762 lxc_conf_free(c
->lxc_conf
);
1766 #define do_lxcapi_clear_config(c) lxcapi_clear_config(c)
1770 * create a container with the given parameters.
1771 * @c: container to be created. It has the lxcpath, name, and a starting
1772 * configuration already set
1773 * @t: the template to execute to instantiate the root filesystem and
1774 * adjust the configuration.
1775 * @bdevtype: backing store type to use. If NULL, dir will be used.
1776 * @specs: additional parameters for the backing store, i.e. LVM vg to
1779 * @argv: the arguments to pass to the template, terminated by NULL. If no
1780 * arguments, you can just pass NULL.
1782 static bool __lxcapi_create(struct lxc_container
*c
, const char *t
,
1783 const char *bdevtype
, struct bdev_specs
*specs
,
1784 int flags
, char *const argv
[])
1786 __do_close
int fd_rootfs
= -EBADF
;
1787 __do_free
char *path_template
= NULL
;
1791 bool bret
= false, rootfs_managed
= true;
1797 path_template
= get_template_path(t
);
1799 return log_error(false, "Template \"%s\" not found", t
);
1802 /* If a template is passed in, and the rootfs already is defined in the
1803 * container config and exists, then the caller is trying to create an
1804 * existing container. Return an error, but do NOT delete the container.
1806 if (do_lxcapi_is_defined(c
) && c
->lxc_conf
&& c
->lxc_conf
->rootfs
.path
&&
1807 access(c
->lxc_conf
->rootfs
.path
, F_OK
) == 0 && path_template
)
1808 return log_error(false, "Container \"%s\" already exists in \"%s\"",
1809 c
->name
, c
->config_path
);
1812 !do_lxcapi_load_config(c
, lxc_global_config_value("lxc.default_config")))
1813 return log_error(false, "Failed to load default configuration file %s",
1814 lxc_global_config_value("lxc.default_config"));
1816 fd_rootfs
= create_container_dir(c
);
1818 return log_error(false, "Failed to create container %s", c
->name
);
1820 if (c
->lxc_conf
->rootfs
.path
)
1821 rootfs_managed
= false;
1823 /* If both template and rootfs.path are set, template is setup as
1824 * rootfs.path. The container is already created if we have a config and
1825 * rootfs.path is accessible
1827 if (!c
->lxc_conf
->rootfs
.path
&& !path_template
) {
1828 /* No template passed in and rootfs does not exist. */
1829 if (!c
->save_config(c
, NULL
)) {
1830 ERROR("Failed to save initial config for \"%s\"", c
->name
);
1838 /* Rootfs passed into configuration, but does not exist. */
1839 if (c
->lxc_conf
->rootfs
.path
&& access(c
->lxc_conf
->rootfs
.path
, F_OK
) != 0) {
1840 ERROR("The rootfs \"%s\" does not exist", c
->lxc_conf
->rootfs
.path
);
1844 if (do_lxcapi_is_defined(c
) && c
->lxc_conf
->rootfs
.path
&& !path_template
) {
1845 /* Rootfs already existed, user just wanted to save the loaded
1848 if (!c
->save_config(c
, NULL
))
1849 ERROR("Failed to save initial config for \"%s\"", c
->name
);
1855 /* Mark that this container as being created */
1856 partial_fd
= create_partial(fd_rootfs
, c
);
1857 if (partial_fd
< 0) {
1858 SYSERROR("Failed to mark container as being partially created");
1862 /* No need to get disk lock bc we have the partial lock. */
1866 /* Create the storage.
1867 * Note we can't do this in the same task as we use to execute the
1868 * template because of the way zfs works.
1869 * After you 'zfs create', zfs mounts the fs only in the initial
1874 SYSERROR("Failed to fork task for container creation template");
1878 if (pid
== 0) { /* child */
1879 struct lxc_storage
*bdev
= NULL
;
1881 bdev
= do_storage_create(c
, bdevtype
, specs
);
1883 ERROR("Failed to create %s storage for %s",
1884 bdevtype
? bdevtype
: "(none)", c
->name
);
1885 _exit(EXIT_FAILURE
);
1888 /* Save config file again to store the new rootfs location. */
1889 if (!do_lxcapi_save_config(c
, NULL
)) {
1890 ERROR("Failed to save initial config for %s", c
->name
);
1891 /* Parent task won't see the storage driver in the
1892 * config so we delete it.
1894 bdev
->ops
->umount(bdev
);
1895 bdev
->ops
->destroy(bdev
);
1896 _exit(EXIT_FAILURE
);
1899 _exit(EXIT_SUCCESS
);
1902 if (!wait_exited(pid
))
1905 /* Reload config to get the rootfs. */
1906 lxc_conf_free(c
->lxc_conf
);
1909 if (!load_config_locked(c
, c
->configfile
))
1912 if (!create_run_template(c
, path_template
, !!(flags
& LXC_CREATE_QUIET
), argv
))
1915 /* Now clear out the lxc_conf we have, reload from the created
1918 do_lxcapi_clear_config(c
);
1921 if (!prepend_lxc_header(c
->configfile
, path_template
, argv
)) {
1922 ERROR("Failed to prepend header to config file");
1927 bret
= load_config_locked(c
, c
->configfile
);
1931 remove_partial(c
, partial_fd
);
1935 bool reset_managed
= c
->lxc_conf
->rootfs
.managed
;
1938 * Ensure that we don't destroy storage we didn't create
1941 if (!rootfs_managed
)
1942 c
->lxc_conf
->rootfs
.managed
= false;
1943 container_destroy(c
, NULL
);
1944 c
->lxc_conf
->rootfs
.managed
= reset_managed
;
1950 static bool do_lxcapi_create(struct lxc_container
*c
, const char *t
,
1951 const char *bdevtype
, struct bdev_specs
*specs
,
1952 int flags
, char *const argv
[])
1954 return __lxcapi_create(c
, t
, bdevtype
, specs
, flags
, argv
);
1957 WRAP_API_5(bool, lxcapi_create
, const char *, const char *,
1958 struct bdev_specs
*, int, char *const *)
1960 static bool do_lxcapi_reboot(struct lxc_container
*c
)
1962 __do_close
int pidfd
= -EBADF
;
1965 int rebootsignal
= SIGINT
;
1970 if (!do_lxcapi_is_running(c
))
1973 pidfd
= do_lxcapi_init_pidfd(c
);
1975 pid
= do_lxcapi_init_pid(c
);
1980 if (c
->lxc_conf
&& c
->lxc_conf
->rebootsignal
)
1981 rebootsignal
= c
->lxc_conf
->rebootsignal
;
1984 ret
= lxc_raw_pidfd_send_signal(pidfd
, rebootsignal
, NULL
, 0);
1986 ret
= kill(pid
, rebootsignal
);
1988 return log_warn(false, "Failed to send signal %d to pid %d",
1994 WRAP_API(bool, lxcapi_reboot
)
1996 static bool do_lxcapi_reboot2(struct lxc_container
*c
, int timeout
)
1998 __do_close
int pidfd
= -EBADF
, state_client_fd
= -EBADF
;
1999 int rebootsignal
= SIGINT
;
2001 lxc_state_t states
[MAX_STATE
] = {0};
2007 if (!do_lxcapi_is_running(c
))
2010 pidfd
= do_lxcapi_init_pidfd(c
);
2012 pid
= do_lxcapi_init_pid(c
);
2017 if (c
->lxc_conf
&& c
->lxc_conf
->rebootsignal
)
2018 rebootsignal
= c
->lxc_conf
->rebootsignal
;
2020 /* Add a new state client before sending the shutdown signal so that we
2021 * don't miss a state.
2024 states
[RUNNING
] = 2;
2025 ret
= lxc_cmd_add_state_client(c
->name
, c
->config_path
, states
,
2030 if (state_client_fd
< 0)
2036 if (ret
< MAX_STATE
)
2040 /* Send reboot signal to container. */
2042 killret
= lxc_raw_pidfd_send_signal(pidfd
, rebootsignal
, NULL
, 0);
2044 killret
= kill(pid
, rebootsignal
);
2046 return log_warn(false, "Failed to send signal %d to pidfd(%d)/pid(%d)", rebootsignal
, pidfd
, pid
);
2047 TRACE("Sent signal %d to pidfd(%d)/pid(%d)", rebootsignal
, pidfd
, pid
);
2052 ret
= lxc_cmd_sock_rcv_state(state_client_fd
, timeout
);
2056 TRACE("Received state \"%s\"", lxc_state2str(ret
));
2063 WRAP_API_1(bool, lxcapi_reboot2
, int)
2065 static bool do_lxcapi_shutdown(struct lxc_container
*c
, int timeout
)
2067 __do_close
int pidfd
= -EBADF
, state_client_fd
= -EBADF
;
2068 int haltsignal
= SIGPWR
;
2070 lxc_state_t states
[MAX_STATE
] = {0};
2076 if (!do_lxcapi_is_running(c
))
2079 pidfd
= do_lxcapi_init_pidfd(c
);
2080 pid
= do_lxcapi_init_pid(c
);
2084 /* Detect whether we should send SIGRTMIN + 3 (e.g. systemd). */
2085 if (c
->lxc_conf
&& c
->lxc_conf
->haltsignal
)
2086 haltsignal
= c
->lxc_conf
->haltsignal
;
2087 else if (task_blocks_signal(pid
, (SIGRTMIN
+ 3)))
2088 haltsignal
= (SIGRTMIN
+ 3);
2092 * Add a new state client before sending the shutdown signal so
2093 * that we don't miss a state.
2096 states
[STOPPED
] = 1;
2097 ret
= lxc_cmd_add_state_client(c
->name
, c
->config_path
, states
,
2102 if (state_client_fd
< 0)
2108 if (ret
< MAX_STATE
)
2113 struct pollfd pidfd_poll
= {
2118 killret
= lxc_raw_pidfd_send_signal(pidfd
, haltsignal
,
2121 return log_warn(false, "Failed to send signal %d to pidfd %d",
2124 TRACE("Sent signal %d to pidfd %d", haltsignal
, pidfd
);
2127 * No need for going through all of the state server
2128 * complications anymore. We can just poll on pidfds. :)
2132 ret
= poll(&pidfd_poll
, 1, timeout
* 1000);
2133 if (ret
< 0 || !(pidfd_poll
.revents
& POLLIN
))
2136 TRACE("Pidfd polling detected container exit");
2139 killret
= kill(pid
, haltsignal
);
2141 return log_warn(false, "Failed to send signal %d to pid %d",
2144 TRACE("Sent signal %d to pid %d", haltsignal
, pid
);
2150 ret
= lxc_cmd_sock_rcv_state(state_client_fd
, timeout
);
2154 TRACE("Received state \"%s\"", lxc_state2str(ret
));
2161 WRAP_API_1(bool, lxcapi_shutdown
, int)
2163 static bool lxcapi_createl(struct lxc_container
*c
, const char *t
,
2164 const char *bdevtype
, struct bdev_specs
*specs
, int flags
, ...)
2173 current_config
= c
->lxc_conf
;
2176 * since we're going to wait for create to finish, I don't think we
2177 * need to get a copy of the arguments.
2179 va_start(ap
, flags
);
2180 args
= lxc_va_arg_list_to_argv(ap
, 0, 0);
2183 ERROR("Failed to allocate memory");
2187 bret
= __lxcapi_create(c
, t
, bdevtype
, specs
, flags
, args
);
2191 current_config
= NULL
;
2195 static void do_clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
)
2197 if (strequal(key
, "lxc.cgroup"))
2198 return clear_unexp_config_line(conf
, key
, true);
2200 if (strequal(key
, "lxc.network"))
2201 return clear_unexp_config_line(conf
, key
, true);
2203 if (strequal(key
, "lxc.net"))
2204 return clear_unexp_config_line(conf
, key
, true);
2206 /* Clear a network with a specific index. */
2207 if (strnequal(key
, "lxc.net.", 8)) {
2212 ret
= lxc_safe_uint(idx
, &(unsigned int){0});
2214 return clear_unexp_config_line(conf
, key
, true);
2217 if (strequal(key
, "lxc.hook"))
2218 return clear_unexp_config_line(conf
, key
, true);
2220 return clear_unexp_config_line(conf
, key
, false);
2223 static bool do_lxcapi_clear_config_item(struct lxc_container
*c
,
2227 struct lxc_config_t
*config
;
2229 if (!c
|| !c
->lxc_conf
)
2232 if (container_mem_lock(c
))
2235 config
= lxc_get_config(key
);
2237 ret
= config
->clr(key
, c
->lxc_conf
, NULL
);
2239 do_clear_unexp_config_line(c
->lxc_conf
, key
);
2241 container_mem_unlock(c
);
2245 WRAP_API_1(bool, lxcapi_clear_config_item
, const char *)
2247 static inline bool enter_net_ns(struct lxc_container
*c
)
2249 pid_t pid
= do_lxcapi_init_pid(c
);
2254 if ((geteuid() != 0 || (c
->lxc_conf
&& !list_empty(&c
->lxc_conf
->id_map
))) &&
2255 (access("/proc/self/ns/user", F_OK
) == 0))
2256 if (!switch_to_ns(pid
, "user"))
2259 return switch_to_ns(pid
, "net");
2262 /* Used by qsort and bsearch functions for comparing names. */
2263 static inline int string_cmp(char **first
, char **second
)
2265 return strcmp(*first
, *second
);
2268 /* Used by qsort and bsearch functions for comparing container names. */
2269 static inline int container_cmp(struct lxc_container
**first
,
2270 struct lxc_container
**second
)
2272 return strcmp((*first
)->name
, (*second
)->name
);
2275 static bool add_to_array(char ***names
, char *cname
, int pos
)
2277 __do_free
char *dup_cname
= NULL
;
2280 dup_cname
= strdup(cname
);
2284 newnames
= realloc(*names
, (pos
+ 1) * sizeof(char *));
2286 return ret_set_errno(false, ENOMEM
);
2288 newnames
[pos
] = move_ptr(dup_cname
);
2290 /* Sort the array as we will use binary search on it. */
2291 qsort(newnames
, pos
+ 1, sizeof(char *),
2292 (int (*)(const void *, const void *))string_cmp
);
2298 static bool add_to_clist(struct lxc_container
***list
, struct lxc_container
*c
,
2301 struct lxc_container
**newlist
;
2303 newlist
= realloc(*list
, (pos
+ 1) * sizeof(struct lxc_container
*));
2305 return ret_set_errno(false, ENOMEM
);
2309 /* Sort the array as we will use binary search on it. */
2311 qsort(newlist
, pos
+ 1, sizeof(struct lxc_container
*),
2312 (int (*)(const void *, const void *))container_cmp
);
2318 static char **get_from_array(char ***names
, char *cname
, int size
)
2323 return bsearch(&cname
, *names
, size
, sizeof(char *),
2324 (int (*)(const void *, const void *))string_cmp
);
2327 static bool array_contains(char ***names
, char *cname
, int size
)
2329 if (get_from_array(names
, cname
, size
))
2335 static char **do_lxcapi_get_interfaces(struct lxc_container
*c
)
2338 int i
, count
= 0, pipefd
[2];
2339 char **interfaces
= NULL
;
2340 char interface
[IFNAMSIZ
];
2342 if (pipe2(pipefd
, O_CLOEXEC
))
2343 return log_error_errno(NULL
, errno
, "Failed to create pipe");
2349 return log_error_errno(NULL
, errno
, "Failed to fork task to get interfaces information");
2353 call_cleaner(netns_freeifaddrs
) struct netns_ifaddrs
*ifaddrs
= NULL
;
2354 struct netns_ifaddrs
*ifa
= NULL
;
2358 /* close the read-end of the pipe */
2361 if (!enter_net_ns(c
)) {
2362 SYSERROR("Failed to enter network namespace");
2366 /* Grab the list of interfaces */
2367 if (netns_getifaddrs(&ifaddrs
, -1, &(bool){false})) {
2368 SYSERROR("Failed to get interfaces list");
2372 /* Iterate through the interfaces */
2373 for (ifa
= ifaddrs
; ifa
!= NULL
;
2374 ifa
= ifa
->ifa_next
) {
2375 nbytes
= lxc_write_nointr(pipefd
[1], ifa
->ifa_name
, IFNAMSIZ
);
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
)) {
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 return log_error_errno(NULL
, errno
, "Failed to create pipe");
2441 SYSERROR("Failed to create new process");
2448 call_cleaner(netns_freeifaddrs
) struct netns_ifaddrs
*ifaddrs
= NULL
;
2449 struct netns_ifaddrs
*ifa
= NULL
;
2451 char addressOutputBuffer
[INET6_ADDRSTRLEN
];
2452 char *address_ptr
= NULL
;
2453 void *address_ptr_tmp
= NULL
;
2455 /* close the read-end of the pipe */
2458 if (!enter_net_ns(c
)) {
2459 SYSERROR("Failed to attach to network namespace");
2463 /* Grab the list of interfaces */
2464 if (netns_getifaddrs(&ifaddrs
, -1, &(bool){false})) {
2465 SYSERROR("Failed to get interfaces list");
2469 /* Iterate through the interfaces */
2470 for (ifa
= ifaddrs
; ifa
; ifa
= ifa
->ifa_next
) {
2471 if (ifa
->ifa_addr
== NULL
)
2474 #pragma GCC diagnostic push
2475 #pragma GCC diagnostic ignored "-Wcast-align"
2477 if (ifa
->ifa_addr
->sa_family
== AF_INET
) {
2478 if (family
&& !strequal(family
, "inet"))
2481 address_ptr_tmp
= &((struct sockaddr_in
*)ifa
->ifa_addr
)->sin_addr
;
2483 if (family
&& !strequal(family
, "inet6"))
2486 if (((struct sockaddr_in6
*)ifa
->ifa_addr
)->sin6_scope_id
!= (uint32_t)scope
)
2489 address_ptr_tmp
= &((struct sockaddr_in6
*)ifa
->ifa_addr
)->sin6_addr
;
2492 #pragma GCC diagnostic pop
2494 if (interface
&& !strequal(interface
, ifa
->ifa_name
))
2496 else if (!interface
&& strequal("lo", ifa
->ifa_name
))
2499 address_ptr
= (char *)inet_ntop(ifa
->ifa_addr
->sa_family
, address_ptr_tmp
,
2500 addressOutputBuffer
,
2501 sizeof(addressOutputBuffer
));
2505 nbytes
= lxc_write_nointr(pipefd
[1], address_ptr
, INET6_ADDRSTRLEN
);
2506 if (nbytes
!= INET6_ADDRSTRLEN
) {
2507 SYSERROR("Failed to send ipv6 address \"%s\"", address_ptr
);
2517 /* close the write-end of the pipe, thus sending EOF to the reader */
2522 /* close the write-end of the pipe */
2525 while (lxc_read_nointr(pipefd
[0], &address
, INET6_ADDRSTRLEN
) == INET6_ADDRSTRLEN
) {
2526 address
[INET6_ADDRSTRLEN
- 1] = '\0';
2528 if (!add_to_array(&addresses
, address
, count
))
2529 ERROR("PARENT: add_to_array failed");
2534 if (wait_for_pid(pid
)) {
2535 for (i
= 0; i
< count
; i
++)
2542 /* close the read-end of the pipe */
2545 /* Append NULL to the array */
2547 addresses
= (char **)lxc_append_null_to_array((void **)addresses
, count
);
2552 WRAP_API_3(char **, lxcapi_get_ips
, const char *, const char *, int)
2554 static int do_lxcapi_get_config_item(struct lxc_container
*c
, const char *key
, char *retv
, int inlen
)
2557 struct lxc_config_t
*config
;
2559 if (!c
|| !c
->lxc_conf
)
2562 if (container_mem_lock(c
))
2565 config
= lxc_get_config(key
);
2567 ret
= config
->get(key
, retv
, inlen
, c
->lxc_conf
, NULL
);
2569 container_mem_unlock(c
);
2573 WRAP_API_3(int, lxcapi_get_config_item
, const char *, char *, int)
2575 static char* do_lxcapi_get_running_config_item(struct lxc_container
*c
, const char *key
)
2579 if (!c
|| !c
->lxc_conf
)
2582 if (container_mem_lock(c
))
2585 ret
= lxc_cmd_get_config_item(c
->name
, key
, do_lxcapi_get_config_path(c
));
2586 container_mem_unlock(c
);
2590 WRAP_API_1(char *, lxcapi_get_running_config_item
, const char *)
2592 static int do_lxcapi_get_keys(struct lxc_container
*c
, const char *key
, char *retv
, int inlen
)
2596 /* List all config items. */
2598 return lxc_list_config_items(retv
, inlen
);
2600 if (!c
|| !c
->lxc_conf
)
2603 if (container_mem_lock(c
))
2606 /* Support 'lxc.net.<idx>', i.e. 'lxc.net.0'
2607 * This is an intelligent result to show which keys are valid given the
2608 * type of nic it is.
2610 if (strnequal(key
, "lxc.net.", 8))
2611 ret
= lxc_list_net(c
->lxc_conf
, key
, retv
, inlen
);
2613 ret
= lxc_list_subkeys(c
->lxc_conf
, key
, retv
, inlen
);
2615 container_mem_unlock(c
);
2619 WRAP_API_3(int, lxcapi_get_keys
, const char *, char *, int)
2621 static bool do_lxcapi_save_config(struct lxc_container
*c
, const char *alt_file
)
2623 __do_close
int fd_config
= -EBADF
, fd_rootfs
= -EBADF
;
2625 bool bret
= false, need_disklock
= false;
2628 alt_file
= c
->configfile
;
2631 return log_error(false, "No config file found");
2633 /* If we haven't yet loaded a config, load the stock config. */
2635 if (!do_lxcapi_load_config(c
, lxc_global_config_value("lxc.default_config"))) {
2636 return log_error(false, "Error loading default configuration file %s while saving %s",
2637 lxc_global_config_value("lxc.default_config"), c
->name
);
2641 fd_rootfs
= create_container_dir(c
);
2643 return log_error(false, "Failed to create container directory");
2645 /* If we're writing to the container's config file, take the disk lock.
2646 * Otherwise just take the memlock to protect the struct lxc_container
2647 * while we're traversing it.
2649 if (strequal(c
->configfile
, alt_file
))
2650 need_disklock
= true;
2653 lret
= container_disk_lock(c
);
2655 lret
= container_mem_lock(c
);
2657 return log_error(false, "Failed to acquire lock");
2659 fd_config
= open(alt_file
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_CLOEXEC
,
2660 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2661 if (fd_config
< 0) {
2662 SYSERROR("Failed to open config file \"%s\"", alt_file
);
2666 lret
= write_config(fd_config
, c
->lxc_conf
);
2668 SYSERROR("Failed to write config file \"%s\"", alt_file
);
2673 TRACE("Saved config file \"%s\"", alt_file
);
2677 container_disk_unlock(c
);
2679 container_mem_unlock(c
);
2684 WRAP_API_1(bool, lxcapi_save_config
, const char *)
2687 static bool mod_rdep(struct lxc_container
*c0
, struct lxc_container
*c
, bool inc
)
2693 char path
[PATH_MAX
];
2694 char newpath
[PATH_MAX
];
2695 int fd
, ret
, n
= 0, v
= 0;
2697 size_t len
= 0, bytes
= 0;
2699 if (container_disk_lock(c0
))
2702 ret
= strnprintf(path
, sizeof(path
), "%s/%s/lxc_snapshots", c0
->config_path
, c0
->name
);
2706 ret
= strnprintf(newpath
, sizeof(newpath
), "%s\n%s\n", c
->config_path
, c
->name
);
2710 /* If we find an lxc-snapshot file using the old format only listing the
2711 * number of snapshots we will keep using it. */
2712 f1
= fopen(path
, "re");
2714 n
= fscanf(f1
, "%d", &v
);
2716 if (n
== 1 && v
== 0) {
2719 SYSERROR("Failed to remove \"%s\"", path
);
2727 f1
= fopen(path
, "we");
2731 if (fprintf(f1
, "%d\n", v
) < 0) {
2732 ERROR("Error writing new snapshots value");
2739 SYSERROR("Error writing to or closing snapshots file");
2743 /* Here we know that we have or can use an lxc-snapshot file
2744 * using the new format. */
2746 f1
= fopen(path
, "ae");
2750 if (fprintf(f1
, "%s", newpath
) < 0) {
2751 ERROR("Error writing new snapshots entry");
2754 SYSERROR("Error writing to or closing snapshots file");
2760 SYSERROR("Error writing to or closing snapshots file");
2764 if ((fd
= open(path
, O_RDWR
| O_CLOEXEC
)) < 0)
2767 if (fstat(fd
, &fbuf
) < 0) {
2772 if (fbuf
.st_size
!= 0) {
2773 buf
= lxc_strmmap(NULL
, fbuf
.st_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
2774 if (buf
== MAP_FAILED
) {
2775 SYSERROR("Failed to create mapping %s", path
);
2780 len
= strlen(newpath
);
2781 while ((del
= strstr((char *)buf
, newpath
))) {
2782 memmove(del
, del
+ len
, strlen(del
) - len
+ 1);
2786 lxc_strmunmap(buf
, fbuf
.st_size
);
2787 if (ftruncate(fd
, fbuf
.st_size
- bytes
) < 0) {
2788 SYSERROR("Failed to truncate file %s", path
);
2797 /* If the lxc-snapshot file is empty, remove it. */
2798 if (stat(path
, &fbuf
) < 0)
2801 if (!fbuf
.st_size
) {
2804 SYSERROR("Failed to remove \"%s\"", path
);
2811 container_disk_unlock(c0
);
2815 void mod_all_rdeps(struct lxc_container
*c
, bool inc
)
2817 __do_free
char *lxcpath
= NULL
, *lxcname
= NULL
;
2818 __do_fclose
FILE *f
= NULL
;
2819 size_t pathlen
= 0, namelen
= 0;
2820 struct lxc_container
*p
;
2821 char path
[PATH_MAX
];
2824 ret
= strnprintf(path
, sizeof(path
), "%s/%s/lxc_rdepends",
2825 c
->config_path
, c
->name
);
2827 ERROR("Path name too long");
2831 f
= fopen(path
, "re");
2835 while (getline(&lxcpath
, &pathlen
, f
) != -1) {
2836 if (getline(&lxcname
, &namelen
, f
) == -1) {
2837 ERROR("badly formatted file %s", path
);
2841 remove_trailing_newlines(lxcpath
);
2842 remove_trailing_newlines(lxcname
);
2844 if ((p
= lxc_container_new(lxcname
, lxcpath
)) == NULL
) {
2845 ERROR("Unable to find dependent container %s:%s",
2850 if (!mod_rdep(p
, c
, inc
))
2851 ERROR("Failed to update snapshots file for %s:%s",
2854 lxc_container_put(p
);
2858 static bool has_fs_snapshots(struct lxc_container
*c
)
2860 __do_fclose
FILE *f
= NULL
;
2861 char path
[PATH_MAX
];
2865 ret
= strnprintf(path
, sizeof(path
), "%s/%s/lxc_snapshots", c
->config_path
,
2870 /* If the file doesn't exist there are no snapshots. */
2871 if (stat(path
, &fbuf
) < 0)
2876 f
= fopen(path
, "re");
2880 ret
= fscanf(f
, "%d", &v
);
2882 INFO("Container uses new lxc-snapshots format %s", path
);
2888 static bool has_snapshots(struct lxc_container
*c
)
2890 __do_closedir
DIR *dir
= NULL
;
2891 char path
[PATH_MAX
];
2892 struct dirent
*direntp
;
2895 if (!get_snappath_dir(c
, path
))
2898 dir
= opendir(path
);
2902 while ((direntp
= readdir(dir
))) {
2903 if (strequal(direntp
->d_name
, "."))
2906 if (strequal(direntp
->d_name
, ".."))
2915 static bool do_destroy_container(struct lxc_conf
*conf
) {
2918 if (am_guest_unpriv()) {
2919 ret
= userns_exec_full(conf
, storage_destroy_wrapper
, conf
,
2920 "storage_destroy_wrapper");
2927 return storage_destroy(conf
);
2930 static int lxc_rmdir_onedev_wrapper(void *data
)
2932 char *arg
= (char *) data
;
2933 return lxc_rmdir_onedev(arg
, "snaps");
2936 static int lxc_unlink_exec_wrapper(void *data
)
2942 static bool container_destroy(struct lxc_container
*c
,
2943 struct lxc_storage
*storage
)
2947 struct lxc_conf
*conf
;
2952 if (!c
|| !do_lxcapi_is_defined(c
))
2956 if (container_disk_lock(c
))
2959 if (!is_stopped(c
)) {
2960 /* We should queue some sort of error - in c->error_string? */
2961 ERROR("container %s is not stopped", c
->name
);
2965 if (conf
&& !list_empty(&conf
->hooks
[LXCHOOK_DESTROY
])) {
2966 /* Start of environment variable setup for hooks */
2967 if (setenv("LXC_NAME", c
->name
, 1))
2968 SYSERROR("Failed to set environment variable for container name");
2970 if (conf
->rcfile
&& setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1))
2971 SYSERROR("Failed to set environment variable for config path");
2973 if (conf
->rootfs
.mount
&& setenv("LXC_ROOTFS_MOUNT", conf
->rootfs
.mount
, 1))
2974 SYSERROR("Failed to set environment variable for rootfs mount");
2976 if (conf
->rootfs
.path
&& setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1))
2977 SYSERROR("Failed to set environment variable for rootfs mount");
2979 if (conf
->console
.path
&& setenv("LXC_CONSOLE", conf
->console
.path
, 1))
2980 SYSERROR("Failed to set environment variable for console path");
2982 if (conf
->console
.log_path
&& setenv("LXC_CONSOLE_LOGPATH", conf
->console
.log_path
, 1))
2983 SYSERROR("Failed to set environment variable for console log");
2984 /* End of environment variable setup for hooks */
2986 if (run_lxc_hooks(c
->name
, "destroy", conf
, NULL
)) {
2987 ERROR("Failed to execute clone hook for \"%s\"", c
->name
);
2992 if (current_config
&& conf
== current_config
) {
2993 current_config
= NULL
;
2995 if (conf
->logfd
!= -1) {
3001 /* LXC is not managing the storage of the container. */
3002 if (conf
&& !conf
->rootfs
.managed
)
3005 if (conf
&& conf
->rootfs
.path
&& conf
->rootfs
.mount
) {
3006 if (!do_destroy_container(conf
)) {
3007 ERROR("Error destroying rootfs for %s", c
->name
);
3010 INFO("Destroyed rootfs for %s", c
->name
);
3013 mod_all_rdeps(c
, false);
3015 p1
= do_lxcapi_get_config_path(c
);
3024 * strlen("config") = 6
3028 len
= strlen(p1
) + 1 + strlen(c
->name
) + 1 + strlen(LXC_CONFIG_FNAME
) + 1;
3031 ERROR("Failed to allocate memory");
3035 /* For an overlay container the rootfs is considered immutable and
3036 * cannot be removed when restoring from a snapshot.
3038 if (storage
&& (strequal(storage
->type
, "overlay") ||
3039 strequal(storage
->type
, "overlayfs")) &&
3040 (storage
->flags
& LXC_STORAGE_INTERNAL_OVERLAY_RESTORE
)) {
3041 ret
= strnprintf(path
, len
, "%s/%s/%s", p1
, c
->name
, LXC_CONFIG_FNAME
);
3045 if (am_guest_unpriv())
3046 ret
= userns_exec_1(conf
, lxc_unlink_exec_wrapper
, path
,
3047 "lxc_unlink_exec_wrapper");
3051 SYSERROR("Failed to destroy config file \"%s\" for \"%s\"",
3055 INFO("Destroyed config file \"%s\" for \"%s\"", path
, c
->name
);
3061 ret
= strnprintf(path
, len
, "%s/%s", p1
, c
->name
);
3065 if (am_guest_unpriv())
3066 ret
= userns_exec_full(conf
, lxc_rmdir_onedev_wrapper
, path
,
3067 "lxc_rmdir_onedev_wrapper");
3069 ret
= lxc_rmdir_onedev(path
, "snaps");
3071 ERROR("Failed to destroy directory \"%s\" for \"%s\"", path
,
3075 INFO("Destroyed directory \"%s\" for \"%s\"", path
, c
->name
);
3084 container_disk_unlock(c
);
3088 static bool do_lxcapi_destroy(struct lxc_container
*c
)
3090 if (!c
|| !lxcapi_is_defined(c
))
3093 if (c
->lxc_conf
&& c
->lxc_conf
->rootfs
.managed
) {
3094 if (has_snapshots(c
)) {
3095 ERROR("Container %s has snapshots; not removing", c
->name
);
3099 if (has_fs_snapshots(c
)) {
3100 ERROR("container %s has snapshots on its rootfs", c
->name
);
3105 return container_destroy(c
, NULL
);
3108 WRAP_API(bool, lxcapi_destroy
)
3110 static bool do_lxcapi_destroy_with_snapshots(struct lxc_container
*c
)
3112 if (!c
|| !lxcapi_is_defined(c
))
3115 if (!lxcapi_snapshot_destroy_all(c
)) {
3116 ERROR("Error deleting all snapshots");
3120 return lxcapi_destroy(c
);
3123 WRAP_API(bool, lxcapi_destroy_with_snapshots
)
3125 int lxc_set_config_item_locked(struct lxc_conf
*conf
, const char *key
,
3129 struct lxc_config_t
*config
;
3132 config
= lxc_get_config(key
);
3134 ret
= config
->set(key
, v
, conf
, NULL
);
3138 if (lxc_config_value_empty(v
))
3139 do_clear_unexp_config_line(conf
, key
);
3141 bret
= do_append_unexp_config_line(conf
, key
, v
);
3148 static bool do_set_config_item_locked(struct lxc_container
*c
, const char *key
,
3154 c
->lxc_conf
= lxc_conf_init();
3159 ret
= lxc_set_config_item_locked(c
->lxc_conf
, key
, v
);
3166 static bool do_lxcapi_set_config_item(struct lxc_container
*c
, const char *key
, const char *v
)
3173 if (container_mem_lock(c
))
3176 b
= do_set_config_item_locked(c
, key
, v
);
3178 container_mem_unlock(c
);
3182 WRAP_API_2(bool, lxcapi_set_config_item
, const char *, const char *)
3184 static char *lxcapi_config_file_name(struct lxc_container
*c
)
3186 if (!c
|| !c
->configfile
)
3189 return strdup(c
->configfile
);
3192 static const char *lxcapi_get_config_path(struct lxc_container
*c
)
3194 if (!c
|| !c
->config_path
)
3197 return (const char *)(c
->config_path
);
3202 * Just recalculate the c->configfile based on the
3203 * c->config_path, which must be set.
3204 * The lxc_container must be locked or not yet public.
3206 static bool set_config_filename(struct lxc_container
*c
)
3211 if (!c
->config_path
)
3214 /* $lxc_path + "/" + c->name + "/" + "config" + '\0' */
3215 len
= strlen(c
->config_path
) + 1 + strlen(c
->name
) + 1 + strlen(LXC_CONFIG_FNAME
) + 1;
3216 newpath
= malloc(len
);
3220 ret
= strnprintf(newpath
, len
, "%s/%s/%s", c
->config_path
, c
->name
, LXC_CONFIG_FNAME
);
3222 fprintf(stderr
, "Error printing out config file name\n");
3227 free(c
->configfile
);
3228 c
->configfile
= newpath
;
3233 static bool do_lxcapi_set_config_path(struct lxc_container
*c
, const char *path
)
3237 char *oldpath
= NULL
;
3242 if (container_mem_lock(c
))
3247 ERROR("Out of memory setting new lxc path");
3253 oldpath
= c
->config_path
;
3256 /* Since we've changed the config path, we have to change the
3257 * config file name too */
3258 if (!set_config_filename(c
)) {
3259 ERROR("Out of memory setting new config filename");
3261 free(c
->config_path
);
3262 c
->config_path
= oldpath
;
3268 container_mem_unlock(c
);
3272 WRAP_API_1(bool, lxcapi_set_config_path
, const char *)
3274 static bool do_lxcapi_set_cgroup_item(struct lxc_container
*c
, const char *subsys
, const char *value
)
3276 call_cleaner(cgroup_exit
) struct cgroup_ops
*cgroup_ops
= NULL
;
3285 ret
= cgroup_set(c
->name
, c
->config_path
, subsys
, value
);
3286 if (ret
< 0 && ERRNO_IS_NOT_SUPPORTED(ret
)) {
3287 cgroup_ops
= cgroup_init(c
->lxc_conf
);
3291 ret
= cgroup_ops
->set(cgroup_ops
, subsys
, value
, c
->name
, c
->config_path
);
3297 WRAP_API_2(bool, lxcapi_set_cgroup_item
, const char *, const char *)
3299 static int do_lxcapi_get_cgroup_item(struct lxc_container
*c
, const char *subsys
, char *retv
, int inlen
)
3301 call_cleaner(cgroup_exit
) struct cgroup_ops
*cgroup_ops
= NULL
;
3310 ret
= cgroup_get(c
->name
, c
->config_path
, subsys
, retv
, inlen
);
3311 if (ret
< 0 && ERRNO_IS_NOT_SUPPORTED(ret
)) {
3312 cgroup_ops
= cgroup_init(c
->lxc_conf
);
3316 return cgroup_ops
->get(cgroup_ops
, subsys
, retv
, inlen
, c
->name
, c
->config_path
);
3322 WRAP_API_3(int, lxcapi_get_cgroup_item
, const char *, char *, int)
3324 const char *lxc_get_global_config_item(const char *key
)
3326 return lxc_global_config_value(key
);
3329 const char *lxc_get_version(void)
3334 static int copy_file(const char *old
, const char *new)
3341 if (file_exists(new)) {
3342 ERROR("copy destination %s exists", new);
3346 ret
= stat(old
, &sbuf
);
3348 INFO("Error stat'ing %s", old
);
3352 in
= open(old
, O_RDONLY
);
3354 SYSERROR("Error opening original file %s", old
);
3358 out
= open(new, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
3360 SYSERROR("Error opening new file %s", new);
3366 len
= lxc_read_nointr(in
, buf
, 8096);
3368 SYSERROR("Error reading old file %s", old
);
3375 ret
= lxc_write_nointr(out
, buf
, len
);
3376 if (ret
< len
) { /* should we retry? */
3377 SYSERROR("Error: write to new file %s was interrupted", new);
3385 /* We set mode, but not owner/group. */
3386 ret
= chmod(new, sbuf
.st_mode
);
3388 SYSERROR("Error setting mode on %s", new);
3400 static int copyhooks(struct lxc_container
*oldc
, struct lxc_container
*c
)
3402 __do_free
char *cpath
= NULL
;
3404 struct string_entry
*entry
;
3406 len
= strlen(oldc
->config_path
) + strlen(oldc
->name
) + 3;
3407 cpath
= malloc(len
);
3409 return ret_errno(ENOMEM
);
3411 ret
= strnprintf(cpath
, len
, "%s/%s/", oldc
->config_path
, oldc
->name
);
3415 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
3416 list_for_each_entry(entry
, &c
->lxc_conf
->hooks
[i
], head
) {
3417 __do_free
char *hookname
= NULL
;
3418 char *fname
, *new_hook
;
3419 char tmppath
[PATH_MAX
];
3421 fname
= strrchr(entry
->val
, '/');
3425 /* If this hook is public - ignore. */
3426 if (!strnequal(entry
->val
, cpath
, len
- 1))
3429 /* copy the script, and change the entry in confile */
3430 ret
= strnprintf(tmppath
, sizeof(tmppath
), "%s/%s/%s",
3431 c
->config_path
, c
->name
, fname
+1);
3435 ret
= copy_file(entry
->val
, tmppath
);
3439 new_hook
= strdup(tmppath
);
3441 return syserror("out of memory copying hook path");
3443 hookname
= move_ptr(entry
->val
);
3444 entry
->val
= move_ptr(new_hook
);
3448 if (!clone_update_unexp_hooks(c
->lxc_conf
, oldc
->config_path
,
3449 c
->config_path
, oldc
->name
, c
->name
)) {
3450 return syserror_ret(-1, "Error saving new hooks in clone");
3453 do_lxcapi_save_config(c
, NULL
);
3457 static int copy_fstab(struct lxc_container
*oldc
, struct lxc_container
*c
)
3459 char newpath
[PATH_MAX
];
3460 char *oldpath
= oldc
->lxc_conf
->fstab
;
3466 clear_unexp_config_line(c
->lxc_conf
, "lxc.mount.fstab", false);
3468 char *p
= strrchr(oldpath
, '/');
3472 ret
= strnprintf(newpath
, sizeof(newpath
), "%s/%s%s",
3473 c
->config_path
, c
->name
, p
);
3475 ERROR("error printing new path for %s", oldpath
);
3479 if (file_exists(newpath
)) {
3480 ERROR("error: fstab file %s exists", newpath
);
3484 if (copy_file(oldpath
, newpath
) < 0) {
3485 ERROR("error: copying %s to %s", oldpath
, newpath
);
3489 free(c
->lxc_conf
->fstab
);
3491 c
->lxc_conf
->fstab
= strdup(newpath
);
3492 if (!c
->lxc_conf
->fstab
) {
3493 ERROR("error: allocating pathname");
3497 if (!do_append_unexp_config_line(c
->lxc_conf
, "lxc.mount.fstab", newpath
)) {
3498 ERROR("error saving new lxctab");
3505 static void copy_rdepends(struct lxc_container
*c
, struct lxc_container
*c0
)
3507 char path0
[PATH_MAX
], path1
[PATH_MAX
];
3510 ret
= strnprintf(path0
, sizeof(path0
), "%s/%s/lxc_rdepends", c0
->config_path
,
3513 WARN("Error copying reverse dependencies");
3517 ret
= strnprintf(path1
, sizeof(path1
), "%s/%s/lxc_rdepends", c
->config_path
,
3520 WARN("Error copying reverse dependencies");
3524 if (copy_file(path0
, path1
) < 0) {
3525 INFO("Error copying reverse dependencies");
3530 static bool add_rdepends(struct lxc_container
*c
, struct lxc_container
*c0
)
3532 __do_fclose
FILE *f
= NULL
;
3534 char path
[PATH_MAX
];
3536 ret
= strnprintf(path
, sizeof(path
), "%s/%s/lxc_rdepends", c
->config_path
, c
->name
);
3540 f
= fopen(path
, "ae");
3544 /* If anything goes wrong, just return an error. */
3545 return fprintf(f
, "%s\n%s\n", c0
->config_path
, c0
->name
) > 0;
3549 * If the fs natively supports snapshot clones with no penalty,
3550 * then default to those even if not requested.
3551 * Currently we only do this for btrfs.
3553 static bool should_default_to_snapshot(struct lxc_container
*c0
,
3554 struct lxc_container
*c1
)
3556 __do_free
char *p0
= NULL
, *p1
= NULL
;
3558 size_t l0
= strlen(c0
->config_path
) + strlen(c0
->name
) + 2;
3559 size_t l1
= strlen(c1
->config_path
) + strlen(c1
->name
) + 2;
3560 char *rootfs
= c0
->lxc_conf
->rootfs
.path
;
3562 p0
= must_realloc(NULL
, l0
+ 1);
3563 p1
= must_realloc(NULL
, l1
+ 1);
3564 ret
= strnprintf(p0
, l0
, "%s/%s", c0
->config_path
, c0
->name
);
3568 ret
= strnprintf(p1
, l1
, "%s/%s", c1
->config_path
, c1
->name
);
3572 if (!is_btrfs_fs(p0
) || !is_btrfs_fs(p1
))
3575 if (is_btrfs_subvol(rootfs
) <= 0)
3578 return btrfs_same_fs(p0
, p1
) == 0;
3581 static int copy_storage(struct lxc_container
*c0
, struct lxc_container
*c
,
3582 const char *newtype
, int flags
, const char *bdevdata
,
3585 struct lxc_storage
*bdev
;
3588 if (should_default_to_snapshot(c0
, c
))
3589 flags
|= LXC_CLONE_SNAPSHOT
;
3591 bdev
= storage_copy(c0
, c
->name
, c
->config_path
, newtype
, flags
,
3592 bdevdata
, newsize
, &need_rdep
);
3594 ERROR("Error copying storage.");
3598 /* Set new rootfs. */
3599 free(c
->lxc_conf
->rootfs
.path
);
3600 c
->lxc_conf
->rootfs
.path
= strdup(bdev
->src
);
3603 if (!c
->lxc_conf
->rootfs
.path
) {
3604 ERROR("Out of memory while setting storage path.");
3608 /* Append a new lxc.rootfs.path entry to the unexpanded config. */
3609 clear_unexp_config_line(c
->lxc_conf
, "lxc.rootfs.path", false);
3610 if (!do_append_unexp_config_line(c
->lxc_conf
, "lxc.rootfs.path",
3611 c
->lxc_conf
->rootfs
.path
)) {
3612 ERROR("Error saving new rootfs to cloned config.");
3616 if (flags
& LXC_CLONE_SNAPSHOT
)
3617 copy_rdepends(c
, c0
);
3620 if (!add_rdepends(c
, c0
))
3621 WARN("Error adding reverse dependency from %s to %s",
3625 mod_all_rdeps(c
, true);
3630 struct clone_update_data
{
3631 struct lxc_container
*c0
;
3632 struct lxc_container
*c1
;
3637 static int clone_update_rootfs(struct clone_update_data
*data
)
3639 struct lxc_container
*c0
= data
->c0
;
3640 struct lxc_container
*c
= data
->c1
;
3641 int flags
= data
->flags
;
3642 char **hookargs
= data
->hookargs
;
3644 char path
[PATH_MAX
];
3645 struct lxc_storage
*bdev
;
3647 struct lxc_conf
*conf
= c
->lxc_conf
;
3649 /* update hostname in rootfs */
3650 /* we're going to mount, so run in a clean namespace to simplify cleanup */
3652 (void)lxc_drop_groups();
3654 if (setgid(0) < 0) {
3655 ERROR("Failed to setgid to 0");
3659 if (setuid(0) < 0) {
3660 ERROR("Failed to setuid to 0");
3664 if (unshare(CLONE_NEWNS
) < 0)
3667 ret
= lxc_storage_prepare(conf
);
3670 bdev
= conf
->rootfs
.storage
;
3672 if (!strequal(bdev
->type
, "dir")) {
3673 if (unshare(CLONE_NEWNS
) < 0) {
3674 ERROR("error unsharing mounts");
3675 lxc_storage_put(conf
);
3679 if (detect_shared_rootfs() && mount(NULL
, "/", NULL
, MS_SLAVE
| MS_REC
, NULL
))
3680 SYSERROR("Failed to recursively turn root mount tree into dependent mount. Continuing...");
3682 if (bdev
->ops
->mount(bdev
) < 0) {
3683 lxc_storage_put(conf
);
3686 } else { /* TODO come up with a better way */
3688 bdev
->dest
= strdup(lxc_storage_get_path(bdev
->src
, bdev
->type
));
3691 if (!list_empty(&conf
->hooks
[LXCHOOK_CLONE
])) {
3692 /* Start of environment variable setup for hooks */
3693 if (c0
->name
&& setenv("LXC_SRC_NAME", c0
->name
, 1))
3694 SYSERROR("failed to set environment variable for source container name");
3696 if (setenv("LXC_NAME", c
->name
, 1))
3697 SYSERROR("failed to set environment variable for container name");
3699 if (conf
->rcfile
&& setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1))
3700 SYSERROR("failed to set environment variable for config path");
3702 if (bdev
->dest
&& setenv("LXC_ROOTFS_MOUNT", bdev
->dest
, 1))
3703 SYSERROR("failed to set environment variable for rootfs mount");
3705 if (conf
->rootfs
.path
&& setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1))
3706 SYSERROR("failed to set environment variable for rootfs mount");
3708 if (run_lxc_hooks(c
->name
, "clone", conf
, hookargs
)) {
3709 ERROR("Error executing clone hook for %s", c
->name
);
3710 lxc_storage_put(conf
);
3715 if (!(flags
& LXC_CLONE_KEEPNAME
)) {
3716 ret
= strnprintf(path
, sizeof(path
), "%s/etc/hostname", bdev
->dest
);
3717 lxc_storage_put(conf
);
3722 if (!file_exists(path
))
3725 if (!(fout
= fopen(path
, "we"))) {
3726 SYSERROR("unable to open %s: ignoring", path
);
3730 if (fprintf(fout
, "%s", c
->name
) < 0) {
3735 if (fclose(fout
) < 0)
3738 lxc_storage_put(conf
);
3744 static int clone_update_rootfs_wrapper(void *data
)
3746 struct clone_update_data
*arg
= (struct clone_update_data
*) data
;
3747 return clone_update_rootfs(arg
);
3751 * We want to support:
3752 sudo lxc-clone -o o1 -n n1 -s -L|-fssize fssize -v|--vgname vgname \
3753 -p|--lvprefix lvprefix -t|--fstype fstype -B backingstore
3755 -s [ implies overlay]
3758 only rootfs gets converted (copied/snapshotted) on clone.
3761 static int create_file_dirname(char *path
, struct lxc_conf
*conf
)
3763 __do_close
int fd_rootfs
= -EBADF
;
3766 p
= strrchr(path
, '/');
3771 fd_rootfs
= do_create_container_dir(path
, conf
);
3774 return fd_rootfs
>= 0;
3777 static struct lxc_container
*do_lxcapi_clone(struct lxc_container
*c
, const char *newname
,
3778 const char *lxcpath
, int flags
,
3779 const char *bdevtype
, const char *bdevdata
, uint64_t newsize
,
3782 char newpath
[PATH_MAX
];
3784 struct clone_update_data data
;
3785 size_t saved_unexp_len
;
3787 int storage_copied
= 0;
3788 char *origroot
= NULL
, *saved_unexp_conf
= NULL
;
3789 struct lxc_container
*c2
= NULL
;
3791 if (!c
|| !do_lxcapi_is_defined(c
))
3794 if (container_mem_lock(c
))
3796 if (!is_stopped(c
) && !(flags
& LXC_CLONE_ALLOW_RUNNING
)) {
3797 ERROR("error: Original container (%s) is running. Use --allowrunning if you want to force a snapshot of the running container.", c
->name
);
3801 /* Make sure the container doesn't yet exist. */
3806 lxcpath
= do_lxcapi_get_config_path(c
);
3808 ret
= strnprintf(newpath
, sizeof(newpath
), "%s/%s/%s", lxcpath
, newname
, LXC_CONFIG_FNAME
);
3810 SYSERROR("clone: failed making config pathname");
3814 if (file_exists(newpath
)) {
3815 ERROR("error: clone: %s exists", newpath
);
3819 ret
= create_file_dirname(newpath
, c
->lxc_conf
);
3820 if (ret
< 0 && errno
!= EEXIST
) {
3821 ERROR("Error creating container dir for %s", newpath
);
3825 /* Copy the configuration. Tweak it as needed. */
3826 if (c
->lxc_conf
->rootfs
.path
) {
3827 origroot
= c
->lxc_conf
->rootfs
.path
;
3828 c
->lxc_conf
->rootfs
.path
= NULL
;
3831 fd
= open(newpath
, O_WRONLY
| O_CREAT
| O_CLOEXEC
,
3832 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3834 SYSERROR("Failed to open \"%s\"", newpath
);
3838 saved_unexp_conf
= c
->lxc_conf
->unexpanded_config
;
3839 saved_unexp_len
= c
->lxc_conf
->unexpanded_len
;
3840 c
->lxc_conf
->unexpanded_config
= strdup(saved_unexp_conf
);
3841 if (!c
->lxc_conf
->unexpanded_config
) {
3846 clear_unexp_config_line(c
->lxc_conf
, "lxc.rootfs.path", false);
3847 write_config(fd
, c
->lxc_conf
);
3850 c
->lxc_conf
->rootfs
.path
= origroot
;
3852 free(c
->lxc_conf
->unexpanded_config
);
3853 c
->lxc_conf
->unexpanded_config
= saved_unexp_conf
;
3854 saved_unexp_conf
= NULL
;
3855 c
->lxc_conf
->unexpanded_len
= saved_unexp_len
;
3857 ret
= strnprintf(newpath
, sizeof(newpath
), "%s/%s/%s", lxcpath
, newname
, LXC_ROOTFS_DNAME
);
3859 SYSERROR("clone: failed making rootfs pathname");
3863 ret
= mkdir(newpath
, 0755);
3865 /* For an overlay container the rootfs is considered immutable
3866 * and will not have been removed when restoring from a
3869 if (errno
!= ENOENT
&&
3870 !(flags
& LXC_STORAGE_INTERNAL_OVERLAY_RESTORE
)) {
3871 SYSERROR("Failed to create directory \"%s\"", newpath
);
3876 if (am_guest_unpriv()) {
3877 if (chown_mapped_root(newpath
, c
->lxc_conf
) < 0) {
3878 ERROR("Error chowning %s to container root", newpath
);
3883 c2
= lxc_container_new(newname
, lxcpath
);
3885 ERROR("clone: failed to create new container (%s %s)", newname
,
3890 /* copy/snapshot rootfs's */
3891 ret
= copy_storage(c
, c2
, bdevtype
, flags
, bdevdata
, newsize
);
3895 /* update utsname */
3896 if (!(flags
& LXC_CLONE_KEEPNAME
)) {
3897 clear_unexp_config_line(c2
->lxc_conf
, "lxc.utsname", false);
3898 clear_unexp_config_line(c2
->lxc_conf
, "lxc.uts.name", false);
3900 if (!do_set_config_item_locked(c2
, "lxc.uts.name", newname
)) {
3901 ERROR("Error setting new hostname");
3907 ret
= copyhooks(c
, c2
);
3909 ERROR("error copying hooks");
3913 if (copy_fstab(c
, c2
) < 0) {
3914 ERROR("error copying fstab");
3918 /* update macaddrs */
3919 if (!(flags
& LXC_CLONE_KEEPMACADDR
)) {
3920 if (!network_new_hwaddrs(c2
->lxc_conf
)) {
3921 ERROR("Error updating mac addresses");
3926 /* Update absolute paths for overlay mount directories. */
3927 if (ovl_update_abs_paths(c2
->lxc_conf
, c
->config_path
, c
->name
, lxcpath
, newname
) < 0)
3930 /* We've now successfully created c2's storage, so clear it out if we
3935 if (!c2
->save_config(c2
, NULL
))
3938 if ((pid
= fork()) < 0) {
3944 ret
= wait_for_pid(pid
);
3948 container_mem_unlock(c
);
3955 data
.hookargs
= hookargs
;
3957 if (am_guest_unpriv())
3958 ret
= userns_exec_full(c
->lxc_conf
, clone_update_rootfs_wrapper
,
3959 &data
, "clone_update_rootfs_wrapper");
3961 ret
= clone_update_rootfs(&data
);
3963 _exit(EXIT_FAILURE
);
3965 container_mem_unlock(c
);
3966 _exit(EXIT_SUCCESS
);
3969 container_mem_unlock(c
);
3971 if (!storage_copied
)
3972 c2
->lxc_conf
->rootfs
.path
= NULL
;
3975 lxc_container_put(c2
);
3981 static struct lxc_container
*lxcapi_clone(struct lxc_container
*c
, const char *newname
,
3982 const char *lxcpath
, int flags
,
3983 const char *bdevtype
, const char *bdevdata
, uint64_t newsize
,
3986 struct lxc_container
* ret
;
3988 current_config
= c
? c
->lxc_conf
: NULL
;
3989 ret
= do_lxcapi_clone(c
, newname
, lxcpath
, flags
, bdevtype
, bdevdata
, newsize
, hookargs
);
3990 current_config
= NULL
;
3995 static bool do_lxcapi_rename(struct lxc_container
*c
, const char *newname
)
3997 struct lxc_storage
*bdev
;
3998 struct lxc_container
*newc
;
4000 if (!c
|| !c
->name
|| !c
->config_path
|| !c
->lxc_conf
)
4003 if (has_fs_snapshots(c
) || has_snapshots(c
)) {
4004 ERROR("Renaming a container with snapshots is not supported");
4008 if (lxc_storage_prepare(c
->lxc_conf
)) {
4009 ERROR("Failed to find original backing store type");
4012 bdev
= c
->lxc_conf
->rootfs
.storage
;
4014 newc
= lxcapi_clone(c
, newname
, c
->config_path
, LXC_CLONE_KEEPMACADDR
, NULL
, bdev
->type
, 0, NULL
);
4015 lxc_storage_put(c
->lxc_conf
);
4017 lxc_container_put(newc
);
4021 if (newc
&& lxcapi_is_defined(newc
))
4022 lxc_container_put(newc
);
4024 if (!container_destroy(c
, NULL
)) {
4025 ERROR("Could not destroy existing container %s", c
->name
);
4032 WRAP_API_1(bool, lxcapi_rename
, const char *)
4034 static int lxcapi_attach(struct lxc_container
*c
,
4035 lxc_attach_exec_t exec_function
, void *exec_payload
,
4036 lxc_attach_options_t
*options
, pid_t
*attached_process
)
4043 current_config
= c
->lxc_conf
;
4045 ret
= lxc_attach(c
, exec_function
, exec_payload
, options
,
4047 current_config
= NULL
;
4051 static int do_lxcapi_attach_run_wait(struct lxc_container
*c
,
4052 lxc_attach_options_t
*options
,
4053 const char *program
,
4054 const char *const argv
[])
4056 lxc_attach_command_t command
;
4063 command
.program
= (char *)program
;
4064 command
.argv
= (char **)argv
;
4066 ret
= lxc_attach(c
, lxc_attach_run_command
, &command
, options
, &pid
);
4070 return lxc_wait_for_pid_status(pid
);
4073 static int lxcapi_attach_run_wait(struct lxc_container
*c
,
4074 lxc_attach_options_t
*options
,
4075 const char *program
, const char *const argv
[])
4079 current_config
= c
? c
->lxc_conf
: NULL
;
4080 ret
= do_lxcapi_attach_run_wait(c
, options
, program
, argv
);
4081 current_config
= NULL
;
4086 static int get_next_index(const char *lxcpath
, char *cname
)
4088 __do_free
char *fname
= NULL
;
4092 fname
= must_realloc(NULL
, strlen(lxcpath
) + 20);
4095 sprintf(fname
, "%s/snap%d", lxcpath
, i
);
4097 ret
= stat(fname
, &sb
);
4105 static bool get_snappath_dir(struct lxc_container
*c
, char *snappath
)
4110 * If the old style snapshot path exists, use it
4111 * /var/lib/lxc -> /var/lib/lxcsnaps
4113 ret
= strnprintf(snappath
, PATH_MAX
, "%ssnaps", c
->config_path
);
4117 if (dir_exists(snappath
)) {
4118 ret
= strnprintf(snappath
, PATH_MAX
, "%ssnaps/%s", c
->config_path
, c
->name
);
4126 * Use the new style path
4127 * /var/lib/lxc -> /var/lib/lxc + c->name + /snaps + \0
4129 ret
= strnprintf(snappath
, PATH_MAX
, "%s/%s/snaps", c
->config_path
, c
->name
);
4136 static int do_lxcapi_snapshot(struct lxc_container
*c
, const char *commentfile
)
4138 __do_free
char *dfnam
= NULL
;
4143 struct lxc_container
*c2
;
4144 char snappath
[PATH_MAX
], newname
[20];
4148 if (!c
|| !lxcapi_is_defined(c
))
4151 if (!storage_can_backup(c
->lxc_conf
)) {
4152 ERROR("%s's backing store cannot be backed up", c
->name
);
4153 ERROR("Your container must use another backing store type");
4157 if (!get_snappath_dir(c
, snappath
))
4160 i
= get_next_index(snappath
, c
->name
);
4162 if (lxc_mkdir_p(snappath
, 0755) < 0) {
4163 ERROR("Failed to create snapshot directory %s", snappath
);
4167 ret
= strnprintf(newname
, 20, "snap%d", i
);
4172 * We pass LXC_CLONE_SNAPSHOT to make sure that a rdepends file entry is
4173 * created in the original container
4175 flags
= LXC_CLONE_SNAPSHOT
| LXC_CLONE_KEEPMACADDR
| LXC_CLONE_KEEPNAME
|
4176 LXC_CLONE_KEEPBDEVTYPE
| LXC_CLONE_MAYBE_SNAPSHOT
;
4177 if (storage_lxc_is_dir(c
->lxc_conf
)) {
4178 ERROR("Snapshot of directory-backed container requested");
4179 ERROR("Making a copy-clone. If you do want snapshots, then");
4180 ERROR("please create overlay clone first, snapshot that");
4181 ERROR("and keep the original container pristine");
4182 flags
&= ~LXC_CLONE_SNAPSHOT
| LXC_CLONE_MAYBE_SNAPSHOT
;
4185 c2
= do_lxcapi_clone(c
, newname
, snappath
, flags
, NULL
, NULL
, 0, NULL
);
4187 ERROR("Failed to clone of %s:%s", c
->config_path
, c
->name
);
4191 lxc_container_put(c2
);
4193 /* Now write down the creation time. */
4196 if (!localtime_r(&timer
, &tm_info
)) {
4197 ERROR("Failed to get localtime");
4201 strftime(buffer
, 25, "%Y:%m:%d %H:%M:%S", &tm_info
);
4203 len
= strlen(snappath
) + 1 + strlen(newname
) + 1 + strlen(LXC_TIMESTAMP_FNAME
) + 1;
4204 dfnam
= must_realloc(NULL
, len
);
4205 ret
= strnprintf(dfnam
, len
, "%s/%s/%s", snappath
, newname
, LXC_TIMESTAMP_FNAME
);
4208 f
= fopen(dfnam
, "we");
4210 ERROR("Failed to open %s", dfnam
);
4214 if (fprintf(f
, "%s", buffer
) < 0) {
4215 SYSERROR("Writing timestamp");
4222 SYSERROR("Writing timestamp");
4227 __do_free
char *path
= NULL
;
4228 /* $p / $name / comment \0 */
4229 len
= strlen(snappath
) + 1 + strlen(newname
) + 1 + strlen(LXC_COMMENT_FNAME
) + 1;
4231 path
= must_realloc(NULL
, len
);
4232 ret
= strnprintf(path
, len
, "%s/%s/%s", snappath
, newname
, LXC_COMMENT_FNAME
);
4235 return copy_file(commentfile
, path
) < 0 ? -1 : i
;
4241 WRAP_API_1(int, lxcapi_snapshot
, const char *)
4243 static void lxcsnap_free(struct lxc_snapshot
*s
)
4246 free(s
->comment_pathname
);
4251 static char *get_snapcomment_path(char *snappath
, char *name
)
4253 __do_free
char *s
= NULL
;
4254 /* $snappath/$name/comment */
4255 int ret
, len
= strlen(snappath
) + strlen(name
) + 10;
4261 ret
= strnprintf(s
, len
, "%s/%s/comment", snappath
, name
);
4268 static char *get_timestamp(char* snappath
, char *name
)
4270 __do_free
char *s
= NULL
;
4271 __do_fclose
FILE *fin
= NULL
;
4272 char path
[PATH_MAX
];
4275 ret
= strnprintf(path
, sizeof(path
), "%s/%s/ts", snappath
, name
);
4279 fin
= fopen(path
, "re");
4283 (void) fseek(fin
, 0, SEEK_END
);
4285 (void) fseek(fin
, 0, SEEK_SET
);
4292 nbytes
= fread(s
, 1, len
, fin
);
4293 if (nbytes
< 0 || nbytes
!= (ssize_t
)len
)
4294 return log_error_errno(NULL
, errno
, "reading timestamp");
4301 static int do_lxcapi_snapshot_list(struct lxc_container
*c
, struct lxc_snapshot
**ret_snaps
)
4303 __do_closedir
DIR *dir
= NULL
;
4304 char snappath
[PATH_MAX
], path2
[PATH_MAX
];
4306 struct dirent
*direntp
;
4307 struct lxc_snapshot
*snaps
=NULL
, *nsnaps
;
4309 if (!c
|| !lxcapi_is_defined(c
))
4312 if (!get_snappath_dir(c
, snappath
)) {
4313 ERROR("path name too long");
4317 dir
= opendir(snappath
);
4319 INFO("Failed to open %s - assuming no snapshots", snappath
);
4323 while ((direntp
= readdir(dir
))) {
4324 if (strequal(direntp
->d_name
, "."))
4327 if (strequal(direntp
->d_name
, ".."))
4330 ret
= strnprintf(path2
, sizeof(path2
), "%s/%s/%s", snappath
, direntp
->d_name
, LXC_CONFIG_FNAME
);
4332 ERROR("pathname too long");
4336 if (!file_exists(path2
))
4339 nsnaps
= realloc(snaps
, (count
+ 1)*sizeof(*snaps
));
4341 SYSERROR("Out of memory");
4346 snaps
[count
].free
= lxcsnap_free
;
4347 snaps
[count
].name
= strdup(direntp
->d_name
);
4348 if (!snaps
[count
].name
)
4351 snaps
[count
].lxcpath
= strdup(snappath
);
4352 if (!snaps
[count
].lxcpath
) {
4353 free(snaps
[count
].name
);
4357 snaps
[count
].comment_pathname
= get_snapcomment_path(snappath
, direntp
->d_name
);
4358 snaps
[count
].timestamp
= get_timestamp(snappath
, direntp
->d_name
);
4367 for (int i
= 0; i
< count
; i
++)
4368 lxcsnap_free(&snaps
[i
]);
4376 WRAP_API_1(int, lxcapi_snapshot_list
, struct lxc_snapshot
**)
4378 static bool do_lxcapi_snapshot_restore(struct lxc_container
*c
, const char *snapname
, const char *newname
)
4380 char clonelxcpath
[PATH_MAX
];
4382 struct lxc_container
*snap
, *rest
;
4383 struct lxc_storage
*bdev
;
4386 if (!c
|| !c
->name
|| !c
->config_path
)
4389 if (has_fs_snapshots(c
)) {
4390 ERROR("container rootfs has dependent snapshots");
4394 if (lxc_storage_prepare(c
->lxc_conf
)) {
4395 ERROR("Failed to find original backing store type");
4398 bdev
= c
->lxc_conf
->rootfs
.storage
;
4400 /* For an overlay container the rootfs is considered immutable
4401 * and cannot be removed when restoring from a snapshot. We pass this
4402 * internal flag along to communicate this to various parts of the
4405 if (strequal(bdev
->type
, "overlay") || strequal(bdev
->type
, "overlayfs"))
4406 bdev
->flags
|= LXC_STORAGE_INTERNAL_OVERLAY_RESTORE
;
4411 if (!get_snappath_dir(c
, clonelxcpath
)) {
4412 lxc_storage_put(c
->lxc_conf
);
4415 /* how should we lock this? */
4417 snap
= lxc_container_new(snapname
, clonelxcpath
);
4418 if (!snap
|| !lxcapi_is_defined(snap
)) {
4419 ERROR("Could not open snapshot %s", snapname
);
4422 lxc_container_put(snap
);
4424 lxc_storage_put(c
->lxc_conf
);
4428 if (strequal(c
->name
, newname
)) {
4429 if (!container_destroy(c
, bdev
)) {
4430 ERROR("Could not destroy existing container %s", newname
);
4431 lxc_container_put(snap
);
4432 lxc_storage_put(c
->lxc_conf
);
4437 if (!strequal(bdev
->type
, "dir") && !strequal(bdev
->type
, "loop"))
4438 flags
= LXC_CLONE_SNAPSHOT
| LXC_CLONE_MAYBE_SNAPSHOT
;
4440 if (strequal(bdev
->type
, "overlay") || strequal(bdev
->type
, "overlayfs"))
4441 flags
|= LXC_STORAGE_INTERNAL_OVERLAY_RESTORE
;
4443 rest
= lxcapi_clone(snap
, newname
, c
->config_path
, flags
, bdev
->type
,
4445 lxc_storage_put(c
->lxc_conf
);
4446 if (rest
&& lxcapi_is_defined(rest
))
4450 lxc_container_put(rest
);
4452 lxc_container_put(snap
);
4456 WRAP_API_2(bool, lxcapi_snapshot_restore
, const char *, const char *)
4458 static bool do_snapshot_destroy(const char *snapname
, const char *clonelxcpath
)
4460 struct lxc_container
*snap
= NULL
;
4463 snap
= lxc_container_new(snapname
, clonelxcpath
);
4465 ERROR("Could not find snapshot %s", snapname
);
4469 if (!do_lxcapi_destroy(snap
)) {
4470 ERROR("Could not destroy snapshot %s", snapname
);
4478 lxc_container_put(snap
);
4483 static bool remove_all_snapshots(const char *path
)
4485 __do_closedir
DIR *dir
= NULL
;
4486 struct dirent
*direntp
;
4489 dir
= opendir(path
);
4491 SYSERROR("opendir on snapshot path %s", path
);
4495 while ((direntp
= readdir(dir
))) {
4496 if (strequal(direntp
->d_name
, "."))
4499 if (strequal(direntp
->d_name
, ".."))
4502 if (!do_snapshot_destroy(direntp
->d_name
, path
)) {
4509 SYSERROR("Error removing directory %s", path
);
4514 static bool do_lxcapi_snapshot_destroy(struct lxc_container
*c
, const char *snapname
)
4516 char clonelxcpath
[PATH_MAX
];
4518 if (!c
|| !c
->name
|| !c
->config_path
|| !snapname
)
4521 if (!get_snappath_dir(c
, clonelxcpath
))
4524 return do_snapshot_destroy(snapname
, clonelxcpath
);
4527 WRAP_API_1(bool, lxcapi_snapshot_destroy
, const char *)
4529 static bool do_lxcapi_snapshot_destroy_all(struct lxc_container
*c
)
4531 char clonelxcpath
[PATH_MAX
];
4533 if (!c
|| !c
->name
|| !c
->config_path
)
4536 if (!get_snappath_dir(c
, clonelxcpath
))
4539 return remove_all_snapshots(clonelxcpath
);
4542 WRAP_API(bool, lxcapi_snapshot_destroy_all
)
4544 static bool do_lxcapi_may_control(struct lxc_container
*c
)
4549 return lxc_try_cmd(c
->name
, c
->config_path
) == 0;
4552 WRAP_API(bool, lxcapi_may_control
)
4554 static bool do_add_remove_node(pid_t init_pid
, const char *path
, bool add
,
4560 char chrootpath
[PATH_MAX
];
4561 char *directory_path
= NULL
;
4565 SYSERROR("Failed to fork()");
4570 ret
= wait_for_pid(pid
);
4572 ERROR("Failed to create device node");
4579 /* prepare the path */
4580 ret
= strnprintf(chrootpath
, sizeof(chrootpath
), "/proc/%d/root", init_pid
);
4584 ret
= chroot(chrootpath
);
4586 _exit(EXIT_FAILURE
);
4590 _exit(EXIT_FAILURE
);
4592 /* remove path if it exists */
4593 ret
= faccessat(AT_FDCWD
, path
, F_OK
, AT_SYMLINK_NOFOLLOW
);
4597 SYSERROR("Failed to remove \"%s\"", path
);
4598 _exit(EXIT_FAILURE
);
4603 _exit(EXIT_SUCCESS
);
4605 /* create any missing directories */
4608 _exit(EXIT_FAILURE
);
4610 directory_path
= dirname(tmp
);
4611 ret
= lxc_mkdir_p(directory_path
, 0755);
4612 if (ret
< 0 && errno
!= EEXIST
) {
4613 SYSERROR("Failed to create path \"%s\"", directory_path
);
4615 _exit(EXIT_FAILURE
);
4618 /* create the device node */
4619 ret
= mknod(path
, st
->st_mode
, st
->st_rdev
);
4622 SYSERROR("Failed to create device node at \"%s\"", path
);
4623 _exit(EXIT_FAILURE
);
4626 _exit(EXIT_SUCCESS
);
4629 static bool add_remove_device_node(struct lxc_container
*c
, const char *src_path
, const char *dest_path
, bool add
)
4633 char value
[LXC_MAX_BUFFER
];
4637 /* make sure container is running */
4638 if (!do_lxcapi_is_running(c
)) {
4639 ERROR("container is not running");
4643 /* use src_path if dest_path is NULL otherwise use dest_path */
4644 p
= dest_path
? dest_path
: src_path
;
4646 /* make sure we can access p */
4647 if(access(p
, F_OK
) < 0 || stat(p
, &st
) < 0)
4650 /* continue if path is character device or block device */
4651 if (S_ISCHR(st
.st_mode
))
4652 ret
= strnprintf(value
, sizeof(value
), "c %d:%d rwm", major(st
.st_rdev
), minor(st
.st_rdev
));
4653 else if (S_ISBLK(st
.st_mode
))
4654 ret
= strnprintf(value
, sizeof(value
), "b %d:%d rwm", major(st
.st_rdev
), minor(st
.st_rdev
));
4660 init_pid
= do_lxcapi_init_pid(c
);
4662 ERROR("Failed to get init pid");
4666 if (!do_add_remove_node(init_pid
, p
, add
, &st
))
4669 /* add or remove device to/from cgroup access list */
4671 if (!do_lxcapi_set_cgroup_item(c
, "devices.allow", value
)) {
4672 ERROR("set_cgroup_item failed while adding the device node");
4676 if (!do_lxcapi_set_cgroup_item(c
, "devices.deny", value
)) {
4677 ERROR("set_cgroup_item failed while removing the device node");
4685 static bool do_lxcapi_add_device_node(struct lxc_container
*c
, const char *src_path
, const char *dest_path
)
4687 // cannot mknod if we're not privileged wrt init_user_ns
4688 if (am_host_unpriv()) {
4689 ERROR(LXC_UNPRIV_EOPNOTSUPP
, __FUNCTION__
);
4693 return add_remove_device_node(c
, src_path
, dest_path
, true);
4696 WRAP_API_2(bool, lxcapi_add_device_node
, const char *, const char *)
4698 static bool do_lxcapi_remove_device_node(struct lxc_container
*c
, const char *src_path
, const char *dest_path
)
4700 if (am_guest_unpriv()) {
4701 ERROR(LXC_UNPRIV_EOPNOTSUPP
, __FUNCTION__
);
4705 return add_remove_device_node(c
, src_path
, dest_path
, false);
4708 WRAP_API_2(bool, lxcapi_remove_device_node
, const char *, const char *)
4710 static bool do_lxcapi_attach_interface(struct lxc_container
*c
,
4712 const char *dst_ifname
)
4717 if (am_guest_unpriv()) {
4718 ERROR(LXC_UNPRIV_EOPNOTSUPP
, __FUNCTION__
);
4723 ERROR("No source interface name given");
4727 ret
= lxc_netdev_isup(ifname
);
4729 /* netdev of ifname is up. */
4730 ret
= lxc_netdev_down(ifname
);
4735 init_pid
= do_lxcapi_init_pid(c
);
4737 ERROR("Failed to get init pid");
4741 ret
= lxc_netdev_move_by_name(ifname
, init_pid
, dst_ifname
);
4745 INFO("Moved network device \"%s\" to network namespace of %d", ifname
, init_pid
);
4752 WRAP_API_2(bool, lxcapi_attach_interface
, const char *, const char *)
4754 static bool do_lxcapi_detach_interface(struct lxc_container
*c
,
4756 const char *dst_ifname
)
4759 pid_t pid
, pid_outside
;
4760 __do_free
char *physname
= NULL
;
4763 * TODO - if this is a physical device, then we need am_host_unpriv.
4764 * But for other types guest privilege suffices.
4766 if (am_guest_unpriv()) {
4767 ERROR(LXC_UNPRIV_EOPNOTSUPP
, __FUNCTION__
);
4772 ERROR("No source interface name given");
4776 pid_outside
= lxc_raw_getpid();
4779 ERROR("Failed to fork");
4783 if (pid
== 0) { /* child */
4786 init_pid
= do_lxcapi_init_pid(c
);
4788 ERROR("Failed to get init pid");
4789 _exit(EXIT_FAILURE
);
4791 if (!switch_to_ns(init_pid
, "net")) {
4792 ERROR("Failed to enter network namespace");
4793 _exit(EXIT_FAILURE
);
4796 /* create new mount namespace for use with remounting /sys and is_wlan() below. */
4797 ret
= unshare(CLONE_NEWNS
);
4799 ERROR("Failed to unshare mount namespace");
4800 _exit(EXIT_FAILURE
);
4803 /* set / recursively as private so that mount propagation doesn't affect us. */
4804 if (mount(NULL
, "/", NULL
, MS_REC
| MS_PRIVATE
, 0) < 0) {
4805 ERROR("Failed to recursively set / as private in mount namespace");
4806 _exit(EXIT_FAILURE
);
4809 ret
= lxc_netdev_isup(ifname
);
4811 ERROR("Failed to determine whether network device \"%s\" is up", ifname
);
4812 _exit(EXIT_FAILURE
);
4815 /* netdev of ifname is up. */
4817 ret
= lxc_netdev_down(ifname
);
4819 ERROR("Failed to set network device \"%s\" down", ifname
);
4820 _exit(EXIT_FAILURE
);
4824 /* remount /sys so is_wlan() can check if this device is a wlan device. */
4825 lxc_attach_remount_sys_proc();
4826 physname
= is_wlan(ifname
);
4828 ret
= lxc_netdev_move_wlan(physname
, ifname
, pid_outside
, dst_ifname
);
4830 ret
= lxc_netdev_move_by_name(ifname
, pid_outside
, dst_ifname
);
4832 /* -EINVAL means there is no netdev named as ifname. */
4835 ERROR("Network device \"%s\" not found", ifname
);
4837 ERROR("Failed to remove network device \"%s\"", ifname
);
4839 _exit(EXIT_FAILURE
);
4842 _exit(EXIT_SUCCESS
);
4845 ret
= wait_for_pid(pid
);
4849 INFO("Moved network device \"%s\" to network namespace of %d", ifname
, pid_outside
);
4853 WRAP_API_2(bool, lxcapi_detach_interface
, const char *, const char *)
4855 static int do_lxcapi_migrate(struct lxc_container
*c
, unsigned int cmd
,
4856 struct migrate_opts
*opts
, unsigned int size
)
4859 struct migrate_opts
*valid_opts
= opts
;
4860 uint64_t features_to_check
= 0;
4862 /* If the caller has a bigger (newer) struct migrate_opts, let's make
4863 * sure that the stuff on the end is zero, i.e. that they didn't ask us
4864 * to do anything special.
4866 if (size
> sizeof(*opts
)) {
4867 unsigned char *addr
;
4870 addr
= (void *)opts
+ sizeof(*opts
);
4871 end
= (void *)opts
+ size
;
4873 for (; addr
< end
; addr
++)
4878 /* If the caller has a smaller struct, let's zero out the end for them
4879 * so we don't accidentally use bits of it that they didn't know about
4882 if (size
< sizeof(*opts
)) {
4883 valid_opts
= malloc(sizeof(*opts
));
4887 memset(valid_opts
, 0, sizeof(*opts
));
4888 memcpy(valid_opts
, opts
, size
);
4892 case MIGRATE_PRE_DUMP
:
4893 if (!do_lxcapi_is_running(c
)) {
4894 ERROR("container is not running");
4898 ret
= !__criu_pre_dump(c
, valid_opts
);
4901 if (!do_lxcapi_is_running(c
)) {
4902 ERROR("container is not running");
4906 ret
= !__criu_dump(c
, valid_opts
);
4908 case MIGRATE_RESTORE
:
4909 if (do_lxcapi_is_running(c
)) {
4910 ERROR("container is already running");
4914 ret
= !__criu_restore(c
, valid_opts
);
4916 case MIGRATE_FEATURE_CHECK
:
4917 features_to_check
= valid_opts
->features_to_check
;
4918 ret
= !__criu_check_feature(&features_to_check
);
4920 /* Something went wrong. Let's let the caller
4921 * know which feature checks failed. */
4922 valid_opts
->features_to_check
= features_to_check
;
4926 ERROR("invalid migrate command %u", cmd
);
4931 if (size
< sizeof(*opts
))
4937 WRAP_API_3(int, lxcapi_migrate
, unsigned int, struct migrate_opts
*, unsigned int)
4939 static bool do_lxcapi_checkpoint(struct lxc_container
*c
, char *directory
, bool stop
, bool verbose
)
4941 struct migrate_opts opts
;
4943 memset(&opts
, 0, sizeof(opts
));
4945 opts
.directory
= directory
;
4947 opts
.verbose
= verbose
;
4949 return !do_lxcapi_migrate(c
, MIGRATE_DUMP
, &opts
, sizeof(opts
));
4952 WRAP_API_3(bool, lxcapi_checkpoint
, char *, bool, bool)
4954 static bool do_lxcapi_restore(struct lxc_container
*c
, char *directory
, bool verbose
)
4956 struct migrate_opts opts
;
4958 memset(&opts
, 0, sizeof(opts
));
4960 opts
.directory
= directory
;
4961 opts
.verbose
= verbose
;
4963 return !do_lxcapi_migrate(c
, MIGRATE_RESTORE
, &opts
, sizeof(opts
));
4966 WRAP_API_2(bool, lxcapi_restore
, char *, bool)
4968 /* @st_mode is the st_mode field of the stat(source) return struct */
4969 static int create_mount_target(const char *dest
, mode_t st_mode
)
4971 char *dirdup
, *destdirname
;
4974 dirdup
= strdup(dest
);
4976 SYSERROR("Failed to duplicate target name \"%s\"", dest
);
4979 destdirname
= dirname(dirdup
);
4981 ret
= lxc_mkdir_p(destdirname
, 0755);
4983 SYSERROR("Failed to create \"%s\"", destdirname
);
4991 if (S_ISDIR(st_mode
))
4992 ret
= mkdir(dest
, 0000);
4994 ret
= mknod(dest
, S_IFREG
| 0000, 0);
4997 TRACE("Created mount target \"%s\"", dest
);
4998 else if (ret
< 0 && errno
!= EEXIST
) {
4999 SYSERROR("Failed to create mount target \"%s\"", dest
);
5006 static int do_lxcapi_mount(struct lxc_container
*c
, const char *source
,
5007 const char *target
, const char *filesystemtype
,
5008 unsigned long mountflags
, const void *data
,
5009 struct lxc_mount
*mnt
)
5012 char template[PATH_MAX
], path
[PATH_MAX
];
5013 pid_t pid
, init_pid
;
5016 int ret
= -1, fd
= -EBADF
;
5018 if (!c
|| !c
->lxc_conf
) {
5019 ERROR("Container or configuration is NULL");
5023 if (!c
->lxc_conf
->shmount
.path_host
) {
5024 ERROR("Host path to shared mountpoint must be specified in the config\n");
5028 ret
= strnprintf(template, sizeof(template), "%s/.lxcmount_XXXXXX", c
->lxc_conf
->shmount
.path_host
);
5030 SYSERROR("Error writing shmounts tempdir name");
5034 /* Create a temporary file / dir under the shared mountpoint */
5035 if (!source
|| strequal(source
, "")) {
5036 /* If source is not specified, maybe we want to mount a filesystem? */
5037 sb
.st_mode
= S_IFDIR
;
5039 ret
= stat(source
, &sb
);
5041 SYSERROR("Error getting stat info about the source \"%s\"", source
);
5046 lxc_is_dir
= (S_ISDIR(sb
.st_mode
) != 0);
5048 sret
= mkdtemp(template);
5050 SYSERROR("Could not create shmounts temporary dir");
5054 fd
= lxc_make_tmpfile(template, false);
5056 SYSERROR("Could not create shmounts temporary file");
5064 SYSERROR("Could not fork");
5070 ret
= mount(source
, template, filesystemtype
, mountflags
, data
);
5072 SYSERROR("Failed to mount onto \"%s\"", template);
5073 _exit(EXIT_FAILURE
);
5075 TRACE("Mounted \"%s\" onto \"%s\"", source
, template);
5077 init_pid
= do_lxcapi_init_pid(c
);
5079 ERROR("Failed to obtain container's init pid");
5080 _exit(EXIT_FAILURE
);
5083 /* Enter the container namespaces */
5084 if (!list_empty(&c
->lxc_conf
->id_map
)) {
5085 if (!switch_to_ns(init_pid
, "user")) {
5086 ERROR("Failed to enter user namespace");
5087 _exit(EXIT_FAILURE
);
5090 if (!lxc_switch_uid_gid(0, 0))
5091 _exit(EXIT_FAILURE
);
5094 if (!switch_to_ns(init_pid
, "mnt")) {
5095 ERROR("Failed to enter mount namespace");
5096 _exit(EXIT_FAILURE
);
5099 ret
= create_mount_target(target
, sb
.st_mode
);
5101 _exit(EXIT_FAILURE
);
5103 suff
= strrchr(template, '/');
5105 goto cleanup_target_in_child
;
5107 ret
= strnprintf(path
, sizeof(path
), "%s%s", c
->lxc_conf
->shmount
.path_cont
, suff
);
5109 SYSERROR("Error writing container mountpoint name");
5110 goto cleanup_target_in_child
;
5113 ret
= mount(path
, target
, NULL
, MS_MOVE
| MS_REC
, NULL
);
5115 SYSERROR("Failed to move the mount from \"%s\" to \"%s\"", path
, target
);
5116 goto cleanup_target_in_child
;
5118 TRACE("Moved mount from \"%s\" to \"%s\"", path
, target
);
5120 _exit(EXIT_SUCCESS
);
5122 cleanup_target_in_child
:
5123 (void)remove(target
);
5124 _exit(EXIT_FAILURE
);
5127 ret
= wait_for_pid(pid
);
5129 SYSERROR("Wait for the child with pid %ld failed", (long)pid
);
5133 if (umount2(template, MNT_DETACH
))
5134 SYSWARN("Failed to remove temporary mount \"%s\"", template);
5137 (void)rmdir(template);
5139 (void)unlink(template);
5148 WRAP_API_6(int, lxcapi_mount
, const char *, const char *, const char *,
5149 unsigned long, const void *, struct lxc_mount
*)
5151 static int do_lxcapi_umount(struct lxc_container
*c
, const char *target
,
5152 unsigned long flags
, struct lxc_mount
*mnt
)
5154 pid_t pid
, init_pid
;
5157 if (!c
|| !c
->lxc_conf
) {
5158 ERROR("Container or configuration is NULL");
5165 SYSERROR("Could not fork");
5170 init_pid
= do_lxcapi_init_pid(c
);
5172 ERROR("Failed to obtain container's init pid");
5173 _exit(EXIT_FAILURE
);
5176 /* Enter the container namespaces */
5177 if (!list_empty(&c
->lxc_conf
->id_map
)) {
5178 if (!switch_to_ns(init_pid
, "user")) {
5179 ERROR("Failed to enter user namespace");
5180 _exit(EXIT_FAILURE
);
5184 if (!switch_to_ns(init_pid
, "mnt")) {
5185 ERROR("Failed to enter mount namespace");
5186 _exit(EXIT_FAILURE
);
5189 /* Do the unmount */
5190 ret
= umount2(target
, flags
);
5192 SYSERROR("Failed to umount \"%s\"", target
);
5193 _exit(EXIT_FAILURE
);
5196 _exit(EXIT_SUCCESS
);
5199 ret
= wait_for_pid(pid
);
5201 SYSERROR("Wait for the child with pid %ld failed", (long)pid
);
5208 WRAP_API_3(int, lxcapi_umount
, const char *, unsigned long, struct lxc_mount
*)
5210 static int lxcapi_attach_run_waitl(struct lxc_container
*c
, lxc_attach_options_t
*options
, const char *program
, const char *arg
, ...)
5219 current_config
= c
->lxc_conf
;
5222 argv
= lxc_va_arg_list_to_argv_const(ap
, 1);
5226 ERROR("Memory allocation error.");
5232 ret
= do_lxcapi_attach_run_wait(c
, options
, program
, (const char * const *)argv
);
5236 current_config
= NULL
;
5240 static int do_lxcapi_seccomp_notify_fd(struct lxc_container
*c
)
5242 if (!c
|| !c
->lxc_conf
)
5243 return ret_set_errno(-1, -EINVAL
);
5245 return lxc_seccomp_get_notify_fd(&c
->lxc_conf
->seccomp
);
5248 WRAP_API(int, lxcapi_seccomp_notify_fd
)
5250 static int do_lxcapi_seccomp_notify_fd_active(struct lxc_container
*c
)
5252 if (!c
|| !c
->lxc_conf
)
5253 return ret_set_errno(-1, -EINVAL
);
5255 return lxc_cmd_get_seccomp_notify_fd(c
->name
, c
->config_path
);
5258 WRAP_API(int, lxcapi_seccomp_notify_fd_active
)
5260 struct lxc_container
*lxc_container_new(const char *name
, const char *configpath
)
5262 struct lxc_container
*c
;
5269 c
= malloc(sizeof(*c
));
5271 fprintf(stderr
, "Failed to allocate memory for %s\n", name
);
5274 memset(c
, 0, sizeof(*c
));
5277 c
->config_path
= strdup(configpath
);
5279 c
->config_path
= strdup(lxc_global_config_value("lxc.lxcpath"));
5280 if (!c
->config_path
) {
5281 fprintf(stderr
, "Failed to allocate memory for %s\n", name
);
5285 remove_trailing_slashes(c
->config_path
);
5288 c
->name
= malloc(len
+ 1);
5290 fprintf(stderr
, "Failed to allocate memory for %s\n", name
);
5293 (void)strlcpy(c
->name
, name
, len
+ 1);
5296 c
->slock
= lxc_newlock(c
->config_path
, name
);
5298 fprintf(stderr
, "Failed to create lock for %s\n", name
);
5302 c
->privlock
= lxc_newlock(NULL
, NULL
);
5304 fprintf(stderr
, "Failed to create private lock for %s\n", name
);
5308 if (!set_config_filename(c
)) {
5309 fprintf(stderr
, "Failed to create config file name for %s\n", name
);
5313 if (file_exists(c
->configfile
) && !lxcapi_load_config(c
, NULL
)) {
5314 fprintf(stderr
, "Failed to load config for %s\n", name
);
5318 rc
= ongoing_create(c
);
5320 case LXC_CREATE_INCOMPLETE
:
5321 SYSERROR("Failed to complete container creation for %s", c
->name
);
5322 container_destroy(c
, NULL
);
5323 lxcapi_clear_config(c
);
5325 case LXC_CREATE_ONGOING
:
5326 /* container creation going on */
5328 case LXC_CREATE_FAILED
:
5329 /* container creation failed */
5330 if (errno
!= EACCES
&& errno
!= EPERM
) {
5331 /* insufficient privileges */
5332 SYSERROR("Failed checking for incomplete container %s creation", c
->name
);
5338 c
->daemonize
= true;
5341 /* Assign the member functions. */
5342 c
->is_defined
= lxcapi_is_defined
;
5343 c
->state
= lxcapi_state
;
5344 c
->is_running
= lxcapi_is_running
;
5345 c
->freeze
= lxcapi_freeze
;
5346 c
->unfreeze
= lxcapi_unfreeze
;
5347 c
->console
= lxcapi_console
;
5348 c
->console_getfd
= lxcapi_console_getfd
;
5349 c
->devpts_fd
= lxcapi_devpts_fd
;
5350 c
->init_pid
= lxcapi_init_pid
;
5351 c
->init_pidfd
= lxcapi_init_pidfd
;
5352 c
->load_config
= lxcapi_load_config
;
5353 c
->want_daemonize
= lxcapi_want_daemonize
;
5354 c
->want_close_all_fds
= lxcapi_want_close_all_fds
;
5355 c
->start
= lxcapi_start
;
5356 c
->startl
= lxcapi_startl
;
5357 c
->stop
= lxcapi_stop
;
5358 c
->config_file_name
= lxcapi_config_file_name
;
5359 c
->wait
= lxcapi_wait
;
5360 c
->set_config_item
= lxcapi_set_config_item
;
5361 c
->destroy
= lxcapi_destroy
;
5362 c
->destroy_with_snapshots
= lxcapi_destroy_with_snapshots
;
5363 c
->rename
= lxcapi_rename
;
5364 c
->save_config
= lxcapi_save_config
;
5365 c
->get_keys
= lxcapi_get_keys
;
5366 c
->create
= lxcapi_create
;
5367 c
->createl
= lxcapi_createl
;
5368 c
->shutdown
= lxcapi_shutdown
;
5369 c
->reboot
= lxcapi_reboot
;
5370 c
->reboot2
= lxcapi_reboot2
;
5371 c
->clear_config
= lxcapi_clear_config
;
5372 c
->clear_config_item
= lxcapi_clear_config_item
;
5373 c
->get_config_item
= lxcapi_get_config_item
;
5374 c
->get_running_config_item
= lxcapi_get_running_config_item
;
5375 c
->get_cgroup_item
= lxcapi_get_cgroup_item
;
5376 c
->set_cgroup_item
= lxcapi_set_cgroup_item
;
5377 c
->get_config_path
= lxcapi_get_config_path
;
5378 c
->set_config_path
= lxcapi_set_config_path
;
5379 c
->clone
= lxcapi_clone
;
5380 c
->get_interfaces
= lxcapi_get_interfaces
;
5381 c
->get_ips
= lxcapi_get_ips
;
5382 c
->attach
= lxcapi_attach
;
5383 c
->attach_run_wait
= lxcapi_attach_run_wait
;
5384 c
->attach_run_waitl
= lxcapi_attach_run_waitl
;
5385 c
->snapshot
= lxcapi_snapshot
;
5386 c
->snapshot_list
= lxcapi_snapshot_list
;
5387 c
->snapshot_restore
= lxcapi_snapshot_restore
;
5388 c
->snapshot_destroy
= lxcapi_snapshot_destroy
;
5389 c
->snapshot_destroy_all
= lxcapi_snapshot_destroy_all
;
5390 c
->may_control
= lxcapi_may_control
;
5391 c
->add_device_node
= lxcapi_add_device_node
;
5392 c
->remove_device_node
= lxcapi_remove_device_node
;
5393 c
->attach_interface
= lxcapi_attach_interface
;
5394 c
->detach_interface
= lxcapi_detach_interface
;
5395 c
->checkpoint
= lxcapi_checkpoint
;
5396 c
->restore
= lxcapi_restore
;
5397 c
->migrate
= lxcapi_migrate
;
5398 c
->console_log
= lxcapi_console_log
;
5399 c
->mount
= lxcapi_mount
;
5400 c
->umount
= lxcapi_umount
;
5401 c
->seccomp_notify_fd
= lxcapi_seccomp_notify_fd
;
5402 c
->seccomp_notify_fd_active
= lxcapi_seccomp_notify_fd_active
;
5407 lxc_container_free(c
);
5411 int lxc_get_wait_states(const char **states
)
5416 for (i
=0; i
<MAX_STATE
; i
++)
5417 states
[i
] = lxc_state2str(i
);
5423 * These next two could probably be done smarter with reusing a common function
5424 * with different iterators and tests...
5426 int list_defined_containers(const char *lxcpath
, char ***names
,
5427 struct lxc_container
***cret
)
5429 __do_closedir
DIR *dir
= NULL
;
5430 size_t array_len
= 0, name_array_len
= 0, ct_array_len
= 0;
5431 struct dirent
*direntp
;
5432 struct lxc_container
*c
;
5435 lxcpath
= lxc_global_config_value("lxc.lxcpath");
5437 dir
= opendir(lxcpath
);
5439 SYSERROR("opendir on lxcpath");
5449 while ((direntp
= readdir(dir
))) {
5450 /* Ignore '.', '..' and any hidden directory. */
5451 if (strnequal(direntp
->d_name
, ".", 1))
5454 if (!config_file_exists(lxcpath
, direntp
->d_name
))
5458 c
= lxc_container_new(direntp
->d_name
, lxcpath
);
5460 INFO("Container %s:%s has a config but could not be loaded",
5461 lxcpath
, direntp
->d_name
);
5465 if (!do_lxcapi_is_defined(c
)) {
5466 INFO("Container %s:%s has a config but is not defined",
5467 lxcpath
, direntp
->d_name
);
5469 lxc_container_put(c
);
5475 if (!add_to_array(names
, direntp
->d_name
, array_len
))
5481 if (!add_to_clist(cret
, c
, array_len
, true)) {
5482 lxc_container_put(c
);
5494 if (names
&& *names
) {
5495 for (size_t i
= 0; i
< name_array_len
; i
++)
5500 if (cret
&& *cret
) {
5501 for (size_t i
= 0; i
< ct_array_len
; i
++)
5502 lxc_container_put((*cret
)[i
]);
5509 int list_active_containers(const char *lxcpath
, char ***nret
,
5510 struct lxc_container
***cret
)
5512 __do_free
char *line
= NULL
;
5513 __do_fclose
FILE *f
= NULL
;
5514 int i
, ret
= -1, cret_cnt
= 0, ct_name_cnt
= 0;
5516 char **ct_name
= NULL
;
5518 struct lxc_container
*c
= NULL
;
5522 lxcpath
= lxc_global_config_value("lxc.lxcpath");
5523 lxcpath_len
= strlen(lxcpath
);
5531 f
= fopen("/proc/net/unix", "re");
5535 while (getline(&line
, &len
, f
) != -1) {
5536 char *p
= strrchr(line
, ' '), *p2
;
5547 if (strnequal(p
, lxcpath
, lxcpath_len
)) {
5549 } else if (strnequal(p
, "lxc/", 4)) {
5559 /* Now p is the start of lxc_name. */
5560 p2
= strchr(p
, '/');
5561 if (!p2
|| !strnequal(p2
, "/command", 8))
5566 char *recvpath
= lxc_cmd_get_lxcpath(p
);
5570 if (!strnequal(lxcpath
, recvpath
, lxcpath_len
)) {
5576 p
= lxc_cmd_get_name(p
);
5581 if (array_contains(&ct_name
, p
, ct_name_cnt
)) {
5587 if (!add_to_array(&ct_name
, p
, ct_name_cnt
)) {
5590 goto free_cret_list
;
5601 c
= lxc_container_new(p
, lxcpath
);
5603 INFO("Container %s:%s is running but could not be loaded", lxcpath
, p
);
5607 goto free_cret_list
;
5614 * If this is an anonymous container, then is_defined *can*
5615 * return false. So we don't do that check. Count on the
5616 * fact that the command socket exists.
5619 if (!add_to_clist(cret
, c
, cret_cnt
, true)) {
5620 lxc_container_put(c
);
5621 goto free_cret_list
;
5627 if (nret
&& cret
&& cret_cnt
!= ct_name_cnt
) {
5629 lxc_container_put(c
);
5630 goto free_cret_list
;
5642 if (cret
&& *cret
) {
5643 for (i
= 0; i
< cret_cnt
; i
++)
5644 lxc_container_put((*cret
)[i
]);
5650 for (i
= 0; i
< ct_name_cnt
; i
++)
5659 int list_all_containers(const char *lxcpath
, char ***nret
,
5660 struct lxc_container
***cret
)
5662 int active_cnt
, ct_cnt
, ct_list_cnt
, ret
;
5663 char **active_name
= NULL
, **ct_name
= NULL
;
5664 struct lxc_container
**ct_list
= NULL
;
5666 ct_cnt
= list_defined_containers(lxcpath
, &ct_name
, NULL
);
5670 active_cnt
= list_active_containers(lxcpath
, &active_name
, NULL
);
5671 if (active_cnt
< 0) {
5677 for (int i
= 0; i
< active_cnt
; i
++) {
5678 if (array_contains(&ct_name
, active_name
[i
], ct_cnt
))
5681 if (!add_to_array(&ct_name
, active_name
[i
], ct_cnt
))
5682 goto free_active_name
;
5689 for (int i
= 0; i
< ct_cnt
; i
++) {
5690 __put_lxc_container
struct lxc_container
*c
= NULL
;
5692 c
= lxc_container_new(ct_name
[i
], lxcpath
);
5694 WARN("Container %s:%s could not be loaded", lxcpath
, ct_name
[i
]);
5698 if (!add_to_clist(&ct_list
, c
, ct_list_cnt
, false))
5708 for (int i
= 0; i
< active_cnt
; i
++)
5709 free(active_name
[i
]);
5715 for (int i
= 0; i
< ct_cnt
; i
++)
5723 for (int i
= 0; i
< ct_list_cnt
; i
++)
5724 lxc_container_put(ct_list
[i
]);
5728 for (int i
= 0; i
< active_cnt
; i
++)
5729 free(active_name
[i
]);
5733 for (int i
= 0; i
< ct_cnt
; i
++)
5740 bool lxc_config_item_is_supported(const char *key
)
5742 return !!lxc_get_config_exact(key
);
5745 bool lxc_has_api_extension(const char *extension
)
5747 /* The NULL API extension is always present. :) */
5751 for (size_t i
= 0; i
< nr_api_extensions
; i
++)
5752 if (strequal(api_extensions
[i
], extension
))