kit/systemd/system/qmi-network@.service

82 lines
2.3 KiB
Desktop File
Executable File

# apt install --no-install-recommends libqmi-utils ifmetric
# cp -v ./qmi-network@.service /etc/systemd/system/
# systemctl daemon-reload
# systemctl enable --now qmi-network@0
# This will NOT work without a .network for your wwan device.
[Unit]
Description=qmi-network for cdc-wdm%i device
Before=freepbx.service
Before=asterisk.service
Requires=sys-subsystem-net-devices-wwan%i.device
[Service]
Type=simple
Restart=always
RestartSec=60s
TimeoutSec=120s
KillMode=process
##
# A note on metrics.
# The lower the value the more priority the route has over others.
# Normal metric (Set this higher so it won't be used over the default
# route).
Environment=NMETRIC=2048
# Failover metric (Set this lower so it's the default route when other
# routes fail).
Environment=FOMETRIC=128
# Ping host.
Environment=PHOST="dns9.quad9.net"
Environment=PHOSTIP="9.9.9.9"
# Ping test interval.
Environment=PTESTINT=20
# Ping timeout. (Ping timeout / knock-out)
Environment=PTESTKO=30
# Ping count (Number of pings).
Environment=PTESTCOUNT=3
##
# Make sure the state is cleared before starting.
ExecStartPre=-rm /tmp/qmi-network-state-cdc-wdm%i
# Stop wwan so it can be reconfigured.
ExecStartPre=networkctl down wwan%i
# Raw IP must be enabled.
ExecStartPre=sh -c "echo 'Y' | tee /sys/class/net/wwan%i/qmi/raw_ip"
# Bring up the network.
ExecStartPre=networkctl up wwan%i
# Start the network via qmi-network scripts.
# As some networks and/or devices take a long time to connect we should
# give it some time to be ready before starting the connection process.
ExecStartPre=-sh -e -c "sleep 5; qmi-network /dev/cdc-wdm%i start && sleep 5;"
# Do a test ping before we start the main loop.
ExecStartPre=sh -e -c "ping -w ${PTESTKO} -I wwan%i -c ${PTESTCOUNT} ${PHOSTIP}"
# Main loop with **very basic** failover.
ExecStart=sh -e -c "while true; do ping -w ${PTESTKO} -I wwan%i -c ${PTESTCOUNT} ${PHOSTIP} 2>&1 >/dev/null; (ping -w ${PTESTKO} -c ${PTESTCOUNT} ${PHOST} 2>&1 >/dev/null && ifmetric wwan%i ${NMETRIC}) || (ping -w ${PTESTKO} -I wwan%i -c ${PTESTCOUNT} ${PHOST} 2>&1 >/dev/null && ifmetric wwan%i ${FOMETRIC}); sleep ${PTESTINT}; done;"
# Stop.. DOWN TIME!
ExecStop=networkctl down wwan%i
ExecStop=qmi-network /dev/cdc-wdm%i stop
# Be sure the network state is cleared on stop too.
ExecStop=-rm /tmp/qmi-network-state-cdc-wdm%i
[Install]
WantedBy=sys-subsystem-net-devices-wwan%i.device