]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
Introduce --lxcpath cmdline option, and make default_lxc_path() return const char *
authorStéphane Graber <stgraber@ubuntu.com>
Tue, 19 Feb 2013 16:48:56 +0000 (11:48 -0500)
committerStéphane Graber <stgraber@ubuntu.com>
Tue, 19 Feb 2013 16:52:44 +0000 (11:52 -0500)
For the lxc-* C binaries, introduce a -P|--lxcpath command line option
to override the system default.

With this, I can

    lxc-create -t ubuntu -n r1
    lxc-create -t ubuntu -n r1 -P /home/ubuntu/lxcbase
    lxc-start -n r1 -d
    lxc-start -n r1 -d -P /home/ubuntu/lxcbase
    lxc-console -n r1 -d -P /home/ubuntu/lxcbase
    lxc-stop -n r1

all working with the right containers (module cgroup stuff).

To do:
    * lxc monitor needs to be made to handle cgroups.
      This is another very invasive one.  I started doing this as
      a part of this set, but that gets hairy, so I'm sending this
      separately.  Note that lxc-wait and lxc-monitor don't work
      without this, and there may be niggles in what I said works
      above - since start.c is doing lxc_monitor_send_state etc
      to the shared abstract unix domain socket.
    * Need to handle the cgroup conflicts.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
24 files changed:
doc/common_options.sgml.in
src/lua-lxc/core.c
src/lxc/arguments.c
src/lxc/arguments.h
src/lxc/commands.c
src/lxc/conf.c
src/lxc/lxc-create.in
src/lxc/lxc.h
src/lxc/lxc_attach.c
src/lxc/lxc_console.c
src/lxc/lxc_execute.c
src/lxc/lxc_info.c
src/lxc/lxc_kill.c
src/lxc/lxc_restart.c
src/lxc/lxc_start.c
src/lxc/lxc_stop.c
src/lxc/lxc_wait.c
src/lxc/lxccontainer.c
src/lxc/lxccontainer.h
src/lxc/restart.c
src/lxc/state.c
src/lxc/state.h
src/lxc/utils.c
src/lxc/utils.h

index 32e89ce8a37425994f7f8f9f4461722ac03fbd50..f89419f09fd5660973f66739520962d89a350f9a 100644 (file)
@@ -57,6 +57,15 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
       </listitem>
     </varlistentry>
 
+    <varlistentry>
+      <term><option>-P, --lxcpath=<replaceable>PATH</replaceable></option></term>
+      <listitem>
+       <para>
+         Use an alternate container path.  The default is @LXCPATH@.
+       </para>
+      </listitem>
+    </varlistentry>
+
     <varlistentry>
       <term><option>-o, --logfile=<replaceable>FILE</replaceable></option></term>
       <listitem>
index c9eaef02e3e13a17e10fb9b47eaf2b6b9aadb052..364178685319d874c18171725b5bba7335f08b9c 100644 (file)
@@ -366,10 +366,9 @@ static int lxc_version_get(lua_State *L) {
 }
 
 static int lxc_default_config_path_get(lua_State *L) {
-    char *lxcpath = lxc_get_default_config_path();
+    const char *lxcpath = lxc_get_default_config_path();
 
     lua_pushstring(L, lxcpath);
-    free(lxcpath);
     return 1;
 }
 
index 76d8366231e485667859ad5e1b83de303a47874d..ceafb5256a1c4f9a509376ec73418a75259a001a 100644 (file)
@@ -32,6 +32,7 @@
 #include <unistd.h>
 
 #include "arguments.h"
+#include "utils.h"
 
 /*---------------------------------------------------------------------------*/
 static int build_shortopts(const struct option *a_options,
@@ -136,6 +137,7 @@ Common options :\n\
   -o, --logfile=FILE               Output log to FILE instead of stderr\n\
   -l, --logpriority=LEVEL          Set log priority to LEVEL\n\
   -q, --quiet                      Don't produce any output\n\
+  -P, --lxcpath=PATH               Use specified container path\n\
   -?, --help                       Give this help list\n\
       --usage                      Give a short usage message\n\
 \n\
@@ -154,6 +156,7 @@ extern int lxc_arguments_parse(struct lxc_arguments *args,
        char shortopts[256];
        int  ret = 0;
 
+       args->lxcpath = default_lxc_path();
        ret = build_shortopts(args->options, shortopts, sizeof(shortopts));
        if (ret < 0) {
                lxc_error(args, "build_shortopts() failed : %s",
@@ -173,6 +176,7 @@ extern int lxc_arguments_parse(struct lxc_arguments *args,
                case 'l':       args->log_priority = optarg; break;
                case 'c':       args->console = optarg; break;
                case 'q':       args->quiet = 1; break;
+               case 'P':       args->lxcpath = optarg; break;
                case OPT_USAGE: print_usage(args->options, args);
                case '?':       print_help(args, 1);
                case 'h':       print_help(args, 0);
index 5f2ecba5eb6ff37c7ab29afcb3f391983ede54ac..69e7b00559047ac75dec1913c5bd450040726eb9 100644 (file)
@@ -47,6 +47,7 @@ struct lxc_arguments {
        const char *console;
        const char *console_log;
        const char *pidfile;
+       const char *lxcpath;
 
        /* for lxc-checkpoint/restart */
        const char *statefile;
@@ -79,6 +80,7 @@ struct lxc_arguments {
        {"quiet", no_argument,  0, 'q'}, \
        {"logfile", required_argument, 0, 'o'}, \
        {"logpriority", required_argument, 0, 'l'}, \
+       {"lxcpath", required_argument, 0, 'P'}, \
        {0, 0, 0, 0}
 
 /* option keys for long only options */
index 2776f032fc304a4f7227d5236f69d26ee0923b11..83e7df1b2a18a9d0ae2d8dd71fb83b0763afcb8e 100644 (file)
@@ -62,7 +62,7 @@ lxc_log_define(lxc_commands, lxc);
 static int fill_sock_name(char *path, int len, const char *name,
                          const char *inpath)
 {
-       char *lxcpath = NULL;
+       const char *lxcpath = NULL;
        int ret;
 
        if (!inpath) {
@@ -73,8 +73,6 @@ static int fill_sock_name(char *path, int len, const char *name,
                }
        }
        ret = snprintf(path, len, "%s/%s/command", lxcpath ? lxcpath : inpath, name);
-       if (lxcpath)
-               free(lxcpath);
 
        if (ret < 0 || ret >= len) {
                ERROR("Name too long");
index bb93189c29cbaacb87a07ef846f6081dbf4173a3..04ab8b86de16aed80bb4d684e0683b4633285ae7 100644 (file)
@@ -1519,7 +1519,7 @@ static int mount_entry_on_absolute_rootfs(struct mntent *mntent,
        unsigned long mntflags;
        char *mntdata;
        int r, ret = 0, offset;
-       char *lxcpath;
+       const char *lxcpath;
 
        if (parse_mntopts(mntent->mnt_opts, &mntflags, &mntdata) < 0) {
                ERROR("failed to parse mount option '%s'", mntent->mnt_opts);
@@ -1535,7 +1535,6 @@ static int mount_entry_on_absolute_rootfs(struct mntent *mntent,
        /* if rootfs->path is a blockdev path, allow container fstab to
         * use $lxcpath/CN/rootfs as the target prefix */
        r = snprintf(path, MAXPATHLEN, "%s/%s/rootfs", lxcpath, lxc_name);
-       free(lxcpath);
        if (r < 0 || r >= MAXPATHLEN)
                goto skipvarlib;
 
index 26ca5800d2f2d653ecd47ade8ed9df6361b7016f..fec551160a0b0d5b5e304c609934cd7f30576ad0 100644 (file)
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
+. @DATADIR@/lxc/lxc.functions
+
 usage() {
     echo "usage: $(basename $0) -n NAME [-f CONFIG_FILE] [-t TEMPLATE] [FS_OPTIONS] --" >&2
-    echo "         [TEMPLATE_OPTIONS]" >&2
+    echo "         [-P lxcpath] [TEMPLATE_OPTIONS]" >&2
     echo >&2
     echo "where FS_OPTIONS is one of:" >&2
     echo "  -B none" >&2
@@ -42,6 +44,7 @@ help() {
     echo "  -f CONFIG_FILE     use an existing configuration file" >&2
     echo "  -t TEMPLATE        use an accessible template script" >&2
     echo "  -B BACKING_STORE   alter the container backing store (default: none)" >&2
+    echo "  --lxcpath path     specify an alternate container patch (default: $lxc_path)" >&2
     echo "  --lvname LV_NAME   specify the LVM logical volume name" >&2
     echo "                      (default: container name)" >&2
     echo "  --dir ROOTFS_DIR   specify path for custom rootfs directory location" >&2
@@ -73,7 +76,6 @@ optarg_check() {
     fi
 }
 
-. @DATADIR@/lxc/lxc.functions
 backingstore=_unset
 fstype=ext4
 fssize=500M
@@ -98,6 +100,11 @@ while [ $# -gt 0 ]; do
             lxc_config=$1
             shift
             ;;
+        -P|--lxcpath)
+            optarg_check $opt "$1"
+            lxc_path=$1
+            shift
+            ;;
         -t|--template)
             optarg_check $opt "$1"
             lxc_template=$1
index 0e1ce632f9cc55d53fc8e84327c873c4bd8686a2..4e770a620183a5f916ba5e4157bd72a0d8443858 100644 (file)
@@ -171,9 +171,10 @@ extern int lxc_checkpoint(const char *name, int sfd, int flags);
  * @sfd: fd from which the container is restarted
  * @conf: lxc_conf structure.
  * @flags : restart flags (an ORed value)
+ * @lxcpath: container path
  * Returns 0 on success, < 0 otherwise
  */
-extern int lxc_restart(const char *, int, struct lxc_conf *, int);
+extern int lxc_restart(const char *, int, struct lxc_conf *, int, const char *);
 
 /*
  * Returns the version number of the library
index b4ccf44fd4978f9d71d25a2e598647b5da5f0ce7..370bfbc4a50abe0da24922fd41fc77c3a6689866 100644 (file)
@@ -130,8 +130,6 @@ int main(int argc, char *argv[])
        void *cgroup_data = NULL;
        uid_t uid;
        char *curdir;
-       /* TODO: add cmdline arg to set lxcpath */
-       const char *lxcpath = NULL;
 
        ret = lxc_caps_init();
        if (ret)
@@ -146,7 +144,7 @@ int main(int argc, char *argv[])
        if (ret)
                return ret;
 
-       init_pid = get_init_pid(my_args.name, lxcpath);
+       init_pid = get_init_pid(my_args.name, my_args.lxcpath);
        if (init_pid < 0) {
                ERROR("failed to get the init pid");
                return -1;
@@ -176,7 +174,7 @@ int main(int argc, char *argv[])
         * by asking lxc-start
         */
        if (namespace_flags == -1) {
-               namespace_flags = lxc_get_clone_flags(my_args.name, lxcpath);
+               namespace_flags = lxc_get_clone_flags(my_args.name, my_args.lxcpath);
                /* call failed */
                if (namespace_flags == -1) {
                        ERROR("failed to automatically determine the "
index 8ff3f5ac3adcd6482bac55d1d66c3be30bbb113f..5f2ef0fb8c81e20a5df9b4e8d3c04a3da5ff6256 100644 (file)
@@ -182,8 +182,6 @@ int main(int argc, char *argv[])
        int err, std_in = 1;
        struct lxc_epoll_descr descr;
        struct termios newtios, oldtios;
-       /* TODO: add cmdline arg to specify lxcpath */
-       char *lxcpath = NULL;
 
        err = lxc_arguments_parse(&my_args, argc, argv);
        if (err)
@@ -200,7 +198,7 @@ int main(int argc, char *argv[])
                return -1;
        }
 
-       err = lxc_console(my_args.name, my_args.ttynum, &master, lxcpath);
+       err = lxc_console(my_args.name, my_args.ttynum, &master, my_args.lxcpath);
        if (err)
                goto out;
 
index 3c76e2e93c66c6e0d95d2b6c6f4a072e781cf78c..23631d8496733e4b4160bf753801ffde03b841de 100644 (file)
@@ -109,14 +109,8 @@ int main(int argc, char *argv[])
                rcfile = (char *)my_args.rcfile;
        else {
                int rc;
-               char *lxcpath = default_lxc_path();
-               if (!lxcpath) {
-                       ERROR("Out of memory");
-                       return -1;
-               }
 
-               rc = asprintf(&rcfile, "%s/%s/config", lxcpath, my_args.name);
-               free(lxcpath);
+               rc = asprintf(&rcfile, "%s/%s/config", my_args.lxcpath, my_args.name);
                if (rc == -1) {
                        SYSERROR("failed to allocate memory");
                        return -1;
@@ -143,5 +137,5 @@ int main(int argc, char *argv[])
        if (lxc_config_define_load(&defines, conf))
                return -1;
 
-       return lxc_execute(my_args.name, my_args.argv, my_args.quiet, conf, NULL);
+       return lxc_execute(my_args.name, my_args.argv, my_args.quiet, conf, my_args.lxcpath);
 }
index fb37b2f1ba95785d8a2b86837890c95068bd99b4..3b1b63f13388a08c40beef7455b0a972e76d552a 100644 (file)
@@ -74,8 +74,6 @@ Options :\n\
 int main(int argc, char *argv[])
 {
        int ret;
-       /* TODO: add lxcpath cmdline arg */
-       const char *lxcpath = NULL;
 
        ret = lxc_arguments_parse(&my_args, argc, argv);
        if (ret)
@@ -89,7 +87,7 @@ int main(int argc, char *argv[])
                state = pid = true;
 
        if (state || test_state) {
-               ret = lxc_getstate(my_args.name, lxcpath);
+               ret = lxc_getstate(my_args.name, my_args.lxcpath);
                if (ret < 0)
                        return 1;
                if (test_state)
@@ -99,7 +97,7 @@ int main(int argc, char *argv[])
        }
 
        if (pid)
-               printf("pid:%10d\n", get_init_pid(my_args.name, lxcpath));
+               printf("pid:%10d\n", get_init_pid(my_args.name, my_args.lxcpath));
 
        return 0;
 }
index 669f469ffb6d27e7b560d953b4e6f607325aeeab..40b3eaa6a4a16d2b9773a811956907d43a064592 100644 (file)
@@ -56,8 +56,6 @@ int main(int argc, char *argv[], char *envp[])
        int ret;
        pid_t pid;
        int sig;
-       /* TODO: add lxcpath cmdline arg */
-       const char *lxcpath = NULL;
 
        ret = lxc_arguments_parse(&my_args, argc, argv);
        if (ret)
@@ -78,7 +76,7 @@ int main(int argc, char *argv[], char *envp[])
        } else
                sig=SIGKILL;
 
-       pid = get_init_pid(my_args.name, lxcpath);
+       pid = get_init_pid(my_args.name, my_args.lxcpath);
        if (pid < 0) {
                ERROR("failed to get the init pid");
                return -1;
index 7561b1b4f702efb01f1e00877a75bace5cf7d022..55e7c6e5cb483306357f24c32dbd8c10c73fbb7f 100644 (file)
@@ -132,14 +132,8 @@ int main(int argc, char *argv[])
                rcfile = (char *)my_args.rcfile;
        else {
                int rc;
-               char *lxcpath = default_lxc_path();
-               if (!lxcpath) {
-                       ERROR("Out of memory");
-                       return -1;
-               }
 
-               rc = asprintf(&rcfile, "%s/%s/config", lxcpath, my_args.name);
-               free(lxcpath);
+               rc = asprintf(&rcfile, "%s/%s/config", my_args.lxcpath, my_args.name);
                if (rc == -1) {
                        SYSERROR("failed to allocate memory");
                        return -1;
@@ -178,7 +172,7 @@ int main(int argc, char *argv[])
                }
        }
 
-       ret = lxc_restart(my_args.name, sfd, conf, my_args.flags);
+       ret = lxc_restart(my_args.name, sfd, conf, my_args.flags, my_args.lxcpath);
 
        if (my_args.statefile)
                close(sfd);
index 9d8db63c5a84a92cfaefaca2350423271547a875..d78f4b7a485bfe69fbf1b3fc409f14746ed3ab9f 100644 (file)
@@ -150,8 +150,6 @@ int main(int argc, char *argv[])
                '\0',
        };
        FILE *pid_fp = NULL;
-       /* TODO: add cmdline arg to specify lxcpath */
-       char *lxcpath = NULL;
 
        lxc_list_init(&defines);
 
@@ -175,14 +173,8 @@ int main(int argc, char *argv[])
                rcfile = (char *)my_args.rcfile;
        else {
                int rc;
-               char *lxcpath = default_lxc_path();
-               if (!lxcpath) {
-                       ERROR("Out of memory");
-                       return -1;
-               }
 
-               rc = asprintf(&rcfile, "%s/%s/config", lxcpath, my_args.name);
-               free(lxcpath);
+               rc = asprintf(&rcfile, "%s/%s/config", my_args.lxcpath, my_args.name);
                if (rc == -1) {
                        SYSERROR("failed to allocate memory");
                        return err;
@@ -260,7 +252,7 @@ int main(int argc, char *argv[])
        if (my_args.close_all_fds)
                conf->close_all_fds = 1;
 
-       err = lxc_start(my_args.name, args, conf, lxcpath);
+       err = lxc_start(my_args.name, args, conf, my_args.lxcpath);
 
        /*
         * exec ourself, that requires to have all opened fd
index 703726a2d1488f362ff8146ab322ebf01079a1e3..4682d16ce9c10aa5117d6fb09df595494be9a7c6 100644 (file)
@@ -29,6 +29,7 @@
 #include <lxc/log.h>
 
 #include "arguments.h"
+#include "utils.h"
 
 static const struct option my_longopts[] = {
        LXC_COMMON_OPTIONS
@@ -50,9 +51,6 @@ Options :\n\
 
 int main(int argc, char *argv[])
 {
-       /* TODO - make lxcpath a cmdline arg */
-       const char *lxcpath = NULL;
-
        if (lxc_arguments_parse(&my_args, argc, argv))
                return -1;
 
@@ -60,5 +58,5 @@ int main(int argc, char *argv[])
                         my_args.progname, my_args.quiet))
                return -1;
 
-       return lxc_stop(my_args.name, lxcpath);
+       return lxc_stop(my_args.name, my_args.lxcpath);
 }
index b0643c73946ad7c1018687013e5398943631f773..c2a2fb4f204ff8a97572fe0b4f715f5bfa0a3aa7 100644 (file)
@@ -87,5 +87,5 @@ int main(int argc, char *argv[])
                         my_args.progname, my_args.quiet))
                return -1;
 
-       return lxc_wait(strdup(my_args.name), my_args.states, my_args.timeout);
+       return lxc_wait(strdup(my_args.name), my_args.states, my_args.timeout, my_args.lxcpath);
 }
index 36330478406837833145a6430a77217c38921090..1e257c0b9cc95f5df239021140922142e073e7a2 100644 (file)
@@ -272,7 +272,7 @@ static bool lxcapi_wait(struct lxc_container *c, const char *state, int timeout)
        if (!c)
                return false;
 
-       ret = lxc_wait(c->name, state, timeout);
+       ret = lxc_wait(c->name, state, timeout, c->config_path);
        return ret == 0;
 }
 
@@ -987,7 +987,7 @@ out:
        return ret;
 }
 
-char *lxc_get_default_config_path(void)
+const char *lxc_get_default_config_path(void)
 {
        return default_lxc_path();
 }
@@ -1006,7 +1006,7 @@ struct lxc_container *lxc_container_new(const char *name, const char *configpath
        if (configpath)
                c->config_path = strdup(configpath);
        else
-               c->config_path = default_lxc_path();
+               c->config_path = strdup(default_lxc_path());
 
        if (!c->config_path) {
                fprintf(stderr, "Out of memory");
index 46c46c5499cace6033e4380f36219bdb78730a47..f0c45651bde200848f4108c8d5b7a99f5d331c0f 100644 (file)
@@ -86,7 +86,7 @@ struct lxc_container *lxc_container_new(const char *name, const char *configpath
 int lxc_container_get(struct lxc_container *c);
 int lxc_container_put(struct lxc_container *c);
 int lxc_get_wait_states(const char **states);
-char *lxc_get_default_config_path(void);
+const char *lxc_get_default_config_path(void);
 
 #if 0
 char ** lxc_get_valid_keys();
index d0b8fa8d4b7fe4dc206d74a972981d0e42320854..03ae384e42b050dd4ce66a9d380026cbbb9ab492 100644 (file)
@@ -64,14 +64,13 @@ static struct lxc_operations restart_ops = {
        .post_start = post_restart
 };
 
-int lxc_restart(const char *name, int sfd, struct lxc_conf *conf, int flags)
+int lxc_restart(const char *name, int sfd, struct lxc_conf *conf, int flags,
+               const char *lxcpath)
 {
        struct restart_args restart_arg = {
                .sfd = sfd,
                .flags = flags
        };
-       /* TODO - make lxcpath a cmdline arg */
-       const char *lxcpath = NULL;
 
        if (lxc_check_inherited(conf, sfd))
                return -1;
index 8552522dd673ff5103c00ccae110cb78bdb0b0c9..7d6c0d55cdc8a1d78948ee515ea82fec9e68dd07 100644 (file)
@@ -191,13 +191,11 @@ static int fillwaitedstates(const char *strstates, int *states)
        return 0;
 }
 
-extern int lxc_wait(const char *lxcname, const char *states, int timeout)
+extern int lxc_wait(const char *lxcname, const char *states, int timeout, const char *lxcpath)
 {
        struct lxc_msg msg;
        int state, ret;
        int s[MAX_STATE] = { }, fd;
-       /* TODO: add cmdline arg to specify lxcpath */
-       char *lxcpath = NULL;
 
        if (fillwaitedstates(states, s))
                return -1;
index c995e55a8139e4087f4de8cacfa6bd7b55a00c89..beeed1883e33e19b14386b52e0cc20815fc3be9b 100644 (file)
@@ -33,6 +33,6 @@ extern lxc_state_t lxc_getstate(const char *name, const char *lxcpath);
 
 extern lxc_state_t lxc_str2state(const char *state);
 extern const char *lxc_state2str(lxc_state_t state);
-extern int lxc_wait(const char *lxcname, const char *states, int timeout);
+extern int lxc_wait(const char *lxcname, const char *states, int timeout, const char *lxcpath);
 
 #endif
index b9e6ffc053e44e00fb313d3729ac2fadb7ef5cdf..e38acab9f3fad6535b3da858e8cbf340b44cfba9 100644 (file)
@@ -212,11 +212,16 @@ static char *copypath(char *p)
        return retbuf;
 }
 
-char *default_lxc_path(void)
+char *default_lxcpath;
+
+const char *default_lxc_path(void)
 {
-       char buf[1024], *p, *retbuf;
+       char buf[1024], *p;
        FILE *fin;
 
+       if (default_lxcpath)
+               return default_lxcpath;
+
        fin = fopen(LXC_GLOBAL_CONF, "r");
        if (fin) {
                while (fgets(buf, 1024, fin)) {
@@ -232,20 +237,16 @@ char *default_lxc_path(void)
                        while (*p && (*p == ' ' || *p == '\t')) p++;
                        if (!*p)
                                continue;
-                       retbuf = copypath(p);
+                       default_lxcpath = copypath(p);
                        goto out;
                }
        }
        /* we couldn't open the file, or didn't find a lxcpath
         * entry there.  Return @LXCPATH@ */
-       retbuf = malloc(strlen(LXCPATH)+1);
-       if (!retbuf)
-               goto out;
-       strcpy(retbuf, LXCPATH);
+       default_lxcpath = LXCPATH;
 
 out:
        if (fin)
                fclose(fin);
-       INFO("returning %s", (retbuf ? retbuf : "null"));
-       return retbuf;
+       return default_lxcpath;
 }
index b24c8fac8613204365bcd166db37a70e524b97c0..a6bf0677dc514bc207c73cd838964bb3b4c09986 100644 (file)
@@ -31,6 +31,6 @@ extern int mkdir_p(const char *dir, mode_t mode);
  * Return a newly allocated buffer containing the default container
  * path.  Caller must free this buffer.
  */
-extern char *default_lxc_path(void);
+extern const char *default_lxc_path(void);
 
 #endif