]> git.proxmox.com Git - mirror_lxc.git/commitdiff
lxc-create: Make location of container rootfs configurable
authorSerge Hallyn <serge.halyn@ubuntu.com>
Tue, 21 Aug 2012 15:11:23 +0000 (10:11 -0500)
committerStéphane Graber <stgraber@ubuntu.com>
Mon, 12 Nov 2012 18:13:52 +0000 (13:13 -0500)
Make 'dir' an explicit backing store type, which accepts '--dir rootfs'
as an option to specify a custom location for the container rootfs.  Also
update lxc-destroy to now remove the rootfs separately, as removing
@LXCPATH@/$name may not hit it.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
13 files changed:
doc/lxc-create.sgml.in
src/lxc/lxc-create.in
src/lxc/lxc-destroy.in
templates/lxc-altlinux.in
templates/lxc-archlinux.in
templates/lxc-busybox.in
templates/lxc-debian.in
templates/lxc-fedora.in
templates/lxc-lenny.in
templates/lxc-opensuse.in
templates/lxc-sshd.in
templates/lxc-ubuntu-cloud.in
templates/lxc-ubuntu.in

index 0ffb21c2473aba59bd1494f3c9e6c5832ba47e56..f4f4e592f8ced98e2d0ac89c6e94732d94e8ca79 100644 (file)
@@ -123,9 +123,13 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
        </term>
        <listitem>
          <para>
-           'backingstore' is one of 'none', 'lvm', or 'btrfs'.  The
+           'backingstore' is one of 'none', 'dir', 'lvm', or 'btrfs'.  The
            default is 'none', meaning that the container root filesystem
            will be a directory under <filename>@LXCPATH@/container/rootfs</filename>.
+           'dir' has the same meaning as 'none', but also allows the optional
+           <replaceable>--dir ROOTFS</replaceable> to be specified, meaning
+           that the container rootfs should be placed under the specified path,
+           rather than the default.
            The option 'btrfs' need not be specified as it will be used
            automatically if the <filename>@LXCPATH@</filename> filesystem is found to
            be btrfs.  If backingstore is 'lvm', then an lvm block device will be
index b21cdc301e0390e8f6f9ad1a9dc8fd097efc5e72..79c67d627c9b32cc0fe80c792e2a9cf25cfd0a92 100644 (file)
@@ -26,6 +26,7 @@ usage() {
     echo >&2
     echo "where FS_OPTIONS is one of:" >&2
     echo "  -B none" >&2
+    echo "  -B dir [--dir rootfs_dir]" >&2
     echo "  -B lvm [--lvname LV_NAME] [--vgname VG_NAME] [--fstype FS_TYPE]" >&2
     echo "    [--fssize FS_SIZE]" >&2
     echo "  -B btrfs" >&2
@@ -43,6 +44,7 @@ help() {
     echo "  -B BACKING_STORE   alter the container backing store (default: none)" >&2
     echo "  --lvname LV_NAME   specify the LVM logical volume name" >&2
     echo "                      (default: container name)" >&2
+    echo "  --dir ROOTFS_DIR   specify path for custom rootfs directory location" >&2
     echo "  --vgname VG_NAME   specify the LVM volume group name (default: lxc)" >&2
     echo "  --fstype FS_TYPE   specify the filesystem type (default: ext4)" >&2
     echo "  --fssize FS_SIZE   specify the filesystem size (default: 500M)" >&2
@@ -61,7 +63,7 @@ help() {
 }
 
 shortoptions='hn:f:t:B:'
-longoptions='help,name:,config:,template:,backingstore:,fstype:,lvname:,vgname:,fssize:'
+longoptions='help,name:,config:,template:,backingstore:,fstype:,dir:,lvname:,vgname:,fssize:'
 lxc_path=@LXCPATH@
 bindir=@BINDIR@
 templatedir=@LXCTEMPLATEDIR@
@@ -69,6 +71,7 @@ backingstore=_unset
 fstype=ext4
 fssize=500M
 vgname=lxc
+custom_rootfs=""
 
 getopt=$(getopt -o $shortoptions --longoptions  $longoptions -- "$@")
 if [ $? != 0 ]; then
@@ -104,6 +107,11 @@ while true; do
                backingstore=$1
                shift
                ;;
+           --dir)
+               shift
+               custom_rootfs=$1
+               shift
+               ;;
            --lvname)
                shift
                lvname=$1
@@ -171,9 +179,13 @@ if [ "$(id -u)" != "0" ]; then
    exit 1
 fi
 
+if [ -n "$custom_rootfs" -a "$backingstore" != "dir" ]; then
+   echo "--dir is only valid with -B dir"
+fi
+
 case "$backingstore" in
-    lvm|none|btrfs|_unset) :;;
-    *) echo "$(basename $0): '$backingstore' is not known (try 'none', 'lvm', 'btrfs')" >&2
+    dir|lvm|none|btrfs|_unset) :;;
+    *) echo "$(basename $0): '$backingstore' is not known (try 'none', 'dir', 'lvm', 'btrfs')" >&2
         usage
         exit 1
         ;;
@@ -258,6 +270,14 @@ else
     cp $lxc_config $lxc_path/$lxc_name/config
 fi
 
+if [ -n "$custom_rootfs" ]; then
+       if grep -q "lxc.rootfs" $lxc_path/$lxc_name/config ; then
+               echo "configuration file already specifies a lxc.rootfs"
+               exit 1
+       fi
+       echo "lxc.rootfs = $custom_rootfs" >> $lxc_path/$lxc_name/config
+fi
+
 # Create the fs as needed
 if [ $backingstore = "lvm" ]; then
     [ -d "$rootfs" ] || mkdir $rootfs
index 846266cadb80f7fc95f3106cd1544317328011ae..ecff083252160fa00a18928bab9882bdf5eba907 100644 (file)
@@ -110,7 +110,7 @@ fi
 # else, ignore it. We'll support deletion of others later.
 rootdev=`grep lxc.rootfs $lxc_path/$lxc_name/config 2>/dev/null | sed -e 's/^[^/]*/\//'`
 if [ -n "$rootdev" ]; then
-       if [ -b "$rootdev" ]; then
+       if [ -b "$rootdev" -o -h "$rootdev" ]; then
                lvdisplay $rootdev > /dev/null 2>&1
                if [ $? -eq 0 ]; then
                        echo "removing backing store: $rootdev"
@@ -126,5 +126,6 @@ if [ -n "$rootdev" ]; then
                fi
        fi
 fi
+
 # recursively remove the container to remove old container configuration
 rm -rf --one-file-system --preserve-root $lxc_path/$lxc_name
index 2d2274a335deccaedba0fe305027d425d1021fa7..0bf9735415417587e1413d5eb099ebaf1df8563c 100644 (file)
@@ -239,11 +239,11 @@ copy_configuration()
 {
 
     mkdir -p $config_path
+    grep -q "^lxc.rootfs" $config_path/config 2>/dev/null || echo "lxc.rootfs = $rootfs_path" >> $config_path/config
     cat <<EOF >> $config_path/config
 lxc.utsname = $name
 lxc.tty = 4
 lxc.pts = 1024
-lxc.rootfs = $rootfs_path
 lxc.mount  = $config_path/fstab
 
 # When using LXC with apparmor, uncomment the next line to run unconfined:
@@ -433,6 +433,11 @@ if [ -f $config_path/config ]; then
     exit 1
 fi
 
+# check for 'lxc.rootfs' passed in through default config by lxc-create
+if grep -q '^lxc.rootfs' $path/config 2>/dev/null ; then
+    rootfs_path=`grep 'lxc.rootfs =' $path/config | awk -F= '{ print $2 }'`
+fi
+
 install_altlinux
 if [ $? -ne 0 ]; then
     echo "failed to install altlinux"
index 18ec0642d3275fc558fbba995c5ebe60160603ac..639d81a38e62c1493ea0d4154febb3f4b85b3bcd 100644 (file)
@@ -218,11 +218,11 @@ EOF
 # write container configuration files
 function copy_configuration {
     mkdir -p "${config_path}"
+    grep -q "^lxc.rootfs" "${config_path}/config" 2>/dev/null || echo "lxc.rootfs=${rootfs_path}" >> "${config_path}/config"
     cat > "${config_path}/config" << EOF
 lxc.utsname=${name}
 lxc.tty=4
 lxc.pts=1024
-lxc.rootfs=${rootfs_path}
 lxc.mount=${config_path}/fstab
 
 # When using LXC with apparmor, uncomment the next line to run unconfined:
@@ -423,6 +423,10 @@ if [ "${EUID}" != "0" ]; then
 fi
 
 rootfs_path="${path}/rootfs"
+# check for 'lxc.rootfs' passed in through default config by lxc-create
+if grep -q '^lxc.rootfs' $path/config 2>/dev/null ; then
+    rootfs_path=`grep 'lxc.rootfs =' $path/config | awk -F= '{ print $2 }'`
+fi
 config_path="${default_path}/${name}"
 
 revert()
index 06d2f8916ffc8f343959fe2f515de85ec531be90..f6e8b5a9ae15eff1fb6a604c94bc3cb5f6df81c8 100644 (file)
@@ -228,11 +228,19 @@ copy_configuration()
     rootfs=$2
     name=$3
 
+grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
 cat <<EOF >> $path/config
 lxc.utsname = $name
 lxc.tty = 1
 lxc.pts = 1
-lxc.rootfs = $rootfs
+EOF
+
+if [ -d "$rootfs/lib" ]; then
+cat <<EOF >> $path/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
 
 # When using LXC with apparmor, uncomment the next line to run unconfined:
 #lxc.aa_profile = unconfined
@@ -287,7 +295,13 @@ if [ -z "$path" ]; then
     exit 1
 fi
 
-rootfs=$path/rootfs
+# detect rootfs
+config="$path/config"
+if grep -q '^lxc.rootfs' $config 2>/dev/null ; then
+    rootfs=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'`
+else
+    rootfs=$path/rootfs
+fi
 
 install_busybox $rootfs $name
 if [ $? -ne 0 ]; then
index 0c4d2c50464b30598ea4474dc93c964f2ee563f2..03a4c6aa2ed5865a0f21d69dfaf6772230dd1177 100644 (file)
@@ -202,10 +202,10 @@ copy_configuration()
     rootfs=$2
     hostname=$3
 
+    grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
     cat <<EOF >> $path/config
 lxc.tty = 4
 lxc.pts = 1024
-lxc.rootfs = $rootfs
 lxc.utsname = $hostname
 
 # When using LXC with apparmor, uncomment the next line to run unconfined:
@@ -312,7 +312,14 @@ if [ "$(id -u)" != "0" ]; then
     exit 1
 fi
 
-rootfs=$path/rootfs
+# detect rootfs
+config="$path/config"
+if grep -q '^lxc.rootfs' $config 2>/dev/null ; then
+    rootfs=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'`
+else
+    rootfs=$path/rootfs
+fi
+
 
 install_debian $rootfs
 if [ $? -ne 0 ]; then
index c2d3143d1c47550b83959bc093e4712c1019ae13..edd4b9dc95dd3b77339f7969a824d64a6c937fbf 100644 (file)
@@ -243,11 +243,11 @@ copy_configuration()
 {
 
     mkdir -p $config_path
+    grep -q "^lxc.rootfs" $config_path/config 2>/dev/null || echo "lxc.rootfs = $rootfs_path" >> $config_path/config
     cat <<EOF >> $config_path/config
 lxc.utsname = $name
 lxc.tty = 4
 lxc.pts = 1024
-lxc.rootfs = $rootfs_path
 lxc.mount  = $config_path/fstab
 
 # When using LXC with apparmor, uncomment the next line to run unconfined:
index de03f01d8df4fe579b210d18b874d14da4534a86..46de1eaeb0c50ca69383a619a39f63e535ced0f5 100644 (file)
@@ -178,10 +178,10 @@ copy_configuration()
     rootfs=$2
     name=$3
 
+    grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
     cat <<EOF >> $path/config
 lxc.tty = 4
 lxc.pts = 1024
-lxc.rootfs = $rootfs
 lxc.cgroup.devices.deny = a
 
 # When using LXC with apparmor, uncomment the next line to run unconfined:
@@ -287,7 +287,13 @@ if [ "$(id -u)" != "0" ]; then
     exit 1
 fi
 
-rootfs=$path/rootfs
+# detect rootfs
+config="$path/config"
+if grep -q '^lxc.rootfs' $config 2>/dev/null ; then
+    rootfs=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'`
+else
+    rootfs=$path/rootfs
+fi
 
 install_debian $rootfs
 if [ $? -ne 0 ]; then
index d728af3123a1d796509e07e96f126c90e9461198..32424515dddf853c499cdfee47da4f555ae99e48 100644 (file)
@@ -254,12 +254,12 @@ copy_configuration()
     rootfs=$2
     name=$3
 
+    grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
     cat <<EOF >> $path/config
 lxc.utsname = $name
 
 lxc.tty = 4
 lxc.pts = 1024
-lxc.rootfs = $rootfs
 lxc.mount  = $path/fstab
 
 # When using LXC with apparmor, uncomment the next line to run unconfined:
@@ -367,7 +367,13 @@ if [ "$(id -u)" != "0" ]; then
     exit 1
 fi
 
-rootfs=$path/rootfs
+# detect rootfs
+config="$path/config"
+if grep -q '^lxc.rootfs' $config 2>/dev/null ; then
+    rootfs=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'`
+else
+    rootfs=$path/rootfs
+fi
 
 install_opensuse $rootfs
 if [ $? -ne 0 ]; then
index d456fbbb8b9daef8c6fca74a081d5fa5e33c6d23..4f8d50407e6a90e8432e9e74fa1812e38bfc9f4e 100644 (file)
@@ -108,10 +108,10 @@ copy_configuration()
     rootfs=$2
     name=$3
 
+    grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
 cat <<EOF >> $path/config
 lxc.utsname = $name
 lxc.pts = 1024
-lxc.rootfs = $rootfs
 
 # When using LXC with apparmor, uncomment the next line to run unconfined:
 #lxc.aa_profile = unconfined
@@ -207,7 +207,13 @@ if [ -z "$path" ]; then
     exit 1
 fi
 
-rootfs=$path/rootfs
+# detect rootfs
+config="$path/config"
+if grep -q '^lxc.rootfs' $config 2>/dev/null ; then
+    rootfs=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'`
+else
+    rootfs=$path/rootfs
+fi
 
 install_sshd $rootfs
 if [ $? -ne 0 ]; then
index 34983099900680552c73f7fbf8c2b523ccd721df..8df05da11b985513cffed6a4d46265b2d1745c86 100644 (file)
@@ -46,12 +46,12 @@ lxc.network.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//
 EOF
     fi
 
+    grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
     cat <<EOF >> $path/config
 lxc.utsname = $name
 
 lxc.tty = 4
 lxc.pts = 1024
-lxc.rootfs = $rootfs
 lxc.mount  = $path/fstab
 lxc.arch = $arch
 lxc.cap.drop = sys_module mac_admin
@@ -249,7 +249,13 @@ if [ "$(id -u)" != "0" ]; then
     exit 1
 fi
 
-rootfs=$path/rootfs
+# detect rootfs
+config="$path/config"
+if grep -q '^lxc.rootfs' $config 2>/dev/null ; then
+    rootfs=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'`
+else
+    rootfs=$path/rootfs
+fi
 
 type ubuntu-cloudimg-query
 type wget
index aca211843a32c16c2794875b0c59ca14abb5eb53..90021fa8e240851503e41ca5cf48b9b6f2c048f6 100644 (file)
@@ -303,13 +303,13 @@ lxc.network.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//
 EOF
     fi
 
+    grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
     cat <<EOF >> $path/config
 lxc.utsname = $name
 
 lxc.devttydir =$ttydir
 lxc.tty = 4
 lxc.pts = 1024
-lxc.rootfs = $rootfs
 lxc.mount  = $path/fstab
 lxc.arch = $arch
 lxc.cap.drop = sys_module mac_admin mac_override
@@ -670,7 +670,13 @@ if [ "$(id -u)" != "0" ]; then
     exit 1
 fi
 
-rootfs=$path/rootfs
+# detect rootfs
+config="$path/config"
+if grep -q '^lxc.rootfs' $config 2>/dev/null ; then
+    rootfs=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'`
+else
+    rootfs=$path/rootfs
+fi
 
 install_ubuntu $rootfs $release $flushcache
 if [ $? -ne 0 ]; then