1# SPDX-License-Identifier: GPL-2.0-only
2# Initially written by: Michael Tokarev <mjt@tls.msk.ru>
3# For QEMU Debian downstream package
4
5set -e
6
7. /etc/init.d/functions
8
9PATH=/sbin:/usr/sbin:/bin:/usr/bin
10DESC="QEMU Guest Agent"
11NAME=qemu-ga
12DAEMON=@bindir@/$NAME
13PIDFILE=/var/run/$NAME.pid
14
15# config
16DAEMON_ARGS=""
17# default transport
18TRANSPORT=virtio-serial:/dev/virtio-ports/org.qemu.guest_agent.0
19NO_START=0
20
21test ! -r /etc/default/qemu-guest-agent || . /etc/default/qemu-guest-agent
22test "$NO_START" = "0" || exit 0
23test -x "$DAEMON" || exit 0
24
25#
26# Function that checks whenever system has necessary environment
27# It also splits $TRANSPORT into $method and $path
28#
29do_check_transport() {
30	method=${TRANSPORT%%:*};
31	path=${TRANSPORT#*:}
32	case "$method" in
33	    virtio-serial | isa-serial)
34		if [ ! -e "$path" ]; then
35		    echo "$NAME: transport endpoint not found, not starting"
36		    return 1
37		fi
38		;;
39	esac
40}
41
42case "$1" in
43  start)
44	do_check_transport || exit 0
45	echo -n "Starting $DESC: "
46	start-stop-daemon -S -p $PIDFILE -x "$DAEMON" -- \
47		$DAEMON_ARGS -d -m "$method" -p "$path"
48	echo "$NAME."
49	;;
50  stop)
51	echo -n "Stopping $DESC: "
52	start-stop-daemon -K -x "$DAEMON" -p $PIDFILE
53	echo "$NAME."
54	;;
55  status)
56	status "$DAEMON"
57	exit $?
58	;;
59  restart|force-reload)
60	do_check_transport || exit 0
61	echo -n "Restarting $DESC: "
62	start-stop-daemon -K -x "$DAEMON" -p $PIDFILE
63	sleep 1
64	start-stop-daemon -S -p $PIDFILE -x "$DAEMON" -- \
65		$DAEMON_ARGS -d -m "$method" -p "$path"
66	echo "$NAME."
67	;;
68  *)
69	N=/etc/init.d/$NAME
70	echo "Usage: $N {start|stop|status|restart|force-reload}" >&2
71	exit 1
72	;;
73esac
74
75exit 0
76