mirror of
				https://github.com/vdsm/virtual-dsm.git
				synced 2025-11-04 17:04:52 +08:00 
			
		
		
		
	IP routing
This commit is contained in:
		
							parent
							
								
									fa69ff9166
								
							
						
					
					
						commit
						3a41f2dda0
					
				@ -27,6 +27,8 @@ configureDHCP() {
 | 
				
			|||||||
  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/)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  [[ "${DEBUG}" == [Yy1]* ]] && set -x
 | 
					  [[ "${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=$?; } || :
 | 
					  { ip link add link "${VM_NET_DEV}" "${VM_NET_VLAN}" type macvlan mode bridge ; rc=$?; } || :
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (( rc != 0 )); then
 | 
					  if (( rc != 0 )); then
 | 
				
			||||||
@ -41,6 +43,7 @@ configureDHCP() {
 | 
				
			|||||||
  ip route del "${NETWORK}" dev "${VM_NET_DEV}"
 | 
					  ip route del "${NETWORK}" dev "${VM_NET_DEV}"
 | 
				
			||||||
  ip route add "${NETWORK}" dev "${VM_NET_VLAN}" metric 0
 | 
					  ip route add "${NETWORK}" dev "${VM_NET_VLAN}" metric 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  # 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
 | 
				
			||||||
@ -50,9 +53,6 @@ configureDHCP() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  ip link set "${VM_NET_TAP}" up
 | 
					  ip link set "${VM_NET_TAP}" up
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ip address flush "${VM_NET_DEV}"
 | 
					 | 
				
			||||||
  ip address flush "${VM_NET_TAP}"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  { set +x; } 2>/dev/null
 | 
					  { set +x; } 2>/dev/null
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  TAP_NR=$(</sys/class/net/"${VM_NET_TAP}"/ifindex)
 | 
					  TAP_NR=$(</sys/class/net/"${VM_NET_TAP}"/ifindex)
 | 
				
			||||||
@ -94,8 +94,7 @@ configureNAT () {
 | 
				
			|||||||
  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
 | 
					  # 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
 | 
				
			||||||
@ -106,12 +105,12 @@ 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
 | 
				
			||||||
  ip link set dockerbridge up
 | 
					  ip link set dockerbridge up
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  #QEMU Works with taps, set tap to the bridge created
 | 
					  # QEMU Works with taps, set tap to the bridge created
 | 
				
			||||||
  ip tuntap add dev "${VM_NET_TAP}" mode tap
 | 
					  ip tuntap add dev "${VM_NET_TAP}" mode tap
 | 
				
			||||||
  ip link set "${VM_NET_TAP}" up promisc on
 | 
					  ip link set "${VM_NET_TAP}" up promisc on
 | 
				
			||||||
  ip link set dev "${VM_NET_TAP}" master dockerbridge
 | 
					  ip link set dev "${VM_NET_TAP}" master dockerbridge
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  #Add internet connection to the VM
 | 
					  # Add internet connection to the VM
 | 
				
			||||||
  iptables -t nat -A POSTROUTING -o "${VM_NET_DEV}" -j MASQUERADE
 | 
					  iptables -t nat -A POSTROUTING -o "${VM_NET_DEV}" -j MASQUERADE
 | 
				
			||||||
  iptables -t nat -A PREROUTING -i "${VM_NET_DEV}" -p tcp  -j DNAT --to $VM_NET_IP
 | 
					  iptables -t nat -A PREROUTING -i "${VM_NET_DEV}" -p tcp  -j DNAT --to $VM_NET_IP
 | 
				
			||||||
  iptables -t nat -A PREROUTING -i "${VM_NET_DEV}" -p udp  -j DNAT --to $VM_NET_IP
 | 
					  iptables -t nat -A PREROUTING -i "${VM_NET_DEV}" -p udp  -j DNAT --to $VM_NET_IP
 | 
				
			||||||
@ -124,7 +123,7 @@ configureNAT () {
 | 
				
			|||||||
  { set +x; } 2>/dev/null
 | 
					  { set +x; } 2>/dev/null
 | 
				
			||||||
  [[ "${DEBUG}" == [Yy1]* ]] && echo
 | 
					  [[ "${DEBUG}" == [Yy1]* ]] && echo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  #Check port forwarding flag
 | 
					  # Check port forwarding flag
 | 
				
			||||||
  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
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user