]> git.proxmox.com Git - mirror_lxc.git/blame - src/tests/lxc-test-usernic.in
github: Update for main branch
[mirror_lxc.git] / src / tests / lxc-test-usernic.in
CommitLineData
20ab58c7
SH
1#!/bin/bash
2
3# lxc: linux Container library
4
5# Authors:
6# Serge Hallyn <serge.hallyn@ubuntu.com>
7#
8# This is a test script for the lxc-user-nic program
9
10# This library is free software; you can redistribute it and/or
11# modify it under the terms of the GNU Lesser General Public
12# License as published by the Free Software Foundation; either
13# version 2.1 of the License, or (at your option) any later version.
14
15# This library is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18# Lesser General Public License for more details.
19
20# You should have received a copy of the GNU Lesser General Public
21# License along with this library; if not, write to the Free Software
250b1eec 22# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20ab58c7 23
061ba5d0
SG
24# This test assumes an Ubuntu host
25
0e17b9c6 26DONE=0
8cb8e496 27LXC_USER_NIC="@LIBEXECDIR@/lxc/lxc-user-nic"
0e17b9c6 28
8befa924 29cleanup() {
9c50a526 30 set +e
0e17b9c6 31
9c50a526 32 (
c85a40bc 33 lxc-stop -n usernic-c1 -k
0e17b9c6
SG
34 lxc-destroy -n usernic-c1
35
061ba5d0 36 sed -i '/usernic-user/d' /run/lxc/nics /etc/lxc/lxc-usernet
0e17b9c6
SG
37 ifconfig usernic-br0 down
38 ifconfig usernic-br1 down
39 brctl delbr usernic-br0
40 brctl delbr usernic-br1
41
c85a40bc 42 run_cmd "lxc-stop -n b1 -k"
0e17b9c6
SG
43 pkill -u $(id -u usernic-user) -9
44
45 rm -rf /tmp/usernic-test /home/usernic-user /run/user/$(id -u usernic-user)
46
47 deluser usernic-user
48 ) >/dev/null 2>&1
49
50 if [ "$DONE" = "1" ]; then
51 echo "PASS"
52 exit 0
53 fi
54
55 echo "FAIL"
56 exit 1
57}
58
59run_cmd() {
198a3f10
SG
60 sudo -i -u usernic-user \
61 env http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
5f850cf9
EV
62 XDG_RUNTIME_DIR=/run/user/$(id -u usernic-user) ASAN_OPTIONS=${ASAN_OPTIONS:-} \
63 UBSAN_OPTIONS=${UBSAN_OPTIONS:-} $*
8befa924 64}
20ab58c7 65
0e17b9c6
SG
66set -eu
67trap cleanup EXIT SIGHUP SIGINT SIGTERM
68
8befa924
SH
69# create a test user
70deluser usernic-user || true
71useradd usernic-user
72sudo mkdir -p /home/usernic-user
79d88b03 73sudo chown usernic-user: /home/usernic-user
8befa924 74usermod -v 910000-919999 -w 910000-919999 usernic-user
8befa924 75
0e17b9c6
SG
76mkdir -p /home/usernic-user/.config/lxc/
77cat > /home/usernic-user/.config/lxc/default.conf << EOF
7fa3f2e9 78lxc.net.0.type = empty
bdcbb6b3
CB
79lxc.idmap = u 0 910000 10000
80lxc.idmap = g 0 910000 10000
8befa924 81EOF
20ab58c7 82
4c69af0c 83if command -v cgm >/dev/null 2>&1; then
2ba5eb93
SG
84 cgm create all usernic-user
85 cgm chown all usernic-user $(id -u usernic-user) $(id -g usernic-user)
86 cgm movepid all usernic-user $$
42e5c987 87elif [ -e /sys/fs/cgroup/cgmanager/sock ]; then
ef4deb7f 88 for d in $(cut -d : -f 2 /proc/self/cgroup); do
3f458ed0
SG
89 dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
90 --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.Create \
91 string:$d string:usernic-user >/dev/null
92
93 dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
94 --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.Chown \
95 string:$d string:usernic-user int32:$(id -u usernic-user) int32:$(id -g usernic-user) >/dev/null
96
97 dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
98 --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.MovePid \
99 string:$d string:usernic-user int32:$$ >/dev/null
100 done
101else
102 for d in /sys/fs/cgroup/*; do
8d5a91fc 103 [ "$d" = "/sys/fs/cgroup/unified" ] && continue
177f793a 104 [ -f $d/cgroup.clone_children ] && echo 1 > $d/cgroup.clone_children
3f458ed0 105 [ ! -d $d/lxctest ] && mkdir $d/lxctest
79d88b03 106 chown -R usernic-user: $d/lxctest
3f458ed0
SG
107 echo $$ > $d/lxctest/tasks
108 done
109fi
0e17b9c6
SG
110
111mkdir -p /run/user/$(id -u usernic-user)
79d88b03 112chown -R usernic-user: /run/user/$(id -u usernic-user) /home/usernic-user
20ab58c7 113
0e17b9c6 114# Create two test bridges
8befa924
SH
115brctl addbr usernic-br0
116brctl addbr usernic-br1
117ifconfig usernic-br0 0.0.0.0 up
118ifconfig usernic-br1 0.0.0.0 up
119
120# Create three containers
41ed9db8 121run_cmd "lxc-create -t busybox -n b1"
0e17b9c6
SG
122run_cmd "lxc-start -n b1 -d"
123p1=$(run_cmd "lxc-info -n b1 -p -H")
8befa924 124
615af4ac
SH
125lxcpath=/home/usernic-user/.local/share/lxc
126lxcname=b1
127
8befa924 128# Assign one veth, should fail as no allowed entries yet
f703d990 129if run_cmd "$LXC_USER_NIC create $lxcpath $lxcname $p1 veth usernic-br0 xx1"; then
8befa924 130 echo "FAIL: able to create nic with no entries"
0e17b9c6 131 exit 1
20ab58c7
SH
132fi
133
8befa924 134# Give him a quota of two
d08363af 135touch /etc/lxc/lxc-usernet
0e17b9c6
SG
136sed -i '/^usernic-user/d' /etc/lxc/lxc-usernet
137echo "usernic-user veth usernic-br0 2" >> /etc/lxc/lxc-usernet
8befa924
SH
138
139# Assign one veth to second bridge, should fail
f703d990 140if run_cmd "$LXC_USER_NIC create $lxcpath $lxcname $p1 veth usernic-br1 xx1"; then
8befa924 141 echo "FAIL: able to create nic with no entries"
0e17b9c6 142 exit 1
8befa924 143fi
20ab58c7 144
8befa924 145# Assign two veths, should succeed
f703d990 146if ! run_cmd "$LXC_USER_NIC create $lxcpath $lxcname $p1 veth usernic-br0 xx2"; then
8befa924 147 echo "FAIL: unable to create first nic"
0e17b9c6 148 exit 1
8befa924 149fi
0e17b9c6 150
f703d990 151if ! run_cmd "$LXC_USER_NIC create $lxcpath $lxcname $p1 veth usernic-br0 xx3"; then
8befa924 152 echo "FAIL: unable to create second nic"
0e17b9c6 153 exit 1
20ab58c7
SH
154fi
155
8befa924 156# Assign one more veth, should fail.
f703d990 157if run_cmd "$LXC_USER_NIC create $lxcpath $lxcname $p1 veth usernic-br0 xx4"; then
8befa924 158 echo "FAIL: able to create third nic"
0e17b9c6 159 exit 1
20ab58c7
SH
160fi
161
8befa924 162# Shut down and restart the container, should be able to assign more nics
c85a40bc 163run_cmd "lxc-stop -n b1 -k"
0e17b9c6
SG
164run_cmd "lxc-start -n b1 -d"
165p1=$(run_cmd "lxc-info -n b1 -p -H")
166
f703d990 167if ! run_cmd "$LXC_USER_NIC create $lxcpath $lxcname $p1 veth usernic-br0 xx5"; then
8befa924
SH
168 echo "FAIL: unable to create nic after destroying the old"
169 cleanup 1
170fi
171
c85a40bc 172run_cmd "lxc-stop -n b1 -k"
8befa924
SH
173
174# Create a root-owned ns
175lxc-create -t busybox -n usernic-c1
176lxc-start -n usernic-c1 -d
0e17b9c6 177p2=$(lxc-info -n usernic-c1 -p -H)
8befa924
SH
178
179# assign veth to it - should fail
f703d990 180if run_cmd "$LXC_USER_NIC create $lxcpath $lxcname $p2 veth usernic-br0 xx6"; then
8befa924
SH
181 echo "FAIL: able to attach nic to root-owned container"
182 cleanup 1
20ab58c7
SH
183fi
184
185echo "All tests passed"
0e17b9c6 186DONE=1