3 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2012 Canonical Ltd.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <sys/types.h>
34 #include <lxc/lxccontainer.h>
45 #include <lxc/utils.h>
46 #include <lxc/monitor.h>
47 #include <lxc/namespace.h>
49 #include <arpa/inet.h>
54 #include <../include/ifaddrs.h>
59 #include <../include/getline.h>
63 lxc_log_define(lxc_container
, lxc
);
65 static bool file_exists(char *f
)
69 return stat(f
, &statbuf
) == 0;
72 static bool config_file_exists(const char *lxcpath
, const char *cname
)
74 /* $lxcpath + '/' + $cname + '/config' + \0 */
75 int ret
, len
= strlen(lxcpath
) + strlen(cname
) + 9;
76 char *fname
= alloca(len
);
78 ret
= snprintf(fname
, len
, "%s/%s/config", lxcpath
, cname
);
79 if (ret
< 0 || ret
>= len
)
82 return file_exists(fname
);
86 * A few functions to help detect when a container creation failed.
87 * If a container creation was killed partway through, then trying
88 * to actually start that container could harm the host. We detect
89 * this by creating a 'partial' file under the container directory,
90 * and keeping an advisory lock. When container creation completes,
91 * we remove that file. When we load or try to start a container, if
92 * we find that file, without a flock, we remove the container.
94 int ongoing_create(struct lxc_container
*c
)
96 int len
= strlen(c
->config_path
) + strlen(c
->name
) + 10;
97 char *path
= alloca(len
);
101 ret
= snprintf(path
, len
, "%s/%s/partial", c
->config_path
, c
->name
);
102 if (ret
< 0 || ret
>= len
) {
103 ERROR("Error writing partial pathname");
107 if (!file_exists(path
))
110 fd
= open(path
, O_RDWR
);
113 // give benefit of the doubt
114 SYSERROR("Error opening partial file");
118 lk
.l_whence
= SEEK_SET
;
122 if (fcntl(fd
, F_GETLK
, &lk
) == 0 && lk
.l_pid
!= -1) {
123 // create is still ongoing
129 // create completed but partial is still there.
136 int create_partial(struct lxc_container
*c
)
138 // $lxcpath + '/' + $name + '/partial' + \0
139 int len
= strlen(c
->config_path
) + strlen(c
->name
) + 10;
140 char *path
= alloca(len
);
144 ret
= snprintf(path
, len
, "%s/%s/partial", c
->config_path
, c
->name
);
145 if (ret
< 0 || ret
>= len
) {
146 ERROR("Error writing partial pathname");
150 if ((fd
=open(path
, O_RDWR
| O_CREAT
| O_EXCL
, 0755)) < 0) {
151 SYSERROR("Erorr creating partial file");
156 lk
.l_whence
= SEEK_SET
;
159 if (fcntl(fd
, F_SETLKW
, &lk
) < 0) {
160 SYSERROR("Error locking partial file %s", path
);
170 void remove_partial(struct lxc_container
*c
, int fd
)
172 // $lxcpath + '/' + $name + '/partial' + \0
173 int len
= strlen(c
->config_path
) + strlen(c
->name
) + 10;
174 char *path
= alloca(len
);
180 ret
= snprintf(path
, len
, "%s/%s/partial", c
->config_path
, c
->name
);
181 if (ret
< 0 || ret
>= len
) {
182 ERROR("Error writing partial pathname");
185 if (unlink(path
) < 0)
186 SYSERROR("Error unlink partial file %s", path
);
190 * 1. container_mem_lock(c) protects the struct lxc_container from multiple threads.
191 * 2. container_disk_lock(c) protects the on-disk container data - in particular the
192 * container configuration file.
193 * The container_disk_lock also takes the container_mem_lock.
194 * 3. thread_mutex protects process data (ex: fd table) from multiple threads.
195 * NOTHING mutexes two independent programs with their own struct
196 * lxc_container for the same c->name, between API calls. For instance,
197 * c->config_read(); c->start(); Between those calls, data on disk
198 * could change (which shouldn't bother the caller unless for instance
199 * the rootfs get moved). c->config_read(); update; c->config_write();
200 * Two such updaters could race. The callers should therefore check their
201 * results. Trying to prevent that would necessarily expose us to deadlocks
202 * due to hung callers. So I prefer to keep the locks only within our own
203 * functions, not across functions.
205 * If you're going to clone while holding a lxccontainer, increment
206 * c->numthreads (under privlock) before forking. When deleting,
207 * decrement numthreads under privlock, then if it hits 0 you can delete.
208 * Do not ever use a lxccontainer whose numthreads you did not bump.
211 static void lxc_container_free(struct lxc_container
*c
)
218 c
->configfile
= NULL
;
220 if (c
->error_string
) {
221 free(c
->error_string
);
222 c
->error_string
= NULL
;
225 lxc_putlock(c
->slock
);
229 lxc_putlock(c
->privlock
);
237 lxc_conf_free(c
->lxc_conf
);
240 if (c
->config_path
) {
241 free(c
->config_path
);
242 c
->config_path
= NULL
;
248 * Consider the following case:
249 freer | racing get()er
250 ==================================================================
251 lxc_container_put() | lxc_container_get()
252 \ lxclock(c->privlock) | c->numthreads < 1? (no)
253 \ c->numthreads = 0 | \ lxclock(c->privlock) -> waits
255 \ lxc_container_free() | \ lxclock() returns
256 | \ c->numthreads < 1 -> return 0
258 \ \ sem_destroy(privlock) |
260 * When the get()er checks numthreads the first time, one of the following
262 * 1. freer has set numthreads = 0. get() returns 0
263 * 2. freer is between lxclock and setting numthreads to 0. get()er will
264 * sem_wait on privlock, get lxclock after freer() drops it, then see
265 * numthreads is 0 and exit without touching lxclock again..
266 * 3. freer has not yet locked privlock. If get()er runs first, then put()er
267 * will see --numthreads = 1 and not call lxc_container_free().
270 int lxc_container_get(struct lxc_container
*c
)
275 // if someone else has already started freeing the container, don't
276 // try to take the lock, which may be invalid
277 if (c
->numthreads
< 1)
280 if (container_mem_lock(c
))
282 if (c
->numthreads
< 1) {
283 // bail without trying to unlock, bc the privlock is now probably
288 container_mem_unlock(c
);
292 int lxc_container_put(struct lxc_container
*c
)
296 if (container_mem_lock(c
))
298 if (--c
->numthreads
< 1) {
299 container_mem_unlock(c
);
300 lxc_container_free(c
);
303 container_mem_unlock(c
);
307 static bool lxcapi_is_defined(struct lxc_container
*c
)
316 if (container_mem_lock(c
))
320 statret
= stat(c
->configfile
, &statbuf
);
326 container_mem_unlock(c
);
330 static const char *lxcapi_state(struct lxc_container
*c
)
336 s
= lxc_getstate(c
->name
, c
->config_path
);
337 return lxc_state2str(s
);
340 static bool is_stopped(struct lxc_container
*c
)
343 s
= lxc_getstate(c
->name
, c
->config_path
);
344 return (s
== STOPPED
);
347 static bool lxcapi_is_running(struct lxc_container
*c
)
354 if (!s
|| strcmp(s
, "STOPPED") == 0)
359 static bool lxcapi_freeze(struct lxc_container
*c
)
365 ret
= lxc_freeze(c
->name
, c
->config_path
);
371 static bool lxcapi_unfreeze(struct lxc_container
*c
)
377 ret
= lxc_unfreeze(c
->name
, c
->config_path
);
383 static int lxcapi_console_getfd(struct lxc_container
*c
, int *ttynum
, int *masterfd
)
389 ttyfd
= lxc_console_getfd(c
, ttynum
, masterfd
);
393 static int lxcapi_console(struct lxc_container
*c
, int ttynum
, int stdinfd
,
394 int stdoutfd
, int stderrfd
, int escape
)
396 return lxc_console(c
, ttynum
, stdinfd
, stdoutfd
, stderrfd
, escape
);
399 static pid_t
lxcapi_init_pid(struct lxc_container
*c
)
404 return lxc_cmd_get_init_pid(c
->name
, c
->config_path
);
407 static bool load_config_locked(struct lxc_container
*c
, const char *fname
)
410 c
->lxc_conf
= lxc_conf_init();
411 if (c
->lxc_conf
&& !lxc_config_read(fname
, c
->lxc_conf
))
416 static bool lxcapi_load_config(struct lxc_container
*c
, const char *alt_file
)
418 bool ret
= false, need_disklock
= false;
424 fname
= c
->configfile
;
430 * If we're reading something other than the container's config,
431 * we only need to lock the in-memory container. If loading the
432 * container's config file, take the disk lock.
434 if (strcmp(fname
, c
->configfile
) == 0)
435 need_disklock
= true;
438 lret
= container_disk_lock(c
);
440 lret
= container_mem_lock(c
);
444 ret
= load_config_locked(c
, fname
);
447 container_disk_unlock(c
);
449 container_mem_unlock(c
);
453 static void lxcapi_want_daemonize(struct lxc_container
*c
)
457 if (container_mem_lock(c
)) {
458 ERROR("Error getting mem lock");
462 /* daemonize implies close_all_fds so set it */
463 c
->lxc_conf
->close_all_fds
= 1;
464 container_mem_unlock(c
);
467 static bool lxcapi_want_close_all_fds(struct lxc_container
*c
)
469 if (!c
|| !c
->lxc_conf
)
471 if (container_mem_lock(c
)) {
472 ERROR("Error getting mem lock");
475 c
->lxc_conf
->close_all_fds
= 1;
476 container_mem_unlock(c
);
480 static bool lxcapi_wait(struct lxc_container
*c
, const char *state
, int timeout
)
487 ret
= lxc_wait(c
->name
, state
, timeout
, c
->config_path
);
492 static bool wait_on_daemonized_start(struct lxc_container
*c
)
494 /* we'll probably want to make this timeout configurable? */
495 int timeout
= 5, ret
, status
;
498 * our child is going to fork again, then exit. reap the
502 if (ret
== -1 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
503 DEBUG("failed waiting for first dual-fork child");
504 return lxcapi_wait(c
, "RUNNING", timeout
);
508 * I can't decide if it'd be more convenient for callers if we accept '...',
509 * or a null-terminated array (i.e. execl vs execv)
511 static bool lxcapi_start(struct lxc_container
*c
, int useinit
, char * const argv
[])
514 struct lxc_conf
*conf
;
516 char *default_args
[] = {
521 /* container exists */
524 /* container has been setup */
528 if ((ret
= ongoing_create(c
)) < 0) {
529 ERROR("Error checking for incomplete creation");
533 ERROR("Error: %s creation was not completed", c
->name
);
536 } else if (ret
== 1) {
537 ERROR("Error: creation of %s is ongoing", c
->name
);
541 /* is this app meant to be run through lxcinit, as in lxc-execute? */
542 if (useinit
&& !argv
)
545 if (container_mem_lock(c
))
548 daemonize
= c
->daemonize
;
549 container_mem_unlock(c
);
552 ret
= lxc_execute(c
->name
, argv
, 1, conf
, c
->config_path
);
553 return ret
== 0 ? true : false;
560 * say, I'm not sure - what locks do we want here? Any?
561 * Is liblxc's locking enough here to protect the on disk
562 * container? We don't want to exclude things like lxc_info
563 * while container is running...
566 if (!lxc_container_get(c
))
568 lxc_monitord_spawn(c
->config_path
);
572 lxc_container_put(c
);
576 return wait_on_daemonized_start(c
);
578 process_unlock(); // we're no longer sharing
579 /* second fork to be reparented by init */
582 SYSERROR("Error doing dual-fork");
587 /* like daemon(), chdir to / and redirect 0,1,2 to /dev/null */
589 SYSERROR("Error chdir()ing to /.");
595 open("/dev/zero", O_RDONLY
);
596 open("/dev/null", O_RDWR
);
597 open("/dev/null", O_RDWR
);
603 ret
= lxc_start(c
->name
, argv
, conf
, c
->config_path
);
606 INFO("container requested reboot");
612 lxc_container_put(c
);
613 exit (ret
== 0 ? true : false);
615 return (ret
== 0 ? true : false);
620 * note there MUST be an ending NULL
622 static bool lxcapi_startl(struct lxc_container
*c
, int useinit
, ...)
625 char **inargs
= NULL
;
628 /* container exists */
632 va_start(ap
, useinit
);
633 inargs
= lxc_va_arg_list_to_argv(ap
, 0, 1);
637 ERROR("Memory allocation error.");
641 /* pass NULL if no arguments were supplied */
642 bret
= lxcapi_start(c
, useinit
, *inargs
? inargs
: NULL
);
647 for (arg
= inargs
; *arg
; arg
++)
655 static bool lxcapi_stop(struct lxc_container
*c
)
662 ret
= lxc_cmd_stop(c
->name
, c
->config_path
);
668 * create the standard expected container dir
670 static bool create_container_dir(struct lxc_container
*c
)
675 len
= strlen(c
->config_path
) + strlen(c
->name
) + 2;
679 ret
= snprintf(s
, len
, "%s/%s", c
->config_path
, c
->name
);
680 if (ret
< 0 || ret
>= len
) {
684 ret
= mkdir(s
, 0755);
689 SYSERROR("failed to create container path for %s\n", c
->name
);
695 static const char *lxcapi_get_config_path(struct lxc_container
*c
);
696 static bool lxcapi_set_config_item(struct lxc_container
*c
, const char *key
, const char *v
);
699 * do_bdev_create: thin wrapper around bdev_create(). Like bdev_create(),
700 * it returns a mounted bdev on success, NULL on error.
702 static struct bdev
*do_bdev_create(struct lxc_container
*c
, const char *type
,
703 struct bdev_specs
*specs
)
710 /* rootfs.path or lxcpath/lxcname/rootfs */
711 if (c
->lxc_conf
->rootfs
.path
&& access(c
->lxc_conf
->rootfs
.path
, F_OK
) == 0) {
712 const char *rpath
= c
->lxc_conf
->rootfs
.path
;
713 len
= strlen(rpath
) + 1;
715 ret
= snprintf(dest
, len
, "%s", rpath
);
717 const char *lxcpath
= lxcapi_get_config_path(c
);
718 len
= strlen(c
->name
) + strlen(lxcpath
) + 9;
720 ret
= snprintf(dest
, len
, "%s/%s/rootfs", lxcpath
, c
->name
);
722 if (ret
< 0 || ret
>= len
)
725 bdev
= bdev_create(dest
, type
, c
->name
, specs
);
727 ERROR("Failed to create backing store type %s\n", type
);
731 lxcapi_set_config_item(c
, "lxc.rootfs", bdev
->src
);
733 /* if we are not root, chown the rootfs dir to root in the
736 if (geteuid() != 0) {
737 if (chown_mapped_root(bdev
->dest
, c
->lxc_conf
) < 0) {
738 ERROR("Error chowning %s to container root\n", bdev
->dest
);
748 * Given the '-t' template option to lxc-create, figure out what to
749 * do. If the template is a full executable path, use that. If it
750 * is something like 'sshd', then return $templatepath/lxc-sshd.
751 * On success return the template, on error return NULL.
753 static char *get_template_path(const char *t
)
758 if (t
[0] == '/' && access(t
, X_OK
) == 0) {
763 len
= strlen(LXCTEMPLATEDIR
) + strlen(t
) + strlen("/lxc-") + 1;
767 ret
= snprintf(tpath
, len
, "%s/lxc-%s", LXCTEMPLATEDIR
, t
);
768 if (ret
< 0 || ret
>= len
) {
772 if (access(tpath
, X_OK
) < 0) {
773 SYSERROR("bad template: %s\n", t
);
781 static char *lxcbasename(char *path
)
783 char *p
= path
+ strlen(path
) - 1;
784 while (*p
!= '/' && p
> path
)
789 static bool create_run_template(struct lxc_container
*c
, char *tpath
, bool quiet
,
799 SYSERROR("failed to fork task for container creation template\n");
803 if (pid
== 0) { // child
804 char *patharg
, *namearg
, *rootfsarg
, *src
;
805 struct bdev
*bdev
= NULL
;
807 int ret
, len
, nargs
= 0;
809 struct lxc_conf
*conf
= c
->lxc_conf
;
811 process_unlock(); // we're no longer sharing
816 open("/dev/zero", O_RDONLY
);
817 open("/dev/null", O_RDWR
);
818 open("/dev/null", O_RDWR
);
821 src
= c
->lxc_conf
->rootfs
.path
;
823 * for an overlayfs create, what the user wants is the template to fill
824 * in what will become the readonly lower layer. So don't mount for
827 if (strncmp(src
, "overlayfs:", 10) == 0) {
828 src
= overlayfs_getlower(src
+10);
830 bdev
= bdev_init(src
, c
->lxc_conf
->rootfs
.mount
, NULL
);
832 ERROR("Error opening rootfs");
836 if (strcmp(bdev
->type
, "dir") != 0) {
837 if (unshare(CLONE_NEWNS
) < 0) {
838 ERROR("error unsharing mounts");
841 if (bdev
->ops
->mount(bdev
) < 0) {
842 ERROR("Error mounting rootfs");
845 } else { // TODO come up with a better way here!
848 bdev
->dest
= strdup(bdev
->src
);
852 * create our new array, pre-pend the template name and
856 for (nargs
= 0; argv
[nargs
]; nargs
++) ;
857 nargs
+= 4; // template, path, rootfs and name args
859 newargv
= malloc(nargs
* sizeof(*newargv
));
862 newargv
[0] = lxcbasename(tpath
);
864 len
= strlen(c
->config_path
) + strlen(c
->name
) + strlen("--path=") + 2;
865 patharg
= malloc(len
);
868 ret
= snprintf(patharg
, len
, "--path=%s/%s", c
->config_path
, c
->name
);
869 if (ret
< 0 || ret
>= len
)
871 newargv
[1] = patharg
;
872 len
= strlen("--name=") + strlen(c
->name
) + 1;
873 namearg
= malloc(len
);
876 ret
= snprintf(namearg
, len
, "--name=%s", c
->name
);
877 if (ret
< 0 || ret
>= len
)
879 newargv
[2] = namearg
;
881 len
= strlen("--rootfs=") + 1 + strlen(bdev
->dest
);
882 rootfsarg
= malloc(len
);
885 ret
= snprintf(rootfsarg
, len
, "--rootfs=%s", bdev
->dest
);
886 if (ret
< 0 || ret
>= len
)
888 newargv
[3] = rootfsarg
;
890 /* add passed-in args */
892 for (i
= 4; i
< nargs
; i
++)
893 newargv
[i
] = argv
[i
-4];
895 /* add trailing NULL */
897 newargv
= realloc(newargv
, nargs
* sizeof(*newargv
));
900 newargv
[nargs
- 1] = NULL
;
903 * If we're running the template in a mapped userns, then
904 * we prepend the template command with:
905 * lxc-usernsexec <-m map1> ... <-m mapn> --
907 if (geteuid() != 0 && !lxc_list_empty(&conf
->id_map
)) {
909 char **n2
= malloc(n2args
* sizeof(*n2
));
914 tpath
= "lxc-usernsexec";
915 n2
[0] = "lxc-usernsexec";
916 lxc_list_for_each(it
, &conf
->id_map
) {
919 n2
= realloc(n2
, n2args
* sizeof(*n2
));
923 n2
[n2args
-1] = malloc(200);
926 ret
= snprintf(n2
[n2args
-1], 200, "%c:%lu:%lu:%lu",
927 map
->idtype
== ID_TYPE_UID
? 'u' : 'g',
928 map
->nsid
, map
->hostid
, map
->range
);
929 if (ret
< 0 || ret
>= 200)
932 bool hostid_mapped
= hostid_is_mapped(geteuid(), conf
);
933 int extraargs
= hostid_mapped
? 1 : 3;
934 n2
= realloc(n2
, (nargs
+ n2args
+ extraargs
) * sizeof(*n2
));
937 if (!hostid_mapped
) {
938 int free_id
= find_unmapped_nsuid(conf
);
941 ERROR("Could not find free uid to map");
944 n2
[n2args
++] = malloc(200);
946 SYSERROR("out of memory");
949 ret
= snprintf(n2
[n2args
-1], 200, "u:%d:%d:1",
951 if (ret
< 0 || ret
>= 200) {
952 ERROR("string too long");
957 for (i
= 0; i
< nargs
; i
++)
958 n2
[i
+ n2args
] = newargv
[i
];
963 execvp(tpath
, newargv
);
964 SYSERROR("failed to execute template %s", tpath
);
968 if (wait_for_pid(pid
) != 0) {
969 ERROR("container creation template for %s failed\n", c
->name
);
976 bool prepend_lxc_header(char *path
, const char *t
, char *const argv
[])
984 unsigned char md_value
[SHA_DIGEST_LENGTH
];
989 f
= fopen(path
, "r");
994 if (fseek(f
, 0, SEEK_END
) < 0)
996 if ((flen
= ftell(f
)) < 0)
998 if (fseek(f
, 0, SEEK_SET
) < 0)
1000 if ((contents
= malloc(flen
+ 1)) == NULL
)
1002 if (fread(contents
, 1, flen
, f
) != flen
)
1003 goto out_free_contents
;
1005 contents
[flen
] = '\0';
1011 goto out_free_contents
;
1014 tpath
= get_template_path(t
);
1016 ERROR("bad template: %s\n", t
);
1017 goto out_free_contents
;
1020 ret
= sha1sum_file(tpath
, md_value
);
1022 ERROR("Error getting sha1sum of %s", tpath
);
1024 goto out_free_contents
;
1030 f
= fopen(path
, "w");
1033 SYSERROR("reopening config for writing");
1037 fprintf(f
, "# Template used to create this container: %s\n", t
);
1039 fprintf(f
, "# Parameters passed to the template:");
1041 fprintf(f
, " %s", *argv
);
1047 fprintf(f
, "# Template script checksum (SHA-1): ");
1048 for (i
=0; i
<SHA_DIGEST_LENGTH
; i
++)
1049 fprintf(f
, "%02x", md_value
[i
]);
1052 if (fwrite(contents
, 1, flen
, f
) != flen
) {
1053 SYSERROR("Writing original contents");
1073 SYSERROR("Error prepending header");
1079 static bool lxcapi_destroy(struct lxc_container
*c
);
1082 * create a container with the given parameters.
1083 * @c: container to be created. It has the lxcpath, name, and a starting
1084 * configuration already set
1085 * @t: the template to execute to instantiate the root filesystem and
1086 * adjust the configuration.
1087 * @bdevtype: backing store type to use. If NULL, dir will be used.
1088 * @specs: additional parameters for the backing store, i.e. LVM vg to
1091 * @argv: the arguments to pass to the template, terminated by NULL. If no
1092 * arguments, you can just pass NULL.
1094 static bool lxcapi_create(struct lxc_container
*c
, const char *t
,
1095 const char *bdevtype
, struct bdev_specs
*specs
, int flags
,
1107 tpath
= get_template_path(t
);
1109 ERROR("bad template: %s\n", t
);
1115 * If a template is passed in, and the rootfs already is defined in
1116 * the container config and exists, then * caller is trying to create
1117 * an existing container. Return an error, but do NOT delete the
1120 if (lxcapi_is_defined(c
) && c
->lxc_conf
&& c
->lxc_conf
->rootfs
.path
&&
1121 access(c
->lxc_conf
->rootfs
.path
, F_OK
) == 0 && tpath
) {
1122 ERROR("Container %s:%s already exists", c
->config_path
, c
->name
);
1127 /* Save the loaded configuration to disk */
1128 if (!c
->save_config(c
, NULL
)) {
1129 ERROR("failed to save starting configuration for %s\n", c
->name
);
1134 * either template or rootfs.path should be set.
1135 * if both template and rootfs.path are set, template is setup as rootfs.path.
1136 * container is already created if we have a config and rootfs.path is accessible
1138 if (!c
->lxc_conf
->rootfs
.path
&& !tpath
)
1139 /* no template passed in and rootfs does not exist: error */
1141 if (c
->lxc_conf
->rootfs
.path
&& access(c
->lxc_conf
->rootfs
.path
, F_OK
) != 0)
1142 /* rootfs passed into configuration, but does not exist: error */
1144 if (lxcapi_is_defined(c
) && c
->lxc_conf
->rootfs
.path
&& !tpath
) {
1145 /* Rootfs already existed, user just wanted to save the
1146 * loaded configuration */
1151 /* Mark that this container is being created */
1152 if ((partial_fd
= create_partial(c
)) < 0)
1155 /* no need to get disk lock bc we have the partial locked */
1158 * Create the backing store
1159 * Note we can't do this in the same task as we use to execute the
1160 * template because of the way zfs works.
1161 * After you 'zfs create', zfs mounts the fs only in the initial
1166 SYSERROR("failed to fork task for container creation template\n");
1170 if (pid
== 0) { // child
1171 struct bdev
*bdev
= NULL
;
1173 process_unlock(); // we're no longer sharing
1174 if (!(bdev
= do_bdev_create(c
, bdevtype
, specs
))) {
1175 ERROR("Error creating backing store type %s for %s",
1176 bdevtype
? bdevtype
: "(none)", c
->name
);
1180 /* save config file again to store the new rootfs location */
1181 if (!c
->save_config(c
, NULL
)) {
1182 ERROR("failed to save starting configuration for %s\n", c
->name
);
1183 // parent task won't see bdev in config so we delete it
1184 bdev
->ops
->umount(bdev
);
1185 bdev
->ops
->destroy(bdev
);
1190 if (wait_for_pid(pid
) != 0)
1193 /* reload config to get the rootfs */
1195 lxc_conf_free(c
->lxc_conf
);
1197 if (!load_config_locked(c
, c
->configfile
))
1200 if (!create_run_template(c
, tpath
, !!(flags
& LXC_CREATE_QUIET
), argv
))
1203 // now clear out the lxc_conf we have, reload from the created
1206 lxc_conf_free(c
->lxc_conf
);
1210 if (!prepend_lxc_header(c
->configfile
, tpath
, argv
)) {
1211 ERROR("Error prepending header to configuration file");
1215 ret
= load_config_locked(c
, c
->configfile
);
1218 if (partial_fd
>= 0)
1219 remove_partial(c
, partial_fd
);
1228 static bool lxcapi_reboot(struct lxc_container
*c
)
1234 if (!c
->is_running(c
))
1236 pid
= c
->init_pid(c
);
1239 if (kill(pid
, SIGINT
) < 0)
1245 static bool lxcapi_shutdown(struct lxc_container
*c
, int timeout
)
1255 if (!c
->is_running(c
))
1257 pid
= c
->init_pid(c
);
1261 retv
= c
->wait(c
, "STOPPED", timeout
);
1262 if (!retv
&& timeout
> 0) {
1264 retv
= c
->wait(c
, "STOPPED", 0); // 0 means don't wait
1269 static bool lxcapi_createl(struct lxc_container
*c
, const char *t
,
1270 const char *bdevtype
, struct bdev_specs
*specs
, int flags
, ...)
1280 * since we're going to wait for create to finish, I don't think we
1281 * need to get a copy of the arguments.
1283 va_start(ap
, flags
);
1284 args
= lxc_va_arg_list_to_argv(ap
, 0, 0);
1287 ERROR("Memory allocation error.");
1291 bret
= c
->create(c
, t
, bdevtype
, specs
, flags
, args
);
1298 static bool lxcapi_clear_config_item(struct lxc_container
*c
, const char *key
)
1302 if (!c
|| !c
->lxc_conf
)
1304 if (container_mem_lock(c
))
1306 ret
= lxc_clear_config_item(c
->lxc_conf
, key
);
1307 container_mem_unlock(c
);
1311 static inline void exit_from_ns(struct lxc_container
*c
, int *old_netns
, int *new_netns
) {
1312 /* Switch back to original netns */
1313 if (*old_netns
>= 0 && setns(*old_netns
, CLONE_NEWNET
))
1314 SYSERROR("failed to setns");
1316 if (*new_netns
>= 0)
1318 if (*old_netns
>= 0)
1323 static inline bool enter_to_ns(struct lxc_container
*c
, int *old_netns
, int *new_netns
) {
1325 char new_netns_path
[MAXPATHLEN
];
1327 if (!c
->is_running(c
))
1330 /* Save reference to old netns */
1332 *old_netns
= open("/proc/self/ns/net", O_RDONLY
);
1334 if (*old_netns
< 0) {
1335 SYSERROR("failed to open /proc/self/ns/net");
1339 /* Switch to new netns */
1340 ret
= snprintf(new_netns_path
, MAXPATHLEN
, "/proc/%d/ns/net", c
->init_pid(c
));
1341 if (ret
< 0 || ret
>= MAXPATHLEN
)
1345 *new_netns
= open(new_netns_path
, O_RDONLY
);
1347 if (*new_netns
< 0) {
1348 SYSERROR("failed to open %s", new_netns_path
);
1352 if (setns(*new_netns
, CLONE_NEWNET
)) {
1353 SYSERROR("failed to setns");
1358 exit_from_ns(c
, old_netns
, new_netns
);
1362 // used by qsort and bsearch functions for comparing names
1363 static inline int string_cmp(char **first
, char **second
)
1365 return strcmp(*first
, *second
);
1368 // used by qsort and bsearch functions for comparing container names
1369 static inline int container_cmp(struct lxc_container
**first
, struct lxc_container
**second
)
1371 return strcmp((*first
)->name
, (*second
)->name
);
1374 static bool add_to_array(char ***names
, char *cname
, int pos
)
1376 char **newnames
= realloc(*names
, (pos
+1) * sizeof(char *));
1378 ERROR("Out of memory");
1383 newnames
[pos
] = strdup(cname
);
1387 // sort the arrray as we will use binary search on it
1388 qsort(newnames
, pos
+ 1, sizeof(char *), (int (*)(const void *,const void *))string_cmp
);
1393 static bool add_to_clist(struct lxc_container
***list
, struct lxc_container
*c
, int pos
)
1395 struct lxc_container
**newlist
= realloc(*list
, (pos
+1) * sizeof(struct lxc_container
*));
1397 ERROR("Out of memory");
1404 // sort the arrray as we will use binary search on it
1405 qsort(newlist
, pos
+ 1, sizeof(struct lxc_container
*), (int (*)(const void *,const void *))container_cmp
);
1410 static char** get_from_array(char ***names
, char *cname
, int size
)
1412 return (char **)bsearch(&cname
, *names
, size
, sizeof(char *), (int (*)(const void *, const void *))string_cmp
);
1416 static bool array_contains(char ***names
, char *cname
, int size
) {
1417 if(get_from_array(names
, cname
, size
) != NULL
)
1422 static bool remove_from_array(char ***names
, char *cname
, int size
)
1424 char **result
= get_from_array(names
, cname
, size
);
1425 if (result
!= NULL
) {
1432 static char** lxcapi_get_interfaces(struct lxc_container
*c
)
1435 struct ifaddrs
*interfaceArray
= NULL
, *tempIfAddr
= NULL
;
1436 char **interfaces
= NULL
;
1437 int old_netns
= -1, new_netns
= -1;
1439 if (!enter_to_ns(c
, &old_netns
, &new_netns
))
1442 /* Grab the list of interfaces */
1443 if (getifaddrs(&interfaceArray
)) {
1444 SYSERROR("failed to get interfaces list");
1448 /* Iterate through the interfaces */
1449 for (tempIfAddr
= interfaceArray
; tempIfAddr
!= NULL
; tempIfAddr
= tempIfAddr
->ifa_next
) {
1450 if (array_contains(&interfaces
, tempIfAddr
->ifa_name
, count
))
1453 if(!add_to_array(&interfaces
, tempIfAddr
->ifa_name
, count
))
1460 freeifaddrs(interfaceArray
);
1462 exit_from_ns(c
, &old_netns
, &new_netns
);
1464 /* Append NULL to the array */
1466 interfaces
= (char **)lxc_append_null_to_array((void **)interfaces
, count
);
1471 for(i
=0;i
<count
;i
++)
1472 free(interfaces
[i
]);
1478 static char** lxcapi_get_ips(struct lxc_container
*c
, char* interface
, char* family
, int scope
)
1481 struct ifaddrs
*interfaceArray
= NULL
, *tempIfAddr
= NULL
;
1482 char addressOutputBuffer
[INET6_ADDRSTRLEN
];
1483 void *tempAddrPtr
= NULL
;
1484 char **addresses
= NULL
;
1485 char *address
= NULL
;
1486 int old_netns
= -1, new_netns
= -1;
1488 if (!enter_to_ns(c
, &old_netns
, &new_netns
))
1491 /* Grab the list of interfaces */
1492 if (getifaddrs(&interfaceArray
)) {
1493 SYSERROR("failed to get interfaces list");
1497 /* Iterate through the interfaces */
1498 for (tempIfAddr
= interfaceArray
; tempIfAddr
!= NULL
; tempIfAddr
= tempIfAddr
->ifa_next
) {
1499 if (tempIfAddr
->ifa_addr
== NULL
)
1502 if(tempIfAddr
->ifa_addr
->sa_family
== AF_INET
) {
1503 if (family
&& strcmp(family
, "inet"))
1505 tempAddrPtr
= &((struct sockaddr_in
*)tempIfAddr
->ifa_addr
)->sin_addr
;
1508 if (family
&& strcmp(family
, "inet6"))
1511 if (((struct sockaddr_in6
*)tempIfAddr
->ifa_addr
)->sin6_scope_id
!= scope
)
1514 tempAddrPtr
= &((struct sockaddr_in6
*)tempIfAddr
->ifa_addr
)->sin6_addr
;
1517 if (interface
&& strcmp(interface
, tempIfAddr
->ifa_name
))
1519 else if (!interface
&& strcmp("lo", tempIfAddr
->ifa_name
) == 0)
1522 address
= (char *)inet_ntop(tempIfAddr
->ifa_addr
->sa_family
,
1524 addressOutputBuffer
,
1525 sizeof(addressOutputBuffer
));
1529 if(!add_to_array(&addresses
, address
, count
))
1536 freeifaddrs(interfaceArray
);
1538 exit_from_ns(c
, &old_netns
, &new_netns
);
1540 /* Append NULL to the array */
1542 addresses
= (char **)lxc_append_null_to_array((void **)addresses
, count
);
1547 for(i
=0;i
<count
;i
++)
1555 static int lxcapi_get_config_item(struct lxc_container
*c
, const char *key
, char *retv
, int inlen
)
1559 if (!c
|| !c
->lxc_conf
)
1561 if (container_mem_lock(c
))
1563 ret
= lxc_get_config_item(c
->lxc_conf
, key
, retv
, inlen
);
1564 container_mem_unlock(c
);
1568 static int lxcapi_get_keys(struct lxc_container
*c
, const char *key
, char *retv
, int inlen
)
1571 return lxc_listconfigs(retv
, inlen
);
1573 * Support 'lxc.network.<idx>', i.e. 'lxc.network.0'
1574 * This is an intelligent result to show which keys are valid given
1575 * the type of nic it is
1577 if (!c
|| !c
->lxc_conf
)
1579 if (container_mem_lock(c
))
1582 if (strncmp(key
, "lxc.network.", 12) == 0)
1583 ret
= lxc_list_nicconfigs(c
->lxc_conf
, key
, retv
, inlen
);
1584 container_mem_unlock(c
);
1588 static bool lxcapi_save_config(struct lxc_container
*c
, const char *alt_file
)
1591 bool ret
= false, need_disklock
= false;
1595 alt_file
= c
->configfile
;
1597 return false; // should we write to stdout if no file is specified?
1599 // If we haven't yet loaded a config, load the stock config
1601 if (!c
->load_config(c
, LXC_DEFAULT_CONFIG
)) {
1602 ERROR("Error loading default configuration file %s while saving %s\n", LXC_DEFAULT_CONFIG
, c
->name
);
1607 if (!create_container_dir(c
))
1611 * If we're writing to the container's config file, take the
1612 * disk lock. Otherwise just take the memlock to protect the
1613 * struct lxc_container while we're traversing it.
1615 if (strcmp(c
->configfile
, alt_file
) == 0)
1616 need_disklock
= true;
1619 lret
= container_disk_lock(c
);
1621 lret
= container_mem_lock(c
);
1626 fout
= fopen(alt_file
, "w");
1629 write_config(fout
, c
->lxc_conf
);
1635 container_disk_unlock(c
);
1637 container_mem_unlock(c
);
1641 static bool mod_rdep(struct lxc_container
*c
, bool inc
)
1643 char path
[MAXPATHLEN
];
1648 if (container_disk_lock(c
))
1650 ret
= snprintf(path
, MAXPATHLEN
, "%s/%s/lxc_snapshots", c
->config_path
,
1652 if (ret
< 0 || ret
> MAXPATHLEN
)
1655 f
= fopen(path
, "r");
1658 ret
= fscanf(f
, "%d", &v
);
1663 ERROR("Corrupted file %s", path
);
1669 f
= fopen(path
, "w");
1673 if (fprintf(f
, "%d\n", v
) < 0) {
1674 ERROR("Error writing new snapshots value");
1684 SYSERROR("Error writing to or closing snapshots file");
1691 container_disk_unlock(c
);
1695 static void strip_newline(char *p
)
1697 size_t len
= strlen(p
);
1700 if (p
[len
-1] == '\n')
1704 static void mod_all_rdeps(struct lxc_container
*c
, bool inc
)
1706 struct lxc_container
*p
;
1707 char *lxcpath
= NULL
, *lxcname
= NULL
, path
[MAXPATHLEN
];
1708 size_t pathlen
= 0, namelen
= 0;
1712 ret
= snprintf(path
, MAXPATHLEN
, "%s/%s/lxc_rdepends",
1713 c
->config_path
, c
->name
);
1714 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1715 ERROR("Path name too long");
1719 f
= fopen(path
, "r");
1723 while (getline(&lxcpath
, &pathlen
, f
) != -1) {
1724 if (getline(&lxcname
, &namelen
, f
) == -1) {
1725 ERROR("badly formatted file %s\n", path
);
1728 strip_newline(lxcpath
);
1729 strip_newline(lxcname
);
1730 if ((p
= lxc_container_new(lxcname
, lxcpath
)) == NULL
) {
1731 ERROR("Unable to find dependent container %s:%s",
1735 if (!mod_rdep(p
, inc
))
1736 ERROR("Failed to increase numsnapshots for %s:%s",
1738 lxc_container_put(p
);
1741 if (lxcpath
) free(lxcpath
);
1742 if (lxcname
) free(lxcname
);
1748 static bool has_snapshots(struct lxc_container
*c
)
1750 char path
[MAXPATHLEN
];
1755 ret
= snprintf(path
, MAXPATHLEN
, "%s/%s/lxc_snapshots", c
->config_path
,
1757 if (ret
< 0 || ret
> MAXPATHLEN
)
1760 f
= fopen(path
, "r");
1764 ret
= fscanf(f
, "%d", &v
);
1776 // do we want the api to support --force, or leave that to the caller?
1777 static bool lxcapi_destroy(struct lxc_container
*c
)
1779 struct bdev
*r
= NULL
;
1782 if (!c
|| !lxcapi_is_defined(c
))
1785 if (container_disk_lock(c
))
1788 if (!is_stopped(c
)) {
1789 // we should queue some sort of error - in c->error_string?
1790 ERROR("container %s is not stopped", c
->name
);
1794 if (c
->lxc_conf
&& has_snapshots(c
)) {
1795 ERROR("container %s has dependent snapshots", c
->name
);
1799 if (c
->lxc_conf
&& c
->lxc_conf
->rootfs
.path
&& c
->lxc_conf
->rootfs
.mount
)
1800 r
= bdev_init(c
->lxc_conf
->rootfs
.path
, c
->lxc_conf
->rootfs
.mount
, NULL
);
1802 if (r
->ops
->destroy(r
) < 0) {
1804 ERROR("Error destroying rootfs for %s", c
->name
);
1810 mod_all_rdeps(c
, false);
1812 const char *p1
= lxcapi_get_config_path(c
);
1813 char *path
= alloca(strlen(p1
) + strlen(c
->name
) + 2);
1814 sprintf(path
, "%s/%s", p1
, c
->name
);
1815 if (lxc_rmdir_onedev(path
) < 0) {
1816 ERROR("Error destroying container directory for %s", c
->name
);
1822 container_disk_unlock(c
);
1826 static bool set_config_item_locked(struct lxc_container
*c
, const char *key
, const char *v
)
1828 struct lxc_config_t
*config
;
1831 c
->lxc_conf
= lxc_conf_init();
1834 config
= lxc_getconfig(key
);
1837 return (0 == config
->cb(key
, v
, c
->lxc_conf
));
1840 static bool lxcapi_set_config_item(struct lxc_container
*c
, const char *key
, const char *v
)
1847 if (container_mem_lock(c
))
1850 b
= set_config_item_locked(c
, key
, v
);
1852 container_mem_unlock(c
);
1856 static char *lxcapi_config_file_name(struct lxc_container
*c
)
1858 if (!c
|| !c
->configfile
)
1860 return strdup(c
->configfile
);
1863 static const char *lxcapi_get_config_path(struct lxc_container
*c
)
1865 if (!c
|| !c
->config_path
)
1867 return (const char *)(c
->config_path
);
1872 * Just recalculate the c->configfile based on the
1873 * c->config_path, which must be set.
1874 * The lxc_container must be locked or not yet public.
1876 static bool set_config_filename(struct lxc_container
*c
)
1881 if (!c
->config_path
)
1884 /* $lxc_path + "/" + c->name + "/" + "config" + '\0' */
1885 len
= strlen(c
->config_path
) + strlen(c
->name
) + strlen("config") + 3;
1886 newpath
= malloc(len
);
1890 ret
= snprintf(newpath
, len
, "%s/%s/config", c
->config_path
, c
->name
);
1891 if (ret
< 0 || ret
>= len
) {
1892 fprintf(stderr
, "Error printing out config file name\n");
1898 free(c
->configfile
);
1899 c
->configfile
= newpath
;
1904 static bool lxcapi_set_config_path(struct lxc_container
*c
, const char *path
)
1908 char *oldpath
= NULL
;
1913 if (container_mem_lock(c
))
1918 ERROR("Out of memory setting new lxc path");
1924 oldpath
= c
->config_path
;
1927 /* Since we've changed the config path, we have to change the
1928 * config file name too */
1929 if (!set_config_filename(c
)) {
1930 ERROR("Out of memory setting new config filename");
1932 free(c
->config_path
);
1933 c
->config_path
= oldpath
;
1939 container_mem_unlock(c
);
1944 static bool lxcapi_set_cgroup_item(struct lxc_container
*c
, const char *subsys
, const char *value
)
1954 if (container_disk_lock(c
))
1957 ret
= lxc_cgroup_set(subsys
, value
, c
->name
, c
->config_path
);
1959 container_disk_unlock(c
);
1963 static int lxcapi_get_cgroup_item(struct lxc_container
*c
, const char *subsys
, char *retv
, int inlen
)
1967 if (!c
|| !c
->lxc_conf
)
1973 if (container_disk_lock(c
))
1976 ret
= lxc_cgroup_get(subsys
, retv
, inlen
, c
->name
, c
->config_path
);
1978 container_disk_unlock(c
);
1982 const char *lxc_get_default_config_path(void)
1984 return default_lxc_path();
1987 const char *lxc_get_default_lvm_vg(void)
1989 return default_lvm_vg();
1992 const char *lxc_get_default_lvm_thin_pool(void)
1994 return default_lvm_thin_pool();
1997 const char *lxc_get_default_zfs_root(void)
1999 return default_zfs_root();
2002 const char *lxc_get_version(void)
2004 return lxc_version();
2007 static int copy_file(char *old
, char *new)
2014 if (file_exists(new)) {
2015 ERROR("copy destination %s exists", new);
2018 ret
= stat(old
, &sbuf
);
2020 INFO("Error stat'ing %s", old
);
2025 in
= open(old
, O_RDONLY
);
2028 SYSERROR("Error opening original file %s", old
);
2032 out
= open(new, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
2035 SYSERROR("Error opening new file %s", new);
2043 len
= read(in
, buf
, 8096);
2045 SYSERROR("Error reading old file %s", old
);
2050 ret
= write(out
, buf
, len
);
2051 if (ret
< len
) { // should we retry?
2052 SYSERROR("Error: write to new file %s was interrupted", new);
2061 // we set mode, but not owner/group
2062 ret
= chmod(new, sbuf
.st_mode
);
2064 SYSERROR("Error setting mode on %s", new);
2078 static int copyhooks(struct lxc_container
*oldc
, struct lxc_container
*c
)
2082 struct lxc_list
*it
;
2084 for (i
=0; i
<NUM_LXC_HOOKS
; i
++) {
2085 lxc_list_for_each(it
, &c
->lxc_conf
->hooks
[i
]) {
2086 char *hookname
= it
->elem
;
2087 char *fname
= strrchr(hookname
, '/');
2088 char tmppath
[MAXPATHLEN
];
2089 if (!fname
) // relative path - we don't support, but maybe we should
2091 // copy the script, and change the entry in confile
2092 ret
= snprintf(tmppath
, MAXPATHLEN
, "%s/%s/%s",
2093 c
->config_path
, c
->name
, fname
+1);
2094 if (ret
< 0 || ret
>= MAXPATHLEN
)
2096 ret
= copy_file(it
->elem
, tmppath
);
2100 it
->elem
= strdup(tmppath
);
2102 ERROR("out of memory copying hook path");
2108 c
->save_config(c
, NULL
);
2112 static void new_hwaddr(char *hwaddr
)
2116 f
= fopen("/dev/urandom", "r");
2120 int ret
= fread(&seed
, sizeof(seed
), 1, f
);
2129 snprintf(hwaddr
, 18, "00:16:3e:%02x:%02x:%02x",
2130 rand() % 255, rand() % 255, rand() % 255);
2133 static void network_new_hwaddrs(struct lxc_container
*c
)
2135 struct lxc_list
*it
;
2137 lxc_list_for_each(it
, &c
->lxc_conf
->network
) {
2138 struct lxc_netdev
*n
= it
->elem
;
2140 new_hwaddr(n
->hwaddr
);
2144 static int copy_fstab(struct lxc_container
*oldc
, struct lxc_container
*c
)
2146 char newpath
[MAXPATHLEN
];
2147 char *oldpath
= oldc
->lxc_conf
->fstab
;
2153 char *p
= strrchr(oldpath
, '/');
2156 ret
= snprintf(newpath
, MAXPATHLEN
, "%s/%s%s",
2157 c
->config_path
, c
->name
, p
);
2158 if (ret
< 0 || ret
>= MAXPATHLEN
) {
2159 ERROR("error printing new path for %s", oldpath
);
2162 if (file_exists(newpath
)) {
2163 ERROR("error: fstab file %s exists", newpath
);
2167 if (copy_file(oldpath
, newpath
) < 0) {
2168 ERROR("error: copying %s to %s", oldpath
, newpath
);
2171 free(c
->lxc_conf
->fstab
);
2172 c
->lxc_conf
->fstab
= strdup(newpath
);
2173 if (!c
->lxc_conf
->fstab
) {
2174 ERROR("error: allocating pathname");
2181 static void copy_rdepends(struct lxc_container
*c
, struct lxc_container
*c0
)
2183 char path0
[MAXPATHLEN
], path1
[MAXPATHLEN
];
2186 ret
= snprintf(path0
, MAXPATHLEN
, "%s/%s/lxc_rdepends", c0
->config_path
,
2188 if (ret
< 0 || ret
>= MAXPATHLEN
) {
2189 WARN("Error copying reverse dependencies");
2192 ret
= snprintf(path1
, MAXPATHLEN
, "%s/%s/lxc_rdepends", c
->config_path
,
2194 if (ret
< 0 || ret
>= MAXPATHLEN
) {
2195 WARN("Error copying reverse dependencies");
2198 if (copy_file(path0
, path1
) < 0) {
2199 INFO("Error copying reverse dependencies");
2204 static bool add_rdepends(struct lxc_container
*c
, struct lxc_container
*c0
)
2207 char path
[MAXPATHLEN
];
2211 ret
= snprintf(path
, MAXPATHLEN
, "%s/%s/lxc_rdepends", c
->config_path
,
2213 if (ret
< 0 || ret
>= MAXPATHLEN
)
2216 f
= fopen(path
, "a");
2221 // if anything goes wrong, just return an error
2222 if (fprintf(f
, "%s\n%s\n", c0
->config_path
, c0
->name
) < 0)
2231 static int copy_storage(struct lxc_container
*c0
, struct lxc_container
*c
,
2232 const char *newtype
, int flags
, const char *bdevdata
, unsigned long newsize
)
2237 bdev
= bdev_copy(c0
->lxc_conf
->rootfs
.path
, c0
->name
, c
->name
,
2238 c0
->config_path
, c
->config_path
, newtype
, !!(flags
& LXC_CLONE_SNAPSHOT
),
2239 bdevdata
, newsize
, &need_rdep
);
2241 ERROR("Error copying storage");
2244 free(c
->lxc_conf
->rootfs
.path
);
2245 c
->lxc_conf
->rootfs
.path
= strdup(bdev
->src
);
2247 if (!c
->lxc_conf
->rootfs
.path
) {
2248 ERROR("Out of memory while setting storage path");
2251 if (flags
& LXC_CLONE_SNAPSHOT
)
2252 copy_rdepends(c
, c0
);
2254 if (!add_rdepends(c
, c0
))
2255 WARN("Error adding reverse dependency from %s to %s",
2259 mod_all_rdeps(c
, true);
2264 static int clone_update_rootfs(struct lxc_container
*c0
,
2265 struct lxc_container
*c
, int flags
,
2269 char path
[MAXPATHLEN
];
2273 struct lxc_conf
*conf
= c
->lxc_conf
;
2275 /* update hostname in rootfs */
2276 /* we're going to mount, so run in a clean namespace to simplify cleanup */
2282 return wait_for_pid(pid
);
2284 process_unlock(); // we're no longer sharing
2285 bdev
= bdev_init(c
->lxc_conf
->rootfs
.path
, c
->lxc_conf
->rootfs
.mount
, NULL
);
2288 if (strcmp(bdev
->type
, "dir") != 0) {
2289 if (unshare(CLONE_NEWNS
) < 0) {
2290 ERROR("error unsharing mounts");
2293 if (bdev
->ops
->mount(bdev
) < 0)
2295 } else { // TODO come up with a better way
2298 bdev
->dest
= strdup(bdev
->src
);
2301 if (!lxc_list_empty(&conf
->hooks
[LXCHOOK_CLONE
])) {
2302 /* Start of environment variable setup for hooks */
2303 if (setenv("LXC_SRC_NAME", c0
->name
, 1)) {
2304 SYSERROR("failed to set environment variable for source container name");
2306 if (setenv("LXC_NAME", c
->name
, 1)) {
2307 SYSERROR("failed to set environment variable for container name");
2309 if (setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1)) {
2310 SYSERROR("failed to set environment variable for config path");
2312 if (setenv("LXC_ROOTFS_MOUNT", conf
->rootfs
.mount
, 1)) {
2313 SYSERROR("failed to set environment variable for rootfs mount");
2315 if (setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1)) {
2316 SYSERROR("failed to set environment variable for rootfs mount");
2319 if (run_lxc_hooks(c
->name
, "clone", conf
, c
->get_config_path(c
), hookargs
)) {
2320 ERROR("Error executing clone hook for %s", c
->name
);
2325 if (!(flags
& LXC_CLONE_KEEPNAME
)) {
2326 ret
= snprintf(path
, MAXPATHLEN
, "%s/etc/hostname", bdev
->dest
);
2327 if (ret
< 0 || ret
>= MAXPATHLEN
)
2329 if (!file_exists(path
))
2331 if (!(fout
= fopen(path
, "w"))) {
2332 SYSERROR("unable to open %s: ignoring\n", path
);
2335 if (fprintf(fout
, "%s", c
->name
) < 0)
2337 if (fclose(fout
) < 0)
2344 * We want to support:
2345 sudo lxc-clone -o o1 -n n1 -s -L|-fssize fssize -v|--vgname vgname \
2346 -p|--lvprefix lvprefix -t|--fstype fstype -B backingstore
2348 -s [ implies overlayfs]
2352 only rootfs gets converted (copied/snapshotted) on clone.
2355 static int create_file_dirname(char *path
)
2357 char *p
= strrchr(path
, '/');
2363 ret
= mkdir(path
, 0755);
2364 if (ret
&& errno
!= EEXIST
)
2365 SYSERROR("creating container path %s\n", path
);
2370 struct lxc_container
*lxcapi_clone(struct lxc_container
*c
, const char *newname
,
2371 const char *lxcpath
, int flags
,
2372 const char *bdevtype
, const char *bdevdata
, unsigned long newsize
,
2375 struct lxc_container
*c2
= NULL
;
2376 char newpath
[MAXPATHLEN
];
2377 int ret
, storage_copied
= 0;
2381 if (!c
|| !c
->is_defined(c
))
2384 if (container_mem_lock(c
))
2387 if (!is_stopped(c
)) {
2388 ERROR("error: Original container (%s) is running", c
->name
);
2392 // Make sure the container doesn't yet exist.
2393 n
= newname
? newname
: c
->name
;
2394 l
= lxcpath
? lxcpath
: c
->get_config_path(c
);
2395 ret
= snprintf(newpath
, MAXPATHLEN
, "%s/%s/config", l
, n
);
2396 if (ret
< 0 || ret
>= MAXPATHLEN
) {
2397 SYSERROR("clone: failed making config pathname");
2400 if (file_exists(newpath
)) {
2401 ERROR("error: clone: %s exists", newpath
);
2405 ret
= create_file_dirname(newpath
);
2406 if (ret
< 0 && errno
!= EEXIST
) {
2407 ERROR("Error creating container dir for %s", newpath
);
2411 // copy the configuration, tweak it as needed,
2413 fout
= fopen(newpath
, "w");
2416 SYSERROR("open %s", newpath
);
2419 write_config(fout
, c
->lxc_conf
);
2424 sprintf(newpath
, "%s/%s/rootfs", l
, n
);
2425 if (mkdir(newpath
, 0755) < 0) {
2426 SYSERROR("error creating %s", newpath
);
2430 c2
= lxc_container_new(n
, l
);
2432 ERROR("clone: failed to create new container (%s %s)", n
, l
);
2437 if (!set_config_item_locked(c2
, "lxc.utsname", newname
)) {
2438 ERROR("Error setting new hostname");
2443 // copy hooks if requested
2444 if (flags
& LXC_CLONE_COPYHOOKS
) {
2445 ret
= copyhooks(c
, c2
);
2447 ERROR("error copying hooks");
2452 if (copy_fstab(c
, c2
) < 0) {
2453 ERROR("error copying fstab");
2458 if (!(flags
& LXC_CLONE_KEEPMACADDR
))
2459 network_new_hwaddrs(c2
);
2461 // copy/snapshot rootfs's
2462 ret
= copy_storage(c
, c2
, bdevtype
, flags
, bdevdata
, newsize
);
2466 // We've now successfully created c2's storage, so clear it out if we
2470 if (!c2
->save_config(c2
, NULL
))
2473 if (clone_update_rootfs(c
, c2
, flags
, hookargs
) < 0)
2476 // TODO: update c's lxc.snapshot = count
2477 container_mem_unlock(c
);
2481 container_mem_unlock(c
);
2483 if (!storage_copied
)
2484 c2
->lxc_conf
->rootfs
.path
= NULL
;
2486 lxc_container_put(c2
);
2492 static int lxcapi_attach(struct lxc_container
*c
, lxc_attach_exec_t exec_function
, void *exec_payload
, lxc_attach_options_t
*options
, pid_t
*attached_process
)
2497 return lxc_attach(c
->name
, c
->config_path
, exec_function
, exec_payload
, options
, attached_process
);
2500 static int lxcapi_attach_run_wait(struct lxc_container
*c
, lxc_attach_options_t
*options
, const char *program
, const char * const argv
[])
2502 lxc_attach_command_t command
;
2509 command
.program
= (char*)program
;
2510 command
.argv
= (char**)argv
;
2511 r
= lxc_attach(c
->name
, c
->config_path
, lxc_attach_run_command
, &command
, options
, &pid
);
2516 return lxc_wait_for_pid_status(pid
);
2519 int get_next_index(const char *lxcpath
, char *cname
)
2525 fname
= alloca(strlen(lxcpath
) + 20);
2527 sprintf(fname
, "%s/snap%d", lxcpath
, i
);
2528 ret
= stat(fname
, &sb
);
2535 static int lxcapi_snapshot(struct lxc_container
*c
, char *commentfile
)
2538 struct lxc_container
*c2
;
2539 char snappath
[MAXPATHLEN
], newname
[20];
2541 // /var/lib/lxc -> /var/lib/lxcsnaps \0
2542 ret
= snprintf(snappath
, MAXPATHLEN
, "%ssnaps/%s", c
->config_path
, c
->name
);
2543 if (ret
< 0 || ret
>= MAXPATHLEN
)
2545 i
= get_next_index(snappath
, c
->name
);
2547 if (mkdir_p(snappath
, 0755) < 0) {
2548 ERROR("Failed to create snapshot directory %s", snappath
);
2552 ret
= snprintf(newname
, 20, "snap%d", i
);
2553 if (ret
< 0 || ret
>= 20)
2556 flags
= LXC_CLONE_SNAPSHOT
| LXC_CLONE_KEEPMACADDR
| LXC_CLONE_KEEPNAME
;
2557 c2
= c
->clone(c
, newname
, snappath
, flags
, NULL
, NULL
, 0, NULL
);
2559 ERROR("clone of %s:%s failed\n", c
->config_path
, c
->name
);
2563 lxc_container_put(c2
);
2565 // Now write down the creation time
2572 tm_info
= localtime(&timer
);
2574 strftime(buffer
, 25, "%Y:%m:%d %H:%M:%S", tm_info
);
2576 char *dfnam
= alloca(strlen(snappath
) + strlen(newname
) + 5);
2577 sprintf(dfnam
, "%s/%s/ts", snappath
, newname
);
2579 f
= fopen(dfnam
, "w");
2582 ERROR("Failed to open %s\n", dfnam
);
2585 if (fprintf(f
, "%s", buffer
) < 0) {
2586 SYSERROR("Writing timestamp");
2594 SYSERROR("Writing timestamp");
2599 // $p / $name / comment \0
2600 int len
= strlen(snappath
) + strlen(newname
) + 10;
2601 char *path
= alloca(len
);
2602 sprintf(path
, "%s/%s/comment", snappath
, newname
);
2603 return copy_file(commentfile
, path
) < 0 ? -1 : i
;
2609 static void lxcsnap_free(struct lxc_snapshot
*s
)
2613 if (s
->comment_pathname
)
2614 free(s
->comment_pathname
);
2621 static char *get_snapcomment_path(char* snappath
, char *name
)
2623 // $snappath/$name/comment
2624 int ret
, len
= strlen(snappath
) + strlen(name
) + 10;
2625 char *s
= malloc(len
);
2628 ret
= snprintf(s
, len
, "%s/%s/comment", snappath
, name
);
2629 if (ret
< 0 || ret
>= len
) {
2637 static char *get_timestamp(char* snappath
, char *name
)
2639 char path
[MAXPATHLEN
], *s
= NULL
;
2643 ret
= snprintf(path
, MAXPATHLEN
, "%s/%s/ts", snappath
, name
);
2644 if (ret
< 0 || ret
>= MAXPATHLEN
)
2647 fin
= fopen(path
, "r");
2651 (void) fseek(fin
, 0, SEEK_END
);
2653 (void) fseek(fin
, 0, SEEK_SET
);
2658 if (fread(s
, 1, len
, fin
) != len
) {
2659 SYSERROR("reading timestamp");
2671 static int lxcapi_snapshot_list(struct lxc_container
*c
, struct lxc_snapshot
**ret_snaps
)
2673 char snappath
[MAXPATHLEN
], path2
[MAXPATHLEN
];
2674 int dirlen
, count
= 0, ret
;
2675 struct dirent dirent
, *direntp
;
2676 struct lxc_snapshot
*snaps
=NULL
, *nsnaps
;
2679 if (!c
|| !lxcapi_is_defined(c
))
2681 // snappath is ${lxcpath}snaps/${lxcname}/
2682 dirlen
= snprintf(snappath
, MAXPATHLEN
, "%ssnaps/%s", c
->config_path
, c
->name
);
2683 if (dirlen
< 0 || dirlen
>= MAXPATHLEN
) {
2684 ERROR("path name too long");
2688 dir
= opendir(snappath
);
2691 INFO("failed to open %s - assuming no snapshots", snappath
);
2695 while (!readdir_r(dir
, &dirent
, &direntp
)) {
2699 if (!strcmp(direntp
->d_name
, "."))
2702 if (!strcmp(direntp
->d_name
, ".."))
2705 ret
= snprintf(path2
, MAXPATHLEN
, "%s/%s/config", snappath
, direntp
->d_name
);
2706 if (ret
< 0 || ret
>= MAXPATHLEN
) {
2707 ERROR("pathname too long");
2710 if (!file_exists(path2
))
2712 nsnaps
= realloc(snaps
, (count
+ 1)*sizeof(*snaps
));
2714 SYSERROR("Out of memory");
2718 snaps
[count
].free
= lxcsnap_free
;
2719 snaps
[count
].name
= strdup(direntp
->d_name
);
2720 if (!snaps
[count
].name
)
2722 snaps
[count
].lxcpath
= strdup(snappath
);
2723 if (!snaps
[count
].lxcpath
) {
2724 free(snaps
[count
].name
);
2727 snaps
[count
].comment_pathname
= get_snapcomment_path(snappath
, direntp
->d_name
);
2728 snaps
[count
].timestamp
= get_timestamp(snappath
, direntp
->d_name
);
2734 WARN("failed to close directory");
2743 for (i
=0; i
<count
; i
++)
2744 lxcsnap_free(&snaps
[i
]);
2749 WARN("failed to close directory");
2754 static bool lxcapi_snapshot_restore(struct lxc_container
*c
, char *snapname
, char *newname
)
2756 char clonelxcpath
[MAXPATHLEN
];
2758 struct lxc_container
*snap
, *rest
;
2762 if (!c
|| !c
->name
|| !c
->config_path
)
2765 bdev
= bdev_init(c
->lxc_conf
->rootfs
.path
, c
->lxc_conf
->rootfs
.mount
, NULL
);
2767 ERROR("Failed to find original backing store type");
2773 if (strcmp(c
->name
, newname
) == 0) {
2774 if (!lxcapi_destroy(c
)) {
2775 ERROR("Could not destroy existing container %s", newname
);
2780 ret
= snprintf(clonelxcpath
, MAXPATHLEN
, "%ssnaps/%s", c
->config_path
, c
->name
);
2781 if (ret
< 0 || ret
>= MAXPATHLEN
) {
2785 // how should we lock this?
2787 snap
= lxc_container_new(snapname
, clonelxcpath
);
2788 if (!snap
|| !lxcapi_is_defined(snap
)) {
2789 ERROR("Could not open snapshot %s", snapname
);
2790 if (snap
) lxc_container_put(snap
);
2795 rest
= lxcapi_clone(snap
, newname
, c
->config_path
, 0, bdev
->type
, NULL
, 0, NULL
);
2797 if (rest
&& lxcapi_is_defined(rest
))
2800 lxc_container_put(rest
);
2801 lxc_container_put(snap
);
2805 static bool lxcapi_snapshot_destroy(struct lxc_container
*c
, char *snapname
)
2808 char clonelxcpath
[MAXPATHLEN
];
2809 struct lxc_container
*snap
= NULL
;
2811 if (!c
|| !c
->name
|| !c
->config_path
)
2814 ret
= snprintf(clonelxcpath
, MAXPATHLEN
, "%ssnaps/%s", c
->config_path
, c
->name
);
2815 if (ret
< 0 || ret
>= MAXPATHLEN
)
2818 snap
= lxc_container_new(snapname
, clonelxcpath
);
2819 if (!snap
|| !lxcapi_is_defined(snap
)) {
2820 ERROR("Could not find snapshot %s", snapname
);
2824 if (!lxcapi_destroy(snap
)) {
2825 ERROR("Could not destroy snapshot %s", snapname
);
2828 lxc_container_put(snap
);
2833 lxc_container_put(snap
);
2837 static bool lxcapi_may_control(struct lxc_container
*c
)
2839 return lxc_try_cmd(c
->name
, c
->config_path
) == 0;
2842 static int lxcapi_attach_run_waitl(struct lxc_container
*c
, lxc_attach_options_t
*options
, const char *program
, const char *arg
, ...)
2852 argv
= lxc_va_arg_list_to_argv_const(ap
, 1);
2856 ERROR("Memory allocation error.");
2861 ret
= lxcapi_attach_run_wait(c
, options
, program
, (const char * const *)argv
);
2866 struct lxc_container
*lxc_container_new(const char *name
, const char *configpath
)
2868 struct lxc_container
*c
;
2870 c
= malloc(sizeof(*c
));
2872 fprintf(stderr
, "failed to malloc lxc_container\n");
2875 memset(c
, 0, sizeof(*c
));
2878 c
->config_path
= strdup(configpath
);
2880 c
->config_path
= strdup(default_lxc_path());
2882 if (!c
->config_path
) {
2883 fprintf(stderr
, "Out of memory");
2887 remove_trailing_slashes(c
->config_path
);
2888 c
->name
= malloc(strlen(name
)+1);
2890 fprintf(stderr
, "Error allocating lxc_container name\n");
2893 strcpy(c
->name
, name
);
2896 if (!(c
->slock
= lxc_newlock(c
->config_path
, name
))) {
2897 fprintf(stderr
, "failed to create lock\n");
2901 if (!(c
->privlock
= lxc_newlock(NULL
, NULL
))) {
2902 fprintf(stderr
, "failed to alloc privlock\n");
2906 if (!set_config_filename(c
)) {
2907 fprintf(stderr
, "Error allocating config file pathname\n");
2911 if (file_exists(c
->configfile
))
2912 lxcapi_load_config(c
, NULL
);
2914 if (ongoing_create(c
) == 2) {
2915 ERROR("Error: %s creation was not completed", c
->name
);
2917 lxc_conf_free(c
->lxc_conf
);
2921 // assign the member functions
2922 c
->is_defined
= lxcapi_is_defined
;
2923 c
->state
= lxcapi_state
;
2924 c
->is_running
= lxcapi_is_running
;
2925 c
->freeze
= lxcapi_freeze
;
2926 c
->unfreeze
= lxcapi_unfreeze
;
2927 c
->console
= lxcapi_console
;
2928 c
->console_getfd
= lxcapi_console_getfd
;
2929 c
->init_pid
= lxcapi_init_pid
;
2930 c
->load_config
= lxcapi_load_config
;
2931 c
->want_daemonize
= lxcapi_want_daemonize
;
2932 c
->want_close_all_fds
= lxcapi_want_close_all_fds
;
2933 c
->start
= lxcapi_start
;
2934 c
->startl
= lxcapi_startl
;
2935 c
->stop
= lxcapi_stop
;
2936 c
->config_file_name
= lxcapi_config_file_name
;
2937 c
->wait
= lxcapi_wait
;
2938 c
->set_config_item
= lxcapi_set_config_item
;
2939 c
->destroy
= lxcapi_destroy
;
2940 c
->save_config
= lxcapi_save_config
;
2941 c
->get_keys
= lxcapi_get_keys
;
2942 c
->create
= lxcapi_create
;
2943 c
->createl
= lxcapi_createl
;
2944 c
->shutdown
= lxcapi_shutdown
;
2945 c
->reboot
= lxcapi_reboot
;
2946 c
->clear_config_item
= lxcapi_clear_config_item
;
2947 c
->get_config_item
= lxcapi_get_config_item
;
2948 c
->get_cgroup_item
= lxcapi_get_cgroup_item
;
2949 c
->set_cgroup_item
= lxcapi_set_cgroup_item
;
2950 c
->get_config_path
= lxcapi_get_config_path
;
2951 c
->set_config_path
= lxcapi_set_config_path
;
2952 c
->clone
= lxcapi_clone
;
2953 c
->get_interfaces
= lxcapi_get_interfaces
;
2954 c
->get_ips
= lxcapi_get_ips
;
2955 c
->attach
= lxcapi_attach
;
2956 c
->attach_run_wait
= lxcapi_attach_run_wait
;
2957 c
->attach_run_waitl
= lxcapi_attach_run_waitl
;
2958 c
->snapshot
= lxcapi_snapshot
;
2959 c
->snapshot_list
= lxcapi_snapshot_list
;
2960 c
->snapshot_restore
= lxcapi_snapshot_restore
;
2961 c
->snapshot_destroy
= lxcapi_snapshot_destroy
;
2962 c
->may_control
= lxcapi_may_control
;
2964 /* we'll allow the caller to update these later */
2965 if (lxc_log_init(NULL
, "none", NULL
, "lxc_container", 0, c
->config_path
)) {
2966 fprintf(stderr
, "failed to open log\n");
2973 lxc_container_free(c
);
2977 int lxc_get_wait_states(const char **states
)
2982 for (i
=0; i
<MAX_STATE
; i
++)
2983 states
[i
] = lxc_state2str(i
);
2988 * These next two could probably be done smarter with reusing a common function
2989 * with different iterators and tests...
2991 int list_defined_containers(const char *lxcpath
, char ***names
, struct lxc_container
***cret
)
2994 int i
, cfound
= 0, nfound
= 0;
2995 struct dirent dirent
, *direntp
;
2996 struct lxc_container
*c
;
2999 lxcpath
= default_lxc_path();
3002 dir
= opendir(lxcpath
);
3006 SYSERROR("opendir on lxcpath");
3015 while (!readdir_r(dir
, &dirent
, &direntp
)) {
3018 if (!strcmp(direntp
->d_name
, "."))
3020 if (!strcmp(direntp
->d_name
, ".."))
3023 if (!config_file_exists(lxcpath
, direntp
->d_name
))
3027 if (!add_to_array(names
, direntp
->d_name
, cfound
))
3037 c
= lxc_container_new(direntp
->d_name
, lxcpath
);
3039 INFO("Container %s:%s has a config but could not be loaded",
3040 lxcpath
, direntp
->d_name
);
3042 if(!remove_from_array(names
, direntp
->d_name
, cfound
--))
3046 if (!lxcapi_is_defined(c
)) {
3047 INFO("Container %s:%s has a config but is not defined",
3048 lxcpath
, direntp
->d_name
);
3050 if(!remove_from_array(names
, direntp
->d_name
, cfound
--))
3052 lxc_container_put(c
);
3056 if (!add_to_clist(cret
, c
, nfound
)) {
3057 lxc_container_put(c
);
3069 if (names
&& *names
) {
3070 for (i
=0; i
<cfound
; i
++)
3074 if (cret
&& *cret
) {
3075 for (i
=0; i
<nfound
; i
++)
3076 lxc_container_put((*cret
)[i
]);
3085 int list_active_containers(const char *lxcpath
, char ***names
, struct lxc_container
***cret
)
3087 int i
, cfound
= 0, nfound
= 0;
3090 char **unique_names
= NULL
;
3092 struct lxc_container
*c
;
3095 lxcpath
= default_lxc_path();
3096 lxcpath_len
= strlen(lxcpath
);
3104 FILE *f
= fopen("/proc/net/unix", "r");
3109 while (getline(&line
, &len
, f
) != -1) {
3110 char *p
= strrchr(line
, ' '), *p2
;
3117 if (strncmp(p
, lxcpath
, lxcpath_len
) != 0)
3123 // Now p is the start of lxc_name
3125 if (!p2
|| strncmp(p2
, "/command", 8) != 0)
3129 if (array_contains(&unique_names
, p
, nfound
))
3132 if (!add_to_array(&unique_names
, p
, nfound
))
3142 c
= lxc_container_new(p
, lxcpath
);
3144 INFO("Container %s:%s is running but could not be loaded",
3147 if(!remove_from_array(&unique_names
, p
, cfound
--))
3154 * If this is an anonymous container, then is_defined *can*
3155 * return false. So we don't do that check. Count on the
3156 * fact that the command socket exists.
3159 if (!add_to_clist(cret
, c
, nfound
)) {
3160 lxc_container_put(c
);
3167 *names
= unique_names
;
3178 if (names
&& *names
) {
3179 for (i
=0; i
<cfound
; i
++)
3183 if (cret
&& *cret
) {
3184 for (i
=0; i
<nfound
; i
++)
3185 lxc_container_put((*cret
)[i
]);