Skip to content

Instantly share code, notes, and snippets.

@sqqqrly
Last active June 20, 2019 17:54
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 sqqqrly/01f3da0b148e835bfac21b8ff82eaed2 to your computer and use it in GitHub Desktop.
Save sqqqrly/01f3da0b148e835bfac21b8ff82eaed2 to your computer and use it in GitHub Desktop.
Create a veth using dummy module without setting an IP
#!/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