summaryrefslogblamecommitdiffstats
path: root/scripts/mount-store
blob: f138c5f735a173f30e24b54dfddfb8c759fada7a (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
















                                                                       
                               











































                                                                                         




                                                                                                        



                                      




                                                                                                                             






                                          




                                              
                                      
                                     

                               
                                                



                                                                          



         
#!/bin/bash

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

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

# 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

# Already mounted?
if awk '{print $2}' "/proc/mounts" | grep -q "^${DEST}\$"; then
	echo "Trying to unmount '$DEST'..."
	umount -v "$DEST"
	RET=$?
	if [ "$RET" -ne "0" ]; then
		echo "Cannot unmount '$DEST'!" >&2
		exit "$RET"
	fi
fi

# Unmount and not requested to mount (local mode)
if [[ "${SOURCE}" == "null" ]]; then
	rm -f "${DEST}/.notmounted"
	exit 0
fi

touch "${DEST}/.notmounted"

# Mount!

if grep -E -q '^[^/].+:.+' <<<$SOURCE; then
	# seems to be NFS
	for i in 1 2 3; do
		mount -t nfs -o rw,async,nolock,vers=3,fg,ac,retry=1,timeo=100,sec=sys "$SOURCE" "$DEST"
		RET=$?
		[ "$RET" -eq "0" ] && break
	done
elif grep -E -q '^//' <<<$SOURCE; then
	# seens to be SMB
	export USER="$USERNAME"
	export PASSWD="$PASSWORD"
	for sec in ntlmv2 ntlm; do
		mount -t cifs -o rw,uid=0,gid=12345,forceuid,forcegid,file_mode=0664,dir_mode=0775,sec=$sec "$SOURCE" "$DEST"
		RET=$?
		[ "$RET" -eq "0" ] && break
	done
	unset USER PASSWD
else
	echo "Unknown mount type: $SOURCE"
	exit 1
fi

if [ "$RET" == "0" ]; then
	chgrp -R images "$DEST" 2>/dev/null
	chmod -R ug+rw "$DEST" 2>/dev/null
	for DIR in $(find "$DEST" -type d); do
		chmod ug+x "$DIR" 2>/dev/null
	done
	TEST="$DEST/.test-$RAND-$RAND"
	sudo -n -u dmsd touch "$TEST"
	RET=$?
	if [ -e "$TEST" ]; then
		sudo -n -u dmsd rm -f -- "$TEST"
	else
		echo "Error: Mounted share is not writable, aborting." >&2
		umount -v "$DEST"
	fi
fi

exit $RET