Files
pmaports-rtwo/main/postmarketos-usb-moded/rootfs-usr-bin-usb-moded-developer-mode
T
Dylan Van Assche ccd3a554a6 main/postmarketos-usb-moded: use service manager for unudhcpd
Also start set -e after config parameters are read. Fallbacks would trigger exit otherwise.

Part-of: <https://gitlab.postmarketos.org/postmarketOS/pmaports/-/merge_requests/8553>
2026-06-18 06:27:13 +00:00

71 lines
1.8 KiB
Bash

#!/bin/sh
# Enable/disable developer USB mode by delegating network operations to NetworkManager
# Dynamically generate NetworkManager connection because it is only available in the right USB mode
#
# Copyright (c) Dylan Van Assche (2025-2026)
CONFIGFS="/sys/kernel/config/usb_gadget"
IFACE=$(cat "$CONFIGFS"/usb-signaller-developer/functions/ncm.*/ifname 2>/dev/null)
IFACE=${IFACE:-"usb0"}
IP_ADDRESS_SERVER=172.16.42.1
IP_ADDRESS_CLIENT=172.16.42.2
CONNECTION_NAME=$(cat "$CONFIGFS"/usb-signaller-developer/configs/*/strings/0x409/configuration 2>/dev/null)
CONNECTION_NAME=${CONNECTION_NAME:-"Developer Mode"}
set -e
_start_unudhcpd() {
if [ -d "/run/systemd" ]; then
systemctl start unudhcpd@"$1"
elif [ -d "/etc/rc.conf" ]; then
rc-service unudhcpd."$1" start
else
echo "Cannot start unudhpcd!"
fi
}
_stop_unudhcpd() {
if [ -d "/run/systemd" ]; then
systemctl stop unudhcpd@"$1"
elif [ -d "/etc/rc.conf" ]; then
rc-service unudhcpd."$1" stop
else
echo "Cannot stop unudhpcd!"
fi
}
up() {
# Bring interface and NetworkManager connection up
nmcli connection add con-name "$CONNECTION_NAME" type ethernet \
ifname "$IFACE" ipv4.method manual ipv4.addresses "$IP_ADDRESS_SERVER"/16
nmcli connection up "$CONNECTION_NAME"
# Start DHCP server
_start_unudhcpd "$IFACE"
}
down() {
# Stop DHCP server
_stop_unudhcpd "$IFACE"
# Bring NetworkManager connection down
nmcli connection down "$CONNECTION_NAME"
nmcli connection delete "$CONNECTION_NAME"
# NetworkManager brings the interface up automatically,
# but doesn't remove it when bringing down.
ifconfig "$IFACE" down
}
case $1 in
up)
up
;;
down)
down
;;
*)
echo "need an argument (up|down)"
exit 0
;;
esac