Re: [Lxc-users] [PATCH 1/1] Add lxc-clone script

2011-06-22 Thread Serge Hallyn
Actually, perhaps this is better integrated into the templates.

I'm working on consolidating and extending the ubuntu templates into
one, and it looks like maybe it's better to put the cloning stuff
into that.  Though it makes the create command syntax all the more
baroque, which I don't like.  But I'll send out the result and
we'll see how it goes.

thanks,
-serge

--
Simplify data backup and recovery for your virtual environment with vRanger.
Installation's a snap, and flexible recovery options mean your data is safe,
secure and there when you need it. Data protection magic?
Nope - It's vRanger. Get your free trial download today.
http://p.sf.net/sfu/quest-sfdev2dev
___
Lxc-users mailing list
Lxc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/lxc-users


[Lxc-users] [PATCH 1/1] Add lxc-clone script

2011-06-21 Thread Serge E. Hallyn
Create an lxc-clone script to clone containers.  It should probably
be factored into helpers and then enhanced, in particular to convert
between LVM and non-LVM containers, create non-snapshot LVM clones,
support loopback devices, and, when stable enough, to use
overlayfs, btrfs, etc.

But this is a start.

Signed-off-by: Serge Hallyn serge.hal...@ubuntu.com
---
 configure.ac |1 +
 lxc.spec.in  |1 +
 src/lxc/Makefile.am  |1 +
 src/lxc/lxc-clone.in |  208 ++
 4 files changed, 211 insertions(+), 0 deletions(-)
 create mode 100644 src/lxc/lxc-clone.in

diff --git a/configure.ac b/configure.ac
index 6cce1b4..0d4f4c0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -157,6 +157,7 @@ AC_CONFIG_FILES([
src/lxc/lxc-setuid
src/lxc/lxc-version
src/lxc/lxc-create
+   src/lxc/lxc-clone
src/lxc/lxc-destroy
 
 ])
diff --git a/lxc.spec.in b/lxc.spec.in
index 452493d..f22cd06 100644
--- a/lxc.spec.in
+++ b/lxc.spec.in
@@ -78,6 +78,7 @@ rm -rf %{buildroot}
 %{_bindir}/*
 %attr(4111,root,root) %{_bindir}/lxc-attach
 %attr(4111,root,root) %{_bindir}/lxc-create
+%attr(4111,root,root) %{_bindir}/lxc-clone
 %attr(4111,root,root) %{_bindir}/lxc-start
 %attr(4111,root,root) %{_bindir}/lxc-netstat
 %attr(4111,root,root) %{_bindir}/lxc-unshare
diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am
index 9def453..1dfca00 100644
--- a/src/lxc/Makefile.am
+++ b/src/lxc/Makefile.am
@@ -70,6 +70,7 @@ bin_SCRIPTS = \
lxc-setuid \
lxc-version \
lxc-create \
+   lxc-clone \
lxc-destroy
 
 bin_PROGRAMS = \
diff --git a/src/lxc/lxc-clone.in b/src/lxc/lxc-clone.in
new file mode 100644
index 000..91944a0
--- /dev/null
+++ b/src/lxc/lxc-clone.in
@@ -0,0 +1,208 @@
+#!/bin/bash
+
+#
+# lxc: linux Container library
+
+# Authors:
+# Serge Hallyn serge.hal...@ubuntu.com
+# Daniel Lezcano daniel.lezc...@free.fr
+
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+
+# This library 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
+# Lesser General Public License for more details.
+
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+usage() {
+echo usage: lxc-clone -o orig -n new [-s] [-h] [-L fssize] [-v 
vgname]
+}
+
+help() {
+usage
+echo
+echo creates a lxc system object.
+echo
+echo Options:
+echo orig: name of the original container
+echo new : name of the new container
+echo -s  : make the new rootfs a snapshot of the original
+echo fssize  : size if creating a new fs.  By default, 2G
+echo vgname  : lvm volume group name, lxc by default
+}
+
+shortoptions='ho:n:sL:v:'
+longoptions='help,orig:,name:,snapshot,fssize,vgname'
+lxc_path=/var/lib/lxc
+bindir=/usr/bin
+snapshot=no
+lxc_size=2G
+lxc_vg=lxc
+
+getopt=$(getopt -o $shortoptions --longoptions  $longoptions -- $@)
+if [ $? != 0 ]; then
+usage
+exit 1;
+fi
+
+eval set -- $getopt
+
+while true; do
+case $1 in
+   -h|--help)
+   help
+   exit 1
+   ;;
+   -s|--snapshot)
+   shift
+   snapshot=yes
+   ;;
+   -o|--orig)
+   shift
+   lxc_orig=$1
+   shift
+   ;;
+   -L|--fssize)
+   shift
+   lxc_size=$1
+   shift
+   ;;
+   -v|--vgname)
+   shift
+   lxc_vg=$1
+   shift
+   ;;
+   -n|--new)
+   shift
+   lxc_new=$1
+   shift
+   ;;
+--)
+   shift
+   break;;
+*)
+   echo $1
+   usage
+   exit 1
+   ;;
+esac
+done
+
+if [ -z $lxc_path ]; then
+echo no configuration path defined !
+exit 1
+fi
+
+if [ ! -r $lxc_path ]; then
+echo configuration path '$lxc_path' not found
+exit 1
+fi
+
+if [ -z $lxc_orig ]; then
+echo no original container name specified
+usage
+exit 1
+fi
+
+if [ -z $lxc_new ]; then
+echo no new container name specified
+usage
+exit 1
+fi
+
+if [ $(id -u) != 0 ]; then
+   echo This command has to be run as root
+   exit 1
+fi
+
+if [ ! -r $lxc_path ]; then
+echo no configuration path defined !
+exit 1
+fi
+
+if [ ! -d $lxc_path/$lxc_orig ]; then
+echo '$lxc_orig' does not exist
+