Skip to content

Instantly share code, notes, and snippets.

@Xm798
Created December 18, 2023 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xm798/8fc088fe4f56a0f520df5bb3c7862c6d to your computer and use it in GitHub Desktop.
Save Xm798/8fc088fe4f56a0f520df5bb3c7862c6d to your computer and use it in GitHub Desktop.
Setup macvlan
#!/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