lxccontainer.c revision 4ba0d9af63fbf7e9acfa068a1fe36b3d287b9c6b
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright © 2012 Canonical Ltd.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This program is free software; you can redistribute it and/or modify
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * it under the terms of the GNU General Public License version 2, as
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * published by the Free Software Foundation.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This program is distributed in the hope that it will be useful,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * but WITHOUT ANY WARRANTY; without even the implied warranty of
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * GNU General Public License for more details.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You should have received a copy of the GNU General Public License along
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * with this program; if not, write to the Free Software Foundation, Inc.,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <../include/ifaddrs.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool file_exists(char *f)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * A few functions to help detect when a container creation failed.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If a container creation was killed partway through, then trying
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * to actually start that container could harm the host. We detect
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * this by creating a 'partial' file under the container directory,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and keeping an advisory lock. When container creation completes,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * we remove that file. When we load or try to start a container, if
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * we find that file, without a flock, we remove the container.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int len = strlen(c->config_path) + strlen(c->name) + 10;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // give benefit of the doubt
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fcntl(fd, F_GETLK, &lk) == 0 && lk.l_pid != -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // create is still ongoing
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // create completed but partial is still there.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // $lxcpath + '/' + $name + '/partial' + \0
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int len = strlen(c->config_path) + strlen(c->name) + 10;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((fd=open(path, O_RDWR | O_CREAT | O_EXCL, 0755)) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid remove_partial(struct lxc_container *c, int fd)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // $lxcpath + '/' + $name + '/partial' + \0
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int len = strlen(c->config_path) + strlen(c->name) + 10;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(path, len, "%s/%s/partial", c->config_path, c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * 1. container_mem_lock(c) protects the struct lxc_container from multiple threads.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * 2. container_disk_lock(c) protects the on-disk container data - in particular the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * container configuration file.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The container_disk_lock also takes the container_mem_lock.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * 3. thread_mutex protects process data (ex: fd table) from multiple threads.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * NOTHING mutexes two independent programs with their own struct
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * lxc_container for the same c->name, between API calls. For instance,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * c->config_read(); c->start(); Between those calls, data on disk
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * could change (which shouldn't bother the caller unless for instance
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the rootfs get moved). c->config_read(); update; c->config_write();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Two such updaters could race. The callers should therefore check their
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * results. Trying to prevent that would necessarily expose us to deadlocks
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * due to hung callers. So I prefer to keep the locks only within our own
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * functions, not across functions.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If you're going to clone while holding a lxccontainer, increment
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * c->numthreads (under privlock) before forking. When deleting,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * decrement numthreads under privlock, then if it hits 0 you can delete.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Do not ever use a lxccontainer whose numthreads you did not bump.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void lxc_container_free(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Consider the following case:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortefreer | racing get()er
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte==================================================================
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortelxc_container_put() | lxc_container_get()
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte\ lxclock(c->privlock) | c->numthreads < 1? (no)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte\ c->numthreads = 0 | \ lxclock(c->privlock) -> waits
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte\ lxcunlock() | \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte\ lxc_container_free() | \ lxclock() returns
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte | \ c->numthreads < 1 -> return 0
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana\ \ (free stuff) |
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte\ \ sem_destroy(privlock) |
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * When the get()er checks numthreads the first time, one of the following
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana * 1. freer has set numthreads = 0. get() returns 0
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * 2. freer is between lxclock and setting numthreads to 0. get()er will
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * sem_wait on privlock, get lxclock after freer() drops it, then see
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * numthreads is 0 and exit without touching lxclock again..
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana * 3. freer has not yet locked privlock. If get()er runs first, then put()er
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * will see --numthreads = 1 and not call lxc_container_free().
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // if someone else has already started freeing the container, don't
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // try to take the lock, which may be invalid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // bail without trying to unlock, bc the privlock is now probably
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // in freed memory
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_is_defined(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool ret = false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic const char *lxcapi_state(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (s == STOPPED);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_is_running(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *s;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_freeze(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_unfreeze(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int lxcapi_console_getfd(struct lxc_container *c, int *ttynum, int *masterfd)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int lxcapi_console(struct lxc_container *c, int ttynum, int stdinfd,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return lxc_console(c, ttynum, stdinfd, stdoutfd, stderrfd, escape);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic pid_t lxcapi_init_pid(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return lxc_cmd_get_init_pid(c->name, c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool load_config_locked(struct lxc_container *c, const char *fname)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c->lxc_conf && !lxc_config_read(fname, c->lxc_conf))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_load_config(struct lxc_container *c, const char *alt_file)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *fname;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If we're reading something other than the container's config,
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana * we only need to lock the in-memory container. If loading the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * container's config file, take the disk lock.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void lxcapi_want_daemonize(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_wait(struct lxc_container *c, const char *state, int timeout)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = lxc_wait(c->name, state, timeout, c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret == 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool wait_on_daemonized_start(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* we'll probably want to make this timeout configurable? */
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana * our child is going to fork again, then exit. reap the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ret == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana DEBUG("failed waiting for first dual-fork child");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * I can't decide if it'd be more convenient for callers if we accept '...',
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * or a null-terminated array (i.e. execl vs execv)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv[])
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* container exists */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* container has been setup */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error: %s creation was not completed", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error: creation of %s is ongoing", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* is this app meant to be run through lxcinit, as in lxc-execute? */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = lxc_execute(c->name, argv, 1, conf, c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret == 0 ? true : false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * say, I'm not sure - what locks do we want here? Any?
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Is liblxc's locking enough here to protect the on disk
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana * container? We don't want to exclude things like lxc_info
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * while container is running...
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* second fork to be reparented by init */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* like daemon(), chdir to / and redirect 0,1,2 to /dev/null */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana ret = lxc_start(c->name, argv, conf, c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (ret == 0 ? true : false);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * note there MUST be an ending NULL
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_startl(struct lxc_container *c, int useinit, ...)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool bret = false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* container exists */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* pass NULL if no arguments were supplied */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bret = lxcapi_start(c, useinit, *inargs ? inargs : NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret == 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * create the standard expected container dir
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool create_container_dir(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte len = strlen(c->config_path) + strlen(c->name) + 2;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(s, len, "%s/%s", c->config_path, c->name);
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to create container path for %s\n", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret == 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic const char *lxcapi_get_config_path(struct lxc_container *c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * do_bdev_create: thin wrapper around bdev_create(). Like bdev_create(),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * it returns a mounted bdev on success, NULL on error.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic struct bdev *do_bdev_create(struct lxc_container *c, const char *type,
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana len = strlen(c->name) + strlen(lxcpath) + 9;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(dest, len, "%s/%s/rootfs", lxcpath, c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Failed to create backing store type %s\n", type);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lxcapi_set_config_item(c, "lxc.rootfs", bdev->src);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Given the '-t' template option to lxc-create, figure out what to
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * do. If the template is a full executable path, use that. If it
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * is something like 'sshd', then return $templatepath/lxc-sshd. If
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * no template was passed in, return NULL (this is ok).
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * On error return (char *) -1.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortechar *get_template_path(const char *t)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (char *) -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte len = strlen(LXCTEMPLATEDIR) + strlen(t) + strlen("/lxc-") + 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (char *) -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(tpath, len, "%s/lxc-%s", LXCTEMPLATEDIR, t);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (char *) -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (char *) -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool create_run_template(struct lxc_container *c, char *tpath, bool quiet,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *const argv[])
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to fork task for container creation template\n");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * for an overlayfs create, what the user wants is the template to fill
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * in what will become the readonly lower layer. So don't mount for
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the template
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bdev = bdev_init(src, c->lxc_conf->rootfs.mount, NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * create our new array, pre-pend the template name and
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte nargs += 4; // template, path, rootfs and name args
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte len = strlen(c->config_path) + strlen(c->name) + strlen("--path=") + 2;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(patharg, len, "--path=%s/%s", c->config_path, c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(namearg, len, "--name=%s", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte len = strlen("--rootfs=") + 1 + strlen(bdev->dest);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(rootfsarg, len, "--rootfs=%s", bdev->dest);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* add passed-in args */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* add trailing NULL */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte newargv = realloc(newargv, nargs * sizeof(*newargv));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* execute */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to execute template %s", tpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("container creation template for %s failed\n", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortebool prepend_lxc_header(char *path, const char *t, char *const argv[])
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool have_tpath = false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fclose(f) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(f, "# Template used to create this container: %s\n", t);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(f, "# Parameters passed to the template:");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(f, "# Template script checksum (SHA-1): ");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i=0; i<SHA_DIGEST_LENGTH; i++)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fclose(f) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_destroy(struct lxc_container *c);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * lxcapi_create:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * create a container with the given parameters.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @c: container to be created. It has the lxcpath, name, and a starting
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * configuration already set
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @t: the template to execute to instantiate the root filesystem and
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * adjust the configuration.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @bdevtype: backing store type to use. If NULL, dir will be used.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @specs: additional parameters for the backing store, i.e. LVM vg to
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @argv: the arguments to pass to the template, terminated by NULL. If no
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * arguments, you can just pass NULL.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_create(struct lxc_container *c, const char *t,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *bdevtype, struct bdev_specs *specs, int flags,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *const argv[])
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool bret = false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("failed to save starting configuration for %s\n", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* container is already created if we have a config and rootfs.path is accessible */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lxcapi_is_defined(c) && c->lxc_conf && c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Mark that this container is being created */
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana /* no need to get disk lock bc we have the partial locked */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Create the backing store
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Note we can't do this in the same task as we use to execute the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * template because of the way zfs works.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * After you 'zfs create', zfs mounts the fs only in the initial
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * namespace.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to fork task for container creation template\n");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!(bdev = do_bdev_create(c, bdevtype, specs))) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error creating backing store type %s for %s",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* save config file again to store the new rootfs location */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("failed to save starting configuration for %s\n", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // parent task won't see bdev in config so we delete it
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* reload config to get the rootfs */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!create_run_template(c, tpath, !!(flags & LXC_CREATE_QUIET), argv))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // now clear out the lxc_conf we have, reload from the created
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // container
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!prepend_lxc_header(c->configfile, tpath, argv)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error prepending header to configuration file");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_reboot(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_shutdown(struct lxc_container *c, int timeout)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte retv = c->wait(c, "STOPPED", 0); // 0 means don't wait
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_createl(struct lxc_container *c, const char *t,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *bdevtype, struct bdev_specs *specs, int flags, ...)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool bret = false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * since we're going to wait for create to finish, I don't think we
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * need to get a copy of the arguments.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bret = c->create(c, t, bdevtype, specs, flags, args);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_clear_config_item(struct lxc_container *c, const char *key)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c || !c->lxc_conf)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret == 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortechar** lxcapi_get_ips(struct lxc_container *c, char* interface, char* family, int scope)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL;
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana int old_netns = -1, new_netns = -1, ret = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Save reference to old netns */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Switch to new netns */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(new_netns_path, MAXPATHLEN, "/proc/%d/ns/net", c->init_pid(c));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Grab the list of interfaces */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Iterate through the interfaces */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte tempAddrPtr = &((struct sockaddr_in *)tempIfAddr->ifa_addr)->sin_addr;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_scope_id != scope)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte tempAddrPtr = &((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_addr;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (interface && strcmp(interface, tempIfAddr->ifa_name))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else if (!interface && strcmp("lo", tempIfAddr->ifa_name) == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte address = (char *)inet_ntop(tempIfAddr->ifa_addr->sa_family,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte temp = realloc(addresses, count * sizeof(*addresses));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Switch back to original netns */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (old_netns >= 0 && setns(old_netns, CLONE_NEWNET))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Append NULL to the array */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte temp = realloc(addresses, count * sizeof(*addresses));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int lxcapi_get_config_item(struct lxc_container *c, const char *key, char *retv, int inlen)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c || !c->lxc_conf)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = lxc_get_config_item(c->lxc_conf, key, retv, inlen);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int lxcapi_get_keys(struct lxc_container *c, const char *key, char *retv, int inlen)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Support 'lxc.network.<idx>', i.e. 'lxc.network.0'
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This is an intelligent result to show which keys are valid given
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the type of nic it is
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c || !c->lxc_conf)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = lxc_list_nicconfigs(c->lxc_conf, key, retv, inlen);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_save_config(struct lxc_container *c, const char *alt_file)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false; // should we write to stdout if no file is specified?
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // If we haven't yet loaded a config, load the stock config
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error loading default configuration file %s while saving %s\n", LXC_DEFAULT_CONFIG, c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If we're writing to the container's config file, take the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * disk lock. Otherwise just take the memlock to protect the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * struct lxc_container while we're traversing it.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte// do we want the api to support --force, or leave that to the caller?
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_destroy(struct lxc_container *c)
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana bool ret = false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // we should queue some sort of error - in c->error_string?
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c->lxc_conf && c->lxc_conf->rootfs.path && c->lxc_conf->rootfs.mount)
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana r = bdev_init(c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error destroying rootfs for %s", c->name);
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana char *path = alloca(strlen(p1) + strlen(c->name) + 2);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error destroying container directory for %s", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool set_config_item_locked(struct lxc_container *c, const char *key, const char *v)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_set_config_item(struct lxc_container *c, const char *key, const char *v)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool b = false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic char *lxcapi_config_file_name(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c || !c->configfile)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic const char *lxcapi_get_config_path(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c || !c->config_path)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (const char *)(c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * not for export
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Just recalculate the c->configfile based on the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * c->config_path, which must be set.
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana * The lxc_container must be locked or not yet public.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool set_config_filename(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* $lxc_path + "/" + c->name + "/" + "config" + '\0' */
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana len = strlen(c->config_path) + strlen(c->name) + strlen("config") + 3;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(newpath, len, "%s/%s/config", c->config_path, c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, "Error printing out config file name\n");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_set_config_path(struct lxc_container *c, const char *path)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool b = false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Since we've changed the config path, we have to change the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * config file name too */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Out of memory setting new config filename");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic bool lxcapi_set_cgroup_item(struct lxc_container *c, const char *subsys, const char *value)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = lxc_cgroup_set(c->name, subsys, value, c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ret == 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int lxcapi_get_cgroup_item(struct lxc_container *c, const char *subsys, char *retv, int inlen)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c || !c->lxc_conf)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = lxc_cgroup_get(c->name, subsys, retv, inlen, c->config_path);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteconst char *lxc_get_default_lvm_vg(void)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteconst char *lxc_get_version(void)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte out = open(new, O_CREAT | O_EXCL | O_WRONLY, 0644);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("write to new file %s was interrupted", new);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // we set mode, but not owner/group
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int copyhooks(struct lxc_container *oldc, struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i=0; i<NUM_LXC_HOOKS; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!fname) // relative path - we don't support, but maybe we should
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana // copy the script, and change the entry in confile
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana ret = snprintf(tmppath, MAXPATHLEN, "%s/%s/%s",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unsigned int seed;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void network_new_hwaddrs(struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int copy_fstab(struct lxc_container *oldc, struct lxc_container *c)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("error printing new path for %s", oldpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("error: copying %s to %s", oldpath, newpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int copy_storage(struct lxc_container *c0, struct lxc_container *c,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *newtype, int flags, const char *bdevdata, unsigned long newsize)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bdev = bdev_copy(c0->lxc_conf->rootfs.path, c0->name, c->name,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c0->config_path, c->config_path, newtype, !!(flags & LXC_CLONE_SNAPSHOT),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // here we could also update all lxc.mount.entries or even
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // items in the lxc.mount fstab list. As discussed on m-l,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // we could do either any source paths starting with the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // lxcpath/oldname, or simply anythign which is not a virtual
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // fs or a bind mount.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int clone_update_rootfs(struct lxc_container *c0,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* update hostname in rootfs */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* we're going to mount, so run in a clean namespace to simplify cleanup */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bdev = bdev_init(c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!lxc_list_empty(&conf->hooks[LXCHOOK_CLONE])) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Start of environment variable setup for hooks */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to set environment variable for source container name");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to set environment variable for container name");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to set environment variable for config path");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to set environment variable for rootfs mount");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("failed to set environment variable for rootfs mount");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (run_lxc_hooks(c->name, "clone", conf, c->get_config_path(c), hookargs)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error executing clone hook for %s", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(path, MAXPATHLEN, "%s/etc/hostname", bdev->dest);
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana * We want to support:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortesudo lxc-clone -o o1 -n n1 -s -L|-fssize fssize -v|--vgname vgname \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte -p|--lvprefix lvprefix -t|--fstype fstype -B backingstore
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte-s [ implies overlayfs]
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte-s -B overlayfs
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteonly rootfs gets converted (copied/snapshotted) on clone.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestruct lxc_container *lxcapi_clone(struct lxc_container *c, const char *newname,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *bdevtype, const char *bdevdata, unsigned long newsize,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *n, *l;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!c || !c->is_defined(c))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("error: Original container (%s) is running", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // Make sure the container doesn't yet exist.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = snprintf(newpath, MAXPATHLEN, "%s/%s/config", l, n);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SYSERROR("clone: failed making config pathname");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error creating container dir for %s", newpath);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // copy the configuration, tweak it as needed,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("clone: failed to create new container (%s %s)", n, l);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // update utsname
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!set_config_item_locked(c2, "lxc.utsname", newname)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // copy hooks if requested
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // update macaddrs
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = copy_storage(c, c2, bdevtype, flags, bdevdata, newsize);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // We've now successfully created c2's storage, so clear it out if we
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // fail after this
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (clone_update_rootfs(c, c2, flags, hookargs) < 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // TODO: update c's lxc.snapshot = count
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic 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)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return lxc_attach(c->name, c->config_path, exec_function, exec_payload, options, attached_process);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int lxcapi_attach_run_wait(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[])
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte r = lxc_attach(c->name, c->config_path, lxc_attach_run_command, &command, options, &pid);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (r < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int lxcapi_attach_run_waitl(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char **argv;
3270659f55e0928d6edec3d26217cc29398a8149Srikanth, Ramana argv = lxc_va_arg_list_to_argv_const(ap, 1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = lxcapi_attach_run_wait(c, options, program, (const char * const *)argv);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestruct lxc_container *lxc_container_new(const char *name, const char *configpath)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c = malloc(sizeof(*c));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, "failed to malloc lxc_container\n");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte memset(c, 0, sizeof(*c));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, "Error allocating lxc_container name\n");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!(c->slock = lxc_newlock(c->config_path, name))) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(stderr, "Error allocating config file pathname\n");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERROR("Error: %s creation was not completed", c->name);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // assign the member functions
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* we'll allow the caller to update these later */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lxc_log_init(NULL, "none", NULL, "lxc_container", 0, c->config_path)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i=0; i<MAX_STATE; i++)