]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxccontainer.c
3 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2012 Canonical Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <sys/types.h>
29 #include "lxccontainer.h"
37 #include <lxc/utils.h>
38 #include <lxc/monitor.h>
40 /* Define unshare() if missing from the C library */
41 /* this is also in attach.c and lxccontainer.c: commonize it in utils.c */
43 static int unshare(int flags
)
46 return syscall(__NR_unshare
, flags
);
56 lxc_log_define(lxc_container
, lxc
);
59 * c->privlock protects the struct lxc_container from multiple threads.
60 * c->slock protects the on-disk container data
61 * NOTHING mutexes two independent programs with their own struct
62 * lxc_container for the same c->name, between API calls. For instance,
63 * c->config_read(); c->start(); Between those calls, data on disk
64 * could change (which shouldn't bother the caller unless for instance
65 * the rootfs get moved). c->config_read(); update; c->config_write();
66 * Two such updaters could race. The callers should therefore check their
67 * results. Trying to prevent that would necessarily expose us to deadlocks
68 * due to hung callers. So I prefer to keep the locks only within our own
69 * functions, not across functions.
71 * If you're going to fork while holding a lxccontainer, increment
72 * c->numthreads (under privlock) before forking. When deleting,
73 * decrement numthreads under privlock, then if it hits 0 you can delete.
74 * Do not ever use a lxccontainer whose numthreads you did not bump.
77 static void lxc_container_free(struct lxc_container
*c
)
86 if (c
->error_string
) {
87 free(c
->error_string
);
88 c
->error_string
= NULL
;
95 sem_t
*l
= c
->privlock
;
105 lxc_conf_free(c
->lxc_conf
);
108 if (c
->config_path
) {
109 free(c
->config_path
);
110 c
->config_path
= NULL
;
116 * Consider the following case:
117 freer | racing get()er
118 ==================================================================
119 lxc_container_put() | lxc_container_get()
120 \ lxclock(c->privlock) | c->numthreads < 1? (no)
121 \ c->numthreads = 0 | \ lxclock(c->privlock) -> waits
123 \ lxc_container_free() | \ lxclock() returns
124 | \ c->numthreads < 1 -> return 0
126 \ \ sem_destroy(privlock) |
128 * When the get()er checks numthreads the first time, one of the following
130 * 1. freer has set numthreads = 0. get() returns 0
131 * 2. freer is between lxclock and setting numthreads to 0. get()er will
132 * sem_wait on privlock, get lxclock after freer() drops it, then see
133 * numthreads is 0 and exit without touching lxclock again..
134 * 3. freer has not yet locked privlock. If get()er runs first, then put()er
135 * will see --numthreads = 1 and not call lxc_container_free().
138 int lxc_container_get(struct lxc_container
*c
)
143 // if someone else has already started freeing the container, don't
144 // try to take the lock, which may be invalid
145 if (c
->numthreads
< 1)
148 if (lxclock(c
->privlock
, 0))
150 if (c
->numthreads
< 1) {
151 // bail without trying to unlock, bc the privlock is now probably
156 lxcunlock(c
->privlock
);
160 int lxc_container_put(struct lxc_container
*c
)
164 if (lxclock(c
->privlock
, 0))
166 if (--c
->numthreads
< 1) {
167 lxcunlock(c
->privlock
);
168 lxc_container_free(c
);
171 lxcunlock(c
->privlock
);
175 static bool file_exists(char *f
)
179 return stat(f
, &statbuf
) == 0;
182 static bool lxcapi_is_defined(struct lxc_container
*c
)
191 if (lxclock(c
->privlock
, 0))
195 statret
= stat(c
->configfile
, &statbuf
);
201 lxcunlock(c
->privlock
);
205 static const char *lxcapi_state(struct lxc_container
*c
)
212 if (lxclock(c
->slock
, 0))
214 s
= lxc_getstate(c
->name
, c
->config_path
);
215 ret
= lxc_state2str(s
);
221 static bool is_stopped_nolock(struct lxc_container
*c
)
224 s
= lxc_getstate(c
->name
, c
->config_path
);
225 return (s
== STOPPED
);
228 static bool lxcapi_is_running(struct lxc_container
*c
)
235 if (!s
|| strcmp(s
, "STOPPED") == 0)
240 static bool lxcapi_freeze(struct lxc_container
*c
)
246 if (lxclock(c
->slock
, 0))
248 ret
= lxc_freeze(c
->name
, c
->config_path
);
255 static bool lxcapi_unfreeze(struct lxc_container
*c
)
261 if (lxclock(c
->slock
, 0))
263 ret
= lxc_unfreeze(c
->name
, c
->config_path
);
270 static pid_t
lxcapi_init_pid(struct lxc_container
*c
)
276 if (lxclock(c
->slock
, 0))
278 ret
= get_init_pid(c
->name
, c
->config_path
);
283 static bool load_config_locked(struct lxc_container
*c
, const char *fname
)
286 c
->lxc_conf
= lxc_conf_init();
287 if (c
->lxc_conf
&& !lxc_config_read(fname
, c
->lxc_conf
))
292 static bool lxcapi_load_config(struct lxc_container
*c
, const char *alt_file
)
299 fname
= c
->configfile
;
304 if (lxclock(c
->slock
, 0))
306 ret
= load_config_locked(c
, fname
);
311 static void lxcapi_want_daemonize(struct lxc_container
*c
)
318 static bool lxcapi_wait(struct lxc_container
*c
, const char *state
, int timeout
)
325 ret
= lxc_wait(c
->name
, state
, timeout
, c
->config_path
);
330 static bool wait_on_daemonized_start(struct lxc_container
*c
)
332 /* we'll probably want to make this timeout configurable? */
333 int timeout
= 5, ret
, status
;
336 * our child is going to fork again, then exit. reap the
340 if (ret
== -1 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
341 DEBUG("failed waiting for first dual-fork child");
342 return lxcapi_wait(c
, "RUNNING", timeout
);
346 * I can't decide if it'd be more convenient for callers if we accept '...',
347 * or a null-terminated array (i.e. execl vs execv)
349 static bool lxcapi_start(struct lxc_container
*c
, int useinit
, char * const argv
[])
352 struct lxc_conf
*conf
;
354 char *default_args
[] = {
359 /* container exists */
362 /* container has been setup */
366 /* is this app meant to be run through lxcinit, as in lxc-execute? */
367 if (useinit
&& !argv
)
370 if (lxclock(c
->privlock
, 0))
373 daemonize
= c
->daemonize
;
374 lxcunlock(c
->privlock
);
377 ret
= lxc_execute(c
->name
, argv
, 1, conf
, c
->config_path
);
378 return ret
== 0 ? true : false;
385 * say, I'm not sure - what locks do we want here? Any?
386 * Is liblxc's locking enough here to protect the on disk
387 * container? We don't want to exclude things like lxc_info
388 * while container is running...
391 if (!lxc_container_get(c
))
393 lxc_monitord_spawn(c
->config_path
);
396 lxc_container_put(c
);
400 return wait_on_daemonized_start(c
);
401 /* second fork to be reparented by init */
404 SYSERROR("Error doing dual-fork");
409 /* like daemon(), chdir to / and redirect 0,1,2 to /dev/null */
411 SYSERROR("Error chdir()ing to /.");
417 open("/dev/null", O_RDONLY
);
418 open("/dev/null", O_RDWR
);
419 open("/dev/null", O_RDWR
);
425 ret
= lxc_start(c
->name
, argv
, conf
, c
->config_path
);
428 INFO("container requested reboot");
434 lxc_container_put(c
);
435 exit (ret
== 0 ? true : false);
437 return (ret
== 0 ? true : false);
442 * note there MUST be an ending NULL
444 static bool lxcapi_startl(struct lxc_container
*c
, int useinit
, ...)
447 char **inargs
= NULL
, **temp
;
451 /* container exists */
455 /* build array of arguments if any */
456 va_start(ap
, useinit
);
459 arg
= va_arg(ap
, char *);
463 temp
= realloc(inargs
, n_inargs
* sizeof(*inargs
));
469 inargs
[n_inargs
- 1] = strdup(arg
); // not sure if it's safe not to copy
473 /* add trailing NULL */
476 temp
= realloc(inargs
, n_inargs
* sizeof(*inargs
));
480 inargs
[n_inargs
- 1] = NULL
;
483 bret
= lxcapi_start(c
, useinit
, inargs
);
488 for (i
= 0; i
< n_inargs
; i
++) {
498 static bool lxcapi_stop(struct lxc_container
*c
)
505 ret
= lxc_stop(c
->name
, c
->config_path
);
510 static bool valid_template(char *t
)
515 statret
= stat(t
, &statbuf
);
522 * create the standard expected container dir
524 static bool create_container_dir(struct lxc_container
*c
)
529 len
= strlen(c
->config_path
) + strlen(c
->name
) + 2;
533 ret
= snprintf(s
, len
, "%s/%s", c
->config_path
, c
->name
);
534 if (ret
< 0 || ret
>= len
) {
538 ret
= mkdir(s
, 0755);
543 SYSERROR("failed to create container path for %s\n", c
->name
);
550 * backing stores not (yet) supported
551 * for ->create, argv contains the arguments to pass to the template,
552 * terminated by NULL. If no arguments, you can just pass NULL.
554 static bool lxcapi_create(struct lxc_container
*c
, char *t
, char *const argv
[])
558 char *tpath
= NULL
, **newargv
;
559 int ret
, len
, nargs
= 0;
564 len
= strlen(LXCTEMPLATEDIR
) + strlen(t
) + strlen("/lxc-") + 1;
568 ret
= snprintf(tpath
, len
, "%s/lxc-%s", LXCTEMPLATEDIR
, t
);
569 if (ret
< 0 || ret
>= len
)
571 if (!valid_template(tpath
)) {
572 ERROR("bad template: %s\n", t
);
576 if (!c
->save_config(c
, NULL
)) {
577 ERROR("failed to save starting configuration for %s\n", c
->name
);
581 /* container is already created if we have a config and rootfs.path is accessible */
582 if (lxcapi_is_defined(c
) && c
->lxc_conf
&& c
->lxc_conf
->rootfs
.path
&& access(c
->lxc_conf
->rootfs
.path
, F_OK
) == 0)
585 /* we're going to fork. but since we'll wait for our child, we
586 * don't need to lxc_container_get */
588 if (lxclock(c
->slock
, 0)) {
589 ERROR("failed to grab global container lock for %s\n", c
->name
);
595 SYSERROR("failed to fork task for container creation template\n");
599 if (pid
== 0) { // child
600 char *patharg
, *namearg
;
606 open("/dev/null", O_RDONLY
);
607 open("/dev/null", O_RDWR
);
608 open("/dev/null", O_RDWR
);
611 * create our new array, pre-pend the template name and
615 for (; argv
[nargs
]; nargs
++) ;
616 nargs
+= 3; // template, path and name args
617 newargv
= malloc(nargs
* sizeof(*newargv
));
622 len
= strlen(c
->config_path
) + strlen(c
->name
) + strlen("--path=") + 2;
623 patharg
= malloc(len
);
626 ret
= snprintf(patharg
, len
, "--path=%s/%s", c
->config_path
, c
->name
);
627 if (ret
< 0 || ret
>= len
)
629 newargv
[1] = patharg
;
630 len
= strlen("--name=") + strlen(c
->name
) + 1;
631 namearg
= malloc(len
);
634 ret
= snprintf(namearg
, len
, "--name=%s", c
->name
);
635 if (ret
< 0 || ret
>= len
)
637 newargv
[2] = namearg
;
639 /* add passed-in args */
641 for (i
= 3; i
< nargs
; i
++)
642 newargv
[i
] = argv
[i
-3];
644 /* add trailing NULL */
646 newargv
= realloc(newargv
, nargs
* sizeof(*newargv
));
649 newargv
[nargs
- 1] = NULL
;
652 execv(tpath
, newargv
);
653 SYSERROR("failed to execute template %s", tpath
);
657 if (wait_for_pid(pid
) != 0) {
658 ERROR("container creation template for %s failed\n", c
->name
);
662 // now clear out the lxc_conf we have, reload from the created
665 lxc_conf_free(c
->lxc_conf
);
667 bret
= load_config_locked(c
, c
->configfile
);
677 static bool lxcapi_shutdown(struct lxc_container
*c
, int timeout
)
687 if (!c
->is_running(c
))
689 pid
= c
->init_pid(c
);
693 retv
= c
->wait(c
, "STOPPED", timeout
);
694 if (!retv
&& timeout
> 0) {
696 retv
= c
->wait(c
, "STOPPED", 0); // 0 means don't wait
701 static bool lxcapi_createl(struct lxc_container
*c
, char *t
, ...)
704 char **args
= NULL
, **temp
;
712 * since we're going to wait for create to finish, I don't think we
713 * need to get a copy of the arguments.
718 arg
= va_arg(ap
, char *);
722 temp
= realloc(args
, (nargs
+1) * sizeof(*args
));
728 args
[nargs
- 1] = arg
;
734 bret
= c
->create(c
, t
, args
);
742 static bool lxcapi_clear_config_item(struct lxc_container
*c
, const char *key
)
746 if (!c
|| !c
->lxc_conf
)
748 if (lxclock(c
->privlock
, 0)) {
751 ret
= lxc_clear_config_item(c
->lxc_conf
, key
);
752 lxcunlock(c
->privlock
);
756 static int lxcapi_get_config_item(struct lxc_container
*c
, const char *key
, char *retv
, int inlen
)
760 if (!c
|| !c
->lxc_conf
)
762 if (lxclock(c
->privlock
, 0)) {
765 ret
= lxc_get_config_item(c
->lxc_conf
, key
, retv
, inlen
);
766 lxcunlock(c
->privlock
);
770 static int lxcapi_get_keys(struct lxc_container
*c
, const char *key
, char *retv
, int inlen
)
773 return lxc_listconfigs(retv
, inlen
);
775 * Support 'lxc.network.<idx>', i.e. 'lxc.network.0'
776 * This is an intelligent result to show which keys are valid given
777 * the type of nic it is
779 if (!c
|| !c
->lxc_conf
)
781 if (lxclock(c
->privlock
, 0))
784 if (strncmp(key
, "lxc.network.", 12) == 0)
785 ret
= lxc_list_nicconfigs(c
->lxc_conf
, key
, retv
, inlen
);
786 lxcunlock(c
->privlock
);
791 /* default config file - should probably come through autoconf */
792 #define LXC_DEFAULT_CONFIG "/etc/lxc/default.conf"
793 static bool lxcapi_save_config(struct lxc_container
*c
, const char *alt_file
)
796 alt_file
= c
->configfile
;
798 return false; // should we write to stdout if no file is specified?
800 if (!c
->load_config(c
, LXC_DEFAULT_CONFIG
)) {
801 ERROR("Error loading default configuration file %s while saving %s\n", LXC_DEFAULT_CONFIG
, c
->name
);
805 if (!create_container_dir(c
))
808 FILE *fout
= fopen(alt_file
, "w");
811 if (lxclock(c
->privlock
, 0)) {
815 write_config(fout
, c
->lxc_conf
);
817 lxcunlock(c
->privlock
);
821 static bool lxcapi_destroy(struct lxc_container
*c
)
828 /* container is already destroyed if we don't have a config and rootfs.path is not accessible */
829 if (!lxcapi_is_defined(c
) && (!c
->lxc_conf
|| !c
->lxc_conf
->rootfs
.path
|| access(c
->lxc_conf
->rootfs
.path
, F_OK
) != 0))
835 if (pid
== 0) { // child
836 execlp("lxc-destroy", "lxc-destroy", "-n", c
->name
, "-P", c
->config_path
, NULL
);
841 if (wait_for_pid(pid
) < 0) {
842 ERROR("Error destroying container %s", c
->name
);
849 static bool lxcapi_set_config_item(struct lxc_container
*c
, const char *key
, const char *v
)
853 struct lxc_config_t
*config
;
858 if (lxclock(c
->privlock
, 0))
862 c
->lxc_conf
= lxc_conf_init();
865 config
= lxc_getconfig(key
);
868 ret
= config
->cb(key
, v
, c
->lxc_conf
);
873 lxcunlock(c
->privlock
);
877 static char *lxcapi_config_file_name(struct lxc_container
*c
)
879 if (!c
|| !c
->configfile
)
881 return strdup(c
->configfile
);
884 static const char *lxcapi_get_config_path(struct lxc_container
*c
)
886 if (!c
|| !c
->config_path
)
888 return (const char *)(c
->config_path
);
893 * Just recalculate the c->configfile based on the
894 * c->config_path, which must be set.
895 * The lxc_container must be locked or not yet public.
897 static bool set_config_filename(struct lxc_container
*c
)
905 /* $lxc_path + "/" + c->name + "/" + "config" + '\0' */
906 len
= strlen(c
->config_path
) + strlen(c
->name
) + strlen("config") + 3;
907 newpath
= malloc(len
);
911 ret
= snprintf(newpath
, len
, "%s/%s/config", c
->config_path
, c
->name
);
912 if (ret
< 0 || ret
>= len
) {
913 fprintf(stderr
, "Error printing out config file name\n");
920 c
->configfile
= newpath
;
925 static bool lxcapi_set_config_path(struct lxc_container
*c
, const char *path
)
929 char *oldpath
= NULL
;
934 if (lxclock(c
->privlock
, 0))
939 ERROR("Out of memory setting new lxc path");
945 oldpath
= c
->config_path
;
948 /* Since we've changed the config path, we have to change the
949 * config file name too */
950 if (!set_config_filename(c
)) {
951 ERROR("Out of memory setting new config filename");
953 free(c
->config_path
);
954 c
->config_path
= oldpath
;
960 lxcunlock(c
->privlock
);
965 static bool lxcapi_set_cgroup_item(struct lxc_container
*c
, const char *subsys
, const char *value
)
973 if (lxclock(c
->privlock
, 0))
976 if (is_stopped_nolock(c
))
979 ret
= lxc_cgroup_set(c
->name
, subsys
, value
, c
->config_path
);
983 lxcunlock(c
->privlock
);
987 static int lxcapi_get_cgroup_item(struct lxc_container
*c
, const char *subsys
, char *retv
, int inlen
)
991 if (!c
|| !c
->lxc_conf
)
994 if (lxclock(c
->privlock
, 0))
997 if (is_stopped_nolock(c
))
1000 ret
= lxc_cgroup_get(c
->name
, subsys
, retv
, inlen
, c
->config_path
);
1003 lxcunlock(c
->privlock
);
1007 const char *lxc_get_default_config_path(void)
1009 return default_lxc_path();
1012 const char *lxc_get_default_lvm_vg(void)
1014 return default_lvm_vg();
1017 const char *lxc_get_default_zfs_root(void)
1019 return default_zfs_root();
1022 const char *lxc_get_version(void)
1024 return lxc_version();
1027 static int copy_file(char *old
, char *new)
1034 if (file_exists(new)) {
1035 ERROR("copy destination %s exists", new);
1038 ret
= stat(old
, &sbuf
);
1040 SYSERROR("stat'ing %s", old
);
1044 in
= open(old
, O_RDONLY
);
1046 SYSERROR("opening original file %s", old
);
1049 out
= open(new, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
1051 SYSERROR("opening new file %s", new);
1057 len
= read(in
, buf
, 8096);
1059 SYSERROR("reading old file %s", old
);
1064 ret
= write(out
, buf
, len
);
1065 if (ret
< len
) { // should we retry?
1066 SYSERROR("write to new file %s was interrupted", new);
1073 // we set mode, but not owner/group
1074 ret
= chmod(new, sbuf
.st_mode
);
1076 SYSERROR("setting mode on %s", new);
1089 * we're being passed result of two strstrs(x, y). We want to write
1090 * all data up to the first found string, or to end of the string if
1091 * neither string was found.
1092 * This function will return the earliest found string if any, or else
1095 static const char *lowest_nonnull(const char *p1
, const char *p2
)
1101 return p1
< p2
? p1
: p2
;
1104 static int is_word_sep(char c
)
1119 static const char *find_first_wholeword(const char *p
, const char *word
)
1124 while ((p
= strstr(p
, word
)) != NULL
) {
1125 if (is_word_sep(*(p
-1)) && is_word_sep(p
[strlen(word
)]))
1132 static int update_name_and_paths(const char *path
, struct lxc_container
*oldc
,
1133 const char *newname
, const char *newpath
)
1138 const char *p0
, *p1
, *p2
, *end
;
1139 const char *oldpath
= oldc
->get_config_path(oldc
);
1140 const char *oldname
= oldc
->name
;
1142 f
= fopen(path
, "r");
1144 SYSERROR("opening old config");
1147 if (fseek(f
, 0, SEEK_END
) < 0) {
1148 SYSERROR("seeking to end of old config");
1155 SYSERROR("telling size of old config");
1158 if (fseek(f
, 0, SEEK_SET
) < 0) {
1160 SYSERROR("rewinding old config");
1163 contents
= malloc(flen
+1);
1165 SYSERROR("out of memory");
1169 if (fread(contents
, 1, flen
, f
) != flen
) {
1172 SYSERROR("reading old config");
1175 contents
[flen
] = '\0';
1176 if (fclose(f
) < 0) {
1178 SYSERROR("closing old config");
1182 f
= fopen(path
, "w");
1184 SYSERROR("reopening config");
1190 end
= contents
+ flen
;
1192 p1
= find_first_wholeword(p0
, oldpath
);
1193 p2
= find_first_wholeword(p0
, oldname
);
1195 // write the rest and be done
1196 if (fwrite(p0
, 1, (end
-p0
), f
) != (end
-p0
)) {
1197 SYSERROR("writing new config");
1207 const char *p
= lowest_nonnull(p1
, p2
);
1208 const char *new = (p
== p2
) ? newname
: newpath
;
1209 if (fwrite(p0
, 1, (p
-p0
), f
) != (p
-p0
)) {
1210 SYSERROR("writing new config");
1216 // now write the newpath or newname
1217 if (fwrite(new, 1, strlen(new), f
) != strlen(new)) {
1218 SYSERROR("writing new name or path in new config");
1223 p0
+= (p
== p2
) ? strlen(oldname
) : strlen(oldpath
);
1228 static int copyhooks(struct lxc_container
*oldc
, struct lxc_container
*c
)
1232 struct lxc_list
*it
;
1234 for (i
=0; i
<NUM_LXC_HOOKS
; i
++) {
1235 lxc_list_for_each(it
, &c
->lxc_conf
->hooks
[i
]) {
1236 char *hookname
= it
->elem
;
1237 char *fname
= rindex(hookname
, '/');
1238 char tmppath
[MAXPATHLEN
];
1239 if (!fname
) // relative path - we don't support, but maybe we should
1241 // copy the script, and change the entry in confile
1242 ret
= snprintf(tmppath
, MAXPATHLEN
, "%s/%s/%s",
1243 c
->config_path
, c
->name
, fname
+1);
1244 if (ret
< 0 || ret
>= MAXPATHLEN
)
1246 ret
= copy_file(it
->elem
, tmppath
);
1250 it
->elem
= strdup(tmppath
);
1252 ERROR("out of memory copying hook path");
1255 update_name_and_paths(it
->elem
, oldc
, c
->name
, c
->get_config_path(c
));
1259 c
->save_config(c
, NULL
);
1263 static void new_hwaddr(char *hwaddr
)
1265 FILE *f
= fopen("/dev/urandom", "r");
1268 int ret
= fread(&seed
, sizeof(seed
), 1, f
);
1275 snprintf(hwaddr
, 18, "00:16:3e:%02x:%02x:%02x",
1276 rand() % 255, rand() % 255, rand() % 255);
1279 static void network_new_hwaddrs(struct lxc_container
*c
)
1281 struct lxc_list
*it
;
1283 lxc_list_for_each(it
, &c
->lxc_conf
->network
) {
1284 struct lxc_netdev
*n
= it
->elem
;
1286 new_hwaddr(n
->hwaddr
);
1290 static int copy_fstab(struct lxc_container
*oldc
, struct lxc_container
*c
)
1292 char newpath
[MAXPATHLEN
];
1293 char *oldpath
= oldc
->lxc_conf
->fstab
;
1299 char *p
= rindex(oldpath
, '/');
1302 ret
= snprintf(newpath
, MAXPATHLEN
, "%s/%s%s",
1303 c
->config_path
, c
->name
, p
);
1304 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1305 ERROR("error printing new path for %s", oldpath
);
1308 if (file_exists(newpath
)) {
1309 ERROR("error: fstab file %s exists", newpath
);
1313 if (copy_file(oldpath
, newpath
) < 0) {
1314 ERROR("error: copying %s to %s", oldpath
, newpath
);
1317 free(c
->lxc_conf
->fstab
);
1318 c
->lxc_conf
->fstab
= strdup(newpath
);
1319 if (!c
->lxc_conf
->fstab
) {
1320 ERROR("error: allocating pathname");
1327 static int copy_storage(struct lxc_container
*c0
, struct lxc_container
*c
,
1328 const char *newtype
, int flags
, const char *bdevdata
, unsigned long newsize
)
1332 bdev
= bdev_copy(c0
->lxc_conf
->rootfs
.path
, c0
->name
, c
->name
,
1333 c0
->config_path
, c
->config_path
, newtype
, !!(flags
& LXC_CLONE_SNAPSHOT
),
1336 ERROR("error copying storage");
1339 free(c
->lxc_conf
->rootfs
.path
);
1340 c
->lxc_conf
->rootfs
.path
= strdup(bdev
->src
);
1342 if (!c
->lxc_conf
->rootfs
.path
)
1344 // here we could also update all lxc.mount.entries or even
1345 // items in the lxc.mount fstab list. As discussed on m-l,
1346 // we could do either any source paths starting with the
1347 // lxcpath/oldname, or simply anythign which is not a virtual
1348 // fs or a bind mount.
1352 static int clone_update_rootfs(struct lxc_container
*c
, int flags
)
1355 char path
[MAXPATHLEN
];
1360 if (flags
& LXC_CLONE_KEEPNAME
)
1363 /* update hostname in rootfs */
1364 /* we're going to mount, so run in a clean namespace to simplify cleanup */
1370 return wait_for_pid(pid
);
1372 if (unshare(CLONE_NEWNS
) < 0) {
1373 ERROR("error unsharing mounts");
1376 bdev
= bdev_init(c
->lxc_conf
->rootfs
.path
, c
->lxc_conf
->rootfs
.mount
, NULL
);
1379 if (bdev
->ops
->mount(bdev
) < 0)
1381 ret
= snprintf(path
, MAXPATHLEN
, "%s/etc/hostname", bdev
->dest
);
1382 if (ret
< 0 || ret
>= MAXPATHLEN
)
1384 if (!(fout
= fopen(path
, "w"))) {
1385 SYSERROR("unable to open %s: ignoring\n", path
);
1388 if (fprintf(fout
, "%s", c
->name
) < 0)
1390 if (fclose(fout
) < 0)
1396 * We want to support:
1397 sudo lxc-clone -o o1 -n n1 -s -L|-fssize fssize -v|--vgname vgname \
1398 -p|--lvprefix lvprefix -t|--fstype fstype -B backingstore
1400 -s [ implies overlayfs]
1404 only rootfs gets converted (copied/snapshotted) on clone.
1407 static int create_file_dirname(char *path
)
1409 char *p
= rindex(path
, '/');
1415 ret
= mkdir(path
, 0755);
1416 if (ret
&& errno
!= EEXIST
)
1417 SYSERROR("creating container path %s\n", path
);
1422 struct lxc_container
*lxcapi_clone(struct lxc_container
*c
, const char *newname
,
1423 const char *lxcpath
, int flags
,
1424 const char *bdevtype
, const char *bdevdata
, unsigned long newsize
)
1426 struct lxc_container
*c2
= NULL
;
1427 char newpath
[MAXPATHLEN
];
1432 if (!c
|| !c
->is_defined(c
))
1435 if (lxclock(c
->privlock
, 0))
1438 if (c
->is_running(c
)) {
1439 ERROR("error: Original container (%s) is running", c
->name
);
1443 // Make sure the container doesn't yet exist.
1444 n
= newname
? newname
: c
->name
;
1445 l
= lxcpath
? lxcpath
: c
->get_config_path(c
);
1446 ret
= snprintf(newpath
, MAXPATHLEN
, "%s/%s/config", l
, n
);
1447 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1448 SYSERROR("clone: failed making config pathname");
1451 if (file_exists(newpath
)) {
1452 ERROR("error: clone: %s exists", newpath
);
1456 if (create_file_dirname(newpath
) < 0) {
1457 ERROR("Error creating container dir for %s", newpath
);
1461 // copy the configuration, tweak it as needed,
1462 fout
= fopen(newpath
, "w");
1464 SYSERROR("open %s", newpath
);
1467 write_config(fout
, c
->lxc_conf
);
1470 if (update_name_and_paths(newpath
, c
, n
, l
) < 0) {
1471 ERROR("Error updating name in cloned config");
1475 sprintf(newpath
, "%s/%s/rootfs", l
, n
);
1476 if (mkdir(newpath
, 0755) < 0) {
1477 SYSERROR("error creating %s", newpath
);
1481 c2
= lxc_container_new(n
, l
);
1483 ERROR("clone: failed to create new container (%s %s)", n
, l
);
1487 // copy hooks if requested
1488 if (flags
& LXC_CLONE_COPYHOOKS
) {
1489 ret
= copyhooks(c
, c2
);
1491 ERROR("error copying hooks");
1496 if (copy_fstab(c
, c2
) < 0) {
1497 ERROR("error copying fstab");
1502 if (!(flags
& LXC_CLONE_KEEPMACADDR
))
1503 network_new_hwaddrs(c2
);
1505 // copy/snapshot rootfs's
1506 ret
= copy_storage(c
, c2
, bdevtype
, flags
, bdevdata
, newsize
);
1510 if (!c2
->save_config(c2
, NULL
))
1513 if (clone_update_rootfs(c2
, flags
) < 0)
1516 // TODO: update c's lxc.snapshot = count
1517 lxcunlock(c
->privlock
);
1521 lxcunlock(c
->privlock
);
1524 lxc_container_put(c2
);
1530 struct lxc_container
*lxc_container_new(const char *name
, const char *configpath
)
1532 struct lxc_container
*c
;
1534 c
= malloc(sizeof(*c
));
1536 fprintf(stderr
, "failed to malloc lxc_container\n");
1539 memset(c
, 0, sizeof(*c
));
1542 c
->config_path
= strdup(configpath
);
1544 c
->config_path
= strdup(default_lxc_path());
1546 if (!c
->config_path
) {
1547 fprintf(stderr
, "Out of memory");
1551 c
->name
= malloc(strlen(name
)+1);
1553 fprintf(stderr
, "Error allocating lxc_container name\n");
1556 strcpy(c
->name
, name
);
1559 c
->slock
= lxc_newlock(name
);
1561 fprintf(stderr
, "failed to create lock\n");
1565 c
->privlock
= lxc_newlock(NULL
);
1567 fprintf(stderr
, "failed to alloc privlock\n");
1571 if (!set_config_filename(c
)) {
1572 fprintf(stderr
, "Error allocating config file pathname\n");
1576 if (file_exists(c
->configfile
))
1577 lxcapi_load_config(c
, NULL
);
1579 // assign the member functions
1580 c
->is_defined
= lxcapi_is_defined
;
1581 c
->state
= lxcapi_state
;
1582 c
->is_running
= lxcapi_is_running
;
1583 c
->freeze
= lxcapi_freeze
;
1584 c
->unfreeze
= lxcapi_unfreeze
;
1585 c
->init_pid
= lxcapi_init_pid
;
1586 c
->load_config
= lxcapi_load_config
;
1587 c
->want_daemonize
= lxcapi_want_daemonize
;
1588 c
->start
= lxcapi_start
;
1589 c
->startl
= lxcapi_startl
;
1590 c
->stop
= lxcapi_stop
;
1591 c
->config_file_name
= lxcapi_config_file_name
;
1592 c
->wait
= lxcapi_wait
;
1593 c
->set_config_item
= lxcapi_set_config_item
;
1594 c
->destroy
= lxcapi_destroy
;
1595 c
->save_config
= lxcapi_save_config
;
1596 c
->get_keys
= lxcapi_get_keys
;
1597 c
->create
= lxcapi_create
;
1598 c
->createl
= lxcapi_createl
;
1599 c
->shutdown
= lxcapi_shutdown
;
1600 c
->clear_config_item
= lxcapi_clear_config_item
;
1601 c
->get_config_item
= lxcapi_get_config_item
;
1602 c
->get_cgroup_item
= lxcapi_get_cgroup_item
;
1603 c
->set_cgroup_item
= lxcapi_set_cgroup_item
;
1604 c
->get_config_path
= lxcapi_get_config_path
;
1605 c
->set_config_path
= lxcapi_set_config_path
;
1606 c
->clone
= lxcapi_clone
;
1608 /* we'll allow the caller to update these later */
1609 if (lxc_log_init(NULL
, "none", NULL
, "lxc_container", 0, c
->config_path
)) {
1610 fprintf(stderr
, "failed to open log\n");
1617 lxc_container_free(c
);
1621 int lxc_get_wait_states(const char **states
)
1626 for (i
=0; i
<MAX_STATE
; i
++)
1627 states
[i
] = lxc_state2str(i
);