lxc-test-unpriv revision 42e5c9878f0d20b3e9682ef441afed2f0228b298
0N/A#!/bin/bash
0N/A
1479N/A# lxc: linux Container library
0N/A
0N/A# Authors:
919N/A# Serge Hallyn <serge.hallyn@ubuntu.com>
919N/A#
919N/A# This is a test script for unprivileged containers
919N/A
919N/A# This library is free software; you can redistribute it and/or
919N/A# modify it under the terms of the GNU Lesser General Public
919N/A# License as published by the Free Software Foundation; either
919N/A# version 2.1 of the License, or (at your option) any later version.
919N/A
919N/A# This library is distributed in the hope that it will be useful,
919N/A# but WITHOUT ANY WARRANTY; without even the implied warranty of
919N/A# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
919N/A# Lesser General Public License for more details.
919N/A
919N/A# You should have received a copy of the GNU Lesser General Public
919N/A# License along with this library; if not, write to the Free Software
919N/A# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0N/A
0N/A# This test assumes an Ubuntu host
0N/A
0N/Aif [ $(id -u) -ne 0 ]; then
493N/A echo "ERROR: Must run as root."
810N/A exit 1
810N/Afi
810N/Awhich newuidmap >/dev/null 2>&1 || { echo "'newuidmap' command is missing" >&2; exit 1; }
810N/A
493N/ADONE=0
493N/Acleanup() {
0N/A cd /
493N/A
822N/A run_cmd lxc-stop -n c2 -k || true
822N/A run_cmd lxc-stop -n c1 -k || true
493N/A pkill -u $(id -u $TUSER) -9
493N/A
493N/A sed -i '/lxcunpriv/d' /run/lxc/nics /etc/lxc/lxc-usernet
493N/A sed -i '/^lxcunpriv:/d' /etc/subuid /etc/subgid
493N/A
810N/A rm -Rf $HDIR /run/user/$(id -u $TUSER)
810N/A
810N/A deluser $TUSER
1193N/A
970N/A if [ $DONE -eq 0 ]; then
1068N/A echo "FAIL"
1068N/A exit 1
1068N/A fi
1068N/A echo "PASS"
822N/A}
822N/A
1056N/Arun_cmd() {
810N/A sudo -i -u $TUSER \
851N/A env http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
1068N/A XDG_RUNTIME_DIR=/run/user/$(id -u $TUSER) $*
851N/A}
493N/A
493N/A# create a test user
174N/ATUSER=lxcunpriv
1193N/AHDIR=/home/$TUSER
970N/A
967N/AARCH=i386
8N/Aif type dpkg >/dev/null 2>&1; then
61N/A ARCH=$(dpkg --print-architecture)
1068N/Afi
493N/A
1010N/Atrap cleanup EXIT SIGHUP SIGINT SIGTERM
1010N/Aset -eu
186N/A
70N/Adeluser $TUSER && rm -Rf $HDIR || true
851N/Auseradd $TUSER
822N/A
822N/Amkdir -p $HDIR
970N/Aecho "$TUSER veth lxcbr0 2" > /etc/lxc/lxc-usernet
98N/Ased -i '/^lxcunpriv:/d' /etc/subuid /etc/subgid
430N/A
214N/Ausermod -v 910000-919999 -w 910000-919999 $TUSER
98N/A
0N/Amkdir -p $HDIR/.config/lxc/
493N/Acat > $HDIR/.config/lxc/default.conf << EOF
493N/Alxc.network.type = veth
0N/Alxc.network.link = lxcbr0
1056N/Alxc.id_map = u 0 910000 9999
1056N/Alxc.id_map = g 0 910000 9999
1056N/AEOF
1056N/Achown -R $TUSER: $HDIR
493N/A
967N/Amkdir -p /run/user/$(id -u $TUSER)
0N/Achown -R $TUSER: /run/user/$(id -u $TUSER)
493N/A
493N/Acd $HDIR
967N/A
0N/Aif which cgm >/dev/null 2>&1; then
1196N/A cgm create all $TUSER
1196N/A cgm chown all $TUSER $(id -u $TUSER) $(id -g $TUSER)
1196N/A cgm movepid all $TUSER $$
1196N/Aelif [ -e /sys/fs/cgroup/cgmanager/sock ]; then
1196N/A for d in $(cut -d : -f 2 /proc/self/cgroup); do
1196N/A dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
1196N/A --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.Create \
1196N/A string:$d string:$TUSER >/dev/null
1196N/A
1479N/A dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
1479N/A --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.Chown \
1479N/A string:$d string:$TUSER int32:$(id -u $TUSER) int32:$(id -g $TUSER) >/dev/null
1479N/A
1479N/A dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
1479N/A --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.MovePid \
1479N/A string:$d string:$TUSER int32:$$ >/dev/null
1479N/A done
1479N/Aelse
1479N/A for d in /sys/fs/cgroup/*; do
1479N/A [ ! -d $d/lxctest ] && mkdir $d/lxctest
1479N/A chown -R $TUSER: $d/lxctest
1479N/A echo $$ > $d/lxctest/tasks
1479N/A done
1479N/Afi
1479N/A
1479N/A# Copy the download template cache if available
1479N/Arun_cmd mkdir -p $HDIR/.cache/lxc
1479N/A[ -d /var/cache/lxc/download ] && \
cp -R /var/cache/lxc/download $HDIR/.cache/lxc && \
chown -R $TUSER: $HDIR/.cache/lxc
run_cmd lxc-create -t download -n c1 -- -d ubuntu -r trusty -a $ARCH
run_cmd lxc-start -n c1 -d
p1=$(run_cmd lxc-info -n c1 -p -H)
[ "$p1" != "-1" ] || { echo "Failed to start container c1"; false; }
run_cmd lxc-info -n c1
run_cmd lxc-attach -n c1 -- /bin/true
run_cmd lxc-stop -n c1
run_cmd lxc-clone -s -o c1 -n c2
run_cmd lxc-start -n c2 -d
p1=$(run_cmd lxc-info -n c2 -p -H)
[ "$p1" != "-1" ] || { echo "Failed to start container c2"; false; }
run_cmd lxc-stop -n c2
DONE=1