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

2015-11-09 Thread Christian Brauner
On Mon, Nov 09, 2015 at 07:55:31PM +, 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.
> > 
> > (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:
> > 
> > Ephemeral containers are created and started by passing the flag -e /
> > --ephemeral. Whenever this flag is missing a copy of the container is 
> > created.
> > The flag -e / --ephemeral implies -s / --snapshot.
> > 
> > (a) start ephemeral container daemonized 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 in daemonized mode:
> > Analogous to lxc-start ephemeral containers start in daemonized
> > mode per default:
> > 
> > lxc-copy -n aa -N bb -e
> > 
> > One can however also explicitly pass -d / --daemon:
> > 
> > lxc-copy -n aa -N bb -e -d
> > 
> > but both commands are equivalent.
> > 
> > (d) start non-ephemeral container in daemonized mode:
> > 
> > lxc-copy -n aa -D -e
> > 
> > (e) start ephemeral container in daemonized mode and keep the original
> > hostname:
> > 
> > lxc-copy -n aa -K -e
> > 
> > (f) start ephemeral container in daemonized mode and keep the
> > MAC-address of the original container:
> > 
> > lxc-copy -n aa -M -e
> > 
> > (g) start ephemeral container with custom mounts (additional mounts can
> > be of type {bind,aufs,overlay}) in daemonized mode:
> > 
> > lxc-copy -n aa -e -m 
> > bind=/src:/dest:ro,aufs=/src:/dest,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 be safer 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 

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

2015-11-09 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.
> 
> (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:
> 
> Ephemeral containers are created and started by passing the flag -e /
> --ephemeral. Whenever this flag is missing a copy of the container is created.
> The flag -e / --ephemeral implies -s / --snapshot.
> 
>   (a) start ephemeral container daemonized 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 in daemonized mode:
>   Analogous to lxc-start ephemeral containers start in daemonized
> mode per default:
> 
>   lxc-copy -n aa -N bb -e
> 
>   One can however also explicitly pass -d / --daemon:
> 
>   lxc-copy -n aa -N bb -e -d
> 
>   but both commands are equivalent.
> 
>   (d) start non-ephemeral container in daemonized mode:
> 
>   lxc-copy -n aa -D -e
> 
>   (e) start ephemeral container in daemonized mode and keep the original
>   hostname:
> 
>   lxc-copy -n aa -K -e
> 
>   (f) start ephemeral container in daemonized mode and keep the
>   MAC-address of the original container:
> 
>   lxc-copy -n aa -M -e
> 
>   (g) start ephemeral container with custom mounts (additional mounts can
> be of type {bind,aufs,overlay}) in daemonized mode:
> 
>   lxc-copy -n aa -e -m 
> bind=/src:/dest:ro,aufs=/src:/dest,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 be safer 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