summaryrefslogtreecommitdiffstats
path: root/core/rootfs/rootfs-stage31/data/inc/functions
blob: c215350c07370602056a02576469f697436cefa3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/ash ##############################################################
#
#			COMMON HELPER FUNCTIONS
#

#########################################################################
#
# Function to drop a debug shell with an error message.
#
# Usage:
#	drop_shell "This is your error message."
#
drop_shell() {
	if [ -n "$MUTED_OUTPUT" ]; then
		exec 1>&4 2>&5
		reset
	fi
	[ $# -gt 0 ] && echo "$*"
	if [ "x$SPLASH" != "x0" ]; then
		echo "System halted. If you want to spawn a debug shell, select"
		echo "debug mode in the boot menu."
		echo ""
		echo "Press any key to reboot."
		read -t 1 -s -n 999999 BLA
		read -n 1 -s BLA
		echo b > /proc/sysrq-trigger
		exit 1
	fi
	echo "CTRL + D will continue booting."
	setsid sh -c 'exec sh </dev/tty1 >/dev/tty1 2>&1'
}

#########################################################################
#
# Helper function to download given FILE_URL from servers in 'slxsrv'
# as given through the kernel command line. File will be saved under TARGET_PATH
#
# Usage:
#       download FILE_URL TARGET_PATH
#
# Example:
#	download "config" "/opt/openslx/config"
#
# Note:
#	FILE_URL can have subpath, e.g. "ubuntu-13.04-x64/config"
#
download() {
	[ $# -ne 2 ] && echo "Error - 'download' requires 2 arguments, $# given." && return 1
	
	if [ -z "$SLX_KCL_SERVERS" ]; then
		if ! [ -s "/opt/openslx/config" ]; then
			echo "Don't have any servers to download from."
		elif ! . "/opt/openslx/config"; then
			echo "Error - could not source '/opt/openslx/config'"
		fi
	fi
	
	local FILE_URL="$1"
	local TARGET_PATH="$2"
	
	# Shuffle server list
	local SERVERS=$(for SERVER in $SLX_CONFIG_SERVERS $SLX_KCL_SERVERS; do echo "$RANDOM $SERVER"; done | sort -u | sed -r 's/^[0-9]+ //')
	
	for TIMEOUT in 1 1 2 END; do
		for SERVER in $SERVERS; do
			rm -f -- "${TARGET_PATH}"
			wget -T 5 -q -O "$TARGET_PATH" "http://${SERVER}/${FILE_URL}"
			RET=$?
			if [ "x$RET" != "x0" -o ! -e "$TARGET_PATH" ]; then
				echo "Error - downloading 'http://$SERVER/$FILE_URL' via wget failed. Exit Code: $RET"
				usleep 50000 # 50ms
			else
				echo "Successfully downloaded 'http://${SERVER}/$FILE_URL'."
				return 0
			fi
		done
		[ "$TIMEOUT" = "END" ] && break
		echo "Trying again in $(($TIMEOUT * 250)) ms..."
		usleep $(($TIMEOUT * 250000))
	done
	# Max retries reached, no success :-(
	return 1
}

# Add benchmark event to var, including uptime as prefix
bench_event() {
	bench_result="${bench_result}$(cut -f 1 -d ' ' "/proc/uptime") $@
"
}

# mini tarcopy <3
# tarcopy <source_dir> <target_dir>
tarcopy() {
	[ -d "$1" -a -d "$2" ] || return 1
	cd "$1"
	find . \! -type d > /tmp/tctmp
	tar -c -p -T /tmp/tctmp | tar -xp -C "$2"
	rm -f -- /tmp/tctmp
	cd - &>/dev/null
}

run_hooks () {
	[ -d "/inc/${1}.d" ] || return 0
	local i
	for i in "/inc/${1}.d/"*; do
		[ -f "$i" ] && . "$i"
	done
	return 0
}