Colors in log
This commit is contained in:
parent
366d830919
commit
1f1001f81d
1 changed files with 21 additions and 46 deletions
|
@ -22,69 +22,43 @@ set -Eeuo pipefail
|
||||||
|
|
||||||
configureDHCP() {
|
configureDHCP() {
|
||||||
|
|
||||||
VM_NET_VLAN="${VM_NET_TAP}_vlan"
|
|
||||||
GATEWAY=$(ip r | grep default | awk '{print $3}')
|
|
||||||
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/)
|
|
||||||
|
|
||||||
[[ "${DEBUG}" == [Yy1]* ]] && set -x
|
|
||||||
|
|
||||||
# Create a macvlan network to allow for communication between the host and the VM guest
|
|
||||||
{ ip link add link "${VM_NET_DEV}" "${VM_NET_VLAN}" type macvlan mode bridge ; rc=$?; } || :
|
|
||||||
|
|
||||||
if (( rc != 0 )); then
|
|
||||||
echo "ERROR: Cannot create macvlan interface. Please make sure the network type is 'macvlan' and not 'ipvlan',"
|
|
||||||
echo "ERROR: and that the NET_ADMIN capability has been added to the container config: --cap-add NET_ADMIN" && exit 15
|
|
||||||
fi
|
|
||||||
|
|
||||||
ip address add "${IP}" dev "${VM_NET_VLAN}"
|
|
||||||
ip link set dev "${VM_NET_VLAN}" up
|
|
||||||
|
|
||||||
ip route flush dev "${VM_NET_VLAN}"
|
|
||||||
ip route del "${NETWORK}" dev "${VM_NET_DEV}"
|
|
||||||
ip route add "${NETWORK}" dev "${VM_NET_VLAN}" metric 0
|
|
||||||
|
|
||||||
# Create a macvtap network for the VM guest
|
# Create a macvtap network for the VM guest
|
||||||
{ ip link add link "${VM_NET_DEV}" name "${VM_NET_TAP}" address "${VM_NET_MAC}" type macvtap mode bridge ; rc=$?; } || :
|
|
||||||
|
|
||||||
|
{ ip link add link "${VM_NET_DEV}" name "${VM_NET_TAP}" address "${VM_NET_MAC}" type macvtap mode bridge ; rc=$?; } || :
|
||||||
|
|
||||||
if (( rc != 0 )); then
|
if (( rc != 0 )); then
|
||||||
echo "ERROR: Capability NET_ADMIN has not been set most likely. Please add the "
|
error "Cannot create macvtap interface. Please make sure the network type is 'macvlan' and not 'ipvlan',"
|
||||||
echo "ERROR: following docker setting to your container: --cap-add NET_ADMIN" && exit 16
|
error "and that the NET_ADMIN capability has been added to the container config: --cap-add NET_ADMIN" && exit 16
|
||||||
fi
|
fi
|
||||||
|
|
||||||
ip link set "${VM_NET_TAP}" up
|
ip link set "${VM_NET_TAP}" up
|
||||||
|
|
||||||
{ set +x; } 2>/dev/null
|
|
||||||
|
|
||||||
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}"
|
||||||
|
|
||||||
# Create dev file (there is no udev in container: need to be done manually)
|
# Create dev file (there is no udev in container: need to be done manually)
|
||||||
IFS=: read -r MAJOR MINOR < <(cat /sys/devices/virtual/net/"${VM_NET_TAP}"/tap*/dev)
|
IFS=: read -r MAJOR MINOR < <(cat /sys/devices/virtual/net/"${VM_NET_TAP}"/tap*/dev)
|
||||||
|
(( MAJOR < 1)) && error "Cannot find: sys/devices/virtual/net/${VM_NET_TAP}" && exit 18
|
||||||
if (( MAJOR < 1)); then
|
|
||||||
echo "ERROR: Cannot find: sys/devices/virtual/net/${VM_NET_TAP}" && exit 18
|
|
||||||
fi
|
|
||||||
|
|
||||||
[[ ! -e "${TAP_PATH}" ]] && [[ -e "/dev0/${TAP_PATH##*/}" ]] && ln -s "/dev0/${TAP_PATH##*/}" "${TAP_PATH}"
|
[[ ! -e "${TAP_PATH}" ]] && [[ -e "/dev0/${TAP_PATH##*/}" ]] && ln -s "/dev0/${TAP_PATH##*/}" "${TAP_PATH}"
|
||||||
|
|
||||||
if [[ ! -e "${TAP_PATH}" ]]; then
|
if [[ ! -e "${TAP_PATH}" ]]; then
|
||||||
{ mknod "${TAP_PATH}" c "$MAJOR" "$MINOR" ; rc=$?; } || :
|
{ mknod "${TAP_PATH}" c "$MAJOR" "$MINOR" ; rc=$?; } || :
|
||||||
(( rc != 0 )) && echo "ERROR: Cannot mknod: ${TAP_PATH} ($rc)" && exit 20
|
(( rc != 0 )) && error "Cannot mknod: ${TAP_PATH} ($rc)" && exit 20
|
||||||
fi
|
fi
|
||||||
|
|
||||||
{ exec 30>>"$TAP_PATH"; rc=$?; } || :
|
{ exec 30>>"$TAP_PATH"; rc=$?; } || :
|
||||||
|
|
||||||
if (( rc != 0 )); then
|
if (( rc != 0 )); then
|
||||||
echo "ERROR: Cannot create TAP interface ($rc). Please add the following docker settings to your "
|
error "Cannot create TAP interface ($rc). Please add the following docker settings to your "
|
||||||
echo "ERROR: container: --device-cgroup-rule='c ${MAJOR}:* rwm' --device=/dev/vhost-net" && exit 21
|
error "container: --device-cgroup-rule='c ${MAJOR}:* rwm' --device=/dev/vhost-net" && exit 21
|
||||||
fi
|
fi
|
||||||
|
|
||||||
{ exec 40>>/dev/vhost-net; rc=$?; } || :
|
{ exec 40>>/dev/vhost-net; rc=$?; } || :
|
||||||
|
|
||||||
if (( rc != 0 )); then
|
if (( rc != 0 )); then
|
||||||
echo "ERROR: VHOST can not be found ($rc). Please add the following "
|
error "VHOST can not be found ($rc). Please add the following "
|
||||||
echo "ERROR: docker setting to your container: --device=/dev/vhost-net" && exit 22
|
error "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"
|
||||||
|
@ -92,15 +66,16 @@ configureDHCP() {
|
||||||
|
|
||||||
configureNAT () {
|
configureNAT () {
|
||||||
|
|
||||||
|
# Create a bridge with a static IP for the VM guest
|
||||||
|
|
||||||
VM_NET_IP='20.20.20.21'
|
VM_NET_IP='20.20.20.21'
|
||||||
[[ "${DEBUG}" == [Yy1]* ]] && set -x
|
[[ "${DEBUG}" == [Yy1]* ]] && set -x
|
||||||
|
|
||||||
# Create bridge with static IP for the VM guest
|
|
||||||
{ ip link add dev dockerbridge type bridge ; rc=$?; } || :
|
{ ip link add dev dockerbridge type bridge ; rc=$?; } || :
|
||||||
|
|
||||||
if (( rc != 0 )); then
|
if (( rc != 0 )); then
|
||||||
echo "ERROR: Capability NET_ADMIN has not been set most likely. Please add the "
|
error "Capability NET_ADMIN has not been set most likely. Please add the "
|
||||||
echo "ERROR: following docker setting to your container: --cap-add NET_ADMIN" && exit 23
|
error "following docker setting to your container: --cap-add NET_ADMIN" && exit 23
|
||||||
fi
|
fi
|
||||||
|
|
||||||
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
|
||||||
|
@ -128,7 +103,7 @@ configureNAT () {
|
||||||
if [[ $(< /proc/sys/net/ipv4/ip_forward) -eq 0 ]]; then
|
if [[ $(< /proc/sys/net/ipv4/ip_forward) -eq 0 ]]; then
|
||||||
{ sysctl -w net.ipv4.ip_forward=1 ; rc=$?; } || :
|
{ sysctl -w net.ipv4.ip_forward=1 ; rc=$?; } || :
|
||||||
if (( rc != 0 )); then
|
if (( rc != 0 )); then
|
||||||
echo "ERROR: Please add the following docker setting to your container: --sysctl net.ipv4.ip_forward=1" && exit 24
|
error "Please add the following docker setting to your container: --sysctl net.ipv4.ip_forward=1" && exit 24
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -178,6 +153,8 @@ configureNAT () {
|
||||||
$DNSMASQ ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS}
|
$DNSMASQ ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS}
|
||||||
|
|
||||||
{ set +x; } 2>/dev/null
|
{ set +x; } 2>/dev/null
|
||||||
|
|
||||||
|
[[ "${DEBUG}" == [Yy1]* ]] && echo
|
||||||
}
|
}
|
||||||
|
|
||||||
# ######################################
|
# ######################################
|
||||||
|
@ -191,7 +168,7 @@ if [ ! -c /dev/net/tun ]; then
|
||||||
chmod 666 /dev/net/tun
|
chmod 666 /dev/net/tun
|
||||||
fi
|
fi
|
||||||
|
|
||||||
[ ! -c /dev/net/tun ] && echo "ERROR: TUN network interface not available..." && exit 85
|
[ ! -c /dev/net/tun ] && error "TUN network interface not available..." && exit 85
|
||||||
|
|
||||||
# Create the necessary file structure for /dev/vhost-net
|
# Create the necessary file structure for /dev/vhost-net
|
||||||
if [ ! -c /dev/vhost-net ]; then
|
if [ ! -c /dev/vhost-net ]; then
|
||||||
|
@ -208,15 +185,15 @@ GATEWAY=$(ip r | grep default | awk '{print $3}')
|
||||||
if [[ "${DEBUG}" == [Yy1]* ]]; then
|
if [[ "${DEBUG}" == [Yy1]* ]]; 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
|
info "Container IP is ${IP} with gateway ${GATEWAY}" && echo
|
||||||
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "${DHCP}" == [Yy1]* ]]; then
|
if [[ "${DHCP}" == [Yy1]* ]]; then
|
||||||
|
|
||||||
if [[ "$GATEWAY" == "172."* ]]; then
|
if [[ "$GATEWAY" == "172."* ]]; then
|
||||||
echo -n "ERROR: You cannot enable DHCP while the container is "
|
error "You cannot enable DHCP while the container is "
|
||||||
echo "in a bridge network, only on a macvlan network!" && exit 86
|
error "in a bridge network, only on a macvlan network!" && exit 86
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Configuration for DHCP IP
|
# Configuration for DHCP IP
|
||||||
|
@ -231,6 +208,4 @@ fi
|
||||||
|
|
||||||
NET_OPTS="${NET_OPTS} -device virtio-net-pci,romfile=,netdev=hostnet0,mac=${VM_NET_MAC},id=net0"
|
NET_OPTS="${NET_OPTS} -device virtio-net-pci,romfile=,netdev=hostnet0,mac=${VM_NET_MAC},id=net0"
|
||||||
|
|
||||||
[[ "${DEBUG}" == [Yy1]* ]] && echo && echo "Finished network setup.." && echo
|
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
Loading…
Reference in a new issue