[libvirt] [PATCH] Added example script on how to convert LXC container config

Cedric Bosdonnat cbosdonnat at suse.com
Mon Mar 24 12:27:45 UTC 2014


Hi,

Did anyone have some time to have a look at this small example tool?

Regards,
--
Cedric

On Tue, 2014-03-11 at 13:40 +0100, Cédric Bosdonnat wrote:
> ---
>  Makefile.am                          |  2 +-
>  configure.ac                         |  1 +
>  examples/lxcconvert/Makefile.am      | 19 ++++++++++
>  examples/lxcconvert/virt-lxc-convert | 67 ++++++++++++++++++++++++++++++++++++
>  4 files changed, 88 insertions(+), 1 deletion(-)
>  create mode 100644 examples/lxcconvert/Makefile.am
>  create mode 100644 examples/lxcconvert/virt-lxc-convert
> 
> diff --git a/Makefile.am b/Makefile.am
> index 9847ff0..0ef983f 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -23,7 +23,7 @@ SUBDIRS = . gnulib/lib include src daemon tools docs gnulib/tests \
>    tests po examples/object-events examples/hellolibvirt \
>    examples/dominfo examples/domsuspend examples/apparmor \
>    examples/xml/nwfilter examples/openauth examples/systemtap \
> -  tools/wireshark
> +  examples/lxcconvert tools/wireshark
>  
>  ACLOCAL_AMFLAGS = -I m4
>  
> diff --git a/configure.ac b/configure.ac
> index 62b74c5..f970de0 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -2695,6 +2695,7 @@ AC_CONFIG_FILES([\
>          examples/hellolibvirt/Makefile \
>          examples/systemtap/Makefile \
>          examples/xml/nwfilter/Makefile \
> +        examples/lxcconvert/Makefile \
>          tools/wireshark/Makefile \
>          tools/wireshark/src/Makefile])
>  AC_OUTPUT
> diff --git a/examples/lxcconvert/Makefile.am b/examples/lxcconvert/Makefile.am
> new file mode 100644
> index 0000000..c5ae537
> --- /dev/null
> +++ b/examples/lxcconvert/Makefile.am
> @@ -0,0 +1,19 @@
> +## Copyright (C) 2014 SUSE LINUX Products GmbH, Nuernberg, Germany. 
> +##
> +## This library is free software; you can redistribute it and/or
> +## modify it under the terms of the GNU Lesser General Public
> +## License as published by the Free Software Foundation; either
> +## version 2.1 of the License, or (at your option) any later version.
> +##
> +## This library is distributed in the hope that it will be useful,
> +## but WITHOUT ANY WARRANTY; without even the implied warranty of
> +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +## Lesser General Public License for more details.
> +##
> +## You should have received a copy of the GNU Lesser General Public
> +## License along with this library.  If not, see
> +## <http://www.gnu.org/licenses/>.
> +
> +EXTRA_DIST=				\
> +	virt-lxc-convert
> +
> diff --git a/examples/lxcconvert/virt-lxc-convert b/examples/lxcconvert/virt-lxc-convert
> new file mode 100644
> index 0000000..ea32ff4
> --- /dev/null
> +++ b/examples/lxcconvert/virt-lxc-convert
> @@ -0,0 +1,67 @@
> +#!/usr/bin/env sh
> +
> +function show_help()
> +{
> +    cat << EOF
> +$0 /path/to/lxc/config/file
> +
> +Wrapper around virsh domxml-from-native to ease conversion of LXC
> +containers configuration to libvirt domain XML.
> +EOF
> +}
> +
> +if test "$#" != 1; then
> +    show_help
> +    exit 1
> +fi
> +
> +conf=$1
> +
> +
> +conf_new=/tmp/config-$$
> +domain_new=/tmp/config-$$.xml
> +
> +cp $conf $conf_new
> +
> +# Do we have lxc.mount, and is it pointing to a readable file?
> +fstab=`grep 'lxc.mount[[:space:]]*=' $conf_new | sed -e 's/[[:space:]]\+=[[:space:]]\+/=/' | cut -f 2 -d '='`
> +if test \( -n "$fstab" \) -a \( -r "$fstab" \); then
> +    sed -i -e 's/^lxc.mount[[:space:]]*=.*$//' $conf_new
> +    sed -e 's/^\([^#]\)/lxc.mount.entry = \1/' $fstab | cat $conf_new - >${conf_new}.tmp
> +    mv ${conf_new}.tmp $conf_new
> +fi
> +
> +memory=`free | grep 'Mem:' | sed -e 's: \+: :g' | cut -f 2 -d ' '`
> +default_tmpfs="size=$((memory/2))"
> +
> +# Do we have tmpfs without size param?
> +grep -e 'lxc.mount.entry[[:space:]]*=[[:space:]]*tmpfs[[:space:]]' $conf_new | while read line; do
> +    has_size=`echo $line | grep -v -e 'size='`
> +    # Add the default size here (50%) if no size is given
> +    if test -n "$has_size"; then
> +        sed -i -e "\;$line;s/\([[:space:]]\+[0-9][[:space:]]\+[0-9][::space::]*$\)/,$default_tmpfs\1/" $conf_new
> +    fi
> +    # Convert relative sizes
> +    has_rel_size=`echo $line | grep -e 'size=[0-9]\+%'`
> +    if test -n "$has_rel_size"; then
> +        percent=`echo "$line" | sed -e 's:size=\([0-9]\+\)%:\n\1%\n:' | grep '\%' | sed -e 's/%//'`
> +        size="$((memory*percent/100))"
> +        sed -i -e "\;$line;s/size=[0-9]\+%/size=${size}/" $conf_new
> +    fi
> +done
> +
> +# Do we have any memory limit set?
> +mem_limit=`grep 'lxc.cgroup.memory.limit_in_bytes[[:space:]]*=' $conf_new`
> +if test -z "$mem_limit"; then
> +    sed -i -e "1s:^:lxc.cgroup.memory.limit_in_bytes = $memory\n:" $conf_new
> +fi
> +
> +virsh -c lxc:/// domxml-from-native lxc-tools $conf_new >$domain_new
> +
> +cat $domain_new
> +
> +# Remove the temporary config
> +rm $conf_new
> +rm $domain_new
> +
> +exit 0





More information about the libvir-list mailing list