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
2016-01-10 12:48:06 +03:00
# mkimage needs /proc among those, be clear about that
check_allowed_mountpoints()
{
grep -wqs "^allowed_mountpoints=[^#]*/proc" \
/etc/hasher-priv/system \
`/usr/libexec/hasher-priv/getconf.sh`
}
2012-01-06 21:09:14 +04:00
# poor man's SourceIfExists()
try_source() { [ -f "$1" ] && . "$1"; }
# hasher accepted ones
get_prefices()
{
2012-07-17 16:53:42 +04:00
try_source /etc/hasher-priv/system || exit 1
2012-01-06 21:09:14 +04:00
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 \
2015-09-07 20:57:58 +03:00
| sort -unk5 \
2010-08-28 20:28:42 +04:00
| 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
2016-01-10 12:48:06 +03:00
if ! check_allowed_mountpoints; then
echo "error: hasher's allowed_mountpoints do not include /proc;"
echo "please check hasher docs and /etc/hasher-priv/system"
exit 1
fi >&2
2012-01-06 21:09:14 +04:00
TMPDIRS="`contemplate_dirs $DIRS`"
if [ -z "$TMPDIRS" ]; then
2012-07-17 16:53:42 +04:00
echo "error: no suitable directories found;"
echo "please check QUICKSTART, filesystem and hasher setup"
echo "(mount enough tmpfs into /tmp or fix hasher-priv prefix?)"
2012-01-10 18:00:56 +04:00
exit 1
2012-07-17 16:53:42 +04:00
fi >&2
2012-01-10 18:00:56 +04:00
TEMP="`choose_tmpdir $TMPDIRS`"
if [ -z "$TEMP" ]; then
2012-07-17 16:53:42 +04:00
echo "error: no suitable directories found;"
echo "please check hasher docs and filesystem setup"
echo "(nodev and/or noexec on an otherwise suitable filesystem?)"
2012-01-06 21:09:14 +04:00
exit 1
2012-07-17 16:53:42 +04:00
fi >&2
2012-01-06 21:09:14 +04:00
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%/.}"