aboutsummaryrefslogtreecommitdiff
blob: 9e7eec3c202e9b508cf73d82fa31a9a898a281d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
# set -ex

usage() {
	echo "usage: $(basename $0) --name <name> [netstat options]"
}

help() {
	usage
	echo
	echo "execute netstat for the specified container"
	echo "with the added netstat options"
	echo
	echo "Options:"
	echo "name  : name of the container"
	echo "help  : this current help."
	echo
	echo "to be executed as root."
}

exec=""

if [ $# -eq  0 ]; then
	usage
	exit 1
fi

for i in "$@"; do
	case $i in
		-h|--help)
			help; exit 1;;
		-n|--name)
			name=$2; shift 2;;
		--exec)
			exec="exec"; shift;;
	esac
done

if [ -z "$exec" ]; then
    exec @BINDIR@/lxc-unshare -s MOUNT -- $0 -n $name --exec "$@"
fi

if [ -z "$name" ]; then
	usage
	exit 1
fi

lxc-info -n $name 2>&1 | grep -q 'STOPPED'
if [ $? -eq 0 ]; then
	echo "Container $name is not running"
	exit 1
fi

cgroups=$(mount -l -t cgroup)
cgroup_path=""

for i in "$cgroups"; do

    cgroup_name=$(echo $i | awk ' { print $1 } ')
    cgroup_path=$(echo $i | awk ' { print $3 } ')

    if [ "$cgroup_name" == "lxc" ]; then
        break;
    fi

done

if [ -z "$cgroup_path" ]; then
	cgroups=`grep -m1 -E '^[^ \t]+[ \t]+[^ \t]+[ \t]+cgroup' /proc/self/mounts`
	for i in "$cgroups"; do
	    cgroup_path=$(echo $i | awk ' { print $2 } ')
	    if [ -n $cgroup_path ]; then
		break;
	    fi
	done
fi

if [ -z "$cgroup_path" ]; then
    echo "no cgroup mount point found"
    exit 1
fi

# the container will be in:
# ${cgroup_path}.${init_cgroup_path}."lxc".$name
init_cgroup=`cat /proc/1/cgroup | awk -F: '{ print $3 }' | head -1`
final_cgroup_path=$cgroup_path/$init_cgroup/lxc
pid=$(head -1 $final_cgroup_path/$name/tasks)

if [ -z "$pid" ]; then
    echo "no process found for '$name'"
    exit 1
fi

mount -n --bind /proc/$pid/net /proc/$$/net && \
    exec netstat "$@"