mktmpdir 2.64 KB
Newer Older
1 2
#!/bin/sh
# analyze free space, preferring tmpfs over really many gigaz
3
# and taking into account configured hasher workdir prefices
4

5 6
# NB: use BUILDDIR to override autodetection (see ../QUICKSTART)

7
# hope there aren't spaces in RM's $HOME are they?
Michael Shigorin's avatar
Michael Shigorin committed
8
DIRS="$TMP $TMPDIR $HOME/hasher /tmp /var/tmp .."
9
MINSIZE=262144		# face control criterion
10

11 12 13 14 15 16 17 18
# 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`
}

19 20 21 22 23 24
# poor man's SourceIfExists()
try_source() { [ -f "$1" ] && . "$1"; }

# hasher accepted ones
get_prefices()
{
25
	try_source /etc/hasher-priv/system || exit 1
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
	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
}

44 45 46 47 48 49
# hasher emits no meaningful errors regarding those, sigh
check_options()
{
	! egrep -q "^$1 $2 .*no(dev|exec)" /proc/mounts
}

50
# pick existing, writeable, >256M free space dirs
51 52
# rank them wrt type: tmpfs > realfs > rootfs
choose_tmpdir() {
53
	for i in "$@"; do
54 55
		[ -d "$i" -a -w "$i" ] || continue
		echo -n "$i "
56
		df -PT "$i" | tail -1
57
	done \
58
	| sort -unk5 \
59
	| while read dir dev fstype size used free percent mnt; do
60
		check_options "$dev" "$mnt" || continue
61 62 63 64 65 66 67 68 69 70
		[ "$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' '
}

71
# bringing it all together
72 73 74 75 76 77
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

78 79
TMPDIRS="`contemplate_dirs $DIRS`"
if [ -z "$TMPDIRS" ]; then
80 81 82
	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?)"
83
	exit 1
84
fi >&2
85 86 87

TEMP="`choose_tmpdir $TMPDIRS`"
if [ -z "$TEMP" ]; then
88 89 90
	echo "error: no suitable directories found;"
	echo "please check hasher docs and filesystem setup"
	echo "(nodev and/or noexec on an otherwise suitable filesystem?)"
91
	exit 1
92
fi >&2
93

94
DIR="$TEMP/`dirname "$1"`"
Michael Shigorin's avatar
Michael Shigorin committed
95 96 97
NAME="`basename "${1:-tmpdir}"`"
mkdir -p "$DIR"	# in case $1 contains slash(es)
mktemp -d "$NAME.XXXXXXX" --tmpdir="${DIR%/.}"