[Lxc-users] [RFC] best way to add creation of lvm containers

Serge E. Hallyn serge at hallyn.com
Fri Jul 1 17:31:05 UTC 2011


Hey,

so lxc-clone will create a snapshot-based clone of an lvm-backed
container in about a second.  Creating the first lvm-backed
container is a bit of a pain though.  I do it using the script
below, called 'lxclvmconvert' on my machine.  So I do

   lxc-create -t ubuntu -f /etc/lxc.conf -n mavbase -- -r maverick
   lxclvmconvert mavbase

and from then on I can do fast

   lxc-clone -s -o mavbase -n mav-bugxyz

My question is, where do we want to put this functionality?  Of course
I *can* put it in the ubuntu template itself, but I'm leary of adding
too many options to that.  Consider that just for the lvm support we'd
need to add optional arguments for:

   backing store type:  (lvm, loopback file, real blockdev)
   backing store fstype
   backing store size
   other options, i.e. lvm volume group name

So, do you think it would be better for the container creation templates
to offer this support, or to have a separate tool, not lxclvmconvert, but
maybe 'lxc-convert', which converts a container from any supported backing
type to any other.  Backing types I guess could start out by including

   directory (the current way)
   lvm
   loopback file
   raw device

thanks,
-serge

#!/bin/sh

if [ $# -lt 1 ]; then
	echo "Usage: $0 container-name <size> <fstype>"
	exit 1
fi

c=$1
size=2G
fstype=ext3
echo "converting container $c"

if [ $# -gt 1 ]; then
	size=$2
	echo "Using size $size"
fi

if [ $# -gt 2 ]; then
	fstype=$3
	echo "Using fstype $fstype"
fi

if [ ! -d /var/lib/lxc/$c/rootfs ]; then
	echo "Container $c doesn't seem to exist?"
	exit 1
fi

if [ -e /dev/lxc/$c ]; then
	echo "/dev/lxc/$c already exists.  Bailing"
	exit 1
fi

lvcreate -L $size -n $c lxc || cleanup
sleep 1
mkfs -t $fstype /dev/lxc/$c
if [ $? -ne 0 ]; then
	echo "Failed to create the filesystem"
	lvremove -f /dev/lxc/$c
	exit 1
fi

mkdir /var/lib/lxc/$c/lvm || { lvremove -f /dev/lxc/$c; exit 1; }
mount -t $fstype /dev/lxc/$c /var/lib/lxc/$c/lvm || { lvremove -f /dev/lxc/$c; exit 1; }
rsync -va /var/lib/lxc/$c/rootfs/ /var/lib/lxc/$c/lvm || echo "Rsync had errors, you may want to check;  continuing"
umount /var/lib/lxc/$c/lvm
rmdir /var/lib/lxc/$c/lvm
rm -rf /var/lib/lxc/$c/rootfs
mkdir /var/lib/lxc/$c/rootfs

sed -i '/lxc.rootfs/d' /var/lib/lxc/$c/config
echo "lxc.rootfs = /dev/lxc/$c" >> /var/lib/lxc/$c/config

echo "Finished"





More information about the lxc-users mailing list