From 81c5933f62c85f2b09288393b44f831a1debfdee Mon Sep 17 00:00:00 2001
From: Alexey Shabalin <shaba@altlinux.org>
Date: Tue, 23 Aug 2011 17:35:23 +0400
Subject: [PATCH] - Change --with-config-path in configure for define path for
 config files. - Add --with-container-path to configure for
 define path for containers repository. Allow install config
 files for containers to /etc/lxc dir. For old way you can
 use same --with-config-path and --with-container-path.

---
 configure.ac              |   17 +++++++++++++----
 src/lxc/lxc-clone.in      |   30 ++++++++++++++++--------------
 src/lxc/lxc-create.in     |    8 +++++---
 src/lxc/lxc-destroy.in    |    6 ++++--
 src/lxc/lxc_execute.c     |    2 +-
 src/lxc/lxc_restart.c     |    2 +-
 src/lxc/lxc_start.c       |    2 +-
 templates/lxc-busybox.in  |   11 ++++++-----
 templates/lxc-debian.in   |    7 ++++---
 templates/lxc-fedora.in   |    6 ++++--
 templates/lxc-lenny.in    |    7 ++++---
 templates/lxc-opensuse.in |   11 ++++++-----
 templates/lxc-sshd.in     |    9 +++++----
 templates/lxc-ubuntu.in   |   11 ++++++-----
 14 files changed, 76 insertions(+), 53 deletions(-)

diff --git a/configure.ac b/configure.ac
index 56c5f98..8b53060 100644
--- a/configure.ac
+++ b/configure.ac
@@ -43,8 +43,14 @@ AS_AC_EXPAND(DOCDIR, $docdir)
 AC_ARG_WITH([config-path],
 	[AC_HELP_STRING(
 		[--with-config-path=dir],
-		[lxc configuration repository path]
-	)], [], [with_config_path="${localstatedir}/lib/lxc"])
+		[lxc configuration files path]
+	)], [], [with_config_path="${sysconfdir}/lxc"])
+
+AC_ARG_WITH([container-path],
+	[AC_HELP_STRING(
+		[--with-container-path=dir],
+		[lxc containers repository path]
+	)], [], [with_container_path="${localstatedir}/lib/lxc"])
 
 AC_ARG_WITH([rootfs-path],
 	[AC_HELP_STRING(
@@ -54,13 +60,16 @@ AC_ARG_WITH([rootfs-path],
 
 AS_AC_EXPAND(LXC_GENERATE_DATE, "$(date)")
 
-AS_AC_EXPAND(LXCPATH, "${with_config_path}")
+AS_AC_EXPAND(LXCETCPATH, "${with_config_path}")
+AS_AC_EXPAND(LXCPATH, "${with_container_path}")
 AS_AC_EXPAND(LXCROOTFSMOUNT, "${with_rootfs_path}")
 AS_AC_EXPAND(LXCINITDIR, $libdir/lxc)
 AS_AC_EXPAND(LXCTEMPLATEDIR, $libdir/lxc/templates)
-AH_TEMPLATE([LXCPATH], [lxc configuration repository])
+AH_TEMPLATE([LXCETCPATH], [lxc configuration files])
+AH_TEMPLATE([LXCPATH], [lxc containers repository])
 AH_TEMPLATE([LXCINITDIR], [lxc-init directory location])
 AH_TEMPLATE([LXCROOTFSMOUNT], [lxc default rootfs mount point])
+AC_DEFINE_UNQUOTED(LXCETCPATH, "$LXCETCPATH")
 AC_DEFINE_UNQUOTED(LXCPATH, "$LXCPATH")
 AC_DEFINE_UNQUOTED(LXCINITDIR, "$LXCINITDIR")
 AC_DEFINE_UNQUOTED(LXCROOTFSMOUNT, "$LXCROOTFSMOUNT")
diff --git a/src/lxc/lxc-clone.in b/src/lxc/lxc-clone.in
index 91944a0..810efac 100644
--- a/src/lxc/lxc-clone.in
+++ b/src/lxc/lxc-clone.in
@@ -40,7 +40,8 @@ help() {
 
 shortoptions='ho:n:sL:v:'
 longoptions='help,orig:,name:,snapshot,fssize,vgname'
-lxc_path=/var/lib/lxc
+lxc_path=@LXCPATH@
+config_path=@LXCETCPATH@
 bindir=/usr/bin
 snapshot=no
 lxc_size=2G
@@ -127,12 +128,12 @@ if [ ! -r $lxc_path ]; then
     exit 1
 fi
 
-if [ ! -d "$lxc_path/$lxc_orig" ]; then
+if [ ! -d "$lxc_path/$lxc_orig" -o ! -f "$config_path/$lxc_orig/config" ]; then
     echo "'$lxc_orig' does not exist"
     exit 1
 fi
 
-if [ -d "$lxc_path/$lxc_new" ]; then
+if [ -d "$lxc_path/$lxc_new" -o -f "$config_path/$lxc_new/config" ]; then
     echo "'$lxc_new' already exists"
     exit 1
 fi
@@ -140,25 +141,26 @@ fi
 trap "${bindir}/lxc-destroy -n $lxc_new; echo aborted; exit 1" SIGHUP SIGINT SIGTERM
 
 mkdir -p $lxc_path/$lxc_new
+mkdir -p $config_path/$lxc_new
 
 echo "Tweaking configuration"
-cp $lxc_path/$lxc_orig/config $lxc_path/$lxc_new/config
-sed -i '/lxc.utsname/d' $lxc_path/$lxc_new/config
-echo "lxc.utsname = $hostname" >> $lxc_path/$lxc_new/config
+cp $config_path/$lxc_orig/config $config_path/$lxc_new/config
+sed -i '/lxc.utsname/d' $config_path/$lxc_new/config
+echo "lxc.utsname = $hostname" >> $config_path/$lxc_new/config
 
-sed -i '/lxc.mount/d' $lxc_path/$lxc_new/config
-echo "lxc.mount = $lxc_path/$lxc_new/fstab" >> $lxc_path/$lxc_new/config
+sed -i '/lxc.mount/d' $config_path/$lxc_new/config
+echo "lxc.mount = $config_path/$lxc_new/fstab" >> $config_path/$lxc_new/config
 
-cp $lxc_path/$lxc_orig/fstab $lxc_path/$lxc_new/fstab
-sed -i "s@$lxc_path/$lxc_orig@$lxc_path/$lxc_new@" $lxc_path/$lxc_new/fstab
+cp $config_path/$lxc_orig/fstab $config_path/$lxc_new/fstab
+sed -i "s@$config_path/$lxc_orig@$config_path/$lxc_new@" $config_path/$lxc_new/fstab
 
 echo "Copying rootfs..."
 rootfs=$lxc_path/$lxc_new/rootfs
 # First figure out if the old is a device.  For now we only support
 # lvm devices.
 mounted=0
-sed -i '/lxc.rootfs/d' $lxc_path/$lxc_new/config
-oldroot=`grep lxc.rootfs $lxc_path/$lxc_orig/config | awk -F= '{ print $2 '}`
+sed -i '/lxc.rootfs/d' $config_path/$lxc_new/config
+oldroot=`grep lxc.rootfs $config_path/$lxc_orig/config | awk -F= '{ print $2 '}`
 if [ -b $oldroot ]; then
 	# this is a device.  If we don't want to snapshot, then mkfs, mount
 	# and rsync.  Trivial but not yet implemented
@@ -173,14 +175,14 @@ if [ -b $oldroot ]; then
 	fi
 	# ok, create a snapshot of the lvm device
 	lvcreate -s -L $lxc_size -n $lxc_new /dev/$lxc_vg/$lxc_orig || exit 1
-	echo "lxc.rootfs = /dev/$lxc_vg/$lxc_new" >> $lxc_path/$lxc_new/config
+	echo "lxc.rootfs = /dev/$lxc_vg/$lxc_new" >> $config_path/$lxc_new/config
 	# and mount it so we can tweak it
 	mkdir -p $lxc_path/$lxc_new/rootfs
 	mount /dev/$lxc_vg/$lxc_new $rootfs || { echo "failed to mount new rootfs"; exit 1; }
 	mounted=1
 else
 	cp -a $lxc_path/$lxc_orig/rootfs $lxc_path/$lxc_new/rootfs || return 1
-	echo "lxc.rootfs = $rootfs" >> $lxc_path/$lxc_new/config
+	echo "lxc.rootfs = $rootfs" >> $config_path/$lxc_new/config
 fi
 
 echo "Updating rootfs..."
diff --git a/src/lxc/lxc-create.in b/src/lxc/lxc-create.in
index 63750e9..a12d8eb 100644
--- a/src/lxc/lxc-create.in
+++ b/src/lxc/lxc-create.in
@@ -49,6 +49,7 @@ help() {
 shortoptions='hn:f:t:'
 longoptions='help,name:,config:,template:'
 lxc_path=@LXCPATH@
+lxc_etc_path=@LXCETCPATH@
 bindir=@BINDIR@
 templatedir=@LXCTEMPLATEDIR@
 
@@ -118,7 +119,7 @@ if [ ! -r $lxc_path ]; then
     exit 1
 fi
 
-if [ -d "$lxc_path/$lxc_name" ]; then
+if [ -d "$lxc_path/$lxc_name" -o -d "$lxc_etc_path/$lxc_name" ]; then
     echo "'$lxc_name' already exists"
     exit 1
 fi
@@ -126,16 +127,17 @@ fi
 trap "${bindir}/lxc-destroy -n $lxc_name; echo aborted; exit 1" SIGHUP SIGINT SIGTERM
 
 mkdir -p $lxc_path/$lxc_name
+mkdir -p $lxc_etc_path/$lxc_name
 
 if [ -z "$lxc_config" ]; then
-    touch $lxc_path/$lxc_name/config
+    touch $lxc_etc_path/$lxc_name/config
 else
     if [ ! -r "$lxc_config" ]; then
 	echo "'$lxc_config' configuration file not found"
 	exit 1
     fi
 
-    cp $lxc_config $lxc_path/$lxc_name/config
+    cp $lxc_config $lxc_etc_path/$lxc_name/config
 fi
 
 if [ ! -z $lxc_template ]; then
diff --git a/src/lxc/lxc-destroy.in b/src/lxc/lxc-destroy.in
index ddd3c3b..91bdbe1 100644
--- a/src/lxc/lxc-destroy.in
+++ b/src/lxc/lxc-destroy.in
@@ -37,6 +37,7 @@ fi
 shortoptions='n:'
 longoptions='name:'
 lxc_path=@LXCPATH@
+lxc_etc_path=@LXCETCPATH@
 
 getopt=$(getopt -o $shortoptions --longoptions  $longoptions -- "$@")
 if [ $? != 0 ]; then
@@ -70,7 +71,7 @@ if [ -z "$lxc_name" ]; then
     exit 1
 fi
 
-if [ ! -d "$lxc_path/$lxc_name" ]; then
+if [ ! -d "$lxc_path/$lxc_name" -o ! -f "$lxc_etc_path/$lxc_name/config"  ]; then
     echo "'$lxc_name' does not exist"
     exit 1
 fi
@@ -78,7 +79,7 @@ fi
 # Deduce the type of rootfs
 # If LVM partition, destroy it.  If anything else, ignore it.  We'll support
 # deletion of others later.
-rootdev=`grep lxc.rootfs $lxc_path/$lxc_name/config | awk -F= '{ print $2 '}`
+rootdev=`grep lxc.rootfs $lxc_etc_path/$lxc_name/config | awk -F= '{ print $2 '}`
 if [ -b $rootdev -o -h $rootdev ]; then
 	lvdisplay $rootdev > /dev/null 2>&1
 	if [ $? -eq 0 ]; then
@@ -87,3 +88,4 @@ if [ -b $rootdev -o -h $rootdev ]; then
 fi
 # recursively remove the container to remove old container configuration
 rm -rf --preserve-root $lxc_path/$lxc_name
+rm -rf --preserve-root $lxc_etc_path/$lxc_name
diff --git a/src/lxc/lxc_execute.c b/src/lxc/lxc_execute.c
index f480859..e2dd5b1 100644
--- a/src/lxc/lxc_execute.c
+++ b/src/lxc/lxc_execute.c
@@ -113,7 +113,7 @@ int main(int argc, char *argv[])
 	else {
 		int rc;
 
-		rc = asprintf(&rcfile, LXCPATH "/%s/config", my_args.name);
+		rc = asprintf(&rcfile, LXCETCPATH "/%s/config", my_args.name);
 		if (rc == -1) {
 			SYSERROR("failed to allocate memory");
 			return -1;
diff --git a/src/lxc/lxc_restart.c b/src/lxc/lxc_restart.c
index 7548682..31c8143 100644
--- a/src/lxc/lxc_restart.c
+++ b/src/lxc/lxc_restart.c
@@ -132,7 +132,7 @@ int main(int argc, char *argv[])
 	else {
 		int rc;
 
-		rc = asprintf(&rcfile, LXCPATH "/%s/config", my_args.name);
+		rc = asprintf(&rcfile, LXCETCPATH "/%s/config", my_args.name);
 		if (rc == -1) {
 			SYSERROR("failed to allocate memory");
 			return -1;
diff --git a/src/lxc/lxc_start.c b/src/lxc/lxc_start.c
index fdd4c72..36b4dd8 100644
--- a/src/lxc/lxc_start.c
+++ b/src/lxc/lxc_start.c
@@ -131,7 +131,7 @@ int main(int argc, char *argv[])
 	else {
 		int rc;
 
-		rc = asprintf(&rcfile, LXCPATH "/%s/config", my_args.name);
+		rc = asprintf(&rcfile, LXCETCPATH "/%s/config", my_args.name);
 		if (rc == -1) {
 			SYSERROR("failed to allocate memory");
 			return err;
diff --git a/templates/lxc-busybox.in b/templates/lxc-busybox.in
index 720ceef..ccd5830 100644
--- a/templates/lxc-busybox.in
+++ b/templates/lxc-busybox.in
@@ -224,11 +224,11 @@ configure_busybox()
 
 copy_configuration()
 {
-    path=$1
+    config_path=$1
     rootfs=$2
     name=$3
 
-cat <<EOF >> $path/config
+cat <<EOF >> $config_path/$name/config
 lxc.utsname = $name
 lxc.tty = 1
 lxc.pts = 1
@@ -236,14 +236,14 @@ lxc.rootfs = $rootfs
 EOF
 
 if [ -d "$rootfs/lib" ]; then
-cat <<EOF >> $path/config
+cat <<EOF >> $config_path/$name/config
 lxc.mount.entry=/lib $rootfs/lib none ro,bind 0 0
 lxc.mount.entry=/usr/lib $rootfs/usr/lib none ro,bind 0 0
 EOF
 fi
 
 if [ -d "/lib64" ] && [ -d "$rootfs/lib64" ]; then
-cat <<EOF >> $path/config
+cat <<EOF >> $config_path/$name/config
 lxc.mount.entry=/lib64 $rootfs/lib64 none ro,bind 0 0
 lxc.mount.entry=/usr/lib64 $rootfs/usr/lib64 none ro,bind 0 0
 EOF
@@ -287,6 +287,7 @@ if [ -z "$path" ]; then
 fi
 
 rootfs=$path/rootfs
+config_path=@LXCETCPATH@
 
 install_busybox $rootfs $name
 if [ $? -ne 0 ]; then
@@ -300,7 +301,7 @@ if [ $? -ne 0 ]; then
     exit 1
 fi
 
-copy_configuration $path $rootfs $name
+copy_configuration $config_path $rootfs $name
 if [ $? -ne 0 ]; then
     echo "failed to write configuration file"
     exit 1
diff --git a/templates/lxc-debian.in b/templates/lxc-debian.in
index 75a33a4..4cc9830 100644
--- a/templates/lxc-debian.in
+++ b/templates/lxc-debian.in
@@ -184,11 +184,11 @@ install_debian()
 
 copy_configuration()
 {
-    path=$1
+    config_path=$1
     rootfs=$2
     name=$3
 
-    cat <<EOF >> $path/config
+    cat <<EOF >> $config_path/$name/config
 lxc.tty = 4
 lxc.pts = 1024
 lxc.rootfs = $rootfs
@@ -294,6 +294,7 @@ if [ "$(id -u)" != "0" ]; then
 fi
 
 rootfs=$path/rootfs
+config_path=@LXCETCPATH@
 
 install_debian $rootfs
 if [ $? -ne 0 ]; then
@@ -307,7 +308,7 @@ if [ $? -ne 0 ]; then
     exit 1
 fi
 
-copy_configuration $path $rootfs
+copy_configuration $config_path $rootfs $name
 if [ $? -ne 0 ]; then
     echo "failed write configuration file"
     exit 1
diff --git a/templates/lxc-fedora.in b/templates/lxc-fedora.in
index 81f8bc4..1df602b 100644
--- a/templates/lxc-fedora.in
+++ b/templates/lxc-fedora.in
@@ -28,7 +28,8 @@
 #Configurations
 arch=$(arch)
 cache_base=/var/cache/lxc/fedora/$arch
-default_path=/var/lib/lxc
+default_path=@LXCPATH@
+default_config_path=@LXCETCPATH@
 root_password=rooter
 lxc_network_type=veth
 lxc_network_link=virbr0
@@ -227,6 +228,7 @@ EOF
 +proc            $rootfs_path/proc         proc    nodev,noexec,nosuid 0 0
 +devpts          $rootfs_path/dev/pts      devpts defaults 0 0
 +sysfs           $rootfs_path/sys          sysfs defaults  0 0
+EOF
 
     if [ $? -ne 0 ]; then
 	echo "Failed to add configuration"
@@ -327,7 +329,7 @@ if [ "$(id -u)" != "0" ]; then
 fi
 
 rootfs_path=$path/$name/rootfs
-config_path=$default_path/$name
+config_path=$default_config_path/$name
 cache=$cache_base/$release
 
 if [ -f $config_path/config ]; then
diff --git a/templates/lxc-lenny.in b/templates/lxc-lenny.in
index 3720dce..b9bc4a2 100644
--- a/templates/lxc-lenny.in
+++ b/templates/lxc-lenny.in
@@ -174,11 +174,11 @@ install_debian()
 
 copy_configuration()
 {
-    path=$1
+    config_path=$1
     rootfs=$2
     name=$3
 
-    cat <<EOF >> $path/config
+    cat <<EOF >> $config_path/$name/config
 lxc.tty = 4
 lxc.pts = 1024
 lxc.rootfs = $rootfs
@@ -284,6 +284,7 @@ if [ "$(id -u)" != "0" ]; then
 fi
 
 rootfs=$path/rootfs
+config_path=@LXCETCPATH@
 
 install_debian $rootfs
 if [ $? -ne 0 ]; then
@@ -297,7 +298,7 @@ if [ $? -ne 0 ]; then
     exit 1
 fi
 
-copy_configuration $path $rootfs
+copy_configuration $config_path $rootfs $name
 if [ $? -ne 0 ]; then
     echo "failed write configuration file"
     exit 1
diff --git a/templates/lxc-opensuse.in b/templates/lxc-opensuse.in
index 811876a..8e90b18 100644
--- a/templates/lxc-opensuse.in
+++ b/templates/lxc-opensuse.in
@@ -228,17 +228,17 @@ install_opensuse()
 
 copy_configuration()
 {
-    path=$1
+    config_path=$1
     rootfs=$2
     name=$3
 
-    cat <<EOF >> $path/config
+    cat <<EOF >> $config_path/$name/config
 lxc.utsname = $name
 
 lxc.tty = 4
 lxc.pts = 1024
 lxc.rootfs = $rootfs
-lxc.mount  = $path/fstab
+lxc.mount  = $config_path/$name/fstab
 
 lxc.cgroup.devices.deny = a
 # /dev/null and zero
@@ -258,7 +258,7 @@ lxc.cgroup.devices.allow = c 5:2 rwm
 lxc.cgroup.devices.allow = c 254:0 rwm
 EOF
 
-    cat <<EOF > $path/fstab
+    cat <<EOF > $config_path/$name/fstab
 proc            $rootfs/proc         proc	nodev,noexec,nosuid 0 0
 sysfs           $rootfs/sys          sysfs	defaults  0 0
 EOF
@@ -343,6 +343,7 @@ if [ "$(id -u)" != "0" ]; then
 fi
 
 rootfs=$path/rootfs
+config_path=@LXCETCPATH@
 
 install_opensuse $rootfs
 if [ $? -ne 0 ]; then
@@ -356,7 +357,7 @@ if [ $? -ne 0 ]; then
     exit 1
 fi
 
-copy_configuration $path $rootfs $name
+copy_configuration $config_path $rootfs $name
 if [ $? -ne 0 ]; then
     echo "failed write configuration file"
     exit 1
diff --git a/templates/lxc-sshd.in b/templates/lxc-sshd.in
index 0e8346f..c57843b 100644
--- a/templates/lxc-sshd.in
+++ b/templates/lxc-sshd.in
@@ -92,11 +92,11 @@ EOF
 
 copy_configuration()
 {
-    path=$1
+    config_path=$1
     rootfs=$2
     name=$3
 
-cat <<EOF >> $path/config
+cat <<EOF >> $config_path/$name/config
 lxc.utsname = $name
 lxc.pts = 1024
 lxc.rootfs = $rootfs
@@ -110,7 +110,7 @@ lxc.mount.entry=@LXCTEMPLATEDIR@/lxc-sshd $rootfs/sbin/init none bind 0 0
 EOF
 
 if [ "$(uname -m)" = "x86_64" ]; then
-    cat <<EOF >> $path/config
+    cat <<EOF >> $config_path/$name/config
 lxc.mount.entry=/lib64 $rootfs/lib64 none ro,bind 0 0
 EOF
 fi
@@ -171,6 +171,7 @@ if [ -z "$path" ]; then
 fi
 
 rootfs=$path/rootfs
+config_path=@LXCETCPATH@
 
 install_sshd $rootfs
 if [ $? -ne 0 ]; then
@@ -184,7 +185,7 @@ if [ $? -ne 0 ]; then
     exit 1
 fi
 
-copy_configuration $path $rootfs $name
+copy_configuration $config_path $rootfs $name
 if [ $? -ne 0 ]; then
     echo "failed to write configuration file"
     exit 1
diff --git a/templates/lxc-ubuntu.in b/templates/lxc-ubuntu.in
index 9a41a49..f966bd8 100644
--- a/templates/lxc-ubuntu.in
+++ b/templates/lxc-ubuntu.in
@@ -162,7 +162,7 @@ install_ubuntu()
 
 copy_configuration()
 {
-    path=$1
+    config_path=$1
     rootfs=$2
     name=$3
     arch=$4
@@ -171,13 +171,13 @@ copy_configuration()
         arch="i686"
     fi
 
-    cat <<EOF >> $path/config
+    cat <<EOF >> $config_path/$name/config
 lxc.utsname = $name
 
 lxc.tty = 4
 lxc.pts = 1024
 lxc.rootfs = $rootfs
-lxc.mount  = $path/fstab
+lxc.mount  = $config_path/$name/fstab
 lxc.arch = $arch
 
 lxc.cgroup.devices.deny = a
@@ -200,7 +200,7 @@ lxc.cgroup.devices.allow = c 254:0 rwm
 lxc.cgroup.devices.allow = c 10:229 rwm
 EOF
 
-    cat <<EOF > $path/fstab
+    cat <<EOF > $config_path/$name/fstab
 proc            $rootfs/proc         proc    nodev,noexec,nosuid 0 0
 sysfs           $rootfs/sys          sysfs defaults  0 0
 EOF
@@ -468,6 +468,7 @@ if [ "$(id -u)" != "0" ]; then
 fi
 
 rootfs=$path/rootfs
+config_path=@LXCETCPATH@
 
 install_ubuntu $rootfs $release
 if [ $? -ne 0 ]; then
@@ -481,7 +482,7 @@ if [ $? -ne 0 ]; then
     exit 1
 fi
 
-copy_configuration $path $rootfs $name $arch
+copy_configuration $config_path $rootfs $name $arch
 if [ $? -ne 0 ]; then
     echo "failed write configuration file"
     exit 1
-- 
1.7.6

