lxc-test-unpriv revision ef4deb7f20dea12dd4225bbcc4ab471d0454289d
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#!/bin/bash
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen# lxc: linux Container library
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen# Authors:
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen# Serge Hallyn <serge.hallyn@ubuntu.com>
10399559650f552a23949772be79eb6a80198c5aTimo Sirainen#
39ea5717264668e2c7f9f7986eb821d21785f47fTimo Sirainen# This is a test script for unprivileged containers
eca38954bcf972618f6b85932a3690acbd2b673aTimo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen# This library is free software; you can redistribute it and/or
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen# modify it under the terms of the GNU Lesser General Public
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen# License as published by the Free Software Foundation; either
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen# version 2.1 of the License, or (at your option) any later version.
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen# This library is distributed in the hope that it will be useful,
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen# but WITHOUT ANY WARRANTY; without even the implied warranty of
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen# Lesser General Public License for more details.
8d25b6ad05b99e75613cb045a121efd51e6afbb6Timo Sirainen
8d25b6ad05b99e75613cb045a121efd51e6afbb6Timo Sirainen# You should have received a copy of the GNU Lesser General Public
8d25b6ad05b99e75613cb045a121efd51e6afbb6Timo Sirainen# License along with this library; if not, write to the Free Software
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
d3fce898d31ad40b554c91f3035a7f4d7d52ed52Timo Sirainen# This test assumes an Ubuntu host
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainenif [ $(id -u) -ne 0 ]; then
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen echo 'run as root'
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen exit 1
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenfi
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenwhich newuidmap >/dev/null 2>&1 || { echo "'newuidmap' command is missing" >&2; exit 1; }
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo SirainenDONE=0
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainencleanup() {
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainen cd
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen run_cmd lxc-stop -n c1 -k
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen pkill -u $(id -u $TUSER) -9
d694b6009574ee6e3cfaee3834cbdbcd431affb0Timo Sirainen
d694b6009574ee6e3cfaee3834cbdbcd431affb0Timo Sirainen sed -i '/lxcunpriv/d' /run/lxc/nics /etc/lxc/lxc-usernet
d694b6009574ee6e3cfaee3834cbdbcd431affb0Timo Sirainen sed -i '/^lxcunpriv:/d' /etc/subuid /etc/subgid
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen rm -Rf $HDIR /run/user/$(id -u $TUSER)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen deluser $TUSER
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen if [ $DONE -eq 0 ]; then
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen echo "FAIL"
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen exit 1
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen fi
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen echo "PASS"
d694b6009574ee6e3cfaee3834cbdbcd431affb0Timo Sirainen}
d694b6009574ee6e3cfaee3834cbdbcd431affb0Timo Sirainen
d694b6009574ee6e3cfaee3834cbdbcd431affb0Timo Sirainenrun_cmd() {
d694b6009574ee6e3cfaee3834cbdbcd431affb0Timo Sirainen sudo -i -u $TUSER \
d694b6009574ee6e3cfaee3834cbdbcd431affb0Timo Sirainen env http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen XDG_RUNTIME_DIR=/run/user/$(id -u $TUSER) $*
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen# create a test user
2be7df5df08ac4639ad83559ec5fcf552c84fb4aTimo SirainenTUSER=lxcunpriv
2be7df5df08ac4639ad83559ec5fcf552c84fb4aTimo SirainenHDIR=/home/$TUSER
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo SirainenARCH=i386
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainenif type dpkg >/dev/null 2>&1; then
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainen ARCH=$(dpkg --print-architecture)
9686ada8061745b4b8753572f6c04b7a27a8544eTimo Sirainenfi
fefd62f9aaccecbc6011ecae359a3389668cbff5Timo Sirainen
75bb83681e30d6a86109bbafdfe6b513c11124bcTimo Sirainentrap cleanup EXIT SIGHUP SIGINT SIGTERM
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainenset -eu
09b15d7b0619e8a1ad5a78b1f86b864a7f76b3c7Timo Sirainen
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainendeluser $TUSER && rm -Rf $HDIR || true
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenuseradd $TUSER
mkdir -p $HDIR
echo "$TUSER veth lxcbr0 2" > /etc/lxc/lxc-usernet
sed -i '/^lxcunpriv:/d' /etc/subuid /etc/subgid
usermod -v 910000-919999 -w 910000-919999 $TUSER
mkdir -p $HDIR/.config/lxc/
cat > $HDIR/.config/lxc/default.conf << EOF
lxc.network.type = veth
lxc.network.link = lxcbr0
lxc.id_map = u 0 910000 9999
lxc.id_map = g 0 910000 9999
EOF
chown -R $TUSER $HDIR
mkdir -p /run/user/$(id -u $TUSER)
chown -R $TUSER /run/user/$(id -u $TUSER)
cd $HDIR
if [ -e /sys/fs/cgroup/cgmanager/sock ]; then
for d in $(cut -d : -f 2 /proc/self/cgroup); do
dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
--type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.Create \
string:$d string:$TUSER >/dev/null
dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
--type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.Chown \
string:$d string:$TUSER int32:$(id -u $TUSER) int32:$(id -g $TUSER) >/dev/null
dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
--type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.MovePid \
string:$d string:$TUSER int32:$$ >/dev/null
done
else
for d in /sys/fs/cgroup/*; do
[ ! -d $d/lxctest ] && mkdir $d/lxctest
chown -R $TUSER $d/lxctest
echo $$ > $d/lxctest/tasks
done
fi
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
DONE=1