]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blobdiff - src/initscripts/helper/aws-setup
cloud: Execute user-data scripts at the end of initialization
[people/pmueller/ipfire-2.x.git] / src / initscripts / helper / aws-setup
index 122e5a31ddd4b4ec778884a39dc948ed27ab9ddd..f14f4eb57894180614e69beaae0e639e70df4e24 100644 (file)
@@ -1,8 +1,33 @@
 #!/bin/bash
+###############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2007-2022  IPFire Team  <info@ipfire.org>                     #
+#                                                                             #
+# 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 3 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, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
 
 . /etc/sysconfig/rc
 . ${rc_functions}
 
+# Set PATH to find our own executables
+export PATH=/usr/local/sbin:/usr/local/bin:${PATH}
+
+# AWS supports an MTU of up to 9001 bytes
+DEFAULT_MTU=9001
+
 get() {
        local file="${1}"
 
@@ -47,22 +72,6 @@ prefix2netmask() {
        to_address "$(( netmask ^ 0xffffffff ))"
 }
 
-find_interface() {
-       local mac="${1}"
-
-       local path
-       for path in /sys/class/net/*; do
-               local address="$(<${path}/address)"
-
-               if [ "${mac}" = "${address}" ]; then
-                       basename "${path}"
-                       return 0
-               fi
-       done
-
-       return 1
-}
-
 import_aws_configuration() {
        local instance_id="$(get meta-data/instance-id)"
 
@@ -109,12 +118,6 @@ import_aws_configuration() {
                fi
        done
 
-       # Download user-data
-       local user_data="$(get user-data)"
-
-       # Import any DNS server settings
-       eval $(/usr/local/bin/readhash <(grep -E "^DNS([0-9])=" /var/ipfire/ethernet/settings 2>/dev/null))
-
        # Import network configuration
        # After this, no network connectivity will be available from this script due to the
        # renaming of the network interfaces for which they have to be shut down
@@ -143,12 +146,10 @@ import_aws_configuration() {
 
                local prefix="${subnet#*/}"
                local netmask="$(prefix2netmask "${prefix}")"
-               local netmask_num="$(to_integer "${netmask}")"
 
                # Calculate the network and broadcast addresses
                local netaddress="${subnet%/*}"
                local netaddress_num="$(to_integer "${netaddress}")"
-               local broadcast="$(to_address $(( ipv4_address_num | (0xffffffff ^ netmask_num) )))"
 
                case "${device_number}" in
                        # RED
@@ -158,10 +159,6 @@ import_aws_configuration() {
                                # The gateway is always the first IP address in the subnet
                                local gateway="$(to_address $(( netaddress_num + 1 )))"
 
-                               # The AWS internal DNS service is available on the second IP address of the VPC
-                               local dns1="$(to_address $(( vpc_netaddress_num + 2 )))"
-                               local dns2=
-
                                (
                                        echo "RED_TYPE=STATIC"
                                        echo "RED_DEV=${interface_name}"
@@ -170,10 +167,8 @@ import_aws_configuration() {
                                        echo "RED_ADDRESS=${ipv4_address}"
                                        echo "RED_NETMASK=${netmask}"
                                        echo "RED_NETADDRESS=${netaddress}"
-                                       echo "RED_BROADCAST=${broadcast}"
+                                       echo "RED_MTU=1500"
                                        echo "DEFAULT_GATEWAY=${gateway}"
-                                       echo "DNS1=${DNS1:-${dns1}}"
-                                       echo "DNS2=${DNS2:-${dns2}}"
                                ) >> /var/ipfire/ethernet/settings
 
                                # Import aliases for RED
@@ -193,7 +188,7 @@ import_aws_configuration() {
                                        echo "GREEN_ADDRESS=${ipv4_address}"
                                        echo "GREEN_NETMASK=${netmask}"
                                        echo "GREEN_NETADDRESS=${netaddress}"
-                                       echo "GREEN_BROADCAST=${broadcast}"
+                                       echo "GREEN_MTU=${DEFAULT_MTU}"
                                ) >> /var/ipfire/ethernet/settings
                                ;;
 
@@ -209,18 +204,10 @@ import_aws_configuration() {
                                        echo "ORANGE_ADDRESS=${ipv4_address}"
                                        echo "ORANGE_NETMASK=${netmask}"
                                        echo "ORANGE_NETADDRESS=${netaddress}"
-                                       echo "ORANGE_BROADCAST=${broadcast}"
+                                       echo "ORANGE_MTU=${DEFAULT_MTU}"
                                ) >> /var/ipfire/ethernet/settings
                                ;;
                esac
-
-               # Rename interface
-               local interface="$(find_interface "${mac}")"
-
-               if [ -n "${interface}" ] && [ -n "${interface_name}" ] && [ "${interface}" != "${interface_name}" ]; then
-                       ip link set "${interface}" down
-                       ip link set "${interface}" name "${interface_name}"
-               fi
        done
 
        # Save CONFIG_TYPE
@@ -228,6 +215,9 @@ import_aws_configuration() {
 
        # Actions performed only on the very first start
        if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
+               # Disable using ISP nameservers
+               sed -e "s/^USE_ISP_NAMESERVERS=.*/USE_ISP_NAMESERVERS=off/" -i /var/ipfire/dns/settings
+
                # Enable SSH
                sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
 
@@ -250,14 +240,24 @@ import_aws_configuration() {
                        echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
                ) >> /var/ipfire/firewall/input
 
-               # This script has now completed the first steps of setup
-               touch /var/ipfire/main/firstsetup_ok
+               # Download user-data
+               local user_data="$(get user-data)"
 
                # Save user-data script to be executed later
                if [ "${user_data:0:2}" = "#!" ]; then
                        echo "${user_data}" > /tmp/aws-user-data.script
                        chmod 700 /tmp/aws-user-data.script
+
+                       # Run the user-data script
+                       local now="$(date -u +"%s")"
+                       /tmp/aws-user-data.script &>/var/log/user-data.log.${now}
+
+                       # Delete the script right away
+                       rm /tmp/aws-user-data.script
                fi
+
+               # This script has now completed the first steps of setup
+               touch /var/ipfire/main/firstsetup_ok
        fi
 
        # All done
@@ -280,13 +280,27 @@ case "${reason}" in
                # Add the default route
                ip route add default via "${new_routers}"
 
+               # Setup DNS
+               for domain_name_server in ${new_domain_name_servers}; do
+                       echo "nameserver ${domain_name_server}"
+               done > /etc/resolv.conf
+
+               # The system is online now
+               touch /var/ipfire/red/active
+
                # Import AWS configuration
                import_aws_configuration
                ;;
 
        EXPIRE|FAIL|RELEASE|STOP)
+               # The system is no longer online
+               rm -f /var/ipfire/red/active
+
                # Remove all IP addresses
                ip addr flush dev "${interface}"
+
+               # Shut down the interface
+               ip link set "${interface}" down
                ;;
 
        *)