yggdrasil-network / yggdrasil-go

An experiment in scalable routing as an encrypted IPv6 overlay network
https://yggdrasil-network.github.io
Other
3.53k stars 242 forks source link

Provide a sysv init script with the deb package #735

Open akhilman opened 4 years ago

akhilman commented 4 years ago

Please provide a SysV init script for Debian based distributions like antiX, MX Linux, Dog Linux, Puppy Linux and Devuan.

akhilman commented 4 years ago

Here is my attempt to write an init script. Not sure if it's correct, but it works fine.

#!/bin/sh

### BEGIN INIT INFO
# Provides:          yggdrasil
# Required-Start:    $local_fs
# Required-Stop:
# Should-Start:      $network $portmap
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: End-to-end network
# Description:       A fully end-to-end encrypted IPv6 network
### END INIT INFO

. /lib/lsb/init-functions

[ -f /etc/default/rcS ] && . /etc/default/rcS
PATH=/bin:/usr/bin:/sbin:/usr/sbin
DAEMON=/usr/bin/yggdrasil
CONFIG_FILE="/etc/yggdrasil.conf"
DAEMON_OPTS="-useconffile $CONFIG_FILE" 
NAME=yggdrasil
DESC="end-to-end network"

CONFFILE="/etc/yggdrasil.conf"

gen_conf() {
    $DAEMON -genconf > $CONFIG_FILE
    return $?
}

probe_tun() {
    modprobe tun
    return $?
}

probe_ipv6() {
    modprobe ipv6
    return $?
}

pre_start() {
    if [ ! -f "$CONFFILE" ]; then
        log_daemon_msg 'Generating configuration file'
        if gen_conf; then
            log_end_msg 0
        else
            log_end_msg 1
            exit 1
        fi
    fi

    if [ ! -d /sys/module/ipv6/ ]; then
        log_daemon_msg 'Inserting ipv6 module'
        if probe_ipv6; then
            log_end_msg 0
        else
            log_end_msg 1
            exit 1
        fi
    fi

    if [ ! -e /dev/net/tun ]; then
        log_daemon_msg 'Inserting TUN module'
        if probe_tun; then
            log_end_msg 0
        else
            log_end_msg 1
            exit 1
        fi
    fi

    if [ "$(cat /sys/module/ipv6/parameters/disable)" -eq "1" ]; then
        echo "ipv6 is disabled"
        exit 1
    fi
}

test -x $DAEMON || exit 0

set -e

case "$1" in
  start)
    pre_start
    log_daemon_msg "Starting $DESC" "$NAME"
    start-stop-daemon --start --background --quiet \
        --pidfile /var/run/$NAME.pid --make-pidfile \
        --exec $DAEMON -- $DAEMON_OPTS
    log_end_msg $?
    ;;
  stop)
    log_daemon_msg "Stopping $DESC" "$NAME"
    start-stop-daemon --stop --oknodo --quiet --pidfile /var/run/$NAME.pid \
        --retry 10 --exec $DAEMON
    log_end_msg $?
    ;;
  force-reload)
    # check whether $DAEMON is running. If so, restart
    start-stop-daemon --stop --test --quiet --pidfile \
        /var/run/$NAME.pid --exec $DAEMON \
    && $0 restart \
    || exit 0
    ;;
  restart)
    log_daemon_msg "Restarting $DESC" "$NAME"
    $0 stop
    $0 start
    ;;
  status)
    if [ -s /var/run/$NAME.pid ]; then
            RUNNING=$(cat /var/run/$NAME.pid)
            if [ -d /proc/$RUNNING ]; then
                if [ $(readlink /proc/$RUNNING/exe) = $DAEMON ]; then
                    log_success_msg "$NAME is running"
                    exit 0
                fi
            fi

            # No such PID, or executables don't match
            log_failure_msg "$NAME is not running, but pidfile existed"
            rm /var/run/$NAME.pid
            exit 1
        else
            rm -f /var/run/$NAME.pid
            log_failure_msg "$NAME not running"
            exit 1
        fi
    ;;
  *)
    N=/etc/init.d/$NAME
    log_failure_msg "Usage: $N {start|stop|restart|force-reload}"
    exit 1
    ;;
esac