Skip to content

Instantly share code, notes, and snippets.

@wentam
Last active February 12, 2023 15:21
Show Gist options
  • Save wentam/3a175c71a2f535e1606bb40e0f1aef58 to your computer and use it in GitHub Desktop.
Save wentam/3a175c71a2f535e1606bb40e0f1aef58 to your computer and use it in GitHub Desktop.
#!/bin/sh
# See the validate_env function for dependencies
if [ $# -lt 2 ]; then
echo "Usage: [testing directory] [mirror user]";
echo "Make sure the testing directory can be access by both mirror user and this script's user"
exit 1;
fi
export test_dir="$(realpath $1)"
export mirror_user="$2"
mkdir -p $test_dir
# Make sure everything goes away when done
trap "umount -l ${test_dir}/b; trap - SIGTERM; kill -- -$$" SIGINT SIGTERM EXIT
check_cmd() {
cmd="$1"
command -v $cmd > /dev/null;
if [ $? -eq 1 ]; then echo "==> You don't have $cmd, exiting."; exit 1; fi
}
validate_env() {
set +e
check_cmd bindfs
check_cmd sudo
check_cmd strace
check_cmd find
check_cmd xargs
check_cmd umount
check_cmd bash
ls "$test_dir" > /dev/null
if [ $? -ne 0 ]; then
echo "==> Unable to access test dir as script user ($(whoami)), exiting";
exit 1;
fi
if [ "$(id -u)" -eq "$(id -u ${mirror_user})" ]; then
echo "==> Mirror user must be different than the user running this script, exiting";
exit 1;
fi
echo "==> Changing to user '$mirror_user' via sudo, may prompt sudo password:"
sudo -u "$mirror_user" echo > /dev/null
if [ $? -ne 0 ]; then echo "==> Unable to switch to mirror user with sudo, exiting"; exit 1; fi
sudo -u "$mirror_user" ls "$test_dir" > /dev/null
if [ $? -ne 0 ]; then
echo "==> Unable to access test dir as mirror user (${mirror_user}), exiting";
exit 1;
fi
}
setup() {
set -e
mkdir -p "$test_dir"
cd "$test_dir"
echo -n "Setting up..."
mkdir -p a b
bindfs --mirror=$mirror_user a/ b/
sleep 1 # Wait for bindfs to start
mkdir -p b/sub/
chmod 777 "$test_dir"
filecount="$(find b/sub/ -type f | wc -l)"
if [ $filecount -lt 20000 ]; then
for f in {1..20000}; do touch b/sub/$f; done
fi
echo "done"
}
statter() {
set -e
while true; do
find $1 -print0 | xargs -0 -n 100 -P 100 stat 2>/dev/null >/dev/null;
done > /dev/null
}
try_for_error() {
echo "Trying to produce error"
set +e
while true; do
strace -o /tmp/bindfs-repro-strace1 rm -f "${test_dir}/b/foo";
if [ $? -ne 0 ]; then
echo -e "\n==> Successfully reproduced: Error occured in rm, see /tmp/bindfs-repro-strace1";
break;
fi
strace -o /tmp/bindfs-repro-strace2 touch "${test_dir}/b/foo";
if [ $? -ne 0 ]; then
echo -e "\n==> Successfully reproduced: Error occured in touch, see /tmp/bindfs-repro-strace2";
break;
fi
done
}
validate_env
setup
cd "$test_dir"
sudo -u "$mirror_user" bash -c "$(declare -f statter); statter ${test_dir}/b &"
echo "C-c to stop, will automatically stop on error"
try_for_error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment