summaryrefslogtreecommitdiffstats
path: root/core/modules/speedcheck/data/opt/openslx/bin/image_speedcheck
blob: 7cfe9c80adb5c1b2f2ffab2bb977d95be1dd99b9 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash

showmsg() {
	if [ -n "$DISPLAY" ]; then
		zenity --error --text "$*" &> /dev/null && return 0
		xmessage "$*" &> /dev/null && return 0
	fi
	echo "$*"
}

mountpoint=
servers=
sticky=
start=
file=
nfs=
mode="--auto"

while (( $# > 0 )); do
	case "$1" in
		--start)
			start="$2"
			shift
			;;
		--console)
			mode="--console"
			;;
		--nfs)
			nfs=1
			;;
		--file)
			file="$2"
			shift
			;;
		--servers)
			servers="$2"
			shift
			;;
		*)
			break
			;;
	esac
	shift
done

if [ -z "$file" ]; then
	. /opt/openslx/config
	if mountpoint /mnt/vmstore; then
		file=$( find /mnt/vmstore -type f -printf "%s\t%p\n" | sort -n | tail -n 1 | awk '{print $2}' )
		[ -z "$SLX_VM_DNBD3" ] && nfs=1
	elif [ -s "/tmp/vmchooser-benchmark" ] || [ -s "/tmp/vmchooser2/vmchooser2.xml" ] \
			|| curl -m 5 -L "$SLX_VMCHOOSER_BASE_URL/list" > "/tmp/vmchooser-benchmark"; then
		xml="/tmp/vmchooser-benchmark"
		[ -s "$xml" ] || xml="/tmp/vmchooser2/vmchooser2.xml"
		file="$( xmlstarlet sel -T -t -v "//settings/eintrag/image_name/@param" "$xml" | head -n 1 )"
		if [ -n "$nfs" ]; then
			showmsg "--nfs given, but NFS not enabled/mounted."
			exit 1
		fi
	else
		showmsg "Could not automatically determine any image name, specify one via --file as relative path"
		exit 1
	fi
	rid=0
else
	# Extract rid from name:rid format (if any), then remove from $file
	rid="${file##*:}"
	if [ "$rid" = "$file" ] || ! [ "$rid" -gt 0 ]; then
		# No rid in filename
		rid=0
	else
		# Found rid, remove from $file
		file="${file%:*}"
	fi
	if [ -n "$nfs" ]; then
		file="$( find /mnt/vmstore/"$file"* -type f -printf "%s\t%p\n" | sort -n | tail -n 1 | awk '{print $2}' )"
	fi
fi

# Common intermediate format; relative to mount point (if any), get rid of any ./ prefix
file="${file#/mnt/vmstore/}"
file="${file#/}"
file="${file#.}"
file="${file#/}"

if [ -z "$file" ]; then
	showmsg "Cannot test, no image found"
	exit 1
fi

if [ -n "$nfs" ]; then
	# For NFS, make absolute again
	file="/mnt/vmstore/${file}"
	if ! [ -s "$file" ]; then
		showmsg "Cannot access '$file'"
		exit 1
	fi
else
	if [ -z "$servers" ]; then
		. /opt/openslx/config
		servers="$SLX_DNBD3_SERVERS"
		if [ "$servers" = "127.0.0.1" ]; then
			# don't use local caching for this test
			servers="$( awk '$1 != "127.0.0.1:5003" {printf("%s ", $1)}' "/sys/block/dnbd0/net/alt_servers" )"
			if [ -z "$servers" ]; then
				# poor man's way to convert \n to ' '
				servers="$( echo $( cut -c2- "/etc/dnbd3-server/alt-servers" ) )"
			fi
		fi
	else
		sticky="-S"
	fi
	mountpoint="/tmp/speedtest-$$-$RANDOM"
	mkdir "$mountpoint"
	if ! dnbd3-fuse $sticky -h "$servers" -i "$file" -r "$rid" "$mountpoint"; then
		showmsg "Cannot start dnbd3-fuse from $servers"
		exit 1
	fi
	if ! mountpoint "$mountpoint"; then
		showmsg "is not mountpoint"
		exit 1
	fi
	file="${mountpoint}/img"
fi

if [ -n "$start" ]; then
	now="$( date +%s )"
	diff="$(( start - now ))"
	(( diff > 120 )) && diff=0
	if (( diff > 0 )); then
		echo "Waiting $diff seconds for synchronized start"
		sleep "$diff"
	fi
fi

speedcheck "$mode" "$file"

[ -n "$mountpoint" ] && fusermount -u "$mountpoint"
exit 0