Last active
June 20, 2019 17:54
-
-
Save sqqqrly/01f3da0b148e835bfac21b8ff82eaed2 to your computer and use it in GitHub Desktop.
Create a veth using dummy module without setting an IP
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
#!/usr/bin/env bash | |
# Create a dummy veth. | |
# To add an addr: | |
# ip addr add $addr/24 brd + dev $veth label $veth:0 | |
# To remove $veth: | |
# if addr set: | |
# ip addr del $addr/24 brd + dev eth10 label veth5:0 | |
# ip link delete $veth type dummy | |
# rmmod dummy | |
set -o nounset | |
set -o pipefail | |
#set -o errexit | |
# Report and die. If called from within a function/subshell, this will only | |
# exit the function/subshell, not the script. | |
function die { | |
local msg="$1" | |
(>&2 echo Fatal: "$msg") # Subshell avoids interactions with other redirections | |
kill -SIGPIPE "$$" # exit code: 141 | |
} | |
# Report. Override leader with an optional replacement. | |
function info { | |
local msg="$1" | |
local leader="${2:-==}" | |
(echo "$leader" "$msg") # Subshell avoids interactions with other redirections | |
} | |
function create_veth { | |
local veth="$1" | |
local dum="dummy0" | |
# load dummy mod | |
mod=$(lsmod | grep dummy) | |
if [ -z "$mod" ]; then | |
modprobe dummy || die "cannot modprobe dummy" | |
fi | |
# Create $veth unless found | |
veth_show=$(2>&1 ip link show "$veth") | |
if [[ "$veth_show" =~ "does not exist" ]]; then | |
info "Creating veth [$veth]" | |
ip link add "$dum" type dummy || die "cannot add dummy0" | |
ip link set name "$veth" dev "$dum" || die "cannot add $veth name to $dum" | |
ip link set dev "$veth" up || die "cannot bring $veth up" | |
else | |
info "$veth already exists" | |
fi | |
ip link show "$veth" || die "cannot show link $veth" | |
} | |
create_veth "veth5" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment