]>
Commit | Line | Data |
---|---|---|
d08363af 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 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 | ||
061ba5d0 SG |
24 | # This test assumes an Ubuntu host |
25 | ||
d08363af | 26 | if [ $(id -u) -ne 0 ]; then |
c26adb82 | 27 | echo "ERROR: Must run as root." |
d08363af SH |
28 | exit 1 |
29 | fi | |
90e4eead CB |
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 | ||
706f1101 PHL |
42 | mkdir ${MOUNTDIR}/{lowerdir,upperdir,workdir,overlayfs} |
43 | mount -t overlayfs -o lowerdir="${MOUNTDIR}/lowerdir",upperdir="${MOUNTDIR}/upperdir",workdir="${MOUNTDIR}/workdir" none "${MOUNTDIR}/overlayfs" | |
90e4eead | 44 | |
8de0119d | 45 | CORRECT_LINK_TARGET="${MOUNTDIR}/overlayfs/placeholder_file" |
90e4eead CB |
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 | ||
d08363af SH |
65 | which newuidmap >/dev/null 2>&1 || { echo "'newuidmap' command is missing" >&2; exit 1; } |
66 | ||
67 | DONE=0 | |
0815a592 | 68 | KNOWN_RELEASES="precise trusty xenial yakkety zesty" |
17d9d072 | 69 | UNPRIV_LOG=$(mktemp --dry-run) |
d08363af | 70 | cleanup() { |
6ebc0504 | 71 | cd / |
73d3e090 | 72 | |
6bc4165d CB |
73 | if [ $DONE -eq 0 ]; then |
74 | cat "${UNPRIV_LOG}" | |
75 | fi | |
76 | rm -f "${UNPRIV_LOG}" || true | |
77 | ||
17d9d072 CB |
78 | run_cmd lxc-stop -n c2 -k -l trace -o "${UNPRIV_LOG}" || true |
79 | run_cmd lxc-stop -n c1 -k -l trace -o "${UNPRIV_LOG}" || true | |
d0ab6d91 | 80 | pkill -u $(id -u $TUSER) -9 || true |
73d3e090 | 81 | |
061ba5d0 | 82 | sed -i '/lxcunpriv/d' /run/lxc/nics /etc/lxc/lxc-usernet |
d08363af | 83 | sed -i '/^lxcunpriv:/d' /etc/subuid /etc/subgid |
73d3e090 SG |
84 | |
85 | rm -Rf $HDIR /run/user/$(id -u $TUSER) | |
86 | ||
87 | deluser $TUSER | |
88 | ||
d08363af | 89 | if [ $DONE -eq 0 ]; then |
73d3e090 SG |
90 | echo "FAIL" |
91 | exit 1 | |
d08363af | 92 | fi |
6bc4165d | 93 | |
73d3e090 SG |
94 | echo "PASS" |
95 | } | |
96 | ||
97 | run_cmd() { | |
198a3f10 SG |
98 | sudo -i -u $TUSER \ |
99 | env http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \ | |
5f850cf9 EV |
100 | XDG_RUNTIME_DIR=/run/user/$(id -u $TUSER) ASAN_OPTIONS=${ASAN_OPTIONS:-} \ |
101 | UBSAN_OPTIONS=${UBSAN_OPTIONS:-} $* | |
d08363af SH |
102 | } |
103 | ||
104 | # create a test user | |
105 | TUSER=lxcunpriv | |
106 | HDIR=/home/$TUSER | |
107 | ||
fd2b7320 SG |
108 | ARCH=i386 |
109 | if type dpkg >/dev/null 2>&1; then | |
110 | ARCH=$(dpkg --print-architecture) | |
111 | fi | |
112 | ||
d08363af | 113 | trap cleanup EXIT SIGHUP SIGINT SIGTERM |
73d3e090 | 114 | set -eu |
d08363af | 115 | |
438c96e9 | 116 | id $TUSER &> /dev/null && deluser -q --remove-home $TUSER |
d08363af | 117 | useradd $TUSER |
73d3e090 SG |
118 | |
119 | mkdir -p $HDIR | |
9a64d3cf | 120 | echo "$TUSER veth lxcbr0 2" >> /etc/lxc/lxc-usernet |
d08363af | 121 | sed -i '/^lxcunpriv:/d' /etc/subuid /etc/subgid |
73d3e090 | 122 | |
d08363af | 123 | usermod -v 910000-919999 -w 910000-919999 $TUSER |
d08363af | 124 | |
73d3e090 SG |
125 | mkdir -p $HDIR/.config/lxc/ |
126 | cat > $HDIR/.config/lxc/default.conf << EOF | |
7fa3f2e9 | 127 | lxc.net.0.type = veth |
128 | lxc.net.0.link = lxcbr0 | |
bdcbb6b3 CB |
129 | lxc.idmap = u 0 910000 9999 |
130 | lxc.idmap = g 0 910000 9999 | |
d08363af | 131 | EOF |
79d88b03 | 132 | chown -R $TUSER: $HDIR |
73d3e090 SG |
133 | |
134 | mkdir -p /run/user/$(id -u $TUSER) | |
79d88b03 | 135 | chown -R $TUSER: /run/user/$(id -u $TUSER) |
73d3e090 SG |
136 | |
137 | cd $HDIR | |
d08363af | 138 | |
42e5c987 SG |
139 | if which cgm >/dev/null 2>&1; then |
140 | cgm create all $TUSER | |
141 | cgm chown all $TUSER $(id -u $TUSER) $(id -g $TUSER) | |
142 | cgm movepid all $TUSER $$ | |
143 | elif [ -e /sys/fs/cgroup/cgmanager/sock ]; then | |
ef4deb7f | 144 | for d in $(cut -d : -f 2 /proc/self/cgroup); do |
3f458ed0 SG |
145 | dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \ |
146 | --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.Create \ | |
147 | string:$d string:$TUSER >/dev/null | |
148 | ||
149 | dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \ | |
150 | --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.Chown \ | |
151 | string:$d string:$TUSER int32:$(id -u $TUSER) int32:$(id -g $TUSER) >/dev/null | |
152 | ||
153 | dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \ | |
154 | --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.MovePid \ | |
155 | string:$d string:$TUSER int32:$$ >/dev/null | |
156 | done | |
157 | else | |
158 | for d in /sys/fs/cgroup/*; do | |
8d5a91fc | 159 | [ "$d" = "/sys/fs/cgroup/unified" ] && continue |
177f793a | 160 | [ -f $d/cgroup.clone_children ] && echo 1 > $d/cgroup.clone_children |
3f458ed0 | 161 | [ ! -d $d/lxctest ] && mkdir $d/lxctest |
79d88b03 | 162 | chown -R $TUSER: $d/lxctest |
3f458ed0 SG |
163 | echo $$ > $d/lxctest/tasks |
164 | done | |
165 | fi | |
73d3e090 | 166 | |
64ea46c7 PHL |
167 | # default release is trusty, or the systems release if recognized |
168 | release=trusty | |
169 | if [ -f /etc/lsb-release ]; then | |
170 | . /etc/lsb-release | |
171 | rels=$(ubuntu-distro-info --supported 2>/dev/null) || | |
172 | rels="$KNOWN_RELEASES" | |
173 | for r in $rels; do | |
174 | [ "$DISTRIB_CODENAME" = "$r" ] && release="$r" | |
175 | done | |
176 | fi | |
177 | ||
57221f67 SG |
178 | # Copy the download template cache if available |
179 | run_cmd mkdir -p $HDIR/.cache/lxc | |
180 | [ -d /var/cache/lxc/download ] && \ | |
181 | cp -R /var/cache/lxc/download $HDIR/.cache/lxc && \ | |
182 | chown -R $TUSER: $HDIR/.cache/lxc | |
183 | ||
17d9d072 | 184 | run_cmd lxc-create -t download -n c1 -l trace -o "${UNPRIV_LOG}" -- -d ubuntu -r $release -a $ARCH |
73d3e090 | 185 | |
a17d94a5 | 186 | # Make sure we can start it - twice |
d08363af | 187 | |
a17d94a5 | 188 | for count in `seq 1 2`; do |
17d9d072 | 189 | run_cmd lxc-start -n c1 -d -l trace -o "${UNPRIV_LOG}" |
a17d94a5 | 190 | |
17d9d072 | 191 | p1=$(run_cmd lxc-info -n c1 -p -H -l trace -o "${UNPRIV_LOG}") |
a17d94a5 SH |
192 | [ "$p1" != "-1" ] || { echo "Failed to start container c1 (run $count)"; false; } |
193 | ||
17d9d072 CB |
194 | run_cmd lxc-info -n c1 -l trace -o "${UNPRIV_LOG}" |
195 | run_cmd lxc-attach -n c1 -l trace -o "${UNPRIV_LOG}" -- /bin/true | |
a17d94a5 | 196 | |
17d9d072 | 197 | run_cmd lxc-stop -n c1 -k -l trace -o "${UNPRIV_LOG}" |
a17d94a5 | 198 | done |
73d3e090 | 199 | |
17d9d072 CB |
200 | run_cmd lxc-copy -s -n c1 -N c2 -l trace -o "${UNPRIV_LOG}" |
201 | run_cmd lxc-start -n c2 -d -l trace -o "${UNPRIV_LOG}" | |
202 | p1=$(run_cmd lxc-info -n c2 -p -H -l trace -o "${UNPRIV_LOG}") | |
3ad30ff7 SH |
203 | [ "$p1" != "-1" ] || { echo "Failed to start container c2"; false; } |
204 | ||
17d9d072 | 205 | run_cmd lxc-stop -n c2 -k -l trace -o "${UNPRIV_LOG}" |
3ad30ff7 | 206 | |
e2ef635e SH |
207 | if which cgm >/dev/null 2>&1; then |
208 | echo "Testing containers under different cgroups per subsystem" | |
209 | run_cmd cgm create freezer x1/x2 | |
210 | cgm movepid freezer x1 $$ | |
17d9d072 CB |
211 | run_cmd lxc-start -n c1 -d -l trace -o "${UNPRIV_LOG}" |
212 | p1=$(run_cmd lxc-info -n c1 -p -H -l trace -o "${UNPRIV_LOG}") | |
e2ef635e | 213 | [ "$p1" != "-1" ] || { echo "Failed to start container c1"; false; } |
17d9d072 CB |
214 | run_cmd lxc-info -n c1 -l trace -o "${UNPRIV_LOG}" |
215 | run_cmd lxc-attach -n c1 -l trace -o "${UNPRIV_LOG}" -- /bin/true | |
216 | run_cmd lxc-cgroup -n c1 freezer.state -l trace -o "${UNPRIV_LOG}" | |
e2ef635e SH |
217 | |
218 | echo "Testing lxc-attach and lxc-cgroup from different cgroup" | |
219 | cgm movepid freezer x2 $$ | |
17d9d072 CB |
220 | run_cmd lxc-attach -n c1 -l trace -o "${UNPRIV_LOG}" -- /bin/true |
221 | run_cmd lxc-cgroup -n c1 -l trace -o "${UNPRIV_LOG}" freezer.state | |
222 | run_cmd lxc-cgroup -n c1 -l trace -o "${UNPRIV_LOG}" memory.limit_in_bytes | |
e2ef635e SH |
223 | fi |
224 | ||
d08363af | 225 | DONE=1 |