Created
December 18, 2023 13:30
-
-
Save Xm798/8fc088fe4f56a0f520df5bb3c7862c6d to your computer and use it in GitHub Desktop.
Setup macvlan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# macvlan 接口名称[可不修改] | |
MACVLAN_INTERFACE="macvlan-host" | |
# 桥接网口[需要修改] | |
PARENT_INTERFACE="eth0" | |
# macvlan 接口 IP [需修改] | |
IP_ADDRESS="192.168.1.254" | |
# 路由列表 [需修改] | |
ROUTES=("192.168.1.201" "192.168.1.202") | |
start_macvlan() { | |
ip link add $MACVLAN_INTERFACE link $PARENT_INTERFACE type macvlan mode bridge | |
ip addr add $IP_ADDRESS dev $MACVLAN_INTERFACE | |
ip link set $MACVLAN_INTERFACE up | |
for route in "${ROUTES[@]}"; do | |
ip route add $route dev $MACVLAN_INTERFACE | |
done | |
} | |
stop_macvlan() { | |
for route in "${ROUTES[@]}"; do | |
ip route del $route dev $MACVLAN_INTERFACE || true | |
done | |
ip link set $MACVLAN_INTERFACE down || true | |
ip addr del $IP_ADDRESS dev $MACVLAN_INTERFACE || true | |
ip link del $MACVLAN_INTERFACE || true | |
} | |
case "$1" in | |
start) | |
start_macvlan | |
;; | |
stop) | |
stop_macvlan | |
;; | |
*) | |
echo "Usage: $0 {start|stop}" | |
exit 1 | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment