From 212ed23d7d3ff5c16dbb5982b3754721ef41981f Mon Sep 17 00:00:00 2001 From: Nikos Skalkotos Date: Wed, 16 Nov 2016 11:25:07 +0200 Subject: [PATCH] Add new DEFAULT_NIC_CONFIG configuration parameter This can be used to define an action for the default NIC that Ganeti will attach to an instance if none of the --net and --no-nics input arguments are defined on the instance creation command. This resolves #90 --- docs/configuration.rst | 17 ++++++++++ snf-image-helper/tasks/50ConfigureNetwork.in | 35 +++++++++++++++----- snf-image-host/common.sh.in | 15 +++++++++ snf-image-host/create | 1 + snf-image-host/defaults.in | 14 ++++++++ 5 files changed, 74 insertions(+), 8 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index 4ddadb2..f565c8e 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -123,6 +123,20 @@ some external programs in ``/etc/default/snf-image``: # card's NETWORK_TAGS variable. # STATELESS_DHCPV6_TAGS="nfdhcpd stateless_dhcpv6" + # DEFAULT_NIC_CONFIG: This option defines the network configuration to be + # performed if there is a default NIC attached to the instance with no further + # information associated with it. This will happen if the user creates an + # instance and does not define any of the --net and --no-nics input arguments. + # In this case Ganeti will create a NIC with a random MAC and set up according + # to the cluster level NIC parameters. The user may want to leave this NIC + # unconfigured (by leaving this option empty), perform "dhcp" or use one of the + # various IPv6 auto configuration methods. The supported IPv6 methods are: + # "dhcpv6" (Stateful DHCPv6), "slaac_dhcp" (Stateless DHCPv6) and "slaac" + # (Stateless Autoconfiguration). IPv4 and IPv6 configuration methods can be + # defined in conjunction using the plus (`+') sign. IPv4 must precede (e.g.: + # "dhcp+slaac_dhcp"). + # DEFAULT_NIC_CONFIG="dhcp" + # UNATTEND: This variable overwrites the unattend.xml file used when deploying # a Windows image. snf-image-helper will use its own unattend.xml file if this # variable is empty. @@ -167,6 +181,9 @@ The most common configuration parameters the user may need to overwrite are: * **PROGRESS_MONITOR**: To specify an executable that will handle the monitoring messages exported by *snf-image* * **DHCP_TAGS**: To specify which Ganeti networks support DHCP + * **DEFAULT_NIC_CONFIG**: To specify a configuration method for the default + NIC Ganeti will attach on instances that were created without using the + *--net* or *--no-nics* input arguments. * **STATELESS_DHCPV6_TAGS**: To specify which Ganeti networks support SLAAC and stateless DHCPv6 * **STATEFUL_DHCPV6_TAGS**: To specify which Ganeti networks support DHCPv6 diff --git a/snf-image-helper/tasks/50ConfigureNetwork.in b/snf-image-helper/tasks/50ConfigureNetwork.in index 2f9e14f..d784a16 100644 --- a/snf-image-helper/tasks/50ConfigureNetwork.in +++ b/snf-image-helper/tasks/50ConfigureNetwork.in @@ -112,28 +112,47 @@ for index in $(seq 0 $((SNF_IMAGE_NIC_COUNT-1))); do ipv6=yes fi - ARGS="-n $index" + ARGS=("-n" "$index") if [ "$ipv4" = yes ]; then if [ "$dhcp" = yes ]; then - ARGS+=" -4 dhcp" + ARGS+=("-4" "dhcp") else - ARGS+=" -4 static" + ARGS+=("-4" "static") fi fi if [ "$ipv6" = yes ]; then if [ "$dhcp6" = stateful ]; then - ARGS+=" -6 dhcp" + ARGS+=("-6" "dhcp") elif [ "$dhcp6" = stateless ]; then - ARGS+=" -6 slaac_dhcp" + ARGS+=("-6" "slaac_dhcp") else - ARGS+=" -6 slaac" + ARGS+=("-6" "slaac") fi fi - echo "Running: $networking_tool $ARGS" - "$networking_tool" $ARGS + if [ $SNF_IMAGE_NIC_COUNT -eq 1 -a "$ipv4" = "no" -a "$ipv6" = "no" ]; then + # This looks like being the default NIC. + keys=(dhcp {dhcp+,}{dhcpv6,slaac,slaac_dhcp}) + values=("-4 dhcp" {"-4 dhcp ",}{"-6 dhcp","-6 slaac","-6 slaac_dhcp"}) + declare -A default_nic_action + for((i=0; i<${#keys[@]}; i++)); do + default_nic_action["${keys[$i]}"]="${values[$i]}" + done + + warn "Detected a default NIC with no further info associated with it." + + if [ -z "$SNF_IMAGE_DEFAULT_NIC_CONFIG" ]; then + warn "No configuration for default NIC is defined" + else + warn "Performing configuration for default NIC: $SNF_IMAGE_DEFAULT_NIC_CONFIG" + ARGS+=(${default_nic_action["$SNF_IMAGE_DEFAULT_NIC_CONFIG"]}) + fi + fi + + echo "Running: $networking_tool ${ARGS[@]}" + "$networking_tool" "${ARGS[@]}" done diff --git a/snf-image-host/common.sh.in b/snf-image-host/common.sh.in index d3237a8..b000e1b 100644 --- a/snf-image-host/common.sh.in +++ b/snf-image-host/common.sh.in @@ -562,6 +562,21 @@ fi : ${DHCP_TAGS:="auto dhcp nfdhcpd"} : ${STATEFUL_DHCPV6_TAGS:="dhcpv6 stateful_dhcpv6"} : ${STATELESS_DHCPV6_TAGS:="nfdhcpd stateless_dhcpv6"} +: ${DEFAULT_NIC_CONFIG:="dhcp"} + +found=no +for val in "" dhcp {dhcp+,}{dhcpv6,slaac,slaac_dhcp}; do + if [ "$DEFAULT_NIC_CONFIG" = "$val" ]; then + found=yes + break + fi +done + +if [ "$found" = "no" ]; then + log_error "DEFAULT_NIC_CONFIG (=\`$DEFAULT_NIC_CONFIG') has invalid value." + log_error "Valid values are: \`'" dhcp {dhcp+,}{dhcpv6,slaac,slaac_dhcp} + exit 1 +fi SCRIPT_NAME=$(basename $0) diff --git a/snf-image-host/create b/snf-image-host/create index 758caef..c67c79c 100755 --- a/snf-image-host/create +++ b/snf-image-host/create @@ -185,6 +185,7 @@ snf_export_HOSTNAME="$instance" snf_export_DHCP_TAGS="$DHCP_TAGS" snf_export_STATEFUL_DHCPV6_TAGS="$STATEFUL_DHCPV6_TAGS" snf_export_STATELESS_DHCPV6_TAGS="$STATELESS_DHCPV6_TAGS" +snf_export_DEFAULT_NIC_CONFIG="$DEFAULT_NIC_CONFIG" if [ -n "${IMG_PASSWD+dummy}" ]; then snf_export_PASSWD="$IMG_PASSWD" diff --git a/snf-image-host/defaults.in b/snf-image-host/defaults.in index 35c8d00..0b6d1e1 100644 --- a/snf-image-host/defaults.in +++ b/snf-image-host/defaults.in @@ -114,6 +114,20 @@ # card's NETWORK_TAGS variable. # STATELESS_DHCPV6_TAGS="nfdhcpd stateless_dhcpv6" +# DEFAULT_NIC_CONFIG: This option defines the network configuration to be +# performed if there is a default NIC attached to the instance with no further +# information associated with it. This will happen if the user creates an +# instance and does not define any of the --net and --no-nics input arguments. +# In this case Ganeti will create a NIC with a random MAC and set up according +# to the cluster level NIC parameters. The user may want to leave this NIC +# unconfigured (by leaving this option empty), perform "dhcp" or use one of the +# various IPv6 auto configuration methods. The supported IPv6 methods are: +# "dhcpv6" (Stateful DHCPv6), "slaac_dhcp" (Stateless DHCPv6) and "slaac" +# (Stateless Autoconfiguration). IPv4 and IPv6 configuration methods can be +# defined in conjunction using the plus (`+') sign. IPv4 must precede (e.g.: +# "dhcp+slaac_dhcp"). +# DEFAULT_NIC_CONFIG="dhcp" + # UNATTEND: This variable overwrites the unattend.xml file used when deploying # a Windows image. snf-image-helper will use its own unattend.xml file if this # variable is empty. -- GitLab