Commit dedcfff2 authored by Nikos Skalkotos's avatar Nikos Skalkotos
Browse files

Merge pull request #74 from skalkoto/feature-network-configuration

Feature network configuration
parents 9f03775c 888f2c51
commondir=$(datarootdir)/$(PACKAGE)
networkingdir=$(libdir)/$(PACKAGE)/networking
scriptsdir=$(libdir)/$(PACKAGE)
tasksdir=$(scriptsdir)/tasks
export commondir scriptsdir tasksdir
export commondir networkingdir scriptsdir tasksdir
SUBDIRS = tasks
SUBDIRS = tasks networking
dist_doc_DATA = COPYING AUTHORS
dist_bin_SCRIPTS = snf-image-helper
......@@ -12,6 +13,7 @@ dist_common_DATA = common.sh unattend.xml
edit = sed \
-e 's|@commondir[@]|$(commondir)|g' \
-e 's|@networkingdir[@]|$(networkingdir)|g' \
-e 's|@scriptsdir[@]|$(scriptsdir)|g' \
-e 's|@tasksdir[@]|$(tasksdir)|g'
......
......@@ -506,6 +506,20 @@ bsd2linux() {
log_error "Couldn't find out mapping for BSD partition: \`$partition' in \`$device'"
}
cidr2mask() {
local zeroes=$((32-$1))
local shifts
local IFS=.
shift
for i in 1 2 3 4; do
((shifts = zeroes > 8 ? 8 : zeroes))
zeroes=$((zeroes - shifts))
set $((255 >> shifts << shifts)) "$@"
done
echo "$*"
}
find_mount_target() {
local device
device="$1"
......@@ -841,6 +855,58 @@ return_success() {
send_result_${HYPERVISOR} "SUCCESS"
}
networking_opts() {
local usage="$0 [-i | -f | -n <index>] [-4 (dhcp|static)] [-6 (dhcp|slaac|slaac_dhcp)]"
ipv4=none
ipv6=none
index=
initialize=no
finalize=no
while getopts "h?ifn:4:6:" opt; do
case "$opt" in
h|\?)
echo $usage >&2
exit 0
;;
i) initialize=yes
;;
f) finalize=yes
;;
n) index=$OPTARG
;;
4) ipv4=$OPTARG
;;
6) ipv6=$OPTARG
;;
esac
done
if [ -z "$index" -a "$initialize" = no -a "$finalize" = no ]; then
log_error "Either -i, -f or -n must be specified"
fi
if [ "$initialize" = yes -a "$finalize" = yes ]; then
log_error "-i and -f should not be both specified"
fi
if [[ "$index" && ("$initialize" == yes || "$finalize" == yes) ]]; then
log_error "-n cannot be combined with -i or -f"
fi
if [[ ! "$ipv4" =~ (dhcp|static|none) ]]; then
log_error "invalid ipv4: $ipv4"
fi
if [[ ! "$ipv6" =~ (dhcp|slaac|slaac_dhcp|none) ]]; then
log_error "invalid ipv6: $ipv6"
fi
if [[ (! "$index" =~ ^[0-9]+$) && "$prepare" = no ]]; then
log_error "invalid index: $index"
fi
}
trap cleanup EXIT
set -o pipefail
......
......@@ -61,6 +61,7 @@ fi
AC_CONFIG_FILES([
Makefile
tasks/Makefile
networking/Makefile
])
AC_OUTPUT
......
networkingdir=$(libdir)/$(PACKAGE)/networking
dist_networking_SCRIPTS = debian.sh redhat.sh freebsd.sh openbsd.sh netbsd.sh
edit = sed \
-e 's|@sysconfdir[@]|$(sysconfdir)|g' \
-e 's|@localstatedir[@]|$(localstatedir)|g' \
-e 's|@datarootdir[@]|$(datarootdir)|g' \
-e 's|@commondir[@]|$(commondir)|g' \
-e 's|@networkingdir[@]|$(networkingdir)|g' \
-e 's|@scriptsdir[@]|$(scriptsdir)|g'
%:%.in Makefile
rm -f $@ $@.tmp
srcdir=''; \
test -f ./$@.in || srcdir=$(srcdir)/; \
$(edit) $${srcdir}$@.in >$@.tmp
mv $@.tmp $@
CLEANFILES = $(dist_networking_SCRIPTS)
#! /bin/bash
# Copyright (C) 2015 GRNET S.A.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
set -e
. "@commondir@/common.sh"
INTERFACES="$SNF_IMAGE_TARGET/etc/network/interfaces"
networking_opts "$@"
# initialize
if [ "$initialize" = yes ]; then
if [ -f "$INTERFACES" ]; then
mv "$INTERFACES" "${INTERFACES}.snf-image-$RANDOM"
fi
cat > "$INTERFACES" <<EOF
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# Entries were automatically added by snf-image's ConfigureNetwork task.
# The loopback network interface
auto lo
iface lo inet loopback
EOF
exit 0
fi
# finalize
if [ "$finalize" = yes ]; then
exit 0
fi
nic="auto eth${index}\n"
nic+="allow-hotplug eth${index}\n"
if [ "$ipv4" = "dhcp" ]; then
nic+="iface eth${index} inet dhcp\n"
elif [ "$ipv4" = "static" ]; then
netmask="$(cidr2mask "${SUBNET##*/}")"
nic+="iface eth${index} inet static\n"
nic+=" address $IP\n"
nic+=" netmask $netmask\n"
if [ -n "$GATEWAY" ]; then
nic+=" gateway $GATEWAY\n"
fi
fi
if [ "$ipv6" = "dhcp" ]; then
nic+="iface eth${index} inet6 dhcp\n"
elif [ "$ipv6" = "slaac" ]; then
nic+="iface eth${index} inet6 auto\n"
nic+=" privext 0\n"
elif [ "$ipv6" = "slaac_dhcp" ]; then
nic+="iface eth${index} inet6 auto\n"
nic+=" privext 0\n"
nic+=" dhcp 1\n"
fi
echo -e "$nic" >> "$INTERFACES"
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
#! /bin/bash
# Copyright (C) 2015 GRNET S.A.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
set -e
. "@commondir@/common.sh"
CONF="$SNF_IMAGE_TARGET/etc/rc.conf"
networking_opts "$@"
if [ "$initialize" = yes ]; then
cat "$CONF" > "$CONF.snf_image-$(date +%Y%m%d%H%M%S)-$RANDOM"
exit 0
fi
if [ "$finalize" = yes ]; then
exit 0
fi
case "$TYPE" in
paravirtual)
driver=vtnet
driver_index=$paravirtual_index
;;
rtl8139)
driver=rl
driver_index=$rtl8139_index
;;
ne2k_isa|ne2k_pci)
driver=ed
driver_index=$((ne2k_isa_index + ne2k_pci_index))
;;
i82551|i82557b|i82559er)
driver=fxp
driver_index=$((i82551_index + i82557b_index + i82559er_index))
;;
pcnet)
driver=pcn
driver_index=$pcnet_index
;;
e1000)
driver=em
dirver_index=$e1000_index
;;
*)
log_error "Unknown card type: \`$TYPE'"
esac
index=$((driver_index - 1))
tmp_config=$(mktemp)
add_cleanup rm -f "$tmp_config"
grep -v "ifconfig_$driver$index" "$CONF" > "$tmp_config"
cat "$tmp_config" > "$CONF"
if [ "$ipv4" = 'static' ]; then
echo "ifconfig_$driver$index=\"inet $IP netmask $(cidr2mask "${SUBNET##*/}")\"" >> "$CONF"
if [ -n "$GATEWAY" ]; then
sed -i '/^defaultrouter=/d' "$CONF"
echo "defaultrouter=\"$GATEWAY\"" >> "$CONF"
fi
elif [ "$ipv4" = 'dhcp' ]; then
echo "ifconfig_$driver$index=\"DHCP\"" >> "$CONF"
fi
if [ "$ipv6" != 'none' ]; then
sed -i '/^ipv6_enable=/d' "$CONF"
echo 'ipv6_enable="YES"' >> "$CONF"
fi
if [ "$ipv6" = 'dhcp' ]; then
echo "ifconfig_$driver${index}_ipv6=\"inet6 -ifdisabled accept_rtadv\"" >> $CONF
echo -e "dhcp6c_enable=\"YES\"\ndhcp6c_interfaces=\"$driver$index\"" >> $CONF
elif [ "$ipv6" = 'slaac' -o "$ipv6" = 'slaac_dhcp' ]; then
echo "ifconfig_$driver${index}_ipv6=\"inet6 accept_rtadv\"" >> $CONF
sed -i '/^rtsold_enable=/d' "$CONF"
echo 'rtsold_enable="YES"' >> "$CONF"
fi
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
#! /bin/bash
# Copyright (C) 2015 GRNET S.A.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
set -e
. "@commondir@/common.sh"
networking_opts "$@"
if [ "$initialize" = yes ]; then
exit 0
fi
if [ "$finalize" = yes ]; then
exit 0
fi
case "$TYPE" in
paravirtual)
driver=vioif
driver_index=$paravirtual_index
;;
rtl8139)
driver=re
driver_index=$rtl8139_index
;;
ne2k_isa|ne2k_pci)
driver=ne
driver_index=$((ne2k_isa_index + ne2k_pci_index))
;;
i82551|i82557b|i82559er)
driver=fxp
driver_index=$((i82551_index + i82557b_index + i82559er_index))
;;
pcnet)
driver=pcn
driver_index=$pcnet_index
;;
e1000)
driver=wm
driver_index=$e1000_index
;;
*)
log_error "Unknown card type: \`$TYPE'"
esac
index=$((driver_index - 1))
IFCONFIG="$SNF_IMAGE_TARGET/etc/ifconfig.$driver$index"
RC_CONF="$SNF_IMAGE_TARGET/etc/rc.conf"
SYSCTL="$SNF_IMAGE_TARGET/etc/sysctl.conf"
cat /dev/null > "$IFCONFIG"
if [ "$ipv4" != none -o "$ipv6" != none ]; then
echo up >> "$IFCONFIG"
fi
if [ "$ipv4" = 'static' ]; then
echo "inet $IP netmask $(cidr2mask "${SUBNET##*/}")" >> "$IFCONFIG"
if [ -n "$GATEWAY" ]; then
sed -i '/^defaultroute=/d' "$RC_CONF"
echo "defaultroute=$GATEWAY" >> "$RC_CONF"
fi
elif [ "$ipv4" = 'dhcp' ]; then
echo '!dhclient $int' >> "$IFCONFIG"
fi
if [ "$ipv6" = 'slaac' -o "$ipv6" = 'slaac_dhcp' ]; then
sed -i '/^ip6mode=/d' "$RC_CONF"
echo 'ip6mode=autohost' >> "$RC_CONF"
echo '!rtsol $int' >> "$IFCONFIG"
elif [ "$ipv6" = 'dhcp' ]; then
warn "DHCPv6 needs packages from Ports. Not implementing it right now."
fi
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
#! /bin/bash
# Copyright (C) 2015 GRNET S.A.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
set -e
. "@commondir@/common.sh"
networking_opts "$@"
if [ "$initialize" = yes ]; then
exit 0
fi
if [ "$finalize" = yes ]; then
exit 0
fi
case "$TYPE" in
paravirtual)
driver=vio
driver_index=$paravirtual_index
;;
rtl8139)
driver=rl
driver_index=$rtl8139_index
;;
ne2k_isa|ne2k_pci)
driver=ne
driver_index=$((ne2k_isa_index + ne2k_pci_index))
;;
i82551|i82557b|i82559er)
driver=fxp
driver_index=$((i82551_index + i82557b_index + i82559er_index))
;;
pcnet)
driver=pcn
driver_index=$pcnet_index
;;
e1000)
driver=em
driver_index=$e1000_index
;;
*)
log_error "Unknown card type: \`$TYPE'"
esac
index=$((driver_index - 1))
HOSTNAME="$SNF_IMAGE_TARGET/etc/hostname.$driver$index"
MYGATE="$SNF_IMAGE_TARGET/etc/mygate"
SYSCTL="$SNF_IMAGE_TARGET/etc/sysctl.conf"
cat /dev/null > "$HOSTNAME"
if [ "$ipv4" = 'static' ]; then
echo "inet $IP $(cidr2mask "${SUBNET##*/}") NONE" >> "$HOSTNAME"
if [ -n "$GATEWAY" ]; then
echo "$GATEWAY" > "$MYGATE"
fi
elif [ "$ipv4" = 'dhcp' ]; then
echo "dhcp" >> "$HOSTNAME"
fi
if [ "$ipv6" != 'none' ]; then
sed -i '/^net\.inet6\.icmp6\.rediraccept=/d' "$SYSCTL"
echo 'net.inet6.icmp6.rediraccept=1' >> "$SYSCTL"
sed -i '/net\.inet6\.ip6\.accept_rtadv=/d' "$SYSCTL"
echo 'net.inet6.ip6.accept_rtadv=1' >> "$SYSCTL"
fi
if [ "$ipv6" = 'dhcp' ]; then
warn "DHCPv6 needs packages from Ports. Not implementing it right now."
elif [ "$ipv6" = 'slaac' -o "$ipv6" = 'slaac_dhcp' ]; then
echo "rtsol -autoconfprivacy" >> "$HOSTNAME"
fi
if [ "$ipv4" != 'none' -o "$ipv6" != 'none' ]; then
echo 'up' >> "$HOSTNAME"
fi
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
#! /bin/bash
# Copyright (C) 2015 GRNET S.A.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
set -e
. "@commondir@/common.sh"
networking_opts "$@"
tmp_network=$(mktemp)
add_cleanup rm -f "$tmp_network"
NETWORK="$SNF_IMAGE_TARGET/etc/sysconfig/network"
if [ "$initialize" = yes ]; then
echo -e "# Edited by snf-image" > "$tmp_network"
cat "$NETWORK" >> "$tmp_network"
cat "$tmp_network" > "$NETWORK"
exit 0
fi
if [ "$finalize" = yes ]; then
exit 0
fi
IFCFG="$SNF_IMAGE_TARGET/etc/sysconfig/network-scripts/ifcfg-eth${index}"
regexp='^NETWORKING='
new_entries="NETWORKING=yes\n"
if [ "$ipv6" != none ]; then
regexp+='|^NETWORKING_IPV6='
new_entries+="NETWORKING_IPV6=yes\n"
fi
if [ "$ipv4" = 'static' -a -n "$GATEWAY" ]; then
regexp+='|^GATEWAY=|^GATEWAYDEV='
new_entries+="GATEWAY=$GATEWAY\nGATEWAYDEV=eth${index}\n"
fi
touch "$NETWORK"
egrep -v $regexp "$NETWORK" > "$tmp_network"
echo -e "$new_entries" >> "$tmp_network"
cat "$tmp_network" > "$NETWORK"
if [ -f "$IFCFG" ]; then
sed 's/^/#/' "$IFCFG" > "$IFCFG.snf_image-$(date +%Y%m%d%H%M%S)-$RANDOM"
fi
echo -e "DEVICE=eth${index}\nONBOOT=yes\nHWADDR=$MAC" > "$IFCFG"
# If Network Manager is running, let it control the interface
echo "NM_CONTROLLED=yes" >> "$IFCFG"
if [ "$ipv4" = 'static' ]; then
echo -e "BOOTPROTO=static\nIPADDR=$IP\nNETMASK=$(cidr2mask "${SUBNET##*/}")" >> "$IFCFG"
elif [ "$ipv4" = 'dhcp' ]; then
echo -e "BOOTPROTO=dhcp" >> "$IFCFG"
else
echo -e "IPV4_FAILURE_FATAL=no" >> "$IFCFG"
fi
if [ "$ipv6" = 'dhcp' ]; then
echo -e "IPV6INIT=yes\nDHCPV6C=yes" >> "$IFCFG"
elif [ "$ipv6" = 'slaac' ]; then
echo -e "IPV6INIT=yes\nIPV6_AUTOCONF=yes" >> "$IFCFG"
elif [ "$ipv6" = 'slaac_dhcp' ]; then
echo -e "IPV6INIT=yes\nIPV6_AUTOCONF=yes\nDHCPV6C=yes\nDHCPV6C_OPTIONS=\"-S\"" >> "$IFCFG"
else
echo -e "IPV6_FAULURE_FATAL=no" >> "$IFCFG"
fi
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
#! /bin/bash
# Copyright (C) 2015 GRNET S.A.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.