From: Stéphane Graber Date: Mon, 1 Apr 2013 14:36:29 +0000 (-0400) Subject: API shouldn't be calling create for already defined containers or destroy for non... X-Git-Tag: lxc-0.9.0~1^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5a3d2e1efa652ed68d1125c688bb1b9b91889778;p=thirdparty%2Flxc.git API shouldn't be calling create for already defined containers or destroy for non defined ones Currently it always calls create/destroy which might be confusing for the code that checks the return value of those calls to determine whether operation completed successfully or not. >>> c = lxc.Container("r") >>> c.create("ubuntu") True >>> c.create("ubuntu") True >>> c.create("ubuntu") True >>> c.create("ubuntu") True >>> c.create("ubuntu") >>> c.destroy() True >>> c.destroy() lxc-destroy: 'r' does not exist False >>> c.destroy() lxc-destroy: 'r' does not exist False New behaviour >>> c = lxc.Container("r") >>> c.create('ubuntu') True >>> c.create('ubuntu') False >>> c.destroy() True >>> c.destroy() False >>> Tested with following script; import lxc c = lxc.Container("abcdef") print ("set", c.set_config_item("lxc.utsname", "abcdef")) print ("save", c.save_config()) print ("create", c.create("ubuntu")) print ("create", c.create("ubuntu")) print ("destroy", c.destroy()) print ("destroy", c.destroy()) print ("set", c.set_config_item("lxc.utsname", "abcdef")) print ("save", c.save_config()) print ("destroy", c.destroy()) print ("destroy", c.destroy()) Signed-off-by: S.Çağlar Onur Acked-by: Serge E. Hallyn --- diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c index 480c4f5f3..a4376b405 100644 --- a/src/lxc/lxccontainer.c +++ b/src/lxc/lxccontainer.c @@ -523,14 +523,15 @@ static bool lxcapi_create(struct lxc_container *c, char *t, char *const argv[]) goto out; } - if (!create_container_dir(c)) - goto out; - if (!c->save_config(c, NULL)) { ERROR("failed to save starting configuration for %s\n", c->name); goto out; } + /* container is already created if we have a config and rootfs.path is accessible */ + if (lxcapi_is_defined(c) && c->lxc_conf && c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) == 0) + return false; + /* we're going to fork. but since we'll wait for our child, we don't need to lxc_container_get */ @@ -767,6 +768,9 @@ static bool lxcapi_save_config(struct lxc_container *c, const char *alt_file) return false; } + if (!create_container_dir(c)) + return false; + FILE *fout = fopen(alt_file, "w"); if (!fout) return false; @@ -788,6 +792,10 @@ static bool lxcapi_destroy(struct lxc_container *c) if (!c) return false; + /* container is already destroyed if we don't have a config and rootfs.path is not accessible */ + if (!lxcapi_is_defined(c) && (!c->lxc_conf || !c->lxc_conf->rootfs.path || access(c->lxc_conf->rootfs.path, F_OK) != 0)) + return false; + pid = fork(); if (pid < 0) return false;