Commit 6d8ac56b by Serge Hallyn Committed by Daniel Lezcano

add lvm support to lxc-create

1. Some templates copy the cached pristine rootfs using 'cp a b' where b is $lxc_path/$name/rootfs. That doesn't do the right thing if rootfs already exists, as it will when it is an lvm or other mount. So switch to 'rsync a/ b/'. (cp can be made to work too of course). 2. Update lxc-create to support backing stores. For now only lvm is implemented. Signed-off-by: 's avatarSerge Hallyn <serge.hallyn@canonical.com> Signed-off-by: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com>
parent 76e08ff8
...@@ -42,8 +42,8 @@ help() { ...@@ -42,8 +42,8 @@ help() {
shortoptions='ho:n:sL:v:p:t:' shortoptions='ho:n:sL:v:p:t:'
longoptions='help,orig:,name:,snapshot,fssize:,vgname:,lvprefix:,fstype:' longoptions='help,orig:,name:,snapshot,fssize:,vgname:,lvprefix:,fstype:'
lxc_path=/var/lib/lxc lxc_path=@LXCPATH@
bindir=/usr/bin bindir=@BINDIR@
snapshot=no snapshot=no
lxc_size=2G lxc_size=2G
lxc_vg=lxc lxc_vg=lxc
......
...@@ -21,7 +21,12 @@ ...@@ -21,7 +21,12 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
usage() { usage() {
echo "usage: lxc-create -n <name> [-f configuration] [-t template] [-h] -- [template_options]" echo "usage: lxc-create -n <name> [-f configuration] [-t template] [-h] [fsopts] -- [template_options]"
echo " fsopts: -B none"
echo " fsopts: -B lvm [--lvname lvname] [--vgname vgname] [--fstype fstype] [--fssize fssize]"
# echo " fsopts: -B union [--uniontype overlayfs]"
# echo " fsopts: -B loop [--fstype fstype] [--fssize fssize]"
# echo " fsopts: -B qemu-nbd [--type qed|qcow2|raw] [--fstype fstype] [--fssize fssize] # Qemu qed disk format"
} }
help() { help() {
...@@ -33,6 +38,16 @@ help() { ...@@ -33,6 +38,16 @@ help() {
echo "name : name of the container" echo "name : name of the container"
echo "configuration: lxc configuration" echo "configuration: lxc configuration"
echo "template : lxc-template is an accessible template script" echo "template : lxc-template is an accessible template script"
echo
echo "The container backing store can be altered using '-B'. By default it"
echo "is 'none', which is a simple directory tree under /var/lib/lxc/<name>/rootfs"
echo "Otherwise, the following option values may be relevant:"
echo "lvname : [for -lvm] name of lv in which to create lv,"
echo " container-name by default"
echo "vgname : [for -lvm] name of vg in which to create lv, 'lxc' by default"
echo "fstype : name of filesystem to create, ext4 by default"
echo "fssize : size of filesystem to create, 500M by default"
echo
if [ -z $lxc_template ]; then if [ -z $lxc_template ]; then
echo "for template-specific help, specify a template, for instance:" echo "for template-specific help, specify a template, for instance:"
echo "lxc-create -t ubuntu -h" echo "lxc-create -t ubuntu -h"
...@@ -46,13 +61,17 @@ help() { ...@@ -46,13 +61,17 @@ help() {
fi fi
} }
shortoptions='hn:f:t:' shortoptions='hn:f:t:B:'
longoptions='help,name:,config:,template:' longoptions='help,name:,config:,template:,backingstore:,fstype:,lvname:,vgname:,fssize:'
localstatedir=@LOCALSTATEDIR@ localstatedir=@LOCALSTATEDIR@
lxc_path=@LXCPATH@ lxc_path=@LXCPATH@
bindir=@BINDIR@ bindir=@BINDIR@
libdir=@LIBDIR@ libdir=@LIBDIR@
templatedir=@LXCTEMPLATEDIR@ templatedir=@LXCTEMPLATEDIR@
backingstore=none
fstype=ext4
fssize=500M
vgname=lxc
getopt=$(getopt -o $shortoptions --longoptions $longoptions -- "$@") getopt=$(getopt -o $shortoptions --longoptions $longoptions -- "$@")
if [ $? != 0 ]; then if [ $? != 0 ]; then
...@@ -83,6 +102,31 @@ while true; do ...@@ -83,6 +102,31 @@ while true; do
lxc_template=$1 lxc_template=$1
shift shift
;; ;;
-B|--backingstore)
shift
backingstore=$1
shift
;;
--lvname)
shift
lvname=$1
shift
;;
--vgname)
shift
vgname=$1
shift
;;
--fstype)
shift
fstype=$1
shift
;;
--fssize)
shift
fssize=$1
shift
;;
--) --)
shift shift
break;; break;;
...@@ -110,11 +154,21 @@ if [ -z "$lxc_name" ]; then ...@@ -110,11 +154,21 @@ if [ -z "$lxc_name" ]; then
exit 1 exit 1
fi fi
if [ -z "$lvname" ]; then
lvname="$lxc_name"
fi
if [ "$(id -u)" != "0" ]; then if [ "$(id -u)" != "0" ]; then
echo "This command has to be run as root" echo "This command has to be run as root"
exit 1 exit 1
fi fi
if [ $backingstore != "none" -a $backingstore != "lvm" ]; then
echo "only 'none' and 'lvm' backing stores are known"
usage
exit 1
fi
if [ ! -r $lxc_path ]; then if [ ! -r $lxc_path ]; then
echo "no configuration path defined !" echo "no configuration path defined !"
exit 1 exit 1
...@@ -125,7 +179,47 @@ if [ -d "$lxc_path/$lxc_name" ]; then ...@@ -125,7 +179,47 @@ if [ -d "$lxc_path/$lxc_name" ]; then
exit 1 exit 1
fi fi
trap "${bindir}/lxc-destroy -n $lxc_name; echo aborted; exit 1" SIGHUP SIGINT SIGTERM rootfs="$lxc_path/$lxc_name/rootfs"
if [ $backingstore = "lvm" ]; then
which vgscan > /dev/null
if [ $? -ne 0 ]; then
echo "vgscan not found. Please install lvm2 package"
exit 1
fi
grep -q "\<$fstype\>" /proc/filesystems
if [ $? -ne 0 ]; then
echo "$fstype is not listed in /proc/filesystems"
usage
exit 1
fi
vgscan | grep -q "Found volume group \"$vgname\""
if [ $? -ne 0 ]; then
echo "Could not find volume group \"$vgname\""
usage
exit 1
fi
rootdev=/dev/$vgname/$lvname
lvdisplay $rootdev > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "backing store already exists: $rootdev"
echo "please delete it (using \"lvremove $rootdev\") and try again"
exit 1
fi
fi
cleanup() {
if [ $backingstore = "lvm" ]; then
umount $rootfs
lvremove -f $rootdev
fi
${bindir}/lxc-destroy -n $lxc_name
echo aborted
exit 1
}
trap cleanup SIGHUP SIGINT SIGTERM
mkdir -p $lxc_path/$lxc_name mkdir -p $lxc_path/$lxc_name
...@@ -140,13 +234,21 @@ else ...@@ -140,13 +234,21 @@ else
cp $lxc_config $lxc_path/$lxc_name/config cp $lxc_config $lxc_path/$lxc_name/config
fi fi
# Create the fs as needed
mkdir $rootfs
if [ $backingstore = "lvm" ]; then
lvcreate -L $fssize -n $lxc_name $vgname || exit 1
udevadm settle
mkfs -t $fstype $rootdev || exit 1
mount -t $fstype $rootdev $rootfs
fi
if [ ! -z $lxc_template ]; then if [ ! -z $lxc_template ]; then
type ${templatedir}/lxc-$lxc_template >/dev/null type ${templatedir}/lxc-$lxc_template >/dev/null
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "unknown template '$lxc_template'" echo "unknown template '$lxc_template'"
${bindir}/lxc-destroy -n $lxc_name cleanup
exit 1
fi fi
if [ -z "$lxc_config" ]; then if [ -z "$lxc_config" ]; then
...@@ -185,4 +287,13 @@ if [ ! -z $lxc_template ]; then ...@@ -185,4 +287,13 @@ if [ ! -z $lxc_template ]; then
echo "'$lxc_template' template installed" echo "'$lxc_template' template installed"
fi fi
if [ $backingstore = "lvm" ]; then
echo "Unmounting LVM"
umount $rootfs
# TODO: make the templates set this right from the start?
sed -i '/lxc.rootfs/d' $lxc_path/$lxc_name/config
echo "lxc.rootfs = $rootdev" >> $lxc_path/$lxc_name/config
fi
echo "'$lxc_name' created" echo "'$lxc_name' created"
...@@ -142,7 +142,8 @@ copy_debian() ...@@ -142,7 +142,8 @@ copy_debian()
# make a local copy of the minidebian # make a local copy of the minidebian
echo -n "Copying rootfs to $rootfs..." echo -n "Copying rootfs to $rootfs..."
cp -a "$cache/rootfs-$SUITE-$arch" $rootfs || return 1 mkdir -p $rootfs
rsync -a "$cache/rootfs-$SUITE-$arch"/ $rootfs/ || return 1
return 0 return 0
} }
......
...@@ -206,7 +206,8 @@ copy_opensuse() ...@@ -206,7 +206,8 @@ copy_opensuse()
# make a local copy of the mini opensuse # make a local copy of the mini opensuse
echo -n "Copying rootfs to $rootfs ..." echo -n "Copying rootfs to $rootfs ..."
cp -a $cache/rootfs-$arch $rootfs || return 1 mkdir -p $rootfs
rsync -a $cache/rootfs-$arch/ $rootfs/ || return 1
return 0 return 0
} }
......
...@@ -6,8 +6,6 @@ ...@@ -6,8 +6,6 @@
# This script consolidates and extends the existing lxc ubuntu scripts # This script consolidates and extends the existing lxc ubuntu scripts
# #
# XXX todo: add -lvm option
# Copyright 2011 Serge Hallyn <serge.hallyn@canonical.com> # Copyright 2011 Serge Hallyn <serge.hallyn@canonical.com>
# Copyright 2010 Wilhelm Meier # Copyright 2010 Wilhelm Meier
# Author: Wilhelm Meier <wilhelm.meier@fh-kl.de> # Author: Wilhelm Meier <wilhelm.meier@fh-kl.de>
...@@ -195,7 +193,8 @@ copy_ubuntu() ...@@ -195,7 +193,8 @@ copy_ubuntu()
# make a local copy of the miniubuntu # make a local copy of the miniubuntu
echo -n "Copying rootfs to $rootfs ..." echo -n "Copying rootfs to $rootfs ..."
cp -a $cache/rootfs-$arch $rootfs || return 1 mkdir -p $rootfs
rsync -a $cache/rootfs-$arch/ $rootfs/ || return 1
return 0 return 0
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment