GUIDE:Keepalive Openmediavault with WOL packets, shutdown server when WOL packets stop - Systemd Service

  • I've just bitten the bullet and moved my old 2010 vintage atom server from OMV2 after 3/4 years of use to OMV 4.28 on Debian 9 (Stretch) using the Openmediavault iso. Everything went smoothly. I use it for NFS shares and unionfs for my old usb drives. I have 14 usb drives, ranging from 40GB to 500GB, that I've accumulated over the years, giving me 3.5 TB.
    The way I use the server is I have a small script I execute from my laptop to send a couple of WOL packets every 30 seconds to the Openmediavault server. On the Openmediavault 2.x server, I had another script, a 'keep alive' script. The server receives the WOL packets, wakes up and remains up and if it stops receiving the WOL packets after 5 minutes, the server automatically shuts down. That way, I can control the startup/shutdown of the server myself, "manually". This is the way my old buffalo Linkstation used to work before I moved to the significantly superior and brilliant Openmediavault. With OMV2, I added a line to /etc/rc.local and copied over my 'keep alive' script.
    However, with OMV4, Debian 9 uses Systemd for startup scripts, so I couldn't just do the same thing as before.
    I initially tried the 'Autoshutdown' plugin, but that won't look for port 9 (The WOL port), even though the GUI allows it.....only through use of it's fake mode did I realise this. I did try to see if the the other Autoshutdown conditions would be suitable for me but concluded that they didn't have the flexibility of the prescence or absence of WOL packets to start/shutdown OMV server, especially as I use different (Linux/Android/Windows) clients to connect to the OMV server.


    Yesterday, I learned enough about Systemd to setup my 'keep alive' of my OMV4 server. I have documented it here for other people.


    First, on OMV server, setup Wake On Lan of the network card by following information for Debian Stretch https://wiki.debian.org/WakeOnLan#Wake_On_LAN
    (i.e. creating file cat /etc/network/interfaces.d/enp1s0 and checking it works). NOTE enp1s0 is the name of the interface on my OMV server, yours may be different, so you'll need to use command line 'ip link' to check interface name.)


    Setup on OMV server, systemd, keepalive-with-wol.service
    1.) Install tcpdump. This is used to check for WOL packets. Use command line on OMV server:
    apt-get install tcpdump


    2.) Create a file on OMV server named 'keepalive-with-wol.service' in /etc/systemd/system
    ......or copy file from laptop using command line e.g.
    scp keepalive-with-wol.service root@openmediavault:/etc/systemd/system


    Set up file (it's structure is just like an old style MS Windows .ini file) named 'keepalive-with-wol.service' as follows:
    [Unit]
    Description=keepalive-with-wol.service
    Requires=network.target
    After=network.target


    [Service]
    Type=simple
    Restart=on-success
    RuntimeMaxSec=300
    ExecStart=/bin/bash -c "tcpdump -c 2 -i enp1s0 '(udp and port 9)'"
    ExecStop=/bin/bash -c "echo STOPPING keepalive-with-wol.service"
    ExecStopPost=/bin/bash -c 'if [ "$$SERVICE_RESULT" = timeout ]; then systemctl poweroff; fi'


    [Install]
    WantedBy=multi-user.target


    3.) Enable the 'keepalive-with-wol.service' service to start at bootup using command line on OMV server:
    systemctl enable keepalive-with-wol


    How it Works:
    --------------
    Systemd service 'keepalive-with-wol.service' is automatically started at bootup and tcpdump command is executed, which waits to receive WOL magic packets. Once a WOL is received, the tcpdump process finishes with status 'success' and the service stops.
    To keep rechecking, the service is restarted on success i.e. 'Restart=success'.
    The tcpdump process will wait indefinitely for the WOL packets, so if no WOL packets received, to cause a timeout after 5 minutes, the 'RuntimeMax=300' means systemd waits for 300 seconds before killing the tcpdump process and stopping the service with status 'timeout'. Note, the RuntimeMax "counter" is reset for each start or restart of the service.
    The 'ExecStopPost' section, means that once the service stopped, the exit status is checked and if the service is stopped because of a 'timeout' i.e. no WOL packets received, then system is powered off.


    4.) Here is the bash script (I name StartOpenmediavault.sh) I use on my laptop to remotely start OMV server with WOL packets. Note, '70:71:BC:BC:73:C2' is the mac address of my OMV server network card. You might need to 'sudo apt-get install wakeonlan' on your laptop if wakeonlan program is not already installed. Plus don't forget to make the script executable !


    #!/bin/bash
    echo -ne "\033]0;$("basename" $0)\007"
    #
    echo
    echo Starting OPENMEDIAVAULT
    echo


    while [ 6 -lt 9 ]; do


    wakeonlan 70:71:BC:BC:73:C2
    wakeonlan 70:71:BC:BC:73:C2
    wakeonlan 70:71:BC:BC:73:C2


    sleep 30
    done



    5.) Useful systemd Commands
    systemctl status keepalive-with-wol
    systemctl start keepalive-with-wol
    systemctl stop keepalive-with-wol
    systemctl enable keepalive-with-wol
    systemctl disable keepalive-with-wol
    systemctl daemon-reload


    6.) Useful Links:
    -------------
    https://www.writebash.com/adva…vice-in-debian-9-309.html
    https://linuxconfig.org/how-to…emd-service-unit-in-linux
    https://www.freedesktop.org/so…md/man/systemd.unit.html#
    https://www.freedesktop.org/so…man/systemd.service.html#
    https://www.freedesktop.org/so…md/man/systemd.timer.html
    (ExecStopPost:)
    https://unix.stackexchange.com…-use-onfailure-in-systemd
    https://unix.stackexchange.com…mand-when-a-service-fails
    https://www.freedesktop.org/so…ec.html#%24SERVICE_RESULT

Jetzt mitmachen!

Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!