feat: Generate unique MAC address (#611)

This commit is contained in:
Kroese 2024-01-30 04:46:44 +01:00 committed by GitHub
parent 72141bab7a
commit 3c31bc91e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 36 additions and 12 deletions

View File

@ -15,7 +15,9 @@ if [[ "$GPU" != [Yy1]* ]] || [[ "$ARCH" != "amd64" ]]; then
fi fi
DISPLAY_OPTS="-display egl-headless,rendernode=/dev/dri/renderD128 -vga $VGA" [[ "${VGA,,}" == "virtio" ]] && VGA="virtio-vga"
DISPLAY_OPTS="-display egl-headless,rendernode=/dev/dri/renderD128"
DISPLAY_OPTS="$DISPLAY_OPTS -vga none -device $VGA"
[ ! -d /dev/dri ] && mkdir -m 755 /dev/dri [ ! -d /dev/dri ] && mkdir -m 755 /dev/dri

View File

@ -3,8 +3,8 @@ set -Eeuo pipefail
# Docker environment variables # Docker environment variables
: "${MAC:=""}"
: "${DHCP:="N"}" : "${DHCP:="N"}"
: "${MAC:="02:11:32:AA:BB:CC"}"
: "${VM_NET_DEV:=""}" : "${VM_NET_DEV:=""}"
: "${VM_NET_TAP:="dsm"}" : "${VM_NET_TAP:="dsm"}"
@ -33,7 +33,7 @@ configureDHCP() {
fi fi
while ! ip link set "$VM_NET_TAP" up; do while ! ip link set "$VM_NET_TAP" up; do
info "Waiting for address to become available..." info "Waiting for MAC address $VM_NET_MAC to become available..."
sleep 2 sleep 2
done done
@ -128,7 +128,7 @@ configureNAT() {
ip address add ${VM_NET_IP%.*}.1/24 broadcast ${VM_NET_IP%.*}.255 dev dockerbridge ip address add ${VM_NET_IP%.*}.1/24 broadcast ${VM_NET_IP%.*}.255 dev dockerbridge
while ! ip link set dockerbridge up; do while ! ip link set dockerbridge up; do
info "Waiting for address to become available..." info "Waiting for IP address to become available..."
sleep 2 sleep 2
done done
@ -136,7 +136,7 @@ configureNAT() {
ip tuntap add dev "$VM_NET_TAP" mode tap ip tuntap add dev "$VM_NET_TAP" mode tap
while ! ip link set "$VM_NET_TAP" up promisc on; do 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 sleep 2
done done
@ -211,14 +211,20 @@ getInfo() {
error "$ADD_ERR -e \"VM_NET_DEV=NAME\" to specify another interface name." && exit 27 error "$ADD_ERR -e \"VM_NET_DEV=NAME\" to specify another interface name." && exit 27
fi fi
VM_NET_MAC="${VM_NET_MAC//-/:}" if [ -z "$VM_NET_MAC" ]; then
# Generate MAC address based on Docker container ID in hostname
VM_NET_MAC=$(echo "$HOST" | md5sum | sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/02:11:32:\3:\4:\5/')
fi
VM_NET_MAC="${VM_NET_MAC,,//-/:}"
if [[ ${#VM_NET_MAC} == 12 ]]; then if [[ ${#VM_NET_MAC} == 12 ]]; then
m="$VM_NET_MAC" 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}" VM_NET_MAC="${m:0:2}:${m:2:2}:${m:4:2}:${m:6:2}:${m:8:2}:${m:10:2}"
fi fi
if [[ ${#VM_NET_MAC} != 17 ]]; then 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 fi
GATEWAY=$(ip r | grep default | awk '{print $3}') GATEWAY=$(ip r | grep default | awk '{print $3}')
@ -242,16 +248,16 @@ getInfo
html "Initializing network..." html "Initializing network..."
if [[ "$DEBUG" == [Yy1]* ]]; then if [[ "$DEBUG" == [Yy1]* ]]; then
info "Container IP is $IP with gateway $GATEWAY on interface $VM_NET_DEV" && echo info "Host: $HOST IP: $IP Gateway: $GATEWAY Interface: $VM_NET_DEV MAC: $VM_NET_MAC"
[ -f /etc/resolv.conf ] && cat /etc/resolv.conf
echo
fi fi
if [[ "$DHCP" == [Yy1]* ]]; then if [[ "$DHCP" == [Yy1]* ]]; then
if [[ "$GATEWAY" == "172."* ]]; then if [[ "$GATEWAY" == "172."* ]] && [[ "$DEBUG" != [Yy1]* ]]; then
if [[ "$DEBUG" != [Yy1]* ]]; then
error "You can only enable DHCP while the container is on a macvlan network!" && exit 26 error "You can only enable DHCP while the container is on a macvlan network!" && exit 26
fi fi
fi
# Configuration for DHCP IP # Configuration for DHCP IP
configureDHCP configureDHCP

View File

@ -35,6 +35,7 @@ TEMPLATE="/var/www/index.html"
FOOTER1="$APP for Docker v$(</run/version)" FOOTER1="$APP for Docker v$(</run/version)"
FOOTER2="<a href='$SUPPORT'>$SUPPORT</a>" FOOTER2="<a href='$SUPPORT'>$SUPPORT</a>"
HOST=$(hostname -s)
KERNEL=$(uname -r | cut -b 1) KERNEL=$(uname -r | cut -b 1)
MINOR=$(uname -r | cut -d '.' -f2) MINOR=$(uname -r | cut -d '.' -f2)
ARCH=$(dpkg --print-architecture) ARCH=$(dpkg --print-architecture)

View File

@ -9,6 +9,21 @@ set -Eeuo pipefail
: "${HOST_MODEL:=""}" : "${HOST_MODEL:=""}"
: "${GUEST_SERIAL:=""}" : "${GUEST_SERIAL:=""}"
if [ -n "$HOST_MAC" ]; then
HOST_MAC="${HOST_MAC//-/:}"
if [[ ${#HOST_MAC} == 12 ]]; then
m="$HOST_MAC"
HOST_MAC="${m:0:2}:${m:2:2}:${m:4:2}:${m:6:2}:${m:8:2}:${m:10:2}"
fi
if [[ ${#HOST_MAC} != 17 ]]; then
error "Invalid HOST_MAC address: '$HOST_MAC', should be 12 or 17 digits long!" && exit 28
fi
fi
HOST_ARGS=() HOST_ARGS=()
HOST_ARGS+=("-cpu=$CPU_CORES") HOST_ARGS+=("-cpu=$CPU_CORES")
HOST_ARGS+=("-cpu_arch=$HOST_CPU") HOST_ARGS+=("-cpu_arch=$HOST_CPU")