summaryrefslogblamecommitdiffstats
path: root/scripts/mount-store
blob: 715a8a4a7f665932ac7706ba402fbd2fbf210bc4 (plain) (tree)
1
2
3
4
5
6
7
8







                                                                     


                                              








                                                                       
                               






                                                       


                                 
























































                                                                                                       













                                                                                         

                                                 
                          
                                                   


              
               
 




                                                          



                                           
                                    
                                          
                                                                                                                              
                      



                                                                      
                                           
                                                            
            
                                      
                         

                                 





































                                                                                                                          





                                          
       
 





                                                 

         
#!/bin/bash

if [ $# -lt 2 ]; then
	echo "Bad call to $0." >&2
	echo "Expected: $0 images <source> [username] [password]" >&2
	exit 1
fi

declare -rg CIFS_OPTS="/opt/openslx/cifs.opts"
declare -rg NFS_OPTS="/opt/openslx/nfs.opts"

WHAT="$1"
SOURCE="$2"
USERNAME="$3"
PASSWORD="$4"

# Currently WHAT can only be images (the central store for all images),
# but maybe there will be other storage types in the future.
case "$WHAT" in
images)
	DEST="/srv/openslx/nfs"
	;;
*)
	echo "Invalid/Unknown mount type: '$WHAT'." >&2
	exit 1
	;;
esac

FLAG="${DEST}/.notmounted"
SUBDIR="${DEST}/bwlehrpool_store"

storage_test () {
	rm -f -- "${FLAG}"
	if [ -e "${FLAG}" ]; then
		echo "Error: File '.notmounted' exists on remote storage and could not be deleted." >&2
		echo "Error: Make sure the share is writable." >&2
		return 5
	fi
	chgrp images "${DEST}" 2>/dev/null
	mkdir -p "${SUBDIR}"
	if [ ! -d "${SUBDIR}" ]; then
		echo "Error: Could not create $(basename "$SUBDIR")! Storage not writable!" >&2
		return 6
	fi
	echo "Applying group..."
	find "${SUBDIR}" -type d -exec chgrp images {} \; 2>/dev/null
	echo "Applying permissions..."
	find "${SUBDIR}" -type d -exec chmod ug+rwx {} \; 2>/dev/null
	echo "Creating test file..."
	local TEST="${SUBDIR}/.deleteme-$RANDOM-$RANDOM"
	sudo -n -u dmsd touch "$TEST"
	local RET=$?
	if [ -e "$TEST" ]; then
		sudo -n -u dmsd rm -f -- "$TEST"
	else
		[ "$RET" = "0" ] && RET=127
		echo "Error: Mounted share is not writable." >&2
		ls -al "${DEST}" "${SUBDIR}" >&2
	fi
	return $RET
}

# Already mounted?
TRIES=0
while awk '{print $2}' "/proc/mounts" | grep -q "^${DEST}\$"; do
	echo "Trying to unmount '$DEST'..."
	if [ "$TRIES" -gt 5 ]; then
		echo "Error: Cannot unmount '$DEST', giving up." >&2
		exit "$RET"
	elif [ "$TRIES" -gt 3 ]; then
		umount -v -f "$DEST"
	else
		umount -v "$DEST"
	fi
	RET=$?
	if [ "$RET" -ne "0" ]; then
		cat >&2 <<-HEREDOC
			Error: Cannot unmount '$DEST'!
			Storage might be in use (running VM upload etc.)
			Try stopping DMSD first

			If the problem persists, try rebooting the server.
		HEREDOC
		exit "$RET"
	fi
	TRIES=$(( $TRIES + 1 ))
done

# Sanity checks: Destination exists?
if [ ! -d "$DEST" ]; then
	mkdir -p "$DEST"
	chown root:images "$DEST"
	chmod 0775 "$DEST"
fi

# Still a no?
if [ ! -d "$DEST" ]; then
	echo "Mount point '$DEST' does not exist and could not be created!" >&2
	echo "This should not happen and means this server is severely messed up. :(" >&2
	exit 1
fi

# Unmount and not requested to mount (local mode)
if [[ "${SOURCE}" == "null" ]]; then
	rm -f -- "${FLAG}"
	echo "Success. Now using internal storage."
	exit 0
fi

touch "${FLAG}"

if [[ "${SOURCE}" == "unknown" ]]; then
	echo "Storage type not configured, doing nothing."
	exit 0
fi

# Mount!

if grep -E -q '^[^/].+:.+' <<<$SOURCE; then
	# seems to be NFS
	for opt in vers=4 vers=3; do
		echo " * Trying ${opt}..."
		mount -v -t nfs -o rw,noatime,noexec,nodev,async,nolock,$opt,fg,ac,retry=0,timeo=200,sec=sys "$SOURCE" "$DEST"
		RET=$?
		[ "$RET" -ne "0" ] && continue
		echo "Mount succeeded, checking write permissions...."
		storage_test
		RET=$?
		[ "$RET" -eq "0" ] && break
		umount -v "$DEST" || umount -v -f -l "$DEST"
	done
elif grep -E -q '^//' <<<$SOURCE; then
	# seems to be SMB
	export USER="$USERNAME"
	export PASSWD="$PASSWORD"
	RET=999
	OPTSTR=
	if [ -f "$CIFS_OPTS" ]; then
		OPTSTR=$(cat "$CIFS_OPTS")
	fi
	if [ -n "$OPTSTR" ]; then
		echo " * Trying last successful mount options..."
		if mount -v -t cifs -o "$OPTSTR" "$SOURCE" "$DEST"; then
			echo "Mount succeeded, checking write permissions...."
			storage_test
			RET=$?
			if [ "$RET" -ne "0" ]; then
				umount -v "$DEST" || umount -v -f -l "$DEST"
			fi
		fi
	fi
	if [ "$RET" -ne "0" ]; then
		for vers in "" "3.0" "2.1" "1.0" "2.0"; do
			[ -n "$vers" ] && vers=",vers=${vers}"
			for sec in "" "ntlmssp" "ntlmv2" "ntlm"; do
				[ -n "$sec" ] && sec=",sec=${sec}"
				echo " * Trying ...${vers}${sec}..."
				OPTSTR="rw,uid=0,gid=12345,forceuid,forcegid,nounix,file_mode=0664,dir_mode=0775$vers$sec"
				mount -v -t cifs -o "$OPTSTR" "$SOURCE" "$DEST"
				RET=$?
				[ "$RET" -ne "0" ] && continue
				echo "Mount succeeded, checking write permissions...."
				storage_test
				RET=$?
				[ "$RET" -eq "0" ] && break
				umount -v "$DEST" || umount -v -f -l "$DEST"
			done
			[ "$RET" -eq "0" ] && break
		done
		if [ "$RET" -eq "0" ] && [ -n "$OPTSTR" ]; then
			echo "$OPTSTR" > "$CIFS_OPTS"
		fi
	fi
	unset USER PASSWD
else
	echo "Unknown mount type: $SOURCE"
	exit 1
fi

echo ""

if [ "$RET" = "0" ]; then
	echo "----------------------------------"
	echo "-- Share mounted successfully!  --"
	echo "----------------------------------"
fi

exit $RET