Skip to content

Instantly share code, notes, and snippets.

@mathershifter
Last active July 21, 2022 17:41
Show Gist options
  • Save mathershifter/1bb67f08a100ab5b37e384d45b0dae95 to your computer and use it in GitHub Desktop.
Save mathershifter/1bb67f08a100ab5b37e384d45b0dae95 to your computer and use it in GitHub Desktop.
# Example:
#
# source ceoslab.rc
#
# run-netc ceos1-net
# run-netc ceos2-net
#
# for i in `seq 1 16`
# do
# link-netc ceos1-net ceos2-net eth${i} eth${i}
# done
#
# create-ceos ceoslab:latest ceos1 ceos1-net
# create-ceos ceoslab:latest ceos2 ceos2-net
#
# start-ceos ceos1
# start-ceos ceos2
exit-on-error() {
exit_code=$1
last_command=${@:2}
if [ $exit_code -ne 0 ]; then
>&2 echo "\"${last_command}\" command failed with exit code ${exit_code}."
exit $exit_code
fi
}
start-ceos() {
name=$1
docker start ${name}
}
#
# create-ceos <image> <name> <network-container>
#
create-ceos() {
image=$1
name=$2
netc=$3
flash_cli=""
if [ -z ${4} ]
then
flash_cli="-v ${4}:/mnt/flash"
fi
docker create --name=${name} --net=container:${netc} --privileged \
-e INTFTYPE=eth \
-e ETBA=1 \
-e SKIP_ZEROTOUCH_BARRIER_IN_SYSDBINIT=1 \
-e CEOS=1 \
-e EOS_PLATFORM=ceoslab \
-e container=docker \
-e MGMT_INTF=eth0 \
${flash_cli} \
-i -t ${image} \
/sbin/init systemd.setenv=INTFTYPE=eth \
systemd.setenv=ETBA=1 \
systemd.setenv=SKIP_ZEROTOUCH_BARRIER_IN_SYSDBINIT=1 \
systemd.setenv=CEOS=1 \
systemd.setenv=EOS_PLATFORM=ceoslab \
systemd.setenv=container=docker \
systemd.setenv=MGMT_INTF=eth0
}
# Create and start a cEOS container
#
# run-ceos <image> <name> <network-container>
run-ceos() {
image=$1
name=$2
netc=$3
#startup=$4
create-ceos $image $name $netc
start-ceos $name
}
# destroy-ceos <name>
destroy-ceos() {
name=$1
docker kill ${name}
docker rm ${name}
}
# run-netc <name>
run-netc() {
name=${1}
docker run -d --name=${name} --net=none busybox /bin/init
}
# destroy-netc <name>
destroy-netc() {
name=${1}
docker kill ${name}
docker rm ${name}
}
# link netc <network-container1> <network-container2> <intf1> <intf2>
link-netc() {
netc1=$1
netc2=$2
intf1=$3
intf2=$4
sudo -v
exit-on-error $? !!
_pid1=$(docker inspect --format '{{.State.Pid}}' ${netc1})
ns1=/proc/${_pid1}/ns/net
_pid2=$(docker inspect --format '{{.State.Pid}}' ${netc2})
ns2=/proc/${_pid2}/ns/net
echo Connecting ${netc1}[${ns1}]:${intf1} to ${netc2}[${ns2}]:${intf2}
sudo ip link add ${netc1}-${intf1} type veth peer name ${netc2}-${intf2}
sudo ip link set ${netc1}-${intf1} netns ${ns1} name ${intf1} up
sudo ip link set ${netc2}-${intf2} netns ${ns2} name ${intf2} up
sudo nsenter --net=${ns1} ethtool --offload ${intf1} tx off
sudo nsenter --net=${ns2} ethtool --offload ${intf2} tx off
}
# show-netc <name>
show-netc() {
name=$1
sudo -v
exit-on-error $? !!
docker ps -f name=${name}
sudo ip netns exec ${name} ip addr
}
export -f exit-on-error
export -f create-ceos
export -f start-ceos
export -f run-ceos
export -f destroy-ceos
export -f run-netc
export -f destroy-netc
export -f link-netc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment