Sync
This commit is contained in:
parent
68c21e783d
commit
81d45c7d4c
1 changed files with 29 additions and 14 deletions
|
@ -1,7 +1,7 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -eu
|
set -eu
|
||||||
|
|
||||||
# Docker environment variabeles
|
# Docker environment variables
|
||||||
|
|
||||||
: ${VM_NET_TAP:='qemu'}
|
: ${VM_NET_TAP:='qemu'}
|
||||||
: ${VM_NET_DEV:='eth0'}
|
: ${VM_NET_DEV:='eth0'}
|
||||||
|
@ -25,7 +25,11 @@ configureDHCP() {
|
||||||
NETWORK=$(ip -o route | grep "${VM_NET_DEV}" | grep -v default | awk '{print $1}')
|
NETWORK=$(ip -o route | grep "${VM_NET_DEV}" | grep -v default | awk '{print $1}')
|
||||||
IP=$(ip address show dev "${VM_NET_DEV}" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)
|
IP=$(ip address show dev "${VM_NET_DEV}" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)
|
||||||
|
|
||||||
ip l add link "${VM_NET_DEV}" "${VM_NET_VLAN}" type macvlan mode bridge
|
if ! ip link add link "${VM_NET_DEV}" "${VM_NET_VLAN}" type macvlan mode bridge > /dev/null 2>&1 ; then
|
||||||
|
echo -n "ERROR: Capability NET_ADMIN has not been set. Please add the "
|
||||||
|
echo "following docker setting to your container: --cap-add NET_ADMIN" && exit 15
|
||||||
|
fi
|
||||||
|
|
||||||
ip address add "${IP}" dev "${VM_NET_VLAN}"
|
ip address add "${IP}" dev "${VM_NET_VLAN}"
|
||||||
ip link set dev "${VM_NET_VLAN}" up
|
ip link set dev "${VM_NET_VLAN}" up
|
||||||
|
|
||||||
|
@ -35,10 +39,14 @@ configureDHCP() {
|
||||||
ip route add "${NETWORK}" dev "${VM_NET_VLAN}" metric 0
|
ip route add "${NETWORK}" dev "${VM_NET_VLAN}" metric 0
|
||||||
ip route add default via "${GATEWAY}"
|
ip route add default via "${GATEWAY}"
|
||||||
|
|
||||||
echo "Info: Acquiring an IP address via DHCP using MAC address ${VM_NET_MAC}..."
|
echo "INFO: Acquiring an IP address via DHCP using MAC address ${VM_NET_MAC}..."
|
||||||
|
|
||||||
ip l add link "${VM_NET_DEV}" name "${VM_NET_TAP}" address "${VM_NET_MAC}" type macvtap mode bridge || true
|
if ! ip link add link "${VM_NET_DEV}" name "${VM_NET_TAP}" address "${VM_NET_MAC}" type macvtap mode bridge > /dev/null 2>&1 ; then
|
||||||
ip l set "${VM_NET_TAP}" up
|
echo -n "ERROR: Capability NET_ADMIN has not been set. Please add the "
|
||||||
|
echo "following docker setting to your container: --cap-add NET_ADMIN" && exit 16
|
||||||
|
fi
|
||||||
|
|
||||||
|
ip link set "${VM_NET_TAP}" up
|
||||||
|
|
||||||
ip a flush "${VM_NET_DEV}"
|
ip a flush "${VM_NET_DEV}"
|
||||||
ip a flush "${VM_NET_TAP}"
|
ip a flush "${VM_NET_TAP}"
|
||||||
|
@ -46,12 +54,12 @@ configureDHCP() {
|
||||||
DHCP_IP=$(dhclient -v "${VM_NET_TAP}" 2>&1 | grep ^bound | cut -d' ' -f3)
|
DHCP_IP=$(dhclient -v "${VM_NET_TAP}" 2>&1 | grep ^bound | cut -d' ' -f3)
|
||||||
|
|
||||||
if [[ "${DHCP_IP}" == [0-9.]* ]]; then
|
if [[ "${DHCP_IP}" == [0-9.]* ]]; then
|
||||||
echo "Info: Successfully acquired IP ${DHCP_IP} from the DHCP server..."
|
echo "INFO: Successfully acquired IP ${DHCP_IP} from the DHCP server..."
|
||||||
else
|
else
|
||||||
echo "ERROR: Cannot acquire an IP address from the DHCP server" && exit 16
|
echo "ERROR: Cannot acquire an IP address from the DHCP server" && exit 17
|
||||||
fi
|
fi
|
||||||
|
|
||||||
ip a flush "${VM_NET_TAP}"
|
ip address flush "${VM_NET_TAP}"
|
||||||
|
|
||||||
TAP_NR=$(</sys/class/net/"${VM_NET_TAP}"/ifindex)
|
TAP_NR=$(</sys/class/net/"${VM_NET_TAP}"/ifindex)
|
||||||
TAP_PATH="/dev/tap${TAP_NR}"
|
TAP_PATH="/dev/tap${TAP_NR}"
|
||||||
|
@ -72,8 +80,8 @@ configureDHCP() {
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! exec 30>>"$TAP_PATH"; then
|
if ! exec 30>>"$TAP_PATH"; then
|
||||||
echo -n "ERROR: Please add the following docker variables to your container: "
|
echo -n "ERROR: Cannot create TAP interface. Please add the following docker settings to your "
|
||||||
echo "--device=/dev/vhost-net --device-cgroup-rule='c ${MAJOR}:* rwm'" && exit 21
|
echo "container: --device-cgroup-rule='c ${MAJOR}:* rwm' --device=/dev/vhost-net" && exit 21
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Create /dev/vhost-net
|
# Create /dev/vhost-net
|
||||||
|
@ -83,8 +91,8 @@ configureDHCP() {
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! exec 40>>/dev/vhost-net; then
|
if ! exec 40>>/dev/vhost-net; then
|
||||||
echo -n "ERROR: VHOST can not be found. Please add the following docker "
|
echo -n "ERROR: VHOST can not be found. Please add the following "
|
||||||
echo "variable to your container: --device=/dev/vhost-net" && exit 22
|
echo "docker setting to your container: --device=/dev/vhost-net" && exit 22
|
||||||
fi
|
fi
|
||||||
|
|
||||||
NET_OPTS="-netdev tap,id=hostnet0,vhost=on,vhostfd=40,fd=30"
|
NET_OPTS="-netdev tap,id=hostnet0,vhost=on,vhostfd=40,fd=30"
|
||||||
|
@ -95,7 +103,12 @@ configureNAT () {
|
||||||
VM_NET_IP='20.20.20.21'
|
VM_NET_IP='20.20.20.21'
|
||||||
|
|
||||||
#Create bridge with static IP for the VM guest
|
#Create bridge with static IP for the VM guest
|
||||||
ip link add dev dockerbridge type bridge
|
|
||||||
|
if ! ip link add dev dockerbridge type bridge > /dev/null 2>&1 ; then
|
||||||
|
echo -n "ERROR: Capability NET_ADMIN has not been set. Please add the "
|
||||||
|
echo "following docker setting to your container: --cap-add NET_ADMIN" && exit 23
|
||||||
|
fi
|
||||||
|
|
||||||
ip addr add ${VM_NET_IP%.*}.1/24 broadcast ${VM_NET_IP%.*}.255 dev dockerbridge
|
ip addr add ${VM_NET_IP%.*}.1/24 broadcast ${VM_NET_IP%.*}.255 dev dockerbridge
|
||||||
ip link set dockerbridge up
|
ip link set dockerbridge up
|
||||||
|
|
||||||
|
@ -181,7 +194,9 @@ GATEWAY=$(ip r | grep default | awk '{print $3}')
|
||||||
if [ "$DEBUG" = "Y" ]; then
|
if [ "$DEBUG" = "Y" ]; then
|
||||||
|
|
||||||
IP=$(ip address show dev "${VM_NET_DEV}" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)
|
IP=$(ip address show dev "${VM_NET_DEV}" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)
|
||||||
echo "Info: Container IP is ${IP} with gateway ${GATEWAY}" && echo
|
echo "INFO: Container IP is ${IP} with gateway ${GATEWAY}" && echo
|
||||||
|
ifconfig
|
||||||
|
ip route && echo
|
||||||
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue