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

Cédric Bosdonnat cbosdonnat at suse.com
Fri Mar 28 16:31:22 UTC 2014


---
 Makefile.am                          |   2 +-
 configure.ac                         |   1 +
 examples/lxcconvert/Makefile.am      |  18 ++++++
 examples/lxcconvert/virt-lxc-convert | 108 +++++++++++++++++++++++++++++++++++
 4 files changed, 128 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 73efffa..f84d4bb 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2699,6 +2699,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..09cf5d9
--- /dev/null
+++ b/examples/lxcconvert/Makefile.am
@@ -0,0 +1,18 @@
+## 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..a6c5721
--- /dev/null
+++ b/examples/lxcconvert/virt-lxc-convert
@@ -0,0 +1,108 @@
+#!/bin/sh
+
+handler_cleanup()
+{
+    if ! test -z "$conf_dir"; then
+        # Remove the temporary config
+        rm -r "$conf_dir"
+    fi
+}
+trap handler_cleanup INT EXIT
+
+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
+
+if test "$1" = "--help" || test "$1" = "-h"; then
+    show_help
+    exit $?
+fi
+
+conf=$1
+
+conf_dir=$(mktemp --tmpdir -d virt-lxc-convert-XXX)
+conf_new=$conf_dir/config
+
+cp "$conf" "$conf_new"
+
+# Do we have lxc.mount, and is it pointing to a readable file?
+fstab=$(sed -n '/lxc.mount[[:space:]]*=/ s/[[:space:]]*=[[:space:]]*/=/p' \
+        "$conf_new" | cut -f 2 -d '=')
+if test -n "$fstab" && test -r "$fstab"; then
+    sed 's/^lxc.mount[[:space:]]*=.*$//' "$conf_new" >"${conf_new}.tmp"
+    mv "${conf_new}.tmp" "${conf_new}"
+    sed 's/^\([^#]\)/lxc.mount.entry = \1/' "$fstab" >>"${conf_new}"
+fi
+
+memory=$(free | sed -n '/Mem:/s/ \+/ /gp' | cut -f 2 -d ' ')
+default_tmpfs="size=$((memory/2))"
+
+# Do we have tmpfs without size param?
+lineno=0
+while read line; do
+    lineno=$(expr $lineno + 1)
+    has_rel_size=false
+    case $line in
+        lxc.mount.entry[[:space:]]*=[[:space:]]*tmpfs[[:space:]]*)
+            is_tmpfs=true
+            ;;
+        *)
+            is_tmpfs=false
+            ;;
+    esac
+
+    # We only care about tmpfs mount entries here
+    if ! $is_tmpfs; then
+        continue
+    fi
+
+    case $line in
+        *size=[0-9][0-9]*%*)
+            has_rel_size=true
+            has_size=true
+            ;;
+        *size=*)
+            has_size=true
+            ;;
+        *)
+            has_size=false
+            ;;
+    esac
+
+    # Add the default size here (50%) if no size is given
+    if ! $has_size; then
+        last_option_match="\([[:space:]]*[0-9][[:space:]]*[0-9][::space::]*$\)"
+        sed "${lineno}s/$last_option_match/,$default_tmpfs\1/" \
+            "$conf_new" >"${conf_new}.tmp"
+        mv "${conf_new}.tmp" "${conf_new}"
+    fi
+
+    # Convert relative sizes
+    if $has_rel_size; then
+        percent=$(echo "$line" | sed 's/.*size=\([0-9][0-9]*\)%.*/\1/')
+        size="$((memory*percent/100))"
+        sed "${lineno}s/size=[0-9]*%/size=${size}/" \
+            "$conf_new" >"${conf_new}.tmp"
+        mv "${conf_new}.tmp" "${conf_new}"
+    fi
+done < "$conf_new"
+
+# 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
+    echo "lxc.cgroup.memory.limit_in_bytes = $memory" >> "$conf_new"
+fi
+
+virsh -c lxc:/// domxml-from-native lxc-tools $conf_new
+exit $?
-- 
1.8.4.5




More information about the libvir-list mailing list