summaryrefslogtreecommitdiffstats
path: root/scripts/mount-store
blob: 260b0c74ab145b958c222235e0421d1db32d9c15 (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
#!/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

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

# 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
		echo "Storage might be in use (running VM upload etc.)" >&2
		echo "Try stopping DMSD first" >&2
		exit "$RET"
	fi
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
		mount -v -t nfs -o rw,noatime,noexec,nodev,async,nolock,$opt,fg,ac,retry=1,timeo=150,sec=sys "$SOURCE" "$DEST"
		RET=$?
		[ "$RET" -eq "0" ] && break
	done
elif grep -E -q '^//' <<<$SOURCE; then
	# seems to be SMB
	export USER="$USERNAME"
	export PASSWD="$PASSWORD"
	for sec in ntlmv2 ntlm; do
		echo " * Trying ${sec}..."
		mount -v -t cifs -o rw,uid=0,gid=12345,forceuid,forcegid,nounix,file_mode=0664,dir_mode=0775,sec=$sec "$SOURCE" "$DEST"
		RET=$?
		if [ "$RET" -eq "0" ]; then
			echo " * Success!"
			break
		fi
	done
	unset USER PASSWD
else
	echo "Unknown mount type: $SOURCE"
	exit 1
fi

echo "----------------------------------"

if [ "$RET" == "0" ]; then
	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
		umount -v "$DEST"
		exit 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
		umount -v "$DEST"
		exit 6
	fi
	chgrp -R images "${SUBDIR}" 2>/dev/null &
	chmod -R ug+rwX "${SUBDIR}" 2>/dev/null &
	TEST="${SUBDIR}/.deleteme-$RANDOM-$RANDOM"
	sudo -n -u dmsd touch "$TEST"
	RET=$?
	if [ -e "$TEST" ]; then
		sudo -n -u dmsd rm -f -- "$TEST"
	else
		[ "$RET" = "0" ] && RET=127
		echo "Error: Mounted share is not writable, aborting." >&2
		umount -v "$DEST"
	fi
fi

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

exit $RET