summaryrefslogblamecommitdiffstats
path: root/core/modules/speedcheck/data/opt/openslx/bin/image_speedcheck
blob: 7cfe9c80adb5c1b2f2ffab2bb977d95be1dd99b9 (plain) (tree)
1
2
3
4
5
6
7
8
9

           






                                                                   
 

           
       


      
             
 





                                  


                                        


















                                    

                                        
                                                                                                               
                                               
                                                                                           







                                                                                                             
            
                                                                                                                   

                      
             
    








                                                                           
                              
                                                                                                                          


          





                                                                                        




                                             
                      

                                      




















                                                                                                                          










                                                                                      









                                                                   
                          


                                                   
#!/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