]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
create: add a quiet flag
authorSerge Hallyn <serge.hallyn@ubuntu.com>
Fri, 12 Jul 2013 14:44:41 +0000 (09:44 -0500)
committerSerge Hallyn <serge.hallyn@ubuntu.com>
Fri, 12 Jul 2013 16:19:54 +0000 (11:19 -0500)
If set, then fds 0,1,2 will be redirected while the creation
template is executed.

Note, as Dwight has pointed out, if fd 0 is redirected, then if
templates ask for input there will be a problem.  We could simply
not redirect fd 0, or we could require that templates work without
interaction.  I'm assuming here that we want to do the latter, but
I'm open to changing that.

Reported-by: "S.Çağlar Onur" <caglar@10ur.org>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
src/lua-lxc/core.c
src/lxc/lxc_create.c
src/lxc/lxccontainer.c
src/lxc/lxccontainer.h
src/python-lxc/lxc.c
src/tests/cgpath.c
src/tests/clonetest.c
src/tests/console.c
src/tests/createtest.c
src/tests/get_item.c
src/tests/shutdowntest.c

index 778ef9926639f9bee602b3f1af42610c969e9748..642dbfc58fb177a218f4e65d5beb8f89d82428f3 100644 (file)
@@ -111,7 +111,7 @@ static int container_create(lua_State *L)
        argv[i] = strdupa(luaL_checkstring(L, i+3));
     argv[i] = NULL;
 
-    lua_pushboolean(L, !!c->create(c, template_name, NULL, NULL, argv));
+    lua_pushboolean(L, !!c->create(c, template_name, NULL, NULL, 0, argv));
     return 1;
 }
 
index 6d8ca01bbc69567ead33762f4f630130f521b876..bd08ea26f33bcae4e910a1f2a38e68636cfd0bb1 100644 (file)
@@ -169,6 +169,7 @@ int main(int argc, char *argv[])
 {
        struct lxc_container *c;
        struct bdev_specs spec;
+       int flags = 0;
 
        /* this is a short term test.  We'll probably want to check for
         * write access to lxcpath instead */
@@ -228,7 +229,9 @@ int main(int argc, char *argv[])
 
        if (strcmp(my_args.bdevtype, "_unset") == 0)
                my_args.bdevtype = NULL;
-       if (!c->create(c, my_args.template, my_args.bdevtype, &spec, &argv[optind])) {
+       if (my_args.quiet)
+               flags = LXC_CREATE_QUIET;
+       if (!c->create(c, my_args.template, my_args.bdevtype, &spec, flags, &argv[optind])) {
                ERROR("Error creating container %s", c->name);
                lxc_container_put(c);
                exit(1);
index 1584a2b218038761bb57f224b94a23f802bc8446..446827c0a45b83fbfa6cbfb1f0a56c8631da2a69 100644 (file)
@@ -752,7 +752,7 @@ static char *lxcbasename(char *path)
        return p;
 }
 
-static bool create_run_template(struct lxc_container *c, char *tpath,
+static bool create_run_template(struct lxc_container *c, char *tpath, bool quiet,
                                char *const argv[])
 {
        pid_t pid;
@@ -773,6 +773,14 @@ static bool create_run_template(struct lxc_container *c, char *tpath,
                int ret, len, nargs = 0;
                char **newargv;
 
+               if (quiet) {
+                       close(0);
+                       close(1);
+                       close(2);
+                       open("/dev/zero", O_RDONLY);
+                       open("/dev/null", O_RDWR);
+                       open("/dev/null", O_RDWR);
+               }
                if (unshare(CLONE_NEWNS) < 0) {
                        ERROR("error unsharing mounts");
                        exit(1);
@@ -878,7 +886,7 @@ static bool lxcapi_destroy(struct lxc_container *c);
  * arguments, you can just pass NULL.
  */
 static bool lxcapi_create(struct lxc_container *c, const char *t,
-               const char *bdevtype, struct bdev_specs *specs,
+               const char *bdevtype, struct bdev_specs *specs, int flags,
                char *const argv[])
 {
        bool bret = false;
@@ -951,7 +959,7 @@ static bool lxcapi_create(struct lxc_container *c, const char *t,
        if (!load_config_locked(c, c->configfile))
                goto out;
 
-       if (!create_run_template(c, tpath, argv))
+       if (!create_run_template(c, tpath, !!(flags & LXC_CREATE_QUIET), argv))
                goto out_unlock;
 
        // now clear out the lxc_conf we have, reload from the created
@@ -1014,7 +1022,7 @@ static bool lxcapi_shutdown(struct lxc_container *c, int timeout)
 }
 
 static bool lxcapi_createl(struct lxc_container *c, const char *t,
-               const char *bdevtype, struct bdev_specs *specs, ...)
+               const char *bdevtype, struct bdev_specs *specs, int flags, ...)
 {
        bool bret = false;
        char **args = NULL, **temp;
@@ -1028,7 +1036,7 @@ static bool lxcapi_createl(struct lxc_container *c, const char *t,
         * since we're going to wait for create to finish, I don't think we
         * need to get a copy of the arguments.
         */
-       va_start(ap, specs);
+       va_start(ap, flags);
        while (1) {
                char *arg;
                arg = va_arg(ap, char *);
@@ -1047,7 +1055,7 @@ static bool lxcapi_createl(struct lxc_container *c, const char *t,
        if (args)
                args[nargs] = NULL;
 
-       bret = c->create(c, t, bdevtype, specs, args);
+       bret = c->create(c, t, bdevtype, specs, flags, args);
 
 out:
        if (args)
index 5449a46ccfa86328860cb5f9082e473eb0b197b4..3399c7f122880a9e43eca90f63d5a3e1124f02d0 100644 (file)
@@ -12,6 +12,9 @@
 #define LXC_CLONE_SNAPSHOT        (1 << 3)
 #define LXC_CLONE_MAXFLAGS        (1 << 4)
 
+#define LXC_CREATE_QUIET         (1 << 0)
+#define LXC_CREATE_MAXFLAGS       (1 << 1)
+
 struct bdev_specs;
 
 struct lxc_container {
@@ -51,9 +54,9 @@ struct lxc_container {
        bool (*destroy)(struct lxc_container *c);
        bool (*save_config)(struct lxc_container *c, const char *alt_file);
        bool (*create)(struct lxc_container *c, const char *t, const char *bdevtype,
-                       struct bdev_specs *specs, char *const argv[]);
+                       struct bdev_specs *specs, int flags, char *const argv[]);
        bool (*createl)(struct lxc_container *c, const char *t, const char *bdevtype,
-                       struct bdev_specs *specs, ...);
+                       struct bdev_specs *specs, int flags, ...);
        /* send SIGINT to ask container to reboot */
        bool (*reboot)(struct lxc_container *c);
        /* send SIGPWR.  if timeout is not 0 or -1, do a hard stop after timeout seconds */
index 02428d464c01197075b9bf6dec4b3db91a747f02..18f22248cd7dc6be0fdc40af8f59ab142fefbeef 100644 (file)
@@ -249,7 +249,7 @@ Container_create(Container *self, PyObject *args, PyObject *kwds)
         }
     }
 
-    if (self->container->create(self->container, template_name, NULL, NULL, create_args))
+    if (self->container->create(self->container, template_name, NULL, NULL, 0, create_args))
         retval = Py_True;
     else
         retval = Py_False;
index 55c6664bc595d29e71b168eeb843497eb3e8f10b..944769ca34e38b8c62e2133b05f47af4bcc35605 100644 (file)
@@ -279,7 +279,7 @@ static int test_container(const char *lxcpath,
                c = lxc_container_new(name, lxcpath);
        }
        c->set_config_item(c, "lxc.network.type", "empty");
-       if (!c->createl(c, template, NULL, NULL, NULL)) {
+       if (!c->createl(c, template, NULL, NULL, 0, NULL)) {
                TSTERR("creating container %s", name);
                goto out2;
        }
index f15f400d9394b9595ba5a31b5a18e3ed5b74b072..da3ce75269ff4a1a9d7e847723b3652a1cd2eb01 100644 (file)
@@ -53,7 +53,7 @@ int main(int argc, char *argv[])
                goto out;
        }
        c->save_config(c, NULL);
-       if (!c->createl(c, "ubuntu", NULL, NULL, NULL)) {
+       if (!c->createl(c, "ubuntu", NULL, NULL, 0, NULL)) {
                fprintf(stderr, "%d: failed to create a container\n", __LINE__);
                goto out;
        }
index 434f7f2eed98d52018bbf8415e2bc732ab2cf0c8..cd639810980e70656e12c72f003fd044382b132d 100644 (file)
@@ -137,7 +137,7 @@ static int test_console(const char *lxcpath,
                c->destroy(c);
                c = lxc_container_new(name, lxcpath);
        }
-       if (!c->createl(c, template, NULL, NULL, NULL)) {
+       if (!c->createl(c, template, NULL, NULL, 0, NULL)) {
                TSTERR("creating container %s", name);
                goto out2;
        }
index f33f59bfcdd8cfdb4ffd98828b952a8cc28d33bb..879d0a122a5af79b0d7be40be2bcfadbaa7df056 100644 (file)
@@ -50,7 +50,7 @@ int main(int argc, char *argv[])
        }
        c->set_config_item(c, "lxc.network.link", "lxcbr0");
        c->set_config_item(c, "lxc.network.flags", "up");
-       if (!c->createl(c, "ubuntu", NULL, NULL, "-r", "lucid", NULL)) {
+       if (!c->createl(c, "ubuntu", NULL, NULL, 0, "-r", "lucid", NULL)) {
                fprintf(stderr, "%d: failed to create a lucid container\n", __LINE__);
                goto out;
        }
index 95118995c19a8b32312d139815f1a8985ed95eaf..c4c95cbd48bce4ac1dcaeb7999d895108f7a11de 100644 (file)
@@ -170,7 +170,7 @@ int main(int argc, char *argv[])
                ret = 1;
                goto out;
        }
-       if (!c->createl(c, "ubuntu", NULL, NULL, "-r", "lucid", NULL)) {
+       if (!c->createl(c, "ubuntu", NULL, NULL, 0, "-r", "lucid", NULL)) {
                fprintf(stderr, "%d: failed to create a lucid container\n", __LINE__);
                ret = 1;
                goto out;
index 6e50cb09708de0483c79186fd8a133234dc05972..f67995e1d590326c1762ad7c5780ab9cc4ddb67e 100644 (file)
@@ -51,7 +51,7 @@ int main(int argc, char *argv[])
        }
        c->set_config_item(c, "lxc.network.link", "lxcbr0");
        c->set_config_item(c, "lxc.network.flags", "up");
-       if (!c->createl(c, "ubuntu", NULL, NULL, "-r", "lucid", NULL)) {
+       if (!c->createl(c, "ubuntu", NULL, NULL, 0, "-r", "lucid", NULL)) {
                fprintf(stderr, "%d: failed to create a lucid container\n", __LINE__);
                goto out;
        }