[CRIU] [PATCH 8/8] test: Test and example how to dump namespace with macvlan
Pavel Emelyanov
xemul at parallels.com
Thu Dec 26 05:01:11 PST 2013
One note -- the macvlan's index _does_ change. We should
either patch iproute2 or use raw kernel API to preserve it.
Signed-off-by: Pavel Emelyanov <xemul at parallels.com>
---
test/ext-links/Makefile | 4 ++++
test/ext-links/addmv.sh | 8 +++++++
test/ext-links/addmv_raw.sh | 6 +++++
test/ext-links/mvlink.c | 28 ++++++++++++++++++++++
test/ext-links/run.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++
test/ext-links/run_ns.sh | 10 ++++++++
test/ext-links/run_wait.sh | 15 ++++++++++++
7 files changed, 129 insertions(+)
create mode 100644 test/ext-links/Makefile
create mode 100755 test/ext-links/addmv.sh
create mode 100755 test/ext-links/addmv_raw.sh
create mode 100644 test/ext-links/mvlink.c
create mode 100755 test/ext-links/run.sh
create mode 100755 test/ext-links/run_ns.sh
create mode 100755 test/ext-links/run_wait.sh
diff --git a/test/ext-links/Makefile b/test/ext-links/Makefile
new file mode 100644
index 0000000..6b32c25
--- /dev/null
+++ b/test/ext-links/Makefile
@@ -0,0 +1,4 @@
+all: mvlink.so
+
+mvlink.so: mvlink.c
+ gcc -g -Werror -Wall -shared -nostartfiles mvlink.c -o mvlink.so -iquote ../../include -fPIC
diff --git a/test/ext-links/addmv.sh b/test/ext-links/addmv.sh
new file mode 100755
index 0000000..0ccc971
--- /dev/null
+++ b/test/ext-links/addmv.sh
@@ -0,0 +1,8 @@
+#!/bin/bash
+# $1 -- link name
+# $2 -- file with namespace pid
+if [ "$CRTOOLS_SCRIPT_ACTION" == "setup-namespaces" ]; then
+ $(dirname $0)/addmv_raw.sh $1 $(cat $2)
+else
+ exit 0
+fi
diff --git a/test/ext-links/addmv_raw.sh b/test/ext-links/addmv_raw.sh
new file mode 100755
index 0000000..224f243
--- /dev/null
+++ b/test/ext-links/addmv_raw.sh
@@ -0,0 +1,6 @@
+#!/bin/bash
+# $1 -- link name
+# $2 -- pid of task in namespace
+set -x
+$ip link add link eth0 name $1 type macvlan || exit 1
+$ip link set $1 netns $2
diff --git a/test/ext-links/mvlink.c b/test/ext-links/mvlink.c
new file mode 100644
index 0000000..a1c764d
--- /dev/null
+++ b/test/ext-links/mvlink.c
@@ -0,0 +1,28 @@
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "criu-plugin.h"
+#include "criu-log.h"
+
+extern cr_plugin_init_t cr_plugin_init;
+extern cr_plugin_dump_ext_link_t cr_plugin_dump_ext_link;
+
+int cr_plugin_init(void)
+{
+ pr_info("Initialized macvlan dumper\n");
+ return 0;
+}
+
+int cr_plugin_dump_ext_link(int index, int type, char *kind)
+{
+ if (strcmp(kind, "macvlan"))
+ return -ENOTSUP;
+ else {
+ pr_info("Dump %d macvlan\n", index);
+ return 0;
+ }
+}
diff --git a/test/ext-links/run.sh b/test/ext-links/run.sh
new file mode 100755
index 0000000..0019b02
--- /dev/null
+++ b/test/ext-links/run.sh
@@ -0,0 +1,58 @@
+#!/bin/bash
+
+ip=${CR_IP_TOOL:-xip}
+mvln="mv0"
+finf="finish"
+outf="ns_output"
+pidf="ns_pid"
+criu="../../criu"
+
+export ip
+export mvln
+export finf
+export outf
+export pidf
+
+function fail {
+ $ip link del $mvln
+ touch $finf
+ echo $@
+ exit 1
+}
+
+# Build the mvlink plugin
+make
+
+set -x
+
+rm -f "$finf" "$outf" "$pidf"
+rm -rf "dump"
+
+# Unshare netns. The run_ns will exit once ns is spawned.
+unshare --net ./run_ns.sh
+nspid=$(cat $pidf)
+ps $nspid
+
+# Create and push macvlan device into it. CRIU doesn't support
+# macvlans treating them as external devices.
+./addmv_raw.sh $mvln $nspid || fail "Can't setup namespace"
+
+# Dump
+sleep 1
+mkdir dump
+$criu dump -t $nspid -D dump/ -o dump.log -v4 --lib $(pwd) || fail "Can't dump namespace"
+
+# Restore
+# Ask for the pid (shouldn't change, so just as an example), ask to call
+# script that will put macvlan device back into namespace
+sleep 1
+rm -f $pidf
+$criu restore -D dump/ -o restore.log -v4 --pidfile $(pwd)/$pidf --action-script "$(pwd)/addmv.sh $mvln $(pwd)/$pidf" -d || fail "Can't restore namespaces"
+
+# Finish and check results
+touch $finf
+set +x
+while ! egrep 'PASS|FAIL' $outf; do
+ echo "Waiting"
+ sleep 1
+done
diff --git a/test/ext-links/run_ns.sh b/test/ext-links/run_ns.sh
new file mode 100755
index 0000000..878f038
--- /dev/null
+++ b/test/ext-links/run_ns.sh
@@ -0,0 +1,10 @@
+#!/bin/bash
+set -x
+echo "NS: $$" >> $outf
+echo "Links before:" >> $outf
+$ip link list >> $outf 2>&1
+# Detach from session, terminal and parent
+setsid ./run_wait.sh < /dev/null >> $outf 2>&1 &
+# Keep pid for future reference :)
+echo "$!" > $pidf
+exit 0
diff --git a/test/ext-links/run_wait.sh b/test/ext-links/run_wait.sh
new file mode 100755
index 0000000..d600e37
--- /dev/null
+++ b/test/ext-links/run_wait.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+
+echo "Wait: $$"
+while [ ! -e "$finf" ]; do
+ echo "WAIT ($$)"
+ sleep 1;
+done
+
+echo "Links after:"
+$ip link list
+
+# The mvln device (exported from run.sh) should exits in
+# namespace after we get restored
+echo "Check for $mvln:"
+$ip link list $mvln && echo "PASS" || echo "FAIL"
--
1.8.3.1
More information about the CRIU
mailing list