]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/lxc-test-unpriv
tests: don't fail when no processes for user exist
[mirror_lxc.git] / src / tests / lxc-test-unpriv
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 unprivileged containers
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
22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
24 # This test assumes an Ubuntu host
25
26 if [ $(id -u) -ne 0 ]; then
27 echo "ERROR: Must run as root."
28 exit 1
29 fi
30
31 # Test if we're using an overlayfs module that handles symlinks correctly. If
32 # not, we skip these tests since overlay clones will not work correctly.
33 if modprobe -q overlayfs; then
34 TMPDIR=$(mktemp -d)
35
36 MOUNTDIR="${TMPDIR}/ovl_symlink_test"
37
38 mkdir ${MOUNTDIR}
39
40 mount -t tmpfs none ${MOUNTDIR}
41
42 mkdir "${MOUNTDIR}/lowerdir" "${MOUNTDIR}/upperdir" "${MOUNTDIR}/overlayfs"
43 mount -t overlayfs -o lowerdir="${MOUNTDIR}/lowerdir",upperdir="${MOUNTDIR}/upperdir" none "${MOUNTDIR}/overlayfs"
44
45 CORRECT_LINK_TARGET="${MOUNTDIR}/overlayfs/dummy_file"
46 exec 9> "${CORRECT_LINK_TARGET}"
47
48 DETECTED_LINK_TARGET=$(readlink -q /proc/$$/fd/9)
49
50 # cleanup
51 exec 9>&-
52
53 umount "${MOUNTDIR}/overlayfs"
54 umount ${MOUNTDIR}
55
56 rmdir ${MOUNTDIR}
57
58 # This overlay module does not correctly handle symlinks, so skip the
59 # tests.
60 if [ "${DETECTED_LINK_TARGET}" != "${CORRECT_LINK_TARGET}" ]; then
61 exit 0
62 fi
63 fi
64
65 which newuidmap >/dev/null 2>&1 || { echo "'newuidmap' command is missing" >&2; exit 1; }
66
67 DONE=0
68 KNOWN_RELEASES="precise trusty xenial yakkety zesty"
69 cleanup() {
70 cd /
71
72 run_cmd lxc-stop -n c2 -k || true
73 run_cmd lxc-stop -n c1 -k || true
74 pkill -u $(id -u $TUSER) -9 || true
75
76 sed -i '/lxcunpriv/d' /run/lxc/nics /etc/lxc/lxc-usernet
77 sed -i '/^lxcunpriv:/d' /etc/subuid /etc/subgid
78
79 rm -Rf $HDIR /run/user/$(id -u $TUSER)
80
81 deluser $TUSER
82
83 if [ $DONE -eq 0 ]; then
84 echo "FAIL"
85 exit 1
86 fi
87 echo "PASS"
88 }
89
90 run_cmd() {
91 sudo -i -u $TUSER \
92 env http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
93 XDG_RUNTIME_DIR=/run/user/$(id -u $TUSER) $*
94 }
95
96 # create a test user
97 TUSER=lxcunpriv
98 HDIR=/home/$TUSER
99
100 ARCH=i386
101 if type dpkg >/dev/null 2>&1; then
102 ARCH=$(dpkg --print-architecture)
103 fi
104
105 trap cleanup EXIT SIGHUP SIGINT SIGTERM
106 set -eu
107
108 deluser $TUSER && rm -Rf $HDIR || true
109 useradd $TUSER
110
111 mkdir -p $HDIR
112 echo "$TUSER veth lxcbr0 2" >> /etc/lxc/lxc-usernet
113 sed -i '/^lxcunpriv:/d' /etc/subuid /etc/subgid
114
115 usermod -v 910000-919999 -w 910000-919999 $TUSER
116
117 mkdir -p $HDIR/.config/lxc/
118 cat > $HDIR/.config/lxc/default.conf << EOF
119 lxc.network.type = veth
120 lxc.network.link = lxcbr0
121 lxc.id_map = u 0 910000 9999
122 lxc.id_map = g 0 910000 9999
123 EOF
124 chown -R $TUSER: $HDIR
125
126 mkdir -p /run/user/$(id -u $TUSER)
127 chown -R $TUSER: /run/user/$(id -u $TUSER)
128
129 cd $HDIR
130
131 if which cgm >/dev/null 2>&1; then
132 cgm create all $TUSER
133 cgm chown all $TUSER $(id -u $TUSER) $(id -g $TUSER)
134 cgm movepid all $TUSER $$
135 elif [ -e /sys/fs/cgroup/cgmanager/sock ]; then
136 for d in $(cut -d : -f 2 /proc/self/cgroup); do
137 dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
138 --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.Create \
139 string:$d string:$TUSER >/dev/null
140
141 dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
142 --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.Chown \
143 string:$d string:$TUSER int32:$(id -u $TUSER) int32:$(id -g $TUSER) >/dev/null
144
145 dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
146 --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.MovePid \
147 string:$d string:$TUSER int32:$$ >/dev/null
148 done
149 else
150 for d in /sys/fs/cgroup/*; do
151 [ -f $d/cgroup.clone_children ] && echo 1 > $d/cgroup.clone_children
152 [ ! -d $d/lxctest ] && mkdir $d/lxctest
153 chown -R $TUSER: $d/lxctest
154 echo $$ > $d/lxctest/tasks
155 done
156 fi
157
158 # default release is trusty, or the systems release if recognized
159 release=trusty
160 if [ -f /etc/lsb-release ]; then
161 . /etc/lsb-release
162 rels=$(ubuntu-distro-info --supported 2>/dev/null) ||
163 rels="$KNOWN_RELEASES"
164 for r in $rels; do
165 [ "$DISTRIB_CODENAME" = "$r" ] && release="$r"
166 done
167 fi
168
169 # Copy the download template cache if available
170 run_cmd mkdir -p $HDIR/.cache/lxc
171 [ -d /var/cache/lxc/download ] && \
172 cp -R /var/cache/lxc/download $HDIR/.cache/lxc && \
173 chown -R $TUSER: $HDIR/.cache/lxc
174
175 run_cmd lxc-create -t download -n c1 -- -d ubuntu -r $release -a $ARCH
176
177 # Make sure we can start it - twice
178
179 for count in `seq 1 2`; do
180 run_cmd lxc-start -n c1 -d
181
182 p1=$(run_cmd lxc-info -n c1 -p -H)
183 [ "$p1" != "-1" ] || { echo "Failed to start container c1 (run $count)"; false; }
184
185 run_cmd lxc-info -n c1
186 run_cmd lxc-attach -n c1 -- /bin/true
187
188 run_cmd lxc-stop -n c1 -k
189 done
190
191 run_cmd lxc-copy -s -n c1 -N c2
192 run_cmd lxc-start -n c2 -d
193 p1=$(run_cmd lxc-info -n c2 -p -H)
194 [ "$p1" != "-1" ] || { echo "Failed to start container c2"; false; }
195
196 run_cmd lxc-stop -n c2 -k
197
198 if which cgm >/dev/null 2>&1; then
199 echo "Testing containers under different cgroups per subsystem"
200 run_cmd cgm create freezer x1/x2
201 cgm movepid freezer x1 $$
202 run_cmd lxc-start -n c1 -d
203 p1=$(run_cmd lxc-info -n c1 -p -H)
204 [ "$p1" != "-1" ] || { echo "Failed to start container c1"; false; }
205 run_cmd lxc-info -n c1
206 run_cmd lxc-attach -n c1 -- /bin/true
207 run_cmd lxc-cgroup -n c1 freezer.state
208
209 echo "Testing lxc-attach and lxc-cgroup from different cgroup"
210 cgm movepid freezer x2 $$
211 run_cmd lxc-attach -n c1 -- /bin/true
212 run_cmd lxc-cgroup -n c1 freezer.state
213 run_cmd lxc-cgroup -n c1 memory.limit_in_bytes
214 fi
215
216 DONE=1