Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network macvlan for containers #12

Merged
merged 13 commits into from
Jun 13, 2020
8 changes: 8 additions & 0 deletions createlinks
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ event_actions('nethserver-docker-update', qw(
nethserver-docker-create-network 20
nethserver-docker-create-portainer 20
nethserver-docker-create-aeria 20
nethserver-docker-macvlan-creation 20
));
event_services('nethserver-docker-update', qw(
docker restart
Expand All @@ -49,3 +50,10 @@ event_actions('nethserver-docker-plugin-update', qw(
initialize-default-databases 00
nethserver-docker-upgrade-plugin 20
));

#
# restart docker to create at the end the macvlan0
#
event_actions('interface-update', qw(
nethserver-docker-interface-update-restart-docker 99
));
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

#
# Restart docker after the event interface-update
#

/usr/bin/systemctl restart docker
55 changes: 55 additions & 0 deletions root/etc/e-smith/events/actions/nethserver-docker-macvlan-creation
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash

macVlanNic=$(/usr/sbin/e-smith/db configuration getprop docker macVlanNic)
macVlanGateway=$(/usr/sbin/e-smith/db configuration getprop docker macVlanGateway)
macVlanLocalNetwork=$(/usr/sbin/e-smith/db configuration getprop docker macVlanLocalNetwork)

# Check if macvlan network does not exist, and attempt to create it as necessariy
#

status=$(/sbin/e-smith/config getprop docker status)

if [[ ${status} != enabled ]]; then
exit 0
fi

if ! systemctl is-active -q docker; then
echo "[WARNING] Attempt to start docker"
systemctl start docker
fi


HasNetwork=$(docker network ls -f name=macvlan -q)
if [[ $? != 0 ]]; then
exit 1
fi

if [[ -n ${HasNetwork} ]]; then
exit 0
fi

if [[ -z $macVlanGateway ]]; then
exit 0
fi

if [[ -z $macVlanLocalNetwork ]]; then
exit 0
fi

if [[ -z $macVlanNic ]]; then
exit 0
fi

# We want a bridge to create macvlan on it, we verify before
nicExist=$(/usr/sbin/e-smith/db networks get $macVlanNic)
if [[ -z $nicExist ]];then
exit 0
fi

isBridge=$(/usr/sbin/e-smith/db networks gettype $macVlanNic)
if [[ $isBridge != 'bridge' ]];then
echo "The nic is not a bridge, macvlan cannot be created"
exit 0
fi

/usr/bin/docker network create --driver=macvlan --gateway=${macVlanGateway} --subnet=${macVlanLocalNetwork} -o parent=${macVlanNic} macvlan
5 changes: 5 additions & 0 deletions root/etc/e-smith/templates/etc/shorewall/interfaces/50mac
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

#
# macvl macvlan network
#
macvl macvlan+ optional
7 changes: 7 additions & 0 deletions root/etc/e-smith/templates/etc/shorewall/policy/40mac
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

#
# accept macvl macvlan network from localhost
#

$FW macvl ACCEPT
macvl $FW ACCEPT
6 changes: 6 additions & 0 deletions root/etc/e-smith/templates/etc/shorewall/zones/50mac
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

#
# macvl macvlan
#

macvl ipv4
6 changes: 6 additions & 0 deletions root/etc/rsyslog.d/docker-macvlan.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#
# message log remove martian messages
#

if $programname == "kernel" and ($msg contains "IPv4: martian source" or $msg contains "ll header") then stop

4 changes: 3 additions & 1 deletion root/etc/systemd/system/docker.service.d/nethserver.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[Unit]
After=
After=network-online.target shorewall.service
PartOf=network.service

[Service]
ExecStart=
ExecStart=/usr/bin/dockerd --config-file=/etc/docker/docker.conf
ExecStart=/usr/bin/dockerd --config-file=/etc/docker/docker.conf
ExecStartPost=/usr/libexec/dockerCreateMacVlan0
43 changes: 43 additions & 0 deletions root/usr/libexec/dockerCreateMacVlan0
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

macVlanNetwork=$(/usr/sbin/e-smith/db configuration getprop docker macVlanNetwork)
macVlanNic=$(/usr/sbin/e-smith/db configuration getprop docker macVlanNic)

if [[ -z $macVlanNetwork ]]; then
exit 0
fi

if [[ -z $macVlanNic ]]; then
exit 0
fi

nicExist=$(/usr/sbin/e-smith/db networks get $macVlanNic)
if [[ -z $nicExist ]];then
exit 0
fi

isBridge=$(/usr/sbin/e-smith/db networks gettype $macVlanNic)
if [[ $isBridge != 'bridge' ]];then
echo "The nic is not a bridge, macvlan cannot be created"
exit 0
fi

#
# First delete macvlan0
#
/usr/sbin/ip link delete macvlan0 &> /dev/null

#
# Create macvlan0
#
/usr/sbin/ip link add macvlan0 link $macVlanNic type macvlan mode bridge
/usr/sbin/ip addr add $macVlanNetwork dev macvlan0
/usr/sbin/ip link set macvlan0 up

#
# restart the docker container on macvlan. Needed after network.service restart or interface-update event
#
isMacvlanRunning=$(/usr/bin/docker ps -a -q -f network=macvlan)
if [[ $isMacvlanRunning ]]; then
/usr/bin/docker restart $(/usr/bin/docker ps -a -q -f network=macvlan)
fi