1#!/bin/bash
2#
3# Copyright (c) 2016 SUSE Linux GmbH
4#
5# This library is free software; you can redistribute it and/or
6# modify it under the terms of version 2.1 of the GNU Lesser General Public
7# License as published by the Free Software Foundation.
8#
9# This library is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12# Lesser General Public License for more details.
13#
14# You should have received a copy of the GNU Lesser General Public
15# License along with this library; If not, see <http://www.gnu.org/licenses/>.
16#
17
18XENSTORED=@XENSTORED@
19
20. @XEN_SCRIPT_DIR@/hotplugpath.sh
21
22test_xenstore () {
23	test -f @XEN_RUN_DIR@/xenstored.pid
24	return $?
25}
26
27timeout_xenstore () {
28	local time=0
29	local timeout=30
30
31	while [ $time -lt $timeout ] && ! test_xenstore ; do
32		echo -n .
33		time=$(($time+1))
34		sleep 1
35	done
36	echo
37
38	# Exit if we timed out
39	if ! [ $time -lt $timeout ] ; then
40		echo "Could not start $@"
41		return 1
42	fi
43
44	return 0
45}
46
47test_xenstore && exit 0
48
49test -f @CONFIG_DIR@/@CONFIG_LEAF_DIR@/xencommons && . @CONFIG_DIR@/@CONFIG_LEAF_DIR@/xencommons
50
51[ "$XENSTORETYPE" = "" ] && XENSTORETYPE=daemon
52
53/bin/mkdir -p @XEN_RUN_DIR@
54
55[ "$XENSTORETYPE" = "daemon" ] && {
56	[ -z "$XENSTORED_TRACE" ] || XENSTORED_ARGS="$XENSTORED_ARGS -T @XEN_LOG_DIR@/xenstored-trace.log"
57	[ -z "$XENSTORED_MAX_OPEN_FDS" ] && XENSTORED_MAX_OPEN_FDS=unlimited
58	[ -z "$XENSTORED" ] && XENSTORED=@XENSTORED@
59	[ -x "$XENSTORED" ] || {
60		echo "No xenstored found"
61		exit 1
62	}
63	XS_OOM_SCORE=-$((${XENSTORED_OOM_MEM_THRESHOLD:-50} * 10))
64
65	[ "$XENSTORED_MAX_OPEN_FDS" = "unlimited" ] || {
66		[ -z "${XENSTORED_MAX_OPEN_FDS//[0-9]}" ] &&
67		[ -n "$XENSTORED_MAX_OPEN_FDS" ] || {
68			echo "XENSTORED_MAX_OPEN_FDS=$XENSTORED_MAX_OPEN_FDS invalid"
69			echo "Setting to default \"unlimited\"."
70			XENSTORED_MAX_OPEN_FDS=unlimited
71		}
72	}
73	[ -r /proc/sys/fs/nr_open ] && {
74		MAX_FDS=`cat /proc/sys/fs/nr_open`
75		[ "$XENSTORED_MAX_OPEN_FDS" = "unlimited" ] && XENSTORED_MAX_OPEN_FDS=$MAX_FDS
76		[ $XENSTORED_MAX_OPEN_FDS -gt $MAX_FDS ] && {
77			echo "XENSTORED_MAX_OPEN_FDS exceeds system limit."
78			echo "Setting to \"$MAX_FDS\"."
79			XENSTORED_MAX_OPEN_FDS=$MAX_FDS
80		}
81	}
82
83	rm -f @XEN_RUN_DIR@/xenstored.pid
84
85	echo -n Starting $XENSTORED...
86	prlimit --nofile=$XENSTORED_MAX_OPEN_FDS $XENSTORED --pid-file @XEN_RUN_DIR@/xenstored.pid $XENSTORED_ARGS
87
88	systemd-notify --booted 2>/dev/null || timeout_xenstore $XENSTORED || exit 1
89	XS_PID=`cat @XEN_RUN_DIR@/xenstored.pid`
90	echo $XS_OOM_SCORE >/proc/$XS_PID/oom_score_adj
91
92	exit 0
93}
94
95[ "$XENSTORETYPE" = "domain" ] && {
96	[ -z "$XENSTORE_DOMAIN_KERNEL" ] && XENSTORE_DOMAIN_KERNEL=@LIBEXEC@/boot/xenstore-stubdom.gz
97	XENSTORE_DOMAIN_ARGS="$XENSTORE_DOMAIN_ARGS --kernel $XENSTORE_DOMAIN_KERNEL"
98	[ -z "$XENSTORE_DOMAIN_SIZE" ] && XENSTORE_DOMAIN_SIZE=8
99	XENSTORE_DOMAIN_ARGS="$XENSTORE_DOMAIN_ARGS --memory $XENSTORE_DOMAIN_SIZE"
100	[ -z "$XENSTORE_MAX_DOMAIN_SIZE" ] || XENSTORE_DOMAIN_ARGS="$XENSTORE_DOMAIN_ARGS --maxmem $XENSTORE_MAX_DOMAIN_SIZE"
101	[ -z "$XENSTORED_TRACE" ] || XENSTORE_DOMAIN_ARGS="$XENSTORE_DOMAIN_ARGS -T xenstored-trace.log"
102
103	echo -n Starting $XENSTORE_DOMAIN_KERNEL...
104	${LIBEXEC_BIN}/init-xenstore-domain $XENSTORE_DOMAIN_ARGS || exit 1
105	systemd-notify --ready 2>/dev/null
106
107	exit 0
108}
109
110echo "illegal value $XENSTORETYPE for XENSTORETYPE"
111exit 1
112