#!/bin/bash
# For kernels linux-generic-lts-vivid and 3.18+,
# this script fails with error message:
# "rm: cannot remove ‘mntpt/foo’: Operation not permitted"
# strace output:
# "unlinkat(AT_FDCWD, "mntpt/foodir", AT_REMOVEDIR) = -1 EPERM (Operation
# not permitted)"
# Preliminary checks
[ "$UID" -eq "0" ] && \
echo "script should be run as a normal user, not as root" && exit 1
# lxc-usernsexec provides an easy way to enter a user-namespace.
# host uid 1000 is mapped to root in the user-namespace.
[ ! -f "/usr/bin/lxc-usernsexec" ] && \
echo "lxc-usernsexec not found. It is part of the lxc package" && exit 1
script='
testdir="$HOME/testoverlayfs"
mkdir -m 0775 -p "$testdir"
mount -t tmpfs tmpfs -o mode=0775 "$testdir" || exit 1
cd "$testdir" || exit 1
mkdir -m 0775 -p lowerdir/foo upperdir workdir mntpt
touch lowerdir/foo/bar.txt
mount -n -t overlay overlay \
-o lowerdir=lowerdir,upperdir=upperdir,workdir=workdir mntpt
echo -e "\nmounted overlayfs"
echo -e "\nls -RF mntpt \n -------------"
ls -RF mntpt
echo -e "\nrm -r mntpt/* \n -------------"
rm -r mntpt/*
echo -e "exit code=$?"
echo -e "\nls -RF mntpt \n -------------"
ls -RF mntpt
umount mntpt
echo -e "\nunmounted overlayfs"
echo "cleaning up"
cd "$testdir"/..
umount "$testdir"
rmdir "$testdir"
'
lxc-usernsexec -m b:0:1000:1 -- bash -c "$script"