add new version

This commit is contained in:
Michael Grote 2023-06-10 10:01:06 +02:00
parent 60df3d193f
commit 295b8cfdec
6 changed files with 143 additions and 86 deletions

52
.gitignore vendored
View File

@ -1,52 +0,0 @@
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf

View File

@ -16,5 +16,4 @@ RUN setcap cap_net_raw=+ep /bin/mdns-repeater
COPY run.sh /app/
RUN chmod +x /app/run.sh
ENTRYPOINT ["/app/run.sh"]
CMD ["/bin/mdns-repeater", "-f", "eth0.20", "eth0.100"]
ENTRYPOINT /app/run.sh "${VLANS}"

View File

@ -1,8 +1,10 @@
# docker-mdns-repeater-mikrotik
Save Image as Tar.gz
```bash
docker save registry.mgrote.net/mdns-repeater-mikrotik:latest -o ./mdns-repeater-mikrotik:latest.tgz
# How to build the container
To build the container make the script executable `chmod +x ./build_image.sh` and run it with one of the desired architectures as first parameter
```
sudo ./build_image.sh [x86|arm|arm64]
```
If you want to build the container for a platform different to your current run this once before building
```
sudo docker run --privileged --rm docker/binfmt:a7996909642ee92942dcd6cff44b9b95f08dad64
```
The final images are places in the `images` subfolder in the build directory.

28
build_image.sh Normal file
View File

@ -0,0 +1,28 @@
#!/bin/bash
usage(){
echo -e "Usage:\n\t$0 [arm|arm64|x86]"
exit 1
}
if [[ -z "$1" ]]; then
usage
fi
ARCH=${1,,}
[[ -d ./images ]] || mkdir ./images
case $ARCH in
arm)
sudo docker buildx build --no-cache --platform linux/arm/v6 -t mdns .
sudo docker save mdns -o ./images/mdns_arm32.tar
;;
arm64)
sudo docker buildx build --no-cache --platform linux/arm64 -t mdns .
sudo docker save mdns -o ./images/mdns_arm64.tar
;;
x86)
sudo docker buildx build --no-cache --platform linux/amd64 -t mdns .
sudo docker save mdns -o ./images/mdns_x86.tar
;;
*)
usage
;;
esac

65
mikrotik-setup-script.rsc Normal file
View File

@ -0,0 +1,65 @@
{
# name of bridge to add veth interface to
:local BRIDGENAME "bridgeLocal"
# name the veth interface will get
:local VETHNAME "mDNSTrunk"
# set hostname of container
:local HOSTNAME "mDNS"
# set vlan-ids to reflect traffic between
:local VLANS {100;200}
# define image filename
:local IMAGENAME "mdns_x86.tar"
# path to data root for the container
:local CONTAINERROOT "docker/container/mdns_repeater"
# -------------------------------
# variable holds all vlans seperated by spaces
:local ALLVLANS ""
# check bridge existance
:if ([:len [/interface bridge find name="$BRIDGENAME"]] = 0) do={
:put "Could not find a bridge with name '$BRIDGENAME' !"
:quit
}
# add veth interface
:if ([:len [/interface veth find name="$VETHNAME"]] = 0) do={
# add veth interface
/interface veth add address=172.19.19.2/24 gateway=172.19.19.1 name=$VETHNAME
}
# add veth interface to bridge
:if ([:len [/interface bridge port find interface=$VETHNAME]] = 0) do={
/interface bridge port add interface=$VETHNAME bridge=$BRIDGENAME
}
# for each vlan id
:foreach id in=$VLANS do={
# find existing vlan entry
:local vlanentry [/interface bridge vlan find vlan-ids ~ "(^|;)$id(;|\$)" and bridge=$BRIDGENAME]
:if ([:len $vlanentry] = 0) do={
# if entry does not exist add it to vlan list
/interface bridge vlan add bridge=$BRIDGENAME tagged="$BRIDGENAME,$VETHNAME" vlan-ids=$id
} else={
# if entry exists and veth interface is not a tagged member append veth interface to vlan as tagged interface
:if ([:len [/interface bridge vlan find vlan-ids=$id and current-tagged ~ "$VETHNAME"]] = 0) do={
/interface bridge vlan set $vlanentry tagged=([get $vlanentry tagged],"$VETHNAME")
}
}
# build ALLVLAN variable
:if ($ALLVLANS != "") do={
:set ALLVLANS ($ALLVLANS . " " . $id)
} else={
:set ALLVLANS $id
}
}
# add container environment variable wich defines the vlans used
:if ([:len [/container envs find name=mdns key=VLANS]] = 0) do={
/container envs add name=mdns key=VLANS value=$ALLVLANS
}
# finally add container
:if ([:len [/container find root-dir="$CONTAINERROOT"]] = 0) do={
/container add file=$IMAGENAME envlist=mdns logging=yes start-on-boot=yes root-dir="$CONTAINERROOT" interface=$VETHNAME hostname=$HOSTNAME
} else={
:put "Container already exists, please delete it beforehand and run again!"
}
}

65
run.sh
View File

@ -1,31 +1,46 @@
#!/bin/bash
# Exit on error
# on error exit
set -e
# set vlan parent interface
INTERFACE=eth0
# assign vlan ids via commandline parameter 1
VLANS=$1
if [[ -z "$VLANS" ]];then
echo -e "No VLAN-IDs assigned through environment variable with name 'VLANS'!\nExit container."
exit 1
fi
# build interface list with parent interface and vlan ids
ALLVLANIF=$(echo -n $VLANS | sed -re "s/\b([0-9]+)/${INTERFACE}.\1/g")
HOSTNAME="mDns"
INTERFACE="eth0"
VLANS="20 100"
# get MTU of parent interface
MTU=$(ip link show $INTERFACE | grep -Ei 'mtu \d+' | cut -d " " -f2)
MTU=$(ip link show "$INTERFACE" | awk '{print $5}')
for VLAN in $VLANS; do
# INTERFACE PROVISION
IFNAME="${INTERFACE}.${VLAN}"
[ ! -d "/sys/class/net/${IFNAME}" ] && {
echo "create interface ${IFNAME}"
ip link add link "$INTERFACE" name "$IFNAME" mtu "$MTU" type vlan id "$VLAN"
}
echo "bring up ${IFNAME} interface"
ip link set "${IFNAME}" up
# DHCP
[ -f "/var/run/udhcpc.${IFNAME}.pid" ] && {
kill "$(cat "/var/run/udhcpc.$IFNAME.pid")" || true
rm "/var/run/udhcpc.$IFNAME.pid"
}
echo "starting dhcp client on ${IFNAME}"
udhcpc -b -i "$IFNAME" -x hostname:"$HOSTNAME" -p "/var/run/udhcpc.${IFNAME}.pid"
# cleanup non used vlan interfaces
for VLAN in $(ls -1 /sys/class/net | grep -E "^${INTERFACE}\\." | grep -Ev "$(echo $ALLVLANIF | tr ' ' '|')") ;do
echo "Deleting orphaned interface: $VLAN"
ip link delete $VLAN
done
exec "$@"
# for each vlan id
for VLAN in $VLANS; do
VLANIF="${INTERFACE}.$VLAN"
# create vlan interface
[[ ! -d "/sys/class/net/${VLANIF}" ]] && {
echo "Creating interface ${VLANIF}"
ip link add link $INTERFACE name "$VLANIF" mtu $MTU type vlan id $VLAN
}
echo "Bring up $VLANIF interface"
ip link set "$VLANIF" up
# starting DHCP client on interface
[[ -f "/var/run/udhcpc.${VLANIF}.pid" ]] && {
kill "$(cat "/var/run/udhcpc.${VLANIF}.pid")" >/dev/null
rm "/var/run/udhcpc.${VLANIF}.pid"
}
echo "Starting dhcp client on ${VLANIF}"
udhcpc -b -i $VLANIF -x hostname:"$HOSTNAME" -p "/var/run/udhcpc.${VLANIF}.pid"
done
echo "Starting mDNS repeater process ... "
# run repeater on vlan interfaces
exec /bin/mdns-repeater -f $ALLVLANIF