]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
API shouldn't be calling create for already defined containers or destroy for non...
authorStéphane Graber <stgraber@ubuntu.com>
Mon, 1 Apr 2013 14:36:29 +0000 (10:36 -0400)
committerStéphane Graber <stgraber@ubuntu.com>
Mon, 1 Apr 2013 14:36:29 +0000 (10:36 -0400)
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 <caglar@10ur.org>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
src/lxc/lxccontainer.c

index 480c4f5f3f7c90b20624a6338caff6248e118e05..a4376b405c4978033ce1c8b6c343f977e420f67f 100644 (file)
@@ -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;