3 # lxc: linux Container library
6 # Serge Hallyn <serge.hallyn@ubuntu.com>
8 # This is a test script for unprivileged containers
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.
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.
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
24 # This test assumes an Ubuntu host
26 if [ $
(id
-u) -ne 0 ]; then
27 echo "ERROR: Must run as root."
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
36 MOUNTDIR
="${TMPDIR}/ovl_symlink_test"
40 mount
-t tmpfs none
${MOUNTDIR}
42 mkdir
${MOUNTDIR}/{lowerdir
,upperdir
,workdir
,overlayfs
}
43 mount
-t overlayfs
-o lowerdir
="${MOUNTDIR}/lowerdir",upperdir="${MOUNTDIR}/upperdir",workdir="${MOUNTDIR}/workdir" none "${MOUNTDIR}/overlayfs"
45 CORRECT_LINK_TARGET
="${MOUNTDIR}/overlayfs/placeholder_file"
46 exec 9> "${CORRECT_LINK_TARGET}"
48 DETECTED_LINK_TARGET
=$
(readlink
-q /proc
/$$
/fd
/9)
53 umount
"${MOUNTDIR}/overlayfs"
58 # This overlay module does not correctly handle symlinks, so skip the
60 if [ "${DETECTED_LINK_TARGET}" != "${CORRECT_LINK_TARGET}" ]; then
65 command -v newuidmap
>/dev
/null
2>&1 ||
{ echo "'newuidmap' command is missing" >&2; exit 1; }
68 UNPRIV_LOG
=$
(mktemp
--dry-run)
72 if [ $DONE -eq 0 ]; then
75 rm -f "${UNPRIV_LOG}" || true
77 run_cmd lxc-stop
-n c2
-k -l trace
-o "${UNPRIV_LOG}" || true
78 run_cmd lxc-stop
-n c1
-k -l trace
-o "${UNPRIV_LOG}" || true
79 pkill
-u $
(id
-u $TUSER) -9 || true
81 sed -i '/lxcunpriv/d' /run
/lxc
/nics
/etc
/lxc
/lxc-usernet
82 sed -i '/^lxcunpriv:/d' /etc
/subuid
/etc
/subgid
84 rm -Rf $HDIR /run
/user
/$
(id
-u $TUSER)
88 if [ $DONE -eq 0 ]; then
98 env http_proxy
=${http_proxy:-} https_proxy
=${https_proxy:-} \
99 XDG_RUNTIME_DIR
=/run
/user
/$
(id
-u $TUSER) ASAN_OPTIONS
=${ASAN_OPTIONS:-} \
100 UBSAN_OPTIONS
=${UBSAN_OPTIONS:-} $
*
107 trap cleanup EXIT SIGHUP SIGINT SIGTERM
110 id
$TUSER &> /dev
/null
&& deluser
-q --remove-home $TUSER
114 echo "$TUSER veth lxcbr0 2" >> /etc
/lxc
/lxc-usernet
115 sed -i '/^lxcunpriv:/d' /etc
/subuid
/etc
/subgid
117 usermod
-v 910000-919999 -w 910000-919999 $TUSER
119 mkdir
-p $HDIR/.config
/lxc
/
120 cat > $HDIR/.config
/lxc
/default.conf
<< EOF
121 lxc.net.0.type = veth
122 lxc.net.0.link = lxcbr0
123 lxc.idmap = u 0 910000 9999
124 lxc.idmap = g 0 910000 9999
126 chown
-R $TUSER: $HDIR
128 mkdir
-p /run
/user
/$
(id
-u $TUSER)
129 chown
-R $TUSER: /run
/user
/$
(id
-u $TUSER)
133 if command -v cgm
>/dev
/null
2>&1; then
134 cgm create all
$TUSER
135 cgm chown all
$TUSER $
(id
-u $TUSER) $
(id
-g $TUSER)
136 cgm movepid all
$TUSER $$
137 elif [ -e /sys
/fs
/cgroup
/cgmanager
/sock
]; then
138 for d
in $
(cut
-d : -f 2 /proc
/self
/cgroup
); do
139 dbus-send
--print-reply --address=unix
:path
=/sys
/fs
/cgroup
/cgmanager
/sock \
140 --type=method_call
/org
/linuxcontainers
/cgmanager org.linuxcontainers.cgmanager0_0.Create \
141 string
:$d string
:$TUSER >/dev
/null
143 dbus-send
--print-reply --address=unix
:path
=/sys
/fs
/cgroup
/cgmanager
/sock \
144 --type=method_call
/org
/linuxcontainers
/cgmanager org.linuxcontainers.cgmanager0_0.Chown \
145 string
:$d string
:$TUSER int32
:$
(id
-u $TUSER) int32
:$
(id
-g $TUSER) >/dev
/null
147 dbus-send
--print-reply --address=unix
:path
=/sys
/fs
/cgroup
/cgmanager
/sock \
148 --type=method_call
/org
/linuxcontainers
/cgmanager org.linuxcontainers.cgmanager0_0.MovePid \
149 string
:$d string
:$TUSER int32
:$$
>/dev
/null
152 for d
in /sys
/fs
/cgroup
/*; do
153 [ "$d" = "/sys/fs/cgroup/unified" ] && continue
154 [ -f $d/cgroup.clone_children
] && echo 1 > $d/cgroup.clone_children
155 [ ! -d $d/lxctest
] && mkdir
$d/lxctest
156 chown
-R $TUSER: $d/lxctest
157 echo $$
> $d/lxctest
/tasks
161 run_cmd lxc-create
-t busybox
-n c1
-l trace
-o "${UNPRIV_LOG}"
163 # Make sure we can start it - twice
165 for count
in $
(seq 1 2); do
166 run_cmd lxc-start
-n c1
-d -l trace
-o "${UNPRIV_LOG}"
168 p1
=$
(run_cmd lxc-info
-n c1
-p -H -l trace
-o "${UNPRIV_LOG}")
169 [ "$p1" != "-1" ] ||
{ echo "Failed to start container c1 (run $count)"; false
; }
171 run_cmd lxc-info
-n c1
-l trace
-o "${UNPRIV_LOG}"
172 run_cmd lxc-attach
-n c1
-l trace
-o "${UNPRIV_LOG}" -- /bin
/true
174 run_cmd lxc-stop
-n c1
-k -l trace
-o "${UNPRIV_LOG}"
177 run_cmd lxc-copy
-s -n c1
-N c2
-l trace
-o "${UNPRIV_LOG}"
178 run_cmd lxc-start
-n c2
-d -l trace
-o "${UNPRIV_LOG}"
179 p1
=$
(run_cmd lxc-info
-n c2
-p -H -l trace
-o "${UNPRIV_LOG}")
180 [ "$p1" != "-1" ] ||
{ echo "Failed to start container c2"; false
; }
182 run_cmd lxc-stop
-n c2
-k -l trace
-o "${UNPRIV_LOG}"
184 if command -v cgm
>/dev
/null
2>&1; then
185 echo "Testing containers under different cgroups per subsystem"
186 run_cmd cgm create freezer x
1/x2
187 cgm movepid freezer x1 $$
188 run_cmd lxc-start
-n c1
-d -l trace
-o "${UNPRIV_LOG}"
189 p1
=$
(run_cmd lxc-info
-n c1
-p -H -l trace
-o "${UNPRIV_LOG}")
190 [ "$p1" != "-1" ] ||
{ echo "Failed to start container c1"; false
; }
191 run_cmd lxc-info
-n c1
-l trace
-o "${UNPRIV_LOG}"
192 run_cmd lxc-attach
-n c1
-l trace
-o "${UNPRIV_LOG}" -- /bin
/true
193 run_cmd lxc-cgroup
-n c1 freezer.state
-l trace
-o "${UNPRIV_LOG}"
195 echo "Testing lxc-attach and lxc-cgroup from different cgroup"
196 cgm movepid freezer x2 $$
197 run_cmd lxc-attach
-n c1
-l trace
-o "${UNPRIV_LOG}" -- /bin
/true
198 run_cmd lxc-cgroup
-n c1
-l trace
-o "${UNPRIV_LOG}" freezer.state
199 run_cmd lxc-cgroup
-n c1
-l trace
-o "${UNPRIV_LOG}" memory.limit_in_bytes