lxc-test-unpriv revision 42e5c9878f0d20b3e9682ef441afed2f0228b298
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib#!/bin/bash
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib# lxc: linux Container library
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib# Authors:
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib# Serge Hallyn <serge.hallyn@ubuntu.com>
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib#
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib# This is a test script for unprivileged containers
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib# This library is free software; you can redistribute it and/or
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib# modify it under the terms of the GNU Lesser General Public
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib# License as published by the Free Software Foundation; either
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib# version 2.1 of the License, or (at your option) any later version.
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib# This library is distributed in the hope that it will be useful,
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib# but WITHOUT ANY WARRANTY; without even the implied warranty of
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib# Lesser General Public License for more details.
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib# You should have received a copy of the GNU Lesser General Public
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib# License along with this library; if not, write to the Free Software
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib# This test assumes an Ubuntu host
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibif [ $(id -u) -ne 0 ]; then
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib echo "ERROR: Must run as root."
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib exit 1
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibfi
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibwhich newuidmap >/dev/null 2>&1 || { echo "'newuidmap' command is missing" >&2; exit 1; }
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibDONE=0
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibcleanup() {
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib cd /
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib run_cmd lxc-stop -n c2 -k || true
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib run_cmd lxc-stop -n c1 -k || true
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib pkill -u $(id -u $TUSER) -9
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib sed -i '/lxcunpriv/d' /run/lxc/nics /etc/lxc/lxc-usernet
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib sed -i '/^lxcunpriv:/d' /etc/subuid /etc/subgid
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib rm -Rf $HDIR /run/user/$(id -u $TUSER)
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib deluser $TUSER
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib if [ $DONE -eq 0 ]; then
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib echo "FAIL"
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib exit 1
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib fi
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib echo "PASS"
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib}
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibrun_cmd() {
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib sudo -i -u $TUSER \
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib env http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib XDG_RUNTIME_DIR=/run/user/$(id -u $TUSER) $*
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib}
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib# create a test user
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibTUSER=lxcunpriv
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibHDIR=/home/$TUSER
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibARCH=i386
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibif type dpkg >/dev/null 2>&1; then
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib ARCH=$(dpkg --print-architecture)
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibfi
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibtrap cleanup EXIT SIGHUP SIGINT SIGTERM
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibset -eu
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibdeluser $TUSER && rm -Rf $HDIR || true
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibuseradd $TUSER
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibmkdir -p $HDIR
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibecho "$TUSER veth lxcbr0 2" > /etc/lxc/lxc-usernet
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibsed -i '/^lxcunpriv:/d' /etc/subuid /etc/subgid
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibusermod -v 910000-919999 -w 910000-919999 $TUSER
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibmkdir -p $HDIR/.config/lxc/
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibcat > $HDIR/.config/lxc/default.conf << EOF
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdiblxc.network.type = veth
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdiblxc.network.link = lxcbr0
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdiblxc.id_map = u 0 910000 9999
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdiblxc.id_map = g 0 910000 9999
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibEOF
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibchown -R $TUSER: $HDIR
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibmkdir -p /run/user/$(id -u $TUSER)
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibchown -R $TUSER: /run/user/$(id -u $TUSER)
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibcd $HDIR
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibif which cgm >/dev/null 2>&1; then
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib cgm create all $TUSER
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib cgm chown all $TUSER $(id -u $TUSER) $(id -g $TUSER)
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib cgm movepid all $TUSER $$
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibelif [ -e /sys/fs/cgroup/cgmanager/sock ]; then
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib for d in $(cut -d : -f 2 /proc/self/cgroup); do
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.Create \
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib string:$d string:$TUSER >/dev/null
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.Chown \
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib string:$d string:$TUSER int32:$(id -u $TUSER) int32:$(id -g $TUSER) >/dev/null
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.MovePid \
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib string:$d string:$TUSER int32:$$ >/dev/null
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib done
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibelse
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib for d in /sys/fs/cgroup/*; do
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib [ ! -d $d/lxctest ] && mkdir $d/lxctest
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib chown -R $TUSER: $d/lxctest
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib echo $$ > $d/lxctest/tasks
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib done
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibfi
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib# Copy the download template cache if available
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibrun_cmd mkdir -p $HDIR/.cache/lxc
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib[ -d /var/cache/lxc/download ] && \
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib cp -R /var/cache/lxc/download $HDIR/.cache/lxc && \
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib chown -R $TUSER: $HDIR/.cache/lxc
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibrun_cmd lxc-create -t download -n c1 -- -d ubuntu -r trusty -a $ARCH
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibrun_cmd lxc-start -n c1 -d
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibp1=$(run_cmd lxc-info -n c1 -p -H)
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib[ "$p1" != "-1" ] || { echo "Failed to start container c1"; false; }
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibrun_cmd lxc-info -n c1
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibrun_cmd lxc-attach -n c1 -- /bin/true
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibrun_cmd lxc-stop -n c1
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibrun_cmd lxc-clone -s -o c1 -n c2
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibrun_cmd lxc-start -n c2 -d
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibp1=$(run_cmd lxc-info -n c2 -p -H)
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib[ "$p1" != "-1" ] || { echo "Failed to start container c2"; false; }
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibrun_cmd lxc-stop -n c2
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdibDONE=1
3d2b040efe01bad393a1b2505e59d6d0a7b117d2theAdib