Commit 6c41630a authored by Nikos Skalkotos's avatar Nikos Skalkotos
Browse files

Add a new network configuration task

This task is used for configuring the VM's NICs.
parent e9a187a1
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 =
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.
### BEGIN TASK INFO
# Provides: ConfigureNetwork
# RunBefore: EnforcePersonality
# RunAfter: InstallUnattend
# Short-Description: Configure the VM's network interfaces
### END TASK INFO
set -e
. "@commondir@/common.sh"
trap task_cleanup EXIT
report_task_start
# Check if the task should be prevented from running.
check_if_excluded
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
fi
distro=$(get_base_distro "$SNF_IMAGE_TARGET")
if [ ! -f "@networkingdir@/$distro.sh" ]; then
warn "Don't know how to configure the network for this OS"
exit 0
fi
export SNF_IMAGE_TARGET
# Initialize the driver
"@networkingdir@/$distro.sh" -i
for index in $(seq 0 $((NIC_COUNT-1))); do
dhcp=no
dhcp6=no
ipv4=no
ipv6=no
eval export TYPE=\"\$NIC_${index}_FRONTEND_TYPE\"
eval export IP=\"\$NIC_${index}_IP\"
eval export LINK=\"\$NIC_${index}_LINK\"
eval export MAC=\"\$NIC_${index}_MAC\"
eval export MODE=\"\$NIC_${index}_MODE\"
eval export NAME=\"\$NIC_${index}_NAME\"
eval export GATEWAY=\"\$NIC_${index}_NETWORK_GATEWAY\"
eval export GATEWAY6=\"\$NIC_${index}_NETWORK_GATEWAY6\"
eval export NETWORK_NAME=\"\$NIC_${index}_NETWORK_NAME\"
eval export SUBNET=\"\$NIC_${index}_NETWORK_SUBNET\"
eval export SUBNET6=\"\$NIC_${index}_NETWORK_SUBNET6\"
eval export TAGS=\"\$NIC_${index}_NETWORK_TAGS\"
for tag in ${TAGS}; do
for dhcp_tag in $SNF_IMAGE_DHCP_TAGS; do
if [ "$tag" = "$dhcp_tag" ]; then
dhcp=yes
fi
done
for dhcp_tag in $SNF_IMAGE_STATEFUL_DHCPV6_TAGS; do
if [ "$tag" = "$dhcp_tag" ]; then
dhcp6=stateful
fi
done
for dhcp_tag in $SNF_IMAGE_STATELESS_DHCPV6_TAGS; do
if [ "$tag" = "$dhcp_tag" ]; then
dhcp6=stateless
fi
done
done
if [ -n "${IP}" ]; then
ipv4=yes
fi
if [ -n "${GATEWAY6}" ]; then
ipv6=yes
fi
ARGS="-n $index"
if [ "$ipv4" = yes ]; then
if [ "$dhcp" = yes ]; then
ARGS+=" -4 dhcp"
else
ARGS+=" -4 static"
fi
fi
if [ "$ipv6" = yes ]; then
if [ "$dhcp6" = stateful ]; then
ARGS+=" -6 dhcp"
elif [ "$dhcp6" = stateless ]; then
ARGS+=" -6 slaac_dhcp"
else
ARGS+=" -6 slaac"
fi
fi
"@networkingdir@/$distro.sh" $ARGS
done
# Finalize the driver
"@networkingdir@/$distro.sh" -f
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
......@@ -11,6 +11,7 @@ dist_tasks_SCRIPTS = \
40SELinuxAutorelabel \
50AssignHostname \
50ChangePassword \
50ConfigureNetwork \
50FilesystemResizeMounted \
60EnforcePersonality \
80UmountImage
......@@ -20,6 +21,7 @@ edit = sed \
-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'
......
......@@ -230,6 +230,8 @@ create_floppy() {
exit 1
fi
fi
# Put all the NIC info in the floppy
set | egrep ^NIC_ | sed -e 's/^/export /' >> "$target/rules"
umount "$target"
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment