From 295b8cfdec251852e2c002f2b5fd4299e63d1559 Mon Sep 17 00:00:00 2001 From: Michael Grote Date: Sat, 10 Jun 2023 10:01:06 +0200 Subject: [PATCH] add new version --- .gitignore | 52 ------------------------------- Dockerfile | 3 +- README.md | 16 +++++----- build_image.sh | 28 +++++++++++++++++ mikrotik-setup-script.rsc | 65 +++++++++++++++++++++++++++++++++++++++ run.sh | 65 ++++++++++++++++++++++++--------------- 6 files changed, 143 insertions(+), 86 deletions(-) delete mode 100644 .gitignore create mode 100644 build_image.sh create mode 100644 mikrotik-setup-script.rsc diff --git a/.gitignore b/.gitignore deleted file mode 100644 index c6127b3..0000000 --- a/.gitignore +++ /dev/null @@ -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 diff --git a/Dockerfile b/Dockerfile index 7e6de36..be220fd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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}" diff --git a/README.md b/README.md index def7979..66762bf 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/build_image.sh b/build_image.sh new file mode 100644 index 0000000..d83ccc3 --- /dev/null +++ b/build_image.sh @@ -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 + diff --git a/mikrotik-setup-script.rsc b/mikrotik-setup-script.rsc new file mode 100644 index 0000000..058f609 --- /dev/null +++ b/mikrotik-setup-script.rsc @@ -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!" + } +} diff --git a/run.sh b/run.sh index a758f62..427b838 100644 --- a/run.sh +++ b/run.sh @@ -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