2010-08-28 20:28:42 +04:00
#!/bin/sh
# analyze free space, preferring tmpfs over really many gigaz
2012-01-06 21:09:14 +04:00
# and taking into account configured hasher workdir prefices
2010-08-28 20:28:42 +04:00
# hope there aren't spaces in RM's $HOME are they?
2012-01-02 19:45:09 +04:00
DIRS="$TMP $TMPDIR $HOME/hasher /tmp /var/tmp .."
2011-11-02 16:13:13 +04:00
MINSIZE=262144 # face control criterion
2010-08-28 20:28:42 +04:00
2012-01-06 21:09:14 +04:00
# poor man's SourceIfExists()
try_source() { [ -f "$1" ] && . "$1"; }
# hasher accepted ones
get_prefices()
{
try_source /etc/hasher-priv/system
try_source `/usr/libexec/hasher-priv/getconf.sh`
echo "$prefix" | tr ':' '\n' | while read i; do realpath "$i"; done
}
# drop candidates that hasher won't handle anyways
# NB: doesn't take --number into account,
# prefix lists are defined by the primary configuration
contemplate_dirs()
{
for d in "$@"; do
D="`realpath "$d"`";
for p in `get_prefices`; do
[ "${D#$p}" = "$D" ] || echo "$D";
done;
done \
| uniq # _not_ sort -u
}
2012-01-10 18:00:56 +04:00
# hasher emits no meaningful errors regarding those, sigh
check_options()
{
! egrep -q "^$1 $2 .*no(dev|exec)" /proc/mounts
}
2011-11-02 16:13:13 +04:00
# pick existing, writeable, >256M free space dirs
2010-08-28 20:28:42 +04:00
# rank them wrt type: tmpfs > realfs > rootfs
choose_tmpdir() {
2012-01-06 21:09:14 +04:00
for i in "$@"; do
2010-08-28 20:28:42 +04:00
[ -d "$i" -a -w "$i" ] || continue
echo -n "$i "
2012-07-16 20:55:30 +04:00
df -PT "$i" | tail -1
2010-08-28 20:28:42 +04:00
done \
| sort -unk6 \
| while read dir dev fstype size used free percent mnt; do
2012-01-10 18:00:56 +04:00
check_options "$dev" "$mnt" || continue
2010-08-28 20:28:42 +04:00
[ "$free" -gt "$MINSIZE" ] || continue
[ "$fstype" = "tmpfs" ] && { echo "2 $dir $free"; continue; }
[ "$mnt" = "/" ] && { echo "0 $dir $free"; continue; }
echo "1 $dir $free"
done \
| sort -n \
| tail -1 \
| cut -f2 -d' '
}
2012-01-06 21:09:14 +04:00
# bringing it all together
TMPDIRS="`contemplate_dirs $DIRS`"
if [ -z "$TMPDIRS" ]; then
2012-01-10 18:00:56 +04:00
echo "error: no suitable directories found;" >&2
2012-01-06 21:09:14 +04:00
echo "please check docs, filesystem and hasher setup" >&2
2012-01-10 18:00:56 +04:00
echo "(mount enough tmpfs into /tmp or fix hasher-priv prefix?)" >&2
exit 1
fi
TEMP="`choose_tmpdir $TMPDIRS`"
if [ -z "$TEMP" ]; then
echo "error: no suitable directories found;" >&2
echo "please check hasher docs and filesystem setup" >&2
echo "(nodev and/or noexec on an otherwise suitable filesystem?)" >&2
2012-01-06 21:09:14 +04:00
exit 1
fi
2012-01-10 18:00:56 +04:00
DIR="$TEMP/`dirname "$1"`"
2012-01-02 19:45:09 +04:00
NAME="`basename "${1:-tmpdir}"`"
mkdir -p "$DIR" # in case $1 contains slash(es)
mktemp -d "$NAME.XXXXXXX" --tmpdir="${DIR%/.}"