Re: [lxc-devel] [PATCH v2] Add lxc-copy executable

2015-11-06 Thread Christian Brauner
On Thu, Nov 05, 2015 at 11:15:08PM +, Serge Hallyn wrote:
> Quoting Christian Brauner (christianvanbrau...@gmail.com):
> > This is a complete reimplementation of lxc-clone and lxc-start-ephemeral.
> > lxc-copy merges the functionalities of lxc-clone + lxc-start-ephemeral into 
> > one
> > executable.
> > 
> > (1) Cloning containers:
> > 
> > (a) as copy:
> > 
> > lxc-copy -n aa -N bb
> > 
> > (b) as snapshot:
> > 
> > lxc-copy -n aa -N bb -s
> > 
> > (2) Renaming containers:
> > 
> > lxc-copy -n aa -N bb -R
> > 
> > (3) Starting ephemeral containers:
> > 
> > (a) start ephemeral container as daemon with random name:
> > 
> > lxc-copy -n aa -e
> > 
> > (b) start ephemeral container in foreground mode with random name:
> > 
> > lxc-copy -n aa -e -F
> > 
> > (c) start ephemeral container with specified name as daemon:
> > 
> > lxc-copy -n aa -N bb
> 
> ^  you didn't add '-e' here.  Is that a typo in this example?
Yes, this is a typo.

> 
> by "as daemon" you mean daemonized (-d) mode right?
Yes, I mean daemonized. The name of the flag was modelled after lxc-start which
also uses [-d|--daemon].

> 
> > (d) start non-ephemeral container as daemon:
> > 
> > lxc-copy -n aa -K
> 
> > (e) start ephemeral container with custom mounts (additional mounts can
> > be of type {bind,aufs,overlay}) as daemon:
> > 
> > lxc-copy -n aa -e -m 
> > bind=/src:/dest:ro,aufs=/src:/dest:rw,overlay=/src:/dest
> 
> I can't quite tell how to distinguish here between a command
> which does or does not start the container.
Sorry, this is a typo in example (d).
The flag [-e|--ephemeral] starts a container.

> 
> How about adding an explicity [-S|--start] without which the
> copy is created but not started?
That is exactly what [-e|--ephemeral] does.

> 
> Is there any meaningful difference between
> 
>   lxc-copy -n aa -e -D -s
> and
>   lxc-copy -n aa -s
> ?
Yes, in the first example the container is also started and [-e|--ephemeral]
implies [-s|--snapshot] anyway.

> 
> Or should we just have '-e: ephemeral, deleted when done' versus
> no -e: copy is persistent.
Yes, this is the current implementation and meaning of [-e|--ephemeral]. The two
examples (c) and (d) in the commit message which confused you are just missing
the [-e|--ephemeral] flag. I'll resend the patch with the examples fixed and
some more notes in the commit message about ephemeral containers.
___
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel


Re: [lxc-devel] [PATCH v2] Add lxc-copy executable

2015-11-05 Thread Serge Hallyn
Quoting Christian Brauner (christianvanbrau...@gmail.com):
> This is a complete reimplementation of lxc-clone and lxc-start-ephemeral.
> lxc-copy merges the functionalities of lxc-clone + lxc-start-ephemeral into 
> one
> executable.
> 
> (1) Cloning containers:
> 
>   (a) as copy:
> 
>   lxc-copy -n aa -N bb
> 
>   (b) as snapshot:
> 
>   lxc-copy -n aa -N bb -s
> 
> (2) Renaming containers:
> 
>   lxc-copy -n aa -N bb -R
> 
> (3) Starting ephemeral containers:
> 
>   (a) start ephemeral container as daemon with random name:
> 
>   lxc-copy -n aa -e
> 
>   (b) start ephemeral container in foreground mode with random name:
> 
>   lxc-copy -n aa -e -F
> 
>   (c) start ephemeral container with specified name as daemon:
> 
>   lxc-copy -n aa -N bb

^  you didn't add '-e' here.  Is that a typo in this example?

by "as daemon" you mean daemonized (-d) mode right?

>   (d) start non-ephemeral container as daemon:
> 
>   lxc-copy -n aa -K

>   (e) start ephemeral container with custom mounts (additional mounts can
> be of type {bind,aufs,overlay}) as daemon:
> 
>   lxc-copy -n aa -e -m 
> bind=/src:/dest:ro,aufs=/src:/dest:rw,overlay=/src:/dest

I can't quite tell how to distinguish here between a command
which does or does not start the container.

How about adding an explicity [-S|--start] without which the
copy is created but not started?

Is there any meaningful difference between

lxc-copy -n aa -e -D -s
and
lxc-copy -n aa -s
?

Or should we just have '-e: ephemeral, deleted when done' versus
no -e: copy is persistent.

-serge
___
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel


[lxc-devel] [PATCH v2] Add lxc-copy executable

2015-11-04 Thread Christian Brauner
Changes v2:
(1) The mkdtemp() call accidently created a directory in the current working
directory of the shell it was called in. This is now fixed by correctly
creating the full path first and then using the offset
strlen(my_args->newpath) + 1 to point my_args->newname to the name of the
container:

ret = snprintf(randname, MAXPATHLEN, "%s/%s_XX", my_args->newpath, 
my_args->name);
if (ret < 0 || ret >= MAXPATHLEN)
return -1;
if (!mkdtemp(randname))
return -1;
my_args->newname = randname + strlen(my_args->newpath) + 1;

Changes in comparison to first implementation:

(1) Instead of using a custom function to generate random names for ephemeral
container we use mkdtemp(). This will be safer and the code considerably
easier.
(2) Make the whole code easily extendable by using a design centered around a
struct and an enum. The current design will allow us to add further mount
types should we want to:
enum mnttype {
LXC_MNT_BIND,
LXC_MNT_AUFS,
LXC_MNT_OVL,
};

struct mnts {
enum mnttype type;
char *src;
char *dest;
char *options;
char *upper;
char *workdir;
char *lower;
};

   (E.g. lowerdir is currently not used but we could in the future allow users
to specify multiple lowerdirs on the command line. All we would have to do
is to extend the corresponding parse function parse_ovl_mnt().)

(3) We use getsubopt() to parse the custom mounts user can specify with the -m
flag and the keys {aufs,bind,overlay}. This will avoid redundancy because
users do not have to specify multiple -m flags but can rather just do -m
bind=/a:/b,overlay/c:/d etc. Furthermore, it will allow us to add further
keys rather easily.

(4) There is a branch lxccopy on github for easier testing:

https://github.com/brauner/lxc/tree/lxccopy

Serge, I wonder how many bugs you will find. :)

Christian Brauner (1):
  Add lxc-copy executable

 src/lxc/Makefile.am |   2 +
 src/lxc/arguments.h |   2 +
 src/lxc/lxc_copy.c  | 744 
 3 files changed, 748 insertions(+)
 create mode 100644 src/lxc/lxc_copy.c

-- 
2.6.2

___
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel


[lxc-devel] [PATCH v2] Add lxc-copy executable

2015-11-04 Thread Christian Brauner
This is a complete reimplementation of lxc-clone and lxc-start-ephemeral.
lxc-copy merges the functionalities of lxc-clone + lxc-start-ephemeral into one
executable.

(1) Cloning containers:

(a) as copy:

lxc-copy -n aa -N bb

(b) as snapshot:

lxc-copy -n aa -N bb -s

(2) Renaming containers:

lxc-copy -n aa -N bb -R

(3) Starting ephemeral containers:

(a) start ephemeral container as daemon with random name:

lxc-copy -n aa -e

(b) start ephemeral container in foreground mode with random name:

lxc-copy -n aa -e -F

(c) start ephemeral container with specified name as daemon:

lxc-copy -n aa -N bb

(d) start non-ephemeral container as daemon:

lxc-copy -n aa -K

(e) start ephemeral container with custom mounts (additional mounts can
be of type {bind,aufs,overlay}) as daemon:

lxc-copy -n aa -e -m 
bind=/src:/dest:ro,aufs=/src:/dest:rw,overlay=/src:/dest

(4) Other options:

lxc-copy --help

In order to create a random containername and random upper- and workdirs for
custom mounts we use mkdtemp() to not just create the names but also directly
create the corresponding directories. This will give us more safety and make
the code considerably shorter.

Signed-off-by: Christian Brauner 
---
 src/lxc/Makefile.am |   2 +
 src/lxc/arguments.h |   2 +
 src/lxc/lxc_copy.c  | 744 
 3 files changed, 748 insertions(+)
 create mode 100644 src/lxc/lxc_copy.c

diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am
index ce46495..72f1e9b 100644
--- a/src/lxc/Makefile.am
+++ b/src/lxc/Makefile.am
@@ -183,6 +183,7 @@ bin_PROGRAMS = \
lxc-cgroup \
lxc-checkpoint \
lxc-clone \
+   lxc-copy \
lxc-config \
lxc-console \
lxc-create \
@@ -226,6 +227,7 @@ init_lxc_SOURCES = lxc_init.c
 lxc_monitor_SOURCES = lxc_monitor.c
 lxc_monitord_SOURCES = lxc_monitord.c
 lxc_clone_SOURCES = lxc_clone.c
+lxc_copy_SOURCES = lxc_copy.c
 lxc_start_SOURCES = lxc_start.c
 lxc_stop_SOURCES = lxc_stop.c
 lxc_top_SOURCES = lxc_top.c
diff --git a/src/lxc/arguments.h b/src/lxc/arguments.h
index bf03dc5..c261935 100644
--- a/src/lxc/arguments.h
+++ b/src/lxc/arguments.h
@@ -100,6 +100,7 @@ struct lxc_arguments {
 
/* lxc-snapshot and lxc-clone */
enum task {
+   CLONE,
DESTROY,
LIST,
RESTORE,
@@ -111,6 +112,7 @@ struct lxc_arguments {
char *newname;
char *newpath;
char *snapname;
+   int keepdata;
int keepname;
int keepmac;
 
diff --git a/src/lxc/lxc_copy.c b/src/lxc/lxc_copy.c
new file mode 100644
index 000..ac1b051
--- /dev/null
+++ b/src/lxc/lxc_copy.c
@@ -0,0 +1,744 @@
+/*
+ *
+ * Copyright © 2015 Christian Brauner .
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#define _GNU_SOURCE
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+#include "attach.h"
+#include "log.h"
+#include "confile.h"
+#include "arguments.h"
+#include "lxc.h"
+#include "conf.h"
+#include "state.h"
+#include "utils.h"
+#include "bdev.h"
+
+lxc_log_define(lxc_copy_ui, lxc);
+
+enum mnttype {
+   LXC_MNT_BIND,
+   LXC_MNT_AUFS,
+   LXC_MNT_OVL,
+};
+
+struct mnts {
+   enum mnttype type;
+   char *src;
+   char *dest;
+   char *options;
+   char *upper;
+   char *workdir;
+   char *lower;
+};
+
+static unsigned int num = 0;
+static struct mnts *mnts = NULL;
+
+static int my_parser(struct lxc_arguments *args, int c, char *arg);
+
+static const struct option my_longopts[] = {
+   { "newname", required_argument, 0, 'N'},
+   { "newpath", required_argument, 0, 'p'},
+   { "rename", no_argument, 0, 'R'},
+   { "snapshot", no_argument, 0, 's'},
+   { "foreground", no_argument, 0, 'F'},
+   { "daemon", no_argument, 0, 'd'},
+   { "ephemeral", no_argument, 0, 'e'},
+   { "mount", required_argument, 0, 'm'},
+   { "backingstore", required_argument, 0, 'B'},
+   { "fssize", required_argument, 0, 'L'},
+   { "keepdata", no_argument, 0, 'D'},
+   { "