added ipvlan management to rtpctl.d

This commit is contained in:
Lockszmith (runtipi@kateryna) 2025-02-16 13:34:45 -05:00 committed by Lockszmith (@kateryna)
parent 3877084515
commit f420053442
1 changed files with 85 additions and 1 deletions

View File

@ -20,13 +20,15 @@ set -e
cd "${RUNTIPI_ROOT}" > /dev/null
. ./user-config/.env.local
runtipi-cli() {
if ! [ -x "${RUNTIPI_CLI}" ]; then
>&2 printf '%s\n' \
"ERROR: ${RUNTIPI_CLI} not found or not executable!"
return 1
fi
"${RUNTIPI_CLI}" "${@}"
"${RUNTIPI_CLI}" "${@}"
}
append_file_param() {
@ -53,6 +55,82 @@ runtipi-app-docker-compose() {
${@:2}
}
link-exists() {
local EXIT_CODE=0
printf 'Current status:\n link: '
ip -br -c link show "${1:?Must supply device name}" 2>&1 || return 1
}
manage-ipvlan() {
local IPVLAN_IF="${2:-ipvlan-lan}"
local NET_IP="${NET_IP:-${INTERNAL_IP:?}}"
local IP_BASE="${NET_IP%\.[[:digit:]]*}"
local NET_IF="${NET_IF:-"$(ip -4 -br a s to "${NET_IP}" | cut -d' ' -f1)"}"
NET_IF="${NET_IF:-$($(ip -4 -br a s to "${IP_BASE}.0/24" | cut -d' ' -f1))}"
NET_IF="${NET_IF:?"Could not detect network interface for ${NET_IP}"}"
case "${1}" in
rm)
manage-ipvlan status "${IPVLAN_IF}" && ( \
ip link delete "${IPVLAN_IF}" \
&& printf '%s\n' "${IPVLAN_IF} removed"
) || return 1
;;
add)
link-exists "${IPVLAN_IF}" > /dev/null 2>&1 \
&& link-exists "${IPVLAN_IF}" || ( \
ip link add "${IPVLAN_IF}" link "${NET_IF}" type ipvlan mode l2 \
&& ip addr add ${NET_IP} dev "${IPVLAN_IF}" \
&& ip link set "${IPVLAN_IF}" up \
&& printf '%s\n' "${IPVLAN_IF} created"\
&& link-exists "${IPVLAN_IF}" \
|| ip link delete "${IPVLAN_IF}"
)
;;
route)
local ROUTE_SCOPE="${3:?Must supply routing scope in the form of ###.###.###.###[/##]}"
manage-ipvlan add "${IPVLAN_IF}" > /dev/null 2>&1 \
&& for IP in "${@:3}"; do
# Only add route if it does not already exists
printf 'Adding route for %-15s ' "$IP"
ip route get "$IP" 2> /dev/null | grep -q "dev ${IPVLAN_IF} src ${NET_IP//\./\\.}" \
&& printf 'Skipping, already exists.' \
|| ip route add "$IP" dev "${IPVLAN_IF}" \
&& printf '\n' \
|| (printf 'Could NOT add %s\n' "$IP" >&2; return 1)
done
link-exists "${IPVLAN_IF}"
;;
status)
link-exists "${IPVLAN_IF}" || return 1
;;
*)
printf '%s %-12s %s\n' \
"$(manage-ipvlan status)" "" ""\
"" "" ""\
"Usage:" "" ""\
" ${BASE_NAME} ipvlan <command>" "" ""\
"" "" "" \
"Available commands:" "" ""\
"" "" "" \
"" "fix" "implement ipvlan fix" \
"" "rm" "remove ipvlan fix" \
"" "status" "output interface status" \
return 0
;;
esac
( printf 'addr: ' && ip -4 -br -c addr show "${IPVLAN_IF}" \
&& printf 'Routes:\n' \
&& ip -c route show dev "${IPVLAN_IF}" \
| awk '{print} END{if (NR==0) print "<none found>"}'
) 2>&1 | sed -e '/[^:]$/s/^/ /' \
|| return 1
# ip link show [ DEVICE | group GROUP ] [up] [master DEV] [vrf NAME] [type TYPE] [nomaster]
}
dls() {
local base='{{.Status}}\t{{.ID}}\t{{.Names}}\t{{.Image}}' #'\t{{.Networks}}\t{{.Ports}}\t{{.Mounts}}'
local compose='{{.Label "com.docker.compose.project"}}\t{{.Label "com.docker.compose.service"}}'
@ -95,6 +173,9 @@ case "${1}" in
shell)
runtipi-app-docker-compose "${2:?}" exec ${5:+"${@:5}"} -it "${4:-${2}}" "${3:-bash}"
;;
ipvlan)
manage-ipvlan "${@:2}"
;;
setup)
ln -s $2 "$(cd -- "${SCRIPT_DIR}" && pwd)/${BASE_NAME}" "${3:-$HOME/.local/bin/}"
;;
@ -123,6 +204,9 @@ case "${1}" in
"" "shell" "enter an insteractive shell" \
"" "down-all" "stop and remove everything" \
"" "" "" \
"networking" "" ""\
"" "ipvlan" "manage ipvlan networking interface fix" \
"" "" "" \
"misc." "" ""\
"" "exec" "execute within the shell, START_DIR env applies" \
"" "" "" \