<div dir="ltr"><div style>Please use this version instead, this fixes the destroy for a container with only config, tested with following script;</div><div style><br></div><div style><div>caglar@qgq:~/Project$ sudo python3 test.py </div>

<div>set True<br></div><div>save True</div><div>create True</div><div>create False</div><div>destroy True</div><div>destroy False</div><div>set True</div><div>save True</div><div>destroy True</div><div><br></div><div>caglar@qgq:~/Project$ cat test.py </div>

<div>import lxc</div><div>c = lxc.Container("abcdef")</div><div>print ("set", c.set_config_item("lxc.utsname", "abcdef"))</div><div>print ("save", c.save_config())</div><div>

print ("create", c.create("ubuntu"))</div><div>print ("create", c.create("ubuntu"))</div><div>print ("destroy", c.destroy())</div><div>print ("destroy", c.destroy())</div>

<div>print</div><div>print ("set", c.set_config_item("lxc.utsname", "abcdef"))</div><div>print ("save", c.save_config())</div><div>print ("destroy", c.destroy())</div><div>

<br></div></div><div>diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c</div><div>index 480c4f5..26ce7dc 100644</div><div>--- a/src/lxc/lxccontainer.c</div><div>+++ b/src/lxc/lxccontainer.c</div><div>@@ -523,14 +523,15 @@ static bool lxcapi_create(struct lxc_container *c, char *t, char *const argv[])</div>

<div> <span class="" style="white-space:pre">          </span>goto out;</div><div> <span class="" style="white-space:pre">        </span>}</div><div> </div><div>-<span class="" style="white-space:pre">        </span>if (!create_container_dir(c))</div>

<div>-<span class="" style="white-space:pre">           </span>goto out;</div><div>-</div><div> <span class="" style="white-space:pre">        </span>if (!c->save_config(c, NULL)) {</div><div> <span class="" style="white-space:pre">               </span>ERROR("failed to save starting configuration for %s\n", c->name);</div>

<div> <span class="" style="white-space:pre">          </span>goto out;</div><div> <span class="" style="white-space:pre">        </span>}</div><div> </div><div>+<span class="" style="white-space:pre">        </span>/* container is already created if we have a config and rootfs.path is accessible */</div>

<div>+<span class="" style="white-space:pre">   </span>if (c->lxc_conf && c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) == 0) </div><div>+<span class="" style="white-space:pre">              </span>return false;</div>

<div>+</div><div> <span class="" style="white-space:pre">  </span>/* we're going to fork.  but since we'll wait for our child, we</div><div> <span class="" style="white-space:pre"> </span>   don't need to lxc_container_get */</div>

<div> </div><div>@@ -767,6 +768,9 @@ static bool lxcapi_save_config(struct lxc_container *c, const char *alt_file)</div><div> <span class="" style="white-space:pre">                 </span>return false;</div><div> <span class="" style="white-space:pre">            </span>}</div>

<div> </div><div>+<span class="" style="white-space:pre">  </span>if (!create_container_dir(c))</div><div>+<span class="" style="white-space:pre">             </span>return false;</div><div>+</div><div> <span class="" style="white-space:pre">    </span>FILE *fout = fopen(alt_file, "w");</div>

<div> <span class="" style="white-space:pre">  </span>if (!fout)</div><div> <span class="" style="white-space:pre">               </span>return false;</div><div>@@ -788,6 +792,10 @@ static bool lxcapi_destroy(struct lxc_container *c)</div>

<div> <span class="" style="white-space:pre">  </span>if (!c)</div><div> <span class="" style="white-space:pre">          </span>return false;</div><div> </div><div>+<span class="" style="white-space:pre">    </span>/* container is already destroyed if we don't have a config and rootfs.path is not accessible */</div>

<div>+<span class="" style="white-space:pre">   </span>if (!lxcapi_is_defined(c) && (!c->lxc_conf || !c->lxc_conf->rootfs.path || access(c->lxc_conf->rootfs.path, F_OK) != 0)) </div><div>+<span class="" style="white-space:pre">         </span>return false;</div>

<div>+</div><div> <span class="" style="white-space:pre">  </span>pid = fork();</div><div> <span class="" style="white-space:pre">    </span>if (pid < 0)</div><div> <span class="" style="white-space:pre">          </span>return false;</div>

</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Apr 1, 2013 at 12:02 AM, Serge Hallyn <span dir="ltr"><<a href="mailto:serge.hallyn@ubuntu.com" target="_blank">serge.hallyn@ubuntu.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">Quoting S.Çağlar Onur (<a href="mailto:caglar@10ur.org">caglar@10ur.org</a>):<br>
> What about something like following?<br>
<br>
</div>This looks good to me.<br>
<br>
Acked-by: Serge E. Hallyn <<a href="mailto:serge.hallyn@ubuntu.com">serge.hallyn@ubuntu.com</a>><br>
<div class="HOEnZb"><div class="h5"><br>
> diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c<br>
> index 480c4f5..eb99e5a 100644<br>
> --- a/src/lxc/lxccontainer.c<br>
> +++ b/src/lxc/lxccontainer.c<br>
> @@ -508,7 +508,7 @@ static bool lxcapi_create(struct lxc_container *c, char<br>
> *t, char *const argv[])<br>
>   int len, nargs = 0;<br>
>   char **newargv;<br>
><br>
> - if (!c)<br>
> + if (!c || access(c->lxc_conf->rootfs.path, F_OK) == 0)<br>
>   return false;<br>
><br>
>   len = strlen(LXCTEMPLATEDIR) + strlen(t) + strlen("/lxc-") + 1;<br>
> @@ -523,9 +523,6 @@ static bool lxcapi_create(struct lxc_container *c, char<br>
> *t, char *const argv[])<br>
>   goto out;<br>
>   }<br>
><br>
> - if (!create_container_dir(c))<br>
> - goto out;<br>
> -<br>
>   if (!c->save_config(c, NULL)) {<br>
>   ERROR("failed to save starting configuration for %s\n", c->name);<br>
>   goto out;<br>
> @@ -767,6 +764,9 @@ static bool lxcapi_save_config(struct lxc_container *c,<br>
> const char *alt_file)<br>
>   return false;<br>
>   }<br>
><br>
> + if (!create_container_dir(c))<br>
> + return false;<br>
> +<br>
>   FILE *fout = fopen(alt_file, "w");<br>
>   if (!fout)<br>
>   return false;<br>
> @@ -785,7 +785,7 @@ static bool lxcapi_destroy(struct lxc_container *c)<br>
>   pid_t pid;<br>
>   int ret, status;<br>
><br>
> - if (!c)<br>
> + if (!c || access(c->lxc_conf->rootfs.path, F_OK) != 0)<br>
>   return false;<br>
><br>
>   pid = fork();<br>
><br>
><br>
> and this is how it behaves with it;<br>
><br>
> caglar@qgq:~$ cat test.py<br>
> import lxc<br>
> c = lxc.Container("abcdef")<br>
> print (c.set_config_item("lxc.utsname", "abcdef"))<br>
> print (c.save_config())<br>
> print (c.create("ubuntu"))<br>
> print (c.create("ubuntu"))<br>
> print (c.destroy())<br>
> print (c.destroy())<br>
><br>
> caglar@qgq:~$ sudo python3 test.py<br>
> True<br>
> True<br>
> True<br>
> False<br>
> True<br>
> False<br>
><br>
><br>
> On Sun, Mar 31, 2013 at 7:26 PM, S.Çağlar Onur <<a href="mailto:caglar@10ur.org">caglar@10ur.org</a>> wrote:<br>
><br>
> > Hi Stéphane,<br>
> ><br>
> > Hmm, then I believe there is another bug somewhere cause here is what<br>
> > happens with your ordering;<br>
> ><br>
> > caglar@qgq:~/Project/lxc$ sudo python3<br>
> > Python 3.2.3 (default, Oct 19 2012, 19:53:16)<br>
> > [GCC 4.7.2] on linux2<br>
> > Type "help", "copyright", "credits" or "license" for more information.<br>
> > >>> import lxc<br>
> > __main__:1: Warning: The python-lxc API isn't yet stable and may change at<br>
> > any point in the future.<br>
> >  >>> c = lxc.Container("abcdef")<br>
> > >>> c.set_config_item("lxc.utsname", "blah")<br>
> > True<br>
> > >>> c.set_config_item("lxc.utsname", "abcdef")<br>
> > True<br>
> > >>> c.save_config()<br>
> > False<br>
> > >>> c.config_file_name<br>
> > '/var/lib/lxc/abcdef/config'<br>
> ><br>
> > so it looks like save_config don't work if the container directory is not<br>
> > there and as long as I see only create calls create_container_dir to do<br>
> > that.<br>
> ><br>
> > Maybe correct way to handle that is to call create_container_dir from<br>
> > save_config as well but checking the rootfs directory's existence from<br>
> > create/destrory?<br>
> ><br>
> > Best,<br>
> ><br>
> ><br>
> > On Sun, Mar 31, 2013 at 5:22 PM, Stéphane Graber <<a href="mailto:stgraber@ubuntu.com">stgraber@ubuntu.com</a>><br>
> > wrote:<br>
> > ><br>
> > > On 03/31/2013 04:22 PM, S.Çağlar Onur wrote:<br>
> > > > From: "S.Çağlar Onur" <<a href="mailto:caglar@10ur.org">caglar@10ur.org</a>><br>
> > > ><br>
> > > > Currently it behaves like following which might be confusing for the<br>
> > code that checks the return value of those calls to determine whether<br>
> > operation completed successfully or not.<br>
> > > ><br>
> > > >>> c = lxc.Container("r")<br>
> > > >>>> c.create("ubuntu")<br>
> > > > True<br>
> > > >>>> c.create("ubuntu")<br>
> > > > True<br>
> > > >>>> c.create("ubuntu")<br>
> > > > True<br>
> > > >>>> c.create("ubuntu")<br>
> > > > True<br>
> > > >>>> c.create("ubuntu")<br>
> > > >>>> c.destroy()<br>
> > > > True<br>
> > > >>>> c.destroy()<br>
> > > > lxc-destroy: 'r' does not exist<br>
> > > > False<br>
> > > >>>> c.destroy()<br>
> > > > lxc-destroy: 'r' does not exist<br>
> > > > False<br>
> > > ><br>
> > > > New behaviour<br>
> > > ><br>
> > > >>>> c = lxc.Container("r")<br>
> > > >>>> c.create('ubuntu')<br>
> > > > True<br>
> > > >>>> c.create('ubuntu')<br>
> > > > False<br>
> > > >>>> c.destroy()<br>
> > > > True<br>
> > > >>>> c.destroy()<br>
> > > > False<br>
> > > >>>><br>
> > ><br>
> > ><br>
> > > Won't this break the following?<br>
> > > c = lxc.Container("abcdef")<br>
> > > c.set_config_item("lxc.utsname", "blah")<br>
> > > c.save_config()<br>
> > > c.create("ubuntu")<br>
> > ><br>
> > > I personally always considered ".create()" to mean "generate a new<br>
> > > rootfs" which doesn't at all mean "generate a new config file".<br>
> > ><br>
> > > ".destroy()" on the other hand destroys everything, including the config.<br>
> > ><br>
> > ><br>
> > ><br>
> > > > Signed-off-by: S.Çağlar Onur <<a href="mailto:caglar@10ur.org">caglar@10ur.org</a>><br>
> > > > ---<br>
> > > >  src/lxc/lxccontainer.c |    4 ++--<br>
> > > >  1 file changed, 2 insertions(+), 2 deletions(-)<br>
> > > ><br>
> > > > diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c<br>
> > > > index 480c4f5..7a11c85 100644<br>
> > > > --- a/src/lxc/lxccontainer.c<br>
> > > > +++ b/src/lxc/lxccontainer.c<br>
> > > > @@ -508,7 +508,7 @@ static bool lxcapi_create(struct lxc_container *c,<br>
> > char *t, char *const argv[])<br>
> > > >       int len, nargs = 0;<br>
> > > >       char **newargv;<br>
> > > ><br>
> > > > -     if (!c)<br>
> > > > +     if (!c || lxcapi_is_defined(c))<br>
> > > >               return false;<br>
> > > ><br>
> > > >       len = strlen(LXCTEMPLATEDIR) + strlen(t) + strlen("/lxc-") + 1;<br>
> > > > @@ -785,7 +785,7 @@ static bool lxcapi_destroy(struct lxc_container *c)<br>
> > > >       pid_t pid;<br>
> > > >       int ret, status;<br>
> > > ><br>
> > > > -     if (!c)<br>
> > > > +     if (!c || !lxcapi_is_defined(c))<br>
> > > >               return false;<br>
> > > ><br>
> > > >       pid = fork();<br>
> > > ><br>
> > ><br>
> > ><br>
> > > --<br>
> > > Stéphane Graber<br>
> > > Ubuntu developer<br>
> > > <a href="http://www.ubuntu.com" target="_blank">http://www.ubuntu.com</a><br>
> > ><br>
> > ><br>
> > ><br>
> > ------------------------------------------------------------------------------<br>
> > > Own the Future-Intel(R) Level Up Game Demo Contest 2013<br>
> > > Rise to greatness in Intel's independent game demo contest. Compete<br>
> > > for recognition, cash, and the chance to get your game on Steam.<br>
> > > $5K grand prize plus 10 genre and skill prizes. Submit your demo<br>
> > > by 6/6/13. <a href="http://altfarm.mediaplex.com/ad/ck/12124-176961-30367-2" target="_blank">http://altfarm.mediaplex.com/ad/ck/12124-176961-30367-2</a><br>
> > > _______________________________________________<br>
> > > Lxc-devel mailing list<br>
> > > <a href="mailto:Lxc-devel@lists.sourceforge.net">Lxc-devel@lists.sourceforge.net</a><br>
> > > <a href="https://lists.sourceforge.net/lists/listinfo/lxc-devel" target="_blank">https://lists.sourceforge.net/lists/listinfo/lxc-devel</a><br>
> > ><br>
> ><br>
> ><br>
> ><br>
> > --<br>
> > S.Çağlar Onur <<a href="mailto:caglar@10ur.org">caglar@10ur.org</a>><br>
> ><br>
><br>
><br>
><br>
> --<br>
> S.Çağlar Onur <<a href="mailto:caglar@10ur.org">caglar@10ur.org</a>><br>
<br>
> ------------------------------------------------------------------------------<br>
> Own the Future-Intel(R) Level Up Game Demo Contest 2013<br>
> Rise to greatness in Intel's independent game demo contest. Compete<br>
> for recognition, cash, and the chance to get your game on Steam.<br>
> $5K grand prize plus 10 genre and skill prizes. Submit your demo<br>
> by 6/6/13. <a href="http://altfarm.mediaplex.com/ad/ck/12124-176961-30367-2" target="_blank">http://altfarm.mediaplex.com/ad/ck/12124-176961-30367-2</a><br>
<br>
> _______________________________________________<br>
> Lxc-devel mailing list<br>
> <a href="mailto:Lxc-devel@lists.sourceforge.net">Lxc-devel@lists.sourceforge.net</a><br>
> <a href="https://lists.sourceforge.net/lists/listinfo/lxc-devel" target="_blank">https://lists.sourceforge.net/lists/listinfo/lxc-devel</a><br>
<br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br>S.Çağlar Onur <<a href="mailto:caglar@10ur.org">caglar@10ur.org</a>>
</div>