Skip to content

Commit

Permalink
feat: Generate unique MAC address (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
kroese authored Jan 29, 2024
1 parent ee1f3f8 commit 209f9b3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ set -Eeuo pipefail

# Docker environment variables

: "${MAC:=""}"
: "${DHCP:="N"}"
: "${HOST_PORTS:=""}"
: "${MAC:="82:cf:d0:5e:57:66"}"

: "${VM_NET_DEV:=""}"
: "${VM_NET_TAP:="qemu"}"
Expand Down Expand Up @@ -34,7 +34,7 @@ configureDHCP() {
fi

while ! ip link set "$VM_NET_TAP" up; do
info "Waiting for address to become available..."
info "Waiting for MAC address to become available..."
sleep 2
done

Expand Down Expand Up @@ -152,15 +152,15 @@ configureNAT() {
ip address add ${VM_NET_IP%.*}.1/24 broadcast ${VM_NET_IP%.*}.255 dev dockerbridge

while ! ip link set dockerbridge up; do
info "Waiting for address to become available..."
info "Waiting for IP address to become available..."
sleep 2
done

# QEMU Works with taps, set tap to the bridge created
ip tuntap add dev "$VM_NET_TAP" mode tap

while ! ip link set "$VM_NET_TAP" up promisc on; do
info "Waiting for tap to become available..."
info "Waiting for TAP to become available..."
sleep 2
done

Expand Down Expand Up @@ -238,14 +238,20 @@ getInfo() {
error "$ADD_ERR -e \"VM_NET_DEV=NAME\" to specify another interface name." && exit 27
fi

VM_NET_MAC="${VM_NET_MAC//-/:}"
if [ -z "$MAC" ]; then
# Generate MAC address based on hostname
MAC=$(echo "$HOST" | md5sum | sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/02:\1:\2:\3:\4:\5/')
fi

VM_NET_MAC="${MAC//-/:}"

if [[ ${#VM_NET_MAC} == 12 ]]; then
m="$VM_NET_MAC"
VM_NET_MAC="${m:0:2}:${m:2:2}:${m:4:2}:${m:6:2}:${m:8:2}:${m:10:2}"
fi

if [[ ${#VM_NET_MAC} != 17 ]]; then
error "Invalid mac address: '$VM_NET_MAC', should be 12 or 17 digits long!" && exit 28
error "Invalid MAC address: '$VM_NET_MAC', should be 12 or 17 digits long!" && exit 28
fi

GATEWAY=$(ip r | grep default | awk '{print $3}')
Expand All @@ -268,7 +274,7 @@ getInfo
html "Initializing network..."

if [[ "$DEBUG" == [Yy1]* ]]; then
info "Container IP is $IP with gateway $GATEWAY on interface $VM_NET_DEV" && echo
info "Container: $HOST IP: $IP Gateway: $GATEWAY Interface: $VM_NET_DEV MAC: $VM_NET_MAC" && echo
fi

if [[ "$DHCP" == [Yy1]* ]]; then
Expand Down
1 change: 1 addition & 0 deletions src/reset.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ TEMPLATE="/var/www/index.html"
FOOTER1="$APP for Docker v$(</run/version)"
FOOTER2="<a href='$SUPPORT'>$SUPPORT</a>"

HOST=$(hostname -s)
KERNEL=$(uname -r | cut -b 1)
MINOR=$(uname -r | cut -d '.' -f2)
ARCH=$(dpkg --print-architecture)
Expand Down

0 comments on commit 209f9b3

Please sign in to comment.