summaryrefslogtreecommitdiffstats
path: root/core/modules/dnbd3-proxy-mode/data/opt/openslx/scripts/systemd-setup_dnbd3_proxy
blob: f5915ee78d49533e386e7ebe24ea10b089f973b8 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/ash
# This scripts runs as root and prepares the configuration
# for the dnbd3 server (...)

ERRLOG="/run/dnbd3-proxy.err"

rm -- "$ERRLOG"

errormsg () {
	echo "$@" >> "$ERRLOG"
	slxlog -s -e "dnbd3-setup" "$@"
}

# Get size of disk/device at given path, in kb
disksize () {
	df -k "$1" | tail -n 1 | awk '{print $2}'
}

# Creates the DNBD3 server configuration under DNBD3_CONF_DIR
DNBD3_CONF_DIR="/etc/dnbd3-server"
if ! mkdir -p "${DNBD3_CONF_DIR}"; then
	errormsg "Failed to create '${DNBD3_CONF_DIR}'."
fi

# Checks if the persistent partition (MBR-ID 45 / GPT-LABEL OpenSLX-ID45)
# is present, fallback to /tmp
DNBD3_BASE_DIR="/opt/openslx/persistent"
if grep -q "^/dev/.* ${DNBD3_BASE_DIR} .*rw" /proc/mounts \
		&& [ -k "${DNBD3_BASE_DIR}/data" ] \
		&& [ "$(disksize "${DNBD3_BASE_DIR}/data")" -gt 40000000 ]; then
	# setup_partitions creates a data folder in ID45
	DNBD3_BASE_DIR="${DNBD3_BASE_DIR}/data"
else
	# try /tmp fallback
	DNBD3_BASE_DIR="/tmp"
	if ! grep -q '^/dev/.* '"${DNBD3_BASE_DIR}"' .*rw' /proc/mounts \
		|| [ "$(disksize "${DNBD3_BASE_DIR}")" -lt 40000000 ]; then
		# no sane fallback possible
		errormsg "Neither a persistent part (ID45) nor /tmp (ID44) are available, or they are < 40GB"
	fi
fi
# now try to create the actual folder used by the server
DNBD3_DATA_DIR="${DNBD3_BASE_DIR}/dnbd3"
if ! mkdir -p "${DNBD3_DATA_DIR}"; then
	for i in 0 1 2 3 4 5 $RANDOM $RANDOM; do
		if mkdir -p "${DNBD3_BASE_DIR}/dnbd3.$i"; then
			DNBD3_DATA_DIR="${DNBD3_BASE_DIR}/dnbd3.$i"
			break
		fi
	done
fi

if [ ! -d "${DNBD3_DATA_DIR}" ]; then
	errormsg "Failed to create '${DNBD3_BASE_DIR}(.[0-$i])'"
fi
if ! chown -R dnbd3:dnbd3 "${DNBD3_DATA_DIR}"; then
	errormsg "Failed to chown '${DNBD3_DATA_DIR}' to dnbd3."
fi
if ! chmod -R go-w,u+rwX "${DNBD3_DATA_DIR}"; then
	errormsg "Failed to chmod '${DNBD3_DATA_DIR}' to dnbd3."
fi

# Done with sanity checks, now create configs: server.conf & alt-servers
# Using the information given by the server in /opt/openslx/config
. /opt/openslx/config

DNBD3_BGR="false"
[ -n "${SLX_DNBD3_BGR}" ] && DNBD3_BGR="true"
DNBD3_PORT=5003
rm -f "${DNBD3_CONF_DIR}/server.conf"
# Refer to http://git.openslx.org/dnbd3.git/tree/conf for configuration options
cat << EOF > "${DNBD3_CONF_DIR}/server.conf"
[dnbd3]
listenPort=${DNBD3_PORT}
basePath=${DNBD3_DATA_DIR}
serverPenalty=100000
clientPenalty=0
isProxy=true
backgroundReplication=${DNBD3_BGR}
removeMissingImages=false
uplinkTimeout=5000
clientTimeout=15000

[logging]
consoleMask=ERROR WARNING MINOR INFO
EOF

MY_IPS=$(ip a | grep '^\s*inet\s' | awk '{print $2}')

# helper to echo given list of IPs to ${DNBD3_CONF_DIR}/alt-servers
# optionally takes a single char prefix as first param and
# adds it to the IP (for private dnbd3 servers)
# Also sets FOUND_SAT if satellite IP was seen
add_alt_server() {
	local PRE=
	[ "x$1" = "x-" ] && PRE='-' && shift
	for ALT in "$@"; do
		for ip in $MY_IPS; do
			[ "x$ALT" = "x${ip%/*}" ] && return 0 # Ignore self
		done
		echo "${PRE}${ALT}" >> "${DNBD3_CONF_DIR}/alt-servers"
		[ "x${ALT}" = "x${SLX_PXE_SERVER_IP}" ] && FOUND_SAT="oui"
	done
	return 0
}

rm -f "${DNBD3_CONF_DIR}/alt-servers"
FOUND_SAT=
add_alt_server ${SLX_DNBD3_PUBLIC}
add_alt_server '-' ${SLX_DNBD3_PRIVATE}
# To this day, only the sat IP is in SLX_KCL_SERVERS afaik
[ -z "${FOUND_SAT}" ] && add_alt_server ${SLX_KCL_SERVERS}
chmod -R a+Xr "${DNBD3_CONF_DIR}"

# create rpc.acl to allow the satellite only
rm -f "${DNBD3_CONF_DIR}/rpc.acl"
for SRV in ${SLX_KCL_SERVERS}; do
	echo "$SRV STATS CLIENT_LIST IMAGE_LIST" >> "${DNBD3_CONF_DIR}/rpc.acl"
done

rm -f "/opt/openslx/iptables/rules.d/99-dnbd3"
# now create iptables helper rules
DNBD3_IPTABLES_CONF="/opt/openslx/iptables/rules.d/99-dnbd3"
echo '#!/bin/ash' >> "${DNBD3_IPTABLES_CONF}"
for CIDR in ${SLX_DNBD3_WHITELIST}; do
	echo "iptables -I ipt-helper-INPUT 1 -i br0 -p tcp -s ${CIDR} --dport ${DNBD3_PORT} -j ACCEPT"
done >> "${DNBD3_IPTABLES_CONF}"
[ -n "${SLX_DNBD3_WHITELIST}" ] && \
	echo "iptables -A ipt-helper-INPUT -i br0 -p tcp --dport ${DNBD3_PORT} -j REJECT" >> "${DNBD3_IPTABLES_CONF}"
chmod +x "${DNBD3_IPTABLES_CONF}"

(
	echo "*******************"
	echo "*** DNBD3 Proxy ***"
	echo "*******************"
) | tee -a "/etc/issue" >> "/opt/openslx/etc/issue.template"

M=$(( RANDOM % 60 ))
H=$(( RANDOM % 5 ))
if [ -s "$ERRLOG" ]; then
	W="*"
else
	W="0"
fi
cat > "/etc/cron.d/dnbd3-reboot" <<EOF
# OpenSLX: Reboot proxy at night
SHELL=/bin/ash
PATH=/usr/sbin:/usr/bin:/sbin:/bin:/opt/openslx/sbin:/opt/openslx/bin

$M $H	* * $W	root	reboot
EOF

exit 0