Commit 47fae013 authored by Andrey Vagin's avatar Andrey Vagin Committed by Pavel Emelyanov

zdtm: add a small program to create a zdtm container (v2)

I didn't find a way how to do that with help "unshare".
It's simpler to write this program. It looks better than tricks in
zdtm.sh.

v2: proxify exit status
Signed-off-by: 's avatarAndrey Vagin <avagin@openvz.org>
Acked-by: 's avatarSerge E. Hallyn <serge.hallyn@ubuntu.com>
Acked-by: 's avatarRuslan Kuprieiev <kupruser@gmail.com>
Acked-by: 's avatarChristopher Covington <cov@codeaurora.org>
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 0b33bac3
...@@ -4,3 +4,4 @@ ...@@ -4,3 +4,4 @@
/dev /dev
/dump /dump
/*.log /*.log
/zdtm_ct
...@@ -28,7 +28,13 @@ fault-injection: .FORCE ...@@ -28,7 +28,13 @@ fault-injection: .FORCE
zdtm_ns: $(shell echo "$(TST)" | tr ' ' '\n' | grep -P $(EXP)) zdtm_ns: $(shell echo "$(TST)" | tr ' ' '\n' | grep -P $(EXP))
zdtm_nons: $(shell echo "$(TST)" | tr ' ' '\n' | grep -vP $(EXP)) zdtm_nons: $(shell echo "$(TST)" | tr ' ' '\n' | grep -vP $(EXP))
$(TST): zdtm_ct: zdtm_ct.c
clean:
rm -rf zdtm_ct
$(MAKE) -C zdtm $@
$(TST): zdtm_ct
./zdtm.sh --ct ${ZDTM_ARGS} $(@) &> $(subst /,_,$@).log || \ ./zdtm.sh --ct ${ZDTM_ARGS} $(@) &> $(subst /,_,$@).log || \
{ flock Makefile cat $(subst /,_,$@).log; exit 1; } { flock Makefile cat $(subst /,_,$@).log; exit 1; }
.PHONY: zdtm_ns .PHONY: zdtm_ns
...@@ -881,18 +881,11 @@ while :; do ...@@ -881,18 +881,11 @@ while :; do
[ -z "$ZDTM_SH_IN_CT" ] && { [ -z "$ZDTM_SH_IN_CT" ] && {
export ZDTM_SH_IN_CT=1 export ZDTM_SH_IN_CT=1
shift shift
args="$@"
# pidns is used to avoid conflicts # pidns is used to avoid conflicts
# mntns is used to mount /proc # mntns is used to mount /proc
# net is used to avoid conflicts of parasite sockets # net is used to avoid conflicts of parasite sockets
unshare --pid --mount --ipc --net -- bash -c " make zdtm_ct &&
( ./zdtm_ct ./zdtm.sh "$@"
ip link set up dev lo &&
mount --make-rprivate / &&
umount -l /proc &&
mount -t proc proc /proc/ &&
./zdtm.sh $args
)"
exit exit
} }
shift shift
......
#define _GNU_SOURCE
#include <sched.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/mount.h>
int main(int argc, char **argv)
{
pid_t pid;
int status;
/*
*pidns is used to avoid conflicts
* mntns is used to mount /proc
* net is used to avoid conflicts of parasite sockets
*/
if (unshare(CLONE_NEWNS | CLONE_NEWPID | CLONE_NEWNET | CLONE_NEWIPC))
return 1;
pid = fork();
if (pid == 0) {
if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)) {
fprintf(stderr, "mount(/, S_REC | MS_PRIVATE)): %m");
return 1;
}
umount2("/proc", MNT_DETACH);
umount2("/dev/pts", MNT_DETACH);
if (mount("zdtm_proc", "/proc", "proc", 0, NULL)) {
fprintf(stderr, "mount(/proc): %m");
return 1;
}
if (mount("zdtm_devpts", "/dev/pts", "devpts", 0,
"newinstance,ptmxmode=0666")) {
fprintf(stderr, "mount(pts): %m");
return 1;
}
if (mount("/dev/pts/ptmx", "/dev/ptmx", NULL, MS_BIND, NULL)) {
fprintf(stderr, "mount(ptmx): %m");
return 1;
}
if (system("ip link set up dev lo"))
return 1;
execv(argv[1], argv + 1);
fprintf(stderr, "execve: %m");
return 1;
}
if (waitpid(pid, &status, 0) != pid) {
fprintf(stderr, "waitpid: %m");
return 1;
}
if (WIFEXITED(status))
return WEXITSTATUS(status);
else if (WIFSIGNALED(status))
kill(getpid(), WTERMSIG(status));
else
fprintf(stderr, "Unexpected exit status: %x\n", status);
return 1;
}
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