Commit 1881820a by Serge Hallyn Committed by Stéphane Graber

lxc-create: Make location of container rootfs configurable

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: 's avatarSerge Hallyn <serge.hallyn@ubuntu.com>
parent 74a2b586
...@@ -123,9 +123,13 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ...@@ -123,9 +123,13 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
</term> </term>
<listitem> <listitem>
<para> <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 default is 'none', meaning that the container root filesystem
will be a directory under <filename>@LXCPATH@/container/rootfs</filename>. 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 The option 'btrfs' need not be specified as it will be used
automatically if the <filename>@LXCPATH@</filename> filesystem is found to automatically if the <filename>@LXCPATH@</filename> filesystem is found to
be btrfs. If backingstore is 'lvm', then an lvm block device will be be btrfs. If backingstore is 'lvm', then an lvm block device will be
......
...@@ -26,6 +26,7 @@ usage() { ...@@ -26,6 +26,7 @@ usage() {
echo >&2 echo >&2
echo "where FS_OPTIONS is one of:" >&2 echo "where FS_OPTIONS is one of:" >&2
echo " -B none" >&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 " -B lvm [--lvname LV_NAME] [--vgname VG_NAME] [--fstype FS_TYPE]" >&2
echo " [--fssize FS_SIZE]" >&2 echo " [--fssize FS_SIZE]" >&2
echo " -B btrfs" >&2 echo " -B btrfs" >&2
...@@ -43,6 +44,7 @@ help() { ...@@ -43,6 +44,7 @@ help() {
echo " -B BACKING_STORE alter the container backing store (default: none)" >&2 echo " -B BACKING_STORE alter the container backing store (default: none)" >&2
echo " --lvname LV_NAME specify the LVM logical volume name" >&2 echo " --lvname LV_NAME specify the LVM logical volume name" >&2
echo " (default: container 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 " --vgname VG_NAME specify the LVM volume group name (default: lxc)" >&2
echo " --fstype FS_TYPE specify the filesystem type (default: ext4)" >&2 echo " --fstype FS_TYPE specify the filesystem type (default: ext4)" >&2
echo " --fssize FS_SIZE specify the filesystem size (default: 500M)" >&2 echo " --fssize FS_SIZE specify the filesystem size (default: 500M)" >&2
...@@ -61,7 +63,7 @@ help() { ...@@ -61,7 +63,7 @@ help() {
} }
shortoptions='hn:f:t:B:' 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@ lxc_path=@LXCPATH@
bindir=@BINDIR@ bindir=@BINDIR@
templatedir=@LXCTEMPLATEDIR@ templatedir=@LXCTEMPLATEDIR@
...@@ -69,6 +71,7 @@ backingstore=_unset ...@@ -69,6 +71,7 @@ backingstore=_unset
fstype=ext4 fstype=ext4
fssize=500M fssize=500M
vgname=lxc vgname=lxc
custom_rootfs=""
getopt=$(getopt -o $shortoptions --longoptions $longoptions -- "$@") getopt=$(getopt -o $shortoptions --longoptions $longoptions -- "$@")
if [ $? != 0 ]; then if [ $? != 0 ]; then
...@@ -104,6 +107,11 @@ while true; do ...@@ -104,6 +107,11 @@ while true; do
backingstore=$1 backingstore=$1
shift shift
;; ;;
--dir)
shift
custom_rootfs=$1
shift
;;
--lvname) --lvname)
shift shift
lvname=$1 lvname=$1
...@@ -171,9 +179,13 @@ if [ "$(id -u)" != "0" ]; then ...@@ -171,9 +179,13 @@ if [ "$(id -u)" != "0" ]; then
exit 1 exit 1
fi fi
if [ -n "$custom_rootfs" -a "$backingstore" != "dir" ]; then
echo "--dir is only valid with -B dir"
fi
case "$backingstore" in case "$backingstore" in
lvm|none|btrfs|_unset) :;; dir|lvm|none|btrfs|_unset) :;;
*) echo "$(basename $0): '$backingstore' is not known (try 'none', 'lvm', 'btrfs')" >&2 *) echo "$(basename $0): '$backingstore' is not known (try 'none', 'dir', 'lvm', 'btrfs')" >&2
usage usage
exit 1 exit 1
;; ;;
...@@ -258,6 +270,14 @@ else ...@@ -258,6 +270,14 @@ else
cp $lxc_config $lxc_path/$lxc_name/config cp $lxc_config $lxc_path/$lxc_name/config
fi 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 # Create the fs as needed
if [ $backingstore = "lvm" ]; then if [ $backingstore = "lvm" ]; then
[ -d "$rootfs" ] || mkdir $rootfs [ -d "$rootfs" ] || mkdir $rootfs
......
...@@ -110,7 +110,7 @@ fi ...@@ -110,7 +110,7 @@ fi
# else, ignore it. We'll support deletion of others later. # 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/^[^/]*/\//'` rootdev=`grep lxc.rootfs $lxc_path/$lxc_name/config 2>/dev/null | sed -e 's/^[^/]*/\//'`
if [ -n "$rootdev" ]; then if [ -n "$rootdev" ]; then
if [ -b "$rootdev" ]; then if [ -b "$rootdev" -o -h "$rootdev" ]; then
lvdisplay $rootdev > /dev/null 2>&1 lvdisplay $rootdev > /dev/null 2>&1
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "removing backing store: $rootdev" echo "removing backing store: $rootdev"
...@@ -126,5 +126,6 @@ if [ -n "$rootdev" ]; then ...@@ -126,5 +126,6 @@ if [ -n "$rootdev" ]; then
fi fi
fi fi
fi fi
# recursively remove the container to remove old container configuration # recursively remove the container to remove old container configuration
rm -rf --one-file-system --preserve-root $lxc_path/$lxc_name rm -rf --one-file-system --preserve-root $lxc_path/$lxc_name
...@@ -239,11 +239,11 @@ copy_configuration() ...@@ -239,11 +239,11 @@ copy_configuration()
{ {
mkdir -p $config_path 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 cat <<EOF >> $config_path/config
lxc.utsname = $name lxc.utsname = $name
lxc.tty = 4 lxc.tty = 4
lxc.pts = 1024 lxc.pts = 1024
lxc.rootfs = $rootfs_path
lxc.mount = $config_path/fstab lxc.mount = $config_path/fstab
# When using LXC with apparmor, uncomment the next line to run unconfined: # When using LXC with apparmor, uncomment the next line to run unconfined:
...@@ -433,6 +433,11 @@ if [ -f $config_path/config ]; then ...@@ -433,6 +433,11 @@ if [ -f $config_path/config ]; then
exit 1 exit 1
fi 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 install_altlinux
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "failed to install altlinux" echo "failed to install altlinux"
......
...@@ -218,11 +218,11 @@ EOF ...@@ -218,11 +218,11 @@ EOF
# write container configuration files # write container configuration files
function copy_configuration { function copy_configuration {
mkdir -p "${config_path}" 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 cat > "${config_path}/config" << EOF
lxc.utsname=${name} lxc.utsname=${name}
lxc.tty=4 lxc.tty=4
lxc.pts=1024 lxc.pts=1024
lxc.rootfs=${rootfs_path}
lxc.mount=${config_path}/fstab lxc.mount=${config_path}/fstab
# When using LXC with apparmor, uncomment the next line to run unconfined: # When using LXC with apparmor, uncomment the next line to run unconfined:
...@@ -423,6 +423,10 @@ if [ "${EUID}" != "0" ]; then ...@@ -423,6 +423,10 @@ if [ "${EUID}" != "0" ]; then
fi fi
rootfs_path="${path}/rootfs" 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}" config_path="${default_path}/${name}"
revert() revert()
......
...@@ -228,11 +228,19 @@ copy_configuration() ...@@ -228,11 +228,19 @@ copy_configuration()
rootfs=$2 rootfs=$2
name=$3 name=$3
grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
cat <<EOF >> $path/config cat <<EOF >> $path/config
lxc.utsname = $name lxc.utsname = $name
lxc.tty = 1 lxc.tty = 1
lxc.pts = 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: # When using LXC with apparmor, uncomment the next line to run unconfined:
#lxc.aa_profile = unconfined #lxc.aa_profile = unconfined
...@@ -287,7 +295,13 @@ if [ -z "$path" ]; then ...@@ -287,7 +295,13 @@ if [ -z "$path" ]; then
exit 1 exit 1
fi 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 install_busybox $rootfs $name
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
......
...@@ -202,10 +202,10 @@ copy_configuration() ...@@ -202,10 +202,10 @@ copy_configuration()
rootfs=$2 rootfs=$2
hostname=$3 hostname=$3
grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
cat <<EOF >> $path/config cat <<EOF >> $path/config
lxc.tty = 4 lxc.tty = 4
lxc.pts = 1024 lxc.pts = 1024
lxc.rootfs = $rootfs
lxc.utsname = $hostname lxc.utsname = $hostname
# When using LXC with apparmor, uncomment the next line to run unconfined: # When using LXC with apparmor, uncomment the next line to run unconfined:
...@@ -312,7 +312,14 @@ if [ "$(id -u)" != "0" ]; then ...@@ -312,7 +312,14 @@ if [ "$(id -u)" != "0" ]; then
exit 1 exit 1
fi 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 install_debian $rootfs
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
......
...@@ -243,11 +243,11 @@ copy_configuration() ...@@ -243,11 +243,11 @@ copy_configuration()
{ {
mkdir -p $config_path 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 cat <<EOF >> $config_path/config
lxc.utsname = $name lxc.utsname = $name
lxc.tty = 4 lxc.tty = 4
lxc.pts = 1024 lxc.pts = 1024
lxc.rootfs = $rootfs_path
lxc.mount = $config_path/fstab lxc.mount = $config_path/fstab
# When using LXC with apparmor, uncomment the next line to run unconfined: # When using LXC with apparmor, uncomment the next line to run unconfined:
......
...@@ -178,10 +178,10 @@ copy_configuration() ...@@ -178,10 +178,10 @@ copy_configuration()
rootfs=$2 rootfs=$2
name=$3 name=$3
grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
cat <<EOF >> $path/config cat <<EOF >> $path/config
lxc.tty = 4 lxc.tty = 4
lxc.pts = 1024 lxc.pts = 1024
lxc.rootfs = $rootfs
lxc.cgroup.devices.deny = a lxc.cgroup.devices.deny = a
# When using LXC with apparmor, uncomment the next line to run unconfined: # When using LXC with apparmor, uncomment the next line to run unconfined:
...@@ -287,7 +287,13 @@ if [ "$(id -u)" != "0" ]; then ...@@ -287,7 +287,13 @@ if [ "$(id -u)" != "0" ]; then
exit 1 exit 1
fi 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 install_debian $rootfs
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
......
...@@ -254,12 +254,12 @@ copy_configuration() ...@@ -254,12 +254,12 @@ copy_configuration()
rootfs=$2 rootfs=$2
name=$3 name=$3
grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
cat <<EOF >> $path/config cat <<EOF >> $path/config
lxc.utsname = $name lxc.utsname = $name
lxc.tty = 4 lxc.tty = 4
lxc.pts = 1024 lxc.pts = 1024
lxc.rootfs = $rootfs
lxc.mount = $path/fstab lxc.mount = $path/fstab
# When using LXC with apparmor, uncomment the next line to run unconfined: # When using LXC with apparmor, uncomment the next line to run unconfined:
...@@ -367,7 +367,13 @@ if [ "$(id -u)" != "0" ]; then ...@@ -367,7 +367,13 @@ if [ "$(id -u)" != "0" ]; then
exit 1 exit 1
fi 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 install_opensuse $rootfs
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
......
...@@ -108,10 +108,10 @@ copy_configuration() ...@@ -108,10 +108,10 @@ copy_configuration()
rootfs=$2 rootfs=$2
name=$3 name=$3
grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
cat <<EOF >> $path/config cat <<EOF >> $path/config
lxc.utsname = $name lxc.utsname = $name
lxc.pts = 1024 lxc.pts = 1024
lxc.rootfs = $rootfs
# When using LXC with apparmor, uncomment the next line to run unconfined: # When using LXC with apparmor, uncomment the next line to run unconfined:
#lxc.aa_profile = unconfined #lxc.aa_profile = unconfined
...@@ -207,7 +207,13 @@ if [ -z "$path" ]; then ...@@ -207,7 +207,13 @@ if [ -z "$path" ]; then
exit 1 exit 1
fi 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 install_sshd $rootfs
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
......
...@@ -46,12 +46,12 @@ lxc.network.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$// ...@@ -46,12 +46,12 @@ lxc.network.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//
EOF EOF
fi fi
grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
cat <<EOF >> $path/config cat <<EOF >> $path/config
lxc.utsname = $name lxc.utsname = $name
lxc.tty = 4 lxc.tty = 4
lxc.pts = 1024 lxc.pts = 1024
lxc.rootfs = $rootfs
lxc.mount = $path/fstab lxc.mount = $path/fstab
lxc.arch = $arch lxc.arch = $arch
lxc.cap.drop = sys_module mac_admin lxc.cap.drop = sys_module mac_admin
...@@ -249,7 +249,13 @@ if [ "$(id -u)" != "0" ]; then ...@@ -249,7 +249,13 @@ if [ "$(id -u)" != "0" ]; then
exit 1 exit 1
fi 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 ubuntu-cloudimg-query
type wget type wget
......
...@@ -303,13 +303,13 @@ lxc.network.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$// ...@@ -303,13 +303,13 @@ lxc.network.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//
EOF EOF
fi fi
grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
cat <<EOF >> $path/config cat <<EOF >> $path/config
lxc.utsname = $name lxc.utsname = $name
lxc.devttydir =$ttydir lxc.devttydir =$ttydir
lxc.tty = 4 lxc.tty = 4
lxc.pts = 1024 lxc.pts = 1024
lxc.rootfs = $rootfs
lxc.mount = $path/fstab lxc.mount = $path/fstab
lxc.arch = $arch lxc.arch = $arch
lxc.cap.drop = sys_module mac_admin mac_override lxc.cap.drop = sys_module mac_admin mac_override
...@@ -670,7 +670,13 @@ if [ "$(id -u)" != "0" ]; then ...@@ -670,7 +670,13 @@ if [ "$(id -u)" != "0" ]; then
exit 1 exit 1
fi 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 install_ubuntu $rootfs $release $flushcache
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
......
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